lib_share.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 = OC_User::getUser();
  39. $query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
  40. if ($uid_shared_with == self::PUBLICLINK) {
  41. $token = sha1("$uid_shared_with-$source");
  42. $query->execute(array($uid_owner, self::PUBLICLINK, $source, $token, $permissions));
  43. $this->token = $token;
  44. } else {
  45. if (OC_Group::groupExists($uid_shared_with)) {
  46. $gid = $uid_shared_with;
  47. $uid_shared_with = OC_Group::usersInGroup($gid);
  48. // Remove the owner from the list of users in the group
  49. $uid_shared_with = array_diff($uid_shared_with, array($uid_owner));
  50. } else if (OC_User::userExists($uid_shared_with)) {
  51. $gid = null;
  52. $uid_shared_with = array($uid_shared_with);
  53. } else {
  54. throw new Exception($uid_shared_with." is not a user");
  55. }
  56. foreach ($uid_shared_with as $uid) {
  57. // Check if this item is already shared with the user
  58. $checkSource = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE source = ? AND uid_shared_with ".self::getUsersAndGroups($uid));
  59. $resultCheckSource = $checkSource->execute(array($source))->fetchAll();
  60. // TODO Check if the source is inside a folder
  61. if (count($resultCheckSource) > 0 && !isset($gid)) {
  62. throw new Exception("This item is already shared with ".$uid);
  63. }
  64. // Check if the target already exists for the user, if it does append a number to the name
  65. $sharedFolder = "/".$uid."/files/Shared";
  66. $target = $sharedFolder."/".basename($source);
  67. if (self::getSource($target)) {
  68. if ($pos = strrpos($target, ".")) {
  69. $name = substr($target, 0, $pos);
  70. $ext = substr($target, $pos);
  71. } else {
  72. $name = $target;
  73. $ext = "";
  74. }
  75. $counter = 1;
  76. while ($checkTarget !== false) {
  77. $newTarget = $name."_".$counter.$ext;
  78. $checkTarget = self::getSource($newTarget);
  79. $counter++;
  80. }
  81. $target = $newTarget;
  82. }
  83. if (isset($gid)) {
  84. $uid = $uid."@".$gid;
  85. }
  86. $query->execute(array($uid_owner, $uid, $source, $target, $permissions));
  87. // Clear the folder size cache for the 'Shared' folder
  88. $clearFolderSize = OC_DB::prepare("DELETE FROM *PREFIX*foldersize WHERE path = ?");
  89. $clearFolderSize->execute(array($sharedFolder));
  90. }
  91. }
  92. }
  93. /**
  94. * Remove any duplicate or trailing '/' from the path
  95. * @return A clean path
  96. */
  97. private static function cleanPath($path) {
  98. $path = rtrim($path, "/");
  99. return preg_replace('{(/)\1+}', "/", $path);
  100. }
  101. /**
  102. * Generate a string to be used for searching for uid_shared_with that handles both users and groups
  103. * @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
  104. * @return An IN operator as a string
  105. */
  106. private static function getUsersAndGroups($uid = null) {
  107. $in = " IN(";
  108. if (isset($uid) && OC_Group::groupExists($uid)) {
  109. $users = OC_Group::usersInGroup($uid);
  110. foreach ($users as $user) {
  111. // Add a comma only if the the current element isn't the last
  112. if ($user !== end($users)) {
  113. $in .= "'".$user."@".$uid."', ";
  114. } else {
  115. $in .= "'".$user."@".$uid."'";
  116. }
  117. }
  118. } else if (isset($uid)) {
  119. // TODO Check if this is necessary, only constructor needs it as IN. It would be better for other queries to just return =$uid
  120. $in .= "'".$uid."'";
  121. $groups = OC_Group::getUserGroups($uid);
  122. foreach ($groups as $group) {
  123. $in .= ", '".$uid."@".$group."'";
  124. }
  125. } else {
  126. $uid = OC_User::getUser();
  127. $in .= "'".$uid."'";
  128. $groups = OC_Group::getUserGroups($uid);
  129. foreach ($groups as $group) {
  130. $in .= ", '".$uid."@".$group."'";
  131. }
  132. }
  133. $in .= ", '".self::PUBLICLINK."'";
  134. $in .= ")";
  135. return $in;
  136. }
  137. /**
  138. * Create a new entry in the database for a file inside a shared folder
  139. *
  140. * $oldTarget and $newTarget may be the same value. $oldTarget exists in case the file is being moved outside of the folder
  141. *
  142. * @param $oldTarget The current target location
  143. * @param $newTarget The new target location
  144. */
  145. public static function pullOutOfFolder($oldTarget, $newTarget) {
  146. $folders = self::getParentFolders($oldTarget);
  147. $source = $folders['source'].substr($oldTarget, strlen($folders['target']));
  148. $item = self::getItem($folders['target']);
  149. $query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
  150. $query->execute(array($item[0]['uid_owner'], OC_User::getUser(), $source, $newTarget, $item[0]['permissions']));
  151. }
  152. /**
  153. * Get the item with the specified target location
  154. * @param $target The target location of the item
  155. * @return An array with the item
  156. */
  157. public static function getItem($target) {
  158. $target = self::cleanPath($target);
  159. $query = OC_DB::prepare("SELECT uid_owner, source, permissions FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ? LIMIT 1");
  160. return $query->execute(array($target, OC_User::getUser()))->fetchAll();
  161. }
  162. /**
  163. * Get the item with the specified source location
  164. * @param $source The source location of the item
  165. * @return An array with the users and permissions the item is shared with
  166. */
  167. public static function getMySharedItem($source) {
  168. $source = self::cleanPath($source);
  169. $query = OC_DB::prepare("SELECT uid_shared_with, permissions FROM *PREFIX*sharing WHERE source = ? AND uid_owner = ?");
  170. $result = $query->execute(array($source, OC_User::getUser()))->fetchAll();
  171. if (count($result) > 0) {
  172. return $result;
  173. } else if ($originalSource = self::getSource($source)) {
  174. return $query->execute(array($originalSource, OC_User::getUser()))->fetchAll();
  175. } else {
  176. return false;
  177. }
  178. }
  179. /**
  180. * Get all items the current user is sharing
  181. * @return An array with all items the user is sharing
  182. */
  183. public static function getMySharedItems() {
  184. $query = OC_DB::prepare("SELECT uid_shared_with, source, permissions FROM *PREFIX*sharing WHERE uid_owner = ?");
  185. return $query->execute(array(OC_User::getUser()))->fetchAll();
  186. }
  187. /**
  188. * 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
  189. *
  190. * Works for both target and source folders. Can be used for getting all items shared with you e.g. pass '/MTGap/files'
  191. *
  192. * @param $folder The folder of the items to look for
  193. * @return An array with all items in the database that are in the folder
  194. */
  195. public static function getItemsInFolder($folder) {
  196. $folder = self::cleanPath($folder);
  197. // Append '/' in order to filter out the folder itself if not already there
  198. if (substr($folder, -1) !== "/") {
  199. $folder .= "/";
  200. }
  201. $length = strlen($folder);
  202. $query = OC_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());
  203. return $query->execute(array($length, $folder, $length, $folder))->fetchAll();
  204. }
  205. /**
  206. * Get the source and target parent folders of the specified target location
  207. * @param $target The target location of the item
  208. * @return An array with the keys 'source' and 'target' with the values of the source and target parent folders
  209. */
  210. public static function getParentFolders($target) {
  211. $target = self::cleanPath($target);
  212. $query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with".self::getUsersAndGroups()." LIMIT 1");
  213. // Prevent searching for user directory e.g. '/MTGap/files'
  214. $userDirectory = substr($target, 0, strpos($target, "files") + 5);
  215. $target = dirname($target);
  216. $result = array();
  217. while ($target != "" && $target != "/" && $target != "." && $target != $userDirectory) {
  218. // Check if the parent directory of this target location is shared
  219. $result = $query->execute(array($target))->fetchAll();
  220. if (count($result) > 0) {
  221. break;
  222. }
  223. $target = dirname($target);
  224. }
  225. if (count($result) > 0) {
  226. // Return both the source folder and the target folder
  227. return array("source" => $result[0]['source'], "target" => $target);
  228. } else {
  229. return false;
  230. }
  231. }
  232. /**
  233. * Get the source location of the item at the specified target location
  234. * @param $target The target location of the item
  235. * @return Source location or false if target location is not valid
  236. */
  237. public static function getSource($target) {
  238. $target = self::cleanPath($target);
  239. $query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with ".self::getUsersAndGroups()." LIMIT 1");
  240. $result = $query->execute(array($target))->fetchAll();
  241. if (count($result) > 0) {
  242. return $result[0]['source'];
  243. } else {
  244. $folders = self::getParentFolders($target);
  245. if ($folders == true) {
  246. return $folders['source'].substr($target, strlen($folders['target']));
  247. } else {
  248. return false;
  249. }
  250. }
  251. }
  252. /**
  253. * Get the user's permissions for the item at the specified target location
  254. * @param $target The target location of the item
  255. * @return The permissions, use bitwise operators to check against the constants WRITE and DELETE
  256. */
  257. public static function getPermissions($target) {
  258. $target = self::cleanPath($target);
  259. $query = OC_DB::prepare("SELECT permissions FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with ".self::getUsersAndGroups()." LIMIT 1");
  260. $result = $query->execute(array($target))->fetchAll();
  261. if (count($result) > 0) {
  262. return $result[0]['permissions'];
  263. } else {
  264. $folders = self::getParentFolders($target);
  265. if ($folders == true) {
  266. $result = $query->execute(array($folders['target']))->fetchAll();
  267. if (count($result) > 0) {
  268. return $result[0]['permissions'];
  269. }
  270. } else {
  271. OC_Log::write('files_sharing',"Not existing parent folder : ".$target,OC_Log::ERROR);
  272. return false;
  273. }
  274. }
  275. }
  276. /**
  277. * Get the token for a public link
  278. * @return The token of the public link, a sha1 hash
  279. */
  280. public function getToken() {
  281. return $this->token;
  282. }
  283. /**
  284. * Get the token for a public link
  285. * @param $source The source location of the item
  286. * @return The token of the public link, a sha1 hash
  287. */
  288. public static function getTokenFromSource($source) {
  289. $query = OC_DB::prepare("SELECT target FROM *PREFIX*sharing WHERE source = ? AND uid_shared_with = ? AND uid_owner = ? LIMIT 1");
  290. $result = $query->execute(array($source, self::PUBLICLINK, OC_User::getUser()))->fetchAll();
  291. if (count($result) > 0) {
  292. return $result[0]['target'];
  293. } else {
  294. return false;
  295. }
  296. }
  297. /**
  298. * Set the target location to a new value
  299. *
  300. * 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
  301. *
  302. * @param $oldTarget The current target location
  303. * @param $newTarget The new target location
  304. */
  305. public static function setTarget($oldTarget, $newTarget) {
  306. $oldTarget = self::cleanPath($oldTarget);
  307. $newTarget = self::cleanPath($newTarget);
  308. $query = OC_DB::prepare("UPDATE *PREFIX*sharing SET target = REPLACE(target, ?, ?) WHERE uid_shared_with ".self::getUsersAndGroups());
  309. $query->execute(array($oldTarget, $newTarget));
  310. }
  311. /**
  312. * Change the permissions for the specified item and user
  313. *
  314. * 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
  315. *
  316. * @param $source The source location of the item
  317. * @param $uid_shared_with The user to change the permissions for
  318. * @param $permissions The permissions, use the constants WRITE and DELETE
  319. */
  320. public static function setPermissions($source, $uid_shared_with, $permissions) {
  321. $source = self::cleanPath($source);
  322. $query = OC_DB::prepare("UPDATE *PREFIX*sharing SET permissions = ? WHERE SUBSTR(source, 1, ?) = ? AND uid_owner = ? AND uid_shared_with ".self::getUsersAndGroups($uid_shared_with));
  323. $query->execute(array($permissions, strlen($source), $source, OC_User::getUser()));
  324. }
  325. /**
  326. * Unshare the item, removes it from all specified users
  327. *
  328. * You must use the pullOutOfFolder() function to unshare a file inside a shared folder and set $newTarget to nothing
  329. *
  330. * @param $source The source location of the item
  331. * @param $uid_shared_with Array of users to unshare the item from
  332. */
  333. public static function unshare($source, $uid_shared_with) {
  334. $source = self::cleanPath($source);
  335. $query = OC_DB::prepare("DELETE FROM *PREFIX*sharing WHERE SUBSTR(source, 1, ?) = ? AND uid_owner = ? AND uid_shared_with ".self::getUsersAndGroups($uid_shared_with));
  336. $query->execute(array(strlen($source), $source, OC_User::getUser()));
  337. }
  338. /**
  339. * Unshare the item from the current user, removes it only from the database and doesn't touch the source file
  340. *
  341. * 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
  342. *
  343. * @param $target The target location of the item
  344. * @param $delete (Optional) If true delete the entry from the database, if false the permission is set to UNSHARED
  345. */
  346. public static function unshareFromMySelf($target, $delete = true) {
  347. $target = self::cleanPath($target);
  348. if ($delete) {
  349. $query = OC_DB::prepare("DELETE FROM *PREFIX*sharing WHERE SUBSTR(target, 1, ?) = ? AND uid_shared_with ".self::getUsersAndGroups());
  350. $query->execute(array(strlen($target), $target));
  351. } else {
  352. $query = OC_DB::prepare("UPDATE *PREFIX*sharing SET permissions = ? WHERE SUBSTR(target, 1, ?) = ? AND uid_shared_with ".self::getUsersAndGroups());
  353. $query->execute(array(self::UNSHARED, strlen($target), $target));
  354. }
  355. }
  356. /**
  357. * Remove the item from the database, the owner deleted the file
  358. * @param $arguments Array of arguments passed from OC_Hook
  359. */
  360. public static function deleteItem($arguments) {
  361. $source = "/".OC_User::getUser()."/files".self::cleanPath($arguments['path']);
  362. $query = OC_DB::prepare("DELETE FROM *PREFIX*sharing WHERE SUBSTR(source, 1, ?) = ? AND uid_owner = ?");
  363. $query->execute(array(strlen($source), $source, OC_User::getUser()));
  364. }
  365. /**
  366. * Rename the item in the database, the owner renamed the file
  367. * @param $arguments Array of arguments passed from OC_Hook
  368. */
  369. public static function renameItem($arguments) {
  370. $oldSource = "/".OC_User::getUser()."/files".self::cleanPath($arguments['oldpath']);
  371. $newSource = "/".OC_User::getUser()."/files".self::cleanPath($arguments['newpath']);
  372. $query = OC_DB::prepare("UPDATE *PREFIX*sharing SET source = REPLACE(source, ?, ?) WHERE uid_owner = ?");
  373. $query->execute(array($oldSource, $newSource, OC_User::getUser()));
  374. }
  375. }
  376. ?>