helper.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Miguel Prokop <miguel.prokop@vtu.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Share;
  28. class Helper extends \OC\Share\Constants {
  29. /**
  30. * Generate a unique target for the item
  31. * @param string $itemType
  32. * @param string $itemSource
  33. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  34. * @param string $shareWith User or group the item is being shared with
  35. * @param string $uidOwner User that is the owner of shared item
  36. * @param string $suggestedTarget The suggested target originating from a reshare (optional)
  37. * @param int $groupParent The id of the parent group share (optional)
  38. * @throws \Exception
  39. * @return string Item target
  40. */
  41. public static function generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $suggestedTarget = null, $groupParent = null) {
  42. // FIXME: $uidOwner and $groupParent seems to be unused
  43. $backend = \OC\Share\Share::getBackend($itemType);
  44. if ($shareType === self::SHARE_TYPE_LINK || $shareType === self::SHARE_TYPE_REMOTE) {
  45. if (isset($suggestedTarget)) {
  46. return $suggestedTarget;
  47. }
  48. return $backend->generateTarget($itemSource, false);
  49. } else {
  50. if ($shareType == self::SHARE_TYPE_USER) {
  51. // Share with is a user, so set share type to user and groups
  52. $shareType = self::$shareTypeUserAndGroups;
  53. }
  54. // Check if suggested target exists first
  55. if (!isset($suggestedTarget)) {
  56. $suggestedTarget = $itemSource;
  57. }
  58. if ($shareType == self::SHARE_TYPE_GROUP) {
  59. $target = $backend->generateTarget($suggestedTarget, false);
  60. } else {
  61. $target = $backend->generateTarget($suggestedTarget, $shareWith);
  62. }
  63. return $target;
  64. }
  65. }
  66. /**
  67. * Delete all reshares and group share children of an item
  68. * @param int $parent Id of item to delete
  69. * @param bool $excludeParent If true, exclude the parent from the delete (optional)
  70. * @param string $uidOwner The user that the parent was shared with (optional)
  71. * @param int $newParent new parent for the childrens
  72. * @param bool $excludeGroupChildren exclude group children elements
  73. */
  74. public static function delete($parent, $excludeParent = false, $uidOwner = null, $newParent = null, $excludeGroupChildren = false) {
  75. $ids = array($parent);
  76. $deletedItems = array();
  77. $changeParent = array();
  78. $parents = array($parent);
  79. while (!empty($parents)) {
  80. $parents = "'".implode("','", $parents)."'";
  81. // Check the owner on the first search of reshares, useful for
  82. // finding and deleting the reshares by a single user of a group share
  83. $params = array();
  84. if (count($ids) == 1 && isset($uidOwner)) {
  85. // FIXME: don't concat $parents, use Docrine's PARAM_INT_ARRAY approach
  86. $queryString = 'SELECT `id`, `share_with`, `item_type`, `share_type`, ' .
  87. '`item_target`, `file_target`, `parent` ' .
  88. 'FROM `*PREFIX*share` ' .
  89. 'WHERE `parent` IN ('.$parents.') AND `uid_owner` = ? ';
  90. $params[] = $uidOwner;
  91. } else {
  92. $queryString = 'SELECT `id`, `share_with`, `item_type`, `share_type`, ' .
  93. '`item_target`, `file_target`, `parent`, `uid_owner` ' .
  94. 'FROM `*PREFIX*share` WHERE `parent` IN ('.$parents.') ';
  95. }
  96. if ($excludeGroupChildren) {
  97. $queryString .= ' AND `share_type` != ?';
  98. $params[] = self::$shareTypeGroupUserUnique;
  99. }
  100. $query = \OC_DB::prepare($queryString);
  101. $result = $query->execute($params);
  102. // Reset parents array, only go through loop again if items are found
  103. $parents = array();
  104. while ($item = $result->fetchRow()) {
  105. $tmpItem = array(
  106. 'id' => $item['id'],
  107. 'shareWith' => $item['share_with'],
  108. 'itemTarget' => $item['item_target'],
  109. 'itemType' => $item['item_type'],
  110. 'shareType' => (int)$item['share_type'],
  111. );
  112. if (isset($item['file_target'])) {
  113. $tmpItem['fileTarget'] = $item['file_target'];
  114. }
  115. // if we have a new parent for the child we remember the child
  116. // to update the parent, if not we add it to the list of items
  117. // which should be deleted
  118. if ($newParent !== null) {
  119. $changeParent[] = $item['id'];
  120. } else {
  121. $deletedItems[] = $tmpItem;
  122. $ids[] = $item['id'];
  123. $parents[] = $item['id'];
  124. }
  125. }
  126. }
  127. if ($excludeParent) {
  128. unset($ids[0]);
  129. }
  130. if (!empty($changeParent)) {
  131. $idList = "'".implode("','", $changeParent)."'";
  132. $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `parent` = ? WHERE `id` IN ('.$idList.')');
  133. $query->execute(array($newParent));
  134. }
  135. if (!empty($ids)) {
  136. $idList = "'".implode("','", $ids)."'";
  137. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `id` IN ('.$idList.')');
  138. $query->execute();
  139. }
  140. return $deletedItems;
  141. }
  142. /**
  143. * get default expire settings defined by the admin
  144. * @return array contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
  145. */
  146. public static function getDefaultExpireSetting() {
  147. $config = \OC::$server->getConfig();
  148. $defaultExpireSettings = array('defaultExpireDateSet' => false);
  149. // get default expire settings
  150. $defaultExpireDate = $config->getAppValue('core', 'shareapi_default_expire_date', 'no');
  151. if ($defaultExpireDate === 'yes') {
  152. $enforceExpireDate = $config->getAppValue('core', 'shareapi_enforce_expire_date', 'no');
  153. $defaultExpireSettings['defaultExpireDateSet'] = true;
  154. $defaultExpireSettings['expireAfterDays'] = (int)($config->getAppValue('core', 'shareapi_expire_after_n_days', '7'));
  155. $defaultExpireSettings['enforceExpireDate'] = $enforceExpireDate === 'yes' ? true : false;
  156. }
  157. return $defaultExpireSettings;
  158. }
  159. public static function calcExpireDate() {
  160. $expireAfter = \OC\Share\Share::getExpireInterval() * 24 * 60 * 60;
  161. $expireAt = time() + $expireAfter;
  162. $date = new \DateTime();
  163. $date->setTimestamp($expireAt);
  164. $date->setTime(0, 0, 0);
  165. //$dateString = $date->format('Y-m-d') . ' 00:00:00';
  166. return $date;
  167. }
  168. /**
  169. * calculate expire date
  170. * @param array $defaultExpireSettings contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
  171. * @param int $creationTime timestamp when the share was created
  172. * @param int $userExpireDate expire timestamp set by the user
  173. * @return mixed integer timestamp or False
  174. */
  175. public static function calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate = null) {
  176. $expires = false;
  177. $defaultExpires = null;
  178. if (!empty($defaultExpireSettings['defaultExpireDateSet'])) {
  179. $defaultExpires = $creationTime + $defaultExpireSettings['expireAfterDays'] * 86400;
  180. }
  181. if (isset($userExpireDate)) {
  182. // if the admin decided to enforce the default expire date then we only take
  183. // the user defined expire date of it is before the default expire date
  184. if ($defaultExpires && !empty($defaultExpireSettings['enforceExpireDate'])) {
  185. $expires = min($userExpireDate, $defaultExpires);
  186. } else {
  187. $expires = $userExpireDate;
  188. }
  189. } else if ($defaultExpires && !empty($defaultExpireSettings['enforceExpireDate'])) {
  190. $expires = $defaultExpires;
  191. }
  192. return $expires;
  193. }
  194. /**
  195. * Extracts the necessary remote name from a given link
  196. *
  197. * Strips away a potential file name, to allow
  198. * - user
  199. * - user@localhost
  200. * - user@http://localhost
  201. * - user@http://localhost/
  202. * - user@http://localhost/index.php
  203. * - user@http://localhost/index.php/s/{shareToken}
  204. *
  205. * @param string $shareWith
  206. * @return string
  207. */
  208. public static function fixRemoteURLInShareWith($shareWith) {
  209. if (strpos($shareWith, '@')) {
  210. list($user, $remote) = explode('@', $shareWith, 2);
  211. $remote = str_replace('\\', '/', $remote);
  212. if ($fileNamePosition = strpos($remote, '/index.php')) {
  213. $remote = substr($remote, 0, $fileNamePosition);
  214. }
  215. $remote = rtrim($remote, '/');
  216. $shareWith = $user . '@' . $remote;
  217. }
  218. return rtrim($shareWith, '/');
  219. }
  220. }