share.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Björn Schießle <schiessle@owncloud.com>
  6. * @author Craig Morrissey <craig@owncloud.com>
  7. * @author dampfklon <me@dampfklon.de>
  8. * @author Joas Schilling <nickvergessen@owncloud.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@owncloud.com>
  11. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Ramiro Aparicio <rapariciog@gmail.com>
  14. * @author Robin Appelman <icewind@owncloud.com>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Thomas Tanghus <thomas@tanghus.net>
  18. * @author Vincent Petry <pvince81@owncloud.com>
  19. *
  20. * @copyright Copyright (c) 2015, ownCloud, Inc.
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. OC_JSON::checkLoggedIn();
  37. OCP\JSON::callCheck();
  38. $defaults = new \OCP\Defaults();
  39. if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['itemSource'])) {
  40. switch ($_POST['action']) {
  41. case 'share':
  42. if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
  43. try {
  44. $shareType = (int)$_POST['shareType'];
  45. $shareWith = $_POST['shareWith'];
  46. $itemSourceName = isset($_POST['itemSourceName']) ? (string)$_POST['itemSourceName'] : null;
  47. if ($shareType === OCP\Share::SHARE_TYPE_LINK && $shareWith == '') {
  48. $shareWith = null;
  49. }
  50. $itemSourceName=(isset($_POST['itemSourceName'])) ? (string)$_POST['itemSourceName']:'';
  51. $token = OCP\Share::shareItem(
  52. $_POST['itemType'],
  53. $_POST['itemSource'],
  54. $shareType,
  55. $shareWith,
  56. $_POST['permissions'],
  57. $itemSourceName,
  58. (!empty($_POST['expirationDate']) ? new \DateTime((string)$_POST['expirationDate']) : null)
  59. );
  60. if (is_string($token)) {
  61. OC_JSON::success(array('data' => array('token' => $token)));
  62. } else {
  63. OC_JSON::success();
  64. }
  65. } catch (\OC\HintException $exception) {
  66. OC_JSON::error(array('data' => array('message' => $exception->getHint())));
  67. } catch (Exception $exception) {
  68. OC_JSON::error(array('data' => array('message' => $exception->getMessage())));
  69. }
  70. }
  71. break;
  72. case 'unshare':
  73. if (isset($_POST['shareType']) && isset($_POST['shareWith'])) {
  74. if ((int)$_POST['shareType'] === OCP\Share::SHARE_TYPE_LINK && $_POST['shareWith'] == '') {
  75. $shareWith = null;
  76. } else {
  77. $shareWith = (string)$_POST['shareWith'];
  78. }
  79. $return = OCP\Share::unshare((string)$_POST['itemType'],(string) $_POST['itemSource'], (int)$_POST['shareType'], $shareWith);
  80. ($return) ? OC_JSON::success() : OC_JSON::error();
  81. }
  82. break;
  83. case 'setPermissions':
  84. if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
  85. $return = OCP\Share::setPermissions(
  86. (string)$_POST['itemType'],
  87. (string)$_POST['itemSource'],
  88. (int)$_POST['shareType'],
  89. (string)$_POST['shareWith'],
  90. (int)$_POST['permissions']
  91. );
  92. ($return) ? OC_JSON::success() : OC_JSON::error();
  93. }
  94. break;
  95. case 'setExpirationDate':
  96. if (isset($_POST['date'])) {
  97. try {
  98. $return = OCP\Share::setExpirationDate((string)$_POST['itemType'], (string)$_POST['itemSource'], (string)$_POST['date']);
  99. ($return) ? OC_JSON::success() : OC_JSON::error();
  100. } catch (\Exception $e) {
  101. OC_JSON::error(array('data' => array('message' => $e->getMessage())));
  102. }
  103. }
  104. break;
  105. case 'informRecipients':
  106. $l = \OC::$server->getL10N('core');
  107. $shareType = (int) $_POST['shareType'];
  108. $itemType = (string)$_POST['itemType'];
  109. $itemSource = (string)$_POST['itemSource'];
  110. $recipient = (string)$_POST['recipient'];
  111. if($shareType === \OCP\Share::SHARE_TYPE_USER) {
  112. $recipientList[] = $recipient;
  113. } elseif ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  114. $recipientList = \OC_Group::usersInGroup($recipient);
  115. }
  116. // don't send a mail to the user who shared the file
  117. $recipientList = array_diff($recipientList, array(\OCP\User::getUser()));
  118. $mailNotification = new \OC\Share\MailNotifications(
  119. \OC::$server->getUserSession()->getUser()->getUID(),
  120. \OC::$server->getConfig(),
  121. \OC::$server->getL10N('lib'),
  122. \OC::$server->getMailer(),
  123. \OC::$server->getLogger(),
  124. $defaults
  125. );
  126. $result = $mailNotification->sendInternalShareMail($recipientList, $itemSource, $itemType);
  127. \OCP\Share::setSendMailStatus($itemType, $itemSource, $shareType, $recipient, true);
  128. if (empty($result)) {
  129. OCP\JSON::success();
  130. } else {
  131. OCP\JSON::error(array(
  132. 'data' => array(
  133. 'message' => $l->t("Couldn't send mail to following users: %s ",
  134. implode(', ', $result)
  135. )
  136. )
  137. ));
  138. }
  139. break;
  140. case 'informRecipientsDisabled':
  141. $itemSource = (string)$_POST['itemSource'];
  142. $shareType = (int)$_POST['shareType'];
  143. $itemType = (string)$_POST['itemType'];
  144. $recipient = (string)$_POST['recipient'];
  145. \OCP\Share::setSendMailStatus($itemType, $itemSource, $shareType, $recipient, false);
  146. OCP\JSON::success();
  147. break;
  148. case 'email':
  149. // read post variables
  150. $link = (string)$_POST['link'];
  151. $file = (string)$_POST['file'];
  152. $to_address = (string)$_POST['toaddress'];
  153. $mailNotification = new \OC\Share\MailNotifications(
  154. \OC::$server->getUserSession()->getUser()->getUID(),
  155. \OC::$server->getConfig(),
  156. \OC::$server->getL10N('lib'),
  157. \OC::$server->getMailer(),
  158. \OC::$server->getLogger(),
  159. $defaults
  160. );
  161. $expiration = null;
  162. if (isset($_POST['expiration']) && $_POST['expiration'] !== '') {
  163. try {
  164. $date = new DateTime((string)$_POST['expiration']);
  165. $expiration = $date->getTimestamp();
  166. } catch (Exception $e) {
  167. \OCP\Util::writeLog('sharing', "Couldn't read date: " . $e->getMessage(), \OCP\Util::ERROR);
  168. }
  169. }
  170. $result = $mailNotification->sendLinkShareMail($to_address, $file, $link, $expiration);
  171. if(empty($result)) {
  172. \OCP\JSON::success();
  173. } else {
  174. $l = \OC::$server->getL10N('core');
  175. OCP\JSON::error(array(
  176. 'data' => array(
  177. 'message' => $l->t("Couldn't send mail to following users: %s ",
  178. implode(', ', $result)
  179. )
  180. )
  181. ));
  182. }
  183. break;
  184. }
  185. } else if (isset($_GET['fetch'])) {
  186. switch ($_GET['fetch']) {
  187. case 'getItemsSharedStatuses':
  188. if (isset($_GET['itemType'])) {
  189. $return = OCP\Share::getItemsShared((string)$_GET['itemType'], OCP\Share::FORMAT_STATUSES);
  190. is_array($return) ? OC_JSON::success(array('data' => $return)) : OC_JSON::error();
  191. }
  192. break;
  193. case 'getItem':
  194. if (isset($_GET['itemType'])
  195. && isset($_GET['itemSource'])
  196. && isset($_GET['checkReshare'])
  197. && isset($_GET['checkShares'])) {
  198. if ($_GET['checkReshare'] == 'true') {
  199. $reshare = OCP\Share::getItemSharedWithBySource(
  200. (string)$_GET['itemType'],
  201. (string)$_GET['itemSource'],
  202. OCP\Share::FORMAT_NONE,
  203. null,
  204. true
  205. );
  206. } else {
  207. $reshare = false;
  208. }
  209. if ($_GET['checkShares'] == 'true') {
  210. $shares = OCP\Share::getItemShared(
  211. (string)$_GET['itemType'],
  212. (string)$_GET['itemSource'],
  213. OCP\Share::FORMAT_NONE,
  214. null,
  215. true
  216. );
  217. } else {
  218. $shares = false;
  219. }
  220. OC_JSON::success(array('data' => array('reshare' => $reshare, 'shares' => $shares)));
  221. }
  222. break;
  223. case 'getShareWithEmail':
  224. $result = array();
  225. if (isset($_GET['search'])) {
  226. $cm = OC::$server->getContactsManager();
  227. if (!is_null($cm) && $cm->isEnabled()) {
  228. $contacts = $cm->search((string)$_GET['search'], array('FN', 'EMAIL'));
  229. foreach ($contacts as $contact) {
  230. if (!isset($contact['EMAIL'])) {
  231. continue;
  232. }
  233. $emails = $contact['EMAIL'];
  234. if (!is_array($emails)) {
  235. $emails = array($emails);
  236. }
  237. foreach($emails as $email) {
  238. $result[] = array(
  239. 'id' => $contact['id'],
  240. 'email' => $email,
  241. 'displayname' => $contact['FN'],
  242. );
  243. }
  244. }
  245. }
  246. }
  247. OC_JSON::success(array('data' => $result));
  248. break;
  249. case 'getShareWith':
  250. if (isset($_GET['search'])) {
  251. $shareWithinGroupOnly = OC\Share\Share::shareWithGroupMembersOnly();
  252. $shareWith = array();
  253. $groups = OC_Group::getGroups((string)$_GET['search']);
  254. if ($shareWithinGroupOnly) {
  255. $usergroups = OC_Group::getUserGroups(OC_User::getUser());
  256. $groups = array_intersect($groups, $usergroups);
  257. }
  258. $sharedUsers = [];
  259. $sharedGroups = [];
  260. if (isset($_GET['itemShares'])) {
  261. if (isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_USER]) &&
  262. is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_USER])) {
  263. $sharedUsers = $_GET['itemShares'][OCP\Share::SHARE_TYPE_USER];
  264. }
  265. if (isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP]) &&
  266. is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])) {
  267. $sharedGroups = isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP]);
  268. }
  269. }
  270. $count = 0;
  271. $users = array();
  272. $limit = 0;
  273. $offset = 0;
  274. // limit defaults to 15 if not specified via request parameter and can be no larger than 500
  275. $request_limit = min((int)$_GET['limit'] ?: 15, 500);
  276. while ($count < $request_limit && count($users) == $limit) {
  277. $limit = $request_limit - $count;
  278. if ($shareWithinGroupOnly) {
  279. $users = OC_Group::displayNamesInGroups($usergroups, (string)$_GET['search'], $limit, $offset);
  280. } else {
  281. $users = OC_User::getDisplayNames((string)$_GET['search'], $limit, $offset);
  282. }
  283. $offset += $limit;
  284. foreach ($users as $uid => $displayName) {
  285. if (in_array($uid, $sharedUsers)) {
  286. continue;
  287. }
  288. if ((!isset($_GET['itemShares'])
  289. || !is_array((string)$_GET['itemShares'][OCP\Share::SHARE_TYPE_USER])
  290. || !in_array($uid, (string)$_GET['itemShares'][OCP\Share::SHARE_TYPE_USER]))
  291. && $uid != OC_User::getUser()) {
  292. $shareWith[] = array(
  293. 'label' => $displayName,
  294. 'value' => array(
  295. 'shareType' => OCP\Share::SHARE_TYPE_USER,
  296. 'shareWith' => $uid)
  297. );
  298. $count++;
  299. }
  300. }
  301. }
  302. $count = 0;
  303. // enable l10n support
  304. $l = \OC::$server->getL10N('core');
  305. foreach ($groups as $group) {
  306. if (in_array($group, $sharedGroups)) {
  307. continue;
  308. }
  309. if ($count < $request_limit) {
  310. if (!isset($_GET['itemShares'])
  311. || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  312. || !is_array((string)$_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  313. || !in_array($group, (string)$_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])) {
  314. $shareWith[] = array(
  315. 'label' => $group,
  316. 'value' => array(
  317. 'shareType' => OCP\Share::SHARE_TYPE_GROUP,
  318. 'shareWith' => $group
  319. )
  320. );
  321. $count++;
  322. }
  323. } else {
  324. break;
  325. }
  326. }
  327. // allow user to add unknown remote addresses for server-to-server share
  328. $backend = \OCP\Share::getBackend((string)$_GET['itemType']);
  329. if ($backend->isShareTypeAllowed(\OCP\Share::SHARE_TYPE_REMOTE)) {
  330. if (substr_count((string)$_GET['search'], '@') >= 1) {
  331. $shareWith[] = array(
  332. 'label' => (string)$_GET['search'],
  333. 'value' => array(
  334. 'shareType' => \OCP\Share::SHARE_TYPE_REMOTE,
  335. 'shareWith' => (string)$_GET['search']
  336. )
  337. );
  338. }
  339. $contactManager = \OC::$server->getContactsManager();
  340. $addressBookContacts = $contactManager->search($_GET['search'], ['CLOUD', 'FN']);
  341. foreach ($addressBookContacts as $contact) {
  342. if (isset($contact['CLOUD'])) {
  343. foreach ($contact['CLOUD'] as $cloudId) {
  344. $shareWith[] = array(
  345. 'label' => $contact['FN'] . ' (' . $cloudId . ')',
  346. 'value' => array(
  347. 'shareType' => \OCP\Share::SHARE_TYPE_REMOTE,
  348. 'shareWith' => $cloudId
  349. )
  350. );
  351. }
  352. }
  353. }
  354. }
  355. $sorter = new \OC\Share\SearchResultSorter((string)$_GET['search'],
  356. 'label',
  357. \OC::$server->getLogger());
  358. usort($shareWith, array($sorter, 'sort'));
  359. OC_JSON::success(array('data' => $shareWith));
  360. }
  361. break;
  362. }
  363. }