OCNetworking.m 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. //
  2. // OCnetworking.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 "CCCertificate.h"
  27. #import "NSString+Encode.h"
  28. #import "NCBridgeSwift.h"
  29. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  30. @interface OCnetworking ()
  31. {
  32. NSString *_activeUser;
  33. NSString *_activeUserID;
  34. NSString *_activePassword;
  35. NSString *_activeUrl;
  36. }
  37. @end
  38. @implementation OCnetworking
  39. - (id)initWithDelegate:(id <OCNetworkingDelegate>)delegate metadataNet:(CCMetadataNet *)metadataNet withUser:(NSString *)withUser withUserID:(NSString *)withUserID withPassword:(NSString *)withPassword withUrl:(NSString *)withUrl
  40. {
  41. self = [super init];
  42. if (self) {
  43. _delegate = delegate;
  44. _metadataNet = [CCMetadataNet new];
  45. _metadataNet = [metadataNet copy];
  46. _activeUser = withUser;
  47. _activeUserID = withUserID;
  48. _activePassword = withPassword;
  49. _activeUrl = withUrl;
  50. }
  51. return self;
  52. }
  53. - (void)start
  54. {
  55. if (![NSThread isMainThread]) {
  56. [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
  57. return;
  58. }
  59. [self willChangeValueForKey:@"isExecuting"];
  60. _isExecuting = YES;
  61. [self didChangeValueForKey:@"isExecuting"];
  62. if (self.isCancelled) {
  63. [self finish];
  64. } else {
  65. [self poolNetworking];
  66. }
  67. }
  68. - (void)finish
  69. {
  70. [self willChangeValueForKey:@"isExecuting"];
  71. [self willChangeValueForKey:@"isFinished"];
  72. _isExecuting = NO;
  73. _isFinished = YES;
  74. [self didChangeValueForKey:@"isExecuting"];
  75. [self didChangeValueForKey:@"isFinished"];
  76. }
  77. - (void)cancel
  78. {
  79. if (_isExecuting) {
  80. [self complete];
  81. }
  82. [super cancel];
  83. }
  84. - (void)poolNetworking
  85. {
  86. #ifndef EXTENSION
  87. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  88. #endif
  89. if([self respondsToSelector:NSSelectorFromString(_metadataNet.action)])
  90. [self performSelector:NSSelectorFromString(_metadataNet.action)];
  91. }
  92. #pragma --------------------------------------------------------------------------------------------
  93. #pragma mark ===== Delegate =====
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (void)complete
  96. {
  97. [self finish];
  98. #ifndef EXTENSION
  99. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  100. #endif
  101. }
  102. #pragma --------------------------------------------------------------------------------------------
  103. #pragma mark ===== download =====
  104. #pragma --------------------------------------------------------------------------------------------
  105. - (NSURLSessionTask *)downloadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication success:(void (^)(int64_t length))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  106. {
  107. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  108. [communication setUserAgent:[CCUtility getUserAgent]];
  109. NSURLSessionTask *sessionTask = [communication downloadFileSession:fileNameServerUrl toDestiny:fileNameLocalPath defaultPriority:YES onCommunication:communication progress:^(NSProgress *progress) {
  110. //float percent = roundf (progress.fractionCompleted * 100);
  111. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  112. int64_t totalUnitCount = 0;
  113. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  114. NSString *contentLength = [fields objectForKey:@"Content-Length"];
  115. if(contentLength) {
  116. totalUnitCount = (int64_t) [contentLength longLongValue];
  117. }
  118. success(totalUnitCount);
  119. } failureRequest:^(NSURLResponse *response, NSError *error) {
  120. NSString *message;
  121. NSInteger errorCode;
  122. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  123. errorCode = httpResponse.statusCode;
  124. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  125. errorCode = error.code;
  126. // Error
  127. if (errorCode == 503)
  128. message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  129. else
  130. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  131. failure(message, errorCode);
  132. }];
  133. return sessionTask;
  134. }
  135. #pragma --------------------------------------------------------------------------------------------
  136. #pragma mark ===== upload =====
  137. #pragma --------------------------------------------------------------------------------------------
  138. - (NSURLSessionTask *)uploadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication success:(void(^)(NSString *fileID, NSString *etag, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  139. {
  140. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  141. [communication setUserAgent:[CCUtility getUserAgent]];
  142. NSURLSessionTask *sessionTask = [communication uploadFileSession:fileNameLocalPath toDestiny:fileNameServerUrl onCommunication:communication progress:^(NSProgress *progress) {
  143. //float percent = roundf (progress.fractionCompleted * 100);
  144. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  145. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  146. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  147. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  148. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  149. success(fileID, etag, date);
  150. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  151. NSString *message;
  152. NSInteger errorCode;
  153. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  154. errorCode = httpResponse.statusCode;
  155. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  156. errorCode = error.code;
  157. // Error
  158. if (errorCode == 503)
  159. message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  160. else
  161. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  162. failure(message, errorCode);
  163. } failureBeforeRequest:^(NSError *error) {
  164. failure(@"", error.code);
  165. }];
  166. return sessionTask;
  167. }
  168. #pragma --------------------------------------------------------------------------------------------
  169. #pragma mark ===== downloadThumbnail =====
  170. #pragma --------------------------------------------------------------------------------------------
  171. - (void)downloadThumbnail
  172. {
  173. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  174. __block NSString *ext;
  175. NSInteger width = 0, height = 0;
  176. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  177. NSString *dimOfThumbnail = (NSString *)_metadataNet.options;
  178. if ([dimOfThumbnail.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  179. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  180. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  181. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  182. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  183. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileID, ext]]) {
  184. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)])
  185. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:nil errorCode:0];
  186. [self complete];
  187. return;
  188. }
  189. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  190. [communication setUserAgent:[CCUtility getUserAgent]];
  191. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:_metadataNet.fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  192. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  193. if ([recordAccount.account isEqualToString:_metadataNet.account] && [thumbnail length] > 0) {
  194. UIImage *thumbnailImage = [UIImage imageWithData:thumbnail];
  195. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  196. [CCGraphics saveIcoWithEtag:_metadataNet.fileID image:thumbnailImage writeToFile:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileID, ext] copy:NO move:NO fromPath:nil toPath:nil];
  197. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  198. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:nil errorCode:0];
  199. } else {
  200. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  201. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  202. }
  203. [self complete];
  204. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  205. NSString *message = [NSString new];
  206. NSInteger errorCode = [self downloadThumbnailFailureResponse:response error:error message:&message];
  207. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail]) {
  208. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:message errorCode:errorCode];
  209. }
  210. [self complete];
  211. }];
  212. }
  213. - (void)downloadThumbnailWithDimOfThumbnail:(NSString *)dimOfThumbnail fileName:(NSString *)fileName fileNameLocal:(NSString *)fileNameLocal success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  214. {
  215. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  216. __block NSString *ext;
  217. NSInteger width = 0, height = 0;
  218. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  219. if ([dimOfThumbnail.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  220. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  221. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  222. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  223. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  224. NSString *fileNamePathLocal = [NSString stringWithFormat:@"%@/%@.%@", directoryUser, fileNameLocal, ext];
  225. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePathLocal]) {
  226. success();
  227. } else {
  228. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  229. [communication setUserAgent:[CCUtility getUserAgent]];
  230. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  231. [CCGraphics saveIcoWithEtag:fileNameLocal image:[UIImage imageWithData:thumbnail] writeToFile:fileNamePathLocal copy:NO move:NO fromPath:nil toPath:nil];
  232. success();
  233. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  234. NSString *message = [NSString new];
  235. NSInteger errorCode = [self downloadThumbnailFailureResponse:response error:error message:&message];
  236. failure(message, errorCode);
  237. }];
  238. }
  239. }
  240. - (NSInteger)downloadThumbnailFailureResponse:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  241. {
  242. NSInteger errorCode = response.statusCode;
  243. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  244. errorCode = error.code;
  245. // Error
  246. if (errorCode == 503)
  247. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  248. else
  249. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  250. return errorCode;
  251. }
  252. #pragma --------------------------------------------------------------------------------------------
  253. #pragma mark ===== Read Folder =====
  254. #pragma --------------------------------------------------------------------------------------------
  255. - (void)readFolder
  256. {
  257. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  258. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  259. [communication setUserAgent:[CCUtility getUserAgent]];
  260. [communication readFolder:_metadataNet.serverUrl depth:_metadataNet.depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  261. // Test active account
  262. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  263. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  264. if ([self.delegate respondsToSelector:@selector(readFolderSuccessFailure:metadataFolder:metadatas:message:errorCode:)])
  265. [self.delegate readFolderSuccessFailure:_metadataNet metadataFolder:nil metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  266. } else {
  267. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  268. NSMutableArray *metadatas = [NSMutableArray new];
  269. tableMetadata *metadataFolder = [tableMetadata new];
  270. _metadataNet.directoryID = [self readFolderSuccessServerUrl:_metadataNet.serverUrl items:items account:_metadataNet.account metadatas:&metadatas metadataFolder:&metadataFolder];
  271. dispatch_async(dispatch_get_main_queue(), ^{
  272. if ([self.delegate respondsToSelector:@selector(readFolderSuccessFailure:metadataFolder:metadatas:message:errorCode:)])
  273. [self.delegate readFolderSuccessFailure:_metadataNet metadataFolder:metadataFolder metadatas:metadatas message:nil errorCode:0];
  274. });
  275. });
  276. }
  277. [self complete];
  278. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  279. NSString *message = [NSString new];
  280. NSInteger errorCode = [self readFolderFailureServerUrl:_metadataNet.serverUrl response:response error:error message:&message];
  281. if ([self.delegate respondsToSelector:@selector(readFolderSuccessFailure:metadataFolder:metadatas:message:errorCode:)]) {
  282. [self.delegate readFolderSuccessFailure:_metadataNet metadataFolder:nil metadatas:nil message:message errorCode:errorCode];
  283. }
  284. [self complete];
  285. }];
  286. }
  287. - (void)readFolderWithServerUrl:(NSString *)serverUrl depth:(NSString *)depth account:(NSString *)account success:(void(^)(NSArray *metadatas, tableMetadata *metadataFolder, NSString *directoryID))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  288. {
  289. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  290. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  291. [communication setUserAgent:[CCUtility getUserAgent]];
  292. [communication readFolder:serverUrl depth:depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  293. // Test active account
  294. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  295. if (![recordAccount.account isEqualToString:account]) {
  296. failure(NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil), k_CCErrorUserNotAvailble);
  297. } else {
  298. NSMutableArray *metadatas = [NSMutableArray new];
  299. tableMetadata *metadataFolder = [tableMetadata new];
  300. NSString *directoryID = [self readFolderSuccessServerUrl:serverUrl items:items account:account metadatas:&metadatas metadataFolder:&metadataFolder];
  301. success(metadatas, metadataFolder, directoryID);
  302. }
  303. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  304. NSString *message = [NSString new];
  305. NSInteger errorCode = [self readFolderFailureServerUrl:serverUrl response:response error:error message:&message];
  306. failure(message, errorCode);
  307. }];
  308. }
  309. - (NSInteger)readFolderFailureServerUrl:(NSString *)serverUrl response:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  310. {
  311. NSInteger errorCode = response.statusCode;
  312. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  313. errorCode = error.code;
  314. // Error
  315. if (errorCode == 503)
  316. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  317. else
  318. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  319. // Request trusted certificated
  320. if ([error code] == NSURLErrorServerCertificateUntrusted)
  321. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  322. // Activity
  323. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionReadFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  324. return errorCode;
  325. }
  326. - (NSString *)readFolderSuccessServerUrl:(NSString *)serverUrl items:(NSArray *)items account:(NSString *)account metadatas:(NSMutableArray **)metadatas metadataFolder:(tableMetadata **)metadataFolder
  327. {
  328. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  329. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  330. // Check items > 0
  331. if ([items count] == 0) {
  332. #ifndef EXTENSION
  333. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  334. [appDelegate messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  335. #endif
  336. return nil;
  337. }
  338. // directory [0]
  339. OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  340. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  341. NSString *directoryID = [[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDtoFolder.isEncrypted favorite:itemDtoFolder.isFavorite fileID:itemDtoFolder.ocId permissions:itemDtoFolder.permissions serverUrl:serverUrl].directoryID;
  342. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  343. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  344. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  345. NSString *directoryIDFolder;
  346. NSString *serverUrlFolder;
  347. // Metadata . (self Folder)
  348. if ([serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl]]) {
  349. // root folder
  350. serverUrlFolder = @"..";
  351. directoryIDFolder = @"00000000-0000-0000-0000-000000000000";
  352. *metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:@"." serverUrl:serverUrlFolder directoryID:directoryIDFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  353. } else {
  354. serverUrlFolder = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  355. directoryIDFolder = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrlFolder];
  356. if (!directoryIDFolder) {
  357. return directoryID;
  358. }
  359. *metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:[serverUrl lastPathComponent] serverUrl:serverUrlFolder directoryID:directoryIDFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  360. }
  361. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  362. NSString *first = [(OCFileDto*)a fileName];
  363. NSString *second = [(OCFileDto*)b fileName];
  364. return [[first lowercaseString] compare:[second lowercaseString]];
  365. }];
  366. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  367. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  368. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  369. // Skip hidden files
  370. if (fileName.length > 0) {
  371. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  372. continue;
  373. } else
  374. continue;
  375. if (itemDto.isDirectory) {
  376. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:fileName]];
  377. }
  378. // ----- BUG #942 ---------
  379. if ([itemDto.etag length] == 0) {
  380. #ifndef EXTENSION
  381. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  382. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  383. #endif
  384. continue;
  385. }
  386. // ------------------------
  387. [*metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  388. }
  389. return directoryID;
  390. }
  391. #pragma --------------------------------------------------------------------------------------------
  392. #pragma mark ===== Search =====
  393. #pragma --------------------------------------------------------------------------------------------
  394. - (void)search
  395. {
  396. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  397. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  398. [communication setUserAgent:[CCUtility getUserAgent]];
  399. NSString *path = [_activeUrl stringByAppendingString:dav];
  400. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  401. NSString *dateLastModified;
  402. if (_metadataNet.date) {
  403. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  404. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  405. [dateFormatter setLocale:enUSPOSIXLocale];
  406. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  407. dateLastModified = [dateFormatter stringFromDate:_metadataNet.date];
  408. }
  409. [communication search:path folder:folder fileName: [NSString stringWithFormat:@"%%%@%%", _metadataNet.fileName] depth:_metadataNet.depth dateLastModified:dateLastModified contentType:_metadataNet.contentType withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  410. // Test active account
  411. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  412. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  413. if ([self.delegate respondsToSelector:@selector(searchSuccessFailure:metadatas:message:errorCode:)])
  414. [self.delegate searchSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  415. [self complete];
  416. return;
  417. }
  418. NSMutableArray *metadatas = [NSMutableArray new];
  419. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  420. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  421. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  422. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  423. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  424. for(OCFileDto *itemDto in items) {
  425. NSString *serverUrl, *directoryID;
  426. BOOL isFolderEncrypted;
  427. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  428. // Skip hidden files
  429. if (fileName.length > 0) {
  430. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  431. continue;
  432. } else
  433. continue;
  434. // ----- BUG #942 ---------
  435. if ([itemDto.etag length] == 0) {
  436. #ifndef EXTENSION
  437. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  438. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  439. #endif
  440. continue;
  441. }
  442. // ------------------------
  443. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@/files/%@", dav, _activeUserID] withString:@""];
  444. if ([serverUrl hasPrefix:@"/"])
  445. serverUrl = [serverUrl substringFromIndex:1];
  446. if ([serverUrl hasSuffix:@"/"])
  447. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  448. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  449. if (itemDto.isDirectory) {
  450. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName]];
  451. }
  452. directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  453. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:_metadataNet.account];
  454. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  455. }
  456. dispatch_async(dispatch_get_main_queue(), ^{
  457. if ([self.delegate respondsToSelector:@selector(searchSuccessFailure:metadatas:message:errorCode:)])
  458. [self.delegate searchSuccessFailure:_metadataNet metadatas:metadatas message:nil errorCode:0];
  459. });
  460. });
  461. [self complete];
  462. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  463. NSInteger errorCode = response.statusCode;
  464. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  465. errorCode = error.code;
  466. // Error
  467. if ([self.delegate respondsToSelector:@selector(searchSuccessFailure:metadatas:message:errorCode:)]) {
  468. if (errorCode == 503)
  469. [self.delegate searchSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  470. else
  471. [self.delegate searchSuccessFailure:_metadataNet metadatas:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  472. }
  473. // Request trusted certificated
  474. if ([error code] == NSURLErrorServerCertificateUntrusted)
  475. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  476. [self complete];
  477. }];
  478. }
  479. #pragma --------------------------------------------------------------------------------------------
  480. #pragma mark ===== Setting Favorite =====
  481. #pragma --------------------------------------------------------------------------------------------
  482. - (void)settingFavorite
  483. {
  484. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  485. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  486. [communication setUserAgent:[CCUtility getUserAgent]];
  487. NSString *path = [_activeUrl stringByAppendingString:dav];
  488. [communication settingFavoriteServer:path andFileOrFolderPath:_metadataNet.fileName favorite:[_metadataNet.options boolValue] withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token) {
  489. // Test active account
  490. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  491. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  492. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccessFailure:message:errorCode:)])
  493. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  494. [self complete];
  495. return;
  496. }
  497. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccessFailure:message:errorCode:)])
  498. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:nil errorCode:0];
  499. [self complete];
  500. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  501. NSInteger errorCode = response.statusCode;
  502. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  503. errorCode = error.code;
  504. // Error
  505. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccessFailure:message:errorCode:)]) {
  506. if (errorCode == 503)
  507. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  508. else
  509. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  510. }
  511. // Request trusted certificated
  512. if ([error code] == NSURLErrorServerCertificateUntrusted)
  513. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  514. [self complete];
  515. }];
  516. }
  517. #pragma --------------------------------------------------------------------------------------------
  518. #pragma mark ===== Listing Favorites =====
  519. #pragma --------------------------------------------------------------------------------------------
  520. - (void)listingFavorites
  521. {
  522. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  523. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  524. [communication setUserAgent:[CCUtility getUserAgent]];
  525. NSString *path = [_activeUrl stringByAppendingString:dav];
  526. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  527. [communication listingFavorites:path folder:folder withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  528. // Test active account
  529. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  530. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  531. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)])
  532. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  533. [self complete];
  534. return;
  535. }
  536. NSMutableArray *metadatas = [NSMutableArray new];
  537. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  538. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  539. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  540. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  541. // Order by fileNamePath
  542. items = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  543. OCFileDto *record1 = obj1, *record2 = obj2;
  544. NSString *path1 = [[record1.filePath stringByAppendingString:record1.fileName] lowercaseString];
  545. NSString *path2 = [[record2.filePath stringByAppendingString:record2.fileName] lowercaseString];
  546. return [path1 compare:path2];
  547. }];
  548. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  549. for(OCFileDto *itemDto in items) {
  550. NSString *serverUrl, *directoryID;
  551. BOOL isFolderEncrypted;
  552. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  553. // Skip hidden files
  554. if (fileName.length > 0) {
  555. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  556. continue;
  557. } else
  558. continue;
  559. // ----- BUG #942 ---------
  560. if ([itemDto.etag length] == 0) {
  561. #ifndef EXTENSION
  562. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  563. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  564. #endif
  565. continue;
  566. }
  567. // ------------------------
  568. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@/files/%@", dav, _activeUserID] withString:@""];
  569. if ([serverUrl hasPrefix:@"/"])
  570. serverUrl = [serverUrl substringFromIndex:1];
  571. if ([serverUrl hasSuffix:@"/"])
  572. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  573. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  574. if (itemDto.isDirectory) {
  575. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName]];
  576. }
  577. directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  578. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:_metadataNet.account];
  579. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  580. }
  581. dispatch_async(dispatch_get_main_queue(), ^{
  582. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)])
  583. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:metadatas message:nil errorCode:0];
  584. });
  585. });
  586. [self complete];
  587. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  588. NSInteger errorCode = response.statusCode;
  589. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  590. errorCode = error.code;
  591. // Error
  592. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)]) {
  593. if (errorCode == 503)
  594. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  595. else
  596. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  597. }
  598. // Request trusted certificated
  599. if ([error code] == NSURLErrorServerCertificateUntrusted)
  600. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  601. [self complete];
  602. }];
  603. }
  604. #pragma --------------------------------------------------------------------------------------------
  605. #pragma mark ===== Create Folder =====
  606. #pragma --------------------------------------------------------------------------------------------
  607. - (void)createFolder
  608. {
  609. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  610. NSString *nameFolderURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  611. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  612. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  613. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  614. [communication setUserAgent:[CCUtility getUserAgent]];
  615. [communication createFolder:nameFolderURL onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  616. // Test active account
  617. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  618. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  619. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  620. [self.delegate createFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  621. [self complete];
  622. return;
  623. }
  624. NSDictionary *fields = [response allHeaderFields];
  625. _metadataNet.fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  626. _metadataNet.date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  627. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  628. [self.delegate createFolderSuccessFailure:_metadataNet message:nil errorCode:0];
  629. [self complete];
  630. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  631. NSString *message;
  632. if (([_metadataNet.fileName isEqualToString:autoUploadFileName] == YES && [_metadataNet.serverUrl isEqualToString:autoUploadDirectory] == YES))
  633. message = nil;
  634. else
  635. message = [CCError manageErrorOC:response.statusCode error:error];
  636. NSInteger errorCode = response.statusCode;
  637. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  638. errorCode = error.code;
  639. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  640. [self.delegate createFolderSuccessFailure:_metadataNet message:message errorCode:errorCode];
  641. // Request trusted certificated
  642. if ([error code] == NSURLErrorServerCertificateUntrusted)
  643. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  644. [self complete];
  645. } errorBeforeRequest:^(NSError *error) {
  646. NSString *message;
  647. if (([_metadataNet.fileName isEqualToString:autoUploadFileName] == YES && [_metadataNet.serverUrl isEqualToString:autoUploadDirectory] == YES))
  648. message = nil;
  649. else {
  650. if (error.code == OCErrorForbidenCharacters)
  651. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  652. else
  653. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  654. }
  655. // Error
  656. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)]) {
  657. if (error.code == 503)
  658. [self.delegate createFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  659. else
  660. [self.delegate createFolderSuccessFailure:_metadataNet message:message errorCode:error.code];
  661. }
  662. [self complete];
  663. }];
  664. }
  665. - (void)createFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl account:(NSString *)account success:(void(^)(NSString *fileID, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  666. {
  667. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  668. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  669. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  670. [communication setUserAgent:[CCUtility getUserAgent]];
  671. [communication createFolder:serverFileUrl onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  672. if (![[[NCManageDatabase sharedInstance] getAccountActive].account isEqualToString:account]) {
  673. failure(NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil), k_CCErrorUserNotAvailble);
  674. } else {
  675. NSDictionary *fields = [response allHeaderFields];
  676. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  677. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  678. success(fileID, date);
  679. }
  680. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  681. failure(@"", error.code);
  682. } errorBeforeRequest:^(NSError *error) {
  683. NSString *message;
  684. if (error.code == OCErrorForbidenCharacters)
  685. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  686. else
  687. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  688. failure(message, error.code);
  689. }];
  690. }
  691. #pragma --------------------------------------------------------------------------------------------
  692. #pragma mark ===== Delete =====
  693. #pragma --------------------------------------------------------------------------------------------
  694. - (void)deleteFileOrFolder
  695. {
  696. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  697. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  698. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  699. [communication setUserAgent:[CCUtility getUserAgent]];
  700. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  701. // Test active account
  702. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  703. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  704. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)])
  705. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  706. [self complete];
  707. return;
  708. }
  709. if ([_metadataNet.selector rangeOfString:selectorDelete].location != NSNotFound && [self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)])
  710. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:@"" errorCode:0];
  711. [self complete];
  712. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  713. NSString *message = [NSString new];
  714. NSInteger errorCode = [self deleteFileOrFolderFailureServerUrl:_metadataNet.serverUrl response:response error:error message:&message];
  715. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)]) {
  716. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:message errorCode:errorCode];
  717. }
  718. [self complete];
  719. }];
  720. }
  721. - (void)deleteFileOrFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  722. {
  723. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  724. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  725. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  726. [communication setUserAgent:[CCUtility getUserAgent]];
  727. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  728. success();
  729. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  730. NSString *message = [NSString new];
  731. NSInteger errorCode = [self deleteFileOrFolderFailureServerUrl:serverUrl response:response error:error message:&message];
  732. failure(message, errorCode);
  733. }];
  734. }
  735. - (NSInteger)deleteFileOrFolderFailureServerUrl:(NSString *)serverUrl response:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  736. {
  737. NSInteger errorCode = response.statusCode;
  738. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  739. errorCode = error.code;
  740. // Error
  741. if (errorCode == 503)
  742. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  743. else
  744. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  745. // Request trusted certificated
  746. if ([error code] == NSURLErrorServerCertificateUntrusted)
  747. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  748. // Activity
  749. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionDeleteFileFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  750. return errorCode;
  751. }
  752. #pragma --------------------------------------------------------------------------------------------
  753. #pragma mark ===== Move =====
  754. #pragma --------------------------------------------------------------------------------------------
  755. - (void)moveFileOrFolder
  756. {
  757. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  758. NSString *origineURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  759. NSString *destinazioneURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrlTo, _metadataNet.fileNameTo];
  760. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  761. [communication setUserAgent:[CCUtility getUserAgent]];
  762. [communication moveFileOrFolder:origineURL toDestiny:destinazioneURL onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  763. if ([_metadataNet.selector isEqualToString:selectorRename] && [self.delegate respondsToSelector:@selector(renameSuccess:)])
  764. [self.delegate renameSuccess:_metadataNet];
  765. if ([_metadataNet.selector rangeOfString:selectorMove].location != NSNotFound && [self.delegate respondsToSelector:@selector(moveSuccess:)])
  766. [self.delegate moveSuccess:_metadataNet];
  767. [self complete];
  768. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  769. NSInteger errorCode = response.statusCode;
  770. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  771. errorCode = error.code;
  772. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)])
  773. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  774. // Request trusted certificated
  775. if ([error code] == NSURLErrorServerCertificateUntrusted)
  776. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  777. [self complete];
  778. } errorBeforeRequest:^(NSError *error) {
  779. NSString *message;
  780. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  781. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  782. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  783. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  784. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  785. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  786. } else {
  787. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  788. }
  789. // Error
  790. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)]) {
  791. if (error.code == 503)
  792. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  793. else
  794. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:message errorCode:error.code];
  795. }
  796. [self complete];
  797. }];
  798. }
  799. #pragma --------------------------------------------------------------------------------------------
  800. #pragma mark ===== ReadFile =====
  801. #pragma --------------------------------------------------------------------------------------------
  802. - (void)readFile
  803. {
  804. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  805. NSString *fileName;
  806. if (_metadataNet.fileName) {
  807. fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  808. } else {
  809. _metadataNet.fileName = @".";
  810. fileName = _metadataNet.serverUrl;
  811. }
  812. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  813. [communication setUserAgent:[CCUtility getUserAgent]];
  814. [communication readFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  815. // Test active account
  816. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  817. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  818. if ([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  819. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  820. [self complete];
  821. return;
  822. }
  823. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:_metadataNet.serverUrl account:_metadataNet.account];
  824. if ([recordAccount.account isEqualToString:_metadataNet.account] && [items count] > 0) {
  825. tableMetadata *metadata = [tableMetadata new];
  826. OCFileDto *itemDto = [items objectAtIndex:0];
  827. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_metadataNet.serverUrl];
  828. if (directoryID) {
  829. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  830. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  831. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  832. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileName:_metadataNet.fileName serverUrl:_metadataNet.serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  833. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  834. [self.delegate readFileSuccessFailure:_metadataNet metadata:metadata message:nil errorCode:0];
  835. } else {
  836. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  837. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:@"Directory not found" errorCode:k_CCErrorInternalError];
  838. }
  839. }
  840. // BUG 1038
  841. if ([items count] == 0) {
  842. #ifndef EXTENSION
  843. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  844. [appDelegate messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  845. #endif
  846. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  847. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:@"Read File WebDAV : [items NULL] please fix" errorCode:k_CCErrorInternalError];
  848. }
  849. [self complete];
  850. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  851. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  852. _metadataNet.errorRetry++;
  853. NSInteger errorCode = response.statusCode;
  854. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  855. errorCode = error.code;
  856. // Error
  857. if ([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)] && [recordAccount.account isEqualToString:_metadataNet.account]) {
  858. if (errorCode == 503)
  859. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  860. else
  861. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  862. }
  863. // Request trusted certificated
  864. if ([error code] == NSURLErrorServerCertificateUntrusted)
  865. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  866. [self complete];
  867. }];
  868. }
  869. #pragma --------------------------------------------------------------------------------------------
  870. #pragma mark ===== Shared =====
  871. #pragma --------------------------------------------------------------------------------------------
  872. - (void)readShareServer
  873. {
  874. #ifndef EXTENSION
  875. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  876. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  877. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  878. [communication setUserAgent:[CCUtility getUserAgent]];
  879. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  880. // Test active account
  881. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  882. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  883. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  884. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  885. [self complete];
  886. return;
  887. }
  888. BOOL openWindow = NO;
  889. [appDelegate.sharesID removeAllObjects];
  890. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  891. for (OCSharedDto *item in items)
  892. [appDelegate.sharesID setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  893. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  894. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  895. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  896. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  897. }
  898. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  899. [self.delegate readSharedSuccess:_metadataNet items:appDelegate.sharesID openWindow:openWindow];
  900. [self complete];
  901. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  902. NSInteger errorCode = response.statusCode;
  903. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  904. errorCode = error.code;
  905. // Error
  906. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  907. if (errorCode == 503)
  908. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  909. else
  910. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  911. }
  912. // Request trusted certificated
  913. if ([error code] == NSURLErrorServerCertificateUntrusted)
  914. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  915. [self complete];
  916. }];
  917. #endif
  918. }
  919. - (void)share
  920. {
  921. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  922. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  923. [communication setUserAgent:[CCUtility getUserAgent]];
  924. [communication shareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPassword:[_metadataNet.password encodeString:NSUTF8StringEncoding] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  925. [self readShareServer];
  926. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  927. NSInteger errorCode = response.statusCode;
  928. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  929. errorCode = error.code;
  930. // Error
  931. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  932. if (errorCode == 503)
  933. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  934. else
  935. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  936. }
  937. // Request trusted certificated
  938. if ([error code] == NSURLErrorServerCertificateUntrusted)
  939. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  940. [self complete];
  941. }];
  942. }
  943. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  944. - (void)shareWith
  945. {
  946. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  947. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  948. [communication setUserAgent:[CCUtility getUserAgent]];
  949. [communication shareWith:_metadataNet.share shareeType:_metadataNet.shareeType inServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPermissions:_metadataNet.sharePermission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  950. [self readShareServer];
  951. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  952. NSInteger errorCode = response.statusCode;
  953. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  954. errorCode = error.code;
  955. // Error
  956. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  957. if (errorCode == 503)
  958. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  959. else
  960. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  961. }
  962. // Request trusted certificated
  963. if ([error code] == NSURLErrorServerCertificateUntrusted)
  964. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  965. [self complete];
  966. }];
  967. }
  968. - (void)updateShare
  969. {
  970. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  971. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  972. [communication setUserAgent:[CCUtility getUserAgent]];
  973. [communication updateShare:[_metadataNet.share intValue] ofServerPath:[_activeUrl stringByAppendingString:@"/"] withPasswordProtect:[_metadataNet.password encodeString:NSUTF8StringEncoding] andExpirationTime:_metadataNet.expirationTime andPermissions:_metadataNet.sharePermission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  974. [self readShareServer];
  975. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  976. #ifndef EXTENSION
  977. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  978. [appDelegate messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  979. #endif
  980. NSInteger errorCode = response.statusCode;
  981. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  982. errorCode = error.code;
  983. // Error
  984. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  985. if (errorCode == 503)
  986. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  987. else
  988. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  989. }
  990. // Request trusted certificated
  991. if ([error code] == NSURLErrorServerCertificateUntrusted)
  992. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  993. [self complete];
  994. }];
  995. }
  996. - (void)unShare
  997. {
  998. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  999. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1000. [communication setUserAgent:[CCUtility getUserAgent]];
  1001. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1002. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  1003. [self.delegate unShareSuccess:_metadataNet];
  1004. [self complete];
  1005. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1006. #ifndef EXTENSION
  1007. [(AppDelegate *)[[UIApplication sharedApplication] delegate] messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  1008. #endif
  1009. NSInteger errorCode = response.statusCode;
  1010. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1011. errorCode = error.code;
  1012. // Error
  1013. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  1014. if (errorCode == 503)
  1015. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1016. else
  1017. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1018. }
  1019. // Request trusted certificated
  1020. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1021. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1022. [self complete];
  1023. }];
  1024. }
  1025. - (void)getUserAndGroup
  1026. {
  1027. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1028. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1029. [communication setUserAgent:[CCUtility getUserAgent]];
  1030. [communication searchUsersAndGroupsWith:_metadataNet.options forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  1031. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  1032. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  1033. [self complete];
  1034. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1035. NSInteger errorCode = response.statusCode;
  1036. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1037. errorCode = error.code;
  1038. // Error
  1039. if ([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)]) {
  1040. if (errorCode == 503)
  1041. [self.delegate getUserAndGroupFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1042. else
  1043. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1044. }
  1045. // Request trusted certificated
  1046. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1047. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1048. [self complete];
  1049. }];
  1050. }
  1051. - (void)getSharePermissionsFile
  1052. {
  1053. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1054. NSString *fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  1055. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1056. [communication setUserAgent:[CCUtility getUserAgent]];
  1057. [communication getSharePermissionsFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *permissions, NSString *redirectedServer) {
  1058. // Test active account
  1059. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1060. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1061. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)])
  1062. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1063. [self complete];
  1064. return;
  1065. }
  1066. if([self.delegate respondsToSelector:@selector(getSharePermissionsFileSuccess:permissions:)])
  1067. [self.delegate getSharePermissionsFileSuccess:_metadataNet permissions:permissions];
  1068. [self complete];
  1069. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1070. NSInteger errorCode = response.statusCode;
  1071. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1072. errorCode = error.code;
  1073. // Error
  1074. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)]) {
  1075. if (errorCode == 503)
  1076. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1077. else
  1078. [self.delegate getSharePermissionsFileFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1079. }
  1080. // Request trusted certificated
  1081. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1082. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1083. [self complete];
  1084. }];
  1085. }
  1086. #pragma --------------------------------------------------------------------------------------------
  1087. #pragma mark ===== Activity =====
  1088. #pragma --------------------------------------------------------------------------------------------
  1089. - (void)getActivityServer
  1090. {
  1091. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1092. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1093. [communication setUserAgent:[CCUtility getUserAgent]];
  1094. [communication getActivityServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  1095. // Test active account
  1096. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1097. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1098. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)])
  1099. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1100. [self complete];
  1101. return;
  1102. }
  1103. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)])
  1104. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:listOfActivity message:nil errorCode:0];
  1105. [self complete];
  1106. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1107. NSInteger errorCode = response.statusCode;
  1108. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1109. errorCode = error.code;
  1110. // Error
  1111. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)]) {
  1112. if (errorCode == 503)
  1113. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1114. else
  1115. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1116. }
  1117. // Request trusted certificated
  1118. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1119. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1120. [self complete];
  1121. }];
  1122. }
  1123. #pragma --------------------------------------------------------------------------------------------
  1124. #pragma mark ===== External Sites =====
  1125. #pragma --------------------------------------------------------------------------------------------
  1126. - (void)getExternalSitesServer
  1127. {
  1128. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1129. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1130. [communication setUserAgent:[CCUtility getUserAgent]];
  1131. [communication getExternalSitesServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1132. // Test active account
  1133. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1134. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1135. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)])
  1136. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1137. [self complete];
  1138. return;
  1139. }
  1140. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)])
  1141. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:listOfExternalSites message:nil errorCode:0];
  1142. [self complete];
  1143. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1144. NSInteger errorCode = response.statusCode;
  1145. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1146. errorCode = error.code;
  1147. // Error
  1148. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)]) {
  1149. if (errorCode == 503)
  1150. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1151. else
  1152. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1153. }
  1154. // Request trusted certificated
  1155. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1156. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1157. [self complete];
  1158. }];
  1159. }
  1160. #pragma --------------------------------------------------------------------------------------------
  1161. #pragma mark ===== Middleware Ping =====
  1162. #pragma --------------------------------------------------------------------------------------------
  1163. /*
  1164. - (void)middlewarePing
  1165. {
  1166. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1167. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1168. [communication setUserAgent:[CCUtility getUserAgent]];
  1169. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1170. [self complete];
  1171. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1172. NSInteger errorCode = response.statusCode;
  1173. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1174. errorCode = error.code;
  1175. // Error
  1176. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  1177. if (errorCode == 503)
  1178. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1179. else
  1180. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1181. }
  1182. // Request trusted certificated
  1183. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1184. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1185. [self complete];
  1186. }];
  1187. }
  1188. */
  1189. #pragma --------------------------------------------------------------------------------------------
  1190. #pragma mark ===== Notification =====
  1191. #pragma --------------------------------------------------------------------------------------------
  1192. - (void)getNotificationServer
  1193. {
  1194. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1195. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1196. [communication setUserAgent:[CCUtility getUserAgent]];
  1197. [communication getNotificationServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  1198. // Test active account
  1199. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1200. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1201. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
  1202. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1203. [self complete];
  1204. return;
  1205. }
  1206. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
  1207. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:listOfNotifications message:nil errorCode:0];
  1208. [self complete];
  1209. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1210. NSInteger errorCode = response.statusCode;
  1211. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1212. errorCode = error.code;
  1213. // Error
  1214. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)]) {
  1215. if (errorCode == 503)
  1216. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1217. else
  1218. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1219. }
  1220. // Request trusted certificated
  1221. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1222. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1223. // Activity
  1224. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1225. [self complete];
  1226. }];
  1227. }
  1228. - (void)setNotificationServer
  1229. {
  1230. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1231. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1232. [communication setUserAgent:[CCUtility getUserAgent]];
  1233. NSString *type = _metadataNet.options;
  1234. [communication setNotificationServer:_metadataNet.serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1235. // Test active account
  1236. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1237. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1238. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
  1239. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1240. [self complete];
  1241. return;
  1242. }
  1243. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
  1244. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:nil errorCode:0];
  1245. [self complete];
  1246. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1247. NSInteger errorCode = response.statusCode;
  1248. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1249. errorCode = error.code;
  1250. // Error
  1251. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)]) {
  1252. if (errorCode == 503)
  1253. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1254. else
  1255. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1256. }
  1257. // Request trusted certificated
  1258. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1259. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1260. [self complete];
  1261. }];
  1262. }
  1263. - (void)subscribingNextcloudServer
  1264. {
  1265. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1266. communication.kindOfCredential = credentialNotSet;
  1267. [communication setUserAgent:[CCUtility getUserAgent]];
  1268. NSDictionary *parameter = _metadataNet.options;
  1269. NSString *pushToken = [parameter objectForKey:@"pushToken"];
  1270. NSString *pushTokenHash = [parameter objectForKey:@"pushTokenHash"];
  1271. NSString *devicePublicKey = [parameter objectForKey:@"devicePublicKey"];
  1272. // encode URL
  1273. devicePublicKey = [devicePublicKey stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1274. [communication subscribingNextcloudServerPush:_activeUrl pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: [NCBrandOptions sharedInstance].pushNotificationServer onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1275. // encode URL
  1276. deviceIdentifier = [deviceIdentifier stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1277. signature = [signature stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1278. publicKey = [publicKey stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1279. [communication subscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature userPublicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1280. // Activity
  1281. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:@"Service registered." type:k_activityTypeSuccess verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1282. [self complete];
  1283. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1284. NSInteger errorCode = response.statusCode;
  1285. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1286. errorCode = error.code;
  1287. // Error
  1288. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1289. if (errorCode == 503)
  1290. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1291. else
  1292. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1293. }
  1294. // Request trusted certificated
  1295. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1296. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1297. // Activity
  1298. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1299. [self complete];
  1300. }];
  1301. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1302. NSInteger errorCode = response.statusCode;
  1303. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1304. errorCode = error.code;
  1305. // Error
  1306. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1307. if (errorCode == 503)
  1308. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1309. else
  1310. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1311. }
  1312. // Request trusted certificated
  1313. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1314. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1315. // Activity
  1316. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1317. [self complete];
  1318. }];
  1319. }
  1320. #pragma --------------------------------------------------------------------------------------------
  1321. #pragma mark ===== User Profile =====
  1322. #pragma --------------------------------------------------------------------------------------------
  1323. - (void)getUserProfile
  1324. {
  1325. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1326. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1327. [communication setUserAgent:[CCUtility getUserAgent]];
  1328. [communication getUserProfileServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1329. // Test active account
  1330. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1331. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1332. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
  1333. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1334. [self complete];
  1335. return;
  1336. }
  1337. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
  1338. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:userProfile message:nil errorCode:0];
  1339. [self complete];
  1340. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1341. NSInteger errorCode = response.statusCode;
  1342. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1343. errorCode = error.code;
  1344. // Error
  1345. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)]) {
  1346. if (errorCode == 503)
  1347. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1348. else
  1349. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1350. }
  1351. // Request trusted certificated
  1352. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1353. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1354. [self complete];
  1355. }];
  1356. }
  1357. #pragma --------------------------------------------------------------------------------------------
  1358. #pragma mark ===== Capabilities =====
  1359. #pragma --------------------------------------------------------------------------------------------
  1360. - (void)getCapabilitiesOfServer
  1361. {
  1362. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1363. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1364. [communication setUserAgent:[CCUtility getUserAgent]];
  1365. [communication getCapabilitiesOfServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1366. // Test active account
  1367. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1368. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1369. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)])
  1370. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1371. [self complete];
  1372. return;
  1373. }
  1374. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)])
  1375. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:capabilities message:nil errorCode:0];
  1376. [self complete];
  1377. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1378. NSInteger errorCode = response.statusCode;
  1379. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1380. errorCode = error.code;
  1381. // Error
  1382. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)]) {
  1383. if (errorCode == 503)
  1384. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1385. else
  1386. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1387. }
  1388. // Request trusted certificated
  1389. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1390. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1391. // Activity
  1392. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionCapabilities selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1393. [self complete];
  1394. }];
  1395. }
  1396. #pragma --------------------------------------------------------------------------------------------
  1397. #pragma mark ===== End-to-End Encryption =====
  1398. #pragma --------------------------------------------------------------------------------------------
  1399. - (void)getEndToEndPublicKeys
  1400. {
  1401. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1402. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1403. [communication setUserAgent:[CCUtility getUserAgent]];
  1404. [communication getEndToEndPublicKeys:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1405. _metadataNet.key = publicKey;
  1406. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysSuccess:)])
  1407. [self.delegate getEndToEndPublicKeysSuccess:_metadataNet];
  1408. [self complete];
  1409. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1410. NSInteger errorCode = response.statusCode;
  1411. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1412. errorCode = error.code;
  1413. // Error
  1414. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysFailure:message:errorCode:)])
  1415. [self.delegate getEndToEndPublicKeysFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1416. // Request trusted certificated
  1417. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1418. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1419. [self complete];
  1420. }];
  1421. }
  1422. - (void)getEndToEndPrivateKeyCipher
  1423. {
  1424. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1425. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1426. [communication setUserAgent:[CCUtility getUserAgent]];
  1427. [communication getEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer) {
  1428. _metadataNet.key = privateKeyChiper;
  1429. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherSuccess:)])
  1430. [self.delegate getEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1431. [self complete];
  1432. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1433. NSInteger errorCode = response.statusCode;
  1434. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1435. errorCode = error.code;
  1436. // Error
  1437. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1438. [self.delegate getEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1439. // Request trusted certificated
  1440. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1441. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1442. [self complete];
  1443. }];
  1444. }
  1445. - (void)signEndToEndPublicKey
  1446. {
  1447. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1448. // URL Encode
  1449. NSString *publicKey = [CCUtility URLEncodeStringFromString:_metadataNet.key];
  1450. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1451. [communication setUserAgent:[CCUtility getUserAgent]];
  1452. [communication signEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1453. _metadataNet.key = publicKey;
  1454. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeySuccess:)])
  1455. [self.delegate signEndToEndPublicKeySuccess:_metadataNet];
  1456. [self complete];
  1457. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1458. NSInteger errorCode = response.statusCode;
  1459. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1460. errorCode = error.code;
  1461. // Error
  1462. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeyFailure:message:errorCode:)])
  1463. [self.delegate signEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1464. // Request trusted certificated
  1465. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1466. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1467. [self complete];
  1468. }];
  1469. }
  1470. - (void)storeEndToEndPrivateKeyCipher
  1471. {
  1472. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1473. // URL Encode
  1474. NSString *privateKeyChiper = [CCUtility URLEncodeStringFromString:_metadataNet.keyCipher];
  1475. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1476. [communication setUserAgent:[CCUtility getUserAgent]];
  1477. [communication storeEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] privateKeyChiper:privateKeyChiper onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer) {
  1478. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherSuccess:)])
  1479. [self.delegate storeEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1480. [self complete];
  1481. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1482. NSInteger errorCode = response.statusCode;
  1483. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1484. errorCode = error.code;
  1485. // Error
  1486. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1487. [self.delegate storeEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1488. // Request trusted certificated
  1489. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1490. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1491. [self complete];
  1492. }];
  1493. }
  1494. - (void)deleteEndToEndPublicKey
  1495. {
  1496. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1497. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1498. [communication setUserAgent:[CCUtility getUserAgent]];
  1499. [communication deleteEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1500. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeySuccess:)])
  1501. [self.delegate deleteEndToEndPublicKeySuccess:_metadataNet];
  1502. [self complete];
  1503. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1504. NSInteger errorCode = response.statusCode;
  1505. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1506. errorCode = error.code;
  1507. // Error
  1508. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeyFailure:message:errorCode:)])
  1509. [self.delegate deleteEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1510. // Request trusted certificated
  1511. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1512. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1513. [self complete];
  1514. }];
  1515. }
  1516. - (void)deleteEndToEndPrivateKey
  1517. {
  1518. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1519. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1520. [communication setUserAgent:[CCUtility getUserAgent]];
  1521. [communication deleteEndToEndPrivateKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1522. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeySuccess:)])
  1523. [self.delegate deleteEndToEndPrivateKeySuccess:_metadataNet];
  1524. [self complete];
  1525. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1526. NSInteger errorCode = response.statusCode;
  1527. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1528. errorCode = error.code;
  1529. // Error
  1530. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeyFailure:message:errorCode:)])
  1531. [self.delegate deleteEndToEndPrivateKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1532. // Request trusted certificated
  1533. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1534. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1535. [self complete];
  1536. }];
  1537. }
  1538. - (void)getEndToEndServerPublicKey
  1539. {
  1540. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1541. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1542. [communication setUserAgent:[CCUtility getUserAgent]];
  1543. [communication getEndToEndServerPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1544. _metadataNet.key = publicKey;
  1545. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeySuccess:)])
  1546. [self.delegate getEndToEndServerPublicKeySuccess:_metadataNet];
  1547. [self complete];
  1548. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1549. NSInteger errorCode = response.statusCode;
  1550. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1551. errorCode = error.code;
  1552. // Error
  1553. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeyFailure:message:errorCode:)])
  1554. [self.delegate getEndToEndServerPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1555. // Request trusted certificated
  1556. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1557. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1558. [self complete];
  1559. }];
  1560. }
  1561. @end
  1562. #pragma --------------------------------------------------------------------------------------------
  1563. #pragma mark ===== OCURLSessionManager =====
  1564. #pragma --------------------------------------------------------------------------------------------
  1565. @implementation OCURLSessionManager
  1566. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1567. {
  1568. // The pinnning check
  1569. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  1570. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1571. } else {
  1572. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1573. }
  1574. }
  1575. @end