OCNetworking.m 89 KB

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