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.

109 lines
3.5 KiB

  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. /**
  27. * TODO notify user a file is being shared with them?
  28. * Share an item, adds an entry into the database
  29. * @param string $item
  30. * @param user item shared with $uid_shared_with
  31. */
  32. public function __construct($item, $public = false, $uid_shared_with) {
  33. if ($item && OC_FILESYSTEM::file_exists($item) && OC_FILESYSTEM::is_readable($item)) {
  34. $uid_owner = $_SESSION['user_id'];
  35. if ($public) {
  36. // TODO create token for public file
  37. $token = sha1("$uid_owner-$item");
  38. } else {
  39. $query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?)");
  40. foreach ($uid_shared_with as $uid) {
  41. $query->execute(array($uid_owner, $uid, $item));
  42. }
  43. }
  44. }
  45. }
  46. /**
  47. * TODO complete lib_permissions
  48. * Change the permissions of the specified item
  49. * @param permissions $permissions
  50. */
  51. public static function setPermissions($item, $uid_shared_with, $permissions) {
  52. $query = OC_DB::prepare("UPDATE *PREFIX*sharing SET permissions = ? WHERE item = ? AND uid_shared_with = ? AND uid_owner = ?");
  53. $query->execute(array($permissions, $item, $uid_shared_with, $_SESSION['user_id']));
  54. }
  55. /**
  56. * Get the permissions for the specified item
  57. * @param unknown_type $item
  58. */
  59. public static function getPermissions($item, $uid_shared_with) {
  60. $query = OC_DB::prepare("SELECT permissions FROM *PREFIX*sharing WHERE item = ? AND uid_shared_with = ? AND uid_owner = ? ");
  61. return $query->execute(array($item, $uid_shared_with, $_SESSION['user_id']))->fetchAll();
  62. }
  63. /**
  64. * Unshare the item, removes it from all users specified
  65. * @param array $uid_shared_with
  66. */
  67. public static function unshare($item, $uid_shared_with) {
  68. $query = OC_DB::prepare("DELETE FROM *PREFIX*sharing WHERE item = ? AND uid_shared_with = ? AND uid_owner = ?");
  69. foreach ($uid_shared_with as $uid) {
  70. $query->execute(array($item, $uid, $_SESSION['user_id']));
  71. }
  72. }
  73. /**
  74. * Get the source location of the target item
  75. * @return source path
  76. */
  77. public static function getSource($target) {
  78. $query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ?");
  79. $result = $query->execute(array($target, $_SESSION['user_id']))->fetchAll();
  80. return $result[0]['source'];
  81. }
  82. /**
  83. * Get all items the user is sharing
  84. * @return array
  85. */
  86. public static function getSharedItems() {
  87. $query = OC_DB::prepare("SELECT * FROM *PREFIX*sharing WHERE uid_owner = ?");
  88. return $query->execute(array($_SESSION['user_id']))->fetchAll();
  89. }
  90. /**
  91. * Get all items shared with the user
  92. * @return array
  93. */
  94. public static function getItemsSharedWith() {
  95. $query = OC_DB::prepare("SELECT * FROM *PREFIX*sharing WHERE uid_shared_with = ?");
  96. return $query->execute(array($_SESSION['user_id']))->fetchAll();
  97. }
  98. }
  99. ?>