local.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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\Files_Sharing\API;
  27. use OC\HintException;
  28. class Local {
  29. /**
  30. * get all shares
  31. *
  32. * @param array $params option 'file' to limit the result to a specific file/folder
  33. * @return \OC_OCS_Result share information
  34. */
  35. public static function getAllShares($params) {
  36. if (isset($_GET['shared_with_me']) && $_GET['shared_with_me'] !== 'false') {
  37. return self::getFilesSharedWithMe();
  38. }
  39. // if a file is specified, get the share for this file
  40. if (isset($_GET['path'])) {
  41. if ( isset($_GET['reshares']) && $_GET['reshares'] !== 'false' ) {
  42. $reshares = true;
  43. } else {
  44. $reshares = false;
  45. }
  46. if (isset($_GET['subfiles']) && $_GET['subfiles'] !== 'false') {
  47. return self::getSharesFromFolder($_GET['path']);
  48. }
  49. return self::collectShares(self::getFileId($_GET['path']),
  50. self::getItemType($_GET['path']),
  51. false,
  52. $_GET['path'],
  53. $reshares);
  54. }
  55. $shares = \OCP\Share::getItemShared('file', null);
  56. if ($shares === false) {
  57. return new \OC_OCS_Result(null, 404, 'could not get shares');
  58. } else {
  59. foreach ($shares as &$share) {
  60. if ($share['item_type'] === 'file' && isset($share['path'])) {
  61. $share['mimetype'] = \OC_Helper::getFileNameMimeType($share['path']);
  62. if (\OC::$server->getPreviewManager()->isMimeSupported($share['mimetype'])) {
  63. $share['isPreviewAvailable'] = true;
  64. }
  65. }
  66. if (!is_null($share['token'])) {
  67. $share['url'] = \OC::$server->getURLGenerator()->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share['token']]);
  68. }
  69. }
  70. return new \OC_OCS_Result($shares);
  71. }
  72. }
  73. /**
  74. * get share information for a given share
  75. *
  76. * @param array $params which contains a 'id'
  77. * @return \OC_OCS_Result share information
  78. */
  79. public static function getShare($params) {
  80. $s = self::getShareFromId($params['id']);
  81. return self::collectShares($s['file_source'], $s['item_type'], true, null, false, (int)$params['id']);
  82. }
  83. /**
  84. * collect all share information, either of a specific share or all
  85. * shares for a given path
  86. *
  87. * @param string $itemSource
  88. * @param string $itemType
  89. * @param bool $getSpecificShare
  90. * @param string $path
  91. * @param bool $reshares
  92. * @param int $id
  93. *
  94. * @return \OC_OCS_Result
  95. */
  96. private static function collectShares($itemSource, $itemType, $getSpecificShare = false, $path = null, $reshares = false, $id = null) {
  97. if ($itemSource !== null) {
  98. $shares = \OCP\Share::getItemShared($itemType, $itemSource);
  99. $receivedFrom = \OCP\Share::getItemSharedWithBySource($itemType, $itemSource);
  100. // if a specific share was specified only return this one
  101. if ($getSpecificShare === true) {
  102. foreach ($shares as $share) {
  103. if ($share['id'] === $id) {
  104. $shares = array('element' => $share);
  105. break;
  106. }
  107. }
  108. } else {
  109. foreach ($shares as $key => $share) {
  110. $shares[$key]['path'] = $path;
  111. }
  112. }
  113. // include also reshares in the lists. This means that the result
  114. // will contain every user with access to the file.
  115. if ($reshares === true) {
  116. $shares = self::addReshares($shares, $itemSource);
  117. }
  118. if ($receivedFrom) {
  119. foreach ($shares as $key => $share) {
  120. $shares[$key]['received_from'] = $receivedFrom['uid_owner'];
  121. $shares[$key]['received_from_displayname'] = \OCP\User::getDisplayName($receivedFrom['uid_owner']);
  122. }
  123. }
  124. } else {
  125. $shares = null;
  126. }
  127. if ($shares === null || empty($shares)) {
  128. return new \OC_OCS_Result(null, 404, 'share doesn\'t exist');
  129. } else {
  130. foreach ($shares as &$share) {
  131. if (!is_null($share['token'])) {
  132. $share['url'] = \OC::$server->getURLGenerator()->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share['token']]);
  133. }
  134. }
  135. return new \OC_OCS_Result($shares);
  136. }
  137. }
  138. /**
  139. * add reshares to a array of shares
  140. * @param array $shares array of shares
  141. * @param int $itemSource item source ID
  142. * @return array new shares array which includes reshares
  143. */
  144. private static function addReshares($shares, $itemSource) {
  145. // if there are no shares than there are also no reshares
  146. $firstShare = reset($shares);
  147. if ($firstShare) {
  148. $path = $firstShare['path'];
  149. } else {
  150. return $shares;
  151. }
  152. $select = '`*PREFIX*share`.`id`, `item_type`, `*PREFIX*share`.`parent`, `share_type`, `share_with`, `file_source`, `path` , `*PREFIX*share`.`permissions`, `stime`, `expiration`, `token`, `storage`, `mail_send`, `mail_send`';
  153. $getReshares = \OCP\DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` WHERE `*PREFIX*share`.`file_source` = ? AND `*PREFIX*share`.`item_type` IN (\'file\', \'folder\') AND `uid_owner` != ?');
  154. $reshares = $getReshares->execute(array($itemSource, \OCP\User::getUser()))->fetchAll();
  155. foreach ($reshares as $key => $reshare) {
  156. if (isset($reshare['share_with']) && $reshare['share_with'] !== '') {
  157. $reshares[$key]['share_with_displayname'] = \OCP\User::getDisplayName($reshare['share_with']);
  158. }
  159. // add correct path to the result
  160. $reshares[$key]['path'] = $path;
  161. }
  162. return array_merge($shares, $reshares);
  163. }
  164. /**
  165. * get share from all files in a given folder (non-recursive)
  166. * @param string $path
  167. * @return \OC_OCS_Result
  168. */
  169. private static function getSharesFromFolder($path) {
  170. $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files');
  171. if(!$view->is_dir($path)) {
  172. return new \OC_OCS_Result(null, 400, "not a directory");
  173. }
  174. $content = $view->getDirectoryContent($path);
  175. $result = array();
  176. foreach ($content as $file) {
  177. // workaround because folders are named 'dir' in this context
  178. $itemType = $file['type'] === 'file' ? 'file' : 'folder';
  179. $share = \OCP\Share::getItemShared($itemType, $file['fileid']);
  180. if($share) {
  181. $receivedFrom = \OCP\Share::getItemSharedWithBySource($itemType, $file['fileid']);
  182. reset($share);
  183. $key = key($share);
  184. if ($receivedFrom) {
  185. $share[$key]['received_from'] = $receivedFrom['uid_owner'];
  186. $share[$key]['received_from_displayname'] = \OCP\User::getDisplayName($receivedFrom['uid_owner']);
  187. }
  188. $result = array_merge($result, $share);
  189. }
  190. }
  191. return new \OC_OCS_Result($result);
  192. }
  193. /**
  194. * get files shared with the user
  195. * @return \OC_OCS_Result
  196. */
  197. private static function getFilesSharedWithMe() {
  198. try {
  199. $shares = \OCP\Share::getItemsSharedWith('file');
  200. foreach ($shares as &$share) {
  201. if ($share['item_type'] === 'file') {
  202. $share['mimetype'] = \OC_Helper::getFileNameMimeType($share['file_target']);
  203. if (\OC::$server->getPreviewManager()->isMimeSupported($share['mimetype'])) {
  204. $share['isPreviewAvailable'] = true;
  205. }
  206. }
  207. }
  208. $result = new \OC_OCS_Result($shares);
  209. } catch (\Exception $e) {
  210. $result = new \OC_OCS_Result(null, 403, $e->getMessage());
  211. }
  212. return $result;
  213. }
  214. /**
  215. * create a new share
  216. * @param array $params
  217. * @return \OC_OCS_Result
  218. */
  219. public static function createShare($params) {
  220. $path = isset($_POST['path']) ? $_POST['path'] : null;
  221. if($path === null) {
  222. return new \OC_OCS_Result(null, 400, "please specify a file or folder path");
  223. }
  224. $itemSource = self::getFileId($path);
  225. $itemSourceName = $itemSource;
  226. $itemType = self::getItemType($path);
  227. if($itemSource === null) {
  228. return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist.");
  229. }
  230. $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null;
  231. $shareType = isset($_POST['shareType']) ? (int)$_POST['shareType'] : null;
  232. switch($shareType) {
  233. case \OCP\Share::SHARE_TYPE_REMOTE:
  234. $shareWith = rtrim($shareWith, '/');
  235. $itemSourceName = basename($path);
  236. case \OCP\Share::SHARE_TYPE_USER:
  237. case \OCP\Share::SHARE_TYPE_GROUP:
  238. $permissions = isset($_POST['permissions']) ? (int)$_POST['permissions'] : 31;
  239. break;
  240. case \OCP\Share::SHARE_TYPE_LINK:
  241. //allow password protection
  242. $shareWith = isset($_POST['password']) ? $_POST['password'] : null;
  243. //check public link share
  244. $publicUploadEnabled = \OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_public_upload', 'yes');
  245. if(isset($_POST['publicUpload']) && $publicUploadEnabled !== 'yes') {
  246. return new \OC_OCS_Result(null, 403, "public upload disabled by the administrator");
  247. }
  248. $publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'false';
  249. // read, create, update (7) if public upload is enabled or
  250. // read (1) if public upload is disabled
  251. $permissions = $publicUpload === 'true' ? 7 : 1;
  252. break;
  253. default:
  254. return new \OC_OCS_Result(null, 400, "unknown share type");
  255. }
  256. if (($permissions & \OCP\Constants::PERMISSION_READ) === 0) {
  257. return new \OC_OCS_Result(null, 400, 'invalid permissions');
  258. }
  259. try {
  260. $token = \OCP\Share::shareItem(
  261. $itemType,
  262. $itemSource,
  263. $shareType,
  264. $shareWith,
  265. $permissions,
  266. $itemSourceName
  267. );
  268. } catch (HintException $e) {
  269. return new \OC_OCS_Result(null, 400, $e->getHint());
  270. } catch (\Exception $e) {
  271. return new \OC_OCS_Result(null, 403, $e->getMessage());
  272. }
  273. if ($token) {
  274. $data = array();
  275. $data['id'] = 'unknown';
  276. $shares = \OCP\Share::getItemShared($itemType, $itemSource);
  277. if(is_string($token)) { //public link share
  278. foreach ($shares as $share) {
  279. if ($share['token'] === $token) {
  280. $data['id'] = $share['id'];
  281. break;
  282. }
  283. }
  284. $data['url'] = \OC::$server->getURLGenerator()->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]);
  285. $data['token'] = $token;
  286. } else {
  287. foreach ($shares as $share) {
  288. if ($share['share_with'] === $shareWith && $share['share_type'] === $shareType) {
  289. $data['id'] = $share['id'];
  290. break;
  291. }
  292. }
  293. }
  294. return new \OC_OCS_Result($data);
  295. } else {
  296. return new \OC_OCS_Result(null, 404, "couldn't share file");
  297. }
  298. }
  299. /**
  300. * update shares, e.g. password, permissions, etc
  301. * @param array $params shareId 'id' and the parameter we want to update
  302. * currently supported: permissions, password, publicUpload
  303. * @return \OC_OCS_Result
  304. */
  305. public static function updateShare($params) {
  306. $share = self::getShareFromId($params['id']);
  307. if(!isset($share['file_source'])) {
  308. return new \OC_OCS_Result(null, 404, "wrong share Id, share doesn't exist.");
  309. }
  310. try {
  311. if(isset($params['_put']['permissions'])) {
  312. return self::updatePermissions($share, $params);
  313. } elseif (isset($params['_put']['password'])) {
  314. return self::updatePassword($params['id'], (int)$share['share_type'], $params['_put']['password']);
  315. } elseif (isset($params['_put']['publicUpload'])) {
  316. return self::updatePublicUpload($share, $params);
  317. } elseif (isset($params['_put']['expireDate'])) {
  318. return self::updateExpireDate($share, $params);
  319. }
  320. } catch (\Exception $e) {
  321. return new \OC_OCS_Result(null, 400, $e->getMessage());
  322. }
  323. return new \OC_OCS_Result(null, 400, "Wrong or no update parameter given");
  324. }
  325. /**
  326. * update permissions for a share
  327. * @param array $share information about the share
  328. * @param array $params contains 'permissions'
  329. * @return \OC_OCS_Result
  330. */
  331. private static function updatePermissions($share, $params) {
  332. $itemSource = $share['item_source'];
  333. $itemType = $share['item_type'];
  334. $shareWith = $share['share_with'];
  335. $shareType = $share['share_type'];
  336. $permissions = isset($params['_put']['permissions']) ? (int)$params['_put']['permissions'] : null;
  337. $publicUploadStatus = \OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_public_upload', 'yes');
  338. $publicUploadEnabled = ($publicUploadStatus === 'yes') ? true : false;
  339. // only change permissions for public shares if public upload is enabled
  340. // and we want to set permissions to 1 (read only) or 7 (allow upload)
  341. if ( (int)$shareType === \OCP\Share::SHARE_TYPE_LINK ) {
  342. if ($publicUploadEnabled === false || ($permissions !== 7 && $permissions !== 1)) {
  343. return new \OC_OCS_Result(null, 400, "can't change permission for public link share");
  344. }
  345. }
  346. if (($permissions & \OCP\Constants::PERMISSION_READ) === 0) {
  347. return new \OC_OCS_Result(null, 400, 'invalid permissions');
  348. }
  349. try {
  350. $return = \OCP\Share::setPermissions(
  351. $itemType,
  352. $itemSource,
  353. $shareType,
  354. $shareWith,
  355. $permissions
  356. );
  357. } catch (\Exception $e) {
  358. return new \OC_OCS_Result(null, 404, $e->getMessage());
  359. }
  360. if ($return) {
  361. return new \OC_OCS_Result();
  362. } else {
  363. return new \OC_OCS_Result(null, 404, "couldn't set permissions");
  364. }
  365. }
  366. /**
  367. * enable/disable public upload
  368. * @param array $share information about the share
  369. * @param array $params contains 'publicUpload' which can be 'yes' or 'no'
  370. * @return \OC_OCS_Result
  371. */
  372. private static function updatePublicUpload($share, $params) {
  373. $publicUploadEnabled = \OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_public_upload', 'yes');
  374. if($publicUploadEnabled !== 'yes') {
  375. return new \OC_OCS_Result(null, 403, "public upload disabled by the administrator");
  376. }
  377. if ($share['item_type'] !== 'folder' ||
  378. (int)$share['share_type'] !== \OCP\Share::SHARE_TYPE_LINK ) {
  379. return new \OC_OCS_Result(null, 400, "public upload is only possible for public shared folders");
  380. }
  381. // read, create, update (7) if public upload is enabled or
  382. // read (1) if public upload is disabled
  383. $params['_put']['permissions'] = $params['_put']['publicUpload'] === 'true' ? 7 : 1;
  384. return self::updatePermissions($share, $params);
  385. }
  386. /**
  387. * set expire date for public link share
  388. * @param array $share information about the share
  389. * @param array $params contains 'expireDate' which needs to be a well formated date string, e.g DD-MM-YYYY
  390. * @return \OC_OCS_Result
  391. */
  392. private static function updateExpireDate($share, $params) {
  393. // only public links can have a expire date
  394. if ((int)$share['share_type'] !== \OCP\Share::SHARE_TYPE_LINK ) {
  395. return new \OC_OCS_Result(null, 400, "expire date only exists for public link shares");
  396. }
  397. try {
  398. $expireDateSet = \OCP\Share::setExpirationDate($share['item_type'], $share['item_source'], $params['_put']['expireDate'], (int)$share['stime']);
  399. $result = ($expireDateSet) ? new \OC_OCS_Result() : new \OC_OCS_Result(null, 404, "couldn't set expire date");
  400. } catch (\Exception $e) {
  401. $result = new \OC_OCS_Result(null, 404, $e->getMessage());
  402. }
  403. return $result;
  404. }
  405. /**
  406. * update password for public link share
  407. * @param int $shareId
  408. * @param int $shareType
  409. * @param string $password
  410. * @return \OC_OCS_Result
  411. */
  412. private static function updatePassword($shareId, $shareType, $password) {
  413. if($shareType !== \OCP\Share::SHARE_TYPE_LINK) {
  414. return new \OC_OCS_Result(null, 400, "password protection is only supported for public shares");
  415. }
  416. if($password === '') {
  417. $password = null;
  418. }
  419. try {
  420. $result = \OCP\Share::setPassword($shareId, $password);
  421. } catch (\Exception $e) {
  422. return new \OC_OCS_Result(null, 403, $e->getMessage());
  423. }
  424. if($result) {
  425. return new \OC_OCS_Result();
  426. }
  427. return new \OC_OCS_Result(null, 404, "couldn't set password");
  428. }
  429. /**
  430. * unshare a file/folder
  431. * @param array $params contains the shareID 'id' which should be unshared
  432. * @return \OC_OCS_Result
  433. */
  434. public static function deleteShare($params) {
  435. $share = self::getShareFromId($params['id']);
  436. $fileSource = isset($share['file_source']) ? $share['file_source'] : null;
  437. $itemType = isset($share['item_type']) ? $share['item_type'] : null;;
  438. if($fileSource === null) {
  439. return new \OC_OCS_Result(null, 404, "wrong share ID, share doesn't exist.");
  440. }
  441. $shareWith = isset($share['share_with']) ? $share['share_with'] : null;
  442. $shareType = isset($share['share_type']) ? (int)$share['share_type'] : null;
  443. if( $shareType === \OCP\Share::SHARE_TYPE_LINK) {
  444. $shareWith = null;
  445. }
  446. try {
  447. $return = \OCP\Share::unshare(
  448. $itemType,
  449. $fileSource,
  450. $shareType,
  451. $shareWith);
  452. } catch (\Exception $e) {
  453. return new \OC_OCS_Result(null, 404, $e->getMessage());
  454. }
  455. if ($return) {
  456. return new \OC_OCS_Result();
  457. } else {
  458. $msg = "Unshare Failed";
  459. return new \OC_OCS_Result(null, 404, $msg);
  460. }
  461. }
  462. /**
  463. * get file ID from a given path
  464. * @param string $path
  465. * @return string fileID or null
  466. */
  467. private static function getFileId($path) {
  468. $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files');
  469. $fileId = null;
  470. $fileInfo = $view->getFileInfo($path);
  471. if ($fileInfo) {
  472. $fileId = $fileInfo['fileid'];
  473. }
  474. return $fileId;
  475. }
  476. /**
  477. * get itemType
  478. * @param string $path
  479. * @return string type 'file', 'folder' or null of file/folder doesn't exists
  480. */
  481. private static function getItemType($path) {
  482. $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files');
  483. $itemType = null;
  484. if ($view->is_dir($path)) {
  485. $itemType = "folder";
  486. } elseif ($view->is_file($path)) {
  487. $itemType = "file";
  488. }
  489. return $itemType;
  490. }
  491. /**
  492. * get some information from a given share
  493. * @param int $shareID
  494. * @return array with: item_source, share_type, share_with, item_type, permissions
  495. */
  496. private static function getShareFromId($shareID) {
  497. $sql = 'SELECT `file_source`, `item_source`, `share_type`, `share_with`, `item_type`, `permissions`, `stime` FROM `*PREFIX*share` WHERE `id` = ?';
  498. $args = array($shareID);
  499. $query = \OCP\DB::prepare($sql);
  500. $result = $query->execute($args);
  501. if (\OCP\DB::isError($result)) {
  502. \OCP\Util::writeLog('files_sharing', \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
  503. return null;
  504. }
  505. if ($share = $result->fetchRow()) {
  506. return $share;
  507. }
  508. return null;
  509. }
  510. }