share.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. OC_JSON::checkLoggedIn();
  22. OCP\JSON::callCheck();
  23. OC_App::loadApps();
  24. if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['itemSource'])) {
  25. switch ($_POST['action']) {
  26. case 'share':
  27. if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
  28. try {
  29. $shareType = (int)$_POST['shareType'];
  30. $shareWith = $_POST['shareWith'];
  31. if ($shareType === OCP\Share::SHARE_TYPE_LINK && $shareWith == '') {
  32. $shareWith = null;
  33. }
  34. $token = OCP\Share::shareItem($_POST['itemType'], $_POST['itemSource'], $shareType, $shareWith, $_POST['permissions']);
  35. if (is_string($token)) {
  36. OC_JSON::success(array('data' => array('token' => $token)));
  37. } else {
  38. OC_JSON::success();
  39. }
  40. } catch (Exception $exception) {
  41. OC_JSON::error(array('data' => array('message' => $exception->getMessage())));
  42. }
  43. }
  44. break;
  45. case 'unshare':
  46. if (isset($_POST['shareType']) && isset($_POST['shareWith'])) {
  47. if ((int)$_POST['shareType'] === OCP\Share::SHARE_TYPE_LINK && $_POST['shareWith'] == '') {
  48. $shareWith = null;
  49. } else {
  50. $shareWith = $_POST['shareWith'];
  51. }
  52. $return = OCP\Share::unshare($_POST['itemType'], $_POST['itemSource'], $_POST['shareType'], $shareWith);
  53. ($return) ? OC_JSON::success() : OC_JSON::error();
  54. }
  55. break;
  56. case 'setPermissions':
  57. if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
  58. $return = OCP\Share::setPermissions($_POST['itemType'], $_POST['itemSource'], $_POST['shareType'], $_POST['shareWith'], $_POST['permissions']);
  59. ($return) ? OC_JSON::success() : OC_JSON::error();
  60. }
  61. break;
  62. case 'setExpirationDate':
  63. if (isset($_POST['date'])) {
  64. $return = OCP\Share::setExpirationDate($_POST['itemType'], $_POST['itemSource'], $_POST['date']);
  65. ($return) ? OC_JSON::success() : OC_JSON::error();
  66. }
  67. break;
  68. case 'email':
  69. // read post variables
  70. $user = OCP\USER::getUser();
  71. $type = $_POST['itemType'];
  72. $link = $_POST['link'];
  73. $file = $_POST['file'];
  74. $to_address = $_POST['toaddress'];
  75. // enable l10n support
  76. $l = OC_L10N::get('core');
  77. // setup the email
  78. $subject = (string)$l->t('User %s shared a file with you', $user);
  79. if ($type === 'dir')
  80. $subject = (string)$l->t('User %s shared a folder with you', $user);
  81. $text = (string)$l->t('User %s shared the file "%s" with you. It is available for download here: %s', array($user, $file, $link));
  82. if ($type === 'dir')
  83. $text = (string)$l->t('User %s shared the folder "%s" with you. It is available for download here: %s', array($user, $file, $link));
  84. $default_from = OCP\Util::getDefaultEmailAddress('sharing-noreply');
  85. $from_address = OCP\Config::getUserValue($user, 'settings', 'email', $default_from );
  86. // send it out now
  87. try {
  88. OCP\Util::sendMail($to_address, $to_address, $subject, $text, $from_address, $user);
  89. OCP\JSON::success();
  90. } catch (Exception $exception) {
  91. OCP\JSON::error(array('data' => array('message' => $exception->getMessage())));
  92. }
  93. break;
  94. }
  95. } else if (isset($_GET['fetch'])) {
  96. switch ($_GET['fetch']) {
  97. case 'getItemsSharedStatuses':
  98. if (isset($_GET['itemType'])) {
  99. $return = OCP\Share::getItemsShared($_GET['itemType'], OCP\Share::FORMAT_STATUSES);
  100. is_array($return) ? OC_JSON::success(array('data' => $return)) : OC_JSON::error();
  101. }
  102. break;
  103. case 'getItem':
  104. if (isset($_GET['itemType']) && isset($_GET['itemSource']) && isset($_GET['checkReshare']) && isset($_GET['checkShares'])) {
  105. if ($_GET['checkReshare'] == 'true') {
  106. $reshare = OCP\Share::getItemSharedWithBySource($_GET['itemType'], $_GET['itemSource'], OCP\Share::FORMAT_NONE, null, true);
  107. } else {
  108. $reshare = false;
  109. }
  110. if ($_GET['checkShares'] == 'true') {
  111. $shares = OCP\Share::getItemShared($_GET['itemType'], $_GET['itemSource'], OCP\Share::FORMAT_NONE, null, true);
  112. } else {
  113. $shares = false;
  114. }
  115. OC_JSON::success(array('data' => array('reshare' => $reshare, 'shares' => $shares)));
  116. }
  117. break;
  118. case 'getShareWith':
  119. if (isset($_GET['search'])) {
  120. $sharePolicy = OC_Appconfig::getValue('core', 'shareapi_share_policy', 'global');
  121. $shareWith = array();
  122. // if (OC_App::isEnabled('contacts')) {
  123. // // TODO Add function to contacts to only get the 'fullname' column to improve performance
  124. // $ids = OC_Contacts_Addressbook::activeIds();
  125. // foreach ($ids as $id) {
  126. // $vcards = OC_Contacts_VCard::all($id);
  127. // foreach ($vcards as $vcard) {
  128. // $contact = $vcard['fullname'];
  129. // if (stripos($contact, $_GET['search']) !== false
  130. // && (!isset($_GET['itemShares'])
  131. // || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT])
  132. // || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT])
  133. // || !in_array($contact, $_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT]))) {
  134. // $shareWith[] = array('label' => $contact, 'value' => array('shareType' => 5, 'shareWith' => $vcard['id']));
  135. // }
  136. // }
  137. // }
  138. // }
  139. if ($sharePolicy == 'groups_only') {
  140. $groups = OC_Group::getUserGroups(OC_User::getUser());
  141. } else {
  142. $groups = OC_Group::getGroups();
  143. }
  144. $count = 0;
  145. $users = array();
  146. $limit = 0;
  147. $offset = 0;
  148. while ($count < 4 && count($users) == $limit) {
  149. $limit = 4 - $count;
  150. if ($sharePolicy == 'groups_only') {
  151. $users = OC_Group::usersInGroups($groups, $_GET['search'], $limit, $offset);
  152. } else {
  153. $users = OC_User::getUsers($_GET['search'], $limit, $offset);
  154. }
  155. $offset += $limit;
  156. foreach ($users as $user) {
  157. 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()) {
  158. $shareWith[] = array('label' => $user, 'value' => array('shareType' => OCP\Share::SHARE_TYPE_USER, 'shareWith' => $user));
  159. $count++;
  160. }
  161. }
  162. }
  163. $count = 0;
  164. foreach ($groups as $group) {
  165. if ($count < 4) {
  166. if (stripos($group, $_GET['search']) !== false
  167. && (!isset($_GET['itemShares'])
  168. || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  169. || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  170. || !in_array($group, $_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP]))) {
  171. $shareWith[] = array('label' => $group.' (group)', 'value' => array('shareType' => OCP\Share::SHARE_TYPE_GROUP, 'shareWith' => $group));
  172. $count++;
  173. }
  174. } else {
  175. break;
  176. }
  177. }
  178. OC_JSON::success(array('data' => $shareWith));
  179. }
  180. break;
  181. }
  182. }