OCNetworking.m 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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)getNotificationWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  303. {
  304. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  305. if (tableAccount == nil) {
  306. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  307. } else if ([CCUtility getPassword:account].length == 0) {
  308. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  309. } else if ([CCUtility getCertificateError:account]) {
  310. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  311. }
  312. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  313. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  314. [communication setUserAgent:[CCUtility getUserAgent]];
  315. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  316. completion(account, listOfNotifications, nil, 0);
  317. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  318. NSString *message;
  319. NSInteger errorCode = response.statusCode;
  320. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  321. errorCode = error.code;
  322. // Server Unauthorized
  323. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  324. #ifndef EXTENSION
  325. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  326. #endif
  327. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  328. [CCUtility setCertificateError:account error:YES];
  329. }
  330. // Error
  331. if (errorCode == 503) {
  332. message = NSLocalizedString(@"_server_error_retry_", nil);
  333. } else {
  334. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  335. }
  336. completion(account, nil, message, errorCode);
  337. }];
  338. }
  339. - (void)setNotificationWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  340. {
  341. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  342. if (tableAccount == nil) {
  343. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  344. } else if ([CCUtility getPassword:account].length == 0) {
  345. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  346. } else if ([CCUtility getCertificateError:account]) {
  347. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  348. }
  349. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  350. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  351. [communication setUserAgent:[CCUtility getUserAgent]];
  352. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  353. completion(account, nil, 0);
  354. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  355. NSString *message;
  356. NSInteger errorCode = response.statusCode;
  357. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  358. errorCode = error.code;
  359. // Server Unauthorized
  360. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  361. #ifndef EXTENSION
  362. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  363. #endif
  364. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  365. [CCUtility setCertificateError:account error:YES];
  366. }
  367. // Error
  368. if (errorCode == 503) {
  369. message = NSLocalizedString(@"_server_error_retry_", nil);
  370. } else {
  371. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  372. }
  373. completion(account, message, errorCode);
  374. }];
  375. }
  376. #pragma --------------------------------------------------------------------------------------------
  377. #pragma mark ===== Push Notification =====
  378. #pragma --------------------------------------------------------------------------------------------
  379. - (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
  380. {
  381. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  382. if (tableAccount == nil) {
  383. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  384. } else if ([CCUtility getPassword:account].length == 0) {
  385. completion(account, nil, nil, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  386. } else if ([CCUtility getCertificateError:account]) {
  387. completion(account, nil, nil, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  388. }
  389. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  390. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  391. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  392. #ifdef DEBUG
  393. // proxyServerPath = @"http://127.0.0.1:8088";
  394. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  395. #endif
  396. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  397. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  398. [communication setUserAgent:[CCUtility getUserAgent]];
  399. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: proxyServerPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  400. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  401. signature = [CCUtility URLEncodeStringFromString:signature];
  402. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  403. [communication subscribingPushProxy:proxyServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  404. completion(account, deviceIdentifier, signature, publicKey, nil, 0);
  405. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  406. NSString *message;
  407. NSInteger errorCode = response.statusCode;
  408. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  409. errorCode = error.code;
  410. // Error
  411. if (errorCode == 503)
  412. message = NSLocalizedString(@"_server_error_retry_", nil);
  413. else
  414. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  415. completion(account, nil, nil, nil, message, errorCode);
  416. }];
  417. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  418. NSString *message;
  419. NSInteger errorCode = response.statusCode;
  420. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  421. errorCode = error.code;
  422. // Error
  423. if (errorCode == 503)
  424. message = NSLocalizedString(@"_server_error_retry_", nil);
  425. else
  426. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  427. completion(account, nil, nil, nil, message, errorCode);
  428. }];
  429. }
  430. - (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 {
  431. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  432. if (tableAccount == nil) {
  433. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  434. } else if ([CCUtility getPassword:account].length == 0) {
  435. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  436. } else if ([CCUtility getCertificateError:account]) {
  437. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  438. }
  439. NSString *proxyServer = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  440. #ifdef DEBUG
  441. // proxyServer = @"https://10.132.0.37:8443/pushnotifications";
  442. #endif
  443. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  444. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  445. [communication setUserAgent:[CCUtility getUserAgent]];
  446. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  447. [communication unsubscribingPushProxy:proxyServer deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  448. completion(account, nil, 0);
  449. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  450. NSString *message;
  451. NSInteger errorCode = response.statusCode;
  452. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  453. errorCode = error.code;
  454. // Error
  455. if (errorCode == 503)
  456. message = NSLocalizedString(@"_server_error_retry_", nil);
  457. else
  458. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  459. completion(account, message, errorCode);
  460. }];
  461. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  462. NSString *message;
  463. NSInteger errorCode = response.statusCode;
  464. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  465. errorCode = error.code;
  466. // Error
  467. if (errorCode == 503)
  468. message = NSLocalizedString(@"_server_error_retry_", nil);
  469. else
  470. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  471. completion(account, message, errorCode);
  472. }];
  473. }
  474. - (void)getServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSDictionary*jsongParsed, NSString *message, NSInteger errorCode))completion
  475. {
  476. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld?format=json", serverUrl, (long)notificationId];
  477. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  478. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  479. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  480. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  481. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  482. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  483. if (error) {
  484. NSString *message;
  485. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  486. NSInteger errorCode = httpResponse.statusCode;
  487. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  488. errorCode = error.code;
  489. // Error
  490. if (errorCode == 503)
  491. message = NSLocalizedString(@"_server_error_retry_", nil);
  492. else
  493. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  494. completion(nil, message, errorCode);
  495. } else {
  496. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  497. completion(jsongParsed, nil, 0);
  498. }
  499. }];
  500. [task resume];
  501. }
  502. - (void)deletingServerNotification:(NSString *)serverUrl notificationId:(NSInteger)notificationId completion:(void(^)(NSString *message, NSInteger errorCode))completion
  503. {
  504. // NSData *authData = [[NSString stringWithFormat:@"%@:%@", tableAccount.user, [CCUtility getPassword:tableAccount.account]] dataUsingEncoding:NSUTF8StringEncoding];
  505. // NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  506. // Delete
  507. NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications/%ld", serverUrl, (long)notificationId];
  508. // Delete-all
  509. if (notificationId == 0) {
  510. URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/notifications/api/v2/notifications", serverUrl];
  511. }
  512. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:0 timeoutInterval:20.0];
  513. // [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  514. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  515. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  516. [request setHTTPMethod: @"DELETE"];
  517. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  518. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  519. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  520. if (error) {
  521. NSString *message;
  522. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  523. NSInteger errorCode = httpResponse.statusCode;
  524. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  525. errorCode = error.code;
  526. // Error
  527. if (errorCode == 503)
  528. message = NSLocalizedString(@"_server_error_retry_", nil);
  529. else
  530. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  531. completion(message, errorCode);
  532. } else {
  533. completion(nil, 0);
  534. }
  535. }];
  536. [task resume];
  537. }
  538. #pragma --------------------------------------------------------------------------------------------
  539. #pragma mark ===== Comments =====
  540. #pragma --------------------------------------------------------------------------------------------
  541. - (void)getCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  542. {
  543. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  544. if (tableAccount == nil) {
  545. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  546. } else if ([CCUtility getPassword:account].length == 0) {
  547. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  548. } else if ([CCUtility getCertificateError:account]) {
  549. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  550. }
  551. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  552. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  553. [communication setUserAgent:[CCUtility getUserAgent]];
  554. [communication getComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *list, NSString *redirectedServer) {
  555. completion(account, list, nil, 0);
  556. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  557. NSString *message;
  558. NSInteger errorCode = response.statusCode;
  559. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  560. errorCode = error.code;
  561. // Error
  562. if (errorCode == 503)
  563. message = NSLocalizedString(@"_server_error_retry_", nil);
  564. else
  565. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  566. completion(account, nil,message, errorCode);
  567. }];
  568. }
  569. - (void)putCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  570. {
  571. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  572. if (tableAccount == nil) {
  573. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  574. } else if ([CCUtility getPassword:account].length == 0) {
  575. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  576. } else if ([CCUtility getCertificateError:account]) {
  577. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  578. }
  579. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  580. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  581. [communication setUserAgent:[CCUtility getUserAgent]];
  582. [communication putComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  583. completion(account, nil, 0);
  584. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  585. NSString *message;
  586. NSInteger errorCode = response.statusCode;
  587. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  588. errorCode = error.code;
  589. // Error
  590. if (errorCode == 503)
  591. message = NSLocalizedString(@"_server_error_retry_", nil);
  592. else
  593. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  594. completion(account, message, errorCode);
  595. }];
  596. }
  597. - (void)updateCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID message:(NSString *)message completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  598. {
  599. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  600. if (tableAccount == nil) {
  601. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  602. } else if ([CCUtility getPassword:account].length == 0) {
  603. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  604. } else if ([CCUtility getCertificateError:account]) {
  605. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  606. }
  607. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  608. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  609. [communication setUserAgent:[CCUtility getUserAgent]];
  610. [communication updateComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID message:message onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  611. completion(account, nil, 0);
  612. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  613. NSString *message;
  614. NSInteger errorCode = response.statusCode;
  615. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  616. errorCode = error.code;
  617. // Error
  618. if (errorCode == 503)
  619. message = NSLocalizedString(@"_server_error_retry_", nil);
  620. else
  621. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  622. completion(account, message, errorCode);
  623. }];
  624. }
  625. - (void)readMarkCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  626. {
  627. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  628. if (tableAccount == nil) {
  629. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  630. } else if ([CCUtility getPassword:account].length == 0) {
  631. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  632. } else if ([CCUtility getCertificateError:account]) {
  633. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  634. }
  635. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  636. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  637. [communication setUserAgent:[CCUtility getUserAgent]];
  638. [communication readMarkComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  639. completion(account, nil, 0);
  640. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  641. NSString *message;
  642. NSInteger errorCode = response.statusCode;
  643. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  644. errorCode = error.code;
  645. // Error
  646. if (errorCode == 503)
  647. message = NSLocalizedString(@"_server_error_retry_", nil);
  648. else
  649. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  650. completion(account, message, errorCode);
  651. }];
  652. }
  653. - (void)deleteCommentsWithAccount:(NSString *)account fileId:(NSString *)fileId messageID:(NSString *)messageID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  654. {
  655. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  656. if (tableAccount == nil) {
  657. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  658. } else if ([CCUtility getPassword:account].length == 0) {
  659. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  660. } else if ([CCUtility getCertificateError:account]) {
  661. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  662. }
  663. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  664. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  665. [communication setUserAgent:[CCUtility getUserAgent]];
  666. [communication deleteComments:[NSString stringWithFormat:@"%@%@", tableAccount.url, k_dav] fileId:fileId messageID:messageID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  667. completion(account, nil, 0);
  668. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  669. NSString *message;
  670. NSInteger errorCode = response.statusCode;
  671. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  672. errorCode = error.code;
  673. // Error
  674. if (errorCode == 503)
  675. message = NSLocalizedString(@"_server_error_retry_", nil);
  676. else
  677. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  678. completion(account, message, errorCode);
  679. }];
  680. }
  681. #pragma --------------------------------------------------------------------------------------------
  682. #pragma mark ===== Third Parts =====
  683. #pragma --------------------------------------------------------------------------------------------
  684. - (void)getHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  685. {
  686. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  687. if (tableAccount == nil) {
  688. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  689. } else if ([CCUtility getPassword:account].length == 0) {
  690. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  691. } else if ([CCUtility getCertificateError:account]) {
  692. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  693. }
  694. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  695. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  696. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  697. [communication setUserAgent:[CCUtility getUserAgent]];
  698. [communication getHCUserProfile:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  699. completion(account, userProfile, nil, 0);
  700. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  701. NSString *message;
  702. NSInteger errorCode = response.statusCode;
  703. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  704. errorCode = error.code;
  705. // Server Unauthorized
  706. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  707. #ifndef EXTENSION
  708. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  709. #endif
  710. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  711. [CCUtility setCertificateError:account error:YES];
  712. }
  713. // Error
  714. if (errorCode == 503)
  715. message = NSLocalizedString(@"_server_error_retry_", nil);
  716. else
  717. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  718. completion(account, nil,message, errorCode);
  719. }];
  720. }
  721. - (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
  722. {
  723. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  724. if (tableAccount == nil) {
  725. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  726. } else if ([CCUtility getPassword:account].length == 0) {
  727. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  728. } else if ([CCUtility getCertificateError:account]) {
  729. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  730. }
  731. // Create JSON
  732. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  733. if (address) [dataDic setValue:address forKey:@"address"];
  734. if (businesssize) {
  735. if ([businesssize isEqualToString:@"1-4"]) { [dataDic setValue:[NSNumber numberWithInt:1] forKey:@"businesssize"]; }
  736. else if ([businesssize isEqualToString:@"5-9"]) { [dataDic setValue:[NSNumber numberWithInt:5] forKey:@"businesssize"]; }
  737. else if ([businesssize isEqualToString:@"10-19"]) { [dataDic setValue:[NSNumber numberWithInt:10] forKey:@"businesssize"]; }
  738. else if ([businesssize isEqualToString:@"20-49"]) { [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"businesssize"]; }
  739. else if ([businesssize isEqualToString:@"50-99"]) { [dataDic setValue:[NSNumber numberWithInt:50] forKey:@"businesssize"]; }
  740. else if ([businesssize isEqualToString:@"100-249"]) { [dataDic setValue:[NSNumber numberWithInt:100] forKey:@"businesssize"]; }
  741. else if ([businesssize isEqualToString:@"250-499"]) { [dataDic setValue:[NSNumber numberWithInt:250] forKey:@"businesssize"]; }
  742. else if ([businesssize isEqualToString:@"500-999"]) { [dataDic setValue:[NSNumber numberWithInt:500] forKey:@"businesssize"]; }
  743. else if ([businesssize isEqualToString:@"1000+"]) { [dataDic setValue:[NSNumber numberWithInt:1000] forKey:@"businesssize"]; }
  744. }
  745. if (businesstype) [dataDic setValue:businesstype forKey:@"businesstype"];
  746. if (city) [dataDic setValue:city forKey:@"city"];
  747. if (company) [dataDic setValue:company forKey:@"company"];
  748. if (country) [dataDic setValue:country forKey:@"country"];
  749. if (displayname) [dataDic setValue:displayname forKey:@"displayname"];
  750. if (email) [dataDic setValue:email forKey:@"email"];
  751. if (phone) [dataDic setValue:phone forKey:@"phone"];
  752. if (role_) [dataDic setValue:role_ forKey:@"role"];
  753. if (twitter) [dataDic setValue:twitter forKey:@"twitter"];
  754. if (website) [dataDic setValue:website forKey:@"website"];
  755. if (zip) [dataDic setValue:zip forKey:@"zip"];
  756. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  757. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  758. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  759. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  760. [communication setUserAgent:[CCUtility getUserAgent]];
  761. [communication putHCUserProfile:serverPath data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  762. completion(account, nil, 0);
  763. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  764. NSString *message;
  765. NSInteger errorCode = response.statusCode;
  766. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  767. errorCode = error.code;
  768. // Server Unauthorized
  769. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  770. #ifndef EXTENSION
  771. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  772. #endif
  773. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  774. [CCUtility setCertificateError:account error:YES];
  775. }
  776. // Error
  777. if (errorCode == 503)
  778. message = NSLocalizedString(@"_server_error_retry_", nil);
  779. else
  780. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  781. completion(account, message, errorCode);
  782. }];
  783. }
  784. - (void)getHCFeaturesWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, HCFeatures *features, NSString *message, NSInteger errorCode))completion
  785. {
  786. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  787. if (tableAccount == nil) {
  788. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  789. } else if ([CCUtility getPassword:account].length == 0) {
  790. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  791. } else if ([CCUtility getCertificateError:account]) {
  792. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  793. }
  794. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/features/%@", serverUrl, tableAccount.userID];
  795. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  796. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  797. [communication setUserAgent:[CCUtility getUserAgent]];
  798. [communication getHCFeatures:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer) {
  799. completion(account, features, nil, 0);
  800. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  801. NSString *message;
  802. NSInteger errorCode = response.statusCode;
  803. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  804. errorCode = error.code;
  805. // Server Unauthorized
  806. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  807. #ifndef EXTENSION
  808. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  809. #endif
  810. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  811. [CCUtility setCertificateError:account error:YES];
  812. }
  813. // Error
  814. if (errorCode == 503)
  815. message = NSLocalizedString(@"_server_error_retry_", nil);
  816. else
  817. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  818. completion(account, nil,message, errorCode);
  819. }];
  820. }
  821. #pragma --------------------------------------------------------------------------------------------
  822. #pragma mark ===== didReceiveChallenge =====
  823. #pragma --------------------------------------------------------------------------------------------
  824. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  825. {
  826. // The pinnning check
  827. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  828. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  829. } else {
  830. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  831. }
  832. }
  833. @end
  834. #pragma --------------------------------------------------------------------------------------------
  835. #pragma mark ===== OCURLSessionManager =====
  836. #pragma --------------------------------------------------------------------------------------------
  837. @implementation OCURLSessionManager
  838. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  839. {
  840. // The pinnning check
  841. if ([[NCNetworking sharedInstance] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  842. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  843. } else {
  844. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  845. }
  846. }
  847. @end