OCNetworking.m 90 KB

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