share.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski mtgap@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. require_once '../../lib/base.php';
  22. OC_JSON::checkLoggedIn();
  23. if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['itemSource'])) {
  24. switch ($_POST['action']) {
  25. case 'share':
  26. if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
  27. try {
  28. if ((int)$_POST['shareType'] === OCP\Share::SHARE_TYPE_LINK && $_POST['shareWith'] == '') {
  29. $shareWith = null;
  30. } else {
  31. $shareWith = $_POST['shareWith'];
  32. }
  33. OCP\Share::shareItem($_POST['itemType'], $_POST['itemSource'], (int)$_POST['shareType'], $shareWith, $_POST['permissions']);
  34. OC_JSON::success();
  35. } catch (Exception $exception) {
  36. OC_JSON::error(array('data' => array('message' => $exception->getMessage())));
  37. }
  38. }
  39. break;
  40. case 'unshare':
  41. if (isset($_POST['shareType']) && isset($_POST['shareWith'])) {
  42. if ((int)$_POST['shareType'] === OCP\Share::SHARE_TYPE_LINK && $_POST['shareWith'] == '') {
  43. $shareWith = null;
  44. } else {
  45. $shareWith = $_POST['shareWith'];
  46. }
  47. $return = OCP\Share::unshare($_POST['itemType'], $_POST['itemSource'], $_POST['shareType'], $shareWith);
  48. ($return) ? OC_JSON::success() : OC_JSON::error();
  49. }
  50. break;
  51. case 'setPermissions':
  52. if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
  53. $return = OCP\Share::setPermissions($_POST['itemType'], $_POST['itemSource'], $_POST['shareType'], $_POST['shareWith'], $_POST['permissions']);
  54. ($return) ? OC_JSON::success() : OC_JSON::error();
  55. }
  56. break;
  57. }
  58. } else if (isset($_GET['fetch'])) {
  59. switch ($_GET['fetch']) {
  60. case 'getItemsSharedStatuses':
  61. if (isset($_GET['itemType'])) {
  62. $return = OCP\Share::getItemsShared($_GET['itemType'], OCP\Share::FORMAT_STATUSES);
  63. is_array($return) ? OC_JSON::success(array('data' => $return)) : OC_JSON::error();
  64. }
  65. break;
  66. case 'getItem':
  67. if (isset($_GET['itemType']) && isset($_GET['itemSource']) && isset($_GET['checkReshare']) && isset($_GET['checkShares'])) {
  68. if ($_GET['checkReshare'] == 'true') {
  69. $reshare = OCP\Share::getItemSharedWithBySource($_GET['itemType'], $_GET['itemSource'], OCP\Share::FORMAT_NONE, null, true);
  70. } else {
  71. $reshare = false;
  72. }
  73. if ($_GET['checkShares'] == 'true') {
  74. $shares = OCP\Share::getItemShared($_GET['itemType'], $_GET['itemSource']);
  75. } else {
  76. $shares = false;
  77. }
  78. OC_JSON::success(array('data' => array('reshare' => $reshare, 'shares' => $shares)));
  79. }
  80. break;
  81. case 'getShareWith':
  82. if (isset($_GET['search'])) {
  83. $shareWith = array();
  84. if (OC_App::isEnabled('contacts')) {
  85. // TODO Add function to contacts to only get the 'fullname' column to improve performance
  86. $ids = OC_Contacts_Addressbook::activeIds();
  87. foreach ($ids as $id) {
  88. $vcards = OC_Contacts_VCard::all($id);
  89. foreach ($vcards as $vcard) {
  90. $contact = $vcard['fullname'];
  91. if (stripos($contact, $_GET['search']) !== false
  92. && (!isset($_GET['itemShares'])
  93. || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT])
  94. || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT])
  95. || !in_array($contact, $_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT]))) {
  96. $shareWith[] = array('label' => $contact, 'value' => array('shareType' => 5, 'shareWith' => $vcard['id']));
  97. }
  98. }
  99. }
  100. }
  101. $count = 0;
  102. $users = array();
  103. $limit = 0;
  104. $offset = 0;
  105. while ($count < 4 && count($users) == $limit) {
  106. $limit = 4 - $count;
  107. $users = OC_User::getUsers($_GET['search'], $limit, $offset);
  108. $offset += $limit;
  109. foreach ($users as $user) {
  110. if ((!isset($_GET['itemShares']) || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_USER]) || !in_array($user, $_GET['itemShares'][OCP\Share::SHARE_TYPE_USER])) && $user != OC_User::getUser()) {
  111. $shareWith[] = array('label' => $user, 'value' => array('shareType' => OCP\Share::SHARE_TYPE_USER, 'shareWith' => $user));
  112. $count++;
  113. }
  114. }
  115. }
  116. $count = 0;
  117. $groups = OC_Group::getUserGroups(OC_User::getUser());
  118. foreach ($groups as $group) {
  119. if ($count < 4) {
  120. if (stripos($group, $_GET['search']) !== false
  121. && (!isset($_GET['itemShares'])
  122. || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  123. || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  124. || !in_array($group, $_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP]))) {
  125. $shareWith[] = array('label' => $group.' (group)', 'value' => array('shareType' => OCP\Share::SHARE_TYPE_GROUP, 'shareWith' => $group));
  126. $count++;
  127. }
  128. } else {
  129. break;
  130. }
  131. }
  132. OC_JSON::success(array('data' => $shareWith));
  133. }
  134. break;
  135. }
  136. }
  137. ?>