OCNetworking.m 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. //
  2. // OCnetworking.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "OCNetworking.h"
  24. #import "CCUtility.h"
  25. #import "CCGraphics.h"
  26. #import "NSString+Encode.h"
  27. #import "NCBridgeSwift.h"
  28. #import "NCXMLGetAppPasswordParser.h"
  29. @implementation OCNetworking
  30. + (OCNetworking *)sharedManager {
  31. static OCNetworking *sharedManager;
  32. @synchronized(self)
  33. {
  34. if (!sharedManager) {
  35. sharedManager = [OCNetworking new];
  36. sharedManager.checkRemoteUserInProgress = false;
  37. }
  38. return sharedManager;
  39. }
  40. }
  41. - (id)init
  42. {
  43. self = [super init];
  44. [self sharedOCCommunication];
  45. return self;
  46. }
  47. #pragma --------------------------------------------------------------------------------------------
  48. #pragma mark ===== OCCommunication =====
  49. #pragma --------------------------------------------------------------------------------------------
  50. - (OCCommunication *)sharedOCCommunication
  51. {
  52. static OCCommunication* sharedOCCommunication = nil;
  53. if (sharedOCCommunication == nil)
  54. {
  55. // Network
  56. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  57. configuration.allowsCellularAccess = YES;
  58. configuration.discretionary = NO;
  59. configuration.HTTPMaximumConnectionsPerHost = k_maxConcurrentOperation;
  60. configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  61. OCURLSessionManager *networkSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configuration];
  62. [networkSessionManager.operationQueue setMaxConcurrentOperationCount: k_maxConcurrentOperation];
  63. networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  64. // Download
  65. NSURLSessionConfiguration *configurationDownload = [NSURLSessionConfiguration defaultSessionConfiguration];
  66. configurationDownload.allowsCellularAccess = YES;
  67. configurationDownload.discretionary = NO;
  68. configurationDownload.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  69. configurationDownload.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  70. configurationDownload.timeoutIntervalForRequest = k_timeout_upload;
  71. OCURLSessionManager *downloadSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configurationDownload];
  72. [downloadSessionManager.operationQueue setMaxConcurrentOperationCount:k_maxHTTPConnectionsPerHost];
  73. [downloadSessionManager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
  74. return NSURLSessionAuthChallengePerformDefaultHandling;
  75. }];
  76. // Upload
  77. NSURLSessionConfiguration *configurationUpload = [NSURLSessionConfiguration defaultSessionConfiguration];
  78. configurationUpload.allowsCellularAccess = YES;
  79. configurationUpload.discretionary = NO;
  80. configurationUpload.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  81. configurationUpload.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  82. configurationUpload.timeoutIntervalForRequest = k_timeout_upload;
  83. OCURLSessionManager *uploadSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configurationUpload];
  84. [uploadSessionManager.operationQueue setMaxConcurrentOperationCount:k_maxHTTPConnectionsPerHost];
  85. [uploadSessionManager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
  86. return NSURLSessionAuthChallengePerformDefaultHandling;
  87. }];
  88. sharedOCCommunication = [[OCCommunication alloc] initWithUploadSessionManager:uploadSessionManager andDownloadSessionManager:downloadSessionManager andNetworkSessionManager:networkSessionManager];
  89. }
  90. return sharedOCCommunication;
  91. }
  92. #pragma --------------------------------------------------------------------------------------------
  93. #pragma mark ===== Share =====
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (void)readShareWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  96. {
  97. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  98. if (tableAccount == nil) {
  99. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  100. } else if ([CCUtility getPassword:account].length == 0) {
  101. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  102. } else if ([CCUtility getCertificateError:account]) {
  103. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  104. }
  105. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  106. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  107. [communication setUserAgent:[CCUtility getUserAgent]];
  108. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  109. completion(account, items, nil, 0);
  110. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  111. NSString *message;
  112. NSInteger errorCode = response.statusCode;
  113. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  114. errorCode = error.code;
  115. // Error
  116. if (errorCode == 503) {
  117. message = NSLocalizedString(@"_server_error_retry_", nil);
  118. } else {
  119. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  120. }
  121. completion(account, nil, message, errorCode);
  122. }];
  123. }
  124. - (void)readShareWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  125. {
  126. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  127. if (tableAccount == nil) {
  128. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  129. } else if ([CCUtility getPassword:account].length == 0) {
  130. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  131. } else if ([CCUtility getCertificateError:account]) {
  132. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  133. }
  134. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  135. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  136. [communication setUserAgent:[CCUtility getUserAgent]];
  137. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] andPath:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  138. completion(account, listOfShared, nil, 0);
  139. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  140. NSString *message;
  141. NSInteger errorCode = response.statusCode;
  142. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  143. errorCode = error.code;
  144. // Error
  145. if (errorCode == 503) {
  146. message = NSLocalizedString(@"_server_error_retry_", nil);
  147. } else {
  148. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  149. }
  150. completion(account, nil, message, errorCode);
  151. }];
  152. }
  153. - (void)shareWithAccount:(NSString *)account fileName:(NSString *)fileName password:(NSString *)password permission:(NSInteger)permission hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  154. {
  155. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  156. if (tableAccount == nil) {
  157. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  158. } else if ([CCUtility getPassword:account].length == 0) {
  159. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  160. } else if ([CCUtility getCertificateError:account]) {
  161. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  162. }
  163. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  164. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  165. [communication setUserAgent:[CCUtility getUserAgent]];
  166. [communication shareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPassword:[password encodeString:NSUTF8StringEncoding] andPermission:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  167. completion(account, nil, 0);
  168. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  169. NSString *message;
  170. NSInteger errorCode = response.statusCode;
  171. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  172. errorCode = error.code;
  173. // Error
  174. if (errorCode == 503) {
  175. message = NSLocalizedString(@"_server_error_retry_", nil);
  176. } else {
  177. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  178. }
  179. completion(account, message, errorCode);
  180. }];
  181. }
  182. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  183. - (void)shareUserGroupWithAccount:(NSString *)account userOrGroup:(NSString *)userOrGroup fileName:(NSString *)fileName permission:(NSInteger)permission shareeType:(NSInteger)shareeType completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  184. {
  185. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  186. if (tableAccount == nil) {
  187. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  188. } else if ([CCUtility getPassword:account].length == 0) {
  189. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  190. } else if ([CCUtility getCertificateError:account]) {
  191. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  192. }
  193. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  194. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  195. [communication setUserAgent:[CCUtility getUserAgent]];
  196. [communication shareWith:userOrGroup shareeType:shareeType inServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPermissions:permission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  197. completion(account, nil, 0);
  198. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  199. NSString *message;
  200. NSInteger errorCode = response.statusCode;
  201. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  202. errorCode = error.code;
  203. // Error
  204. if (errorCode == 503) {
  205. message = NSLocalizedString(@"_server_error_retry_", nil);
  206. } else {
  207. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  208. }
  209. completion(account, message, errorCode);
  210. }];
  211. }
  212. - (void)shareUpdateAccount:(NSString *)account shareID:(NSInteger)shareID password:(NSString *)password note:(NSString *)note permission:(NSInteger)permission expirationTime:(NSString *)expirationTime hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  213. {
  214. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  215. if (tableAccount == nil) {
  216. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  217. } else if ([CCUtility getPassword:account].length == 0) {
  218. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  219. } else if ([CCUtility getCertificateError:account]) {
  220. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  221. }
  222. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  223. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  224. [communication setUserAgent:[CCUtility getUserAgent]];
  225. [communication updateShare:shareID ofServerPath:[tableAccount.url stringByAppendingString:@"/"] withPasswordProtect:[password encodeString:NSUTF8StringEncoding] andNote:note andExpirationTime:expirationTime andPermissions:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  226. completion(account, nil, 0);
  227. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  228. NSString *message;
  229. NSInteger errorCode = response.statusCode;
  230. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  231. errorCode = error.code;
  232. // Error
  233. if (errorCode == 503) {
  234. message = NSLocalizedString(@"_server_error_retry_", nil);
  235. } else {
  236. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  237. }
  238. completion(account, message, errorCode);
  239. }];
  240. }
  241. - (void)unshareAccount:(NSString *)account shareID:(NSInteger)shareID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  242. {
  243. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  244. if (tableAccount == nil) {
  245. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  246. } else if ([CCUtility getPassword:account].length == 0) {
  247. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  248. } else if ([CCUtility getCertificateError:account]) {
  249. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  250. }
  251. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  252. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  253. [communication setUserAgent:[CCUtility getUserAgent]];
  254. [communication unShareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andIdRemoteShared:shareID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  255. completion(account, nil, 0);
  256. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  257. NSString *message;
  258. NSInteger errorCode = response.statusCode;
  259. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  260. errorCode = error.code;
  261. // Error
  262. if (errorCode == 503) {
  263. message = NSLocalizedString(@"_server_error_retry_", nil);
  264. } else {
  265. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  266. }
  267. completion(account, message, errorCode);
  268. }];
  269. }
  270. - (void)getUserGroupWithAccount:(NSString *)account searchString:(NSString *)searchString completion:(void (^)(NSString *account, NSArray *item, NSString *message, NSInteger errorCode))completion
  271. {
  272. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  273. if (tableAccount == nil) {
  274. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  275. } else if ([CCUtility getPassword:account].length == 0) {
  276. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  277. } else if ([CCUtility getCertificateError:account]) {
  278. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  279. }
  280. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  281. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  282. [communication setUserAgent:[CCUtility getUserAgent]];
  283. [communication searchUsersAndGroupsWith:searchString forPage:1 with:50 ofServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  284. completion(account, itemList, nil, 0);
  285. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  286. NSString *message;
  287. NSInteger errorCode = response.statusCode;
  288. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  289. errorCode = error.code;
  290. // Error
  291. if (errorCode == 503) {
  292. message = NSLocalizedString(@"_server_error_retry_", nil);
  293. } else {
  294. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  295. }
  296. completion(account, nil, message, errorCode);
  297. }];
  298. }
  299. #pragma --------------------------------------------------------------------------------------------
  300. #pragma mark ===== VAR =====
  301. #pragma --------------------------------------------------------------------------------------------
  302. - (void)getNotificationWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  303. {
  304. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  305. if (tableAccount == nil) {
  306. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  307. } else if ([CCUtility getPassword:account].length == 0) {
  308. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  309. } else if ([CCUtility getCertificateError:account]) {
  310. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  311. }
  312. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  313. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  314. [communication setUserAgent:[CCUtility getUserAgent]];
  315. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  316. completion(account, listOfNotifications, nil, 0);
  317. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  318. NSString *message;
  319. NSInteger errorCode = response.statusCode;
  320. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  321. errorCode = error.code;
  322. // Server Unauthorized
  323. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  324. #ifndef EXTENSION
  325. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  326. #endif
  327. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  328. [CCUtility setCertificateError:account error:YES];
  329. }
  330. // Error
  331. if (errorCode == 503) {
  332. message = NSLocalizedString(@"_server_error_retry_", nil);
  333. } else {
  334. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  335. }
  336. completion(account, nil, message, errorCode);
  337. }];
  338. }
  339. - (void)setNotificationWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  340. {
  341. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  342. if (tableAccount == nil) {
  343. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  344. } else if ([CCUtility getPassword:account].length == 0) {
  345. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  346. } else if ([CCUtility getCertificateError:account]) {
  347. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  348. }
  349. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  350. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  351. [communication setUserAgent:[CCUtility getUserAgent]];
  352. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  353. completion(account, nil, 0);
  354. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  355. NSString *message;
  356. NSInteger errorCode = response.statusCode;
  357. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  358. errorCode = error.code;
  359. // Server Unauthorized
  360. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  361. #ifndef EXTENSION
  362. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  363. #endif
  364. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  365. [CCUtility setCertificateError:account error:YES];
  366. }
  367. // Error
  368. if (errorCode == 503) {
  369. message = NSLocalizedString(@"_server_error_retry_", nil);
  370. } else {
  371. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  372. }
  373. completion(account, message, errorCode);
  374. }];
  375. }
  376. #pragma --------------------------------------------------------------------------------------------
  377. #pragma mark ===== Push Notification =====
  378. #pragma --------------------------------------------------------------------------------------------
  379. - (void)subscribingPushNotificationWithAccount:(NSString *)account url:(NSString *)url pushToken:(NSString *)pushToken Hash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey completion:(void(^)(NSString *account, NSString *deviceIdentifier, NSString *deviceIdentifierSignature, NSString *publicKey, NSString *message, NSInteger errorCode))completion
  380. {
  381. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  382. if (tableAccount == nil) {
  383. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  384. } else if ([CCUtility getPassword:account].length == 0) {
  385. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  386. } else if ([CCUtility getCertificateError:account]) {
  387. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  388. }
  389. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  390. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  391. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  392. #ifdef DEBUG
  393. // proxyServerPath = @"http://127.0.0.1:8088";
  394. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  395. #endif
  396. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  397. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  398. [communication setUserAgent:[CCUtility getUserAgent]];
  399. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: proxyServerPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  400. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  401. signature = [CCUtility URLEncodeStringFromString:signature];
  402. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  403. [communication subscribingPushProxy:proxyServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  404. completion(account, deviceIdentifier, signature, publicKey, nil, 0);
  405. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  406. NSString *message;
  407. NSInteger errorCode = response.statusCode;
  408. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  409. errorCode = error.code;
  410. // Error
  411. if (errorCode == 503)
  412. message = NSLocalizedString(@"_server_error_retry_", nil);
  413. else
  414. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  415. completion(account, nil, nil, nil, message, errorCode);
  416. }];
  417. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  418. NSString *message;
  419. NSInteger errorCode = response.statusCode;
  420. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  421. errorCode = error.code;
  422. // Error
  423. if (errorCode == 503)
  424. message = NSLocalizedString(@"_server_error_retry_", nil);
  425. else
  426. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  427. completion(account, nil, nil, nil, message, errorCode);
  428. }];
  429. }
  430. - (void)unsubscribingPushNotificationWithAccount:(NSString *)account url:(NSString *)url deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey completion:(void (^)(NSString *account ,NSString *message, NSInteger errorCode))completion {
  431. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  432. if (tableAccount == nil) {
  433. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  434. } else if ([CCUtility getPassword:account].length == 0) {
  435. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  436. } else if ([CCUtility getCertificateError:account]) {
  437. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  438. }
  439. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  440. #ifdef DEBUG
  441. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  442. #endif
  443. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  444. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  445. [communication setUserAgent:[CCUtility getUserAgent]];
  446. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  447. [communication unsubscribingPushProxy:proxyServer deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  448. completion(account, nil, 0);
  449. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  450. NSString *message;
  451. NSInteger errorCode = response.statusCode;
  452. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  453. errorCode = error.code;
  454. // Error
  455. if (errorCode == 503)
  456. message = NSLocalizedString(@"_server_error_retry_", nil);
  457. else
  458. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  459. completion(account, message, errorCode);
  460. }];
  461. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  462. NSString *message;
  463. NSInteger errorCode = response.statusCode;
  464. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  465. errorCode = error.code;
  466. // Error
  467. if (errorCode == 503)
  468. message = NSLocalizedString(@"_server_error_retry_", nil);
  469. else
  470. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  471. completion(account, message, errorCode);
  472. }];
  473. }
  474. - (void)getServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSDictionary*jsongParsed, NSString *message, NSInteger errorCode))completion
  475. {
  476. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld?format=json", serverUrl, (long)notificationId];
  477. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  478. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  479. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  480. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  481. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  482. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  483. if (error) {
  484. NSString *message;
  485. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  486. NSInteger errorCode = httpResponse.statusCode;
  487. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  488. errorCode = error.code;
  489. // Error
  490. if (errorCode == 503)
  491. message = NSLocalizedString(@"_server_error_retry_", nil);
  492. else
  493. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  494. completion(nil, message, errorCode);
  495. } else {
  496. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  497. completion(jsongParsed, nil, 0);
  498. }
  499. }];
  500. [task resume];
  501. }
  502. - (void)deletingServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSString *message, NSInteger errorCode))completion
  503. {
  504. // NSData *authData = [[NSString stringWithFormat:@"%@:%@", tableAccount.user, [CCUtility getPassword:tableAccount.account]] dataUsingEncoding:NSUTF8StringEncoding];
  505. // NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  506. // Delete
  507. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld", serverUrl, (long)notificationId];
  508. // Delete-all
  509. if (notificationId == 0) {
  510. URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications", serverUrl];
  511. }
  512. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  513. // [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  514. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  515. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  516. [request setHTTPMethod: @"DELETE"];
  517. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  518. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  519. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  520. if (error) {
  521. NSString *message;
  522. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  523. NSInteger errorCode = httpResponse.statusCode;
  524. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  525. errorCode = error.code;
  526. // Error
  527. if (errorCode == 503)
  528. message = NSLocalizedString(@"_server_error_retry_", nil);
  529. else
  530. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  531. completion(message, errorCode);
  532. } else {
  533. completion(nil, 0);
  534. }
  535. }];
  536. [task resume];
  537. }
  538. #pragma --------------------------------------------------------------------------------------------
  539. #pragma mark ===== Trash =====
  540. #pragma --------------------------------------------------------------------------------------------
  541. - (void)listingTrashWithAccount:(NSString *)account path:(NSString *)path serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  542. {
  543. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  544. if (tableAccount == nil) {
  545. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  546. } else if ([CCUtility getPassword:account].length == 0) {
  547. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  548. } else if ([CCUtility getCertificateError:account]) {
  549. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  550. }
  551. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  552. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  553. [communication setUserAgent:[CCUtility getUserAgent]];
  554. [communication listingTrash:[serverUrl stringByAppendingString:path] depth:depth onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  555. // Check items > 0
  556. if ([items count] == 0) {
  557. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  558. completion(account, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  559. } else {
  560. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  561. NSMutableArray *listTrash = [NSMutableArray new];
  562. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  563. if ([items count] > 1) {
  564. for (NSUInteger i=1; i < [items count]; i++) {
  565. OCFileDto *itemDto = [items objectAtIndex:i];
  566. tableTrash *trash = [tableTrash new];
  567. trash.account = account;
  568. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  569. trash.directory = itemDto.isDirectory;
  570. trash.fileId = itemDto.fileId;
  571. trash.fileName = itemDto.fileName;
  572. NSArray *array = [itemDto.filePath componentsSeparatedByString:path];
  573. long len = [[array objectAtIndex:0] length];
  574. trash.filePath = [itemDto.filePath substringFromIndex:len];
  575. trash.hasPreview = itemDto.hasPreview;
  576. trash.size = itemDto.size;
  577. trash.trashbinFileName = itemDto.trashbinFileName;
  578. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  579. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  580. NSDictionary *results = [[NCCommunicationCommon shared] objcGetInternalContenTypeWithFileName:trash.trashbinFileName contentType:@"" directory:itemDto.isDirectory];
  581. trash.contentType = results[@"contentType"];
  582. trash.iconName = results[@"iconName"];
  583. trash.typeFile = results[@"typeFile"];
  584. [listTrash addObject:trash];
  585. }
  586. }
  587. dispatch_async(dispatch_get_main_queue(), ^{
  588. completion(account, listTrash, nil, 0);
  589. });
  590. });
  591. }
  592. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  593. NSString *message;
  594. NSInteger errorCode = response.statusCode;
  595. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  596. errorCode = error.code;
  597. // Error
  598. if (errorCode == 503)
  599. message = NSLocalizedString(@"_server_error_retry_", nil);
  600. else
  601. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  602. completion(account, nil,message, errorCode);
  603. }];
  604. }
  605. - (void)emptyTrashWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  606. {
  607. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  608. if (tableAccount == nil) {
  609. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  610. } else if ([CCUtility getPassword:account].length == 0) {
  611. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  612. } else if ([CCUtility getCertificateError:account]) {
  613. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  614. }
  615. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", tableAccount.url, k_dav, tableAccount.userID];
  616. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  617. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  618. [communication setUserAgent:[CCUtility getUserAgent]];
  619. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  620. completion(account, nil, 0);
  621. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  622. NSString *message;
  623. NSInteger errorCode = response.statusCode;
  624. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  625. errorCode = error.code;
  626. // Error
  627. if (errorCode == 503)
  628. message = NSLocalizedString(@"_server_error_retry_", nil);
  629. else
  630. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  631. completion(account, message, errorCode);
  632. }];
  633. }
  634. #pragma --------------------------------------------------------------------------------------------
  635. #pragma mark ===== Comments =====
  636. #pragma --------------------------------------------------------------------------------------------
  637. - (void)getCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  638. {
  639. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  640. if (tableAccount == nil) {
  641. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  642. } else if ([CCUtility getPassword:account].length == 0) {
  643. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  644. } else if ([CCUtility getCertificateError:account]) {
  645. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  646. }
  647. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  648. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  649. [communication setUserAgent:[CCUtility getUserAgent]];
  650. [communication getComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *list, NSString *redirectedServer) {
  651. completion(account, list, nil, 0);
  652. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  653. NSString *message;
  654. NSInteger errorCode = response.statusCode;
  655. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  656. errorCode = error.code;
  657. // Error
  658. if (errorCode == 503)
  659. message = NSLocalizedString(@"_server_error_retry_", nil);
  660. else
  661. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  662. completion(account, nil,message, errorCode);
  663. }];
  664. }
  665. - (void)putCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  666. {
  667. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  668. if (tableAccount == nil) {
  669. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  670. } else if ([CCUtility getPassword:account].length == 0) {
  671. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  672. } else if ([CCUtility getCertificateError:account]) {
  673. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  674. }
  675. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  676. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  677. [communication setUserAgent:[CCUtility getUserAgent]];
  678. [communication putComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  679. completion(account, nil, 0);
  680. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  681. NSString *message;
  682. NSInteger errorCode = response.statusCode;
  683. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  684. errorCode = error.code;
  685. // Error
  686. if (errorCode == 503)
  687. message = NSLocalizedString(@"_server_error_retry_", nil);
  688. else
  689. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  690. completion(account, message, errorCode);
  691. }];
  692. }
  693. - (void)updateCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  694. {
  695. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  696. if (tableAccount == nil) {
  697. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  698. } else if ([CCUtility getPassword:account].length == 0) {
  699. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  700. } else if ([CCUtility getCertificateError:account]) {
  701. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  702. }
  703. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  704. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  705. [communication setUserAgent:[CCUtility getUserAgent]];
  706. [communication updateComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  707. completion(account, nil, 0);
  708. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  709. NSString *message;
  710. NSInteger errorCode = response.statusCode;
  711. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  712. errorCode = error.code;
  713. // Error
  714. if (errorCode == 503)
  715. message = NSLocalizedString(@"_server_error_retry_", nil);
  716. else
  717. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  718. completion(account, message, errorCode);
  719. }];
  720. }
  721. - (void)readMarkCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  722. {
  723. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  724. if (tableAccount == nil) {
  725. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  726. } else if ([CCUtility getPassword:account].length == 0) {
  727. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  728. } else if ([CCUtility getCertificateError:account]) {
  729. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  730. }
  731. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  732. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  733. [communication setUserAgent:[CCUtility getUserAgent]];
  734. [communication readMarkComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  735. completion(account, nil, 0);
  736. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  737. NSString *message;
  738. NSInteger errorCode = response.statusCode;
  739. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  740. errorCode = error.code;
  741. // Error
  742. if (errorCode == 503)
  743. message = NSLocalizedString(@"_server_error_retry_", nil);
  744. else
  745. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  746. completion(account, message, errorCode);
  747. }];
  748. }
  749. - (void)deleteCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  750. {
  751. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  752. if (tableAccount == nil) {
  753. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  754. } else if ([CCUtility getPassword:account].length == 0) {
  755. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  756. } else if ([CCUtility getCertificateError:account]) {
  757. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  758. }
  759. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  760. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  761. [communication setUserAgent:[CCUtility getUserAgent]];
  762. [communication deleteComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  763. completion(account, nil, 0);
  764. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  765. NSString *message;
  766. NSInteger errorCode = response.statusCode;
  767. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  768. errorCode = error.code;
  769. // Error
  770. if (errorCode == 503)
  771. message = NSLocalizedString(@"_server_error_retry_", nil);
  772. else
  773. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  774. completion(account, message, errorCode);
  775. }];
  776. }
  777. #pragma --------------------------------------------------------------------------------------------
  778. #pragma mark ===== Third Parts =====
  779. #pragma --------------------------------------------------------------------------------------------
  780. - (void)getHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  781. {
  782. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  783. if (tableAccount == nil) {
  784. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  785. } else if ([CCUtility getPassword:account].length == 0) {
  786. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  787. } else if ([CCUtility getCertificateError:account]) {
  788. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  789. }
  790. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  791. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  792. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  793. [communication setUserAgent:[CCUtility getUserAgent]];
  794. [communication getHCUserProfile:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  795. completion(account, userProfile, nil, 0);
  796. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  797. NSString *message;
  798. NSInteger errorCode = response.statusCode;
  799. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  800. errorCode = error.code;
  801. // Server Unauthorized
  802. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  803. #ifndef EXTENSION
  804. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  805. #endif
  806. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  807. [CCUtility setCertificateError:account error:YES];
  808. }
  809. // Error
  810. if (errorCode == 503)
  811. message = NSLocalizedString(@"_server_error_retry_", nil);
  812. else
  813. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  814. completion(account, nil,message, errorCode);
  815. }];
  816. }
  817. - (void)putHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl address:(NSString *)address businesssize:(NSString *)businesssize businesstype:(NSString *)businesstype city:(NSString *)city company:(NSString *)company country:(NSString *)country displayname:(NSString *)displayname email:(NSString *)email phone:(NSString *)phone role_:(NSString *)role_ twitter:(NSString *)twitter website:(NSString *)website zip:(NSString *)zip completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  818. {
  819. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  820. if (tableAccount == nil) {
  821. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  822. } else if ([CCUtility getPassword:account].length == 0) {
  823. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  824. } else if ([CCUtility getCertificateError:account]) {
  825. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  826. }
  827. // Create JSON
  828. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  829. if (address) [dataDic setValue:address forKey:@"address"];
  830. if (businesssize) {
  831. if ([businesssize isEqualToString:@"1-4"]) { [dataDic setValue:[NSNumber numberWithInt:1] forKey:@"businesssize"]; }
  832. else if ([businesssize isEqualToString:@"5-9"]) { [dataDic setValue:[NSNumber numberWithInt:5] forKey:@"businesssize"]; }
  833. else if ([businesssize isEqualToString:@"10-19"]) { [dataDic setValue:[NSNumber numberWithInt:10] forKey:@"businesssize"]; }
  834. else if ([businesssize isEqualToString:@"20-49"]) { [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"businesssize"]; }
  835. else if ([businesssize isEqualToString:@"50-99"]) { [dataDic setValue:[NSNumber numberWithInt:50] forKey:@"businesssize"]; }
  836. else if ([businesssize isEqualToString:@"100-249"]) { [dataDic setValue:[NSNumber numberWithInt:100] forKey:@"businesssize"]; }
  837. else if ([businesssize isEqualToString:@"250-499"]) { [dataDic setValue:[NSNumber numberWithInt:250] forKey:@"businesssize"]; }
  838. else if ([businesssize isEqualToString:@"500-999"]) { [dataDic setValue:[NSNumber numberWithInt:500] forKey:@"businesssize"]; }
  839. else if ([businesssize isEqualToString:@"1000+"]) { [dataDic setValue:[NSNumber numberWithInt:1000] forKey:@"businesssize"]; }
  840. }
  841. if (businesstype) [dataDic setValue:businesstype forKey:@"businesstype"];
  842. if (city) [dataDic setValue:city forKey:@"city"];
  843. if (company) [dataDic setValue:company forKey:@"company"];
  844. if (country) [dataDic setValue:country forKey:@"country"];
  845. if (displayname) [dataDic setValue:displayname forKey:@"displayname"];
  846. if (email) [dataDic setValue:email forKey:@"email"];
  847. if (phone) [dataDic setValue:phone forKey:@"phone"];
  848. if (role_) [dataDic setValue:role_ forKey:@"role"];
  849. if (twitter) [dataDic setValue:twitter forKey:@"twitter"];
  850. if (website) [dataDic setValue:website forKey:@"website"];
  851. if (zip) [dataDic setValue:zip forKey:@"zip"];
  852. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  853. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  854. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  855. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  856. [communication setUserAgent:[CCUtility getUserAgent]];
  857. [communication putHCUserProfile:serverPath data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  858. completion(account, nil, 0);
  859. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  860. NSString *message;
  861. NSInteger errorCode = response.statusCode;
  862. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  863. errorCode = error.code;
  864. // Server Unauthorized
  865. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  866. #ifndef EXTENSION
  867. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  868. #endif
  869. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  870. [CCUtility setCertificateError:account error:YES];
  871. }
  872. // Error
  873. if (errorCode == 503)
  874. message = NSLocalizedString(@"_server_error_retry_", nil);
  875. else
  876. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  877. completion(account, message, errorCode);
  878. }];
  879. }
  880. - (void)getHCFeaturesWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, HCFeatures *features, NSString *message, NSInteger errorCode))completion
  881. {
  882. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  883. if (tableAccount == nil) {
  884. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  885. } else if ([CCUtility getPassword:account].length == 0) {
  886. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  887. } else if ([CCUtility getCertificateError:account]) {
  888. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  889. }
  890. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/features/%@", serverUrl, tableAccount.userID];
  891. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  892. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  893. [communication setUserAgent:[CCUtility getUserAgent]];
  894. [communication getHCFeatures:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer) {
  895. completion(account, features, nil, 0);
  896. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  897. NSString *message;
  898. NSInteger errorCode = response.statusCode;
  899. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  900. errorCode = error.code;
  901. // Server Unauthorized
  902. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  903. #ifndef EXTENSION
  904. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  905. #endif
  906. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  907. [CCUtility setCertificateError:account error:YES];
  908. }
  909. // Error
  910. if (errorCode == 503)
  911. message = NSLocalizedString(@"_server_error_retry_", nil);
  912. else
  913. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  914. completion(account, nil,message, errorCode);
  915. }];
  916. }
  917. #pragma --------------------------------------------------------------------------------------------
  918. #pragma mark ===== didReceiveChallenge =====
  919. #pragma --------------------------------------------------------------------------------------------
  920. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  921. {
  922. // The pinnning check
  923. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  924. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  925. } else {
  926. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  927. }
  928. }
  929. @end
  930. #pragma --------------------------------------------------------------------------------------------
  931. #pragma mark ===== OCURLSessionManager =====
  932. #pragma --------------------------------------------------------------------------------------------
  933. @implementation OCURLSessionManager
  934. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  935. {
  936. // The pinnning check
  937. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  938. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  939. } else {
  940. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  941. }
  942. }
  943. @end