OCNetworking.m 100 KB

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