Notifications.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * @author Björn Schießle <bjoern@schiessle.org>
  4. * @author Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\FederatedFileSharing;
  23. use OCP\AppFramework\Http;
  24. use OCP\BackgroundJob\IJobList;
  25. use OCP\Http\Client\IClientService;
  26. class Notifications {
  27. const RESPONSE_FORMAT = 'json'; // default response format for ocs calls
  28. /** @var AddressHandler */
  29. private $addressHandler;
  30. /** @var IClientService */
  31. private $httpClientService;
  32. /** @var DiscoveryManager */
  33. private $discoveryManager;
  34. /** @var IJobList */
  35. private $jobList;
  36. /**
  37. * @param AddressHandler $addressHandler
  38. * @param IClientService $httpClientService
  39. * @param DiscoveryManager $discoveryManager
  40. * @param IJobList $jobList
  41. */
  42. public function __construct(
  43. AddressHandler $addressHandler,
  44. IClientService $httpClientService,
  45. DiscoveryManager $discoveryManager,
  46. IJobList $jobList
  47. ) {
  48. $this->addressHandler = $addressHandler;
  49. $this->httpClientService = $httpClientService;
  50. $this->discoveryManager = $discoveryManager;
  51. $this->jobList = $jobList;
  52. }
  53. /**
  54. * send server-to-server share to remote server
  55. *
  56. * @param string $token
  57. * @param string $shareWith
  58. * @param string $name
  59. * @param int $remote_id
  60. * @param string $owner
  61. * @param string $ownerFederatedId
  62. * @param string $sharedBy
  63. * @param string $sharedByFederatedId
  64. * @return bool
  65. * @throws \OC\HintException
  66. * @throws \OC\ServerNotAvailableException
  67. */
  68. public function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner, $ownerFederatedId, $sharedBy, $sharedByFederatedId) {
  69. list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
  70. if ($user && $remote) {
  71. $url = $remote;
  72. $local = $this->addressHandler->generateRemoteURL();
  73. $fields = array(
  74. 'shareWith' => $user,
  75. 'token' => $token,
  76. 'name' => $name,
  77. 'remoteId' => $remote_id,
  78. 'owner' => $owner,
  79. 'ownerFederatedId' => $ownerFederatedId,
  80. 'sharedBy' => $sharedBy,
  81. 'sharedByFederatedId' => $sharedByFederatedId,
  82. 'remote' => $local,
  83. );
  84. $url = $this->addressHandler->removeProtocolFromUrl($url);
  85. $result = $this->tryHttpPostToShareEndpoint($url, '', $fields);
  86. $status = json_decode($result['result'], true);
  87. if ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)) {
  88. \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]);
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. /**
  95. * ask owner to re-share the file with the given user
  96. *
  97. * @param string $token
  98. * @param int $id remote Id
  99. * @param int $shareId internal share Id
  100. * @param string $remote remote address of the owner
  101. * @param string $shareWith
  102. * @param int $permission
  103. * @return bool
  104. * @throws \OC\HintException
  105. * @throws \OC\ServerNotAvailableException
  106. */
  107. public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission) {
  108. $fields = array(
  109. 'shareWith' => $shareWith,
  110. 'token' => $token,
  111. 'permission' => $permission,
  112. 'remoteId' => $shareId
  113. );
  114. $url = $this->addressHandler->removeProtocolFromUrl($remote);
  115. $result = $this->tryHttpPostToShareEndpoint(rtrim($url, '/'), '/' . $id . '/reshare', $fields);
  116. $status = json_decode($result['result'], true);
  117. $httpRequestSuccessful = $result['success'];
  118. $ocsCallSuccessful = $status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200;
  119. $validToken = isset($status['ocs']['data']['token']) && is_string($status['ocs']['data']['token']);
  120. $validRemoteId = isset($status['ocs']['data']['remoteId']);
  121. if ($httpRequestSuccessful && $ocsCallSuccessful && $validToken && $validRemoteId) {
  122. return [
  123. $status['ocs']['data']['token'],
  124. (int)$status['ocs']['data']['remoteId']
  125. ];
  126. }
  127. return false;
  128. }
  129. /**
  130. * send server-to-server unshare to remote server
  131. *
  132. * @param string $remote url
  133. * @param int $id share id
  134. * @param string $token
  135. * @return bool
  136. */
  137. public function sendRemoteUnShare($remote, $id, $token) {
  138. $this->sendUpdateToRemote($remote, $id, $token, 'unshare');
  139. }
  140. /**
  141. * send server-to-server unshare to remote server
  142. *
  143. * @param string $remote url
  144. * @param int $id share id
  145. * @param string $token
  146. * @return bool
  147. */
  148. public function sendRevokeShare($remote, $id, $token) {
  149. $this->sendUpdateToRemote($remote, $id, $token, 'revoke');
  150. }
  151. /**
  152. * send notification to remote server if the permissions was changed
  153. *
  154. * @param string $remote
  155. * @param int $remoteId
  156. * @param string $token
  157. * @param int $permissions
  158. * @return bool
  159. */
  160. public function sendPermissionChange($remote, $remoteId, $token, $permissions) {
  161. $this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]);
  162. }
  163. /**
  164. * forward accept reShare to remote server
  165. *
  166. * @param string $remote
  167. * @param int $remoteId
  168. * @param string $token
  169. */
  170. public function sendAcceptShare($remote, $remoteId, $token) {
  171. $this->sendUpdateToRemote($remote, $remoteId, $token, 'accept');
  172. }
  173. /**
  174. * forward decline reShare to remote server
  175. *
  176. * @param string $remote
  177. * @param int $remoteId
  178. * @param string $token
  179. */
  180. public function sendDeclineShare($remote, $remoteId, $token) {
  181. $this->sendUpdateToRemote($remote, $remoteId, $token, 'decline');
  182. }
  183. /**
  184. * inform remote server whether server-to-server share was accepted/declined
  185. *
  186. * @param string $remote
  187. * @param string $token
  188. * @param int $remoteId Share id on the remote host
  189. * @param string $action possible actions: accept, decline, unshare, revoke, permissions
  190. * @param array $data
  191. * @param int $try
  192. * @return boolean
  193. */
  194. public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) {
  195. $fields = array('token' => $token);
  196. foreach ($data as $key => $value) {
  197. $fields[$key] = $value;
  198. }
  199. $url = $this->addressHandler->removeProtocolFromUrl($remote);
  200. $result = $this->tryHttpPostToShareEndpoint(rtrim($url, '/'), '/' . $remoteId . '/' . $action, $fields);
  201. $status = json_decode($result['result'], true);
  202. if ($result['success'] &&
  203. ($status['ocs']['meta']['statuscode'] === 100 ||
  204. $status['ocs']['meta']['statuscode'] === 200
  205. )
  206. ) {
  207. return true;
  208. } elseif ($try === 0) {
  209. // only add new job on first try
  210. $this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob',
  211. [
  212. 'remote' => $remote,
  213. 'remoteId' => $remoteId,
  214. 'token' => $token,
  215. 'action' => $action,
  216. 'data' => json_encode($data),
  217. 'try' => $try,
  218. 'lastRun' => $this->getTimestamp()
  219. ]
  220. );
  221. }
  222. return false;
  223. }
  224. /**
  225. * return current timestamp
  226. *
  227. * @return int
  228. */
  229. protected function getTimestamp() {
  230. return time();
  231. }
  232. /**
  233. * try http post first with https and then with http as a fallback
  234. *
  235. * @param string $remoteDomain
  236. * @param string $urlSuffix
  237. * @param array $fields post parameters
  238. * @return array
  239. * @throws \Exception
  240. */
  241. protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) {
  242. $client = $this->httpClientService->newClient();
  243. $protocol = 'https://';
  244. $result = [
  245. 'success' => false,
  246. 'result' => '',
  247. ];
  248. $try = 0;
  249. while ($result['success'] === false && $try < 2) {
  250. $endpoint = $this->discoveryManager->getShareEndpoint($protocol . $remoteDomain);
  251. try {
  252. $response = $client->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, [
  253. 'body' => $fields,
  254. 'timeout' => 10,
  255. 'connect_timeout' => 10,
  256. ]);
  257. $result['result'] = $response->getBody();
  258. $result['success'] = true;
  259. break;
  260. } catch (\Exception $e) {
  261. // if flat re-sharing is not supported by the remote server
  262. // we re-throw the exception and fall back to the old behaviour.
  263. // (flat re-shares has been introduced in Nextcloud 9.1)
  264. if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
  265. throw $e;
  266. }
  267. $try++;
  268. $protocol = 'http://';
  269. }
  270. }
  271. return $result;
  272. }
  273. }