Notifications.php 8.5 KB

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