lib_share.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2011 Michael Gapczynski GapczynskiM@gmail.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. /**
  23. * This class manages shared items within the database.
  24. */
  25. class OC_Share {
  26. const WRITE = 1;
  27. const DELETE = 2;
  28. const UNSHARED = -1;
  29. const PUBLICLINK = "public";
  30. private $token;
  31. /**
  32. * Share an item, adds an entry into the database
  33. * @param $source The source location of the item
  34. * @param $uid_shared_with The user or group to share the item with
  35. * @param $permissions The permissions, use the constants WRITE and DELETE
  36. */
  37. public function __construct($source, $uid_shared_with, $permissions) {
  38. $uid_owner = OCP\USER::getUser();
  39. $query = OCP\DB::prepare('INSERT INTO `*PREFIX*sharing` VALUES(?,?,?,?,?)');
  40. // Check if this is a reshare and use the original source
  41. if ($result = OC_Share::getSource($source)) {
  42. $source = $result;
  43. }
  44. if ($uid_shared_with == self::PUBLICLINK) {
  45. $token = sha1("$uid_shared_with-$source");
  46. $query->execute(array($uid_owner, self::PUBLICLINK, $source, $token, $permissions));
  47. $this->token = $token;
  48. } else {
  49. if (OC_Group::groupExists($uid_shared_with)) {
  50. $gid = $uid_shared_with;
  51. $uid_shared_with = OC_Group::usersInGroup($gid);
  52. // Remove the owner from the list of users in the group
  53. $uid_shared_with = array_diff($uid_shared_with, array($uid_owner));
  54. } else if (OCP\User::userExists($uid_shared_with)) {
  55. if(OCP\Config::getAppValue('files_sharing', 'allowSharingWithEveryone', 'no') == 'yes') {
  56. $gid = null;
  57. $uid_shared_with = array($uid_shared_with);
  58. } else {
  59. $userGroups = OC_Group::getUserGroups($uid_owner);
  60. // Check if the user is in one of the owner's groups
  61. foreach ($userGroups as $group) {
  62. if ($inGroup = OC_Group::inGroup($uid_shared_with, $group)) {
  63. $gid = null;
  64. $uid_shared_with = array($uid_shared_with);
  65. break;
  66. }
  67. }
  68. if (!$inGroup) {
  69. throw new Exception("You can't share with ".$uid_shared_with);
  70. }
  71. }
  72. } else {
  73. throw new Exception($uid_shared_with." is not a user");
  74. }
  75. foreach ($uid_shared_with as $uid) {
  76. // Check if this item is already shared with the user
  77. $checkSource = OCP\DB::prepare('SELECT `source` FROM `*PREFIX*sharing` WHERE `source` = ? AND `uid_shared_with` '.self::getUsersAndGroups($uid, false));
  78. $resultCheckSource = $checkSource->execute(array($source))->fetchAll();
  79. // TODO Check if the source is inside a folder
  80. if (count($resultCheckSource) > 0) {
  81. if (!isset($gid)) {
  82. throw new Exception("This item is already shared with ".$uid);
  83. } else {
  84. // Skip this user if sharing with a group
  85. continue;
  86. }
  87. }
  88. // Check if the target already exists for the user, if it does append a number to the name
  89. $sharedFolder = '/'.$uid.'/files/Shared';
  90. $target = $sharedFolder."/".basename($source);
  91. $checkTarget = OCP\DB::prepare('SELECT `source` FROM `*PREFIX*sharing` WHERE `target` = ? AND `uid_shared_with` '.self::getUsersAndGroups($uid, false).' LIMIT 1');
  92. $result = $checkTarget->execute(array($target))->fetchAll();
  93. if (count($result) > 0) {
  94. if ($pos = strrpos($target, ".")) {
  95. $name = substr($target, 0, $pos);
  96. $ext = substr($target, $pos);
  97. } else {
  98. $name = $target;
  99. $ext = "";
  100. }
  101. $counter = 1;
  102. while (count($result) > 0) {
  103. $target = $name."_".$counter.$ext;
  104. $result = $checkTarget->execute(array($target))->fetchAll();
  105. $counter++;
  106. }
  107. }
  108. // Update mtime of shared folder to invoke a file cache rescan
  109. $rootView=new OC_FilesystemView('/');
  110. if (!$rootView->is_dir($sharedFolder)) {
  111. if (!$rootView->is_dir('/'.$uid.'/files')) {
  112. OC_Util::tearDownFS();
  113. OC_Util::setupFS($uid);
  114. OC_Util::tearDownFS();
  115. }
  116. $rootView->mkdir($sharedFolder);
  117. }
  118. $rootView->touch($sharedFolder);
  119. if (isset($gid)) {
  120. $uid = $uid."@".$gid;
  121. }
  122. $query->execute(array($uid_owner, $uid, $source, $target, $permissions));
  123. }
  124. }
  125. }
  126. /**
  127. * Remove any duplicate or trailing '/' from the path
  128. * @return A clean path
  129. */
  130. private static function cleanPath($path) {
  131. $path = rtrim($path, "/");
  132. return preg_replace('{(/)\1+}', "/", $path);
  133. }
  134. /**
  135. * Generate a string to be used for searching for uid_shared_with that handles both users and groups
  136. * @param $uid (Optional) The uid to get the user groups for, a gid to get the users in a group, or if not set the current user
  137. * @return An IN operator as a string
  138. */
  139. private static function getUsersAndGroups($uid = null, $includePrivateLinks = true) {
  140. $in = " IN(";
  141. if (isset($uid) && OC_Group::groupExists($uid)) {
  142. $users = OC_Group::usersInGroup($uid);
  143. foreach ($users as $user) {
  144. // Add a comma only if the the current element isn't the last
  145. if ($user !== end($users)) {
  146. $in .= "'".$user."@".$uid."', ";
  147. } else {
  148. $in .= "'".$user."@".$uid."'";
  149. }
  150. }
  151. } else if (isset($uid)) {
  152. // TODO Check if this is necessary, only constructor needs it as IN. It would be better for other queries to just return =$uid
  153. $in .= "'".$uid."'";
  154. $groups = OC_Group::getUserGroups($uid);
  155. foreach ($groups as $group) {
  156. $in .= ", '".$uid."@".$group."'";
  157. }
  158. } else {
  159. $uid = OCP\USER::getUser();
  160. $in .= "'".$uid."'";
  161. $groups = OC_Group::getUserGroups($uid);
  162. foreach ($groups as $group) {
  163. $in .= ", '".$uid."@".$group."'";
  164. }
  165. }
  166. if ($includePrivateLinks) {
  167. $in .= ", '".self::PUBLICLINK."'";
  168. }
  169. $in .= ")";
  170. return $in;
  171. }
  172. private static function updateFolder($uid_shared_with) {
  173. if ($uid_shared_with != self::PUBLICLINK) {
  174. if (OC_Group::groupExists($uid_shared_with)) {
  175. $uid_shared_with = OC_Group::usersInGroup($uid_shared_with);
  176. // Remove the owner from the list of users in the group
  177. $uid_shared_with = array_diff($uid_shared_with, array(OCP\USER::getUser()));
  178. } else {
  179. $pos = strrpos($uid_shared_with, '@');
  180. if ($pos !== false && OC_Group::groupExists(substr($uid_shared_with, $pos + 1))) {
  181. $uid_shared_with = array(substr($uid_shared_with, 0, $pos));
  182. } else {
  183. $uid_shared_with = array($uid_shared_with);
  184. }
  185. }
  186. foreach ($uid_shared_with as $uid) {
  187. $sharedFolder = $uid.'/files/Shared';
  188. // Update mtime of shared folder to invoke a file cache rescan
  189. $rootView = new OC_FilesystemView('/');
  190. $rootView->touch($sharedFolder);
  191. }
  192. }
  193. }
  194. /**
  195. * Create a new entry in the database for a file inside a shared folder
  196. *
  197. * $oldTarget and $newTarget may be the same value. $oldTarget exists in case the file is being moved outside of the folder
  198. *
  199. * @param $oldTarget The current target location
  200. * @param $newTarget The new target location
  201. */
  202. public static function pullOutOfFolder($oldTarget, $newTarget) {
  203. $folders = self::getParentFolders($oldTarget);
  204. $source = $folders['source'].substr($oldTarget, strlen($folders['target']));
  205. $item = self::getItem($folders['target']);
  206. $query = OCP\DB::prepare('INSERT INTO `*PREFIX*sharing` VALUES(?,?,?,?,?)');
  207. $query->execute(array($item[0]['uid_owner'], OCP\USER::getUser(), $source, $newTarget, $item[0]['permissions']));
  208. }
  209. /**
  210. * Get the item with the specified target location
  211. * @param $target The target location of the item
  212. * @return An array with the item
  213. */
  214. public static function getItem($target) {
  215. $target = self::cleanPath($target);
  216. $query = OCP\DB::prepare('SELECT `uid_owner`, `source`, `permissions` FROM `*PREFIX*sharing` WHERE `target` = ? AND `uid_shared_with` = ? LIMIT 1');
  217. return $query->execute(array($target, OCP\USER::getUser()))->fetchAll();
  218. }
  219. /**
  220. * Get the item with the specified source location
  221. * @param $source The source location of the item
  222. * @return An array with the users and permissions the item is shared with
  223. */
  224. public static function getMySharedItem($source) {
  225. $source = self::cleanPath($source);
  226. $query = OCP\DB::prepare('SELECT `uid_shared_with`, `permissions` FROM `*PREFIX*sharing` WHERE `source` = ? AND `uid_owner` = ?');
  227. $result = $query->execute(array($source, OCP\USER::getUser()))->fetchAll();
  228. if (count($result) > 0) {
  229. return $result;
  230. } else if ($originalSource = self::getSource($source)) {
  231. return $query->execute(array($originalSource, OCP\USER::getUser()))->fetchAll();
  232. } else {
  233. return false;
  234. }
  235. }
  236. /**
  237. * Get all items the current user is sharing
  238. * @return An array with all items the user is sharing
  239. */
  240. public static function getMySharedItems() {
  241. $query = OCP\DB::prepare('SELECT `uid_shared_with`, `source`, `permissions` FROM `*PREFIX*sharing` WHERE `uid_owner` = ?');
  242. return $query->execute(array(OCP\USER::getUser()))->fetchAll();
  243. }
  244. /**
  245. * Get the items within a shared folder that have their own entry for the purpose of name, location, or permissions that differ from the folder itself
  246. *
  247. * Works for both target and source folders. Can be used for getting all items shared with you e.g. pass '/MTGap/files'
  248. *
  249. * @param $folder The folder of the items to look for
  250. * @return An array with all items in the database that are in the folder
  251. */
  252. public static function getItemsInFolder($folder) {
  253. $folder = self::cleanPath($folder);
  254. // Append '/' in order to filter out the folder itself if not already there
  255. if (substr($folder, -1) !== "/") {
  256. $folder .= "/";
  257. }
  258. $length = strlen($folder);
  259. $query = OCP\DB::prepare('SELECT `uid_owner`, `source`, `target`, `permissions` FROM `*PREFIX*sharing` WHERE SUBSTR(`source`, 1, ?) = ? OR SUBSTR(`target`, 1, ?) = ? AND `uid_shared_with` '.self::getUsersAndGroups());
  260. return $query->execute(array($length, $folder, $length, $folder))->fetchAll();
  261. }
  262. /**
  263. * Get the source and target parent folders of the specified target location
  264. * @param $target The target location of the item
  265. * @return An array with the keys 'source' and 'target' with the values of the source and target parent folders
  266. */
  267. public static function getParentFolders($target) {
  268. $target = self::cleanPath($target);
  269. $query = OCP\DB::prepare('SELECT `source` FROM `*PREFIX*sharing` WHERE `target` = ? AND `uid_shared_with` '.self::getUsersAndGroups().' LIMIT 1');
  270. // Prevent searching for user directory e.g. '/MTGap/files'
  271. $userDirectory = substr($target, 0, strpos($target, "files") + 5);
  272. $target = dirname($target);
  273. $result = array();
  274. while ($target != "" && $target != "/" && $target != "." && $target != $userDirectory && $target != "\\") {
  275. // Check if the parent directory of this target location is shared
  276. $result = $query->execute(array($target))->fetchAll();
  277. if (count($result) > 0) {
  278. break;
  279. }
  280. $target = dirname($target);
  281. }
  282. if (count($result) > 0) {
  283. // Return both the source folder and the target folder
  284. return array("source" => $result[0]['source'], "target" => $target);
  285. } else {
  286. return false;
  287. }
  288. }
  289. /**
  290. * Get the source location of the item at the specified target location
  291. * @param $target The target location of the item
  292. * @return Source location or false if target location is not valid
  293. */
  294. public static function getSource($target) {
  295. $target = self::cleanPath($target);
  296. $query = OCP\DB::prepare('SELECT `source` FROM `*PREFIX*sharing` WHERE `target` = ? AND `uid_shared_with` '.self::getUsersAndGroups().' LIMIT 1');
  297. $result = $query->execute(array($target))->fetchAll();
  298. if (count($result) > 0) {
  299. return $result[0]['source'];
  300. } else {
  301. $folders = self::getParentFolders($target);
  302. if ($folders == true) {
  303. return $folders['source'].substr($target, strlen($folders['target']));
  304. } else {
  305. return false;
  306. }
  307. }
  308. }
  309. public static function getTarget($source) {
  310. $source = self::cleanPath($source);
  311. $query = OCP\DB::prepare('SELECT `target` FROM `*PREFIX*sharing` WHERE `source` = ? AND `uid_owner` = ? LIMIT 1');
  312. $result = $query->execute(array($source, OCP\USER::getUser()))->fetchAll();
  313. if (count($result) > 0) {
  314. return $result[0]['target'];
  315. } else {
  316. // TODO Check in folders
  317. return false;
  318. }
  319. }
  320. /**
  321. * Get the user's permissions for the item at the specified target location
  322. * @param $target The target location of the item
  323. * @return The permissions, use bitwise operators to check against the constants WRITE and DELETE
  324. */
  325. public static function getPermissions($target) {
  326. $target = self::cleanPath($target);
  327. $query = OCP\DB::prepare('SELECT `permissions` FROM `*PREFIX*sharing` WHERE `target` = ? AND `uid_shared_with` '.self::getUsersAndGroups().' LIMIT 1');
  328. $result = $query->execute(array($target))->fetchAll();
  329. if (count($result) > 0) {
  330. return $result[0]['permissions'];
  331. } else {
  332. $folders = self::getParentFolders($target);
  333. if ($folders == true) {
  334. $result = $query->execute(array($folders['target']))->fetchAll();
  335. if (count($result) > 0) {
  336. return $result[0]['permissions'];
  337. }
  338. } else {
  339. OCP\Util::writeLog('files_sharing',"Not existing parent folder : ".$target,OCP\Util::ERROR);
  340. return false;
  341. }
  342. }
  343. }
  344. /**
  345. * Get the token for a public link
  346. * @return The token of the public link, a sha1 hash
  347. */
  348. public function getToken() {
  349. return $this->token;
  350. }
  351. /**
  352. * Get the token for a public link
  353. * @param $source The source location of the item
  354. * @return The token of the public link, a sha1 hash
  355. */
  356. public static function getTokenFromSource($source) {
  357. $query = OCP\DB::prepare('SELECT `target` FROM `*PREFIX*sharing` WHERE `source` = ? AND `uid_shared_with` = ? AND `uid_owner` = ? LIMIT 1');
  358. $result = $query->execute(array($source, self::PUBLICLINK, OCP\USER::getUser()))->fetchAll();
  359. if (count($result) > 0) {
  360. return $result[0]['target'];
  361. } else {
  362. return false;
  363. }
  364. }
  365. /**
  366. * Set the target location to a new value
  367. *
  368. * You must use the pullOutOfFolder() function to change the target location of a file inside a shared folder if the target location differs from the folder
  369. *
  370. * @param $oldTarget The current target location
  371. * @param $newTarget The new target location
  372. */
  373. public static function setTarget($oldTarget, $newTarget) {
  374. $oldTarget = self::cleanPath($oldTarget);
  375. $newTarget = self::cleanPath($newTarget);
  376. $query = OCP\DB::prepare('UPDATE `*PREFIX*sharing` SET `target` = `REPLACE(`target`, ?, ?) WHERE `uid_shared_with` '.self::getUsersAndGroups());
  377. $query->execute(array($oldTarget, $newTarget));
  378. }
  379. /**
  380. * Change the permissions for the specified item and user
  381. *
  382. * You must construct a new shared item to change the permissions of a file inside a shared folder if the permissions differ from the folder
  383. *
  384. * @param $source The source location of the item
  385. * @param $uid_shared_with The user to change the permissions for
  386. * @param $permissions The permissions, use the constants WRITE and DELETE
  387. */
  388. public static function setPermissions($source, $uid_shared_with, $permissions) {
  389. $source = self::cleanPath($source);
  390. $query = OCP\DB::prepare('UPDATE `*PREFIX*sharing` SET `permissions` = ? WHERE SUBSTR(`source`, 1, ?) = ? AND `uid_owner` = ? AND `uid_shared_with` '.self::getUsersAndGroups($uid_shared_with));
  391. $query->execute(array($permissions, strlen($source), $source, OCP\USER::getUser()));
  392. }
  393. /**
  394. * Unshare the item, removes it from all specified users
  395. *
  396. * You must use the pullOutOfFolder() function to unshare a file inside a shared folder and set $newTarget to nothing
  397. *
  398. * @param $source The source location of the item
  399. * @param $uid_shared_with Array of users to unshare the item from
  400. */
  401. public static function unshare($source, $uid_shared_with) {
  402. $source = self::cleanPath($source);
  403. $uid_owner = OCP\USER::getUser();
  404. $query = OCP\DB::prepare('DELETE FROM `*PREFIX*sharing` WHERE SUBSTR(`source`, 1, ?) = ? AND `uid_owner` = ? AND `uid_shared_with` '.self::getUsersAndGroups($uid_shared_with, false));
  405. $query->execute(array(strlen($source), $source, $uid_owner));
  406. self::updateFolder($uid_shared_with);
  407. }
  408. /**
  409. * Unshare the item from the current user, removes it only from the database and doesn't touch the source file
  410. *
  411. * You must use the pullOutOfFolder() function before you call unshareFromMySelf() and set the delete parameter to false to unshare from self a file inside a shared folder
  412. *
  413. * @param $target The target location of the item
  414. * @param $delete (Optional) If true delete the entry from the database, if false the permission is set to UNSHARED
  415. */
  416. public static function unshareFromMySelf($target, $delete = true) {
  417. $target = self::cleanPath($target);
  418. if ($delete) {
  419. $query = OCP\DB::prepare('DELETE FROM `*PREFIX*sharing` WHERE SUBSTR(`target`, 1, ?) = ? AND `uid_shared_with` '.self::getUsersAndGroups());
  420. $query->execute(array(strlen($target), $target));
  421. } else {
  422. $query = OCP\DB::prepare('UPDATE `*PREFIX*sharing` SET `permissions` = ? WHERE SUBSTR(`target`, 1, ?) = ? AND `uid_shared_with` '.self::getUsersAndGroups());
  423. $query->execute(array(self::UNSHARED, strlen($target), $target));
  424. }
  425. }
  426. /**
  427. * Remove the item from the database, the owner deleted the file
  428. * @param $arguments Array of arguments passed from OC_Hook
  429. */
  430. public static function deleteItem($arguments) {
  431. $source = "/".OCP\USER::getUser()."/files".self::cleanPath($arguments['path']);
  432. $result = self::getMySharedItem($source);
  433. if (is_array($result)) {
  434. foreach ($result as $item) {
  435. self::updateFolder($item['uid_shared_with']);
  436. }
  437. }
  438. $query = OCP\DB::prepare('DELETE FROM `*PREFIX*sharing` WHERE SUBSTR(`source`, 1, ?) = ? AND `uid_owner` = ?');
  439. $query->execute(array(strlen($source), $source, OCP\USER::getUser()));
  440. }
  441. /**
  442. * Rename the item in the database, the owner renamed the file
  443. * @param $arguments Array of arguments passed from OC_Hook
  444. */
  445. public static function renameItem($arguments) {
  446. $oldSource = "/".OCP\USER::getUser()."/files".self::cleanPath($arguments['oldpath']);
  447. $newSource = "/".OCP\USER::getUser()."/files".self::cleanPath($arguments['newpath']);
  448. $query = OCP\DB::prepare('UPDATE `*PREFIX*sharing` SET `source` = REPLACE(`source`, ?, ?) WHERE `uid_owner` = ?');
  449. $query->execute(array($oldSource, $newSource, OCP\USER::getUser()));
  450. }
  451. public static function updateItem($arguments) {
  452. $source = "/".OCP\USER::getUser()."/files".self::cleanPath($arguments['path']);
  453. $result = self::getMySharedItem($source);
  454. if (is_array($result)) {
  455. foreach ($result as $item) {
  456. self::updateFolder($item['uid_shared_with']);
  457. }
  458. }
  459. }
  460. public static function removeUser($arguments) {
  461. $query = OCP\DB::prepare('SELECT `uid_shared_with` FROM `*PREFIX*sharing` WHERE `uid_owner` = ?');
  462. $result = $query->execute(array($arguments['uid']))->fetchAll();
  463. if (is_array($result)) {
  464. $result = array_unique($result);
  465. foreach ($result as $item) {
  466. self::updateFolder($item['uid_shared_with']);
  467. }
  468. $query = OCP\DB::prepare('DELETE FROM `*PREFIX*sharing` WHERE `uid_owner` = ? OR `uid_shared_with` '.self::getUsersAndGroups($arguments['uid']));
  469. $query->execute(array($arguments['uid']));
  470. }
  471. }
  472. public static function addToGroupShare($arguments) {
  473. $length = -strlen($arguments['gid']) - 1;
  474. $query = OCP\DB::prepare('SELECT `uid_owner`, `source`, `permissions` FROM `*PREFIX*sharing` WHERE SUBSTR(`uid_shared_with`, '.$length.') = ?');
  475. $gid = '@'.$arguments['gid'];
  476. $result = $query->execute(array($gid))->fetchAll();
  477. if (count($result) > 0) {
  478. $lastSource = '';
  479. for ($i = 0; $i < count($result) - 1; $i++) {
  480. if ($result[$i]['source'] != $lastSource) {
  481. new OC_Share($result[$i]['source'], $arguments['gid'], $result[$i]['permissions']);
  482. $lastSource = $result[$i]['source'];
  483. }
  484. }
  485. }
  486. }
  487. public static function removeFromGroupShare($arguments) {
  488. $query = OCP\DB::prepare('DELETE FROM `*PREFIX*sharing` WHERE `uid_shared_with` = ?');
  489. $query->execute(array($arguments['uid'].'@'.$arguments['gid']));
  490. self::updateFolder($arguments['uid']);
  491. }
  492. }
  493. ?>