OCNetworking.m 114 KB

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