server2server.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @copyright Copyright (c) 2015, 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\Files_Sharing\API;
  23. use OCA\Files_Sharing\Activity;
  24. class Server2Server {
  25. /**
  26. * create a new share
  27. *
  28. * @param array $params
  29. * @return \OC_OCS_Result
  30. */
  31. public function createShare($params) {
  32. if (!$this->isS2SEnabled(true)) {
  33. return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing');
  34. }
  35. $remote = isset($_POST['remote']) ? $_POST['remote'] : null;
  36. $token = isset($_POST['token']) ? $_POST['token'] : null;
  37. $name = isset($_POST['name']) ? $_POST['name'] : null;
  38. $owner = isset($_POST['owner']) ? $_POST['owner'] : null;
  39. $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null;
  40. $remoteId = isset($_POST['remoteId']) ? (int)$_POST['remoteId'] : null;
  41. if ($remote && $token && $name && $owner && $remoteId && $shareWith) {
  42. if(!\OCP\Util::isValidFileName($name)) {
  43. return new \OC_OCS_Result(null, 400, 'The mountpoint name contains invalid characters.');
  44. }
  45. \OCP\Util::writeLog('files_sharing', 'shareWith before, ' . $shareWith, \OCP\Util::DEBUG);
  46. \OCP\Util::emitHook(
  47. '\OCA\Files_Sharing\API\Server2Server',
  48. 'preLoginNameUsedAsUserName',
  49. array('uid' => &$shareWith)
  50. );
  51. \OCP\Util::writeLog('files_sharing', 'shareWith after, ' . $shareWith, \OCP\Util::DEBUG);
  52. if (!\OCP\User::userExists($shareWith)) {
  53. return new \OC_OCS_Result(null, 400, 'User does not exists');
  54. }
  55. \OC_Util::setupFS($shareWith);
  56. $externalManager = new \OCA\Files_Sharing\External\Manager(
  57. \OC::$server->getDatabaseConnection(),
  58. \OC\Files\Filesystem::getMountManager(),
  59. \OC\Files\Filesystem::getLoader(),
  60. \OC::$server->getHTTPHelper(),
  61. \OC::$server->getNotificationManager(),
  62. $shareWith
  63. );
  64. try {
  65. $externalManager->addShare($remote, $token, '', $name, $owner, false, $shareWith, $remoteId);
  66. $user = $owner . '@' . $this->cleanupRemote($remote);
  67. \OC::$server->getActivityManager()->publishActivity(
  68. Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_RECEIVED, array($user, trim($name, '/')), '', array(),
  69. '', '', $shareWith, Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_LOW);
  70. /**
  71. * FIXME
  72. $urlGenerator = \OC::$server->getURLGenerator();
  73. $notificationManager = \OC::$server->getNotificationManager();
  74. $notification = $notificationManager->createNotification();
  75. $notification->setApp('files_sharing')
  76. ->setUser($shareWith)
  77. ->setTimestamp(time())
  78. ->setObject('remote_share', $remoteId)
  79. ->setSubject('remote_share', [$user, trim($name, '/')]);
  80. $declineAction = $notification->createAction();
  81. $declineAction->setLabel('decline')
  82. ->setLink($urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/remote_shares/' . $remoteId), 'DELETE');
  83. $notification->addAction($declineAction);
  84. $acceptAction = $notification->createAction();
  85. $acceptAction->setLabel('accept')
  86. ->setLink($urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/remote_shares/' . $remoteId), 'POST');
  87. $notification->addAction($acceptAction);
  88. $notificationManager->notify($notification);
  89. */
  90. return new \OC_OCS_Result();
  91. } catch (\Exception $e) {
  92. \OCP\Util::writeLog('files_sharing', 'server can not add remote share, ' . $e->getMessage(), \OCP\Util::ERROR);
  93. return new \OC_OCS_Result(null, 500, 'internal server error, was not able to add share from ' . $remote);
  94. }
  95. }
  96. return new \OC_OCS_Result(null, 400, 'server can not add remote share, missing parameter');
  97. }
  98. /**
  99. * accept server-to-server share
  100. *
  101. * @param array $params
  102. * @return \OC_OCS_Result
  103. */
  104. public function acceptShare($params) {
  105. if (!$this->isS2SEnabled()) {
  106. return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing');
  107. }
  108. $id = $params['id'];
  109. $token = isset($_POST['token']) ? $_POST['token'] : null;
  110. $share = self::getShare($id, $token);
  111. if ($share) {
  112. list($file, $link) = self::getFile($share['uid_owner'], $share['file_source']);
  113. $event = \OC::$server->getActivityManager()->generateEvent();
  114. $event->setApp(Activity::FILES_SHARING_APP)
  115. ->setType(Activity::TYPE_REMOTE_SHARE)
  116. ->setAffectedUser($share['uid_owner'])
  117. ->setSubject(Activity::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share['share_with'], basename($file)])
  118. ->setObject('files', $share['file_source'], $file)
  119. ->setLink($link);
  120. \OC::$server->getActivityManager()->publish($event);
  121. }
  122. return new \OC_OCS_Result();
  123. }
  124. /**
  125. * decline server-to-server share
  126. *
  127. * @param array $params
  128. * @return \OC_OCS_Result
  129. */
  130. public function declineShare($params) {
  131. if (!$this->isS2SEnabled()) {
  132. return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing');
  133. }
  134. $id = $params['id'];
  135. $token = isset($_POST['token']) ? $_POST['token'] : null;
  136. $share = $this->getShare($id, $token);
  137. if ($share) {
  138. // userId must be set to the user who unshares
  139. \OCP\Share::unshare($share['item_type'], $share['item_source'], $share['share_type'], null, $share['uid_owner']);
  140. list($file, $link) = $this->getFile($share['uid_owner'], $share['file_source']);
  141. $event = \OC::$server->getActivityManager()->generateEvent();
  142. $event->setApp(Activity::FILES_SHARING_APP)
  143. ->setType(Activity::TYPE_REMOTE_SHARE)
  144. ->setAffectedUser($share['uid_owner'])
  145. ->setSubject(Activity::SUBJECT_REMOTE_SHARE_DECLINED, [$share['share_with'], basename($file)])
  146. ->setObject('files', $share['file_source'], $file)
  147. ->setLink($link);
  148. \OC::$server->getActivityManager()->publish($event);
  149. }
  150. return new \OC_OCS_Result();
  151. }
  152. /**
  153. * remove server-to-server share if it was unshared by the owner
  154. *
  155. * @param array $params
  156. * @return \OC_OCS_Result
  157. */
  158. public function unshare($params) {
  159. if (!$this->isS2SEnabled()) {
  160. return new \OC_OCS_Result(null, 503, 'Server does not support federated cloud sharing');
  161. }
  162. $id = $params['id'];
  163. $token = isset($_POST['token']) ? $_POST['token'] : null;
  164. $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share_external` WHERE `remote_id` = ? AND `share_token` = ?');
  165. $query->execute(array($id, $token));
  166. $share = $query->fetchRow();
  167. if ($token && $id && !empty($share)) {
  168. $remote = $this->cleanupRemote($share['remote']);
  169. $owner = $share['owner'] . '@' . $remote;
  170. $mountpoint = $share['mountpoint'];
  171. $user = $share['user'];
  172. $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share_external` WHERE `remote_id` = ? AND `share_token` = ?');
  173. $query->execute(array($id, $token));
  174. if ($share['accepted']) {
  175. $path = trim($mountpoint, '/');
  176. } else {
  177. $path = trim($share['name'], '/');
  178. }
  179. \OC::$server->getActivityManager()->publishActivity(
  180. Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_UNSHARED, array($owner, $path), '', array(),
  181. '', '', $user, Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_MEDIUM);
  182. }
  183. return new \OC_OCS_Result();
  184. }
  185. private function cleanupRemote($remote) {
  186. $remote = substr($remote, strpos($remote, '://') + 3);
  187. return rtrim($remote, '/');
  188. }
  189. /**
  190. * get share
  191. *
  192. * @param int $id
  193. * @param string $token
  194. * @return array
  195. */
  196. private function getShare($id, $token) {
  197. $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ? AND `token` = ? AND `share_type` = ?');
  198. $query->execute(array($id, $token, \OCP\Share::SHARE_TYPE_REMOTE));
  199. $share = $query->fetchRow();
  200. return $share;
  201. }
  202. /**
  203. * get file
  204. *
  205. * @param string $user
  206. * @param int $fileSource
  207. * @return array with internal path of the file and a absolute link to it
  208. */
  209. private function getFile($user, $fileSource) {
  210. \OC_Util::setupFS($user);
  211. $file = \OC\Files\Filesystem::getPath($fileSource);
  212. $args = \OC\Files\Filesystem::is_dir($file) ? array('dir' => $file) : array('dir' => dirname($file), 'scrollto' => $file);
  213. $link = \OCP\Util::linkToAbsolute('files', 'index.php', $args);
  214. return array($file, $link);
  215. }
  216. /**
  217. * check if server-to-server sharing is enabled
  218. *
  219. * @param bool $incoming
  220. * @return bool
  221. */
  222. private function isS2SEnabled($incoming = false) {
  223. $result = \OCP\App::isEnabled('files_sharing');
  224. if ($incoming) {
  225. $result = $result && \OCA\Files_Sharing\Helper::isIncomingServer2serverShareEnabled();
  226. } else {
  227. $result = $result && \OCA\Files_Sharing\Helper::isOutgoingServer2serverShareEnabled();
  228. }
  229. return $result;
  230. }
  231. }