You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.php 17KB

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