share.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. $defaults = new \OCP\Defaults();
  25. if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['itemSource'])) {
  26. switch ($_POST['action']) {
  27. case 'share':
  28. if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
  29. try {
  30. $shareType = (int)$_POST['shareType'];
  31. $shareWith = $_POST['shareWith'];
  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. );
  42. if (is_string($token)) {
  43. OC_JSON::success(array('data' => array('token' => $token)));
  44. } else {
  45. OC_JSON::success();
  46. }
  47. } catch (Exception $exception) {
  48. OC_JSON::error(array('data' => array('message' => $exception->getMessage())));
  49. }
  50. }
  51. break;
  52. case 'unshare':
  53. if (isset($_POST['shareType']) && isset($_POST['shareWith'])) {
  54. if ((int)$_POST['shareType'] === OCP\Share::SHARE_TYPE_LINK && $_POST['shareWith'] == '') {
  55. $shareWith = null;
  56. } else {
  57. $shareWith = $_POST['shareWith'];
  58. }
  59. $return = OCP\Share::unshare($_POST['itemType'], $_POST['itemSource'], $_POST['shareType'], $shareWith);
  60. ($return) ? OC_JSON::success() : OC_JSON::error();
  61. }
  62. break;
  63. case 'setPermissions':
  64. if (isset($_POST['shareType']) && isset($_POST['shareWith']) && isset($_POST['permissions'])) {
  65. $return = OCP\Share::setPermissions(
  66. $_POST['itemType'],
  67. $_POST['itemSource'],
  68. $_POST['shareType'],
  69. $_POST['shareWith'],
  70. $_POST['permissions']
  71. );
  72. ($return) ? OC_JSON::success() : OC_JSON::error();
  73. }
  74. break;
  75. case 'setExpirationDate':
  76. if (isset($_POST['date'])) {
  77. $return = OCP\Share::setExpirationDate($_POST['itemType'], $_POST['itemSource'], $_POST['date']);
  78. ($return) ? OC_JSON::success() : OC_JSON::error();
  79. }
  80. break;
  81. case 'informRecipients':
  82. $l = OC_L10N::get('core');
  83. $shareType = (int) $_POST['shareType'];
  84. $itemType = $_POST['itemType'];
  85. $itemSource = $_POST['itemSource'];
  86. $recipient = $_POST['recipient'];
  87. $ownerDisplayName = \OCP\User::getDisplayName();
  88. $from = \OCP\Util::getDefaultEmailAddress('sharing-noreply');
  89. $noMail = array();
  90. $recipientList = array();
  91. if($shareType === \OCP\Share::SHARE_TYPE_USER) {
  92. $recipientList[] = $recipient;
  93. } elseif ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  94. $recipientList = \OC_Group::usersInGroup($recipient);
  95. }
  96. // don't send a mail to the user who shared the file
  97. $recipientList = array_diff($recipientList, array(\OCP\User::getUser()));
  98. // send mail to all recipients with an email address
  99. foreach ($recipientList as $recipient) {
  100. //get correct target folder name
  101. $email = OC_Preferences::getValue($recipient, 'settings', 'email', '');
  102. if ($email !== '') {
  103. $displayName = \OCP\User::getDisplayName($recipient);
  104. $items = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient);
  105. $filename = trim($items[0]['file_target'], '/');
  106. $subject = (string)$l->t('%s shared »%s« with you', array($ownerDisplayName, $filename));
  107. $expiration = null;
  108. if (isset($items[0]['expiration'])) {
  109. $date = new DateTime($items[0]['expiration']);
  110. $expiration = $date->format('Y-m-d');
  111. }
  112. if ($itemType === 'folder') {
  113. $foldername = "/Shared/" . $filename;
  114. } else {
  115. // if it is a file we can just link to the Shared folder,
  116. // that's the place where the user will find the file
  117. $foldername = "/Shared";
  118. }
  119. $link = \OCP\Util::linkToAbsolute('files', 'index.php', array("dir" => $foldername));
  120. $content = new OC_Template("core", "mail", "");
  121. $content->assign('link', $link);
  122. $content->assign('user_displayname', $ownerDisplayName);
  123. $content->assign('filename', $filename);
  124. $content->assign('expiration', $expiration);
  125. $text = $content->fetchPage();
  126. $content = new OC_Template("core", "altmail", "");
  127. $content->assign('link', $link);
  128. $content->assign('user_displayname', $ownerDisplayName);
  129. $content->assign('filename', $filename);
  130. $content->assign('expiration', $expiration);
  131. $alttext = $content->fetchPage();
  132. $default_from = OCP\Util::getDefaultEmailAddress('sharing-noreply');
  133. $from = OCP\Config::getUserValue(\OCP\User::getUser(), 'settings', 'email', $default_from);
  134. // send it out now
  135. try {
  136. OCP\Util::sendMail($email, $displayName, $subject, $text, $from, $ownerDisplayName, 1, $alttext);
  137. } catch (Exception $exception) {
  138. $noMail[] = \OCP\User::getDisplayName($recipient);
  139. }
  140. }
  141. }
  142. \OCP\Share::setSendMailStatus($itemType, $itemSource, $shareType, true);
  143. if (empty($noMail)) {
  144. OCP\JSON::success();
  145. } else {
  146. OCP\JSON::error(array(
  147. 'data' => array(
  148. 'message' => $l->t("Couldn't send mail to following users: %s ",
  149. implode(', ', $noMail)
  150. )
  151. )
  152. ));
  153. }
  154. break;
  155. case 'informRecipientsDisabled':
  156. $itemSource = $_POST['itemSource'];
  157. $shareType = $_POST['shareType'];
  158. $itemType = $_POST['itemType'];
  159. $recipient = $_POST['recipient'];
  160. \OCP\Share::setSendMailStatus($itemType, $itemSource, $shareType, false);
  161. OCP\JSON::success();
  162. break;
  163. case 'email':
  164. // read post variables
  165. $user = OCP\USER::getUser();
  166. $displayName = OCP\User::getDisplayName();
  167. $type = $_POST['itemType'];
  168. $link = $_POST['link'];
  169. $file = $_POST['file'];
  170. $to_address = $_POST['toaddress'];
  171. // enable l10n support
  172. $l = OC_L10N::get('core');
  173. // setup the email
  174. $subject = (string)$l->t('%s shared »%s« with you', array($displayName, $file));
  175. $content = new OC_Template("core", "mail", "");
  176. $content->assign ('link', $link);
  177. $content->assign ('type', $type);
  178. $content->assign ('user_displayname', $displayName);
  179. $content->assign ('filename', $file);
  180. $text = $content->fetchPage();
  181. $content = new OC_Template("core", "altmail", "");
  182. $content->assign ('link', $link);
  183. $content->assign ('type', $type);
  184. $content->assign ('user_displayname', $displayName);
  185. $content->assign ('filename', $file);
  186. $alttext = $content->fetchPage();
  187. $default_from = OCP\Util::getDefaultEmailAddress('sharing-noreply');
  188. $from_address = OCP\Config::getUserValue($user, 'settings', 'email', $default_from );
  189. // send it out now
  190. try {
  191. OCP\Util::sendMail($to_address, $to_address, $subject, $text, $from_address, $displayName, 1, $alttext);
  192. OCP\JSON::success();
  193. } catch (Exception $exception) {
  194. OCP\JSON::error(array('data' => array('message' => OC_Util::sanitizeHTML($exception->getMessage()))));
  195. }
  196. break;
  197. }
  198. } else if (isset($_GET['fetch'])) {
  199. switch ($_GET['fetch']) {
  200. case 'getItemsSharedStatuses':
  201. if (isset($_GET['itemType'])) {
  202. $return = OCP\Share::getItemsShared($_GET['itemType'], OCP\Share::FORMAT_STATUSES);
  203. is_array($return) ? OC_JSON::success(array('data' => $return)) : OC_JSON::error();
  204. }
  205. break;
  206. case 'getItem':
  207. if (isset($_GET['itemType'])
  208. && isset($_GET['itemSource'])
  209. && isset($_GET['checkReshare'])
  210. && isset($_GET['checkShares'])) {
  211. if ($_GET['checkReshare'] == 'true') {
  212. $reshare = OCP\Share::getItemSharedWithBySource(
  213. $_GET['itemType'],
  214. $_GET['itemSource'],
  215. OCP\Share::FORMAT_NONE,
  216. null,
  217. true
  218. );
  219. } else {
  220. $reshare = false;
  221. }
  222. if ($_GET['checkShares'] == 'true') {
  223. $shares = OCP\Share::getItemShared(
  224. $_GET['itemType'],
  225. $_GET['itemSource'],
  226. OCP\Share::FORMAT_NONE,
  227. null,
  228. true
  229. );
  230. } else {
  231. $shares = false;
  232. }
  233. OC_JSON::success(array('data' => array('reshare' => $reshare, 'shares' => $shares)));
  234. }
  235. break;
  236. case 'getShareWith':
  237. if (isset($_GET['search'])) {
  238. $sharePolicy = OC_Appconfig::getValue('core', 'shareapi_share_policy', 'global');
  239. $shareWith = array();
  240. // if (OC_App::isEnabled('contacts')) {
  241. // // TODO Add function to contacts to only get the 'fullname' column to improve performance
  242. // $ids = OC_Contacts_Addressbook::activeIds();
  243. // foreach ($ids as $id) {
  244. // $vcards = OC_Contacts_VCard::all($id);
  245. // foreach ($vcards as $vcard) {
  246. // $contact = $vcard['fullname'];
  247. // if (stripos($contact, $_GET['search']) !== false
  248. // && (!isset($_GET['itemShares'])
  249. // || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT])
  250. // || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT])
  251. // || !in_array($contact, $_GET['itemShares'][OCP\Share::SHARE_TYPE_CONTACT]))) {
  252. // $shareWith[] = array('label' => $contact, 'value' => array('shareType' => 5, 'shareWith' => $vcard['id']));
  253. // }
  254. // }
  255. // }
  256. // }
  257. $groups = OC_Group::getGroups($_GET['search']);
  258. if ($sharePolicy == 'groups_only') {
  259. $usergroups = OC_Group::getUserGroups(OC_User::getUser());
  260. $groups = array_intersect($groups, $usergroups);
  261. }
  262. $count = 0;
  263. $users = array();
  264. $limit = 0;
  265. $offset = 0;
  266. while ($count < 15 && count($users) == $limit) {
  267. $limit = 15 - $count;
  268. if ($sharePolicy == 'groups_only') {
  269. $users = OC_Group::DisplayNamesInGroups($usergroups, $_GET['search'], $limit, $offset);
  270. } else {
  271. $users = OC_User::getDisplayNames($_GET['search'], $limit, $offset);
  272. }
  273. $offset += $limit;
  274. foreach ($users as $uid => $displayName) {
  275. if ((!isset($_GET['itemShares'])
  276. || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_USER])
  277. || !in_array($uid, $_GET['itemShares'][OCP\Share::SHARE_TYPE_USER]))
  278. && $uid != OC_User::getUser()) {
  279. $shareWith[] = array(
  280. 'label' => $displayName,
  281. 'value' => array(
  282. 'shareType' => OCP\Share::SHARE_TYPE_USER,
  283. 'shareWith' => $uid)
  284. );
  285. $count++;
  286. }
  287. }
  288. }
  289. $count = 0;
  290. // enable l10n support
  291. $l = OC_L10N::get('core');
  292. foreach ($groups as $group) {
  293. if ($count < 15) {
  294. if (!isset($_GET['itemShares'])
  295. || !isset($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  296. || !is_array($_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])
  297. || !in_array($group, $_GET['itemShares'][OCP\Share::SHARE_TYPE_GROUP])) {
  298. $shareWith[] = array(
  299. 'label' => $group,
  300. 'value' => array(
  301. 'shareType' => OCP\Share::SHARE_TYPE_GROUP,
  302. 'shareWith' => $group
  303. )
  304. );
  305. $count++;
  306. }
  307. } else {
  308. break;
  309. }
  310. }
  311. OC_JSON::success(array('data' => $shareWith));
  312. }
  313. break;
  314. }
  315. }