OCNetworking.m 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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 ===== Share =====
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (void)readShareWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  96. {
  97. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  98. if (tableAccount == nil) {
  99. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  100. } else if ([CCUtility getPassword:account].length == 0) {
  101. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  102. } else if ([CCUtility getCertificateError:account]) {
  103. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  104. }
  105. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  106. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  107. [communication setUserAgent:[CCUtility getUserAgent]];
  108. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  109. completion(account, items, nil, 0);
  110. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  111. NSString *message;
  112. NSInteger errorCode = response.statusCode;
  113. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  114. errorCode = error.code;
  115. // Error
  116. if (errorCode == 503) {
  117. message = NSLocalizedString(@"_server_error_retry_", nil);
  118. } else {
  119. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  120. }
  121. completion(account, nil, message, errorCode);
  122. }];
  123. }
  124. - (void)readShareWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  125. {
  126. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  127. if (tableAccount == nil) {
  128. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  129. } else if ([CCUtility getPassword:account].length == 0) {
  130. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  131. } else if ([CCUtility getCertificateError:account]) {
  132. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  133. }
  134. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  135. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  136. [communication setUserAgent:[CCUtility getUserAgent]];
  137. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] andPath:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  138. completion(account, listOfShared, nil, 0);
  139. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  140. NSString *message;
  141. NSInteger errorCode = response.statusCode;
  142. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  143. errorCode = error.code;
  144. // Error
  145. if (errorCode == 503) {
  146. message = NSLocalizedString(@"_server_error_retry_", nil);
  147. } else {
  148. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  149. }
  150. completion(account, nil, message, errorCode);
  151. }];
  152. }
  153. - (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
  154. {
  155. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  156. if (tableAccount == nil) {
  157. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  158. } else if ([CCUtility getPassword:account].length == 0) {
  159. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  160. } else if ([CCUtility getCertificateError:account]) {
  161. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  162. }
  163. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  164. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  165. [communication setUserAgent:[CCUtility getUserAgent]];
  166. [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) {
  167. completion(account, nil, 0);
  168. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  169. NSString *message;
  170. NSInteger errorCode = response.statusCode;
  171. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  172. errorCode = error.code;
  173. // Error
  174. if (errorCode == 503) {
  175. message = NSLocalizedString(@"_server_error_retry_", nil);
  176. } else {
  177. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  178. }
  179. completion(account, message, errorCode);
  180. }];
  181. }
  182. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  183. - (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
  184. {
  185. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  186. if (tableAccount == nil) {
  187. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  188. } else if ([CCUtility getPassword:account].length == 0) {
  189. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  190. } else if ([CCUtility getCertificateError:account]) {
  191. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  192. }
  193. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  194. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  195. [communication setUserAgent:[CCUtility getUserAgent]];
  196. [communication shareWith:userOrGroup shareeType:shareeType inServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPermissions:permission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  197. completion(account, nil, 0);
  198. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  199. NSString *message;
  200. NSInteger errorCode = response.statusCode;
  201. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  202. errorCode = error.code;
  203. // Error
  204. if (errorCode == 503) {
  205. message = NSLocalizedString(@"_server_error_retry_", nil);
  206. } else {
  207. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  208. }
  209. completion(account, message, errorCode);
  210. }];
  211. }
  212. - (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
  213. {
  214. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  215. if (tableAccount == nil) {
  216. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  217. } else if ([CCUtility getPassword:account].length == 0) {
  218. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  219. } else if ([CCUtility getCertificateError:account]) {
  220. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  221. }
  222. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  223. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  224. [communication setUserAgent:[CCUtility getUserAgent]];
  225. [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) {
  226. completion(account, nil, 0);
  227. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  228. NSString *message;
  229. NSInteger errorCode = response.statusCode;
  230. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  231. errorCode = error.code;
  232. // Error
  233. if (errorCode == 503) {
  234. message = NSLocalizedString(@"_server_error_retry_", nil);
  235. } else {
  236. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  237. }
  238. completion(account, message, errorCode);
  239. }];
  240. }
  241. - (void)unshareAccount:(NSString *)account shareID:(NSInteger)shareID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  242. {
  243. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  244. if (tableAccount == nil) {
  245. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  246. } else if ([CCUtility getPassword:account].length == 0) {
  247. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  248. } else if ([CCUtility getCertificateError:account]) {
  249. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  250. }
  251. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  252. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  253. [communication setUserAgent:[CCUtility getUserAgent]];
  254. [communication unShareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andIdRemoteShared:shareID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  255. completion(account, nil, 0);
  256. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  257. NSString *message;
  258. NSInteger errorCode = response.statusCode;
  259. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  260. errorCode = error.code;
  261. // Error
  262. if (errorCode == 503) {
  263. message = NSLocalizedString(@"_server_error_retry_", nil);
  264. } else {
  265. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  266. }
  267. completion(account, message, errorCode);
  268. }];
  269. }
  270. - (void)getUserGroupWithAccount:(NSString *)account searchString:(NSString *)searchString completion:(void (^)(NSString *account, NSArray *item, NSString *message, NSInteger errorCode))completion
  271. {
  272. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  273. if (tableAccount == nil) {
  274. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  275. } else if ([CCUtility getPassword:account].length == 0) {
  276. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  277. } else if ([CCUtility getCertificateError:account]) {
  278. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  279. }
  280. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  281. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  282. [communication setUserAgent:[CCUtility getUserAgent]];
  283. [communication searchUsersAndGroupsWith:searchString forPage:1 with:50 ofServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  284. completion(account, itemList, nil, 0);
  285. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  286. NSString *message;
  287. NSInteger errorCode = response.statusCode;
  288. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  289. errorCode = error.code;
  290. // Error
  291. if (errorCode == 503) {
  292. message = NSLocalizedString(@"_server_error_retry_", nil);
  293. } else {
  294. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  295. }
  296. completion(account, nil, message, errorCode);
  297. }];
  298. }
  299. #pragma --------------------------------------------------------------------------------------------
  300. #pragma mark ===== VAR =====
  301. #pragma --------------------------------------------------------------------------------------------
  302. - (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
  303. {
  304. BOOL previews = false;
  305. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  306. if (tableAccount == nil) {
  307. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  308. } else if ([CCUtility getPassword:account].length == 0) {
  309. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  310. } else if ([CCUtility getCertificateError:account]) {
  311. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  312. }
  313. NSInteger serverVersionMajor = [[NCManageDatabase sharedInstance] getCapabilitiesServerIntWithAccount:account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
  314. if (serverVersionMajor >= k_nextcloud_version_15_0) {
  315. previews = true;
  316. }
  317. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  318. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  319. [communication setUserAgent:[CCUtility getUserAgent]];
  320. [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) {
  321. completion(account, listOfActivity, nil, 0);
  322. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  323. NSString *message;
  324. NSInteger errorCode = response.statusCode;
  325. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  326. errorCode = error.code;
  327. // Server Unauthorized
  328. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  329. #ifndef EXTENSION
  330. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  331. #endif
  332. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  333. [CCUtility setCertificateError:account error:YES];
  334. }
  335. // Error
  336. if (errorCode == 503)
  337. message = NSLocalizedString(@"_server_error_retry_", nil);
  338. else
  339. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  340. completion(account, nil, message, errorCode);
  341. }];
  342. }
  343. - (void)getNotificationWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  344. {
  345. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  346. if (tableAccount == nil) {
  347. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  348. } else if ([CCUtility getPassword:account].length == 0) {
  349. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  350. } else if ([CCUtility getCertificateError:account]) {
  351. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  352. }
  353. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  354. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  355. [communication setUserAgent:[CCUtility getUserAgent]];
  356. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  357. completion(account, listOfNotifications, nil, 0);
  358. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  359. NSString *message;
  360. NSInteger errorCode = response.statusCode;
  361. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  362. errorCode = error.code;
  363. // Server Unauthorized
  364. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  365. #ifndef EXTENSION
  366. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  367. #endif
  368. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  369. [CCUtility setCertificateError:account error:YES];
  370. }
  371. // Error
  372. if (errorCode == 503) {
  373. message = NSLocalizedString(@"_server_error_retry_", nil);
  374. } else {
  375. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  376. }
  377. completion(account, nil, message, errorCode);
  378. }];
  379. }
  380. - (void)setNotificationWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  381. {
  382. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  383. if (tableAccount == nil) {
  384. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  385. } else if ([CCUtility getPassword:account].length == 0) {
  386. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  387. } else if ([CCUtility getCertificateError:account]) {
  388. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  389. }
  390. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  391. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  392. [communication setUserAgent:[CCUtility getUserAgent]];
  393. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  394. completion(account, nil, 0);
  395. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  396. NSString *message;
  397. NSInteger errorCode = response.statusCode;
  398. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  399. errorCode = error.code;
  400. // Server Unauthorized
  401. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  402. #ifndef EXTENSION
  403. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  404. #endif
  405. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  406. [CCUtility setCertificateError:account error:YES];
  407. }
  408. // Error
  409. if (errorCode == 503) {
  410. message = NSLocalizedString(@"_server_error_retry_", nil);
  411. } else {
  412. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  413. }
  414. completion(account, message, errorCode);
  415. }];
  416. }
  417. #pragma --------------------------------------------------------------------------------------------
  418. #pragma mark ===== Push Notification =====
  419. #pragma --------------------------------------------------------------------------------------------
  420. - (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
  421. {
  422. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  423. if (tableAccount == nil) {
  424. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  425. } else if ([CCUtility getPassword:account].length == 0) {
  426. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  427. } else if ([CCUtility getCertificateError:account]) {
  428. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  429. }
  430. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  431. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  432. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  433. #ifdef DEBUG
  434. // proxyServerPath = @"http://127.0.0.1:8088";
  435. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  436. #endif
  437. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  438. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  439. [communication setUserAgent:[CCUtility getUserAgent]];
  440. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: proxyServerPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  441. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  442. signature = [CCUtility URLEncodeStringFromString:signature];
  443. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  444. [communication subscribingPushProxy:proxyServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  445. completion(account, deviceIdentifier, signature, publicKey, nil, 0);
  446. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  447. NSString *message;
  448. NSInteger errorCode = response.statusCode;
  449. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  450. errorCode = error.code;
  451. // Error
  452. if (errorCode == 503)
  453. message = NSLocalizedString(@"_server_error_retry_", nil);
  454. else
  455. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  456. completion(account, nil, nil, nil, message, errorCode);
  457. }];
  458. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  459. NSString *message;
  460. NSInteger errorCode = response.statusCode;
  461. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  462. errorCode = error.code;
  463. // Error
  464. if (errorCode == 503)
  465. message = NSLocalizedString(@"_server_error_retry_", nil);
  466. else
  467. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  468. completion(account, nil, nil, nil, message, errorCode);
  469. }];
  470. }
  471. - (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 {
  472. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  473. if (tableAccount == nil) {
  474. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  475. } else if ([CCUtility getPassword:account].length == 0) {
  476. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  477. } else if ([CCUtility getCertificateError:account]) {
  478. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  479. }
  480. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  481. #ifdef DEBUG
  482. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  483. #endif
  484. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  485. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  486. [communication setUserAgent:[CCUtility getUserAgent]];
  487. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  488. [communication unsubscribingPushProxy:proxyServer deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  489. completion(account, nil, 0);
  490. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  491. NSString *message;
  492. NSInteger errorCode = response.statusCode;
  493. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  494. errorCode = error.code;
  495. // Error
  496. if (errorCode == 503)
  497. message = NSLocalizedString(@"_server_error_retry_", nil);
  498. else
  499. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  500. completion(account, message, errorCode);
  501. }];
  502. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  503. NSString *message;
  504. NSInteger errorCode = response.statusCode;
  505. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  506. errorCode = error.code;
  507. // Error
  508. if (errorCode == 503)
  509. message = NSLocalizedString(@"_server_error_retry_", nil);
  510. else
  511. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  512. completion(account, message, errorCode);
  513. }];
  514. }
  515. - (void)getServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSDictionary*jsongParsed, NSString *message, NSInteger errorCode))completion
  516. {
  517. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld?format=json", serverUrl, (long)notificationId];
  518. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  519. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  520. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  521. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  522. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  523. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  524. if (error) {
  525. NSString *message;
  526. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  527. NSInteger errorCode = httpResponse.statusCode;
  528. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  529. errorCode = error.code;
  530. // Error
  531. if (errorCode == 503)
  532. message = NSLocalizedString(@"_server_error_retry_", nil);
  533. else
  534. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  535. completion(nil, message, errorCode);
  536. } else {
  537. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  538. completion(jsongParsed, nil, 0);
  539. }
  540. }];
  541. [task resume];
  542. }
  543. - (void)deletingServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSString *message, NSInteger errorCode))completion
  544. {
  545. // NSData *authData = [[NSString stringWithFormat:@"%@:%@", tableAccount.user, [CCUtility getPassword:tableAccount.account]] dataUsingEncoding:NSUTF8StringEncoding];
  546. // NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  547. // Delete
  548. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld", serverUrl, (long)notificationId];
  549. // Delete-all
  550. if (notificationId == 0) {
  551. URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications", serverUrl];
  552. }
  553. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  554. // [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  555. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  556. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  557. [request setHTTPMethod: @"DELETE"];
  558. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  559. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  560. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  561. if (error) {
  562. NSString *message;
  563. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  564. NSInteger errorCode = httpResponse.statusCode;
  565. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  566. errorCode = error.code;
  567. // Error
  568. if (errorCode == 503)
  569. message = NSLocalizedString(@"_server_error_retry_", nil);
  570. else
  571. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  572. completion(message, errorCode);
  573. } else {
  574. completion(nil, 0);
  575. }
  576. }];
  577. [task resume];
  578. }
  579. #pragma --------------------------------------------------------------------------------------------
  580. #pragma mark ===== Manage Mobile Editor OCS API =====
  581. #pragma --------------------------------------------------------------------------------------------
  582. - (void)createLinkRichdocumentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  583. {
  584. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  585. if (tableAccount == nil) {
  586. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  587. } else if ([CCUtility getPassword:account].length == 0) {
  588. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  589. } else if ([CCUtility getCertificateError:account]) {
  590. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  591. }
  592. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  593. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  594. [communication setUserAgent:[CCUtility getUserAgent]];
  595. [communication createLinkRichdocuments:[tableAccount.url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer) {
  596. completion(account, link, nil, 0);
  597. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  598. NSString *message;
  599. NSInteger errorCode = response.statusCode;
  600. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  601. errorCode = error.code;
  602. // Error
  603. if (errorCode == 503)
  604. message = NSLocalizedString(@"_server_error_retry_", nil);
  605. else
  606. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  607. completion(account, nil,message, errorCode);
  608. }];
  609. }
  610. - (void)getTemplatesRichdocumentsWithAccount:(NSString *)account typeTemplate:(NSString *)typeTemplate completion:(void(^)(NSString *account, NSArray *listOfTemplate, NSString *message, NSInteger errorCode))completion
  611. {
  612. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  613. if (tableAccount == nil) {
  614. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  615. } else if ([CCUtility getPassword:account].length == 0) {
  616. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  617. } else if ([CCUtility getCertificateError:account]) {
  618. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  619. }
  620. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  621. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  622. [communication setUserAgent:[CCUtility getUserAgent]];
  623. [communication getTemplatesRichdocuments:[tableAccount.url stringByAppendingString:@"/"] typeTemplate:typeTemplate onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer) {
  624. completion(account, listOfTemplate, nil, 0);
  625. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  626. NSString *message;
  627. NSInteger errorCode = response.statusCode;
  628. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  629. errorCode = error.code;
  630. // Error
  631. if (errorCode == 503)
  632. message = NSLocalizedString(@"_server_error_retry_", nil);
  633. else
  634. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  635. completion(account, nil, message, errorCode);
  636. }];
  637. }
  638. - (void)createNewRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl templateID:(NSString *)templateID completion:(void(^)(NSString *account, NSString *url, NSString *message, NSInteger errorCode))completion
  639. {
  640. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  641. if (tableAccount == nil) {
  642. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  643. } else if ([CCUtility getPassword:account].length == 0) {
  644. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  645. } else if ([CCUtility getCertificateError:account]) {
  646. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  647. }
  648. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  649. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  650. [communication setUserAgent:[CCUtility getUserAgent]];
  651. [communication createNewRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileName templateID:templateID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  652. completion(account, url, nil, 0);
  653. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  654. NSString *message;
  655. NSInteger errorCode = response.statusCode;
  656. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  657. errorCode = error.code;
  658. // Error
  659. if (errorCode == 503)
  660. message = NSLocalizedString(@"_server_error_retry_", nil);
  661. else
  662. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  663. completion(account, nil, message, errorCode);
  664. }];
  665. }
  666. - (void)createAssetRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  667. {
  668. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  669. if (tableAccount == nil) {
  670. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  671. } else if ([CCUtility getPassword:account].length == 0) {
  672. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  673. } else if ([CCUtility getCertificateError:account]) {
  674. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  675. }
  676. NSString *fileNamePath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:tableAccount.url];
  677. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  678. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  679. [communication setUserAgent:[CCUtility getUserAgent]];
  680. [communication createAssetRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  681. completion(account, url, nil, 0);
  682. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  683. NSString *message;
  684. NSInteger errorCode = response.statusCode;
  685. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  686. errorCode = error.code;
  687. // Error
  688. if (errorCode == 503)
  689. message = NSLocalizedString(@"_server_error_retry_", nil);
  690. else
  691. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  692. completion(account, nil, message, errorCode);
  693. }];
  694. }
  695. #pragma --------------------------------------------------------------------------------------------
  696. #pragma mark ===== Trash =====
  697. #pragma --------------------------------------------------------------------------------------------
  698. - (void)listingTrashWithAccount:(NSString *)account path:(NSString *)path serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  699. {
  700. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  701. if (tableAccount == nil) {
  702. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  703. } else if ([CCUtility getPassword:account].length == 0) {
  704. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  705. } else if ([CCUtility getCertificateError:account]) {
  706. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  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 listingTrash:[serverUrl stringByAppendingString:path] depth:depth onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  712. // Check items > 0
  713. if ([items count] == 0) {
  714. [[NCContentPresenter shared] messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  715. completion(account, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  716. } else {
  717. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  718. NSMutableArray *listTrash = [NSMutableArray new];
  719. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  720. if ([items count] > 1) {
  721. for (NSUInteger i=1; i < [items count]; i++) {
  722. OCFileDto *itemDto = [items objectAtIndex:i];
  723. tableTrash *trash = [tableTrash new];
  724. trash.account = account;
  725. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  726. trash.directory = itemDto.isDirectory;
  727. trash.fileId = itemDto.fileId;
  728. trash.fileName = itemDto.fileName;
  729. NSArray *array = [itemDto.filePath componentsSeparatedByString:path];
  730. long len = [[array objectAtIndex:0] length];
  731. trash.filePath = [itemDto.filePath substringFromIndex:len];
  732. trash.hasPreview = itemDto.hasPreview;
  733. trash.size = itemDto.size;
  734. trash.trashbinFileName = itemDto.trashbinFileName;
  735. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  736. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  737. NSDictionary *results = [[NCCommunicationCommon shared] objcGetInternalContenTypeWithFileName:trash.trashbinFileName contentType:@"" directory:itemDto.isDirectory];
  738. trash.contentType = results[@"contentType"];
  739. trash.iconName = results[@"iconName"];
  740. trash.typeFile = results[@"typeFile"];
  741. [listTrash addObject:trash];
  742. }
  743. }
  744. dispatch_async(dispatch_get_main_queue(), ^{
  745. completion(account, listTrash, nil, 0);
  746. });
  747. });
  748. }
  749. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  750. NSString *message;
  751. NSInteger errorCode = response.statusCode;
  752. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  753. errorCode = error.code;
  754. // Error
  755. if (errorCode == 503)
  756. message = NSLocalizedString(@"_server_error_retry_", nil);
  757. else
  758. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  759. completion(account, nil,message, errorCode);
  760. }];
  761. }
  762. - (void)emptyTrashWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  763. {
  764. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  765. if (tableAccount == nil) {
  766. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  767. } else if ([CCUtility getPassword:account].length == 0) {
  768. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  769. } else if ([CCUtility getCertificateError:account]) {
  770. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  771. }
  772. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", tableAccount.url, k_dav, tableAccount.userID];
  773. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  774. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  775. [communication setUserAgent:[CCUtility getUserAgent]];
  776. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  777. completion(account, nil, 0);
  778. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  779. NSString *message;
  780. NSInteger errorCode = response.statusCode;
  781. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  782. errorCode = error.code;
  783. // Error
  784. if (errorCode == 503)
  785. message = NSLocalizedString(@"_server_error_retry_", nil);
  786. else
  787. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  788. completion(account, message, errorCode);
  789. }];
  790. }
  791. #pragma --------------------------------------------------------------------------------------------
  792. #pragma mark ===== Comments =====
  793. #pragma --------------------------------------------------------------------------------------------
  794. - (void)getCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  795. {
  796. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  797. if (tableAccount == nil) {
  798. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  799. } else if ([CCUtility getPassword:account].length == 0) {
  800. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  801. } else if ([CCUtility getCertificateError:account]) {
  802. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  803. }
  804. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  805. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  806. [communication setUserAgent:[CCUtility getUserAgent]];
  807. [communication getComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *list, NSString *redirectedServer) {
  808. completion(account, list, nil, 0);
  809. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  810. NSString *message;
  811. NSInteger errorCode = response.statusCode;
  812. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  813. errorCode = error.code;
  814. // Error
  815. if (errorCode == 503)
  816. message = NSLocalizedString(@"_server_error_retry_", nil);
  817. else
  818. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  819. completion(account, nil,message, errorCode);
  820. }];
  821. }
  822. - (void)putCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  823. {
  824. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  825. if (tableAccount == nil) {
  826. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  827. } else if ([CCUtility getPassword:account].length == 0) {
  828. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  829. } else if ([CCUtility getCertificateError:account]) {
  830. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  831. }
  832. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  833. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  834. [communication setUserAgent:[CCUtility getUserAgent]];
  835. [communication putComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  836. completion(account, nil, 0);
  837. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  838. NSString *message;
  839. NSInteger errorCode = response.statusCode;
  840. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  841. errorCode = error.code;
  842. // Error
  843. if (errorCode == 503)
  844. message = NSLocalizedString(@"_server_error_retry_", nil);
  845. else
  846. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  847. completion(account, message, errorCode);
  848. }];
  849. }
  850. - (void)updateCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  851. {
  852. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  853. if (tableAccount == nil) {
  854. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  855. } else if ([CCUtility getPassword:account].length == 0) {
  856. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  857. } else if ([CCUtility getCertificateError:account]) {
  858. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  859. }
  860. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  861. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  862. [communication setUserAgent:[CCUtility getUserAgent]];
  863. [communication updateComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  864. completion(account, nil, 0);
  865. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  866. NSString *message;
  867. NSInteger errorCode = response.statusCode;
  868. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  869. errorCode = error.code;
  870. // Error
  871. if (errorCode == 503)
  872. message = NSLocalizedString(@"_server_error_retry_", nil);
  873. else
  874. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  875. completion(account, message, errorCode);
  876. }];
  877. }
  878. - (void)readMarkCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  879. {
  880. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  881. if (tableAccount == nil) {
  882. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  883. } else if ([CCUtility getPassword:account].length == 0) {
  884. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  885. } else if ([CCUtility getCertificateError:account]) {
  886. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  887. }
  888. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  889. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  890. [communication setUserAgent:[CCUtility getUserAgent]];
  891. [communication readMarkComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  892. completion(account, nil, 0);
  893. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  894. NSString *message;
  895. NSInteger errorCode = response.statusCode;
  896. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  897. errorCode = error.code;
  898. // Error
  899. if (errorCode == 503)
  900. message = NSLocalizedString(@"_server_error_retry_", nil);
  901. else
  902. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  903. completion(account, message, errorCode);
  904. }];
  905. }
  906. - (void)deleteCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  907. {
  908. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  909. if (tableAccount == nil) {
  910. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  911. } else if ([CCUtility getPassword:account].length == 0) {
  912. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  913. } else if ([CCUtility getCertificateError:account]) {
  914. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  915. }
  916. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  917. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  918. [communication setUserAgent:[CCUtility getUserAgent]];
  919. [communication deleteComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  920. completion(account, nil, 0);
  921. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  922. NSString *message;
  923. NSInteger errorCode = response.statusCode;
  924. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  925. errorCode = error.code;
  926. // Error
  927. if (errorCode == 503)
  928. message = NSLocalizedString(@"_server_error_retry_", nil);
  929. else
  930. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  931. completion(account, message, errorCode);
  932. }];
  933. }
  934. #pragma --------------------------------------------------------------------------------------------
  935. #pragma mark ===== Third Parts =====
  936. #pragma --------------------------------------------------------------------------------------------
  937. - (void)getHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  938. {
  939. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  940. if (tableAccount == nil) {
  941. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  942. } else if ([CCUtility getPassword:account].length == 0) {
  943. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  944. } else if ([CCUtility getCertificateError:account]) {
  945. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  946. }
  947. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  948. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  949. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  950. [communication setUserAgent:[CCUtility getUserAgent]];
  951. [communication getHCUserProfile:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  952. completion(account, userProfile, nil, 0);
  953. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  954. NSString *message;
  955. NSInteger errorCode = response.statusCode;
  956. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  957. errorCode = error.code;
  958. // Server Unauthorized
  959. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  960. #ifndef EXTENSION
  961. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  962. #endif
  963. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  964. [CCUtility setCertificateError:account error:YES];
  965. }
  966. // Error
  967. if (errorCode == 503)
  968. message = NSLocalizedString(@"_server_error_retry_", nil);
  969. else
  970. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  971. completion(account, nil,message, errorCode);
  972. }];
  973. }
  974. - (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
  975. {
  976. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  977. if (tableAccount == nil) {
  978. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  979. } else if ([CCUtility getPassword:account].length == 0) {
  980. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  981. } else if ([CCUtility getCertificateError:account]) {
  982. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  983. }
  984. // Create JSON
  985. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  986. if (address) [dataDic setValue:address forKey:@"address"];
  987. if (businesssize) {
  988. if ([businesssize isEqualToString:@"1-4"]) { [dataDic setValue:[NSNumber numberWithInt:1] forKey:@"businesssize"]; }
  989. else if ([businesssize isEqualToString:@"5-9"]) { [dataDic setValue:[NSNumber numberWithInt:5] forKey:@"businesssize"]; }
  990. else if ([businesssize isEqualToString:@"10-19"]) { [dataDic setValue:[NSNumber numberWithInt:10] forKey:@"businesssize"]; }
  991. else if ([businesssize isEqualToString:@"20-49"]) { [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"businesssize"]; }
  992. else if ([businesssize isEqualToString:@"50-99"]) { [dataDic setValue:[NSNumber numberWithInt:50] forKey:@"businesssize"]; }
  993. else if ([businesssize isEqualToString:@"100-249"]) { [dataDic setValue:[NSNumber numberWithInt:100] forKey:@"businesssize"]; }
  994. else if ([businesssize isEqualToString:@"250-499"]) { [dataDic setValue:[NSNumber numberWithInt:250] forKey:@"businesssize"]; }
  995. else if ([businesssize isEqualToString:@"500-999"]) { [dataDic setValue:[NSNumber numberWithInt:500] forKey:@"businesssize"]; }
  996. else if ([businesssize isEqualToString:@"1000+"]) { [dataDic setValue:[NSNumber numberWithInt:1000] forKey:@"businesssize"]; }
  997. }
  998. if (businesstype) [dataDic setValue:businesstype forKey:@"businesstype"];
  999. if (city) [dataDic setValue:city forKey:@"city"];
  1000. if (company) [dataDic setValue:company forKey:@"company"];
  1001. if (country) [dataDic setValue:country forKey:@"country"];
  1002. if (displayname) [dataDic setValue:displayname forKey:@"displayname"];
  1003. if (email) [dataDic setValue:email forKey:@"email"];
  1004. if (phone) [dataDic setValue:phone forKey:@"phone"];
  1005. if (role_) [dataDic setValue:role_ forKey:@"role"];
  1006. if (twitter) [dataDic setValue:twitter forKey:@"twitter"];
  1007. if (website) [dataDic setValue:website forKey:@"website"];
  1008. if (zip) [dataDic setValue:zip forKey:@"zip"];
  1009. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  1010. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  1011. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1012. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1013. [communication setUserAgent:[CCUtility getUserAgent]];
  1014. [communication putHCUserProfile:serverPath data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1015. completion(account, nil, 0);
  1016. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1017. NSString *message;
  1018. NSInteger errorCode = response.statusCode;
  1019. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1020. errorCode = error.code;
  1021. // Server Unauthorized
  1022. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1023. #ifndef EXTENSION
  1024. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  1025. #endif
  1026. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1027. [CCUtility setCertificateError:account error:YES];
  1028. }
  1029. // Error
  1030. if (errorCode == 503)
  1031. message = NSLocalizedString(@"_server_error_retry_", nil);
  1032. else
  1033. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1034. completion(account, message, errorCode);
  1035. }];
  1036. }
  1037. - (void)getHCFeaturesWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, HCFeatures *features, NSString *message, NSInteger errorCode))completion
  1038. {
  1039. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1040. if (tableAccount == nil) {
  1041. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1042. } else if ([CCUtility getPassword:account].length == 0) {
  1043. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  1044. } else if ([CCUtility getCertificateError:account]) {
  1045. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  1046. }
  1047. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/features/%@", serverUrl, tableAccount.userID];
  1048. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  1049. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  1050. [communication setUserAgent:[CCUtility getUserAgent]];
  1051. [communication getHCFeatures:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer) {
  1052. completion(account, features, nil, 0);
  1053. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1054. NSString *message;
  1055. NSInteger errorCode = response.statusCode;
  1056. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1057. errorCode = error.code;
  1058. // Server Unauthorized
  1059. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  1060. #ifndef EXTENSION
  1061. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  1062. #endif
  1063. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  1064. [CCUtility setCertificateError:account error:YES];
  1065. }
  1066. // Error
  1067. if (errorCode == 503)
  1068. message = NSLocalizedString(@"_server_error_retry_", nil);
  1069. else
  1070. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1071. completion(account, nil,message, errorCode);
  1072. }];
  1073. }
  1074. #pragma --------------------------------------------------------------------------------------------
  1075. #pragma mark ===== didReceiveChallenge =====
  1076. #pragma --------------------------------------------------------------------------------------------
  1077. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  1078. {
  1079. // The pinnning check
  1080. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1081. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1082. } else {
  1083. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1084. }
  1085. }
  1086. @end
  1087. #pragma --------------------------------------------------------------------------------------------
  1088. #pragma mark ===== OCURLSessionManager =====
  1089. #pragma --------------------------------------------------------------------------------------------
  1090. @implementation OCURLSessionManager
  1091. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1092. {
  1093. // The pinnning check
  1094. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  1095. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1096. } else {
  1097. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1098. }
  1099. }
  1100. @end