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.

share.php 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. $defaults = new \OCP\Defaults();
  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. $itemSourceName = isset($_POST['itemSourceName']) ? $_POST['itemSourceName'] : null;
  32. if ($shareType === OCP\Share::SHARE_TYPE_LINK && $shareWith == '') {
  33. $shareWith = null;
  34. }
  35. $token = OCP\Share::shareItem(
  36. $_POST['itemType'],
  37. $_POST['itemSource'],
  38. $shareType,
  39. $shareWith,
  40. $_POST['permissions'],
  41. $itemSourceName,
  42. (!empty($_POST['expirationDate']) ? new \DateTime($_POST['expirationDate']) : null)
  43. );
  44. $token = base_convert($token, 16, 36);
  45. if (is_string($token)) {
  46. OC_JSON::success(array('data' => array('token' => $token)));
  47. } else {
  48. OC_JSON::success();
  49. }
  50. } catch (Exception $exception) {
  51. OC_JSON::error(array('data' => array('message' => $exception->getMessage())));
  52. }
  53. }
  54. break;
  55. case 'unshare':
  56. if (isset($_POST['shareType']) && isset($_POST['shareWith'])) {
  57. if ((int)$_POST['shareType'] === OCP\Share::SHARE_TYPE_LINK && $_POST['shareWith'] == '') {
  58. $shareWith = null;
  59. } else {
  60. $shareWith = $_POST['shareWith'];
  61. }
  62. $return = OCP\Share::unshare($_POST['itemType'], $_POST['itemSource'], $_POST['shareType'], $shareWith);
  63. ($return) ? OC_JSON::success() : OC_JSON::error();
  64. }
  65. break;
  66. case 'setPermissions':
  67. if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
  68. $return = OCP\Share::setPermissions(
  69. $_POST['itemType'],
  70. $_POST['itemSource'],
  71. $_POST['shareType'],
  72. $_POST['shareWith'],
  73. $_POST['permissions']
  74. );
  75. ($return) ? OC_JSON::success() : OC_JSON::error();
  76. }
  77. break;
  78. case 'setExpirationDate':
  79. if (isset($_POST['date'])) {
  80. try {
  81. $return = OCP\Share::setExpirationDate($_POST['itemType'], $_POST['itemSource'], $_POST['date']);
  82. ($return) ? OC_JSON::success() : OC_JSON::error();
  83. } catch (\Exception $e) {
  84. OC_JSON::error(array('data' => array('message' => $e->getMessage())));
  85. }
  86. }
  87. break;
  88. case 'informRecipients':
  89. $l = \OC::$server->getL10N('core');
  90. $shareType = (int) $_POST['shareType'];
  91. $itemType = $_POST['itemType'];
  92. $itemSource = $_POST['itemSource'];
  93. $recipient = $_POST['recipient'];
  94. if($shareType === \OCP\Share::SHARE_TYPE_USER) {
  95. $recipientList[] = $recipient;
  96. } elseif ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  97. $recipientList = \OC_Group::usersInGroup($recipient);
  98. }
  99. // don't send a mail to the user who shared the file
  100. $recipientList = array_diff($recipientList, array(\OCP\User::getUser()));
  101. $mailNotification = new OC\Share\MailNotifications();
  102. $result = $mailNotification->sendInternalShareMail($recipientList, $itemSource, $itemType);
  103. \OCP\Share::setSendMailStatus($itemType, $itemSource, $shareType, $recipient, true);
  104. if (empty($result)) {
  105. OCP\JSON::success();
  106. } else {
  107. OCP\JSON::error(array(
  108. 'data' => array(
  109. 'message' => $l->t("Couldn't send mail to following users: %s ",
  110. implode(', ', $result)
  111. )
  112. )
  113. ));
  114. }
  115. break;
  116. case 'informRecipientsDisabled':
  117. $itemSource = $_POST['itemSource'];
  118. $shareType = $_POST['shareType'];
  119. $itemType = $_POST['itemType'];
  120. $recipient = $_POST['recipient'];
  121. \OCP\Share::setSendMailStatus($itemType, $itemSource, $shareType, $recipient, false);
  122. OCP\JSON::success();
  123. break;
  124. case 'email':
  125. // read post variables
  126. $link = $_POST['link'];
  127. $file = $_POST['file'];
  128. $to_address = $_POST['toaddress'];
  129. $mailNotification = new \OC\Share\MailNotifications();
  130. $expiration = null;
  131. if (isset($_POST['expiration']) && $_POST['expiration'] !== '') {
  132. try {
  133. $date = new DateTime($_POST['expiration']);
  134. $expiration = $date->getTimestamp();
  135. } catch (Exception $e) {
  136. \OCP\Util::writeLog('sharing', "Couldn't read date: " . $e->getMessage(), \OCP\Util::ERROR);
  137. }
  138. }
  139. $result = $mailNotification->sendLinkShareMail($to_address, $file, $link, $expiration);
  140. if(empty($result)) {
  141. \OCP\JSON::success();
  142. } else {
  143. $l = \OC::$server->getL10N('core');
  144. OCP\JSON::error(array(
  145. 'data' => array(
  146. 'message' => $l->t("Couldn't send mail to following users: %s ",
  147. implode(', ', $result)
  148. )
  149. )
  150. ));
  151. }
  152. break;
  153. }
  154. } else if (isset($_GET['fetch'])) {
  155. switch ($_GET['fetch']) {
  156. case 'getItemsSharedStatuses':
  157. if (isset($_GET['itemType'])) {
  158. $return = OCP\Share::getItemsShared($_GET['itemType'], OCP\Share::FORMAT_STATUSES);
  159. is_array($return) ? OC_JSON::success(array('data' => $return)) : OC_JSON::error();
  160. }
  161. break;
  162. case 'getItem':
  163. if (isset($_GET['itemType'])
  164. && isset($_GET['itemSource'])
  165. && isset($_GET['checkReshare'])
  166. && isset($_GET['checkShares'])) {
  167. if ($_GET['checkReshare'] == 'true') {
  168. $reshare = OCP\Share::getItemSharedWithBySource(
  169. $_GET['itemType'],
  170. $_GET['itemSource'],
  171. OCP\Share::FORMAT_NONE,
  172. null,
  173. true
  174. );
  175. } else {
  176. $reshare = false;
  177. }
  178. if ($_GET['checkShares'] == 'true') {
  179. $shares = OCP\Share::getItemShared(
  180. $_GET['itemType'],
  181. $_GET['itemSource'],
  182. OCP\Share::FORMAT_NONE,
  183. null,
  184. true
  185. );
  186. } else {
  187. $shares = false;
  188. }
  189. OC_JSON::success(array('data' => array('reshare' => $reshare, 'shares' => $shares)));
  190. }
  191. break;
  192. case 'getShareWithEmail':
  193. $result = array();
  194. if (isset($_GET['search'])) {
  195. $cm = OC::$server->getContactsManager();
  196. if (!is_null($cm) && $cm->isEnabled()) {
  197. $contacts = $cm->search($_GET['search'], array('FN', 'EMAIL'));
  198. foreach ($contacts as $contact) {
  199. if (!isset($contact['EMAIL'])) {
  200. continue;
  201. }
  202. $emails = $contact['EMAIL'];
  203. if (!is_array($emails)) {
  204. $emails = array($emails);
  205. }
  206. foreach($emails as $email) {
  207. $result[] = array(
  208. 'id' => $contact['id'],
  209. 'email' => $email,
  210. 'displayname' => $contact['FN'],
  211. );
  212. }
  213. }
  214. }
  215. }
  216. OC_JSON::success(array('data' => $result));
  217. break;
  218. case 'getShareWith':
  219. if (isset($_GET['search'])) {
  220. $shareWithinGroupOnly = OC\Share\Share::shareWithGroupMembersOnly();
  221. $shareWith = array();
  222. // if (OC_App::isEnabled('contacts')) {
  223. // // TODO Add function to contacts to only get the 'fullname' column to improve performance
  224. // $ids = OC_Contacts_Addressbook::activeIds();
  225. // foreach ($ids as $id) {
  226. // $vcards = OC_Contacts_VCard::all($id);
  227. // foreach ($vcards as $vcard) {
  228. // $contact = $vcard['fullname'];
  229. // if (stripos($contact, $_GET['search']) !== false
  230. // && (!isset($_GET['itemShares'])
  231. // || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT])
  232. // || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT])
  233. // || !in_array($contact, $_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT]))) {
  234. // $shareWith[] = array('label' => $contact, 'value' => array('shareType' => 5, 'shareWith' => $vcard['id']));
  235. // }
  236. // }
  237. // }
  238. // }
  239. $groups = OC_Group::getGroups($_GET['search']);
  240. if ($shareWithinGroupOnly) {
  241. $usergroups = OC_Group::getUserGroups(OC_User::getUser());
  242. $groups = array_intersect($groups, $usergroups);
  243. }
  244. $count = 0;
  245. $users = array();
  246. $limit = 0;
  247. $offset = 0;
  248. while ($count < 15 && count($users) == $limit) {
  249. $limit = 15 - $count;
  250. if ($shareWithinGroupOnly) {
  251. $users = OC_Group::DisplayNamesInGroups($usergroups, $_GET['search'], $limit, $offset);
  252. } else {
  253. $users = OC_User::getDisplayNames($_GET['search'], $limit, $offset);
  254. }
  255. $offset += $limit;
  256. foreach ($users as $uid => $displayName) {
  257. if ((!isset($_GET['itemShares'])
  258. || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_USER])
  259. || !in_array($uid, $_GET['itemShares'][OCP\Share::SHARE_TYPE_USER]))
  260. && $uid != OC_User::getUser()) {
  261. $shareWith[] = array(
  262. 'label' => $displayName,
  263. 'value' => array(
  264. 'shareType' => OCP\Share::SHARE_TYPE_USER,
  265. 'shareWith' => $uid)
  266. );
  267. $count++;
  268. }
  269. }
  270. }
  271. $count = 0;
  272. // enable l10n support
  273. $l = \OC::$server->getL10N('core');
  274. foreach ($groups as $group) {
  275. if ($count < 15) {
  276. if (!isset($_GET['itemShares'])
  277. || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  278. || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  279. || !in_array($group, $_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])) {
  280. $shareWith[] = array(
  281. 'label' => $group,
  282. 'value' => array(
  283. 'shareType' => OCP\Share::SHARE_TYPE_GROUP,
  284. 'shareWith' => $group
  285. )
  286. );
  287. $count++;
  288. }
  289. } else {
  290. break;
  291. }
  292. }
  293. $sorter = new \OC\Share\SearchResultSorter($_GET['search'],
  294. 'label',
  295. new \OC\Log());
  296. usort($shareWith, array($sorter, 'sort'));
  297. OC_JSON::success(array('data' => $shareWith));
  298. }
  299. break;
  300. }
  301. }