OCNetworking.m 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. //
  2. // OCnetworking.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "OCNetworking.h"
  24. #import "AppDelegate.h"
  25. #import "CCGraphics.h"
  26. #import "CCCertificate.h"
  27. #import "NSString+Encode.h"
  28. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  29. @interface OCnetworking ()
  30. {
  31. NSString *_activeUser;
  32. NSString *_activePassword;
  33. NSString *_activeUrl;
  34. NSURLSessionDownloadTask *_downloadTask;
  35. NSURLSessionUploadTask *_uploadTask;
  36. BOOL _isCryptoCloudMode;
  37. BOOL _hasServerForbiddenCharactersSupport;
  38. }
  39. @end
  40. @implementation OCnetworking
  41. - (id)initWithDelegate:(id <OCNetworkingDelegate>)delegate metadataNet:(CCMetadataNet *)metadataNet withUser:(NSString *)withUser withPassword:(NSString *)withPassword withUrl:(NSString *)withUrl isCryptoCloudMode:(BOOL)isCryptoCloudMode
  42. {
  43. self = [super init];
  44. if (self) {
  45. _delegate = delegate;
  46. _metadataNet = [CCMetadataNet new];
  47. _metadataNet = [metadataNet copy];
  48. _activeUser = withUser;
  49. _activePassword = withPassword;
  50. _activeUrl = withUrl;
  51. _isCryptoCloudMode = isCryptoCloudMode;
  52. }
  53. return self;
  54. }
  55. - (void)start
  56. {
  57. if (![NSThread isMainThread]) {
  58. [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
  59. return;
  60. }
  61. [self willChangeValueForKey:@"isExecuting"];
  62. _isExecuting = YES;
  63. [self didChangeValueForKey:@"isExecuting"];
  64. if (self.isCancelled) {
  65. [self finish];
  66. } else {
  67. [self poolNetworking];
  68. }
  69. }
  70. - (void)finish
  71. {
  72. [self willChangeValueForKey:@"isExecuting"];
  73. [self willChangeValueForKey:@"isFinished"];
  74. _isExecuting = NO;
  75. _isFinished = YES;
  76. [self didChangeValueForKey:@"isExecuting"];
  77. [self didChangeValueForKey:@"isFinished"];
  78. }
  79. - (void)cancel
  80. {
  81. if (_isExecuting) {
  82. if (_downloadTask)
  83. [_downloadTask cancel];
  84. if (_uploadTask)
  85. [_uploadTask cancel];
  86. [self complete];
  87. }
  88. [super cancel];
  89. }
  90. - (void)poolNetworking
  91. {
  92. #ifndef EXTENSION
  93. _hasServerForbiddenCharactersSupport = app.hasServerForbiddenCharactersSupport;
  94. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  95. #else
  96. _hasServerForbiddenCharactersSupport = YES;
  97. #endif
  98. if([self respondsToSelector:NSSelectorFromString(_metadataNet.action)])
  99. [self performSelector:NSSelectorFromString(_metadataNet.action)];
  100. }
  101. #pragma --------------------------------------------------------------------------------------------
  102. #pragma mark ===== Delegate =====
  103. #pragma --------------------------------------------------------------------------------------------
  104. - (void)complete
  105. {
  106. [self finish];
  107. #ifndef EXTENSION
  108. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  109. #endif
  110. }
  111. #pragma --------------------------------------------------------------------------------------------
  112. #pragma mark ===== downloadFile =====
  113. #pragma --------------------------------------------------------------------------------------------
  114. - (void)downloadFile
  115. {
  116. [[CCNetworking sharedNetworking] downloadFile:_metadataNet.metadata serverUrl:_metadataNet.serverUrl downloadData:_metadataNet.downloadData downloadPlist:_metadataNet.downloadPlist selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost session:_metadataNet.session taskStatus:_metadataNet.taskStatus delegate:self];
  117. }
  118. - (void)downloadTaskSave:(NSURLSessionDownloadTask *)downloadTask
  119. {
  120. _downloadTask= downloadTask;
  121. if ([self.delegate respondsToSelector:@selector(downloadTaskSave:)])
  122. [self.delegate downloadTaskSave:downloadTask];
  123. }
  124. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  125. {
  126. [self complete];
  127. if ([self.delegate respondsToSelector:@selector(downloadFileSuccess:serverUrl:selector:selectorPost:)])
  128. [self.delegate downloadFileSuccess:fileID serverUrl:serverUrl selector:selector selectorPost:selectorPost];
  129. }
  130. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  131. {
  132. [self complete];
  133. if ([self.delegate respondsToSelector:@selector(downloadFileFailure:serverUrl:selector:message:errorCode:)])
  134. [self.delegate downloadFileFailure:fileID serverUrl:serverUrl selector:selector message:message errorCode:errorCode];
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark ===== uploadFile =====
  138. #pragma --------------------------------------------------------------------------------------------
  139. - (void)uploadFile
  140. {
  141. [[CCNetworking sharedNetworking] uploadFile:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:_metadataNet.cryptated onlyPlist:NO session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost errorCode:_metadataNet.errorCode delegate:self];
  142. }
  143. - (void)uploadOnlyPlist
  144. {
  145. [[CCNetworking sharedNetworking] uploadFile:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:YES onlyPlist:YES session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost errorCode:_metadataNet.errorCode delegate:self];
  146. }
  147. - (void)uploadAsset
  148. {
  149. [[CCNetworking sharedNetworking] uploadFileFromAssetLocalIdentifier:_metadataNet.identifier fileName:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:_metadataNet.cryptated session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost errorCode:_metadataNet.errorCode delegate:self];
  150. }
  151. - (void)uploadTemplate
  152. {
  153. [[CCNetworking sharedNetworking] uploadTemplate:_metadataNet.fileNamePrint fileNameCrypto:_metadataNet.fileName serverUrl:_metadataNet.serverUrl session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost errorCode:_metadataNet.errorCode delegate:self];
  154. }
  155. - (void)uploadTaskSave:(NSURLSessionUploadTask *)uploadTask
  156. {
  157. _uploadTask = uploadTask;
  158. if ([self.delegate respondsToSelector:@selector(uploadTaskSave:)])
  159. [self.delegate uploadTaskSave:uploadTask];
  160. }
  161. - (void)uploadFileSuccess:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  162. {
  163. [self complete];
  164. if ([self.delegate respondsToSelector:@selector(uploadFileSuccess:fileID:serverUrl:selector:selectorPost:)])
  165. [self.delegate uploadFileSuccess:_metadataNet fileID:fileID serverUrl:serverUrl selector:selector selectorPost:selectorPost];
  166. }
  167. - (void)uploadFileFailure:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  168. {
  169. [self complete];
  170. if ([self.delegate respondsToSelector:@selector(uploadFileFailure:fileID:serverUrl:selector:message:errorCode:)])
  171. [self.delegate uploadFileFailure:_metadataNet fileID:fileID serverUrl:serverUrl selector:selector message:message errorCode:errorCode];
  172. }
  173. #pragma --------------------------------------------------------------------------------------------
  174. #pragma mark ===== downloadThumbnail =====
  175. #pragma --------------------------------------------------------------------------------------------
  176. - (void)downloadThumbnail
  177. {
  178. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  179. __block NSString *ext;
  180. NSInteger width = 0, height = 0;
  181. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  182. NSString *dimOfThumbnail = (NSString *)_metadataNet.options;
  183. if ([dimOfThumbnail.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  184. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  185. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  186. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  187. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  188. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileNameLocal, ext]]) {
  189. [self.delegate downloadThumbnailSuccess:_metadataNet];
  190. [self complete];
  191. return;
  192. }
  193. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  194. [communication setUserAgent:[CCUtility getUserAgent]];
  195. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:_metadataNet.fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  196. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  197. if ([recordAccount.account isEqualToString:_metadataNet.account] && [thumbnail length] > 0) {
  198. UIImage *thumbnailImage = [UIImage imageWithData:thumbnail];
  199. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  200. [CCGraphics saveIcoWithFileID:_metadataNet.fileNameLocal image:thumbnailImage writeToFile:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileNameLocal, ext] copy:NO move:NO fromPath:nil toPath:nil];
  201. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccess:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  202. [self.delegate downloadThumbnailSuccess:_metadataNet];
  203. } else {
  204. if ([self.delegate respondsToSelector:@selector(downloadThumbnailFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  205. [self.delegate downloadThumbnailFailure:_metadataNet message:@"No data" errorCode:0];
  206. }
  207. [self complete];
  208. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  209. NSInteger errorCode = response.statusCode;
  210. if (errorCode == 0)
  211. errorCode = error.code;
  212. // Error
  213. if ([self.delegate respondsToSelector:@selector(downloadThumbnailFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail]) {
  214. if (errorCode == 503)
  215. [self.delegate downloadThumbnailFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  216. else
  217. [self.delegate downloadThumbnailFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  218. }
  219. [self complete];
  220. }];
  221. }
  222. #pragma --------------------------------------------------------------------------------------------
  223. #pragma mark ===== Read Folder =====
  224. #pragma --------------------------------------------------------------------------------------------
  225. - (void)readFolder
  226. {
  227. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  228. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  229. [communication setUserAgent:[CCUtility getUserAgent]];
  230. [communication readFolder:_metadataNet.serverUrl withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  231. NSMutableArray *metadatas = [NSMutableArray new];
  232. // Check items > 0
  233. if ([items count] == 0) {
  234. #ifndef EXTENSION
  235. [app messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  236. #endif
  237. dispatch_async(dispatch_get_main_queue(), ^{
  238. if ([self.delegate respondsToSelector:@selector(readFolderSuccess:permissions:etag:metadatas:)])
  239. [self.delegate readFolderSuccess:_metadataNet permissions:@"" etag:@"" metadatas:metadatas];
  240. });
  241. [self complete];
  242. return;
  243. }
  244. // directory [0]
  245. OCFileDto *itemDtoDirectory = [items objectAtIndex:0];
  246. NSString *permissions = itemDtoDirectory.permissions;
  247. NSString *etagDirectory = itemDtoDirectory.etag;
  248. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  249. NSString *directoryID = [CCCoreData addDirectory:_metadataNet.serverUrl permissions:permissions activeAccount:_metadataNet.account];
  250. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  251. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl];
  252. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  253. // Update metadataNet.directoryID
  254. _metadataNet.directoryID = directoryID;
  255. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  256. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  257. NSString *first = [(OCFileDto*)a fileName];
  258. NSString *second = [(OCFileDto*)b fileName];
  259. return [[first lowercaseString] compare:[second lowercaseString]];
  260. }];
  261. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  262. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  263. itemDto.fileName = [itemDto.fileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  264. // Not in Crypto Cloud Mode skip File Crypto
  265. if (_isCryptoCloudMode == NO) {
  266. NSString *fileName = itemDto.fileName;
  267. if (itemDto.isDirectory) {
  268. fileName = [fileName substringToIndex:[fileName length] - 1];
  269. NSString *serverUrl = [CCUtility stringAppendServerUrl:_metadataNet.serverUrl addFileName:fileName];
  270. [CCCoreData addDirectory:serverUrl permissions:nil activeAccount:_metadataNet.account];
  271. }
  272. if ([CCUtility isFileCryptated:fileName])
  273. continue;
  274. }
  275. // ----- BUG #942 ---------
  276. if ([itemDto.etag length] == 0) {
  277. #ifndef EXTENSION
  278. [app messageNotification:@"Server error" description:@"Metadata etag absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  279. #endif
  280. continue;
  281. }
  282. // ------------------------
  283. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:itemDto.fileName serverUrl:_metadataNet.serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser]];
  284. }
  285. dispatch_async(dispatch_get_main_queue(), ^{
  286. if ([self.delegate respondsToSelector:@selector(readFolderSuccess:permissions:etag:metadatas:)])
  287. [self.delegate readFolderSuccess:_metadataNet permissions:permissions etag:etagDirectory metadatas:metadatas];
  288. });
  289. });
  290. [self complete];
  291. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  292. NSInteger errorCode = response.statusCode;
  293. if (errorCode == 0)
  294. errorCode = error.code;
  295. // Error
  296. if ([self.delegate respondsToSelector:@selector(readFolderFailure:message:errorCode:)]) {
  297. if (errorCode == 503)
  298. [self.delegate readFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  299. else
  300. [self.delegate readFolderFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  301. }
  302. // Request trusted certificated
  303. if ([error code] == NSURLErrorServerCertificateUntrusted)
  304. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  305. // Activity
  306. [CCCoreData addActivityClient:_metadataNet.serverUrl fileID:@"" action:k_activityDebugActionReadFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  307. [self complete];
  308. }];
  309. }
  310. #pragma --------------------------------------------------------------------------------------------
  311. #pragma mark ===== Search =====
  312. #pragma --------------------------------------------------------------------------------------------
  313. - (void)search
  314. {
  315. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  316. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  317. [communication setUserAgent:[CCUtility getUserAgent]];
  318. NSString *path = [_activeUrl stringByAppendingString:dav];
  319. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  320. [communication search:path folder:folder fileName:_metadataNet.fileName depth:_metadataNet.options withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  321. NSMutableArray *metadatas = [NSMutableArray new];
  322. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  323. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl];
  324. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  325. for(OCFileDto *itemDto in items) {
  326. itemDto.fileName = [itemDto.fileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  327. // Not in Crypto Cloud file
  328. NSString *fileName = itemDto.fileName;
  329. if (itemDto.isDirectory)
  330. fileName = [fileName substringToIndex:[fileName length] - 1];
  331. if ([CCUtility isFileCryptated:fileName])
  332. continue;
  333. // ----- BUG #942 ---------
  334. if ([itemDto.etag length] == 0) {
  335. #ifndef EXTENSION
  336. [app messageNotification:@"Server error" description:@"Metadata etag absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  337. #endif
  338. continue;
  339. }
  340. // ------------------------
  341. NSString *serverUrl = [NSString stringWithFormat:@"%@/files/%@", dav, _activeUser];
  342. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:serverUrl withString:@""];
  343. /* TRIM */
  344. if ([serverUrl hasPrefix:@"/"])
  345. serverUrl = [serverUrl substringFromIndex:1];
  346. if ([serverUrl hasSuffix:@"/"])
  347. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  348. /* */
  349. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  350. NSString *directoryID = [CCCoreData addDirectory:serverUrl permissions:itemDto.permissions activeAccount:_metadataNet.account];
  351. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:itemDto.fileName serverUrl:serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser]];
  352. }
  353. if ([self.delegate respondsToSelector:@selector(searchSuccess:metadatas:)])
  354. [self.delegate searchSuccess:_metadataNet metadatas:metadatas];
  355. [self complete];
  356. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  357. NSInteger errorCode = response.statusCode;
  358. if (errorCode == 0)
  359. errorCode = error.code;
  360. // Error
  361. if ([self.delegate respondsToSelector:@selector(searchFailure:message:errorCode:)]) {
  362. if (errorCode == 503)
  363. [self.delegate searchFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  364. else
  365. [self.delegate searchFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  366. }
  367. // Request trusted certificated
  368. if ([error code] == NSURLErrorServerCertificateUntrusted)
  369. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  370. [self complete];
  371. }];
  372. }
  373. #pragma --------------------------------------------------------------------------------------------
  374. #pragma mark ===== Setting Favorite =====
  375. #pragma --------------------------------------------------------------------------------------------
  376. - (void)settingFavorite
  377. {
  378. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  379. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  380. [communication setUserAgent:[CCUtility getUserAgent]];
  381. NSString *path = [_activeUrl stringByAppendingString:dav];
  382. [communication settingFavoriteServer:path andFileOrFolderPath:_metadataNet.fileName favorite:[_metadataNet.options boolValue] withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token) {
  383. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccess:)])
  384. [self.delegate settingFavoriteSuccess:_metadataNet];
  385. [self complete];
  386. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  387. NSInteger errorCode = response.statusCode;
  388. if (errorCode == 0)
  389. errorCode = error.code;
  390. // Error
  391. if ([self.delegate respondsToSelector:@selector(settingFavoriteFailure:message:errorCode:)]) {
  392. if (errorCode == 503)
  393. [self.delegate settingFavoriteFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  394. else
  395. [self.delegate settingFavoriteFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  396. }
  397. // Request trusted certificated
  398. if ([error code] == NSURLErrorServerCertificateUntrusted)
  399. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  400. [self complete];
  401. }];
  402. }
  403. #pragma --------------------------------------------------------------------------------------------
  404. #pragma mark ===== Listing Favorites =====
  405. #pragma --------------------------------------------------------------------------------------------
  406. - (void)listingFavorites
  407. {
  408. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  409. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  410. [communication setUserAgent:[CCUtility getUserAgent]];
  411. NSString *path = [_activeUrl stringByAppendingString:dav];
  412. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  413. [communication listingFavorites:path folder:folder withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  414. NSMutableArray *metadatas = [NSMutableArray new];
  415. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  416. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl];
  417. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  418. // Order by fileNamePath
  419. items = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  420. OCFileDto *record1 = obj1, *record2 = obj2;
  421. NSString *path1 = [[record1.filePath stringByAppendingString:record1.fileName] lowercaseString];
  422. NSString *path2 = [[record2.filePath stringByAppendingString:record2.fileName] lowercaseString];
  423. return [path1 compare:path2];
  424. }];
  425. for(OCFileDto *itemDto in items) {
  426. itemDto.fileName = [itemDto.fileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  427. itemDto.filePath = [itemDto.filePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  428. // Not in Crypto Cloud file
  429. NSString *fileName = itemDto.fileName;
  430. if (itemDto.isDirectory)
  431. fileName = [fileName substringToIndex:[fileName length] - 1];
  432. if ([CCUtility isFileCryptated:fileName])
  433. continue;
  434. // ----- BUG #942 ---------
  435. if ([itemDto.etag length] == 0) {
  436. #ifndef EXTENSION
  437. [app messageNotification:@"Server error" description:@"Metadata etag absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  438. #endif
  439. continue;
  440. }
  441. // ------------------------
  442. NSString *serverUrl = [NSString stringWithFormat:@"%@/files/%@", dav, _activeUser];
  443. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:serverUrl withString:@""];
  444. /* TRIM */
  445. if ([serverUrl hasPrefix:@"/"])
  446. serverUrl = [serverUrl substringFromIndex:1];
  447. if ([serverUrl hasSuffix:@"/"])
  448. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  449. /* */
  450. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  451. NSString *directoryID = [CCCoreData addDirectory:serverUrl permissions:itemDto.permissions activeAccount:_metadataNet.account];
  452. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:itemDto.fileName serverUrl:serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser]];
  453. }
  454. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccess:metadatas:)])
  455. [self.delegate listingFavoritesSuccess:_metadataNet metadatas:metadatas];
  456. [self complete];
  457. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  458. NSInteger errorCode = response.statusCode;
  459. if (errorCode == 0)
  460. errorCode = error.code;
  461. // Error
  462. if ([self.delegate respondsToSelector:@selector(listingFavoritesFailure:message:errorCode:)]) {
  463. if (errorCode == 503)
  464. [self.delegate listingFavoritesFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  465. else
  466. [self.delegate listingFavoritesFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  467. }
  468. // Request trusted certificated
  469. if ([error code] == NSURLErrorServerCertificateUntrusted)
  470. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  471. [self complete];
  472. }];
  473. }
  474. #pragma --------------------------------------------------------------------------------------------
  475. #pragma mark ===== Create Folder =====
  476. #pragma --------------------------------------------------------------------------------------------
  477. - (void)createFolder
  478. {
  479. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  480. NSString *nameFolderURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  481. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  482. [communication setUserAgent:[CCUtility getUserAgent]];
  483. [communication createFolder:nameFolderURL onCommunication:communication withForbiddenCharactersSupported:_hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  484. if ([self.delegate respondsToSelector:@selector(createFolderSuccess:)])
  485. [self.delegate createFolderSuccess:_metadataNet];
  486. [self complete];
  487. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  488. NSString *message;
  489. if (([_metadataNet.fileName isEqualToString:[CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account]] == YES && [_metadataNet.serverUrl isEqualToString:[CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl]] == YES))
  490. message = nil;
  491. else
  492. message = [CCError manageErrorOC:response.statusCode error:error];
  493. NSInteger errorCode = response.statusCode;
  494. if (errorCode == 0)
  495. errorCode = error.code;
  496. if ([self.delegate respondsToSelector:@selector(createFolderFailure:message:errorCode:)])
  497. [self.delegate createFolderFailure:_metadataNet message:message errorCode:errorCode];
  498. // Request trusted certificated
  499. if ([error code] == NSURLErrorServerCertificateUntrusted)
  500. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  501. [self complete];
  502. } errorBeforeRequest:^(NSError *error) {
  503. NSString *message;
  504. if (([_metadataNet.fileName isEqualToString:[CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account]] == YES && [_metadataNet.serverUrl isEqualToString:[CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl]] == YES))
  505. message = nil;
  506. else {
  507. if (error.code == OCErrorForbidenCharacters)
  508. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  509. else
  510. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  511. }
  512. // Error
  513. if ([self.delegate respondsToSelector:@selector(createFolderFailure:message:errorCode:)]) {
  514. if (error.code == 503)
  515. [self.delegate createFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  516. else
  517. [self.delegate createFolderFailure:_metadataNet message:message errorCode:error.code];
  518. }
  519. [self complete];
  520. }];
  521. }
  522. - (BOOL)automaticCreateFolderSync:(NSString *)folderPathName
  523. {
  524. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  525. __block BOOL noError = YES;
  526. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  527. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  528. [communication setUserAgent:[CCUtility getUserAgent]];
  529. [communication readFile:folderPathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  530. dispatch_semaphore_signal(semaphore);
  531. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  532. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  533. [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  534. [CCCoreData clearDateReadAccount:recordAccount.account serverUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] directoryID:nil];
  535. dispatch_semaphore_signal(semaphore);
  536. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  537. noError = NO;
  538. dispatch_semaphore_signal(semaphore);
  539. } errorBeforeRequest:^(NSError *error) {
  540. noError = NO;
  541. dispatch_semaphore_signal(semaphore);
  542. }];
  543. }];
  544. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  545. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  546. return noError;
  547. }
  548. #pragma --------------------------------------------------------------------------------------------
  549. #pragma mark ===== Delete =====
  550. #pragma --------------------------------------------------------------------------------------------
  551. - (void)deleteFileOrFolder
  552. {
  553. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  554. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  555. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  556. [communication setUserAgent:[CCUtility getUserAgent]];
  557. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  558. if ([_metadataNet.selector rangeOfString:selectorDelete].location != NSNotFound && [self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccess:)])
  559. [self.delegate deleteFileOrFolderSuccess:_metadataNet];
  560. [self complete];
  561. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  562. NSInteger errorCode = response.statusCode;
  563. if (errorCode == 0)
  564. errorCode = error.code;
  565. // Error
  566. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderFailure:message:errorCode:)]) {
  567. if (errorCode == 503)
  568. [self.delegate deleteFileOrFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  569. else
  570. [self.delegate deleteFileOrFolderFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  571. }
  572. // Request trusted certificated
  573. if ([error code] == NSURLErrorServerCertificateUntrusted)
  574. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  575. [self complete];
  576. }];
  577. }
  578. #pragma --------------------------------------------------------------------------------------------
  579. #pragma mark ===== Move =====
  580. #pragma --------------------------------------------------------------------------------------------
  581. - (void)moveFileOrFolder
  582. {
  583. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  584. NSString *origineURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  585. NSString *destinazioneURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrlTo, _metadataNet.fileNameTo];
  586. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  587. [communication setUserAgent:[CCUtility getUserAgent]];
  588. [communication moveFileOrFolder:origineURL toDestiny:destinazioneURL onCommunication:communication withForbiddenCharactersSupported:_hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  589. if ([_metadataNet.selector isEqualToString:selectorRename] && [self.delegate respondsToSelector:@selector(renameSuccess:)])
  590. [self.delegate renameSuccess:_metadataNet];
  591. if ([_metadataNet.selector rangeOfString:selectorMove].location != NSNotFound && [self.delegate respondsToSelector:@selector(moveSuccess:revTo:)])
  592. [self.delegate moveSuccess:_metadataNet revTo:nil];
  593. [self complete];
  594. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  595. NSInteger errorCode = response.statusCode;
  596. if (errorCode == 0)
  597. errorCode = error.code;
  598. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)])
  599. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  600. // Request trusted certificated
  601. if ([error code] == NSURLErrorServerCertificateUntrusted)
  602. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  603. [self complete];
  604. } errorBeforeRequest:^(NSError *error) {
  605. NSString *message;
  606. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  607. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  608. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  609. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  610. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  611. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  612. } else {
  613. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  614. }
  615. // Error
  616. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)]) {
  617. if (error.code == 503)
  618. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  619. else
  620. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:message errorCode:error.code];
  621. }
  622. [self complete];
  623. }];
  624. }
  625. #pragma --------------------------------------------------------------------------------------------
  626. #pragma mark ===== ReadFile =====
  627. #pragma --------------------------------------------------------------------------------------------
  628. - (void)readFile
  629. {
  630. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  631. NSString *fileName;
  632. if (_metadataNet.fileName)
  633. fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  634. else
  635. fileName = _metadataNet.serverUrl;
  636. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  637. [communication setUserAgent:[CCUtility getUserAgent]];
  638. [communication readFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  639. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  640. if ([recordAccount.account isEqualToString:_metadataNet.account] && [items count] > 0) {
  641. CCMetadata *metadata = [[CCMetadata alloc] init];
  642. OCFileDto *itemDto = [items objectAtIndex:0];
  643. itemDto.fileName = _metadataNet.fileName;
  644. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_metadataNet.serverUrl activeAccount:_metadataNet.account];
  645. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  646. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl];
  647. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  648. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:_metadataNet.fileNamePrint serverUrl:_metadataNet.serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser];
  649. if([self.delegate respondsToSelector:@selector(readFileSuccess:metadata:)])
  650. [self.delegate readFileSuccess:_metadataNet metadata:metadata];
  651. }
  652. // BUG 1038
  653. if ([items count] == 0) {
  654. #ifndef EXTENSION
  655. [app messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  656. #endif
  657. }
  658. [self complete];
  659. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  660. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  661. _metadataNet.errorRetry++;
  662. NSInteger errorCode = response.statusCode;
  663. if (errorCode == 0)
  664. errorCode = error.code;
  665. // Error
  666. if ([self.delegate respondsToSelector:@selector(readFileFailure:message:errorCode:)] && [recordAccount.account isEqualToString:_metadataNet.account]) {
  667. if (errorCode == 503)
  668. [self.delegate readFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  669. else
  670. [self.delegate readFileFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  671. }
  672. // Request trusted certificated
  673. if ([error code] == NSURLErrorServerCertificateUntrusted)
  674. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  675. [self complete];
  676. }];
  677. }
  678. - (NSError *)readFileSync:(NSString *)filePathName
  679. {
  680. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  681. __block NSError *returnError;
  682. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  683. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  684. [communication setUserAgent:[CCUtility getUserAgent]];
  685. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  686. returnError = nil;
  687. dispatch_semaphore_signal(semaphore);
  688. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  689. // Request trusted certificated
  690. if ([error code] == NSURLErrorServerCertificateUntrusted)
  691. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  692. returnError = error;
  693. dispatch_semaphore_signal(semaphore);
  694. }];
  695. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  696. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  697. return returnError;
  698. }
  699. #pragma --------------------------------------------------------------------------------------------
  700. #pragma mark ===== Shared =====
  701. #pragma --------------------------------------------------------------------------------------------
  702. - (NSMutableDictionary *)getShareID
  703. {
  704. #ifndef EXTENSION
  705. return app.sharesID;
  706. #endif
  707. return [NSMutableDictionary new];
  708. }
  709. - (void)readShareServer
  710. {
  711. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  712. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  713. [communication setUserAgent:[CCUtility getUserAgent]];
  714. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  715. BOOL openWindow = NO;
  716. [[self getShareID] removeAllObjects];
  717. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  718. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  719. for (OCSharedDto *item in items)
  720. [[self getShareID] setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  721. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  722. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  723. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  724. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  725. }
  726. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  727. [self.delegate readSharedSuccess:_metadataNet items:[self getShareID] openWindow:openWindow];
  728. [self complete];
  729. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  730. NSInteger errorCode = response.statusCode;
  731. if (errorCode == 0)
  732. errorCode = error.code;
  733. // Error
  734. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  735. if (errorCode == 503)
  736. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  737. else
  738. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  739. }
  740. // Request trusted certificated
  741. if ([error code] == NSURLErrorServerCertificateUntrusted)
  742. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  743. [self complete];
  744. }];
  745. }
  746. - (void)share
  747. {
  748. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  749. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  750. [communication setUserAgent:[CCUtility getUserAgent]];
  751. [communication shareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPassword:[_metadataNet.password encodeString:NSUTF8StringEncoding] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  752. [self readShareServer];
  753. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  754. NSInteger errorCode = response.statusCode;
  755. if (errorCode == 0)
  756. errorCode = error.code;
  757. // Error
  758. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  759. if (errorCode == 503)
  760. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  761. else
  762. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  763. }
  764. // Request trusted certificated
  765. if ([error code] == NSURLErrorServerCertificateUntrusted)
  766. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  767. [self complete];
  768. }];
  769. }
  770. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  771. - (void)shareWith
  772. {
  773. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  774. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  775. [communication setUserAgent:[CCUtility getUserAgent]];
  776. [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) {
  777. [self readShareServer];
  778. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  779. NSInteger errorCode = response.statusCode;
  780. if (errorCode == 0)
  781. errorCode = error.code;
  782. // Error
  783. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  784. if (errorCode == 503)
  785. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  786. else
  787. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  788. }
  789. // Request trusted certificated
  790. if ([error code] == NSURLErrorServerCertificateUntrusted)
  791. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  792. [self complete];
  793. }];
  794. }
  795. - (void)updateShare
  796. {
  797. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  798. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  799. [communication setUserAgent:[CCUtility getUserAgent]];
  800. [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) {
  801. [self readShareServer];
  802. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  803. #ifndef EXTENSION
  804. [app messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  805. #endif
  806. NSInteger errorCode = response.statusCode;
  807. if (errorCode == 0)
  808. errorCode = error.code;
  809. // Error
  810. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  811. if (errorCode == 503)
  812. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  813. else
  814. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  815. }
  816. // Request trusted certificated
  817. if ([error code] == NSURLErrorServerCertificateUntrusted)
  818. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  819. [self complete];
  820. }];
  821. }
  822. - (void)unShare
  823. {
  824. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  825. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  826. [communication setUserAgent:[CCUtility getUserAgent]];
  827. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  828. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  829. [self.delegate unShareSuccess:_metadataNet];
  830. [self complete];
  831. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  832. #ifndef EXTENSION
  833. [app messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  834. #endif
  835. NSInteger errorCode = response.statusCode;
  836. if (errorCode == 0)
  837. errorCode = error.code;
  838. // Error
  839. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  840. if (errorCode == 503)
  841. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  842. else
  843. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  844. }
  845. // Request trusted certificated
  846. if ([error code] == NSURLErrorServerCertificateUntrusted)
  847. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  848. [self complete];
  849. }];
  850. }
  851. - (void)getUserAndGroup
  852. {
  853. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  854. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  855. [communication setUserAgent:[CCUtility getUserAgent]];
  856. [communication searchUsersAndGroupsWith:_metadataNet.options forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  857. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  858. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  859. [self complete];
  860. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  861. NSInteger errorCode = response.statusCode;
  862. if (errorCode == 0)
  863. errorCode = error.code;
  864. // Error
  865. if ([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)]) {
  866. if (errorCode == 503)
  867. [self.delegate getUserAndGroupFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  868. else
  869. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  870. }
  871. // Request trusted certificated
  872. if ([error code] == NSURLErrorServerCertificateUntrusted)
  873. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  874. [self complete];
  875. }];
  876. }
  877. #pragma --------------------------------------------------------------------------------------------
  878. #pragma mark ===== Activity =====
  879. #pragma --------------------------------------------------------------------------------------------
  880. - (void)getActivityServer
  881. {
  882. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  883. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  884. [communication setUserAgent:[CCUtility getUserAgent]];
  885. [communication getActivityServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  886. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccess:)])
  887. [self.delegate getActivityServerSuccess:listOfActivity];
  888. [self complete];
  889. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  890. NSInteger errorCode = response.statusCode;
  891. if (errorCode == 0)
  892. errorCode = error.code;
  893. // Error
  894. if ([self.delegate respondsToSelector:@selector(getActivityServerFailure:message:errorCode:)]) {
  895. if (errorCode == 503)
  896. [self.delegate getActivityServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  897. else
  898. [self.delegate getActivityServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  899. }
  900. // Request trusted certificated
  901. if ([error code] == NSURLErrorServerCertificateUntrusted)
  902. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  903. [self complete];
  904. }];
  905. }
  906. #pragma --------------------------------------------------------------------------------------------
  907. #pragma mark ===== External Sites =====
  908. #pragma --------------------------------------------------------------------------------------------
  909. - (void)getExternalSitesServer
  910. {
  911. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  912. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  913. [communication setUserAgent:[CCUtility getUserAgent]];
  914. [communication getExternalSitesServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  915. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccess:)])
  916. [self.delegate getExternalSitesServerSuccess:listOfExternalSites];
  917. [self complete];
  918. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  919. NSInteger errorCode = response.statusCode;
  920. if (errorCode == 0)
  921. errorCode = error.code;
  922. // Error
  923. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  924. if (errorCode == 503)
  925. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  926. else
  927. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  928. }
  929. // Request trusted certificated
  930. if ([error code] == NSURLErrorServerCertificateUntrusted)
  931. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  932. [self complete];
  933. }];
  934. }
  935. #pragma --------------------------------------------------------------------------------------------
  936. #pragma mark ===== Notification =====
  937. #pragma --------------------------------------------------------------------------------------------
  938. - (void)getNotificationServer
  939. {
  940. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  941. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  942. [communication setUserAgent:[CCUtility getUserAgent]];
  943. [communication getNotificationServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  944. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccess:)])
  945. [self.delegate getNotificationServerSuccess:listOfNotifications];
  946. [self complete];
  947. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  948. NSInteger errorCode = response.statusCode;
  949. if (errorCode == 0)
  950. errorCode = error.code;
  951. // Error
  952. if ([self.delegate respondsToSelector:@selector(getNotificationServerFailure:message:errorCode:)]) {
  953. if (errorCode == 503)
  954. [self.delegate getNotificationServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  955. else
  956. [self.delegate getNotificationServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  957. }
  958. // Request trusted certificated
  959. if ([error code] == NSURLErrorServerCertificateUntrusted)
  960. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  961. // Activity
  962. [CCCoreData addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  963. [self complete];
  964. }];
  965. }
  966. - (void)setNotificationServer
  967. {
  968. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  969. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  970. [communication setUserAgent:[CCUtility getUserAgent]];
  971. NSString *type = _metadataNet.options;
  972. [communication setNotificationServer:_metadataNet.serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  973. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccess:)])
  974. [self.delegate setNotificationServerSuccess:_metadataNet];
  975. [self complete];
  976. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  977. NSInteger errorCode = response.statusCode;
  978. if (errorCode == 0)
  979. errorCode = error.code;
  980. // Error
  981. if ([self.delegate respondsToSelector:@selector(setNotificationServerFailure:message:errorCode:)]) {
  982. if (errorCode == 503)
  983. [self.delegate setNotificationServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  984. else
  985. [self.delegate setNotificationServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  986. }
  987. // Request trusted certificated
  988. if ([error code] == NSURLErrorServerCertificateUntrusted)
  989. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  990. [self complete];
  991. }];
  992. }
  993. - (void)subscribingNextcloudServer
  994. {
  995. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  996. communication.kindOfCredential = credentialNotSet;
  997. [communication setUserAgent:[CCUtility getUserAgent]];
  998. NSDictionary *parameter = _metadataNet.options;
  999. NSString *pushToken = [parameter objectForKey:@"pushToken"];
  1000. NSString *pushTokenHash = [parameter objectForKey:@"pushTokenHash"];
  1001. NSString *devicePublicKey = [parameter objectForKey:@"devicePublicKey"];
  1002. // encode URL
  1003. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  1004. [communication subscribingNextcloudServerPush:_activeUrl pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath:k_pushNotificationServer onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1005. // encode URL
  1006. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  1007. signature = [CCUtility URLEncodeStringFromString:signature];
  1008. [communication subscribingPushProxy:k_pushNotificationServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature userPublicKey:[CCUtility URLEncodeStringFromString:publicKey] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1009. // Activity
  1010. [CCCoreData addActivityClient:k_pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:@"Service registered." type:k_activityTypeSuccess verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1011. [self complete];
  1012. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1013. NSInteger errorCode = response.statusCode;
  1014. if (errorCode == 0)
  1015. errorCode = error.code;
  1016. // Error
  1017. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1018. if (errorCode == 503)
  1019. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1020. else
  1021. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1022. }
  1023. // Request trusted certificated
  1024. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1025. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1026. // Activity
  1027. [CCCoreData addActivityClient:k_pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1028. [self complete];
  1029. }];
  1030. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1031. NSInteger errorCode = response.statusCode;
  1032. if (errorCode == 0)
  1033. errorCode = error.code;
  1034. // Error
  1035. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1036. if (errorCode == 503)
  1037. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1038. else
  1039. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1040. }
  1041. // Request trusted certificated
  1042. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1043. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1044. // Activity
  1045. [CCCoreData addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1046. [self complete];
  1047. }];
  1048. }
  1049. #pragma --------------------------------------------------------------------------------------------
  1050. #pragma mark ===== User Profile =====
  1051. #pragma --------------------------------------------------------------------------------------------
  1052. - (void)getUserProfile
  1053. {
  1054. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1055. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1056. [communication setUserAgent:[CCUtility getUserAgent]];
  1057. [communication getUserProfileServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1058. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccess:userProfile:)])
  1059. [self.delegate getUserProfileSuccess:_metadataNet userProfile:userProfile];
  1060. [self complete];
  1061. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1062. NSInteger errorCode = response.statusCode;
  1063. if (errorCode == 0)
  1064. errorCode = error.code;
  1065. // Error
  1066. if ([self.delegate respondsToSelector:@selector(getUserProfileFailure:message:errorCode:)]) {
  1067. if (errorCode == 503)
  1068. [self.delegate getUserProfileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1069. else
  1070. [self.delegate getUserProfileFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1071. }
  1072. // Request trusted certificated
  1073. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1074. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1075. [self complete];
  1076. }];
  1077. }
  1078. #pragma --------------------------------------------------------------------------------------------
  1079. #pragma mark ===== Server =====
  1080. #pragma --------------------------------------------------------------------------------------------
  1081. - (NSError *)checkServerSync:(NSString *)serverUrl
  1082. {
  1083. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1084. __block NSError *returnError;
  1085. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  1086. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1087. [communication setUserAgent:[CCUtility getUserAgent]];
  1088. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1089. returnError = nil;
  1090. dispatch_semaphore_signal(semaphore);
  1091. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1092. // Request trusted certificated
  1093. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1094. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1095. returnError = error;
  1096. dispatch_semaphore_signal(semaphore);
  1097. }];
  1098. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  1099. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  1100. return returnError;
  1101. }
  1102. - (void)getFeaturesSupportedByServer
  1103. {
  1104. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1105. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1106. [communication setUserAgent:[CCUtility getUserAgent]];
  1107. [communication getFeaturesSupportedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer) {
  1108. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  1109. if ([self.delegate respondsToSelector:@selector(getFeaturesSupportedByServerSuccess:hasForbiddenCharactersSupport:hasShareSupport:hasShareeSupport:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  1110. [self.delegate getFeaturesSupportedByServerSuccess:hasCapabilitiesSupport hasForbiddenCharactersSupport:hasForbiddenCharactersSupport hasShareSupport:hasShareSupport hasShareeSupport:hasShareeSupport];
  1111. [self complete];
  1112. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1113. NSInteger errorCode = response.statusCode;
  1114. if (errorCode == 0)
  1115. errorCode = error.code;
  1116. // Error
  1117. if ([self.delegate respondsToSelector:@selector(getInfoServerFailure:message:errorCode:)]) {
  1118. if (errorCode == 503)
  1119. [self.delegate getInfoServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1120. else
  1121. [self.delegate getInfoServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1122. }
  1123. // Request trusted certificated
  1124. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1125. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1126. // Activity
  1127. [CCCoreData addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionFeatures selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1128. [self complete];
  1129. }];
  1130. }
  1131. - (void)getCapabilitiesOfServer
  1132. {
  1133. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1134. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1135. [communication setUserAgent:[CCUtility getUserAgent]];
  1136. [communication getCapabilitiesOfServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1137. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  1138. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccess:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  1139. [self.delegate getCapabilitiesOfServerSuccess:capabilities];
  1140. [self complete];
  1141. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1142. NSInteger errorCode = response.statusCode;
  1143. if (errorCode == 0)
  1144. errorCode = error.code;
  1145. // Error
  1146. if ([self.delegate respondsToSelector:@selector(getInfoServerFailure:message:errorCode:)]) {
  1147. if (errorCode == 503)
  1148. [self.delegate getInfoServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1149. else
  1150. [self.delegate getInfoServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1151. }
  1152. // Request trusted certificated
  1153. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1154. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1155. // Activity
  1156. [CCCoreData addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionCapabilities selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh account:_metadataNet.account activeUrl:_activeUrl];
  1157. [self complete];
  1158. }];
  1159. }
  1160. @end
  1161. #pragma --------------------------------------------------------------------------------------------
  1162. #pragma mark ===== OCURLSessionManager =====
  1163. #pragma --------------------------------------------------------------------------------------------
  1164. @implementation OCURLSessionManager
  1165. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1166. {
  1167. // The pinnning check
  1168. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  1169. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1170. } else {
  1171. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1172. }
  1173. }
  1174. @end