OCNetworking.m 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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 ===== Server =====
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (void)checkServerUrl:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password completion:(void (^)(NSString *message, NSInteger errorCode))completion
  96. {
  97. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  98. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  99. [communication setUserAgent:[CCUtility getUserAgent]];
  100. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  101. completion(nil, 0);
  102. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  103. NSString *message;
  104. NSInteger errorCode = response.statusCode;
  105. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  106. errorCode = error.code;
  107. // Error
  108. if (errorCode == 503)
  109. message = NSLocalizedString(@"_server_error_retry_", nil);
  110. else
  111. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  112. completion(message, errorCode);
  113. }];
  114. }
  115. - (void)getAppPassword:(NSString *)serverUrl username:(NSString *)username password:(NSString *)password completion:(void(^)(NSString *token, NSString *message, NSInteger errorCode))completion
  116. {
  117. NSData *authData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
  118. NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  119. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/core/getapppassword", serverUrl];
  120. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  121. [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  122. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  123. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  124. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  125. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  126. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  127. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  128. NSInteger errorCode = 0;
  129. NSString *message = @"";
  130. NSString *token = nil;
  131. if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) {
  132. NCXMLGetAppPasswordParser *parser = [NCXMLGetAppPasswordParser new];
  133. [parser initParserWithData:data];
  134. token = parser.token;
  135. } else {
  136. errorCode = httpResponse.statusCode;
  137. message = [NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode];
  138. }
  139. dispatch_async(dispatch_get_main_queue(), ^{
  140. completion(token, message, errorCode);
  141. });
  142. }];
  143. [task resume];
  144. }
  145. #pragma --------------------------------------------------------------------------------------------
  146. #pragma mark ===== download / upload =====
  147. #pragma --------------------------------------------------------------------------------------------
  148. - (NSURLSessionTask *)downloadWithAccount:(NSString *)account url:(NSString *)url fileNameLocalPath:(NSString *)fileNameLocalPath encode:(BOOL)encode completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  149. {
  150. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  151. if (tableAccount == nil) {
  152. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  153. } else if ([CCUtility getPassword:account].length == 0) {
  154. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  155. } else if ([CCUtility getCertificateError:account]) {
  156. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  157. }
  158. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  159. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  160. [communication setUserAgent:[CCUtility getUserAgent]];
  161. NSURLSessionTask *sessionTask = [communication downloadFileSession:url toDestiny:fileNameLocalPath defaultPriority:YES encode:encode onCommunication:communication progress:^(NSProgress *progress) {
  162. //float percent = roundf (progress.fractionCompleted * 100);
  163. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  164. completion(account, nil, 0);
  165. } failureRequest:^(NSURLResponse *response, NSError *error) {
  166. NSString *message;
  167. NSInteger errorCode = ((NSHTTPURLResponse*)response).statusCode;
  168. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  169. errorCode = error.code;
  170. // Server Unauthorized
  171. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  172. #ifndef EXTENSION
  173. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  174. #endif
  175. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  176. [CCUtility setCertificateError:account error:YES];
  177. }
  178. // Error
  179. if (errorCode == 503)
  180. message = NSLocalizedString(@"_server_error_retry_", nil);
  181. else
  182. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  183. completion(account, message, errorCode);
  184. }];
  185. return sessionTask;
  186. }
  187. #pragma --------------------------------------------------------------------------------------------
  188. #pragma mark ===== Share =====
  189. #pragma --------------------------------------------------------------------------------------------
  190. - (void)readShareWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  191. {
  192. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  193. if (tableAccount == nil) {
  194. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  195. } else if ([CCUtility getPassword:account].length == 0) {
  196. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  197. } else if ([CCUtility getCertificateError:account]) {
  198. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  199. }
  200. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  201. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  202. [communication setUserAgent:[CCUtility getUserAgent]];
  203. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  204. completion(account, items, nil, 0);
  205. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  206. NSString *message;
  207. NSInteger errorCode = response.statusCode;
  208. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  209. errorCode = error.code;
  210. // Error
  211. if (errorCode == 503) {
  212. message = NSLocalizedString(@"_server_error_retry_", nil);
  213. } else {
  214. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  215. }
  216. completion(account, nil, message, errorCode);
  217. }];
  218. }
  219. - (void)readShareWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  220. {
  221. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  222. if (tableAccount == nil) {
  223. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  224. } else if ([CCUtility getPassword:account].length == 0) {
  225. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  226. } else if ([CCUtility getCertificateError:account]) {
  227. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  228. }
  229. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  230. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  231. [communication setUserAgent:[CCUtility getUserAgent]];
  232. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] andPath:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  233. completion(account, listOfShared, nil, 0);
  234. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  235. NSString *message;
  236. NSInteger errorCode = response.statusCode;
  237. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  238. errorCode = error.code;
  239. // Error
  240. if (errorCode == 503) {
  241. message = NSLocalizedString(@"_server_error_retry_", nil);
  242. } else {
  243. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  244. }
  245. completion(account, nil, message, errorCode);
  246. }];
  247. }
  248. - (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
  249. {
  250. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  251. if (tableAccount == nil) {
  252. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  253. } else if ([CCUtility getPassword:account].length == 0) {
  254. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  255. } else if ([CCUtility getCertificateError:account]) {
  256. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  257. }
  258. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  259. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  260. [communication setUserAgent:[CCUtility getUserAgent]];
  261. [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) {
  262. completion(account, nil, 0);
  263. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  264. NSString *message;
  265. NSInteger errorCode = response.statusCode;
  266. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  267. errorCode = error.code;
  268. // Error
  269. if (errorCode == 503) {
  270. message = NSLocalizedString(@"_server_error_retry_", nil);
  271. } else {
  272. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  273. }
  274. completion(account, message, errorCode);
  275. }];
  276. }
  277. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  278. - (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
  279. {
  280. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  281. if (tableAccount == nil) {
  282. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  283. } else if ([CCUtility getPassword:account].length == 0) {
  284. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  285. } else if ([CCUtility getCertificateError:account]) {
  286. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  287. }
  288. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  289. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  290. [communication setUserAgent:[CCUtility getUserAgent]];
  291. [communication shareWith:userOrGroup shareeType:shareeType inServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPermissions:permission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  292. completion(account, nil, 0);
  293. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  294. NSString *message;
  295. NSInteger errorCode = response.statusCode;
  296. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  297. errorCode = error.code;
  298. // Error
  299. if (errorCode == 503) {
  300. message = NSLocalizedString(@"_server_error_retry_", nil);
  301. } else {
  302. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  303. }
  304. completion(account, message, errorCode);
  305. }];
  306. }
  307. - (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
  308. {
  309. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  310. if (tableAccount == nil) {
  311. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  312. } else if ([CCUtility getPassword:account].length == 0) {
  313. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  314. } else if ([CCUtility getCertificateError:account]) {
  315. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  316. }
  317. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  318. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  319. [communication setUserAgent:[CCUtility getUserAgent]];
  320. [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) {
  321. completion(account, nil, 0);
  322. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  323. NSString *message;
  324. NSInteger errorCode = response.statusCode;
  325. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  326. errorCode = error.code;
  327. // Error
  328. if (errorCode == 503) {
  329. message = NSLocalizedString(@"_server_error_retry_", nil);
  330. } else {
  331. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  332. }
  333. completion(account, message, errorCode);
  334. }];
  335. }
  336. - (void)unshareAccount:(NSString *)account shareID:(NSInteger)shareID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  337. {
  338. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  339. if (tableAccount == nil) {
  340. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  341. } else if ([CCUtility getPassword:account].length == 0) {
  342. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  343. } else if ([CCUtility getCertificateError:account]) {
  344. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  345. }
  346. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  347. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  348. [communication setUserAgent:[CCUtility getUserAgent]];
  349. [communication unShareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andIdRemoteShared:shareID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  350. completion(account, nil, 0);
  351. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  352. NSString *message;
  353. NSInteger errorCode = response.statusCode;
  354. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  355. errorCode = error.code;
  356. // Error
  357. if (errorCode == 503) {
  358. message = NSLocalizedString(@"_server_error_retry_", nil);
  359. } else {
  360. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  361. }
  362. completion(account, message, errorCode);
  363. }];
  364. }
  365. - (void)getUserGroupWithAccount:(NSString *)account searchString:(NSString *)searchString completion:(void (^)(NSString *account, NSArray *item, NSString *message, NSInteger errorCode))completion
  366. {
  367. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  368. if (tableAccount == nil) {
  369. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  370. } else if ([CCUtility getPassword:account].length == 0) {
  371. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  372. } else if ([CCUtility getCertificateError:account]) {
  373. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  374. }
  375. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  376. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  377. [communication setUserAgent:[CCUtility getUserAgent]];
  378. [communication searchUsersAndGroupsWith:searchString forPage:1 with:50 ofServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  379. completion(account, itemList, nil, 0);
  380. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  381. NSString *message;
  382. NSInteger errorCode = response.statusCode;
  383. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  384. errorCode = error.code;
  385. // Error
  386. if (errorCode == 503) {
  387. message = NSLocalizedString(@"_server_error_retry_", nil);
  388. } else {
  389. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  390. }
  391. completion(account, nil, message, errorCode);
  392. }];
  393. }
  394. #pragma --------------------------------------------------------------------------------------------
  395. #pragma mark ===== VAR =====
  396. #pragma --------------------------------------------------------------------------------------------
  397. - (void)getActivityWithAccount:(NSString *)account since:(NSInteger)since limit:(NSInteger)limit objectId:(NSString *)objectId objectType:(NSString *)objectType link:(NSString *)link completion:(void(^)(NSString *account, NSArray *listOfActivity, NSString *message, NSInteger errorCode))completion
  398. {
  399. BOOL previews = false;
  400. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  401. if (tableAccount == nil) {
  402. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  403. } else if ([CCUtility getPassword:account].length == 0) {
  404. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  405. } else if ([CCUtility getCertificateError:account]) {
  406. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  407. }
  408. NSInteger serverVersionMajor = [[NCManageDatabase sharedInstance] getCapabilitiesServerIntWithAccount:account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
  409. if (serverVersionMajor >= k_nextcloud_version_15_0) {
  410. previews = true;
  411. }
  412. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  413. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  414. [communication setUserAgent:[CCUtility getUserAgent]];
  415. [communication getActivityServer:[tableAccount.url stringByAppendingString:@"/"] since:since limit:limit objectId:objectId objectType:objectType previews:previews link:link onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  416. completion(account, listOfActivity, nil, 0);
  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. // Server Unauthorized
  423. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  424. #ifndef EXTENSION
  425. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  426. #endif
  427. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  428. [CCUtility setCertificateError:account error:YES];
  429. }
  430. // Error
  431. if (errorCode == 503)
  432. message = NSLocalizedString(@"_server_error_retry_", nil);
  433. else
  434. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  435. completion(account, nil, message, errorCode);
  436. }];
  437. }
  438. - (void)getExternalSitesWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfExternalSites, NSString *message, NSInteger errorCode))completion
  439. {
  440. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  441. if (tableAccount == nil) {
  442. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  443. } else if ([CCUtility getPassword:account].length == 0) {
  444. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  445. } else if ([CCUtility getCertificateError:account]) {
  446. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  447. }
  448. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  449. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  450. [communication setUserAgent:[CCUtility getUserAgent]];
  451. [communication getExternalSitesServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  452. completion(account, listOfExternalSites, nil, 0);
  453. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  454. NSString *message;
  455. NSInteger errorCode = response.statusCode;
  456. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  457. errorCode = error.code;
  458. // Server Unauthorized
  459. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  460. #ifndef EXTENSION
  461. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  462. #endif
  463. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  464. [CCUtility setCertificateError:account error:YES];
  465. }
  466. // Error
  467. if (errorCode == 503) {
  468. message = NSLocalizedString(@"_server_error_retry_", nil);
  469. } else {
  470. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  471. }
  472. completion(account, nil, message, errorCode);
  473. }];
  474. }
  475. - (void)getNotificationWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  476. {
  477. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  478. if (tableAccount == nil) {
  479. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  480. } else if ([CCUtility getPassword:account].length == 0) {
  481. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  482. } else if ([CCUtility getCertificateError:account]) {
  483. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  484. }
  485. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  486. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  487. [communication setUserAgent:[CCUtility getUserAgent]];
  488. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  489. completion(account, listOfNotifications, nil, 0);
  490. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  491. NSString *message;
  492. NSInteger errorCode = response.statusCode;
  493. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  494. errorCode = error.code;
  495. // Server Unauthorized
  496. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  497. #ifndef EXTENSION
  498. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  499. #endif
  500. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  501. [CCUtility setCertificateError:account error:YES];
  502. }
  503. // Error
  504. if (errorCode == 503) {
  505. message = NSLocalizedString(@"_server_error_retry_", nil);
  506. } else {
  507. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  508. }
  509. completion(account, nil, message, errorCode);
  510. }];
  511. }
  512. - (void)setNotificationWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  513. {
  514. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  515. if (tableAccount == nil) {
  516. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  517. } else if ([CCUtility getPassword:account].length == 0) {
  518. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  519. } else if ([CCUtility getCertificateError:account]) {
  520. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  521. }
  522. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  523. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  524. [communication setUserAgent:[CCUtility getUserAgent]];
  525. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  526. completion(account, nil, 0);
  527. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  528. NSString *message;
  529. NSInteger errorCode = response.statusCode;
  530. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  531. errorCode = error.code;
  532. // Server Unauthorized
  533. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  534. #ifndef EXTENSION
  535. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  536. #endif
  537. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  538. [CCUtility setCertificateError:account error:YES];
  539. }
  540. // Error
  541. if (errorCode == 503) {
  542. message = NSLocalizedString(@"_server_error_retry_", nil);
  543. } else {
  544. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  545. }
  546. completion(account, message, errorCode);
  547. }];
  548. }
  549. #pragma --------------------------------------------------------------------------------------------
  550. #pragma mark ===== Push Notification =====
  551. #pragma --------------------------------------------------------------------------------------------
  552. - (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
  553. {
  554. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  555. if (tableAccount == nil) {
  556. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  557. } else if ([CCUtility getPassword:account].length == 0) {
  558. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  559. } else if ([CCUtility getCertificateError:account]) {
  560. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  561. }
  562. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  563. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  564. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  565. #ifdef DEBUG
  566. // proxyServerPath = @"http://127.0.0.1:8088";
  567. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  568. #endif
  569. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  570. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  571. [communication setUserAgent:[CCUtility getUserAgent]];
  572. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: proxyServerPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  573. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  574. signature = [CCUtility URLEncodeStringFromString:signature];
  575. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  576. [communication subscribingPushProxy:proxyServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  577. completion(account, deviceIdentifier, signature, publicKey, nil, 0);
  578. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  579. NSString *message;
  580. NSInteger errorCode = response.statusCode;
  581. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  582. errorCode = error.code;
  583. // Error
  584. if (errorCode == 503)
  585. message = NSLocalizedString(@"_server_error_retry_", nil);
  586. else
  587. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  588. completion(account, nil, nil, nil, message, errorCode);
  589. }];
  590. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  591. NSString *message;
  592. NSInteger errorCode = response.statusCode;
  593. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  594. errorCode = error.code;
  595. // Error
  596. if (errorCode == 503)
  597. message = NSLocalizedString(@"_server_error_retry_", nil);
  598. else
  599. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  600. completion(account, nil, nil, nil, message, errorCode);
  601. }];
  602. }
  603. - (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 {
  604. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  605. if (tableAccount == nil) {
  606. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  607. } else if ([CCUtility getPassword:account].length == 0) {
  608. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  609. } else if ([CCUtility getCertificateError:account]) {
  610. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  611. }
  612. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  613. #ifdef DEBUG
  614. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  615. #endif
  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 unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  620. [communication unsubscribingPushProxy:proxyServer deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  621. completion(account, nil, 0);
  622. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  623. NSString *message;
  624. NSInteger errorCode = response.statusCode;
  625. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  626. errorCode = error.code;
  627. // Error
  628. if (errorCode == 503)
  629. message = NSLocalizedString(@"_server_error_retry_", nil);
  630. else
  631. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  632. completion(account, message, errorCode);
  633. }];
  634. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  635. NSString *message;
  636. NSInteger errorCode = response.statusCode;
  637. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  638. errorCode = error.code;
  639. // Error
  640. if (errorCode == 503)
  641. message = NSLocalizedString(@"_server_error_retry_", nil);
  642. else
  643. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  644. completion(account, message, errorCode);
  645. }];
  646. }
  647. - (void)getServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSDictionary*jsongParsed, NSString *message, NSInteger errorCode))completion
  648. {
  649. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld?format=json", serverUrl, (long)notificationId];
  650. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  651. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  652. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  653. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  654. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  655. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  656. if (error) {
  657. NSString *message;
  658. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  659. NSInteger errorCode = httpResponse.statusCode;
  660. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  661. errorCode = error.code;
  662. // Error
  663. if (errorCode == 503)
  664. message = NSLocalizedString(@"_server_error_retry_", nil);
  665. else
  666. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  667. completion(nil, message, errorCode);
  668. } else {
  669. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  670. completion(jsongParsed, nil, 0);
  671. }
  672. }];
  673. [task resume];
  674. }
  675. - (void)deletingServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSString *message, NSInteger errorCode))completion
  676. {
  677. // NSData *authData = [[NSString stringWithFormat:@"%@:%@", tableAccount.user, [CCUtility getPassword:tableAccount.account]] dataUsingEncoding:NSUTF8StringEncoding];
  678. // NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  679. // Delete
  680. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld", serverUrl, (long)notificationId];
  681. // Delete-all
  682. if (notificationId == 0) {
  683. URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications", serverUrl];
  684. }
  685. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  686. // [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  687. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  688. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  689. [request setHTTPMethod: @"DELETE"];
  690. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  691. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  692. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  693. if (error) {
  694. NSString *message;
  695. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  696. NSInteger errorCode = httpResponse.statusCode;
  697. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  698. errorCode = error.code;
  699. // Error
  700. if (errorCode == 503)
  701. message = NSLocalizedString(@"_server_error_retry_", nil);
  702. else
  703. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  704. completion(message, errorCode);
  705. } else {
  706. completion(nil, 0);
  707. }
  708. }];
  709. [task resume];
  710. }
  711. #pragma --------------------------------------------------------------------------------------------
  712. #pragma mark ===== Manage Mobile Editor OCS API =====
  713. #pragma --------------------------------------------------------------------------------------------
  714. - (void)createLinkRichdocumentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  715. {
  716. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  717. if (tableAccount == nil) {
  718. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  719. } else if ([CCUtility getPassword:account].length == 0) {
  720. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  721. } else if ([CCUtility getCertificateError:account]) {
  722. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  723. }
  724. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  725. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  726. [communication setUserAgent:[CCUtility getUserAgent]];
  727. [communication createLinkRichdocuments:[tableAccount.url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer) {
  728. completion(account, link, nil, 0);
  729. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  730. NSString *message;
  731. NSInteger errorCode = response.statusCode;
  732. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  733. errorCode = error.code;
  734. // Error
  735. if (errorCode == 503)
  736. message = NSLocalizedString(@"_server_error_retry_", nil);
  737. else
  738. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  739. completion(account, nil,message, errorCode);
  740. }];
  741. }
  742. - (void)getTemplatesRichdocumentsWithAccount:(NSString *)account typeTemplate:(NSString *)typeTemplate completion:(void(^)(NSString *account, NSArray *listOfTemplate, NSString *message, NSInteger errorCode))completion
  743. {
  744. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  745. if (tableAccount == nil) {
  746. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  747. } else if ([CCUtility getPassword:account].length == 0) {
  748. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  749. } else if ([CCUtility getCertificateError:account]) {
  750. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  751. }
  752. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  753. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  754. [communication setUserAgent:[CCUtility getUserAgent]];
  755. [communication getTemplatesRichdocuments:[tableAccount.url stringByAppendingString:@"/"] typeTemplate:typeTemplate onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer) {
  756. completion(account, listOfTemplate, nil, 0);
  757. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  758. NSString *message;
  759. NSInteger errorCode = response.statusCode;
  760. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  761. errorCode = error.code;
  762. // Error
  763. if (errorCode == 503)
  764. message = NSLocalizedString(@"_server_error_retry_", nil);
  765. else
  766. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  767. completion(account, nil, message, errorCode);
  768. }];
  769. }
  770. - (void)createNewRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl templateID:(NSString *)templateID completion:(void(^)(NSString *account, NSString *url, NSString *message, NSInteger errorCode))completion
  771. {
  772. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  773. if (tableAccount == nil) {
  774. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  775. } else if ([CCUtility getPassword:account].length == 0) {
  776. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  777. } else if ([CCUtility getCertificateError:account]) {
  778. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  779. }
  780. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  781. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  782. [communication setUserAgent:[CCUtility getUserAgent]];
  783. [communication createNewRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileName templateID:templateID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  784. completion(account, url, nil, 0);
  785. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  786. NSString *message;
  787. NSInteger errorCode = response.statusCode;
  788. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  789. errorCode = error.code;
  790. // Error
  791. if (errorCode == 503)
  792. message = NSLocalizedString(@"_server_error_retry_", nil);
  793. else
  794. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  795. completion(account, nil, message, errorCode);
  796. }];
  797. }
  798. - (void)createAssetRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  799. {
  800. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  801. if (tableAccount == nil) {
  802. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  803. } else if ([CCUtility getPassword:account].length == 0) {
  804. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  805. } else if ([CCUtility getCertificateError:account]) {
  806. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  807. }
  808. NSString *fileNamePath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:tableAccount.url];
  809. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  810. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  811. [communication setUserAgent:[CCUtility getUserAgent]];
  812. [communication createAssetRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  813. completion(account, url, nil, 0);
  814. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  815. NSString *message;
  816. NSInteger errorCode = response.statusCode;
  817. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  818. errorCode = error.code;
  819. // Error
  820. if (errorCode == 503)
  821. message = NSLocalizedString(@"_server_error_retry_", nil);
  822. else
  823. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  824. completion(account, nil, message, errorCode);
  825. }];
  826. }
  827. #pragma --------------------------------------------------------------------------------------------
  828. #pragma mark ===== Trash =====
  829. #pragma --------------------------------------------------------------------------------------------
  830. - (void)listingTrashWithAccount:(NSString *)account path:(NSString *)path serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  831. {
  832. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  833. if (tableAccount == nil) {
  834. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  835. } else if ([CCUtility getPassword:account].length == 0) {
  836. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  837. } else if ([CCUtility getCertificateError:account]) {
  838. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  839. }
  840. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  841. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  842. [communication setUserAgent:[CCUtility getUserAgent]];
  843. [communication listingTrash:[serverUrl stringByAppendingString:path] depth:depth onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  844. // Check items > 0
  845. if ([items count] == 0) {
  846. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  847. completion(account, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  848. } else {
  849. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  850. NSMutableArray *listTrash = [NSMutableArray new];
  851. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  852. if ([items count] > 1) {
  853. for (NSUInteger i=1; i < [items count]; i++) {
  854. OCFileDto *itemDto = [items objectAtIndex:i];
  855. tableTrash *trash = [tableTrash new];
  856. trash.account = account;
  857. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  858. trash.directory = itemDto.isDirectory;
  859. trash.fileId = itemDto.fileId;
  860. trash.fileName = itemDto.fileName;
  861. NSArray *array = [itemDto.filePath componentsSeparatedByString:path];
  862. long len = [[array objectAtIndex:0] length];
  863. trash.filePath = [itemDto.filePath substringFromIndex:len];
  864. trash.hasPreview = itemDto.hasPreview;
  865. trash.size = itemDto.size;
  866. trash.trashbinFileName = itemDto.trashbinFileName;
  867. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  868. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  869. NSDictionary *results = [[NCCommunicationCommon shared] objcGetInternalContenTypeWithFileName:trash.trashbinFileName contentType:@"" directory:itemDto.isDirectory];
  870. trash.contentType = results[@"contentType"];
  871. trash.iconName = results[@"iconName"];
  872. trash.typeFile = results[@"typeFile"];
  873. [listTrash addObject:trash];
  874. }
  875. }
  876. dispatch_async(dispatch_get_main_queue(), ^{
  877. completion(account, listTrash, nil, 0);
  878. });
  879. });
  880. }
  881. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  882. NSString *message;
  883. NSInteger errorCode = response.statusCode;
  884. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  885. errorCode = error.code;
  886. // Error
  887. if (errorCode == 503)
  888. message = NSLocalizedString(@"_server_error_retry_", nil);
  889. else
  890. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  891. completion(account, nil,message, errorCode);
  892. }];
  893. }
  894. - (void)emptyTrashWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  895. {
  896. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  897. if (tableAccount == nil) {
  898. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  899. } else if ([CCUtility getPassword:account].length == 0) {
  900. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  901. } else if ([CCUtility getCertificateError:account]) {
  902. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  903. }
  904. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", tableAccount.url, k_dav, tableAccount.userID];
  905. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  906. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  907. [communication setUserAgent:[CCUtility getUserAgent]];
  908. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  909. completion(account, nil, 0);
  910. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  911. NSString *message;
  912. NSInteger errorCode = response.statusCode;
  913. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  914. errorCode = error.code;
  915. // Error
  916. if (errorCode == 503)
  917. message = NSLocalizedString(@"_server_error_retry_", nil);
  918. else
  919. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  920. completion(account, message, errorCode);
  921. }];
  922. }
  923. #pragma --------------------------------------------------------------------------------------------
  924. #pragma mark ===== Comments =====
  925. #pragma --------------------------------------------------------------------------------------------
  926. - (void)getCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  927. {
  928. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  929. if (tableAccount == nil) {
  930. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  931. } else if ([CCUtility getPassword:account].length == 0) {
  932. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  933. } else if ([CCUtility getCertificateError:account]) {
  934. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  935. }
  936. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  937. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  938. [communication setUserAgent:[CCUtility getUserAgent]];
  939. [communication getComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *list, NSString *redirectedServer) {
  940. completion(account, list, nil, 0);
  941. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  942. NSString *message;
  943. NSInteger errorCode = response.statusCode;
  944. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  945. errorCode = error.code;
  946. // Error
  947. if (errorCode == 503)
  948. message = NSLocalizedString(@"_server_error_retry_", nil);
  949. else
  950. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  951. completion(account, nil,message, errorCode);
  952. }];
  953. }
  954. - (void)putCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  955. {
  956. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  957. if (tableAccount == nil) {
  958. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  959. } else if ([CCUtility getPassword:account].length == 0) {
  960. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  961. } else if ([CCUtility getCertificateError:account]) {
  962. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  963. }
  964. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  965. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  966. [communication setUserAgent:[CCUtility getUserAgent]];
  967. [communication putComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  968. completion(account, nil, 0);
  969. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  970. NSString *message;
  971. NSInteger errorCode = response.statusCode;
  972. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  973. errorCode = error.code;
  974. // Error
  975. if (errorCode == 503)
  976. message = NSLocalizedString(@"_server_error_retry_", nil);
  977. else
  978. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  979. completion(account, message, errorCode);
  980. }];
  981. }
  982. - (void)updateCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  983. {
  984. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  985. if (tableAccount == nil) {
  986. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  987. } else if ([CCUtility getPassword:account].length == 0) {
  988. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  989. } else if ([CCUtility getCertificateError:account]) {
  990. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  991. }
  992. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  993. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  994. [communication setUserAgent:[CCUtility getUserAgent]];
  995. [communication updateComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  996. completion(account, nil, 0);
  997. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  998. NSString *message;
  999. NSInteger errorCode = response.statusCode;
  1000. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1001. errorCode = error.code;
  1002. // Error
  1003. if (errorCode == 503)
  1004. message = NSLocalizedString(@"_server_error_retry_", nil);
  1005. else
  1006. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1007. completion(account, message, errorCode);
  1008. }];
  1009. }
  1010. - (void)readMarkCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1011. {
  1012. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1013. if (tableAccount == nil) {
  1014. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1015. } else if ([CCUtility getPassword:account].length == 0) {
  1016. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1017. } else if ([CCUtility getCertificateError:account]) {
  1018. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1019. }
  1020. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1021. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1022. [communication setUserAgent:[CCUtility getUserAgent]];
  1023. [communication readMarkComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1024. completion(account, nil, 0);
  1025. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1026. NSString *message;
  1027. NSInteger errorCode = response.statusCode;
  1028. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1029. errorCode = error.code;
  1030. // Error
  1031. if (errorCode == 503)
  1032. message = NSLocalizedString(@"_server_error_retry_", nil);
  1033. else
  1034. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1035. completion(account, message, errorCode);
  1036. }];
  1037. }
  1038. - (void)deleteCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1039. {
  1040. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1041. if (tableAccount == nil) {
  1042. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1043. } else if ([CCUtility getPassword:account].length == 0) {
  1044. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1045. } else if ([CCUtility getCertificateError:account]) {
  1046. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1047. }
  1048. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1049. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1050. [communication setUserAgent:[CCUtility getUserAgent]];
  1051. [communication deleteComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1052. completion(account, nil, 0);
  1053. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1054. NSString *message;
  1055. NSInteger errorCode = response.statusCode;
  1056. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1057. errorCode = error.code;
  1058. // Error
  1059. if (errorCode == 503)
  1060. message = NSLocalizedString(@"_server_error_retry_", nil);
  1061. else
  1062. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1063. completion(account, message, errorCode);
  1064. }];
  1065. }
  1066. #pragma --------------------------------------------------------------------------------------------
  1067. #pragma mark ===== Third Parts =====
  1068. #pragma --------------------------------------------------------------------------------------------
  1069. - (void)getHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  1070. {
  1071. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1072. if (tableAccount == nil) {
  1073. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1074. } else if ([CCUtility getPassword:account].length == 0) {
  1075. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1076. } else if ([CCUtility getCertificateError:account]) {
  1077. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1078. }
  1079. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  1080. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1081. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1082. [communication setUserAgent:[CCUtility getUserAgent]];
  1083. [communication getHCUserProfile:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1084. completion(account, userProfile, nil, 0);
  1085. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1086. NSString *message;
  1087. NSInteger errorCode = response.statusCode;
  1088. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1089. errorCode = error.code;
  1090. // Server Unauthorized
  1091. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1092. #ifndef EXTENSION
  1093. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  1094. #endif
  1095. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1096. [CCUtility setCertificateError:account error:YES];
  1097. }
  1098. // Error
  1099. if (errorCode == 503)
  1100. message = NSLocalizedString(@"_server_error_retry_", nil);
  1101. else
  1102. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1103. completion(account, nil,message, errorCode);
  1104. }];
  1105. }
  1106. - (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
  1107. {
  1108. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1109. if (tableAccount == nil) {
  1110. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1111. } else if ([CCUtility getPassword:account].length == 0) {
  1112. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1113. } else if ([CCUtility getCertificateError:account]) {
  1114. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1115. }
  1116. // Create JSON
  1117. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  1118. if (address) [dataDic setValue:address forKey:@"address"];
  1119. if (businesssize) {
  1120. if ([businesssize isEqualToString:@"1-4"]) { [dataDic setValue:[NSNumber numberWithInt:1] forKey:@"businesssize"]; }
  1121. else if ([businesssize isEqualToString:@"5-9"]) { [dataDic setValue:[NSNumber numberWithInt:5] forKey:@"businesssize"]; }
  1122. else if ([businesssize isEqualToString:@"10-19"]) { [dataDic setValue:[NSNumber numberWithInt:10] forKey:@"businesssize"]; }
  1123. else if ([businesssize isEqualToString:@"20-49"]) { [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"businesssize"]; }
  1124. else if ([businesssize isEqualToString:@"50-99"]) { [dataDic setValue:[NSNumber numberWithInt:50] forKey:@"businesssize"]; }
  1125. else if ([businesssize isEqualToString:@"100-249"]) { [dataDic setValue:[NSNumber numberWithInt:100] forKey:@"businesssize"]; }
  1126. else if ([businesssize isEqualToString:@"250-499"]) { [dataDic setValue:[NSNumber numberWithInt:250] forKey:@"businesssize"]; }
  1127. else if ([businesssize isEqualToString:@"500-999"]) { [dataDic setValue:[NSNumber numberWithInt:500] forKey:@"businesssize"]; }
  1128. else if ([businesssize isEqualToString:@"1000+"]) { [dataDic setValue:[NSNumber numberWithInt:1000] forKey:@"businesssize"]; }
  1129. }
  1130. if (businesstype) [dataDic setValue:businesstype forKey:@"businesstype"];
  1131. if (city) [dataDic setValue:city forKey:@"city"];
  1132. if (company) [dataDic setValue:company forKey:@"company"];
  1133. if (country) [dataDic setValue:country forKey:@"country"];
  1134. if (displayname) [dataDic setValue:displayname forKey:@"displayname"];
  1135. if (email) [dataDic setValue:email forKey:@"email"];
  1136. if (phone) [dataDic setValue:phone forKey:@"phone"];
  1137. if (role_) [dataDic setValue:role_ forKey:@"role"];
  1138. if (twitter) [dataDic setValue:twitter forKey:@"twitter"];
  1139. if (website) [dataDic setValue:website forKey:@"website"];
  1140. if (zip) [dataDic setValue:zip forKey:@"zip"];
  1141. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  1142. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  1143. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1144. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1145. [communication setUserAgent:[CCUtility getUserAgent]];
  1146. [communication putHCUserProfile:serverPath data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1147. completion(account, nil, 0);
  1148. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1149. NSString *message;
  1150. NSInteger errorCode = response.statusCode;
  1151. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1152. errorCode = error.code;
  1153. // Server Unauthorized
  1154. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1155. #ifndef EXTENSION
  1156. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  1157. #endif
  1158. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1159. [CCUtility setCertificateError:account error:YES];
  1160. }
  1161. // Error
  1162. if (errorCode == 503)
  1163. message = NSLocalizedString(@"_server_error_retry_", nil);
  1164. else
  1165. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1166. completion(account, message, errorCode);
  1167. }];
  1168. }
  1169. - (void)getHCFeaturesWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, HCFeatures *features, NSString *message, NSInteger errorCode))completion
  1170. {
  1171. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1172. if (tableAccount == nil) {
  1173. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1174. } else if ([CCUtility getPassword:account].length == 0) {
  1175. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1176. } else if ([CCUtility getCertificateError:account]) {
  1177. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1178. }
  1179. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/features/%@", serverUrl, tableAccount.userID];
  1180. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1181. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1182. [communication setUserAgent:[CCUtility getUserAgent]];
  1183. [communication getHCFeatures:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer) {
  1184. completion(account, features, nil, 0);
  1185. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1186. NSString *message;
  1187. NSInteger errorCode = response.statusCode;
  1188. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1189. errorCode = error.code;
  1190. // Server Unauthorized
  1191. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1192. #ifndef EXTENSION
  1193. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  1194. #endif
  1195. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1196. [CCUtility setCertificateError:account error:YES];
  1197. }
  1198. // Error
  1199. if (errorCode == 503)
  1200. message = NSLocalizedString(@"_server_error_retry_", nil);
  1201. else
  1202. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1203. completion(account, nil,message, errorCode);
  1204. }];
  1205. }
  1206. #pragma --------------------------------------------------------------------------------------------
  1207. #pragma mark ===== didReceiveChallenge =====
  1208. #pragma --------------------------------------------------------------------------------------------
  1209. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  1210. {
  1211. // The pinnning check
  1212. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1213. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1214. } else {
  1215. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1216. }
  1217. }
  1218. @end
  1219. #pragma --------------------------------------------------------------------------------------------
  1220. #pragma mark ===== OCURLSessionManager =====
  1221. #pragma --------------------------------------------------------------------------------------------
  1222. @implementation OCURLSessionManager
  1223. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1224. {
  1225. // The pinnning check
  1226. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1227. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1228. } else {
  1229. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1230. }
  1231. }
  1232. @end