summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJörn Friedrich Dreyer <jfd@butonic.de>2012-11-12 14:44:00 +0100
committerJörn Friedrich Dreyer <jfd@butonic.de>2012-11-20 15:03:12 +0100
commitd8a171df26a33710ad162b6acc9f46d00976b986 (patch)
tree958a8024bd6bf2d6ef9b1f6f80b01ec99763df87 /lib
parent568def2b61e45d51d4506ad08723c3e2173481b4 (diff)
downloadnextcloud-server-d8a171df26a33710ad162b6acc9f46d00976b986.tar.gz
nextcloud-server-d8a171df26a33710ad162b6acc9f46d00976b986.zip
implement share via link token
Diffstat (limited to 'lib')
-rw-r--r--lib/helper.php1
-rw-r--r--lib/public/share.php66
-rwxr-xr-xlib/util.php2
3 files changed, 48 insertions, 21 deletions
diff --git a/lib/helper.php b/lib/helper.php
index 27ebd24f6e5..5dec7fadfb4 100644
--- a/lib/helper.php
+++ b/lib/helper.php
@@ -758,5 +758,4 @@ class OC_Helper {
}
return $str;
}
-
}
diff --git a/lib/public/share.php b/lib/public/share.php
index dcb1b5c278e..2ded31a42e4 100644
--- a/lib/public/share.php
+++ b/lib/public/share.php
@@ -53,6 +53,8 @@ class Share {
const FORMAT_STATUSES = -2;
const FORMAT_SOURCES = -3;
+ const TOKEN_LENGTH = 32; // see db_structure.xml
+
private static $shareTypeUserAndGroups = -1;
private static $shareTypeGroupUserUnique = 2;
private static $backends = array();
@@ -140,6 +142,20 @@ class Share {
}
/**
+ * @brief Get the item shared by a token
+ * @param string token
+ * @return Item
+ */
+ public static function getShareByToken($token) {
+ $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `token` = ?',1);
+ $result = $query->execute(array($token));
+ if (\OC_DB::isError($result)) {
+ \OC_Log::write('OCP\Share', \OC_DB::getErrorMessage($result) . ', token=' . $token, \OC_Log::ERROR);
+ }
+ return $result->fetchRow();
+ }
+
+ /**
* @brief Get the shared items of item type owned by the current user
* @param string Item type
* @param int Format (optional) Format type must be defined by the backend
@@ -168,7 +184,7 @@ class Share {
* @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
* @param string User or group the item is being shared with
* @param int CRUDS permissions
- * @return bool Returns true on success or false on failure
+ * @return bool|string Returns true on success or false on failure, Returns token on success for links
*/
public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions) {
$uidOwner = \OC_User::getUser();
@@ -230,23 +246,33 @@ class Share {
$shareWith['users'] = array_diff(\OC_Group::usersInGroup($group), array($uidOwner));
} else if ($shareType === self::SHARE_TYPE_LINK) {
if (\OC_Appconfig::getValue('core', 'shareapi_allow_links', 'yes') == 'yes') {
+ // when updating a link share
if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, $uidOwner, self::FORMAT_NONE, null, 1)) {
- // If password is set delete the old link
- if (isset($shareWith)) {
- self::delete($checkExists['id']);
- } else {
- $message = 'Sharing '.$itemSource.' failed, because this item is already shared with a link';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
- }
+ // remember old token
+ $oldToken = $checkExists['token'];
+ //delete the old share
+ self::delete($checkExists['id']);
}
+
// Generate hash of password - same method as user passwords
if (isset($shareWith)) {
$forcePortable = (CRYPT_BLOWFISH != 1);
$hasher = new \PasswordHash(8, $forcePortable);
$shareWith = $hasher->HashPassword($shareWith.\OC_Config::getValue('passwordsalt', ''));
}
- return self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions);
+
+ // Generate token
+ if (isset($oldToken)) {
+ $token = $oldToken;
+ } else {
+ $token = \OC_Util::generate_random_bytes(self::TOKEN_LENGTH);
+ }
+ $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token);
+ if ($result) {
+ return $token;
+ } else {
+ return false;
+ }
}
$message = 'Sharing '.$itemSource.' failed, because sharing with links is not allowed';
\OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
@@ -654,9 +680,9 @@ class Share {
} else {
if (isset($uidOwner)) {
if ($itemType == 'file' || $itemType == 'folder') {
- $select = '`*PREFIX*share`.`id`, `item_type`, `*PREFIX*share`.`parent`, `share_type`, `share_with`, `file_source`, `path`, `permissions`, `stime`, `expiration`';
+ $select = '`*PREFIX*share`.`id`, `item_type`, `*PREFIX*share`.`parent`, `share_type`, `share_with`, `file_source`, `path`, `permissions`, `stime`, `expiration`, `token`';
} else {
- $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `permissions`, `stime`, `file_source`, `expiration`';
+ $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `permissions`, `stime`, `file_source`, `expiration`, `token`';
}
} else {
if ($fileDependent) {
@@ -835,7 +861,7 @@ class Share {
* @param bool|array Parent folder target (optional)
* @return bool Returns true on success or false on failure
*/
- private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder = null) {
+ private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder = null, $token = null) {
$backend = self::getBackend($itemType);
// Check if this is a reshare
if ($checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true)) {
@@ -892,7 +918,7 @@ class Share {
$fileSource = null;
}
}
- $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`, `parent`, `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`, `file_target`) VALUES (?,?,?,?,?,?,?,?,?,?,?)');
+ $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`, `parent`, `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`, `file_target`, `token`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)');
// Share with a group
if ($shareType == self::SHARE_TYPE_GROUP) {
$groupItemTarget = self::generateTarget($itemType, $itemSource, $shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget);
@@ -913,7 +939,7 @@ class Share {
} else {
$groupFileTarget = null;
}
- $query->execute(array($itemType, $itemSource, $groupItemTarget, $parent, $shareType, $shareWith['group'], $uidOwner, $permissions, time(), $fileSource, $groupFileTarget));
+ $query->execute(array($itemType, $itemSource, $groupItemTarget, $parent, $shareType, $shareWith['group'], $uidOwner, $permissions, time(), $fileSource, $groupFileTarget, $token));
// Save this id, any extra rows for this group share will need to reference it
$parent = \OC_DB::insertid('*PREFIX*share');
// Loop through all users of this group in case we need to add an extra row
@@ -947,11 +973,12 @@ class Share {
'permissions' => $permissions,
'fileSource' => $fileSource,
'fileTarget' => $fileTarget,
- 'id' => $parent
+ 'id' => $parent,
+ 'token' => $token
));
// Insert an extra row for the group share if the item or file target is unique for this user
if ($itemTarget != $groupItemTarget || (isset($fileSource) && $fileTarget != $groupFileTarget)) {
- $query->execute(array($itemType, $itemSource, $itemTarget, $parent, self::$shareTypeGroupUserUnique, $uid, $uidOwner, $permissions, time(), $fileSource, $fileTarget));
+ $query->execute(array($itemType, $itemSource, $itemTarget, $parent, self::$shareTypeGroupUserUnique, $uid, $uidOwner, $permissions, time(), $fileSource, $fileTarget, $token));
$id = \OC_DB::insertid('*PREFIX*share');
}
}
@@ -976,7 +1003,7 @@ class Share {
} else {
$fileTarget = null;
}
- $query->execute(array($itemType, $itemSource, $itemTarget, $parent, $shareType, $shareWith, $uidOwner, $permissions, time(), $fileSource, $fileTarget));
+ $query->execute(array($itemType, $itemSource, $itemTarget, $parent, $shareType, $shareWith, $uidOwner, $permissions, time(), $fileSource, $fileTarget, $token));
$id = \OC_DB::insertid('*PREFIX*share');
\OC_Hook::emit('OCP\Share', 'post_shared', array(
'itemType' => $itemType,
@@ -989,7 +1016,8 @@ class Share {
'permissions' => $permissions,
'fileSource' => $fileSource,
'fileTarget' => $fileTarget,
- 'id' => $id
+ 'id' => $id,
+ 'token' => $token
));
if ($parentFolder === true) {
$parentFolders['id'] = $id;
diff --git a/lib/util.php b/lib/util.php
index e7ba3dd3dfe..adec69248d5 100755
--- a/lib/util.php
+++ b/lib/util.php
@@ -95,7 +95,7 @@ class OC_Util {
*/
public static function getVersion() {
// hint: We only can count up. So the internal version number of ownCloud 4.5 will be 4.90.0. This is not visible to the user
- return array(4, 91, 01);
+ return array(4, 91, 02);
}
/**