OCNetworking.m 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. //
  2. // OCnetworking.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2014 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 "AppDelegate.h"
  25. #import "CCGraphics.h"
  26. #import "CCCertificate.h"
  27. #import "NSString+Encode.h"
  28. #ifdef CC
  29. #import "CCSharedDBSession.h"
  30. #endif
  31. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  32. @interface OCnetworking ()
  33. {
  34. NSString *_activeUser;
  35. NSString *_activePassword;
  36. NSString *_activeUrl;
  37. NSString *_typeCloud;
  38. NSURLSessionDownloadTask *_downloadTask;
  39. NSURLSessionUploadTask *_uploadTask;
  40. BOOL _oneByOne;
  41. BOOL _activityIndicator;
  42. }
  43. @end
  44. @implementation OCnetworking
  45. - (id)initWithDelegate:(id <OCNetworkingDelegate>)delegate metadataNet:(CCMetadataNet *)metadataNet withUser:(NSString *)withUser withPassword:(NSString *)withPassword withUrl:(NSString *)withUrl withTypeCloud:(NSString *)withTypeCloud oneByOne:(BOOL)oneByOne activityIndicator:(BOOL)activityIndicator
  46. {
  47. self = [super init];
  48. if (self) {
  49. _delegate = delegate;
  50. _metadataNet = [[CCMetadataNet alloc] init];
  51. _metadataNet = [metadataNet copy];
  52. _activeUser = withUser;
  53. _activePassword = withPassword;
  54. _activeUrl = withUrl;
  55. _typeCloud = withTypeCloud;
  56. _oneByOne = oneByOne;
  57. _activityIndicator = activityIndicator;
  58. }
  59. return self;
  60. }
  61. - (void)start
  62. {
  63. if (![NSThread isMainThread]) {
  64. [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
  65. return;
  66. }
  67. [self willChangeValueForKey:@"isExecuting"];
  68. _isExecuting = YES;
  69. [self didChangeValueForKey:@"isExecuting"];
  70. if (self.isCancelled) {
  71. [self finish];
  72. } else {
  73. [self poolNetworking];
  74. }
  75. }
  76. - (void)finish
  77. {
  78. [self willChangeValueForKey:@"isExecuting"];
  79. [self willChangeValueForKey:@"isFinished"];
  80. _isExecuting = NO;
  81. _isFinished = YES;
  82. [self didChangeValueForKey:@"isExecuting"];
  83. [self didChangeValueForKey:@"isFinished"];
  84. }
  85. - (void)cancel
  86. {
  87. if (_isExecuting) {
  88. if (_downloadTask)
  89. [_downloadTask cancel];
  90. if (_uploadTask)
  91. [_uploadTask cancel];
  92. [self complete];
  93. }
  94. [super cancel];
  95. }
  96. - (void)poolNetworking
  97. {
  98. #ifndef SHARE_IN
  99. // Animation network
  100. if (_activityIndicator) {
  101. dispatch_async(dispatch_get_main_queue(), ^{
  102. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  103. [[NSNotificationCenter defaultCenter] postNotificationName:@"setTitleCCMainYESAnimation" object:nil];
  104. });
  105. }
  106. #endif
  107. if([self respondsToSelector:NSSelectorFromString(_metadataNet.action)])
  108. [self performSelector:NSSelectorFromString(_metadataNet.action)];
  109. }
  110. #pragma --------------------------------------------------------------------------------------------
  111. #pragma mark ===== Delegate =====
  112. #pragma --------------------------------------------------------------------------------------------
  113. - (void)complete
  114. {
  115. #ifndef SHARE_IN
  116. // Animation network
  117. if (_activityIndicator) {
  118. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  119. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  120. [[NSNotificationCenter defaultCenter] postNotificationName:@"setTitleCCMainNOAnimation" object:nil];
  121. });
  122. }
  123. #endif
  124. [self finish];
  125. }
  126. #pragma --------------------------------------------------------------------------------------------
  127. #pragma mark ===== downloadFile =====
  128. #pragma --------------------------------------------------------------------------------------------
  129. - (void)downloadFile
  130. {
  131. [[CCNetworking sharedNetworking] downloadFile:_metadataNet.metadata serverUrl:_metadataNet.serverUrl downloadData:_metadataNet.downloadData downloadPlist:_metadataNet.downloadPlist selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost session:_metadataNet.session taskStatus:_metadataNet.taskStatus delegate:self];
  132. if (!_oneByOne)
  133. [self complete];
  134. }
  135. - (void)downloadTaskSave:(NSURLSessionDownloadTask *)downloadTask
  136. {
  137. _downloadTask= downloadTask;
  138. if ([self.delegate respondsToSelector:@selector(downloadTaskSave:)])
  139. [self.delegate downloadTaskSave:downloadTask];
  140. }
  141. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  142. {
  143. [self complete];
  144. if ([self.delegate respondsToSelector:@selector(downloadFileSuccess:serverUrl:selector:selectorPost:)])
  145. [self.delegate downloadFileSuccess:fileID serverUrl:serverUrl selector:selector selectorPost:selectorPost];
  146. }
  147. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  148. {
  149. [self complete];
  150. if ([self.delegate respondsToSelector:@selector(downloadFileFailure:serverUrl:selector:message:errorCode:)])
  151. [self.delegate downloadFileFailure:fileID serverUrl:serverUrl selector:selector message:message errorCode:errorCode];
  152. }
  153. #pragma --------------------------------------------------------------------------------------------
  154. #pragma mark ===== uploadFile =====
  155. #pragma --------------------------------------------------------------------------------------------
  156. - (void)uploadFile
  157. {
  158. [[CCNetworking sharedNetworking] uploadFile:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:_metadataNet.cryptated onlyPlist:NO session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost parentRev:nil errorCode:_metadataNet.errorCode delegate:self];
  159. if (!_oneByOne)
  160. [self complete];
  161. }
  162. - (void)uploadOnlyPlist
  163. {
  164. [[CCNetworking sharedNetworking] uploadFile:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:YES onlyPlist:YES session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost parentRev:nil errorCode:_metadataNet.errorCode delegate:self];
  165. if (!_oneByOne)
  166. [self complete];
  167. }
  168. - (void)uploadAsset
  169. {
  170. [[CCNetworking sharedNetworking] uploadFileFromAssetLocalIdentifier:_metadataNet.assetLocalItentifier serverUrl:_metadataNet.serverUrl cryptated:_metadataNet.cryptated session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost parentRev:nil errorCode:_metadataNet.errorCode delegate:self];
  171. if (!_oneByOne)
  172. [self complete];
  173. }
  174. - (void)uploadTemplate
  175. {
  176. [[CCNetworking sharedNetworking] uploadTemplate:_metadataNet.fileNamePrint fileNameCrypto:_metadataNet.fileName serverUrl:_metadataNet.serverUrl session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost parentRev:nil errorCode:_metadataNet.errorCode delegate:self];
  177. if (!_oneByOne)
  178. [self complete];
  179. }
  180. - (void)uploadTaskSave:(NSURLSessionUploadTask *)uploadTask
  181. {
  182. _uploadTask = uploadTask;
  183. if ([self.delegate respondsToSelector:@selector(uploadTaskSave:)])
  184. [self.delegate uploadTaskSave:uploadTask];
  185. }
  186. - (void)uploadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  187. {
  188. [self complete];
  189. if ([self.delegate respondsToSelector:@selector(uploadFileSuccess:serverUrl:selector:selectorPost:)])
  190. [self.delegate uploadFileSuccess:fileID serverUrl:serverUrl selector:selector selectorPost:selectorPost];
  191. }
  192. - (void)uploadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  193. {
  194. [self complete];
  195. if ([self.delegate respondsToSelector:@selector(uploadFileFailure:serverUrl:selector:message:errorCode:)])
  196. [self.delegate uploadFileFailure:fileID serverUrl:serverUrl selector:selector message:message errorCode:errorCode];
  197. }
  198. #pragma --------------------------------------------------------------------------------------------
  199. #pragma mark ===== downloadThumbnail =====
  200. #pragma --------------------------------------------------------------------------------------------
  201. - (void)downloadThumbnail
  202. {
  203. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  204. __block NSString *ext;
  205. NSInteger width = 0, height = 0;
  206. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  207. if ([_metadataNet.options.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  208. else if ([_metadataNet.options.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  209. else if ([_metadataNet.options.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  210. else if ([_metadataNet.options.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  211. else if ([_metadataNet.options.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  212. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileNameLocal, ext]]) {
  213. [self.delegate downloadThumbnailSuccess:_metadataNet];
  214. [self complete];
  215. return;
  216. }
  217. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  218. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  219. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:_metadataNet.fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  220. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  221. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  222. UIImage *thumbnailImage = [UIImage imageWithData:thumbnail];
  223. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  224. [CCGraphics saveIcoWithFileID:_metadataNet.fileNameLocal image:thumbnailImage writeToFile:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileNameLocal, ext] copy:NO move:NO fromPath:nil toPath:nil];
  225. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccess:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  226. [self.delegate downloadThumbnailSuccess:_metadataNet];
  227. }
  228. [self complete];
  229. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  230. NSInteger errorCode = response.statusCode;
  231. if (errorCode == 0)
  232. errorCode = error.code;
  233. if ([self.delegate respondsToSelector:@selector(downloadThumbnailFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  234. [self.delegate downloadThumbnailFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  235. [self complete];
  236. }];
  237. }
  238. #pragma --------------------------------------------------------------------------------------------
  239. #pragma mark ===== Read Folder =====
  240. #pragma --------------------------------------------------------------------------------------------
  241. - (void)readFolder
  242. {
  243. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  244. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  245. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  246. [communication readFolder:_metadataNet.serverUrl withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  247. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  248. // Check items > 0
  249. if ([items count] == 0) {
  250. #ifndef SHARE_IN
  251. [app messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  252. #endif
  253. dispatch_async(dispatch_get_main_queue(), ^{
  254. if ([self.delegate respondsToSelector:@selector(readFolderSuccess:permissions:rev:metadatas:)])
  255. [self.delegate readFolderSuccess:_metadataNet permissions:@"" rev:@"" metadatas:metadatas];
  256. });
  257. [self complete];
  258. return;
  259. }
  260. // directory [0]
  261. OCFileDto *itemDtoDirectory = [items objectAtIndex:0];
  262. NSString *permissions = itemDtoDirectory.permissions;
  263. NSString *rev = itemDtoDirectory.etag;
  264. NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  265. NSString *directoryID = [CCCoreData addDirectory:_metadataNet.serverUrl date:date permissions:permissions activeAccount:_metadataNet.account];
  266. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  267. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl typeCloud:_typeCloud];
  268. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  269. // Update metadataNet.directoryID
  270. _metadataNet.directoryID = directoryID;
  271. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  272. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  273. NSString *first = [(OCFileDto*)a fileName];
  274. NSString *second = [(OCFileDto*)b fileName];
  275. return [[first lowercaseString] compare:[second lowercaseString]];
  276. }];
  277. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  278. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  279. itemDto.fileName = [CCUtility clearFile:itemDto.fileName];
  280. // ----- BUG #942 ---------
  281. if ([itemDto.etag length] == 0) {
  282. #ifndef SHARE_IN
  283. [app messageNotification:@"Server error" description:@"Metadata etag absent, record excluded, please fix" visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  284. #endif
  285. continue;
  286. }
  287. // ------------------------
  288. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:itemDto.fileName serverUrl:_metadataNet.serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser typeCloud:_typeCloud]];
  289. }
  290. dispatch_async(dispatch_get_main_queue(), ^{
  291. if ([self.delegate respondsToSelector:@selector(readFolderSuccess:permissions:rev:metadatas:)])
  292. [self.delegate readFolderSuccess:_metadataNet permissions:permissions rev:rev metadatas:metadatas];
  293. });
  294. });
  295. [self complete];
  296. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  297. NSInteger errorCode = response.statusCode;
  298. if (errorCode == 0)
  299. errorCode = error.code;
  300. if ([self.delegate respondsToSelector:@selector(readFolderFailure:message:errorCode:)])
  301. [self.delegate readFolderFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  302. // Request trusted certificated
  303. if ([error code] == NSURLErrorServerCertificateUntrusted)
  304. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  305. [self complete];
  306. }];
  307. }
  308. #pragma --------------------------------------------------------------------------------------------
  309. #pragma mark ===== Create Folder =====
  310. #pragma --------------------------------------------------------------------------------------------
  311. - (void)createFolder
  312. {
  313. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  314. BOOL hasServerForbiddenCharactersSupport;
  315. NSString *nameFolderURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  316. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  317. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  318. [communication createFolder:nameFolderURL onCommunication:communication withForbiddenCharactersSupported:hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  319. if ([self.delegate respondsToSelector:@selector(createFolderSuccess:)])
  320. [self.delegate createFolderSuccess:_metadataNet];
  321. [self complete];
  322. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  323. NSString *message;
  324. if (([_metadataNet.fileName isEqualToString:[CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account]] == YES && [_metadataNet.serverUrl isEqualToString:[CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl typeCloud:_typeCloud]] == YES))
  325. message = nil;
  326. else
  327. message = [CCError manageErrorOC:response.statusCode error:error];
  328. NSInteger errorCode = response.statusCode;
  329. if (errorCode == 0)
  330. errorCode = error.code;
  331. if ([self.delegate respondsToSelector:@selector(createFolderFailure:message:errorCode:)])
  332. [self.delegate createFolderFailure:_metadataNet message:message errorCode:errorCode];
  333. // Request trusted certificated
  334. if ([error code] == NSURLErrorServerCertificateUntrusted)
  335. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  336. [self complete];
  337. } errorBeforeRequest:^(NSError *error) {
  338. NSString *message;
  339. if (([_metadataNet.fileName isEqualToString:[CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account]] == YES && [_metadataNet.serverUrl isEqualToString:[CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl typeCloud:_typeCloud]] == YES))
  340. message = nil;
  341. else {
  342. if (error.code == OCErrorForbidenCharacters)
  343. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  344. else
  345. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  346. }
  347. if ([self.delegate respondsToSelector:@selector(createFolderFailure:message:errorCode:)])
  348. [self.delegate createFolderFailure:_metadataNet message:message errorCode:error.code];
  349. [self complete];
  350. }];
  351. }
  352. - (NSError *)createFolderSync:(NSString *)folderPathName
  353. {
  354. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  355. BOOL hasServerForbiddenCharactersSupport;
  356. __block NSError *returnError;
  357. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  358. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  359. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  360. [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  361. dispatch_semaphore_signal(semaphore);
  362. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  363. // Request trusted certificated
  364. if ([error code] == NSURLErrorServerCertificateUntrusted)
  365. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  366. returnError = error;
  367. dispatch_semaphore_signal(semaphore);
  368. } errorBeforeRequest:^(NSError *error) {
  369. returnError = error;
  370. dispatch_semaphore_signal(semaphore);
  371. }];
  372. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  373. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  374. return returnError;
  375. }
  376. #pragma --------------------------------------------------------------------------------------------
  377. #pragma mark ===== Delete =====
  378. #pragma --------------------------------------------------------------------------------------------
  379. - (void)deleteFileOrFolder
  380. {
  381. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  382. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  383. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  384. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  385. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  386. if ([_metadataNet.selector rangeOfString:selectorDelete].location != NSNotFound && [self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccess:)])
  387. [self.delegate deleteFileOrFolderSuccess:_metadataNet];
  388. [self complete];
  389. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  390. NSInteger errorCode = response.statusCode;
  391. if (errorCode == 0)
  392. errorCode = error.code;
  393. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderFailure:message:errorCode:)])
  394. [self.delegate deleteFileOrFolderFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  395. // Request trusted certificated
  396. if ([error code] == NSURLErrorServerCertificateUntrusted)
  397. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  398. [self complete];
  399. }];
  400. }
  401. #pragma --------------------------------------------------------------------------------------------
  402. #pragma mark ===== Move =====
  403. #pragma --------------------------------------------------------------------------------------------
  404. - (void)moveFileOrFolder
  405. {
  406. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  407. BOOL hasServerForbiddenCharactersSupport;
  408. NSString *origineURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  409. NSString *destinazioneURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrlTo, _metadataNet.fileNameTo];
  410. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  411. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  412. [communication moveFileOrFolder:origineURL toDestiny:destinazioneURL onCommunication:communication withForbiddenCharactersSupported:hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  413. if ([_metadataNet.selector isEqualToString:selectorRename] && [self.delegate respondsToSelector:@selector(renameSuccess:revTo:)])
  414. [self.delegate renameSuccess:_metadataNet revTo:nil];
  415. if ([_metadataNet.selector rangeOfString:selectorMove].location != NSNotFound && [self.delegate respondsToSelector:@selector(moveSuccess:revTo:)])
  416. [self.delegate moveSuccess:_metadataNet revTo:nil];
  417. [self complete];
  418. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  419. NSInteger errorCode = response.statusCode;
  420. if (errorCode == 0)
  421. errorCode = error.code;
  422. if ([self.delegate respondsToSelector:@selector(moveFileOrFolderFailure:message:errorCode:)])
  423. [self.delegate moveFileOrFolderFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  424. // Request trusted certificated
  425. if ([error code] == NSURLErrorServerCertificateUntrusted)
  426. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  427. [self complete];
  428. } errorBeforeRequest:^(NSError *error) {
  429. NSString *message;
  430. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  431. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  432. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  433. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  434. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  435. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  436. } else {
  437. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  438. }
  439. if ([self.delegate respondsToSelector:@selector(moveFileOrFolderFailure:message:errorCode:)])
  440. [self.delegate moveFileOrFolderFailure:_metadataNet message:message errorCode:error.code];
  441. [self complete];
  442. }];
  443. }
  444. #pragma --------------------------------------------------------------------------------------------
  445. #pragma mark ===== ReadFile =====
  446. #pragma --------------------------------------------------------------------------------------------
  447. - (void)readFile
  448. {
  449. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  450. NSString *fileName;
  451. if (_metadataNet.fileName)
  452. fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  453. else
  454. fileName = _metadataNet.serverUrl;
  455. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  456. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  457. [communication readFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  458. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  459. if ([recordAccount.account isEqualToString:_metadataNet.account] && [items count] > 0) {
  460. CCMetadata *metadata = [[CCMetadata alloc] init];
  461. OCFileDto *itemDto = [items objectAtIndex:0];
  462. itemDto.fileName = _metadataNet.fileName;
  463. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_metadataNet.serverUrl activeAccount:_metadataNet.account];
  464. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  465. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl typeCloud:_typeCloud];
  466. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  467. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:_metadataNet.fileNamePrint serverUrl:_metadataNet.serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser typeCloud:_typeCloud];
  468. if([self.delegate respondsToSelector:@selector(readFileSuccess:metadata:)])
  469. [self.delegate readFileSuccess:_metadataNet metadata:metadata];
  470. }
  471. // BUG 1038
  472. if ([items count] == 0) {
  473. #ifndef SHARE_IN
  474. [app messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  475. #endif
  476. }
  477. [self complete];
  478. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  479. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  480. _metadataNet.errorRetry++;
  481. NSInteger errorCode = response.statusCode;
  482. if (errorCode == 0)
  483. errorCode = error.code;
  484. if([self.delegate respondsToSelector:@selector(readFileFailure:message:errorCode:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  485. [self.delegate readFileFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  486. // Request trusted certificated
  487. if ([error code] == NSURLErrorServerCertificateUntrusted)
  488. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  489. [self complete];
  490. }];
  491. }
  492. - (NSError *)readFileSync:(NSString *)filePathName
  493. {
  494. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  495. __block NSError *returnError;
  496. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  497. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  498. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  499. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  500. returnError = nil;
  501. dispatch_semaphore_signal(semaphore);
  502. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  503. // Request trusted certificated
  504. if ([error code] == NSURLErrorServerCertificateUntrusted)
  505. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  506. returnError = error;
  507. dispatch_semaphore_signal(semaphore);
  508. }];
  509. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  510. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  511. return returnError;
  512. }
  513. #pragma --------------------------------------------------------------------------------------------
  514. #pragma mark ===== Shared =====
  515. #pragma --------------------------------------------------------------------------------------------
  516. - (NSMutableDictionary *)getShareID
  517. {
  518. #ifndef SHARE_IN
  519. return app.sharesID;
  520. #endif
  521. return [NSMutableDictionary new];
  522. }
  523. - (void)readShareServer
  524. {
  525. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  526. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  527. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  528. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  529. BOOL openWindow = NO;
  530. [[self getShareID] removeAllObjects];
  531. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  532. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  533. for (OCSharedDto *item in items)
  534. [[self getShareID] setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  535. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  536. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  537. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  538. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  539. }
  540. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  541. [self.delegate readSharedSuccess:_metadataNet items:[self getShareID] openWindow:openWindow];
  542. [self complete];
  543. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  544. NSInteger errorCode = response.statusCode;
  545. if (errorCode == 0)
  546. errorCode = error.code;
  547. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  548. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  549. // Request trusted certificated
  550. if ([error code] == NSURLErrorServerCertificateUntrusted)
  551. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  552. [self complete];
  553. }];
  554. }
  555. - (void)share
  556. {
  557. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  558. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  559. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  560. [communication shareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPassword:[_metadataNet.password encodeString:NSUTF8StringEncoding] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  561. [self readShareServer];
  562. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  563. NSInteger errorCode = response.statusCode;
  564. if (errorCode == 0)
  565. errorCode = error.code;
  566. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  567. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  568. // Request trusted certificated
  569. if ([error code] == NSURLErrorServerCertificateUntrusted)
  570. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  571. [self complete];
  572. }];
  573. }
  574. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  575. - (void)shareWith
  576. {
  577. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  578. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  579. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  580. [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) {
  581. [self readShareServer];
  582. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  583. NSInteger errorCode = response.statusCode;
  584. if (errorCode == 0)
  585. errorCode = error.code;
  586. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  587. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  588. // Request trusted certificated
  589. if ([error code] == NSURLErrorServerCertificateUntrusted)
  590. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  591. [self complete];
  592. }];
  593. }
  594. - (void)updateShare
  595. {
  596. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  597. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  598. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  599. [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) {
  600. [self readShareServer];
  601. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  602. #ifndef SHARE_IN
  603. [app messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  604. #endif
  605. NSInteger errorCode = response.statusCode;
  606. if (errorCode == 0)
  607. errorCode = error.code;
  608. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  609. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  610. // Request trusted certificated
  611. if ([error code] == NSURLErrorServerCertificateUntrusted)
  612. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  613. [self complete];
  614. }];
  615. }
  616. - (void)unShare
  617. {
  618. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  619. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  620. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  621. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  622. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  623. [self.delegate unShareSuccess:_metadataNet];
  624. [self complete];
  625. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  626. #ifndef SHARE_IN
  627. [app messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  628. #endif
  629. NSInteger errorCode = response.statusCode;
  630. if (errorCode == 0)
  631. errorCode = error.code;
  632. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  633. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  634. // Request trusted certificated
  635. if ([error code] == NSURLErrorServerCertificateUntrusted)
  636. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  637. [self complete];
  638. }];
  639. }
  640. - (void)getUserAndGroup
  641. {
  642. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  643. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  644. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  645. [communication searchUsersAndGroupsWith:_metadataNet.options forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  646. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  647. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  648. [self complete];
  649. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  650. NSInteger errorCode = response.statusCode;
  651. if (errorCode == 0)
  652. errorCode = error.code;
  653. if([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)])
  654. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  655. // Request trusted certificated
  656. if ([error code] == NSURLErrorServerCertificateUntrusted)
  657. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  658. [self complete];
  659. }];
  660. }
  661. #pragma --------------------------------------------------------------------------------------------
  662. #pragma mark ===== Server =====
  663. #pragma --------------------------------------------------------------------------------------------
  664. - (NSError *)checkServerSync:(NSString *)serverUrl
  665. {
  666. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  667. __block NSError *returnError;
  668. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  669. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  670. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  671. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  672. returnError = nil;
  673. dispatch_semaphore_signal(semaphore);
  674. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  675. // Request trusted certificated
  676. if ([error code] == NSURLErrorServerCertificateUntrusted)
  677. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  678. returnError = error;
  679. dispatch_semaphore_signal(semaphore);
  680. }];
  681. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  682. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  683. return returnError;
  684. }
  685. - (void)getFeaturesSupportedByServer
  686. {
  687. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  688. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  689. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  690. [communication getFeaturesSupportedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer) {
  691. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  692. if ([self.delegate respondsToSelector:@selector(getFeaturesSupportedByServerSuccess:hasForbiddenCharactersSupport:hasShareSupport:hasShareeSupport:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  693. [self.delegate getFeaturesSupportedByServerSuccess:hasCapabilitiesSupport hasForbiddenCharactersSupport:hasForbiddenCharactersSupport hasShareSupport:hasShareSupport hasShareeSupport:hasShareeSupport];
  694. [self complete];
  695. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  696. NSInteger errorCode = response.statusCode;
  697. if (errorCode == 0)
  698. errorCode = error.code;
  699. if([self.delegate respondsToSelector:@selector(getInfoServerFailure:message:errorCode:)])
  700. [self.delegate getInfoServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  701. // Request trusted certificated
  702. if ([error code] == NSURLErrorServerCertificateUntrusted)
  703. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  704. [self complete];
  705. }];
  706. }
  707. - (void)getCapabilitiesOfServer
  708. {
  709. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  710. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  711. [communication setUserAgent:[CCUtility getUserAgent:_typeCloud]];
  712. [communication getCapabilitiesOfServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  713. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  714. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccess:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  715. [self.delegate getCapabilitiesOfServerSuccess:capabilities];
  716. [self complete];
  717. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  718. NSInteger errorCode = response.statusCode;
  719. if (errorCode == 0)
  720. errorCode = error.code;
  721. if([self.delegate respondsToSelector:@selector(getInfoServerFailure:message:errorCode:)])
  722. [self.delegate getInfoServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  723. // Request trusted certificated
  724. if ([error code] == NSURLErrorServerCertificateUntrusted)
  725. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  726. [self complete];
  727. }];
  728. }
  729. @end
  730. #pragma --------------------------------------------------------------------------------------------
  731. #pragma mark ===== OCURLSessionManager =====
  732. #pragma --------------------------------------------------------------------------------------------
  733. @implementation OCURLSessionManager
  734. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  735. {
  736. // The pinnning check
  737. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  738. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  739. } else {
  740. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  741. }
  742. }
  743. @end