OCNetworking.m 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  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. - (void)settingFavorite:(NSString *)fileName serverUrl:(NSString *)serverUrl favorite:(BOOL)favorite success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  518. {
  519. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  520. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  521. [communication setUserAgent:[CCUtility getUserAgent]];
  522. NSString *server = [_activeUrl stringByAppendingString:dav];
  523. NSString *fileOrFolderPath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:_activeUrl];
  524. [communication settingFavoriteServer:server andFileOrFolderPath:fileOrFolderPath favorite:favorite withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token) {
  525. success();
  526. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  527. NSInteger errorCode = response.statusCode;
  528. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  529. errorCode = error.code;
  530. NSString *message = [CCError manageErrorOC:response.statusCode error:error];
  531. failure(message, error.code);
  532. }];
  533. }
  534. #pragma --------------------------------------------------------------------------------------------
  535. #pragma mark ===== Listing Favorites =====
  536. #pragma --------------------------------------------------------------------------------------------
  537. - (void)listingFavorites
  538. {
  539. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  540. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  541. [communication setUserAgent:[CCUtility getUserAgent]];
  542. NSString *path = [_activeUrl stringByAppendingString:dav];
  543. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  544. [communication listingFavorites:path folder:folder withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  545. // Test active account
  546. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  547. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  548. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)])
  549. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  550. [self complete];
  551. return;
  552. }
  553. NSMutableArray *metadatas = [NSMutableArray new];
  554. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  555. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  556. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  557. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  558. // Order by fileNamePath
  559. items = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  560. OCFileDto *record1 = obj1, *record2 = obj2;
  561. NSString *path1 = [[record1.filePath stringByAppendingString:record1.fileName] lowercaseString];
  562. NSString *path2 = [[record2.filePath stringByAppendingString:record2.fileName] lowercaseString];
  563. return [path1 compare:path2];
  564. }];
  565. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  566. for(OCFileDto *itemDto in items) {
  567. NSString *serverUrl, *directoryID;
  568. BOOL isFolderEncrypted;
  569. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  570. // Skip hidden files
  571. if (fileName.length > 0) {
  572. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  573. continue;
  574. } else
  575. continue;
  576. // ----- BUG #942 ---------
  577. if ([itemDto.etag length] == 0) {
  578. #ifndef EXTENSION
  579. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  580. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  581. #endif
  582. continue;
  583. }
  584. // ------------------------
  585. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@/files/%@", dav, _activeUserID] withString:@""];
  586. if ([serverUrl hasPrefix:@"/"])
  587. serverUrl = [serverUrl substringFromIndex:1];
  588. if ([serverUrl hasSuffix:@"/"])
  589. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  590. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  591. if (itemDto.isDirectory) {
  592. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName]];
  593. }
  594. directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  595. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:_metadataNet.account];
  596. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  597. }
  598. dispatch_async(dispatch_get_main_queue(), ^{
  599. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)])
  600. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:metadatas message:nil errorCode:0];
  601. });
  602. });
  603. [self complete];
  604. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  605. NSInteger errorCode = response.statusCode;
  606. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  607. errorCode = error.code;
  608. // Error
  609. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)]) {
  610. if (errorCode == 503)
  611. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  612. else
  613. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  614. }
  615. // Request trusted certificated
  616. if ([error code] == NSURLErrorServerCertificateUntrusted)
  617. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  618. [self complete];
  619. }];
  620. }
  621. #pragma --------------------------------------------------------------------------------------------
  622. #pragma mark ===== Create Folder =====
  623. #pragma --------------------------------------------------------------------------------------------
  624. - (void)createFolder
  625. {
  626. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  627. NSString *nameFolderURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  628. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  629. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  630. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  631. [communication setUserAgent:[CCUtility getUserAgent]];
  632. [communication createFolder:nameFolderURL onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  633. // Test active account
  634. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  635. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  636. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  637. [self.delegate createFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  638. [self complete];
  639. return;
  640. }
  641. NSDictionary *fields = [response allHeaderFields];
  642. _metadataNet.fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  643. _metadataNet.date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  644. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  645. [self.delegate createFolderSuccessFailure:_metadataNet message:nil errorCode:0];
  646. [self complete];
  647. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  648. NSString *message;
  649. if (([_metadataNet.fileName isEqualToString:autoUploadFileName] == YES && [_metadataNet.serverUrl isEqualToString:autoUploadDirectory] == YES))
  650. message = nil;
  651. else
  652. message = [CCError manageErrorOC:response.statusCode error:error];
  653. NSInteger errorCode = response.statusCode;
  654. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  655. errorCode = error.code;
  656. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  657. [self.delegate createFolderSuccessFailure:_metadataNet message:message errorCode:errorCode];
  658. // Request trusted certificated
  659. if ([error code] == NSURLErrorServerCertificateUntrusted)
  660. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  661. [self complete];
  662. } errorBeforeRequest:^(NSError *error) {
  663. NSString *message;
  664. if (([_metadataNet.fileName isEqualToString:autoUploadFileName] == YES && [_metadataNet.serverUrl isEqualToString:autoUploadDirectory] == YES))
  665. message = nil;
  666. else {
  667. if (error.code == OCErrorForbidenCharacters)
  668. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  669. else
  670. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  671. }
  672. // Error
  673. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)]) {
  674. if (error.code == 503)
  675. [self.delegate createFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  676. else
  677. [self.delegate createFolderSuccessFailure:_metadataNet message:message errorCode:error.code];
  678. }
  679. [self complete];
  680. }];
  681. }
  682. - (void)createFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl account:(NSString *)account success:(void(^)(NSString *fileID, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  683. {
  684. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  685. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  686. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  687. [communication setUserAgent:[CCUtility getUserAgent]];
  688. [communication createFolder:serverFileUrl onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  689. if (![[[NCManageDatabase sharedInstance] getAccountActive].account isEqualToString:account]) {
  690. failure(NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil), k_CCErrorUserNotAvailble);
  691. } else {
  692. NSDictionary *fields = [response allHeaderFields];
  693. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  694. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  695. success(fileID, date);
  696. }
  697. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  698. failure(@"", error.code);
  699. } errorBeforeRequest:^(NSError *error) {
  700. NSString *message;
  701. if (error.code == OCErrorForbidenCharacters)
  702. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  703. else
  704. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  705. failure(message, error.code);
  706. }];
  707. }
  708. #pragma --------------------------------------------------------------------------------------------
  709. #pragma mark ===== Delete =====
  710. #pragma --------------------------------------------------------------------------------------------
  711. - (void)deleteFileOrFolder
  712. {
  713. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  714. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  715. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  716. [communication setUserAgent:[CCUtility getUserAgent]];
  717. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  718. // Test active account
  719. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  720. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  721. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)])
  722. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  723. [self complete];
  724. return;
  725. }
  726. if ([_metadataNet.selector rangeOfString:selectorDelete].location != NSNotFound && [self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)])
  727. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:@"" errorCode:0];
  728. [self complete];
  729. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  730. NSString *message = [NSString new];
  731. NSInteger errorCode = [self deleteFileOrFolderFailureServerUrl:_metadataNet.serverUrl response:response error:error message:&message];
  732. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)]) {
  733. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:message errorCode:errorCode];
  734. }
  735. [self complete];
  736. }];
  737. }
  738. - (void)deleteFileOrFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  739. {
  740. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  741. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  742. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  743. [communication setUserAgent:[CCUtility getUserAgent]];
  744. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  745. success();
  746. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  747. NSString *message = [NSString new];
  748. NSInteger errorCode = [self deleteFileOrFolderFailureServerUrl:serverUrl response:response error:error message:&message];
  749. failure(message, errorCode);
  750. }];
  751. }
  752. - (NSInteger)deleteFileOrFolderFailureServerUrl:(NSString *)serverUrl response:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  753. {
  754. NSInteger errorCode = response.statusCode;
  755. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  756. errorCode = error.code;
  757. // Error
  758. if (errorCode == 503)
  759. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  760. else
  761. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  762. // Request trusted certificated
  763. if ([error code] == NSURLErrorServerCertificateUntrusted)
  764. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  765. // Activity
  766. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionDeleteFileFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  767. return errorCode;
  768. }
  769. #pragma --------------------------------------------------------------------------------------------
  770. #pragma mark ===== Move =====
  771. #pragma --------------------------------------------------------------------------------------------
  772. - (void)moveFileOrFolder
  773. {
  774. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  775. NSString *origineURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  776. NSString *destinazioneURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrlTo, _metadataNet.fileNameTo];
  777. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  778. [communication setUserAgent:[CCUtility getUserAgent]];
  779. [communication moveFileOrFolder:origineURL toDestiny:destinazioneURL onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  780. if ([_metadataNet.selector isEqualToString:selectorRename] && [self.delegate respondsToSelector:@selector(renameSuccess:)])
  781. [self.delegate renameSuccess:_metadataNet];
  782. if ([_metadataNet.selector rangeOfString:selectorMove].location != NSNotFound && [self.delegate respondsToSelector:@selector(moveSuccess:)])
  783. [self.delegate moveSuccess:_metadataNet];
  784. [self complete];
  785. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  786. NSInteger errorCode = response.statusCode;
  787. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  788. errorCode = error.code;
  789. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)])
  790. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  791. // Request trusted certificated
  792. if ([error code] == NSURLErrorServerCertificateUntrusted)
  793. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  794. [self complete];
  795. } errorBeforeRequest:^(NSError *error) {
  796. NSString *message;
  797. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  798. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  799. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  800. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  801. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  802. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  803. } else {
  804. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  805. }
  806. // Error
  807. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)]) {
  808. if (error.code == 503)
  809. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  810. else
  811. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:message errorCode:error.code];
  812. }
  813. [self complete];
  814. }];
  815. }
  816. - (void)moveFileOrFolder:(NSString *)fileName fileNameTo:(NSString *)fileNameTo success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  817. {
  818. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  819. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  820. [communication setUserAgent:[CCUtility getUserAgent]];
  821. [communication moveFileOrFolder:fileName toDestiny:fileNameTo onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  822. success();
  823. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  824. NSInteger errorCode = response.statusCode;
  825. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  826. errorCode = error.code;
  827. NSString *message = [CCError manageErrorOC:response.statusCode error:error];
  828. failure(message, error.code);
  829. } errorBeforeRequest:^(NSError *error) {
  830. NSString *message;
  831. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  832. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  833. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  834. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  835. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  836. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  837. } else {
  838. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  839. }
  840. failure(message, error.code);
  841. }];
  842. }
  843. #pragma --------------------------------------------------------------------------------------------
  844. #pragma mark ===== ReadFile =====
  845. #pragma --------------------------------------------------------------------------------------------
  846. - (void)readFile
  847. {
  848. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  849. NSString *fileName;
  850. if (_metadataNet.fileName) {
  851. fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  852. } else {
  853. _metadataNet.fileName = @".";
  854. fileName = _metadataNet.serverUrl;
  855. }
  856. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  857. [communication setUserAgent:[CCUtility getUserAgent]];
  858. [communication readFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  859. // Test active account
  860. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  861. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  862. if ([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  863. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  864. [self complete];
  865. return;
  866. }
  867. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:_metadataNet.serverUrl account:_metadataNet.account];
  868. if ([recordAccount.account isEqualToString:_metadataNet.account] && [items count] > 0) {
  869. tableMetadata *metadata = [tableMetadata new];
  870. OCFileDto *itemDto = [items objectAtIndex:0];
  871. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_metadataNet.serverUrl];
  872. if (directoryID) {
  873. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  874. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  875. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  876. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileName:_metadataNet.fileName serverUrl:_metadataNet.serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  877. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  878. [self.delegate readFileSuccessFailure:_metadataNet metadata:metadata message:nil errorCode:0];
  879. } else {
  880. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  881. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:@"Directory not found" errorCode:k_CCErrorInternalError];
  882. }
  883. }
  884. // BUG 1038
  885. if ([items count] == 0) {
  886. #ifndef EXTENSION
  887. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  888. [appDelegate messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  889. #endif
  890. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  891. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:@"Read File WebDAV : [items NULL] please fix" errorCode:k_CCErrorInternalError];
  892. }
  893. [self complete];
  894. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  895. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  896. _metadataNet.errorRetry++;
  897. NSInteger errorCode = response.statusCode;
  898. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  899. errorCode = error.code;
  900. // Error
  901. if ([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)] && [recordAccount.account isEqualToString:_metadataNet.account]) {
  902. if (errorCode == 503)
  903. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  904. else
  905. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  906. }
  907. // Request trusted certificated
  908. if ([error code] == NSURLErrorServerCertificateUntrusted)
  909. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  910. [self complete];
  911. }];
  912. }
  913. #pragma --------------------------------------------------------------------------------------------
  914. #pragma mark ===== Shared =====
  915. #pragma --------------------------------------------------------------------------------------------
  916. - (void)readShareServer
  917. {
  918. #ifndef EXTENSION
  919. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  920. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  921. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  922. [communication setUserAgent:[CCUtility getUserAgent]];
  923. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  924. // Test active account
  925. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  926. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  927. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  928. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  929. [self complete];
  930. return;
  931. }
  932. BOOL openWindow = NO;
  933. [appDelegate.sharesID removeAllObjects];
  934. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  935. for (OCSharedDto *item in items)
  936. [appDelegate.sharesID setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  937. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  938. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  939. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  940. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  941. }
  942. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  943. [self.delegate readSharedSuccess:_metadataNet items:appDelegate.sharesID openWindow:openWindow];
  944. [self complete];
  945. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  946. NSInteger errorCode = response.statusCode;
  947. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  948. errorCode = error.code;
  949. // Error
  950. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  951. if (errorCode == 503)
  952. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  953. else
  954. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  955. }
  956. // Request trusted certificated
  957. if ([error code] == NSURLErrorServerCertificateUntrusted)
  958. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  959. [self complete];
  960. }];
  961. #endif
  962. }
  963. - (void)share
  964. {
  965. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  966. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  967. [communication setUserAgent:[CCUtility getUserAgent]];
  968. [communication shareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPassword:[_metadataNet.password encodeString:NSUTF8StringEncoding] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  969. [self readShareServer];
  970. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  971. NSInteger errorCode = response.statusCode;
  972. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  973. errorCode = error.code;
  974. // Error
  975. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  976. if (errorCode == 503)
  977. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  978. else
  979. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  980. }
  981. // Request trusted certificated
  982. if ([error code] == NSURLErrorServerCertificateUntrusted)
  983. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  984. [self complete];
  985. }];
  986. }
  987. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  988. - (void)shareWith
  989. {
  990. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  991. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  992. [communication setUserAgent:[CCUtility getUserAgent]];
  993. [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) {
  994. [self readShareServer];
  995. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  996. NSInteger errorCode = response.statusCode;
  997. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  998. errorCode = error.code;
  999. // Error
  1000. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  1001. if (errorCode == 503)
  1002. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1003. else
  1004. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1005. }
  1006. // Request trusted certificated
  1007. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1008. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1009. [self complete];
  1010. }];
  1011. }
  1012. - (void)updateShare
  1013. {
  1014. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1015. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1016. [communication setUserAgent:[CCUtility getUserAgent]];
  1017. [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) {
  1018. [self readShareServer];
  1019. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1020. #ifndef EXTENSION
  1021. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1022. [appDelegate messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  1023. #endif
  1024. NSInteger errorCode = response.statusCode;
  1025. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1026. errorCode = error.code;
  1027. // Error
  1028. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  1029. if (errorCode == 503)
  1030. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1031. else
  1032. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1033. }
  1034. // Request trusted certificated
  1035. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1036. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1037. [self complete];
  1038. }];
  1039. }
  1040. - (void)unShare
  1041. {
  1042. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1043. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1044. [communication setUserAgent:[CCUtility getUserAgent]];
  1045. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1046. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  1047. [self.delegate unShareSuccess:_metadataNet];
  1048. [self complete];
  1049. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1050. #ifndef EXTENSION
  1051. [(AppDelegate *)[[UIApplication sharedApplication] delegate] messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  1052. #endif
  1053. NSInteger errorCode = response.statusCode;
  1054. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1055. errorCode = error.code;
  1056. // Error
  1057. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  1058. if (errorCode == 503)
  1059. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1060. else
  1061. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1062. }
  1063. // Request trusted certificated
  1064. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1065. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1066. [self complete];
  1067. }];
  1068. }
  1069. - (void)getUserAndGroup
  1070. {
  1071. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1072. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1073. [communication setUserAgent:[CCUtility getUserAgent]];
  1074. [communication searchUsersAndGroupsWith:_metadataNet.options forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  1075. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  1076. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  1077. [self complete];
  1078. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1079. NSInteger errorCode = response.statusCode;
  1080. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1081. errorCode = error.code;
  1082. // Error
  1083. if ([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)]) {
  1084. if (errorCode == 503)
  1085. [self.delegate getUserAndGroupFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1086. else
  1087. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1088. }
  1089. // Request trusted certificated
  1090. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1091. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1092. [self complete];
  1093. }];
  1094. }
  1095. - (void)getSharePermissionsFile
  1096. {
  1097. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1098. NSString *fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  1099. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1100. [communication setUserAgent:[CCUtility getUserAgent]];
  1101. [communication getSharePermissionsFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *permissions, NSString *redirectedServer) {
  1102. // Test active account
  1103. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1104. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1105. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)])
  1106. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1107. [self complete];
  1108. return;
  1109. }
  1110. if([self.delegate respondsToSelector:@selector(getSharePermissionsFileSuccess:permissions:)])
  1111. [self.delegate getSharePermissionsFileSuccess:_metadataNet permissions:permissions];
  1112. [self complete];
  1113. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1114. NSInteger errorCode = response.statusCode;
  1115. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1116. errorCode = error.code;
  1117. // Error
  1118. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)]) {
  1119. if (errorCode == 503)
  1120. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1121. else
  1122. [self.delegate getSharePermissionsFileFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1123. }
  1124. // Request trusted certificated
  1125. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1126. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1127. [self complete];
  1128. }];
  1129. }
  1130. #pragma --------------------------------------------------------------------------------------------
  1131. #pragma mark ===== Activity =====
  1132. #pragma --------------------------------------------------------------------------------------------
  1133. - (void)getActivityServer
  1134. {
  1135. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1136. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1137. [communication setUserAgent:[CCUtility getUserAgent]];
  1138. [communication getActivityServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  1139. // Test active account
  1140. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1141. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1142. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)])
  1143. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1144. [self complete];
  1145. return;
  1146. }
  1147. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)])
  1148. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:listOfActivity message:nil errorCode:0];
  1149. [self complete];
  1150. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1151. NSInteger errorCode = response.statusCode;
  1152. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1153. errorCode = error.code;
  1154. // Error
  1155. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)]) {
  1156. if (errorCode == 503)
  1157. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1158. else
  1159. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1160. }
  1161. // Request trusted certificated
  1162. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1163. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1164. [self complete];
  1165. }];
  1166. }
  1167. #pragma --------------------------------------------------------------------------------------------
  1168. #pragma mark ===== External Sites =====
  1169. #pragma --------------------------------------------------------------------------------------------
  1170. - (void)getExternalSitesServer
  1171. {
  1172. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1173. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1174. [communication setUserAgent:[CCUtility getUserAgent]];
  1175. [communication getExternalSitesServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1176. // Test active account
  1177. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1178. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1179. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)])
  1180. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1181. [self complete];
  1182. return;
  1183. }
  1184. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)])
  1185. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:listOfExternalSites message:nil errorCode:0];
  1186. [self complete];
  1187. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1188. NSInteger errorCode = response.statusCode;
  1189. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1190. errorCode = error.code;
  1191. // Error
  1192. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)]) {
  1193. if (errorCode == 503)
  1194. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1195. else
  1196. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1197. }
  1198. // Request trusted certificated
  1199. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1200. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1201. [self complete];
  1202. }];
  1203. }
  1204. #pragma --------------------------------------------------------------------------------------------
  1205. #pragma mark ===== Middleware Ping =====
  1206. #pragma --------------------------------------------------------------------------------------------
  1207. /*
  1208. - (void)middlewarePing
  1209. {
  1210. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1211. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1212. [communication setUserAgent:[CCUtility getUserAgent]];
  1213. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1214. [self complete];
  1215. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1216. NSInteger errorCode = response.statusCode;
  1217. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1218. errorCode = error.code;
  1219. // Error
  1220. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  1221. if (errorCode == 503)
  1222. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1223. else
  1224. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1225. }
  1226. // Request trusted certificated
  1227. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1228. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1229. [self complete];
  1230. }];
  1231. }
  1232. */
  1233. #pragma --------------------------------------------------------------------------------------------
  1234. #pragma mark ===== Notification =====
  1235. #pragma --------------------------------------------------------------------------------------------
  1236. - (void)getNotificationServer
  1237. {
  1238. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1239. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1240. [communication setUserAgent:[CCUtility getUserAgent]];
  1241. [communication getNotificationServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  1242. // Test active account
  1243. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1244. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1245. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
  1246. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1247. [self complete];
  1248. return;
  1249. }
  1250. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
  1251. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:listOfNotifications message:nil errorCode:0];
  1252. [self complete];
  1253. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1254. NSInteger errorCode = response.statusCode;
  1255. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1256. errorCode = error.code;
  1257. // Error
  1258. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)]) {
  1259. if (errorCode == 503)
  1260. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1261. else
  1262. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1263. }
  1264. // Request trusted certificated
  1265. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1266. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1267. // Activity
  1268. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1269. [self complete];
  1270. }];
  1271. }
  1272. - (void)setNotificationServer
  1273. {
  1274. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1275. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1276. [communication setUserAgent:[CCUtility getUserAgent]];
  1277. NSString *type = _metadataNet.options;
  1278. [communication setNotificationServer:_metadataNet.serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1279. // Test active account
  1280. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1281. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1282. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
  1283. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1284. [self complete];
  1285. return;
  1286. }
  1287. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
  1288. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:nil errorCode:0];
  1289. [self complete];
  1290. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1291. NSInteger errorCode = response.statusCode;
  1292. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1293. errorCode = error.code;
  1294. // Error
  1295. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)]) {
  1296. if (errorCode == 503)
  1297. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1298. else
  1299. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1300. }
  1301. // Request trusted certificated
  1302. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1303. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1304. [self complete];
  1305. }];
  1306. }
  1307. - (void)subscribingNextcloudServer
  1308. {
  1309. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1310. communication.kindOfCredential = credentialNotSet;
  1311. [communication setUserAgent:[CCUtility getUserAgent]];
  1312. NSDictionary *parameter = _metadataNet.options;
  1313. NSString *pushToken = [parameter objectForKey:@"pushToken"];
  1314. NSString *pushTokenHash = [parameter objectForKey:@"pushTokenHash"];
  1315. NSString *devicePublicKey = [parameter objectForKey:@"devicePublicKey"];
  1316. // encode URL
  1317. devicePublicKey = [devicePublicKey stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1318. [communication subscribingNextcloudServerPush:_activeUrl pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: [NCBrandOptions sharedInstance].pushNotificationServer onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1319. // encode URL
  1320. deviceIdentifier = [deviceIdentifier stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1321. signature = [signature stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1322. publicKey = [publicKey stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1323. [communication subscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature userPublicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1324. // Activity
  1325. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:@"Service registered." type:k_activityTypeSuccess verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1326. [self complete];
  1327. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1328. NSInteger errorCode = response.statusCode;
  1329. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1330. errorCode = error.code;
  1331. // Error
  1332. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1333. if (errorCode == 503)
  1334. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1335. else
  1336. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1337. }
  1338. // Request trusted certificated
  1339. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1340. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1341. // Activity
  1342. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1343. [self complete];
  1344. }];
  1345. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1346. NSInteger errorCode = response.statusCode;
  1347. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1348. errorCode = error.code;
  1349. // Error
  1350. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1351. if (errorCode == 503)
  1352. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1353. else
  1354. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1355. }
  1356. // Request trusted certificated
  1357. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1358. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1359. // Activity
  1360. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1361. [self complete];
  1362. }];
  1363. }
  1364. #pragma --------------------------------------------------------------------------------------------
  1365. #pragma mark ===== User Profile =====
  1366. #pragma --------------------------------------------------------------------------------------------
  1367. - (void)getUserProfile
  1368. {
  1369. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1370. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1371. [communication setUserAgent:[CCUtility getUserAgent]];
  1372. [communication getUserProfileServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1373. // Test active account
  1374. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1375. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1376. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
  1377. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1378. [self complete];
  1379. return;
  1380. }
  1381. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
  1382. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:userProfile message:nil errorCode:0];
  1383. [self complete];
  1384. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1385. NSInteger errorCode = response.statusCode;
  1386. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1387. errorCode = error.code;
  1388. // Error
  1389. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)]) {
  1390. if (errorCode == 503)
  1391. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1392. else
  1393. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1394. }
  1395. // Request trusted certificated
  1396. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1397. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1398. [self complete];
  1399. }];
  1400. }
  1401. #pragma --------------------------------------------------------------------------------------------
  1402. #pragma mark ===== Capabilities =====
  1403. #pragma --------------------------------------------------------------------------------------------
  1404. - (void)getCapabilitiesOfServer
  1405. {
  1406. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1407. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1408. [communication setUserAgent:[CCUtility getUserAgent]];
  1409. [communication getCapabilitiesOfServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1410. // Test active account
  1411. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1412. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1413. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)])
  1414. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1415. [self complete];
  1416. return;
  1417. }
  1418. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)])
  1419. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:capabilities message:nil errorCode:0];
  1420. [self complete];
  1421. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1422. NSInteger errorCode = response.statusCode;
  1423. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1424. errorCode = error.code;
  1425. // Error
  1426. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)]) {
  1427. if (errorCode == 503)
  1428. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1429. else
  1430. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1431. }
  1432. // Request trusted certificated
  1433. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1434. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1435. // Activity
  1436. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionCapabilities selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1437. [self complete];
  1438. }];
  1439. }
  1440. #pragma --------------------------------------------------------------------------------------------
  1441. #pragma mark ===== End-to-End Encryption =====
  1442. #pragma --------------------------------------------------------------------------------------------
  1443. - (void)getEndToEndPublicKeys
  1444. {
  1445. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1446. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1447. [communication setUserAgent:[CCUtility getUserAgent]];
  1448. [communication getEndToEndPublicKeys:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1449. _metadataNet.key = publicKey;
  1450. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysSuccess:)])
  1451. [self.delegate getEndToEndPublicKeysSuccess:_metadataNet];
  1452. [self complete];
  1453. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1454. NSInteger errorCode = response.statusCode;
  1455. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1456. errorCode = error.code;
  1457. // Error
  1458. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysFailure:message:errorCode:)])
  1459. [self.delegate getEndToEndPublicKeysFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1460. // Request trusted certificated
  1461. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1462. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1463. [self complete];
  1464. }];
  1465. }
  1466. - (void)getEndToEndPrivateKeyCipher
  1467. {
  1468. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1469. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1470. [communication setUserAgent:[CCUtility getUserAgent]];
  1471. [communication getEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer) {
  1472. _metadataNet.key = privateKeyChiper;
  1473. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherSuccess:)])
  1474. [self.delegate getEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1475. [self complete];
  1476. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1477. NSInteger errorCode = response.statusCode;
  1478. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1479. errorCode = error.code;
  1480. // Error
  1481. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1482. [self.delegate getEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1483. // Request trusted certificated
  1484. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1485. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1486. [self complete];
  1487. }];
  1488. }
  1489. - (void)signEndToEndPublicKey
  1490. {
  1491. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1492. // URL Encode
  1493. NSString *publicKey = [CCUtility URLEncodeStringFromString:_metadataNet.key];
  1494. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1495. [communication setUserAgent:[CCUtility getUserAgent]];
  1496. [communication signEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1497. _metadataNet.key = publicKey;
  1498. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeySuccess:)])
  1499. [self.delegate signEndToEndPublicKeySuccess:_metadataNet];
  1500. [self complete];
  1501. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1502. NSInteger errorCode = response.statusCode;
  1503. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1504. errorCode = error.code;
  1505. // Error
  1506. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeyFailure:message:errorCode:)])
  1507. [self.delegate signEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1508. // Request trusted certificated
  1509. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1510. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1511. [self complete];
  1512. }];
  1513. }
  1514. - (void)storeEndToEndPrivateKeyCipher
  1515. {
  1516. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1517. // URL Encode
  1518. NSString *privateKeyChiper = [CCUtility URLEncodeStringFromString:_metadataNet.keyCipher];
  1519. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1520. [communication setUserAgent:[CCUtility getUserAgent]];
  1521. [communication storeEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] privateKeyChiper:privateKeyChiper onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer) {
  1522. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherSuccess:)])
  1523. [self.delegate storeEndToEndPrivateKeyCipherSuccess:_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(storeEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1531. [self.delegate storeEndToEndPrivateKeyCipherFailure:_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)deleteEndToEndPublicKey
  1539. {
  1540. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1541. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1542. [communication setUserAgent:[CCUtility getUserAgent]];
  1543. [communication deleteEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1544. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeySuccess:)])
  1545. [self.delegate deleteEndToEndPublicKeySuccess:_metadataNet];
  1546. [self complete];
  1547. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1548. NSInteger errorCode = response.statusCode;
  1549. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1550. errorCode = error.code;
  1551. // Error
  1552. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeyFailure:message:errorCode:)])
  1553. [self.delegate deleteEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1554. // Request trusted certificated
  1555. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1556. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1557. [self complete];
  1558. }];
  1559. }
  1560. - (void)deleteEndToEndPrivateKey
  1561. {
  1562. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1563. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1564. [communication setUserAgent:[CCUtility getUserAgent]];
  1565. [communication deleteEndToEndPrivateKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1566. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeySuccess:)])
  1567. [self.delegate deleteEndToEndPrivateKeySuccess:_metadataNet];
  1568. [self complete];
  1569. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1570. NSInteger errorCode = response.statusCode;
  1571. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1572. errorCode = error.code;
  1573. // Error
  1574. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeyFailure:message:errorCode:)])
  1575. [self.delegate deleteEndToEndPrivateKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1576. // Request trusted certificated
  1577. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1578. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1579. [self complete];
  1580. }];
  1581. }
  1582. - (void)getEndToEndServerPublicKey
  1583. {
  1584. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1585. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1586. [communication setUserAgent:[CCUtility getUserAgent]];
  1587. [communication getEndToEndServerPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1588. _metadataNet.key = publicKey;
  1589. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeySuccess:)])
  1590. [self.delegate getEndToEndServerPublicKeySuccess:_metadataNet];
  1591. [self complete];
  1592. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1593. NSInteger errorCode = response.statusCode;
  1594. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1595. errorCode = error.code;
  1596. // Error
  1597. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeyFailure:message:errorCode:)])
  1598. [self.delegate getEndToEndServerPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1599. // Request trusted certificated
  1600. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1601. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1602. [self complete];
  1603. }];
  1604. }
  1605. @end
  1606. #pragma --------------------------------------------------------------------------------------------
  1607. #pragma mark ===== OCURLSessionManager =====
  1608. #pragma --------------------------------------------------------------------------------------------
  1609. @implementation OCURLSessionManager
  1610. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1611. {
  1612. // The pinnning check
  1613. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  1614. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1615. } else {
  1616. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1617. }
  1618. }
  1619. @end