api.php 17 KB

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