OCNetworking.m 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  1. //
  2. // OCnetworking.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "NSString+Encode.h"
  27. #import "NCBridgeSwift.h"
  28. #import "NCXMLGetAppPasswordParser.h"
  29. @implementation OCNetworking
  30. + (OCNetworking *)sharedManager {
  31. static OCNetworking *sharedManager;
  32. @synchronized(self)
  33. {
  34. if (!sharedManager) {
  35. sharedManager = [OCNetworking new];
  36. sharedManager.checkRemoteUserInProgress = false;
  37. }
  38. return sharedManager;
  39. }
  40. }
  41. - (id)init
  42. {
  43. self = [super init];
  44. [self sharedOCCommunication];
  45. return self;
  46. }
  47. #pragma --------------------------------------------------------------------------------------------
  48. #pragma mark ===== OCCommunication =====
  49. #pragma --------------------------------------------------------------------------------------------
  50. - (OCCommunication *)sharedOCCommunication
  51. {
  52. static OCCommunication* sharedOCCommunication = nil;
  53. if (sharedOCCommunication == nil)
  54. {
  55. // Network
  56. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  57. configuration.allowsCellularAccess = YES;
  58. configuration.discretionary = NO;
  59. configuration.HTTPMaximumConnectionsPerHost = k_maxConcurrentOperation;
  60. configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  61. OCURLSessionManager *networkSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configuration];
  62. [networkSessionManager.operationQueue setMaxConcurrentOperationCount: k_maxConcurrentOperation];
  63. networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  64. // Download
  65. NSURLSessionConfiguration *configurationDownload = [NSURLSessionConfiguration defaultSessionConfiguration];
  66. configurationDownload.allowsCellularAccess = YES;
  67. configurationDownload.discretionary = NO;
  68. configurationDownload.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  69. configurationDownload.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  70. configurationDownload.timeoutIntervalForRequest = k_timeout_upload;
  71. OCURLSessionManager *downloadSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configurationDownload];
  72. [downloadSessionManager.operationQueue setMaxConcurrentOperationCount:k_maxHTTPConnectionsPerHost];
  73. [downloadSessionManager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
  74. return NSURLSessionAuthChallengePerformDefaultHandling;
  75. }];
  76. // Upload
  77. NSURLSessionConfiguration *configurationUpload = [NSURLSessionConfiguration defaultSessionConfiguration];
  78. configurationUpload.allowsCellularAccess = YES;
  79. configurationUpload.discretionary = NO;
  80. configurationUpload.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  81. configurationUpload.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  82. configurationUpload.timeoutIntervalForRequest = k_timeout_upload;
  83. OCURLSessionManager *uploadSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configurationUpload];
  84. [uploadSessionManager.operationQueue setMaxConcurrentOperationCount:k_maxHTTPConnectionsPerHost];
  85. [uploadSessionManager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
  86. return NSURLSessionAuthChallengePerformDefaultHandling;
  87. }];
  88. sharedOCCommunication = [[OCCommunication alloc] initWithUploadSessionManager:uploadSessionManager andDownloadSessionManager:downloadSessionManager andNetworkSessionManager:networkSessionManager];
  89. }
  90. return sharedOCCommunication;
  91. }
  92. #pragma --------------------------------------------------------------------------------------------
  93. #pragma mark ===== Server =====
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (void)checkServerUrl:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password completion:(void (^)(NSString *message, NSInteger errorCode))completion
  96. {
  97. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  98. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  99. [communication setUserAgent:[CCUtility getUserAgent]];
  100. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  101. completion(nil, 0);
  102. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  103. NSString *message;
  104. NSInteger errorCode = response.statusCode;
  105. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  106. errorCode = error.code;
  107. // Error
  108. if (errorCode == 503)
  109. message = NSLocalizedString(@"_server_error_retry_", nil);
  110. else
  111. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  112. completion(message, errorCode);
  113. }];
  114. }
  115. /*
  116. - (void)serverStatusUrl:(NSString *)serverUrl completion:(void(^)(NSString *serverProductName, NSInteger versionMajor, NSInteger versionMicro, NSInteger versionMinor, BOOL extendedSupport,NSString *message, NSInteger errorCode))completion
  117. {
  118. NSString *urlTest = [serverUrl stringByAppendingString:k_serverStatus];
  119. // Remove stored cookies
  120. NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  121. for (NSHTTPCookie *cookie in [storage cookies])
  122. {
  123. [storage deleteCookie:cookie];
  124. }
  125. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlTest] cachePolicy:0 timeoutInterval:20.0];
  126. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  127. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  128. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  129. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  130. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  131. if (error) {
  132. NSString *message;
  133. NSInteger errorCode;
  134. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  135. errorCode = httpResponse.statusCode;
  136. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  137. errorCode = error.code;
  138. // Error
  139. if (errorCode == 503)
  140. message = NSLocalizedString(@"_server_error_retry_", nil);
  141. else
  142. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  143. dispatch_sync(dispatch_get_main_queue(), ^{
  144. completion(nil, 0, 0, 0, false, message, errorCode);
  145. });
  146. } else {
  147. NSString *serverProductName = @"";
  148. NSString *serverVersion = @"0.0.0";
  149. NSString *serverVersionString = @"0.0.0";
  150. NSInteger versionMajor = 0;
  151. NSInteger versionMicro = 0;
  152. NSInteger versionMinor = 0;
  153. BOOL extendedSupport = FALSE;
  154. NSError *error;
  155. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  156. if (error) {
  157. dispatch_sync(dispatch_get_main_queue(), ^{
  158. completion(nil, 0, 0, 0, extendedSupport, NSLocalizedString(@"_no_nextcloud_found_", nil), k_CCErrorInternalError);
  159. });
  160. } else {
  161. serverProductName = [[jsongParsed valueForKey:@"productname"] lowercaseString];
  162. serverVersion = [jsongParsed valueForKey:@"version"];
  163. serverVersionString = [jsongParsed valueForKey:@"versionstring"];
  164. NSArray *arrayVersion = [serverVersionString componentsSeparatedByString:@"."];
  165. if (arrayVersion.count == 1) {
  166. versionMajor = [arrayVersion[0] integerValue];
  167. } else if (arrayVersion.count == 2) {
  168. versionMajor = [arrayVersion[0] integerValue];
  169. versionMinor = [arrayVersion[1] integerValue];
  170. } else if (arrayVersion.count >= 3) {
  171. versionMajor = [arrayVersion[0] integerValue];
  172. versionMinor = [arrayVersion[1] integerValue];
  173. versionMicro = [arrayVersion[2] integerValue];
  174. }
  175. extendedSupport = [[jsongParsed valueForKey:@"extendedSupport"] boolValue];
  176. dispatch_sync(dispatch_get_main_queue(), ^{
  177. completion(serverProductName, versionMajor, versionMicro, versionMinor, extendedSupport, nil, 0);
  178. });
  179. }
  180. }
  181. }];
  182. [task resume];
  183. }
  184. */
  185. /*
  186. - (void)downloadContentsOfUrl:(NSString *)serverUrl completion:(void(^)(NSData *data, NSString *message, NSInteger errorCode))completion
  187. {
  188. // Remove stored cookies
  189. NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  190. for (NSHTTPCookie *cookie in [storage cookies])
  191. {
  192. [storage deleteCookie:cookie];
  193. }
  194. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverUrl] cachePolicy:0 timeoutInterval:20.0];
  195. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  196. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  197. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  198. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  199. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  200. dispatch_sync(dispatch_get_main_queue(), ^{
  201. if (error) {
  202. NSString *message;
  203. NSInteger errorCode;
  204. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  205. errorCode = httpResponse.statusCode;
  206. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  207. errorCode = error.code;
  208. // Error
  209. if (errorCode == 503)
  210. message = NSLocalizedString(@"_server_error_retry_", nil);
  211. else
  212. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  213. completion(nil, message, errorCode);
  214. } else {
  215. completion(data, nil, 0);
  216. }
  217. });
  218. }];
  219. [task resume];
  220. }
  221. */
  222. - (void)getAppPassword:(NSString *)serverUrl username:(NSString *)username password:(NSString *)password completion:(void(^)(NSString *token, NSString *message, NSInteger errorCode))completion
  223. {
  224. NSData *authData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
  225. NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  226. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/core/getapppassword", serverUrl];
  227. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  228. [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  229. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  230. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  231. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  232. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  233. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  234. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  235. NSInteger errorCode = 0;
  236. NSString *message = @"";
  237. NSString *token = nil;
  238. if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) {
  239. NCXMLGetAppPasswordParser *parser = [NCXMLGetAppPasswordParser new];
  240. [parser initParserWithData:data];
  241. token = parser.token;
  242. } else {
  243. errorCode = httpResponse.statusCode;
  244. message = [NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode];
  245. }
  246. dispatch_async(dispatch_get_main_queue(), ^{
  247. completion(token, message, errorCode);
  248. });
  249. }];
  250. [task resume];
  251. }
  252. #pragma --------------------------------------------------------------------------------------------
  253. #pragma mark ===== download / upload =====
  254. #pragma --------------------------------------------------------------------------------------------
  255. - (NSURLSessionTask *)downloadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath encode:(BOOL)encode communication:(OCCommunication *)communication completion:(void (^)(NSString *account, int64_t length, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion
  256. {
  257. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  258. if (tableAccount == nil) {
  259. completion(account, 0, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  260. } else if ([CCUtility getPassword:account].length == 0) {
  261. completion(account, 0, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  262. } else if ([CCUtility getCertificateError:account]) {
  263. completion(account, 0, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  264. }
  265. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  266. [communication setUserAgent:[CCUtility getUserAgent]];
  267. NSURLSessionTask *sessionTask = [communication downloadFileSession:fileNameServerUrl toDestiny:fileNameLocalPath defaultPriority:YES encode:encode onCommunication:communication progress:^(NSProgress *progress) {
  268. //float percent = roundf (progress.fractionCompleted * 100);
  269. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  270. int64_t totalUnitCount = 0;
  271. NSDate *date = [NSDate date];
  272. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  273. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  274. [dateFormatter setLocale:enUSPOSIXLocale];
  275. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  276. NSError *error;
  277. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  278. NSString *contentLength = [fields objectForKey:@"Content-Length"];
  279. if(contentLength) {
  280. totalUnitCount = (int64_t) [contentLength longLongValue];
  281. }
  282. NSString *dateString = [fields objectForKey:@"Date"];
  283. if (dateString) {
  284. if (![dateFormatter getObjectValue:&date forString:dateString range:nil error:&error]) {
  285. date = [NSDate date];
  286. }
  287. } else {
  288. date = [NSDate date];
  289. }
  290. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  291. if (etag == nil) {
  292. completion(account, 0, nil, nil, NSLocalizedString(@"Internal error", nil), k_CCErrorInternalError);
  293. } else {
  294. completion(account, totalUnitCount, etag, date, nil, 0);
  295. }
  296. } failureRequest:^(NSURLResponse *response, NSError *error) {
  297. NSString *message;
  298. NSInteger errorCode = ((NSHTTPURLResponse*)response).statusCode;
  299. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  300. errorCode = error.code;
  301. // Server Unauthorized
  302. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  303. [[OCNetworking sharedManager] checkRemoteUser:account function:@"download" errorCode:errorCode];
  304. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  305. [CCUtility setCertificateError:account error:YES];
  306. }
  307. // Error
  308. if (errorCode == 503)
  309. message = NSLocalizedString(@"_server_error_retry_", nil);
  310. else
  311. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  312. completion(account, 0, nil, nil, message, errorCode);
  313. }];
  314. return sessionTask;
  315. }
  316. - (NSURLSessionTask *)downloadWithAccount:(NSString *)account url:(NSString *)url fileNameLocalPath:(NSString *)fileNameLocalPath encode:(BOOL)encode completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  317. {
  318. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  319. if (tableAccount == nil) {
  320. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  321. } else if ([CCUtility getPassword:account].length == 0) {
  322. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  323. } else if ([CCUtility getCertificateError:account]) {
  324. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  325. }
  326. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  327. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  328. [communication setUserAgent:[CCUtility getUserAgent]];
  329. NSURLSessionTask *sessionTask = [communication downloadFileSession:url toDestiny:fileNameLocalPath defaultPriority:YES encode:encode onCommunication:communication progress:^(NSProgress *progress) {
  330. //float percent = roundf (progress.fractionCompleted * 100);
  331. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  332. completion(account, nil, 0);
  333. } failureRequest:^(NSURLResponse *response, NSError *error) {
  334. NSString *message;
  335. NSInteger errorCode = ((NSHTTPURLResponse*)response).statusCode;
  336. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  337. errorCode = error.code;
  338. // Server Unauthorized
  339. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  340. [[OCNetworking sharedManager] checkRemoteUser:account function:@"download" errorCode:errorCode];
  341. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  342. [CCUtility setCertificateError:account error:YES];
  343. }
  344. // Error
  345. if (errorCode == 503)
  346. message = NSLocalizedString(@"_server_error_retry_", nil);
  347. else
  348. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  349. completion(account, message, errorCode);
  350. }];
  351. return sessionTask;
  352. }
  353. - (NSURLSessionTask *)uploadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath encode:(BOOL)encode communication:(OCCommunication *)communication progress:(void(^)(NSProgress *progress))uploadProgress completion:(void(^)(NSString *account, NSString *ocId, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion
  354. {
  355. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  356. if (tableAccount == nil) {
  357. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  358. } else if ([CCUtility getPassword:account].length == 0) {
  359. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  360. } else if ([CCUtility getCertificateError:account]) {
  361. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  362. }
  363. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  364. [communication setUserAgent:[CCUtility getUserAgent]];
  365. NSURLSessionTask *sessionTask = [communication uploadFileSession:fileNameLocalPath toDestiny:fileNameServerUrl encode:encode onCommunication:communication progress:^(NSProgress *progress) {
  366. uploadProgress(progress);
  367. //float percent = roundf (progress.fractionCompleted * 100);
  368. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  369. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  370. NSString *ocId = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  371. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  372. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  373. completion(account, ocId, etag, date, nil, 0);
  374. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  375. NSString *message;
  376. NSInteger errorCode = ((NSHTTPURLResponse*)response).statusCode;
  377. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  378. errorCode = error.code;
  379. // Server Unauthorized
  380. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  381. [[OCNetworking sharedManager] checkRemoteUser:account function:@"upload" errorCode:errorCode];
  382. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  383. [CCUtility setCertificateError:account error:YES];
  384. }
  385. // Error
  386. if (errorCode == 503)
  387. message = NSLocalizedString(@"_server_error_retry_", nil);
  388. else
  389. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  390. completion(account, nil, nil, nil, message, errorCode);
  391. } failureBeforeRequest:^(NSError *error) {
  392. completion(account, nil, nil, nil, error.description, error.code);
  393. }];
  394. return sessionTask;
  395. }
  396. #pragma --------------------------------------------------------------------------------------------
  397. #pragma mark ===== WebDav =====
  398. #pragma --------------------------------------------------------------------------------------------
  399. - (void)readFolderWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void(^)(NSString *account, NSArray *metadatas, tableMetadata *metadataFolder, NSString *message, NSInteger errorCode))completion
  400. {
  401. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  402. if (tableAccount == nil) {
  403. completion(account, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  404. } else if ([CCUtility getPassword:account].length == 0) {
  405. completion(account, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  406. } else if ([CCUtility getCertificateError:account]) {
  407. completion(account, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  408. }
  409. NSString *url = tableAccount.url;
  410. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  411. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:url];
  412. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  413. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  414. [communication setUserAgent:[CCUtility getUserAgent]];
  415. [communication readFolder:serverUrl depth:depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  416. // Check items > 0
  417. if ([items count] == 0) {
  418. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  419. completion(account, nil, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  420. } else {
  421. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  422. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  423. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  424. // directory [0]
  425. OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  426. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  427. NSMutableArray *metadatas = [NSMutableArray new];
  428. tableMetadata *metadataFolder = [tableMetadata new];
  429. NSString *serverUrlFolder;
  430. // Metadata . (self Folder)
  431. if ([serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:url]]) {
  432. // root folder
  433. serverUrlFolder = k_serverUrl_root;
  434. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:@"." serverUrl:serverUrlFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory account:account isFolderEncrypted:isFolderEncrypted];
  435. } else {
  436. serverUrlFolder = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  437. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:[serverUrl lastPathComponent] serverUrl:serverUrlFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory account:account isFolderEncrypted:isFolderEncrypted];
  438. }
  439. // Add metadata folder
  440. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDtoFolder.isEncrypted favorite:itemDtoFolder.isFavorite ocId:itemDtoFolder.ocId permissions:itemDtoFolder.permissions serverUrl:serverUrl richWorkspace:nil account:account];
  441. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  442. NSString *first = [(OCFileDto*)a fileName];
  443. NSString *second = [(OCFileDto*)b fileName];
  444. return [[first lowercaseString] compare:[second lowercaseString]];
  445. }];
  446. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  447. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  448. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  449. // Skip hidden files
  450. if (fileName.length > 0) {
  451. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  452. continue;
  453. } else {
  454. continue;
  455. }
  456. if (itemDto.isDirectory) {
  457. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite ocId:itemDto.ocId permissions:itemDto.permissions serverUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:fileName] richWorkspace: nil account:account];
  458. }
  459. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory account:account isFolderEncrypted:isFolderEncrypted]];
  460. }
  461. dispatch_async(dispatch_get_main_queue(), ^{
  462. completion(account, metadatas, metadataFolder, nil, 0);
  463. });
  464. });
  465. }
  466. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  467. NSString *message;
  468. NSInteger errorCode = response.statusCode;
  469. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  470. errorCode = error.code;
  471. // Server Unauthorized
  472. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  473. [[OCNetworking sharedManager] checkRemoteUser:account function:@"read file or folder" errorCode:errorCode];
  474. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  475. [CCUtility setCertificateError:account error:YES];
  476. }
  477. // Error
  478. if (errorCode == 503)
  479. message = NSLocalizedString(@"_server_error_retry_", nil);
  480. else
  481. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  482. completion(account, nil, nil, message, errorCode);
  483. }];
  484. }
  485. #pragma --------------------------------------------------------------------------------------------
  486. #pragma mark ===== Share =====
  487. #pragma --------------------------------------------------------------------------------------------
  488. - (void)readShareWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  489. {
  490. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  491. if (tableAccount == nil) {
  492. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  493. } else if ([CCUtility getPassword:account].length == 0) {
  494. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  495. } else if ([CCUtility getCertificateError:account]) {
  496. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  497. }
  498. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  499. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  500. [communication setUserAgent:[CCUtility getUserAgent]];
  501. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  502. completion(account, items, nil, 0);
  503. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  504. NSString *message;
  505. NSInteger errorCode = response.statusCode;
  506. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  507. errorCode = error.code;
  508. // Error
  509. if (errorCode == 503) {
  510. message = NSLocalizedString(@"_server_error_retry_", nil);
  511. } else {
  512. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  513. }
  514. completion(account, nil, message, errorCode);
  515. }];
  516. }
  517. - (void)readShareWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  518. {
  519. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  520. if (tableAccount == nil) {
  521. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  522. } else if ([CCUtility getPassword:account].length == 0) {
  523. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  524. } else if ([CCUtility getCertificateError:account]) {
  525. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  526. }
  527. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  528. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  529. [communication setUserAgent:[CCUtility getUserAgent]];
  530. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] andPath:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  531. completion(account, listOfShared, nil, 0);
  532. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  533. NSString *message;
  534. NSInteger errorCode = response.statusCode;
  535. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  536. errorCode = error.code;
  537. // Error
  538. if (errorCode == 503) {
  539. message = NSLocalizedString(@"_server_error_retry_", nil);
  540. } else {
  541. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  542. }
  543. completion(account, nil, message, errorCode);
  544. }];
  545. }
  546. - (void)shareWithAccount:(NSString *)account fileName:(NSString *)fileName password:(NSString *)password permission:(NSInteger)permission hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  547. {
  548. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  549. if (tableAccount == nil) {
  550. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  551. } else if ([CCUtility getPassword:account].length == 0) {
  552. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  553. } else if ([CCUtility getCertificateError:account]) {
  554. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  555. }
  556. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  557. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  558. [communication setUserAgent:[CCUtility getUserAgent]];
  559. [communication shareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPassword:[password encodeString:NSUTF8StringEncoding] andPermission:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  560. completion(account, nil, 0);
  561. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  562. NSString *message;
  563. NSInteger errorCode = response.statusCode;
  564. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  565. errorCode = error.code;
  566. // Error
  567. if (errorCode == 503) {
  568. message = NSLocalizedString(@"_server_error_retry_", nil);
  569. } else {
  570. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  571. }
  572. completion(account, message, errorCode);
  573. }];
  574. }
  575. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  576. - (void)shareUserGroupWithAccount:(NSString *)account userOrGroup:(NSString *)userOrGroup fileName:(NSString *)fileName permission:(NSInteger)permission shareeType:(NSInteger)shareeType completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  577. {
  578. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  579. if (tableAccount == nil) {
  580. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  581. } else if ([CCUtility getPassword:account].length == 0) {
  582. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  583. } else if ([CCUtility getCertificateError:account]) {
  584. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  585. }
  586. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  587. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  588. [communication setUserAgent:[CCUtility getUserAgent]];
  589. [communication shareWith:userOrGroup shareeType:shareeType inServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPermissions:permission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  590. completion(account, nil, 0);
  591. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  592. NSString *message;
  593. NSInteger errorCode = response.statusCode;
  594. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  595. errorCode = error.code;
  596. // Error
  597. if (errorCode == 503) {
  598. message = NSLocalizedString(@"_server_error_retry_", nil);
  599. } else {
  600. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  601. }
  602. completion(account, message, errorCode);
  603. }];
  604. }
  605. - (void)shareUpdateAccount:(NSString *)account shareID:(NSInteger)shareID password:(NSString *)password note:(NSString *)note permission:(NSInteger)permission expirationTime:(NSString *)expirationTime hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  606. {
  607. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  608. if (tableAccount == nil) {
  609. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  610. } else if ([CCUtility getPassword:account].length == 0) {
  611. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  612. } else if ([CCUtility getCertificateError:account]) {
  613. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  614. }
  615. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  616. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  617. [communication setUserAgent:[CCUtility getUserAgent]];
  618. [communication updateShare:shareID ofServerPath:[tableAccount.url stringByAppendingString:@"/"] withPasswordProtect:[password encodeString:NSUTF8StringEncoding] andNote:note andExpirationTime:expirationTime andPermissions:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  619. completion(account, nil, 0);
  620. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  621. NSString *message;
  622. NSInteger errorCode = response.statusCode;
  623. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  624. errorCode = error.code;
  625. // Error
  626. if (errorCode == 503) {
  627. message = NSLocalizedString(@"_server_error_retry_", nil);
  628. } else {
  629. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  630. }
  631. completion(account, message, errorCode);
  632. }];
  633. }
  634. - (void)unshareAccount:(NSString *)account shareID:(NSInteger)shareID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  635. {
  636. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  637. if (tableAccount == nil) {
  638. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  639. } else if ([CCUtility getPassword:account].length == 0) {
  640. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  641. } else if ([CCUtility getCertificateError:account]) {
  642. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  643. }
  644. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  645. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  646. [communication setUserAgent:[CCUtility getUserAgent]];
  647. [communication unShareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andIdRemoteShared:shareID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  648. completion(account, nil, 0);
  649. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  650. NSString *message;
  651. NSInteger errorCode = response.statusCode;
  652. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  653. errorCode = error.code;
  654. // Error
  655. if (errorCode == 503) {
  656. message = NSLocalizedString(@"_server_error_retry_", nil);
  657. } else {
  658. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  659. }
  660. completion(account, message, errorCode);
  661. }];
  662. }
  663. - (void)getUserGroupWithAccount:(NSString *)account searchString:(NSString *)searchString completion:(void (^)(NSString *account, NSArray *item, NSString *message, NSInteger errorCode))completion
  664. {
  665. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  666. if (tableAccount == nil) {
  667. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  668. } else if ([CCUtility getPassword:account].length == 0) {
  669. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  670. } else if ([CCUtility getCertificateError:account]) {
  671. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  672. }
  673. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  674. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  675. [communication setUserAgent:[CCUtility getUserAgent]];
  676. [communication searchUsersAndGroupsWith:searchString forPage:1 with:50 ofServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  677. completion(account, itemList, nil, 0);
  678. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  679. NSString *message;
  680. NSInteger errorCode = response.statusCode;
  681. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  682. errorCode = error.code;
  683. // Error
  684. if (errorCode == 503) {
  685. message = NSLocalizedString(@"_server_error_retry_", nil);
  686. } else {
  687. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  688. }
  689. completion(account, nil, message, errorCode);
  690. }];
  691. }
  692. #pragma --------------------------------------------------------------------------------------------
  693. #pragma mark ===== VAR =====
  694. #pragma --------------------------------------------------------------------------------------------
  695. - (void)getActivityWithAccount:(NSString *)account since:(NSInteger)since limit:(NSInteger)limit objectId:(NSString *)objectId objectType:(NSString *)objectType link:(NSString *)link completion:(void(^)(NSString *account, NSArray *listOfActivity, NSString *message, NSInteger errorCode))completion
  696. {
  697. BOOL previews = false;
  698. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  699. if (tableAccount == nil) {
  700. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  701. } else if ([CCUtility getPassword:account].length == 0) {
  702. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  703. } else if ([CCUtility getCertificateError:account]) {
  704. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  705. }
  706. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:account];
  707. if (capabilities != nil && capabilities.versionMajor >= k_nextcloud_version_15_0) {
  708. previews = true;
  709. }
  710. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  711. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  712. [communication setUserAgent:[CCUtility getUserAgent]];
  713. [communication getActivityServer:[tableAccount.url stringByAppendingString:@"/"] since:since limit:limit objectId:objectId objectType:objectType previews:previews link:link onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  714. completion(account, listOfActivity, nil, 0);
  715. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  716. NSString *message;
  717. NSInteger errorCode = response.statusCode;
  718. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  719. errorCode = error.code;
  720. // Server Unauthorized
  721. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  722. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get activity" errorCode:errorCode];
  723. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  724. [CCUtility setCertificateError:account error:YES];
  725. }
  726. // Error
  727. if (errorCode == 503)
  728. message = NSLocalizedString(@"_server_error_retry_", nil);
  729. else
  730. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  731. completion(account, nil, message, errorCode);
  732. }];
  733. }
  734. - (void)getExternalSitesWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfExternalSites, NSString *message, NSInteger errorCode))completion
  735. {
  736. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  737. if (tableAccount == nil) {
  738. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  739. } else if ([CCUtility getPassword:account].length == 0) {
  740. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  741. } else if ([CCUtility getCertificateError:account]) {
  742. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  743. }
  744. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  745. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  746. [communication setUserAgent:[CCUtility getUserAgent]];
  747. [communication getExternalSitesServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  748. completion(account, listOfExternalSites, nil, 0);
  749. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  750. NSString *message;
  751. NSInteger errorCode = response.statusCode;
  752. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  753. errorCode = error.code;
  754. // Server Unauthorized
  755. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  756. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get external sites" errorCode:errorCode];
  757. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  758. [CCUtility setCertificateError:account error:YES];
  759. }
  760. // Error
  761. if (errorCode == 503) {
  762. message = NSLocalizedString(@"_server_error_retry_", nil);
  763. } else {
  764. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  765. }
  766. completion(account, nil, message, errorCode);
  767. }];
  768. }
  769. - (void)getNotificationWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  770. {
  771. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  772. if (tableAccount == nil) {
  773. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  774. } else if ([CCUtility getPassword:account].length == 0) {
  775. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  776. } else if ([CCUtility getCertificateError:account]) {
  777. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  778. }
  779. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  780. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  781. [communication setUserAgent:[CCUtility getUserAgent]];
  782. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  783. completion(account, listOfNotifications, nil, 0);
  784. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  785. NSString *message;
  786. NSInteger errorCode = response.statusCode;
  787. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  788. errorCode = error.code;
  789. // Server Unauthorized
  790. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  791. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get notification" errorCode:errorCode];
  792. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  793. [CCUtility setCertificateError:account error:YES];
  794. }
  795. // Error
  796. if (errorCode == 503) {
  797. message = NSLocalizedString(@"_server_error_retry_", nil);
  798. } else {
  799. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  800. }
  801. completion(account, nil, message, errorCode);
  802. }];
  803. }
  804. - (void)setNotificationWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  805. {
  806. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  807. if (tableAccount == nil) {
  808. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  809. } else if ([CCUtility getPassword:account].length == 0) {
  810. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  811. } else if ([CCUtility getCertificateError:account]) {
  812. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  813. }
  814. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  815. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  816. [communication setUserAgent:[CCUtility getUserAgent]];
  817. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  818. completion(account, nil, 0);
  819. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  820. NSString *message;
  821. NSInteger errorCode = response.statusCode;
  822. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  823. errorCode = error.code;
  824. // Server Unauthorized
  825. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  826. [[OCNetworking sharedManager] checkRemoteUser:account function:@"set notification" errorCode:errorCode];
  827. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  828. [CCUtility setCertificateError:account error:YES];
  829. }
  830. // Error
  831. if (errorCode == 503) {
  832. message = NSLocalizedString(@"_server_error_retry_", nil);
  833. } else {
  834. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  835. }
  836. completion(account, message, errorCode);
  837. }];
  838. }
  839. - (void)getCapabilitiesWithAccount:(NSString *)account completion:(void (^)(NSString *account, OCCapabilities *capabilities, NSString *message, NSInteger errorCode))completion
  840. {
  841. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  842. if (tableAccount == nil) {
  843. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  844. } else if ([CCUtility getPassword:account].length == 0) {
  845. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  846. } else if ([CCUtility getCertificateError:account]) {
  847. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  848. }
  849. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  850. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  851. [communication setUserAgent:[CCUtility getUserAgent]];
  852. [communication getCapabilitiesOfServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  853. completion(account, capabilities, nil, 0);
  854. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  855. NSString *message;
  856. NSInteger errorCode = response.statusCode;
  857. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  858. errorCode = error.code;
  859. // Server Unauthorized
  860. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  861. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get capabilities" errorCode:errorCode];
  862. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  863. [CCUtility setCertificateError:account error:YES];
  864. }
  865. // Error
  866. if (errorCode == 503) {
  867. message = NSLocalizedString(@"_server_error_retry_", nil);
  868. } else {
  869. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  870. }
  871. completion(account, nil, message, errorCode);
  872. }];
  873. }
  874. - (void)getUserProfileWithAccount:(NSString *)account completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  875. {
  876. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  877. if (tableAccount == nil) {
  878. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  879. } else if ([CCUtility getPassword:account].length == 0) {
  880. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  881. } else if ([CCUtility getCertificateError:account]) {
  882. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  883. }
  884. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  885. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  886. [communication setUserAgent:[CCUtility getUserAgent]];
  887. [communication getUserProfileServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  888. completion(account, userProfile, nil, 0);
  889. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  890. NSString *message;
  891. NSInteger errorCode = response.statusCode;
  892. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  893. errorCode = error.code;
  894. // Server Unauthorized
  895. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  896. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get user profile" errorCode:errorCode];
  897. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  898. [CCUtility setCertificateError:account error:YES];
  899. }
  900. // Error
  901. if (errorCode == 503) {
  902. message = NSLocalizedString(@"_server_error_retry_", nil);
  903. } else {
  904. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  905. }
  906. completion(account, nil, message, errorCode);
  907. }];
  908. }
  909. #pragma --------------------------------------------------------------------------------------------
  910. #pragma mark ===== Push Notification =====
  911. #pragma --------------------------------------------------------------------------------------------
  912. - (void)subscribingPushNotificationWithAccount:(NSString *)account url:(NSString *)url pushToken:(NSString *)pushToken Hash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey completion:(void(^)(NSString *account, NSString *deviceIdentifier, NSString *deviceIdentifierSignature, NSString *publicKey, NSString *message, NSInteger errorCode))completion
  913. {
  914. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  915. if (tableAccount == nil) {
  916. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  917. } else if ([CCUtility getPassword:account].length == 0) {
  918. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  919. } else if ([CCUtility getCertificateError:account]) {
  920. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  921. }
  922. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  923. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  924. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  925. #ifdef DEBUG
  926. // proxyServerPath = @"http://127.0.0.1:8088";
  927. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  928. #endif
  929. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  930. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  931. [communication setUserAgent:[CCUtility getUserAgent]];
  932. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: proxyServerPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  933. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  934. signature = [CCUtility URLEncodeStringFromString:signature];
  935. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  936. [communication subscribingPushProxy:proxyServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  937. completion(account, deviceIdentifier, signature, publicKey, nil, 0);
  938. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  939. NSString *message;
  940. NSInteger errorCode = response.statusCode;
  941. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  942. errorCode = error.code;
  943. // Error
  944. if (errorCode == 503)
  945. message = NSLocalizedString(@"_server_error_retry_", nil);
  946. else
  947. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  948. completion(account, nil, nil, nil, message, errorCode);
  949. }];
  950. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  951. NSString *message;
  952. NSInteger errorCode = response.statusCode;
  953. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  954. errorCode = error.code;
  955. // Error
  956. if (errorCode == 503)
  957. message = NSLocalizedString(@"_server_error_retry_", nil);
  958. else
  959. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  960. completion(account, nil, nil, nil, message, errorCode);
  961. }];
  962. }
  963. - (void)unsubscribingPushNotificationWithAccount:(NSString *)account url:(NSString *)url deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey completion:(void (^)(NSString *account ,NSString *message, NSInteger errorCode))completion {
  964. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  965. if (tableAccount == nil) {
  966. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  967. } else if ([CCUtility getPassword:account].length == 0) {
  968. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  969. } else if ([CCUtility getCertificateError:account]) {
  970. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  971. }
  972. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  973. #ifdef DEBUG
  974. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  975. #endif
  976. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  977. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  978. [communication setUserAgent:[CCUtility getUserAgent]];
  979. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  980. [communication unsubscribingPushProxy:proxyServer deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  981. completion(account, nil, 0);
  982. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  983. NSString *message;
  984. NSInteger errorCode = response.statusCode;
  985. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  986. errorCode = error.code;
  987. // Error
  988. if (errorCode == 503)
  989. message = NSLocalizedString(@"_server_error_retry_", nil);
  990. else
  991. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  992. completion(account, message, errorCode);
  993. }];
  994. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  995. NSString *message;
  996. NSInteger errorCode = response.statusCode;
  997. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  998. errorCode = error.code;
  999. // Error
  1000. if (errorCode == 503)
  1001. message = NSLocalizedString(@"_server_error_retry_", nil);
  1002. else
  1003. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1004. completion(account, message, errorCode);
  1005. }];
  1006. }
  1007. - (void)getServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSDictionary*jsongParsed, NSString *message, NSInteger errorCode))completion
  1008. {
  1009. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld?format=json", serverUrl, (long)notificationId];
  1010. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  1011. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  1012. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1013. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  1014. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  1015. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  1016. if (error) {
  1017. NSString *message;
  1018. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  1019. NSInteger errorCode = httpResponse.statusCode;
  1020. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1021. errorCode = error.code;
  1022. // Error
  1023. if (errorCode == 503)
  1024. message = NSLocalizedString(@"_server_error_retry_", nil);
  1025. else
  1026. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1027. completion(nil, message, errorCode);
  1028. } else {
  1029. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  1030. completion(jsongParsed, nil, 0);
  1031. }
  1032. }];
  1033. [task resume];
  1034. }
  1035. - (void)deletingServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSString *message, NSInteger errorCode))completion
  1036. {
  1037. // NSData *authData = [[NSString stringWithFormat:@"%@:%@", tableAccount.user, [CCUtility getPassword:tableAccount.account]] dataUsingEncoding:NSUTF8StringEncoding];
  1038. // NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  1039. // Delete
  1040. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld", serverUrl, (long)notificationId];
  1041. // Delete-all
  1042. if (notificationId == 0) {
  1043. URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications", serverUrl];
  1044. }
  1045. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  1046. // [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  1047. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  1048. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1049. [request setHTTPMethod: @"DELETE"];
  1050. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  1051. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  1052. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  1053. if (error) {
  1054. NSString *message;
  1055. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  1056. NSInteger errorCode = httpResponse.statusCode;
  1057. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1058. errorCode = error.code;
  1059. // Error
  1060. if (errorCode == 503)
  1061. message = NSLocalizedString(@"_server_error_retry_", nil);
  1062. else
  1063. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1064. completion(message, errorCode);
  1065. } else {
  1066. completion(nil, 0);
  1067. }
  1068. }];
  1069. [task resume];
  1070. }
  1071. #pragma --------------------------------------------------------------------------------------------
  1072. #pragma mark ===== Manage Mobile Editor OCS API =====
  1073. #pragma --------------------------------------------------------------------------------------------
  1074. - (void)createLinkRichdocumentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  1075. {
  1076. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1077. if (tableAccount == nil) {
  1078. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1079. } else if ([CCUtility getPassword:account].length == 0) {
  1080. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1081. } else if ([CCUtility getCertificateError:account]) {
  1082. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1083. }
  1084. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1085. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1086. [communication setUserAgent:[CCUtility getUserAgent]];
  1087. [communication createLinkRichdocuments:[tableAccount.url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer) {
  1088. completion(account, link, nil, 0);
  1089. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1090. NSString *message;
  1091. NSInteger errorCode = response.statusCode;
  1092. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1093. errorCode = error.code;
  1094. // Error
  1095. if (errorCode == 503)
  1096. message = NSLocalizedString(@"_server_error_retry_", nil);
  1097. else
  1098. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1099. completion(account, nil,message, errorCode);
  1100. }];
  1101. }
  1102. - (void)getTemplatesRichdocumentsWithAccount:(NSString *)account typeTemplate:(NSString *)typeTemplate completion:(void(^)(NSString *account, NSArray *listOfTemplate, NSString *message, NSInteger errorCode))completion
  1103. {
  1104. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1105. if (tableAccount == nil) {
  1106. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1107. } else if ([CCUtility getPassword:account].length == 0) {
  1108. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1109. } else if ([CCUtility getCertificateError:account]) {
  1110. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1111. }
  1112. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1113. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1114. [communication setUserAgent:[CCUtility getUserAgent]];
  1115. [communication getTemplatesRichdocuments:[tableAccount.url stringByAppendingString:@"/"] typeTemplate:typeTemplate onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer) {
  1116. completion(account, listOfTemplate, nil, 0);
  1117. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1118. NSString *message;
  1119. NSInteger errorCode = response.statusCode;
  1120. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1121. errorCode = error.code;
  1122. // Error
  1123. if (errorCode == 503)
  1124. message = NSLocalizedString(@"_server_error_retry_", nil);
  1125. else
  1126. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1127. completion(account, nil, message, errorCode);
  1128. }];
  1129. }
  1130. - (void)createNewRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl templateID:(NSString *)templateID completion:(void(^)(NSString *account, NSString *url, NSString *message, NSInteger errorCode))completion
  1131. {
  1132. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1133. if (tableAccount == nil) {
  1134. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1135. } else if ([CCUtility getPassword:account].length == 0) {
  1136. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1137. } else if ([CCUtility getCertificateError:account]) {
  1138. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1139. }
  1140. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1141. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1142. [communication setUserAgent:[CCUtility getUserAgent]];
  1143. [communication createNewRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileName templateID:templateID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1144. completion(account, url, nil, 0);
  1145. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1146. NSString *message;
  1147. NSInteger errorCode = response.statusCode;
  1148. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1149. errorCode = error.code;
  1150. // Error
  1151. if (errorCode == 503)
  1152. message = NSLocalizedString(@"_server_error_retry_", nil);
  1153. else
  1154. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1155. completion(account, nil, message, errorCode);
  1156. }];
  1157. }
  1158. - (void)createAssetRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  1159. {
  1160. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1161. if (tableAccount == nil) {
  1162. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1163. } else if ([CCUtility getPassword:account].length == 0) {
  1164. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1165. } else if ([CCUtility getCertificateError:account]) {
  1166. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1167. }
  1168. NSString *fileNamePath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:tableAccount.url];
  1169. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1170. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1171. [communication setUserAgent:[CCUtility getUserAgent]];
  1172. [communication createAssetRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1173. completion(account, url, nil, 0);
  1174. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1175. NSString *message;
  1176. NSInteger errorCode = response.statusCode;
  1177. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1178. errorCode = error.code;
  1179. // Error
  1180. if (errorCode == 503)
  1181. message = NSLocalizedString(@"_server_error_retry_", nil);
  1182. else
  1183. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1184. completion(account, nil, message, errorCode);
  1185. }];
  1186. }
  1187. #pragma --------------------------------------------------------------------------------------------
  1188. #pragma mark ===== Full Text Search =====
  1189. #pragma --------------------------------------------------------------------------------------------
  1190. - (void)fullTextSearchWithAccount:(NSString *)account text:(NSString *)text page:(NSInteger)page completion:(void(^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1191. {
  1192. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1193. if (tableAccount == nil) {
  1194. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1195. } else if ([CCUtility getPassword:account].length == 0) {
  1196. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1197. } else if ([CCUtility getCertificateError:account]) {
  1198. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1199. }
  1200. // Create JSON
  1201. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  1202. [dataDic setValue:@"files" forKey:@"providers"];
  1203. [dataDic setValue:text forKey:@"search"];
  1204. [dataDic setValue:[NSNumber numberWithInteger:page] forKey:@"page"];
  1205. [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"size"];
  1206. NSMutableDictionary *options = [NSMutableDictionary new];
  1207. [options setValue:@"" forKey:@"files_within_dir"];
  1208. [options setValue:@"" forKey:@"files_local"];
  1209. [options setValue:@"" forKey:@"files_extension"];
  1210. [dataDic setValue:options forKey:@"options"];
  1211. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  1212. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1213. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1214. [communication setUserAgent:[CCUtility getUserAgent]];
  1215. [communication fullTextSearch:[tableAccount.url stringByAppendingString:@"/"] data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  1216. completion(account, items, nil, 0);
  1217. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1218. NSString *message;
  1219. NSInteger errorCode = response.statusCode;
  1220. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1221. errorCode = error.code;
  1222. // Error
  1223. if (errorCode == 503)
  1224. message = NSLocalizedString(@"_server_error_retry_", nil);
  1225. else
  1226. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1227. completion(account, nil, message, errorCode);
  1228. }];
  1229. }
  1230. #pragma --------------------------------------------------------------------------------------------
  1231. #pragma mark ===== Check remote user =====
  1232. #pragma --------------------------------------------------------------------------------------------
  1233. - (void)checkRemoteUser:(NSString *)account function:(NSString *)function errorCode:(NSInteger)errorCode
  1234. {
  1235. #ifndef EXTENSION
  1236. @synchronized(self) {
  1237. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1238. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:account];
  1239. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1240. NSString *token = [CCUtility getPassword:account];
  1241. if (self.checkRemoteUserInProgress || tableAccount == nil || token == nil) {
  1242. return;
  1243. } else {
  1244. self.checkRemoteUserInProgress = true;
  1245. }
  1246. if (capabilities != nil && capabilities.versionMajor >= k_nextcloud_version_17_0) {
  1247. [[OCNetworking sharedManager] getRemoteWipeStatusWithAccount:account token:token completion:^(NSString *account, BOOL wipe, NSString *message, NSInteger errorCode) {
  1248. if (wipe) {
  1249. [appDelegate deleteAccount:account wipe:true];
  1250. [[NCContentPresenter shared] messageNotification:tableAccount.user description:@"_wipe_account_" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  1251. [[OCNetworking sharedManager] setRemoteWipeCompletitionWithUser:tableAccount.user userID:tableAccount.userID url:tableAccount.url token:token completion:^(NSString *message, NSInteger errorCode) {
  1252. NSLog(@"Wiped");
  1253. }];
  1254. } else {
  1255. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive && [appDelegate.reachability isReachable]) {
  1256. [[NCContentPresenter shared] messageNotification:@"_error_" description:[NSString stringWithFormat:@"During the function request: %@ the server responded with error %ld password re-entry is required", function, (long)errorCode] delay:k_dismissAfterSecond*2 type:messageTypeError errorCode:errorCode];
  1257. [CCUtility setPassword:account password:nil];
  1258. }
  1259. }
  1260. self.checkRemoteUserInProgress = false;
  1261. }];
  1262. } else if ([CCUtility getPassword:account] != nil) {
  1263. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive && [appDelegate.reachability isReachable]) {
  1264. [[NCContentPresenter shared] messageNotification:@"_error_" description:[NSString stringWithFormat:@"During the function request: %@ the server responded with error %ld password re-entry is required", function, (long)errorCode] delay:k_dismissAfterSecond*2 type:messageTypeError errorCode:errorCode];
  1265. [CCUtility setPassword:account password:nil];
  1266. }
  1267. }
  1268. }
  1269. #endif
  1270. }
  1271. - (void)getRemoteWipeStatusWithAccount:(NSString *)account token:(NSString *)token completion:(void(^)(NSString *account, BOOL wipe, NSString *message, NSInteger errorCode))completion
  1272. {
  1273. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1274. if (tableAccount == nil) {
  1275. completion(account, false, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1276. } else if ([CCUtility getPassword:account].length == 0) {
  1277. completion(account, false, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1278. } else if ([CCUtility getCertificateError:account]) {
  1279. completion(account, false, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1280. }
  1281. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1282. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1283. [communication setUserAgent:[CCUtility getUserAgent]];
  1284. [communication getRemoteWipeStatus:tableAccount.url token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, BOOL wipe, NSString *redirectedServer) {
  1285. completion(account, wipe, nil, 0);
  1286. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1287. NSString *message;
  1288. NSInteger errorCode = response.statusCode;
  1289. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1290. errorCode = error.code;
  1291. // Server Unauthorized
  1292. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1293. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get remote wipe status" errorCode:errorCode];
  1294. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1295. [CCUtility setCertificateError:account error:YES];
  1296. }
  1297. // Error
  1298. if (errorCode == 503)
  1299. message = NSLocalizedString(@"_server_error_retry_", nil);
  1300. else
  1301. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1302. completion(account, false, message, errorCode);
  1303. }];
  1304. }
  1305. - (void)setRemoteWipeCompletitionWithUser:(NSString *)user userID:(NSString *)userID url:(NSString *)url token:(NSString *)token completion:(void(^)(NSString *message, NSInteger errorCode))completion
  1306. {
  1307. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1308. [communication setCredentialsWithUser:user andUserID:userID andPassword:token];
  1309. [communication setUserAgent:[CCUtility getUserAgent]];
  1310. [communication setRemoteWipeCompletition:url token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1311. completion(nil, 0);
  1312. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1313. NSString *message;
  1314. NSInteger errorCode = response.statusCode;
  1315. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1316. errorCode = error.code;
  1317. // Error
  1318. if (errorCode == 503)
  1319. message = NSLocalizedString(@"_server_error_retry_", nil);
  1320. else
  1321. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1322. completion(message, errorCode);
  1323. }];
  1324. }
  1325. #pragma --------------------------------------------------------------------------------------------
  1326. #pragma mark ===== Trash =====
  1327. #pragma --------------------------------------------------------------------------------------------
  1328. - (void)listingTrashWithAccount:(NSString *)account path:(NSString *)path serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1329. {
  1330. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1331. if (tableAccount == nil) {
  1332. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1333. } else if ([CCUtility getPassword:account].length == 0) {
  1334. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1335. } else if ([CCUtility getCertificateError:account]) {
  1336. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1337. }
  1338. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1339. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1340. [communication setUserAgent:[CCUtility getUserAgent]];
  1341. [communication listingTrash:[serverUrl stringByAppendingString:path] depth:depth onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  1342. // Check items > 0
  1343. if ([items count] == 0) {
  1344. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  1345. completion(account, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  1346. } else {
  1347. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1348. NSMutableArray *listTrash = [NSMutableArray new];
  1349. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  1350. if ([items count] > 1) {
  1351. for (NSUInteger i=1; i < [items count]; i++) {
  1352. OCFileDto *itemDto = [items objectAtIndex:i];
  1353. tableTrash *trash = [tableTrash new];
  1354. trash.account = account;
  1355. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  1356. trash.directory = itemDto.isDirectory;
  1357. trash.fileId = itemDto.fileId;
  1358. trash.fileName = itemDto.fileName;
  1359. NSArray *array = [itemDto.filePath componentsSeparatedByString:path];
  1360. long len = [[array objectAtIndex:0] length];
  1361. trash.filePath = [itemDto.filePath substringFromIndex:len];
  1362. trash.hasPreview = itemDto.hasPreview;
  1363. trash.size = itemDto.size;
  1364. trash.trashbinFileName = itemDto.trashbinFileName;
  1365. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  1366. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  1367. [CCUtility insertTypeFileIconName:trash.trashbinFileName metadata:(tableMetadata *)trash];
  1368. [listTrash addObject:trash];
  1369. }
  1370. }
  1371. dispatch_async(dispatch_get_main_queue(), ^{
  1372. completion(account, listTrash, nil, 0);
  1373. });
  1374. });
  1375. }
  1376. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1377. NSString *message;
  1378. NSInteger errorCode = response.statusCode;
  1379. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1380. errorCode = error.code;
  1381. // Error
  1382. if (errorCode == 503)
  1383. message = NSLocalizedString(@"_server_error_retry_", nil);
  1384. else
  1385. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1386. completion(account, nil,message, errorCode);
  1387. }];
  1388. }
  1389. - (void)emptyTrashWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1390. {
  1391. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1392. if (tableAccount == nil) {
  1393. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1394. } else if ([CCUtility getPassword:account].length == 0) {
  1395. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1396. } else if ([CCUtility getCertificateError:account]) {
  1397. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1398. }
  1399. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", tableAccount.url, k_dav, tableAccount.userID];
  1400. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1401. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1402. [communication setUserAgent:[CCUtility getUserAgent]];
  1403. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1404. completion(account, nil, 0);
  1405. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1406. NSString *message;
  1407. NSInteger errorCode = response.statusCode;
  1408. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1409. errorCode = error.code;
  1410. // Error
  1411. if (errorCode == 503)
  1412. message = NSLocalizedString(@"_server_error_retry_", nil);
  1413. else
  1414. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1415. completion(account, message, errorCode);
  1416. }];
  1417. }
  1418. #pragma --------------------------------------------------------------------------------------------
  1419. #pragma mark ===== Comments =====
  1420. #pragma --------------------------------------------------------------------------------------------
  1421. - (void)getCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1422. {
  1423. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1424. if (tableAccount == nil) {
  1425. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1426. } else if ([CCUtility getPassword:account].length == 0) {
  1427. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1428. } else if ([CCUtility getCertificateError:account]) {
  1429. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1430. }
  1431. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1432. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1433. [communication setUserAgent:[CCUtility getUserAgent]];
  1434. [communication getComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *list, NSString *redirectedServer) {
  1435. completion(account, list, nil, 0);
  1436. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1437. NSString *message;
  1438. NSInteger errorCode = response.statusCode;
  1439. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1440. errorCode = error.code;
  1441. // Error
  1442. if (errorCode == 503)
  1443. message = NSLocalizedString(@"_server_error_retry_", nil);
  1444. else
  1445. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1446. completion(account, nil,message, errorCode);
  1447. }];
  1448. }
  1449. - (void)putCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1450. {
  1451. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1452. if (tableAccount == nil) {
  1453. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1454. } else if ([CCUtility getPassword:account].length == 0) {
  1455. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1456. } else if ([CCUtility getCertificateError:account]) {
  1457. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1458. }
  1459. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1460. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1461. [communication setUserAgent:[CCUtility getUserAgent]];
  1462. [communication putComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1463. completion(account, nil, 0);
  1464. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1465. NSString *message;
  1466. NSInteger errorCode = response.statusCode;
  1467. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1468. errorCode = error.code;
  1469. // Error
  1470. if (errorCode == 503)
  1471. message = NSLocalizedString(@"_server_error_retry_", nil);
  1472. else
  1473. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1474. completion(account, message, errorCode);
  1475. }];
  1476. }
  1477. - (void)updateCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1478. {
  1479. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1480. if (tableAccount == nil) {
  1481. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1482. } else if ([CCUtility getPassword:account].length == 0) {
  1483. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1484. } else if ([CCUtility getCertificateError:account]) {
  1485. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1486. }
  1487. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1488. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1489. [communication setUserAgent:[CCUtility getUserAgent]];
  1490. [communication updateComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1491. completion(account, nil, 0);
  1492. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1493. NSString *message;
  1494. NSInteger errorCode = response.statusCode;
  1495. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1496. errorCode = error.code;
  1497. // Error
  1498. if (errorCode == 503)
  1499. message = NSLocalizedString(@"_server_error_retry_", nil);
  1500. else
  1501. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1502. completion(account, message, errorCode);
  1503. }];
  1504. }
  1505. - (void)readMarkCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1506. {
  1507. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1508. if (tableAccount == nil) {
  1509. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1510. } else if ([CCUtility getPassword:account].length == 0) {
  1511. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1512. } else if ([CCUtility getCertificateError:account]) {
  1513. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1514. }
  1515. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1516. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1517. [communication setUserAgent:[CCUtility getUserAgent]];
  1518. [communication readMarkComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1519. completion(account, nil, 0);
  1520. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1521. NSString *message;
  1522. NSInteger errorCode = response.statusCode;
  1523. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1524. errorCode = error.code;
  1525. // Error
  1526. if (errorCode == 503)
  1527. message = NSLocalizedString(@"_server_error_retry_", nil);
  1528. else
  1529. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1530. completion(account, message, errorCode);
  1531. }];
  1532. }
  1533. - (void)deleteCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1534. {
  1535. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1536. if (tableAccount == nil) {
  1537. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1538. } else if ([CCUtility getPassword:account].length == 0) {
  1539. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1540. } else if ([CCUtility getCertificateError:account]) {
  1541. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1542. }
  1543. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1544. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1545. [communication setUserAgent:[CCUtility getUserAgent]];
  1546. [communication deleteComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1547. completion(account, nil, 0);
  1548. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1549. NSString *message;
  1550. NSInteger errorCode = response.statusCode;
  1551. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1552. errorCode = error.code;
  1553. // Error
  1554. if (errorCode == 503)
  1555. message = NSLocalizedString(@"_server_error_retry_", nil);
  1556. else
  1557. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1558. completion(account, message, errorCode);
  1559. }];
  1560. }
  1561. #pragma --------------------------------------------------------------------------------------------
  1562. #pragma mark ===== Third Parts =====
  1563. #pragma --------------------------------------------------------------------------------------------
  1564. - (void)getHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  1565. {
  1566. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1567. if (tableAccount == nil) {
  1568. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1569. } else if ([CCUtility getPassword:account].length == 0) {
  1570. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1571. } else if ([CCUtility getCertificateError:account]) {
  1572. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1573. }
  1574. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  1575. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1576. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1577. [communication setUserAgent:[CCUtility getUserAgent]];
  1578. [communication getHCUserProfile:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1579. completion(account, userProfile, nil, 0);
  1580. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1581. NSString *message;
  1582. NSInteger errorCode = response.statusCode;
  1583. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1584. errorCode = error.code;
  1585. // Server Unauthorized
  1586. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1587. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get user profile" errorCode:errorCode];
  1588. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1589. [CCUtility setCertificateError:account error:YES];
  1590. }
  1591. // Error
  1592. if (errorCode == 503)
  1593. message = NSLocalizedString(@"_server_error_retry_", nil);
  1594. else
  1595. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1596. completion(account, nil,message, errorCode);
  1597. }];
  1598. }
  1599. - (void)putHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl address:(NSString *)address businesssize:(NSString *)businesssize businesstype:(NSString *)businesstype city:(NSString *)city company:(NSString *)company country:(NSString *)country displayname:(NSString *)displayname email:(NSString *)email phone:(NSString *)phone role_:(NSString *)role_ twitter:(NSString *)twitter website:(NSString *)website zip:(NSString *)zip completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1600. {
  1601. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1602. if (tableAccount == nil) {
  1603. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1604. } else if ([CCUtility getPassword:account].length == 0) {
  1605. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1606. } else if ([CCUtility getCertificateError:account]) {
  1607. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1608. }
  1609. // Create JSON
  1610. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  1611. if (address) [dataDic setValue:address forKey:@"address"];
  1612. if (businesssize) {
  1613. if ([businesssize isEqualToString:@"1-4"]) { [dataDic setValue:[NSNumber numberWithInt:1] forKey:@"businesssize"]; }
  1614. else if ([businesssize isEqualToString:@"5-9"]) { [dataDic setValue:[NSNumber numberWithInt:5] forKey:@"businesssize"]; }
  1615. else if ([businesssize isEqualToString:@"10-19"]) { [dataDic setValue:[NSNumber numberWithInt:10] forKey:@"businesssize"]; }
  1616. else if ([businesssize isEqualToString:@"20-49"]) { [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"businesssize"]; }
  1617. else if ([businesssize isEqualToString:@"50-99"]) { [dataDic setValue:[NSNumber numberWithInt:50] forKey:@"businesssize"]; }
  1618. else if ([businesssize isEqualToString:@"100-249"]) { [dataDic setValue:[NSNumber numberWithInt:100] forKey:@"businesssize"]; }
  1619. else if ([businesssize isEqualToString:@"250-499"]) { [dataDic setValue:[NSNumber numberWithInt:250] forKey:@"businesssize"]; }
  1620. else if ([businesssize isEqualToString:@"500-999"]) { [dataDic setValue:[NSNumber numberWithInt:500] forKey:@"businesssize"]; }
  1621. else if ([businesssize isEqualToString:@"1000+"]) { [dataDic setValue:[NSNumber numberWithInt:1000] forKey:@"businesssize"]; }
  1622. }
  1623. if (businesstype) [dataDic setValue:businesstype forKey:@"businesstype"];
  1624. if (city) [dataDic setValue:city forKey:@"city"];
  1625. if (company) [dataDic setValue:company forKey:@"company"];
  1626. if (country) [dataDic setValue:country forKey:@"country"];
  1627. if (displayname) [dataDic setValue:displayname forKey:@"displayname"];
  1628. if (email) [dataDic setValue:email forKey:@"email"];
  1629. if (phone) [dataDic setValue:phone forKey:@"phone"];
  1630. if (role_) [dataDic setValue:role_ forKey:@"role"];
  1631. if (twitter) [dataDic setValue:twitter forKey:@"twitter"];
  1632. if (website) [dataDic setValue:website forKey:@"website"];
  1633. if (zip) [dataDic setValue:zip forKey:@"zip"];
  1634. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  1635. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  1636. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1637. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1638. [communication setUserAgent:[CCUtility getUserAgent]];
  1639. [communication putHCUserProfile:serverPath data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1640. completion(account, nil, 0);
  1641. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1642. NSString *message;
  1643. NSInteger errorCode = response.statusCode;
  1644. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1645. errorCode = error.code;
  1646. // Server Unauthorized
  1647. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1648. [[OCNetworking sharedManager] checkRemoteUser:account function:@"put user profile" errorCode:errorCode];
  1649. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1650. [CCUtility setCertificateError:account error:YES];
  1651. }
  1652. // Error
  1653. if (errorCode == 503)
  1654. message = NSLocalizedString(@"_server_error_retry_", nil);
  1655. else
  1656. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1657. completion(account, message, errorCode);
  1658. }];
  1659. }
  1660. - (void)getHCFeaturesWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, HCFeatures *features, NSString *message, NSInteger errorCode))completion
  1661. {
  1662. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1663. if (tableAccount == nil) {
  1664. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1665. } else if ([CCUtility getPassword:account].length == 0) {
  1666. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1667. } else if ([CCUtility getCertificateError:account]) {
  1668. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1669. }
  1670. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/features/%@", serverUrl, tableAccount.userID];
  1671. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1672. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1673. [communication setUserAgent:[CCUtility getUserAgent]];
  1674. [communication getHCFeatures:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer) {
  1675. completion(account, features, nil, 0);
  1676. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1677. NSString *message;
  1678. NSInteger errorCode = response.statusCode;
  1679. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1680. errorCode = error.code;
  1681. // Server Unauthorized
  1682. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1683. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get features" errorCode:errorCode];
  1684. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1685. [CCUtility setCertificateError:account error:YES];
  1686. }
  1687. // Error
  1688. if (errorCode == 503)
  1689. message = NSLocalizedString(@"_server_error_retry_", nil);
  1690. else
  1691. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1692. completion(account, nil,message, errorCode);
  1693. }];
  1694. }
  1695. /*
  1696. - (void)middlewarePing
  1697. {
  1698. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1699. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1700. [communication setUserAgent:[CCUtility getUserAgent]];
  1701. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1702. [self complete];
  1703. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1704. NSInteger errorCode = response.statusCode;
  1705. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1706. errorCode = error.code;
  1707. // Error
  1708. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  1709. if (errorCode == 503)
  1710. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  1711. else
  1712. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1713. }
  1714. // Request trusted certificated
  1715. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1716. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1717. [self complete];
  1718. }];
  1719. }
  1720. */
  1721. #pragma --------------------------------------------------------------------------------------------
  1722. #pragma mark ===== didReceiveChallenge =====
  1723. #pragma --------------------------------------------------------------------------------------------
  1724. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  1725. {
  1726. // The pinnning check
  1727. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1728. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1729. } else {
  1730. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1731. }
  1732. }
  1733. @end
  1734. #pragma --------------------------------------------------------------------------------------------
  1735. #pragma mark ===== OCURLSessionManager =====
  1736. #pragma --------------------------------------------------------------------------------------------
  1737. @implementation OCURLSessionManager
  1738. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1739. {
  1740. // The pinnning check
  1741. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1742. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1743. } else {
  1744. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1745. }
  1746. }
  1747. @end