OCNetworking.m 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  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. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  411. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  412. [communication setUserAgent:[CCUtility getUserAgent]];
  413. [communication readFolder:serverUrl depth:depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  414. // Check items > 0
  415. if ([items count] == 0) {
  416. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  417. completion(account, nil, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  418. } else {
  419. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  420. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  421. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  422. // directory [0]
  423. OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  424. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  425. NSMutableArray *metadatas = [NSMutableArray new];
  426. tableMetadata *metadataFolder = [tableMetadata new];
  427. NSString *serverUrlFolder;
  428. // Metadata . (self Folder)
  429. if ([serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:url]]) {
  430. // root folder
  431. serverUrlFolder = k_serverUrl_root;
  432. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:@"." serverUrl:serverUrlFolder account:account isFolderEncrypted:isFolderEncrypted];
  433. } else {
  434. serverUrlFolder = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  435. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:[serverUrl lastPathComponent] serverUrl:serverUrlFolder account:account isFolderEncrypted:isFolderEncrypted];
  436. }
  437. // Add metadata folder
  438. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDtoFolder.isEncrypted favorite:itemDtoFolder.isFavorite ocId:itemDtoFolder.ocId permissions:itemDtoFolder.permissions serverUrl:serverUrl richWorkspace:nil account:account];
  439. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  440. NSString *first = [(OCFileDto*)a fileName];
  441. NSString *second = [(OCFileDto*)b fileName];
  442. return [[first lowercaseString] compare:[second lowercaseString]];
  443. }];
  444. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  445. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  446. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  447. // Skip hidden files
  448. if (fileName.length > 0) {
  449. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  450. continue;
  451. } else {
  452. continue;
  453. }
  454. if (itemDto.isDirectory) {
  455. (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];
  456. }
  457. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl account:account isFolderEncrypted:isFolderEncrypted]];
  458. }
  459. dispatch_async(dispatch_get_main_queue(), ^{
  460. completion(account, metadatas, metadataFolder, nil, 0);
  461. });
  462. });
  463. }
  464. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  465. NSString *message;
  466. NSInteger errorCode = response.statusCode;
  467. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  468. errorCode = error.code;
  469. // Server Unauthorized
  470. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  471. [[OCNetworking sharedManager] checkRemoteUser:account function:@"read file or folder" errorCode:errorCode];
  472. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  473. [CCUtility setCertificateError:account error:YES];
  474. }
  475. // Error
  476. if (errorCode == 503)
  477. message = NSLocalizedString(@"_server_error_retry_", nil);
  478. else
  479. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  480. completion(account, nil, nil, message, errorCode);
  481. }];
  482. }
  483. #pragma --------------------------------------------------------------------------------------------
  484. #pragma mark ===== Share =====
  485. #pragma --------------------------------------------------------------------------------------------
  486. - (void)readShareWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  487. {
  488. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  489. if (tableAccount == nil) {
  490. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  491. } else if ([CCUtility getPassword:account].length == 0) {
  492. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  493. } else if ([CCUtility getCertificateError:account]) {
  494. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  495. }
  496. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  497. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  498. [communication setUserAgent:[CCUtility getUserAgent]];
  499. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  500. completion(account, items, nil, 0);
  501. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  502. NSString *message;
  503. NSInteger errorCode = response.statusCode;
  504. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  505. errorCode = error.code;
  506. // Error
  507. if (errorCode == 503) {
  508. message = NSLocalizedString(@"_server_error_retry_", nil);
  509. } else {
  510. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  511. }
  512. completion(account, nil, message, errorCode);
  513. }];
  514. }
  515. - (void)readShareWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  516. {
  517. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  518. if (tableAccount == nil) {
  519. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  520. } else if ([CCUtility getPassword:account].length == 0) {
  521. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  522. } else if ([CCUtility getCertificateError:account]) {
  523. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  524. }
  525. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  526. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  527. [communication setUserAgent:[CCUtility getUserAgent]];
  528. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] andPath:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  529. completion(account, listOfShared, nil, 0);
  530. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  531. NSString *message;
  532. NSInteger errorCode = response.statusCode;
  533. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  534. errorCode = error.code;
  535. // Error
  536. if (errorCode == 503) {
  537. message = NSLocalizedString(@"_server_error_retry_", nil);
  538. } else {
  539. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  540. }
  541. completion(account, nil, message, errorCode);
  542. }];
  543. }
  544. - (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
  545. {
  546. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  547. if (tableAccount == nil) {
  548. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  549. } else if ([CCUtility getPassword:account].length == 0) {
  550. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  551. } else if ([CCUtility getCertificateError:account]) {
  552. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  553. }
  554. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  555. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  556. [communication setUserAgent:[CCUtility getUserAgent]];
  557. [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) {
  558. completion(account, nil, 0);
  559. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  560. NSString *message;
  561. NSInteger errorCode = response.statusCode;
  562. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  563. errorCode = error.code;
  564. // Error
  565. if (errorCode == 503) {
  566. message = NSLocalizedString(@"_server_error_retry_", nil);
  567. } else {
  568. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  569. }
  570. completion(account, message, errorCode);
  571. }];
  572. }
  573. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  574. - (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
  575. {
  576. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  577. if (tableAccount == nil) {
  578. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  579. } else if ([CCUtility getPassword:account].length == 0) {
  580. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  581. } else if ([CCUtility getCertificateError:account]) {
  582. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  583. }
  584. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  585. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  586. [communication setUserAgent:[CCUtility getUserAgent]];
  587. [communication shareWith:userOrGroup shareeType:shareeType inServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPermissions:permission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  588. completion(account, nil, 0);
  589. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  590. NSString *message;
  591. NSInteger errorCode = response.statusCode;
  592. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  593. errorCode = error.code;
  594. // Error
  595. if (errorCode == 503) {
  596. message = NSLocalizedString(@"_server_error_retry_", nil);
  597. } else {
  598. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  599. }
  600. completion(account, message, errorCode);
  601. }];
  602. }
  603. - (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
  604. {
  605. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  606. if (tableAccount == nil) {
  607. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  608. } else if ([CCUtility getPassword:account].length == 0) {
  609. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  610. } else if ([CCUtility getCertificateError:account]) {
  611. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  612. }
  613. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  614. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  615. [communication setUserAgent:[CCUtility getUserAgent]];
  616. [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) {
  617. completion(account, nil, 0);
  618. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  619. NSString *message;
  620. NSInteger errorCode = response.statusCode;
  621. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  622. errorCode = error.code;
  623. // Error
  624. if (errorCode == 503) {
  625. message = NSLocalizedString(@"_server_error_retry_", nil);
  626. } else {
  627. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  628. }
  629. completion(account, message, errorCode);
  630. }];
  631. }
  632. - (void)unshareAccount:(NSString *)account shareID:(NSInteger)shareID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  633. {
  634. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  635. if (tableAccount == nil) {
  636. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  637. } else if ([CCUtility getPassword:account].length == 0) {
  638. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  639. } else if ([CCUtility getCertificateError:account]) {
  640. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  641. }
  642. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  643. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  644. [communication setUserAgent:[CCUtility getUserAgent]];
  645. [communication unShareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andIdRemoteShared:shareID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  646. completion(account, nil, 0);
  647. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  648. NSString *message;
  649. NSInteger errorCode = response.statusCode;
  650. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  651. errorCode = error.code;
  652. // Error
  653. if (errorCode == 503) {
  654. message = NSLocalizedString(@"_server_error_retry_", nil);
  655. } else {
  656. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  657. }
  658. completion(account, message, errorCode);
  659. }];
  660. }
  661. - (void)getUserGroupWithAccount:(NSString *)account searchString:(NSString *)searchString completion:(void (^)(NSString *account, NSArray *item, NSString *message, NSInteger errorCode))completion
  662. {
  663. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  664. if (tableAccount == nil) {
  665. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  666. } else if ([CCUtility getPassword:account].length == 0) {
  667. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  668. } else if ([CCUtility getCertificateError:account]) {
  669. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  670. }
  671. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  672. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  673. [communication setUserAgent:[CCUtility getUserAgent]];
  674. [communication searchUsersAndGroupsWith:searchString forPage:1 with:50 ofServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  675. completion(account, itemList, nil, 0);
  676. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  677. NSString *message;
  678. NSInteger errorCode = response.statusCode;
  679. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  680. errorCode = error.code;
  681. // Error
  682. if (errorCode == 503) {
  683. message = NSLocalizedString(@"_server_error_retry_", nil);
  684. } else {
  685. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  686. }
  687. completion(account, nil, message, errorCode);
  688. }];
  689. }
  690. #pragma --------------------------------------------------------------------------------------------
  691. #pragma mark ===== VAR =====
  692. #pragma --------------------------------------------------------------------------------------------
  693. - (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
  694. {
  695. BOOL previews = false;
  696. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  697. if (tableAccount == nil) {
  698. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  699. } else if ([CCUtility getPassword:account].length == 0) {
  700. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  701. } else if ([CCUtility getCertificateError:account]) {
  702. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  703. }
  704. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:account];
  705. if (capabilities != nil && capabilities.versionMajor >= k_nextcloud_version_15_0) {
  706. previews = true;
  707. }
  708. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  709. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  710. [communication setUserAgent:[CCUtility getUserAgent]];
  711. [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) {
  712. completion(account, listOfActivity, nil, 0);
  713. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  714. NSString *message;
  715. NSInteger errorCode = response.statusCode;
  716. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  717. errorCode = error.code;
  718. // Server Unauthorized
  719. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  720. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get activity" errorCode:errorCode];
  721. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  722. [CCUtility setCertificateError:account error:YES];
  723. }
  724. // Error
  725. if (errorCode == 503)
  726. message = NSLocalizedString(@"_server_error_retry_", nil);
  727. else
  728. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  729. completion(account, nil, message, errorCode);
  730. }];
  731. }
  732. - (void)getExternalSitesWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfExternalSites, NSString *message, NSInteger errorCode))completion
  733. {
  734. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  735. if (tableAccount == nil) {
  736. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  737. } else if ([CCUtility getPassword:account].length == 0) {
  738. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  739. } else if ([CCUtility getCertificateError:account]) {
  740. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  741. }
  742. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  743. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  744. [communication setUserAgent:[CCUtility getUserAgent]];
  745. [communication getExternalSitesServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  746. completion(account, listOfExternalSites, nil, 0);
  747. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  748. NSString *message;
  749. NSInteger errorCode = response.statusCode;
  750. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  751. errorCode = error.code;
  752. // Server Unauthorized
  753. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  754. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get external sites" errorCode:errorCode];
  755. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  756. [CCUtility setCertificateError:account error:YES];
  757. }
  758. // Error
  759. if (errorCode == 503) {
  760. message = NSLocalizedString(@"_server_error_retry_", nil);
  761. } else {
  762. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  763. }
  764. completion(account, nil, message, errorCode);
  765. }];
  766. }
  767. - (void)getNotificationWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  768. {
  769. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  770. if (tableAccount == nil) {
  771. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  772. } else if ([CCUtility getPassword:account].length == 0) {
  773. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  774. } else if ([CCUtility getCertificateError:account]) {
  775. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  776. }
  777. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  778. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  779. [communication setUserAgent:[CCUtility getUserAgent]];
  780. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  781. completion(account, listOfNotifications, nil, 0);
  782. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  783. NSString *message;
  784. NSInteger errorCode = response.statusCode;
  785. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  786. errorCode = error.code;
  787. // Server Unauthorized
  788. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  789. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get notification" errorCode:errorCode];
  790. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  791. [CCUtility setCertificateError:account error:YES];
  792. }
  793. // Error
  794. if (errorCode == 503) {
  795. message = NSLocalizedString(@"_server_error_retry_", nil);
  796. } else {
  797. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  798. }
  799. completion(account, nil, message, errorCode);
  800. }];
  801. }
  802. - (void)setNotificationWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  803. {
  804. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  805. if (tableAccount == nil) {
  806. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  807. } else if ([CCUtility getPassword:account].length == 0) {
  808. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  809. } else if ([CCUtility getCertificateError:account]) {
  810. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  811. }
  812. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  813. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  814. [communication setUserAgent:[CCUtility getUserAgent]];
  815. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  816. completion(account, nil, 0);
  817. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  818. NSString *message;
  819. NSInteger errorCode = response.statusCode;
  820. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  821. errorCode = error.code;
  822. // Server Unauthorized
  823. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  824. [[OCNetworking sharedManager] checkRemoteUser:account function:@"set notification" errorCode:errorCode];
  825. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  826. [CCUtility setCertificateError:account error:YES];
  827. }
  828. // Error
  829. if (errorCode == 503) {
  830. message = NSLocalizedString(@"_server_error_retry_", nil);
  831. } else {
  832. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  833. }
  834. completion(account, message, errorCode);
  835. }];
  836. }
  837. - (void)getCapabilitiesWithAccount:(NSString *)account completion:(void (^)(NSString *account, OCCapabilities *capabilities, NSString *message, NSInteger errorCode))completion
  838. {
  839. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  840. if (tableAccount == nil) {
  841. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  842. } else if ([CCUtility getPassword:account].length == 0) {
  843. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  844. } else if ([CCUtility getCertificateError:account]) {
  845. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  846. }
  847. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  848. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  849. [communication setUserAgent:[CCUtility getUserAgent]];
  850. [communication getCapabilitiesOfServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  851. completion(account, capabilities, nil, 0);
  852. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  853. NSString *message;
  854. NSInteger errorCode = response.statusCode;
  855. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  856. errorCode = error.code;
  857. // Server Unauthorized
  858. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  859. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get capabilities" errorCode:errorCode];
  860. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  861. [CCUtility setCertificateError:account error:YES];
  862. }
  863. // Error
  864. if (errorCode == 503) {
  865. message = NSLocalizedString(@"_server_error_retry_", nil);
  866. } else {
  867. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  868. }
  869. completion(account, nil, message, errorCode);
  870. }];
  871. }
  872. - (void)getUserProfileWithAccount:(NSString *)account completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  873. {
  874. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  875. if (tableAccount == nil) {
  876. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  877. } else if ([CCUtility getPassword:account].length == 0) {
  878. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  879. } else if ([CCUtility getCertificateError:account]) {
  880. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  881. }
  882. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  883. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  884. [communication setUserAgent:[CCUtility getUserAgent]];
  885. [communication getUserProfileServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  886. completion(account, userProfile, nil, 0);
  887. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  888. NSString *message;
  889. NSInteger errorCode = response.statusCode;
  890. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  891. errorCode = error.code;
  892. // Server Unauthorized
  893. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  894. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get user profile" errorCode:errorCode];
  895. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  896. [CCUtility setCertificateError:account error:YES];
  897. }
  898. // Error
  899. if (errorCode == 503) {
  900. message = NSLocalizedString(@"_server_error_retry_", nil);
  901. } else {
  902. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  903. }
  904. completion(account, nil, message, errorCode);
  905. }];
  906. }
  907. #pragma --------------------------------------------------------------------------------------------
  908. #pragma mark ===== Push Notification =====
  909. #pragma --------------------------------------------------------------------------------------------
  910. - (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
  911. {
  912. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  913. if (tableAccount == nil) {
  914. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  915. } else if ([CCUtility getPassword:account].length == 0) {
  916. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  917. } else if ([CCUtility getCertificateError:account]) {
  918. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  919. }
  920. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  921. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  922. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  923. #ifdef DEBUG
  924. // proxyServerPath = @"http://127.0.0.1:8088";
  925. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  926. #endif
  927. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  928. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  929. [communication setUserAgent:[CCUtility getUserAgent]];
  930. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: proxyServerPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  931. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  932. signature = [CCUtility URLEncodeStringFromString:signature];
  933. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  934. [communication subscribingPushProxy:proxyServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  935. completion(account, deviceIdentifier, signature, publicKey, nil, 0);
  936. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  937. NSString *message;
  938. NSInteger errorCode = response.statusCode;
  939. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  940. errorCode = error.code;
  941. // Error
  942. if (errorCode == 503)
  943. message = NSLocalizedString(@"_server_error_retry_", nil);
  944. else
  945. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  946. completion(account, nil, nil, nil, message, errorCode);
  947. }];
  948. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  949. NSString *message;
  950. NSInteger errorCode = response.statusCode;
  951. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  952. errorCode = error.code;
  953. // Error
  954. if (errorCode == 503)
  955. message = NSLocalizedString(@"_server_error_retry_", nil);
  956. else
  957. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  958. completion(account, nil, nil, nil, message, errorCode);
  959. }];
  960. }
  961. - (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 {
  962. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  963. if (tableAccount == nil) {
  964. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  965. } else if ([CCUtility getPassword:account].length == 0) {
  966. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  967. } else if ([CCUtility getCertificateError:account]) {
  968. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  969. }
  970. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  971. #ifdef DEBUG
  972. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  973. #endif
  974. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  975. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  976. [communication setUserAgent:[CCUtility getUserAgent]];
  977. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  978. [communication unsubscribingPushProxy:proxyServer deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  979. completion(account, nil, 0);
  980. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  981. NSString *message;
  982. NSInteger errorCode = response.statusCode;
  983. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  984. errorCode = error.code;
  985. // Error
  986. if (errorCode == 503)
  987. message = NSLocalizedString(@"_server_error_retry_", nil);
  988. else
  989. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  990. completion(account, message, errorCode);
  991. }];
  992. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  993. NSString *message;
  994. NSInteger errorCode = response.statusCode;
  995. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  996. errorCode = error.code;
  997. // Error
  998. if (errorCode == 503)
  999. message = NSLocalizedString(@"_server_error_retry_", nil);
  1000. else
  1001. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1002. completion(account, message, errorCode);
  1003. }];
  1004. }
  1005. - (void)getServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSDictionary*jsongParsed, NSString *message, NSInteger errorCode))completion
  1006. {
  1007. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld?format=json", serverUrl, (long)notificationId];
  1008. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  1009. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  1010. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1011. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  1012. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  1013. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  1014. if (error) {
  1015. NSString *message;
  1016. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  1017. NSInteger errorCode = httpResponse.statusCode;
  1018. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1019. errorCode = error.code;
  1020. // Error
  1021. if (errorCode == 503)
  1022. message = NSLocalizedString(@"_server_error_retry_", nil);
  1023. else
  1024. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1025. completion(nil, message, errorCode);
  1026. } else {
  1027. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  1028. completion(jsongParsed, nil, 0);
  1029. }
  1030. }];
  1031. [task resume];
  1032. }
  1033. - (void)deletingServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSString *message, NSInteger errorCode))completion
  1034. {
  1035. // NSData *authData = [[NSString stringWithFormat:@"%@:%@", tableAccount.user, [CCUtility getPassword:tableAccount.account]] dataUsingEncoding:NSUTF8StringEncoding];
  1036. // NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  1037. // Delete
  1038. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld", serverUrl, (long)notificationId];
  1039. // Delete-all
  1040. if (notificationId == 0) {
  1041. URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications", serverUrl];
  1042. }
  1043. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  1044. // [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  1045. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  1046. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1047. [request setHTTPMethod: @"DELETE"];
  1048. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  1049. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  1050. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  1051. if (error) {
  1052. NSString *message;
  1053. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  1054. NSInteger errorCode = httpResponse.statusCode;
  1055. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1056. errorCode = error.code;
  1057. // Error
  1058. if (errorCode == 503)
  1059. message = NSLocalizedString(@"_server_error_retry_", nil);
  1060. else
  1061. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1062. completion(message, errorCode);
  1063. } else {
  1064. completion(nil, 0);
  1065. }
  1066. }];
  1067. [task resume];
  1068. }
  1069. #pragma --------------------------------------------------------------------------------------------
  1070. #pragma mark ===== Manage Mobile Editor OCS API =====
  1071. #pragma --------------------------------------------------------------------------------------------
  1072. - (void)createLinkRichdocumentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  1073. {
  1074. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1075. if (tableAccount == nil) {
  1076. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1077. } else if ([CCUtility getPassword:account].length == 0) {
  1078. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1079. } else if ([CCUtility getCertificateError:account]) {
  1080. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1081. }
  1082. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1083. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1084. [communication setUserAgent:[CCUtility getUserAgent]];
  1085. [communication createLinkRichdocuments:[tableAccount.url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer) {
  1086. completion(account, link, nil, 0);
  1087. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1088. NSString *message;
  1089. NSInteger errorCode = response.statusCode;
  1090. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1091. errorCode = error.code;
  1092. // Error
  1093. if (errorCode == 503)
  1094. message = NSLocalizedString(@"_server_error_retry_", nil);
  1095. else
  1096. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1097. completion(account, nil,message, errorCode);
  1098. }];
  1099. }
  1100. - (void)getTemplatesRichdocumentsWithAccount:(NSString *)account typeTemplate:(NSString *)typeTemplate completion:(void(^)(NSString *account, NSArray *listOfTemplate, NSString *message, NSInteger errorCode))completion
  1101. {
  1102. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1103. if (tableAccount == nil) {
  1104. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1105. } else if ([CCUtility getPassword:account].length == 0) {
  1106. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1107. } else if ([CCUtility getCertificateError:account]) {
  1108. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1109. }
  1110. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1111. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1112. [communication setUserAgent:[CCUtility getUserAgent]];
  1113. [communication getTemplatesRichdocuments:[tableAccount.url stringByAppendingString:@"/"] typeTemplate:typeTemplate onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer) {
  1114. completion(account, listOfTemplate, nil, 0);
  1115. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1116. NSString *message;
  1117. NSInteger errorCode = response.statusCode;
  1118. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1119. errorCode = error.code;
  1120. // Error
  1121. if (errorCode == 503)
  1122. message = NSLocalizedString(@"_server_error_retry_", nil);
  1123. else
  1124. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1125. completion(account, nil, message, errorCode);
  1126. }];
  1127. }
  1128. - (void)createNewRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl templateID:(NSString *)templateID completion:(void(^)(NSString *account, NSString *url, NSString *message, NSInteger errorCode))completion
  1129. {
  1130. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1131. if (tableAccount == nil) {
  1132. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1133. } else if ([CCUtility getPassword:account].length == 0) {
  1134. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1135. } else if ([CCUtility getCertificateError:account]) {
  1136. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1137. }
  1138. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1139. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1140. [communication setUserAgent:[CCUtility getUserAgent]];
  1141. [communication createNewRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileName templateID:templateID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1142. completion(account, url, nil, 0);
  1143. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1144. NSString *message;
  1145. NSInteger errorCode = response.statusCode;
  1146. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1147. errorCode = error.code;
  1148. // Error
  1149. if (errorCode == 503)
  1150. message = NSLocalizedString(@"_server_error_retry_", nil);
  1151. else
  1152. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1153. completion(account, nil, message, errorCode);
  1154. }];
  1155. }
  1156. - (void)createAssetRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  1157. {
  1158. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1159. if (tableAccount == nil) {
  1160. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1161. } else if ([CCUtility getPassword:account].length == 0) {
  1162. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1163. } else if ([CCUtility getCertificateError:account]) {
  1164. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1165. }
  1166. NSString *fileNamePath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:tableAccount.url];
  1167. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1168. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1169. [communication setUserAgent:[CCUtility getUserAgent]];
  1170. [communication createAssetRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1171. completion(account, url, nil, 0);
  1172. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1173. NSString *message;
  1174. NSInteger errorCode = response.statusCode;
  1175. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1176. errorCode = error.code;
  1177. // Error
  1178. if (errorCode == 503)
  1179. message = NSLocalizedString(@"_server_error_retry_", nil);
  1180. else
  1181. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1182. completion(account, nil, message, errorCode);
  1183. }];
  1184. }
  1185. #pragma --------------------------------------------------------------------------------------------
  1186. #pragma mark ===== Full Text Search =====
  1187. #pragma --------------------------------------------------------------------------------------------
  1188. - (void)fullTextSearchWithAccount:(NSString *)account text:(NSString *)text page:(NSInteger)page completion:(void(^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1189. {
  1190. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1191. if (tableAccount == nil) {
  1192. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1193. } else if ([CCUtility getPassword:account].length == 0) {
  1194. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1195. } else if ([CCUtility getCertificateError:account]) {
  1196. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1197. }
  1198. // Create JSON
  1199. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  1200. [dataDic setValue:@"files" forKey:@"providers"];
  1201. [dataDic setValue:text forKey:@"search"];
  1202. [dataDic setValue:[NSNumber numberWithInteger:page] forKey:@"page"];
  1203. [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"size"];
  1204. NSMutableDictionary *options = [NSMutableDictionary new];
  1205. [options setValue:@"" forKey:@"files_within_dir"];
  1206. [options setValue:@"" forKey:@"files_local"];
  1207. [options setValue:@"" forKey:@"files_extension"];
  1208. [dataDic setValue:options forKey:@"options"];
  1209. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  1210. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1211. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1212. [communication setUserAgent:[CCUtility getUserAgent]];
  1213. [communication fullTextSearch:[tableAccount.url stringByAppendingString:@"/"] data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  1214. completion(account, items, nil, 0);
  1215. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1216. NSString *message;
  1217. NSInteger errorCode = response.statusCode;
  1218. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1219. errorCode = error.code;
  1220. // Error
  1221. if (errorCode == 503)
  1222. message = NSLocalizedString(@"_server_error_retry_", nil);
  1223. else
  1224. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1225. completion(account, nil, message, errorCode);
  1226. }];
  1227. }
  1228. #pragma --------------------------------------------------------------------------------------------
  1229. #pragma mark ===== Check remote user =====
  1230. #pragma --------------------------------------------------------------------------------------------
  1231. - (void)checkRemoteUser:(NSString *)account function:(NSString *)function errorCode:(NSInteger)errorCode
  1232. {
  1233. #ifndef EXTENSION
  1234. @synchronized(self) {
  1235. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1236. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:account];
  1237. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1238. NSString *token = [CCUtility getPassword:account];
  1239. if (self.checkRemoteUserInProgress || tableAccount == nil || token == nil) {
  1240. return;
  1241. } else {
  1242. self.checkRemoteUserInProgress = true;
  1243. }
  1244. if (capabilities != nil && capabilities.versionMajor >= k_nextcloud_version_17_0) {
  1245. [[OCNetworking sharedManager] getRemoteWipeStatusWithAccount:account token:token completion:^(NSString *account, BOOL wipe, NSString *message, NSInteger errorCode) {
  1246. if (wipe) {
  1247. [appDelegate deleteAccount:account wipe:true];
  1248. [[NCContentPresenter shared] messageNotification:tableAccount.user description:@"_wipe_account_" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  1249. [[OCNetworking sharedManager] setRemoteWipeCompletitionWithUser:tableAccount.user userID:tableAccount.userID url:tableAccount.url token:token completion:^(NSString *message, NSInteger errorCode) {
  1250. NSLog(@"Wiped");
  1251. }];
  1252. } else {
  1253. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive && [appDelegate.reachability isReachable]) {
  1254. [[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];
  1255. [CCUtility setPassword:account password:nil];
  1256. }
  1257. }
  1258. self.checkRemoteUserInProgress = false;
  1259. }];
  1260. } else if ([CCUtility getPassword:account] != nil) {
  1261. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive && [appDelegate.reachability isReachable]) {
  1262. [[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];
  1263. [CCUtility setPassword:account password:nil];
  1264. }
  1265. }
  1266. }
  1267. #endif
  1268. }
  1269. - (void)getRemoteWipeStatusWithAccount:(NSString *)account token:(NSString *)token completion:(void(^)(NSString *account, BOOL wipe, NSString *message, NSInteger errorCode))completion
  1270. {
  1271. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1272. if (tableAccount == nil) {
  1273. completion(account, false, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1274. } else if ([CCUtility getPassword:account].length == 0) {
  1275. completion(account, false, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1276. } else if ([CCUtility getCertificateError:account]) {
  1277. completion(account, false, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1278. }
  1279. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1280. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1281. [communication setUserAgent:[CCUtility getUserAgent]];
  1282. [communication getRemoteWipeStatus:tableAccount.url token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, BOOL wipe, NSString *redirectedServer) {
  1283. completion(account, wipe, nil, 0);
  1284. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1285. NSString *message;
  1286. NSInteger errorCode = response.statusCode;
  1287. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1288. errorCode = error.code;
  1289. // Server Unauthorized
  1290. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1291. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get remote wipe status" errorCode:errorCode];
  1292. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1293. [CCUtility setCertificateError:account error:YES];
  1294. }
  1295. // Error
  1296. if (errorCode == 503)
  1297. message = NSLocalizedString(@"_server_error_retry_", nil);
  1298. else
  1299. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1300. completion(account, false, message, errorCode);
  1301. }];
  1302. }
  1303. - (void)setRemoteWipeCompletitionWithUser:(NSString *)user userID:(NSString *)userID url:(NSString *)url token:(NSString *)token completion:(void(^)(NSString *message, NSInteger errorCode))completion
  1304. {
  1305. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1306. [communication setCredentialsWithUser:user andUserID:userID andPassword:token];
  1307. [communication setUserAgent:[CCUtility getUserAgent]];
  1308. [communication setRemoteWipeCompletition:url token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1309. completion(nil, 0);
  1310. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1311. NSString *message;
  1312. NSInteger errorCode = response.statusCode;
  1313. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1314. errorCode = error.code;
  1315. // Error
  1316. if (errorCode == 503)
  1317. message = NSLocalizedString(@"_server_error_retry_", nil);
  1318. else
  1319. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1320. completion(message, errorCode);
  1321. }];
  1322. }
  1323. #pragma --------------------------------------------------------------------------------------------
  1324. #pragma mark ===== Trash =====
  1325. #pragma --------------------------------------------------------------------------------------------
  1326. - (void)listingTrashWithAccount:(NSString *)account path:(NSString *)path serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1327. {
  1328. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1329. if (tableAccount == nil) {
  1330. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1331. } else if ([CCUtility getPassword:account].length == 0) {
  1332. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1333. } else if ([CCUtility getCertificateError:account]) {
  1334. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1335. }
  1336. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1337. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1338. [communication setUserAgent:[CCUtility getUserAgent]];
  1339. [communication listingTrash:[serverUrl stringByAppendingString:path] depth:depth onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  1340. // Check items > 0
  1341. if ([items count] == 0) {
  1342. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  1343. completion(account, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  1344. } else {
  1345. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1346. NSMutableArray *listTrash = [NSMutableArray new];
  1347. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  1348. if ([items count] > 1) {
  1349. for (NSUInteger i=1; i < [items count]; i++) {
  1350. OCFileDto *itemDto = [items objectAtIndex:i];
  1351. tableTrash *trash = [tableTrash new];
  1352. trash.account = account;
  1353. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  1354. trash.directory = itemDto.isDirectory;
  1355. trash.fileId = itemDto.fileId;
  1356. trash.fileName = itemDto.fileName;
  1357. NSArray *array = [itemDto.filePath componentsSeparatedByString:path];
  1358. long len = [[array objectAtIndex:0] length];
  1359. trash.filePath = [itemDto.filePath substringFromIndex:len];
  1360. trash.hasPreview = itemDto.hasPreview;
  1361. trash.size = itemDto.size;
  1362. trash.trashbinFileName = itemDto.trashbinFileName;
  1363. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  1364. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  1365. [CCUtility insertTypeFileIconName:trash.trashbinFileName metadata:(tableMetadata *)trash];
  1366. [listTrash addObject:trash];
  1367. }
  1368. }
  1369. dispatch_async(dispatch_get_main_queue(), ^{
  1370. completion(account, listTrash, nil, 0);
  1371. });
  1372. });
  1373. }
  1374. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1375. NSString *message;
  1376. NSInteger errorCode = response.statusCode;
  1377. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1378. errorCode = error.code;
  1379. // Error
  1380. if (errorCode == 503)
  1381. message = NSLocalizedString(@"_server_error_retry_", nil);
  1382. else
  1383. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1384. completion(account, nil,message, errorCode);
  1385. }];
  1386. }
  1387. - (void)emptyTrashWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1388. {
  1389. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1390. if (tableAccount == nil) {
  1391. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1392. } else if ([CCUtility getPassword:account].length == 0) {
  1393. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1394. } else if ([CCUtility getCertificateError:account]) {
  1395. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1396. }
  1397. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", tableAccount.url, k_dav, tableAccount.userID];
  1398. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1399. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1400. [communication setUserAgent:[CCUtility getUserAgent]];
  1401. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1402. completion(account, nil, 0);
  1403. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1404. NSString *message;
  1405. NSInteger errorCode = response.statusCode;
  1406. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1407. errorCode = error.code;
  1408. // Error
  1409. if (errorCode == 503)
  1410. message = NSLocalizedString(@"_server_error_retry_", nil);
  1411. else
  1412. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1413. completion(account, message, errorCode);
  1414. }];
  1415. }
  1416. #pragma --------------------------------------------------------------------------------------------
  1417. #pragma mark ===== Comments =====
  1418. #pragma --------------------------------------------------------------------------------------------
  1419. - (void)getCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1420. {
  1421. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1422. if (tableAccount == nil) {
  1423. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1424. } else if ([CCUtility getPassword:account].length == 0) {
  1425. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1426. } else if ([CCUtility getCertificateError:account]) {
  1427. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1428. }
  1429. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1430. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1431. [communication setUserAgent:[CCUtility getUserAgent]];
  1432. [communication getComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *list, NSString *redirectedServer) {
  1433. completion(account, list, nil, 0);
  1434. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1435. NSString *message;
  1436. NSInteger errorCode = response.statusCode;
  1437. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1438. errorCode = error.code;
  1439. // Error
  1440. if (errorCode == 503)
  1441. message = NSLocalizedString(@"_server_error_retry_", nil);
  1442. else
  1443. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1444. completion(account, nil,message, errorCode);
  1445. }];
  1446. }
  1447. - (void)putCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1448. {
  1449. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1450. if (tableAccount == nil) {
  1451. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1452. } else if ([CCUtility getPassword:account].length == 0) {
  1453. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1454. } else if ([CCUtility getCertificateError:account]) {
  1455. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1456. }
  1457. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1458. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1459. [communication setUserAgent:[CCUtility getUserAgent]];
  1460. [communication putComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1461. completion(account, nil, 0);
  1462. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1463. NSString *message;
  1464. NSInteger errorCode = response.statusCode;
  1465. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1466. errorCode = error.code;
  1467. // Error
  1468. if (errorCode == 503)
  1469. message = NSLocalizedString(@"_server_error_retry_", nil);
  1470. else
  1471. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1472. completion(account, message, errorCode);
  1473. }];
  1474. }
  1475. - (void)updateCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1476. {
  1477. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1478. if (tableAccount == nil) {
  1479. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1480. } else if ([CCUtility getPassword:account].length == 0) {
  1481. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1482. } else if ([CCUtility getCertificateError:account]) {
  1483. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1484. }
  1485. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1486. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1487. [communication setUserAgent:[CCUtility getUserAgent]];
  1488. [communication updateComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1489. completion(account, nil, 0);
  1490. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1491. NSString *message;
  1492. NSInteger errorCode = response.statusCode;
  1493. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1494. errorCode = error.code;
  1495. // Error
  1496. if (errorCode == 503)
  1497. message = NSLocalizedString(@"_server_error_retry_", nil);
  1498. else
  1499. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1500. completion(account, message, errorCode);
  1501. }];
  1502. }
  1503. - (void)readMarkCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1504. {
  1505. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1506. if (tableAccount == nil) {
  1507. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1508. } else if ([CCUtility getPassword:account].length == 0) {
  1509. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1510. } else if ([CCUtility getCertificateError:account]) {
  1511. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1512. }
  1513. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1514. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1515. [communication setUserAgent:[CCUtility getUserAgent]];
  1516. [communication readMarkComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1517. completion(account, nil, 0);
  1518. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1519. NSString *message;
  1520. NSInteger errorCode = response.statusCode;
  1521. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1522. errorCode = error.code;
  1523. // Error
  1524. if (errorCode == 503)
  1525. message = NSLocalizedString(@"_server_error_retry_", nil);
  1526. else
  1527. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1528. completion(account, message, errorCode);
  1529. }];
  1530. }
  1531. - (void)deleteCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1532. {
  1533. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1534. if (tableAccount == nil) {
  1535. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1536. } else if ([CCUtility getPassword:account].length == 0) {
  1537. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1538. } else if ([CCUtility getCertificateError:account]) {
  1539. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1540. }
  1541. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1542. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1543. [communication setUserAgent:[CCUtility getUserAgent]];
  1544. [communication deleteComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1545. completion(account, nil, 0);
  1546. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1547. NSString *message;
  1548. NSInteger errorCode = response.statusCode;
  1549. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1550. errorCode = error.code;
  1551. // Error
  1552. if (errorCode == 503)
  1553. message = NSLocalizedString(@"_server_error_retry_", nil);
  1554. else
  1555. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1556. completion(account, message, errorCode);
  1557. }];
  1558. }
  1559. #pragma --------------------------------------------------------------------------------------------
  1560. #pragma mark ===== Third Parts =====
  1561. #pragma --------------------------------------------------------------------------------------------
  1562. - (void)getHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  1563. {
  1564. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1565. if (tableAccount == nil) {
  1566. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1567. } else if ([CCUtility getPassword:account].length == 0) {
  1568. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1569. } else if ([CCUtility getCertificateError:account]) {
  1570. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1571. }
  1572. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  1573. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1574. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1575. [communication setUserAgent:[CCUtility getUserAgent]];
  1576. [communication getHCUserProfile:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1577. completion(account, userProfile, nil, 0);
  1578. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1579. NSString *message;
  1580. NSInteger errorCode = response.statusCode;
  1581. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1582. errorCode = error.code;
  1583. // Server Unauthorized
  1584. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1585. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get user profile" errorCode:errorCode];
  1586. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1587. [CCUtility setCertificateError:account error:YES];
  1588. }
  1589. // Error
  1590. if (errorCode == 503)
  1591. message = NSLocalizedString(@"_server_error_retry_", nil);
  1592. else
  1593. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1594. completion(account, nil,message, errorCode);
  1595. }];
  1596. }
  1597. - (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
  1598. {
  1599. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1600. if (tableAccount == nil) {
  1601. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1602. } else if ([CCUtility getPassword:account].length == 0) {
  1603. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1604. } else if ([CCUtility getCertificateError:account]) {
  1605. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1606. }
  1607. // Create JSON
  1608. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  1609. if (address) [dataDic setValue:address forKey:@"address"];
  1610. if (businesssize) {
  1611. if ([businesssize isEqualToString:@"1-4"]) { [dataDic setValue:[NSNumber numberWithInt:1] forKey:@"businesssize"]; }
  1612. else if ([businesssize isEqualToString:@"5-9"]) { [dataDic setValue:[NSNumber numberWithInt:5] forKey:@"businesssize"]; }
  1613. else if ([businesssize isEqualToString:@"10-19"]) { [dataDic setValue:[NSNumber numberWithInt:10] forKey:@"businesssize"]; }
  1614. else if ([businesssize isEqualToString:@"20-49"]) { [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"businesssize"]; }
  1615. else if ([businesssize isEqualToString:@"50-99"]) { [dataDic setValue:[NSNumber numberWithInt:50] forKey:@"businesssize"]; }
  1616. else if ([businesssize isEqualToString:@"100-249"]) { [dataDic setValue:[NSNumber numberWithInt:100] forKey:@"businesssize"]; }
  1617. else if ([businesssize isEqualToString:@"250-499"]) { [dataDic setValue:[NSNumber numberWithInt:250] forKey:@"businesssize"]; }
  1618. else if ([businesssize isEqualToString:@"500-999"]) { [dataDic setValue:[NSNumber numberWithInt:500] forKey:@"businesssize"]; }
  1619. else if ([businesssize isEqualToString:@"1000+"]) { [dataDic setValue:[NSNumber numberWithInt:1000] forKey:@"businesssize"]; }
  1620. }
  1621. if (businesstype) [dataDic setValue:businesstype forKey:@"businesstype"];
  1622. if (city) [dataDic setValue:city forKey:@"city"];
  1623. if (company) [dataDic setValue:company forKey:@"company"];
  1624. if (country) [dataDic setValue:country forKey:@"country"];
  1625. if (displayname) [dataDic setValue:displayname forKey:@"displayname"];
  1626. if (email) [dataDic setValue:email forKey:@"email"];
  1627. if (phone) [dataDic setValue:phone forKey:@"phone"];
  1628. if (role_) [dataDic setValue:role_ forKey:@"role"];
  1629. if (twitter) [dataDic setValue:twitter forKey:@"twitter"];
  1630. if (website) [dataDic setValue:website forKey:@"website"];
  1631. if (zip) [dataDic setValue:zip forKey:@"zip"];
  1632. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  1633. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  1634. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1635. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1636. [communication setUserAgent:[CCUtility getUserAgent]];
  1637. [communication putHCUserProfile:serverPath data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1638. completion(account, nil, 0);
  1639. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1640. NSString *message;
  1641. NSInteger errorCode = response.statusCode;
  1642. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1643. errorCode = error.code;
  1644. // Server Unauthorized
  1645. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1646. [[OCNetworking sharedManager] checkRemoteUser:account function:@"put user profile" errorCode:errorCode];
  1647. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1648. [CCUtility setCertificateError:account error:YES];
  1649. }
  1650. // Error
  1651. if (errorCode == 503)
  1652. message = NSLocalizedString(@"_server_error_retry_", nil);
  1653. else
  1654. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1655. completion(account, message, errorCode);
  1656. }];
  1657. }
  1658. - (void)getHCFeaturesWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, HCFeatures *features, NSString *message, NSInteger errorCode))completion
  1659. {
  1660. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1661. if (tableAccount == nil) {
  1662. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1663. } else if ([CCUtility getPassword:account].length == 0) {
  1664. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1665. } else if ([CCUtility getCertificateError:account]) {
  1666. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1667. }
  1668. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/features/%@", serverUrl, tableAccount.userID];
  1669. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1670. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1671. [communication setUserAgent:[CCUtility getUserAgent]];
  1672. [communication getHCFeatures:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer) {
  1673. completion(account, features, nil, 0);
  1674. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1675. NSString *message;
  1676. NSInteger errorCode = response.statusCode;
  1677. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1678. errorCode = error.code;
  1679. // Server Unauthorized
  1680. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1681. [[OCNetworking sharedManager] checkRemoteUser:account function:@"get features" errorCode:errorCode];
  1682. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1683. [CCUtility setCertificateError:account error:YES];
  1684. }
  1685. // Error
  1686. if (errorCode == 503)
  1687. message = NSLocalizedString(@"_server_error_retry_", nil);
  1688. else
  1689. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1690. completion(account, nil,message, errorCode);
  1691. }];
  1692. }
  1693. /*
  1694. - (void)middlewarePing
  1695. {
  1696. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1697. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1698. [communication setUserAgent:[CCUtility getUserAgent]];
  1699. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1700. [self complete];
  1701. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1702. NSInteger errorCode = response.statusCode;
  1703. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1704. errorCode = error.code;
  1705. // Error
  1706. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  1707. if (errorCode == 503)
  1708. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  1709. else
  1710. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1711. }
  1712. // Request trusted certificated
  1713. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1714. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1715. [self complete];
  1716. }];
  1717. }
  1718. */
  1719. #pragma --------------------------------------------------------------------------------------------
  1720. #pragma mark ===== didReceiveChallenge =====
  1721. #pragma --------------------------------------------------------------------------------------------
  1722. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  1723. {
  1724. // The pinnning check
  1725. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1726. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1727. } else {
  1728. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1729. }
  1730. }
  1731. @end
  1732. #pragma --------------------------------------------------------------------------------------------
  1733. #pragma mark ===== OCURLSessionManager =====
  1734. #pragma --------------------------------------------------------------------------------------------
  1735. @implementation OCURLSessionManager
  1736. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1737. {
  1738. // The pinnning check
  1739. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1740. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1741. } else {
  1742. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1743. }
  1744. }
  1745. @end