summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorBjörn Schießle <schiessle@owncloud.com>2014-07-28 17:28:32 +0200
committerBjörn Schießle <schiessle@owncloud.com>2014-07-28 17:28:32 +0200
commitc53b56e3139e517af4bca2aca631f625f435f74e (patch)
treefaea592fb96f60e69036bb43e7ccba2d35a808f8 /lib
parent1fbbaed26149cd2261637e5a737b22089340345b (diff)
parentb7958f79c39b78304daa19ece96072acec50c2ba (diff)
downloadnextcloud-server-c53b56e3139e517af4bca2aca631f625f435f74e.tar.gz
nextcloud-server-c53b56e3139e517af4bca2aca631f625f435f74e.zip
Merge pull request #9798 from owncloud/ocs_share_api_add_expire_date
[share api] add OCS api call to set expire date for link shares
Diffstat (limited to 'lib')
-rw-r--r--lib/private/share/share.php59
-rwxr-xr-xlib/private/util.php19
-rw-r--r--lib/public/share.php5
-rw-r--r--lib/public/util.php9
4 files changed, 83 insertions, 9 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 67da7a2f63f..896b076afa6 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
diff --git a/lib/public/share.php b/lib/public/share.php
index 8566a38c61e..c0939dce53f 100644
--- a/lib/public/share.php
+++ b/lib/public/share.php
@@ -298,10 +298,11 @@ class Share extends \OC\Share\Constants {
* @param string $itemType
* @param string $itemSource
* @param string $date expiration date
+ * @param int $shareTime timestamp from when the file was shared
* @return boolean
*/
- public static function setExpirationDate($itemType, $itemSource, $date) {
- return \OC\Share\Share::setExpirationDate($itemType, $itemSource, $date);
+ public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) {
+ return \OC\Share\Share::setExpirationDate($itemType, $itemSource, $date, $shareTime);
}
/**
diff --git a/lib/public/util.php b/lib/public/util.php
index ff8c743c0df..87b7a4f19db 100644
--- a/lib/public/util.php
+++ b/lib/public/util.php
@@ -519,6 +519,15 @@ class Util {
}
/**
+ * check if share API enforces a default expire date
+ * @return boolean
+ */
+ public static function isDefaultExpireDateEnforced() {
+ return \OC_Util::isDefaultExpireDateEnforced();
+ }
+
+
+ /**
* Checks whether the current version needs upgrade.
*
* @return bool true if upgrade is needed, false otherwise