api.php 16 KB

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