diff options
author | Bjoern Schiessle <schiessle@owncloud.com> | 2014-07-23 16:42:33 +0200 |
---|---|---|
committer | Bjoern Schiessle <schiessle@owncloud.com> | 2014-07-25 13:38:18 +0200 |
commit | ecc1f92bb6c7ac3121c35affad607a344a79ef52 (patch) | |
tree | faf5bb913b3750f809cdd6e7a4ab6f835a2dca6e /lib/private | |
parent | 9834e3854201d68fbd5dcbef09122c48cc5bcb10 (diff) | |
download | nextcloud-server-ecc1f92bb6c7ac3121c35affad607a344a79ef52.tar.gz nextcloud-server-ecc1f92bb6c7ac3121c35affad607a344a79ef52.zip |
add OCS api call to set expire date for link shares
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/share/share.php | 59 | ||||
-rwxr-xr-x | lib/private/util.php | 19 |
2 files changed, 71 insertions, 7 deletions
diff --git a/lib/private/share/share.php b/lib/private/share/share.php index 673c0dc383a..af286e7154e 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -925,19 +925,69 @@ class Share extends \OC\Share\Constants { } /** + * validate expire date if it meets all constraints + * + * @param string $expireDate well formate date string, e.g. "DD-MM-YYYY" + * @param string $shareTime timestamp when the file was shared + * @param string $itemType + * @param string $itemSource + * @return DateTime validated date + * @throws \Exception + */ + private static function validateExpireDate($expireDate, $shareTime, $itemType, $itemSource) { + $l = \OC_L10N::get('lib'); + $date = new \DateTime($expireDate); + $today = new \DateTime('now'); + + // if the user doesn't provide a share time we need to get it from the database + // fall-back mode to keep API stable, because the $shareTime parameter was added later + $defaultExpireDateEnforced = \OCP\Util::isDefaultExpireDateEnforced(); + if ($defaultExpireDateEnforced && $shareTime === null) { + $items = self::getItemShared($itemType, $itemSource); + $firstItem = reset($items); + $shareTime = (int)$firstItem['stime']; + } + + if ($defaultExpireDateEnforced) { + // initialize max date with share time + $maxDate = new \DateTime(); + $maxDate->setTimestamp($shareTime); + $maxDays = \OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); + $maxDate->add(new \DateInterval('P' . $maxDays . 'D')); + if ($date > $maxDate) { + $warning = 'Can not set expire date. Shares can not expire later then ' . $maxDays . ' after they where shared'; + $warning_t = $l->t('Can not set expire date. Shares can not expire later then %s after they where shared', array($maxDays)); + \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN); + throw new \Exception($warning_t); + } + } + + if ($date < $today) { + $message = 'Can not set expire date. Expire date is in the past'; + $message_t = $l->t('Can not set expire date. Expire date is in the past'); + \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::WARN); + throw new \Exception($message_t); + } + + return $date; + } + + /** * Set expiration date for a share * @param string $itemType * @param string $itemSource * @param string $date expiration date + * @param int $shareTime timestamp from when the file was shared + * @throws \Exception * @return boolean */ - public static function setExpirationDate($itemType, $itemSource, $date) { + public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) { $user = \OC_User::getUser(); if ($date == '') { $date = null; } else { - $date = new \DateTime($date); + $date = self::validateExpireDate($date, $shareTime, $itemType, $itemSource); } $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ? AND `uid_owner` = ? AND `share_type` = ?'); $query->bindValue(1, $date, 'datetime'); @@ -954,11 +1004,10 @@ class Share extends \OC\Share\Constants { 'date' => $date, 'uidOwner' => $user )); - + return true; + } - } - /** * Checks whether a share has expired, calls unshareItem() if yes. * @param array $item Share data (usually database row) diff --git a/lib/private/util.php b/lib/private/util.php index eea194288f9..47958ba7a0c 100755 --- a/lib/private/util.php +++ b/lib/private/util.php @@ -22,7 +22,7 @@ class OC_Util { self::$rootMounted = true; } } - + /** * mounting an object storage as the root fs will in essence remove the * necessity of a data folder being present. @@ -50,7 +50,7 @@ class OC_Util { self::$rootMounted = true; } } - + /** * Can be set up * @param string $user @@ -171,6 +171,21 @@ class OC_Util { } /** + * check if share API enforces a default expire date + * @return boolean + */ + public static function isDefaultExpireDateEnforced() { + $isDefaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no'); + $enforceDefaultExpireDate = false; + if ($isDefaultExpireDateEnabled === 'yes') { + $value = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no'); + $enforceDefaultExpireDate = ($value === 'yes') ? true : false; + } + + return $enforceDefaultExpireDate; + } + + /** * Get the quota of a user * @param string $user * @return int Quota bytes |