summaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2014-04-25 11:47:06 +0200
committerRobin Appelman <icewind@owncloud.com>2014-04-25 11:47:06 +0200
commit6c20a014eaecd19c3f68143485c6f74891ee9643 (patch)
tree84bd8e37536e7f28a25afd7586c209d38a25d610 /lib/private
parentcd0c5990f895bcdce47acf2dbf11ebadd920a404 (diff)
parent3fc809dfd80a296d7da922a06f9e13d446b3d3f0 (diff)
downloadnextcloud-server-6c20a014eaecd19c3f68143485c6f74891ee9643.tar.gz
nextcloud-server-6c20a014eaecd19c3f68143485c6f74891ee9643.zip
merge master into webdav-injection
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/connector/sabre/directory.php13
-rw-r--r--lib/private/connector/sabre/file.php11
-rw-r--r--lib/private/connector/sabre/objecttree.php10
-rw-r--r--lib/private/contactsmanager.php7
-rw-r--r--lib/private/files/filesystem.php3
-rw-r--r--lib/private/files/mount/manager.php7
-rw-r--r--lib/private/files/mount/mount.php7
-rw-r--r--lib/private/files/view.php37
-rw-r--r--lib/private/mimetypes.list.php1
-rw-r--r--lib/private/share/constants.php8
-rw-r--r--lib/private/share/helper.php49
-rw-r--r--lib/private/share/share.php311
-rw-r--r--lib/private/user/database.php103
13 files changed, 385 insertions, 182 deletions
diff --git a/lib/private/connector/sabre/directory.php b/lib/private/connector/sabre/directory.php
index 545c1f95ac7..1bb526e451e 100644
--- a/lib/private/connector/sabre/directory.php
+++ b/lib/private/connector/sabre/directory.php
@@ -50,10 +50,6 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa
*/
public function createFile($name, $data = null) {
- if (strtolower($name) === 'shared' && empty($this->path)) {
- throw new \Sabre_DAV_Exception_Forbidden();
- }
-
// for chunked upload also updating a existing file is a "createFile"
// because we create all the chunks before re-assemble them to the existing file.
if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
@@ -87,11 +83,6 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa
* @return void
*/
public function createDirectory($name) {
-
- if (strtolower($name) === 'shared' && empty($this->path)) {
- throw new \Sabre_DAV_Exception_Forbidden();
- }
-
if (!$this->fileView->isCreatable($this->path)) {
throw new \Sabre_DAV_Exception_Forbidden();
}
@@ -196,10 +187,6 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa
*/
public function delete() {
- if ($this->path === 'Shared') {
- throw new \Sabre_DAV_Exception_Forbidden();
- }
-
if (!$this->info->isDeletable()) {
throw new \Sabre_DAV_Exception_Forbidden();
}
diff --git a/lib/private/connector/sabre/file.php b/lib/private/connector/sabre/file.php
index 1d5b3fce32f..66b50a87552 100644
--- a/lib/private/connector/sabre/file.php
+++ b/lib/private/connector/sabre/file.php
@@ -73,13 +73,6 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D
// mark file as partial while uploading (ignored by the scanner)
$partpath = $this->path . '.ocTransferId' . rand() . '.part';
- // if file is located in /Shared we write the part file to the users
- // root folder because we can't create new files in /shared
- // we extend the name with a random number to avoid overwriting a existing file
- if (dirname($partpath) === 'Shared') {
- $partpath = pathinfo($partpath, PATHINFO_FILENAME) . rand() . '.part';
- }
-
try {
$putOkay = $this->fileView->file_put_contents($partpath, $data);
if ($putOkay === false) {
@@ -150,10 +143,6 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D
* @throws Sabre_DAV_Exception_Forbidden
*/
public function delete() {
- if ($this->path === 'Shared') {
- throw new \Sabre_DAV_Exception_Forbidden();
- }
-
if (!$this->info->isDeletable()) {
throw new \Sabre_DAV_Exception_Forbidden();
}
diff --git a/lib/private/connector/sabre/objecttree.php b/lib/private/connector/sabre/objecttree.php
index 605684a7793..35cc1679ab6 100644
--- a/lib/private/connector/sabre/objecttree.php
+++ b/lib/private/connector/sabre/objecttree.php
@@ -115,13 +115,19 @@ class ObjectTree extends \Sabre_DAV_ObjectTree {
list($sourceDir,) = \Sabre_DAV_URLUtil::splitPath($sourcePath);
list($destinationDir,) = \Sabre_DAV_URLUtil::splitPath($destinationPath);
+ $isShareMountPoint = false;
+ list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath( '/' . \OCP\User::getUser() . '/files/' . $sourcePath);
+ if ($storage instanceof \OC\Files\Storage\Shared && !$internalPath) {
+ $isShareMountPoint = true;
+ }
+
// check update privileges
- if (!$this->fileView->isUpdatable($sourcePath)) {
+ if (!$this->fileView->isUpdatable($sourcePath) && !$isShareMountPoint) {
throw new \Sabre_DAV_Exception_Forbidden();
}
if ($sourceDir !== $destinationDir) {
// for a full move we need update privileges on sourcePath and sourceDir as well as destinationDir
- if (ltrim($destinationDir, '/') === '' && strtolower($sourceNode->getName()) === 'shared') {
+ if (ltrim($destinationDir, '/') === '') {
throw new \Sabre_DAV_Exception_Forbidden();
}
if (!$this->fileView->isUpdatable($sourceDir)) {
diff --git a/lib/private/contactsmanager.php b/lib/private/contactsmanager.php
index 1cb3da7098f..4299d88017a 100644
--- a/lib/private/contactsmanager.php
+++ b/lib/private/contactsmanager.php
@@ -37,7 +37,12 @@ namespace OC {
$result = array();
foreach($this->address_books as $address_book) {
$r = $address_book->search($pattern, $searchProperties, $options);
- $result = array_merge($result, $r);
+ $contacts = array();
+ foreach($r as $c){
+ $c['addressbook-key'] = $address_book->getKey();
+ $contacts[] = $c;
+ }
+ $result = array_merge($result, $contacts);
}
return $result;
diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php
index 7e27650c557..434ee495870 100644
--- a/lib/private/files/filesystem.php
+++ b/lib/private/files/filesystem.php
@@ -374,6 +374,9 @@ class Filesystem {
* Returns path like /admin/files
*/
static public function getRoot() {
+ if (!self::$defaultInstance) {
+ return null;
+ }
return self::$defaultInstance->getRoot();
}
diff --git a/lib/private/files/mount/manager.php b/lib/private/files/mount/manager.php
index ff4a336f347..91460b72730 100644
--- a/lib/private/files/mount/manager.php
+++ b/lib/private/files/mount/manager.php
@@ -24,6 +24,13 @@ class Manager {
}
/**
+ * @param string $mountPoint
+ */
+ public function removeMount($mountPoint) {
+ unset($this->mounts[$mountPoint]);
+ }
+
+ /**
* Find the mount for $path
*
* @param $path
diff --git a/lib/private/files/mount/mount.php b/lib/private/files/mount/mount.php
index 0ce2f5975c7..08d5ddf348b 100644
--- a/lib/private/files/mount/mount.php
+++ b/lib/private/files/mount/mount.php
@@ -66,6 +66,13 @@ class Mount {
}
/**
+ * @param string $mountPoint new mount point
+ */
+ public function setMountPoint($mountPoint) {
+ $this->mountPoint = $mountPoint;
+ }
+
+ /**
* create the storage that is mounted
*
* @return \OC\Files\Storage\Storage
diff --git a/lib/private/files/view.php b/lib/private/files/view.php
index 519ed250b1f..58dfc73dcf3 100644
--- a/lib/private/files/view.php
+++ b/lib/private/files/view.php
@@ -348,7 +348,8 @@ class View {
$postFix = (substr($path, -1, 1) === '/') ? '/' : '';
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
- if (!$internalPath || $internalPath === '' || $internalPath === '/') {
+ if (!($storage instanceof \OC\Files\Storage\Shared) &&
+ (!$internalPath || $internalPath === '' || $internalPath === '/')) {
// do not allow deleting the storage's root / the mount point
// because for some storages it might delete the whole contents
// but isn't supposed to work that way
@@ -404,11 +405,21 @@ class View {
if ($run) {
$mp1 = $this->getMountPoint($path1 . $postFix1);
$mp2 = $this->getMountPoint($path2 . $postFix2);
- if ($mp1 == $mp2) {
- list($storage, $internalPath1) = Filesystem::resolvePath($absolutePath1 . $postFix1);
- list(, $internalPath2) = Filesystem::resolvePath($absolutePath2 . $postFix2);
- if ($storage) {
- $result = $storage->rename($internalPath1, $internalPath2);
+ list($storage1, $internalPath1) = Filesystem::resolvePath($absolutePath1 . $postFix1);
+ list(, $internalPath2) = Filesystem::resolvePath($absolutePath2 . $postFix2);
+ // if source and target are on the same storage we can call the rename operation from the
+ // storage. If it is a "Shared" file/folder we call always the rename operation of the
+ // shared storage to handle mount point renaming, etc correctly
+ if ($storage1 instanceof \OC\Files\Storage\Shared) {
+ if ($storage1) {
+ $result = $storage1->rename($absolutePath1, $absolutePath2);
+ \OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2);
+ } else {
+ $result = false;
+ }
+ } elseif ($mp1 == $mp2) {
+ if ($storage1) {
+ $result = $storage1->rename($internalPath1, $internalPath2);
\OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2);
} else {
$result = false;
@@ -417,7 +428,6 @@ class View {
if ($this->is_dir($path1)) {
$result = $this->copy($path1, $path2);
if ($result === true) {
- list($storage1, $internalPath1) = Filesystem::resolvePath($absolutePath1 . $postFix1);
$result = $storage1->unlink($internalPath1);
}
} else {
@@ -431,7 +441,6 @@ class View {
fclose($target);
if ($result !== false) {
- list($storage1, $internalPath1) = Filesystem::resolvePath($absolutePath1 . $postFix1);
$storage1->unlink($internalPath1);
}
}
@@ -972,8 +981,13 @@ class View {
$permissions = $subStorage->getPermissions($rootEntry['path']);
$subPermissionsCache->set($rootEntry['fileid'], $user, $permissions);
}
- // do not allow renaming/deleting the mount point
- $rootEntry['permissions'] = $permissions & (\OCP\PERMISSION_ALL - (\OCP\PERMISSION_UPDATE | \OCP\PERMISSION_DELETE));
+ // do not allow renaming/deleting the mount point if they are not shared files/folders
+ // for shared files/folders we use the permissions given by the owner
+ if ($subStorage instanceof \OC\Files\Storage\Shared) {
+ $rootEntry['permissions'] = $permissions;
+ } else {
+ $rootEntry['permissions'] = $permissions & (\OCP\PERMISSION_ALL - (\OCP\PERMISSION_UPDATE | \OCP\PERMISSION_DELETE));
+ }
//remove any existing entry with the same name
foreach ($files as $i => $file) {
@@ -1154,7 +1168,8 @@ class View {
* @var \OC\Files\Mount\Mount $mount
*/
$cache = $mount->getStorage()->getCache();
- if ($internalPath = $cache->getPathById($id)) {
+ $internalPath = $cache->getPathById($id);
+ if (is_string($internalPath)) {
$fullPath = $mount->getMountPoint() . $internalPath;
if (!is_null($path = $this->getRelativePath($fullPath))) {
return $path;
diff --git a/lib/private/mimetypes.list.php b/lib/private/mimetypes.list.php
index 07e2391c116..3fe454f41f1 100644
--- a/lib/private/mimetypes.list.php
+++ b/lib/private/mimetypes.list.php
@@ -60,6 +60,7 @@ return array(
'epub' => array('application/epub+zip', null),
'exe' => array('application/x-ms-dos-executable', null),
'flac' => array('audio/flac', null),
+ 'flv' => array('video/x-flv', null),
'gif' => array('image/gif', null),
'gz' => array('application/x-gzip', null),
'gzip' => array('application/x-gzip', null),
diff --git a/lib/private/share/constants.php b/lib/private/share/constants.php
index 7e4223d10fa..4c398c43c2d 100644
--- a/lib/private/share/constants.php
+++ b/lib/private/share/constants.php
@@ -26,13 +26,13 @@ class Constants {
const SHARE_TYPE_USER = 0;
const SHARE_TYPE_GROUP = 1;
const SHARE_TYPE_LINK = 3;
- const SHARE_TYPE_EMAIL = 4;
- const SHARE_TYPE_CONTACT = 5;
- const SHARE_TYPE_REMOTE = 6;
+ const SHARE_TYPE_EMAIL = 4; // ToDo Check if it is still in use otherwise remove it
+ const SHARE_TYPE_CONTACT = 5; // ToDo Check if it is still in use otherwise remove it
+ const SHARE_TYPE_REMOTE = 6; // ToDo Check if it is still in use otherwise remove it
const FORMAT_NONE = -1;
const FORMAT_STATUSES = -2;
- const FORMAT_SOURCES = -3;
+ const FORMAT_SOURCES = -3; // ToDo Check if it is still in use otherwise remove it
const TOKEN_LENGTH = 32; // see db_structure.xml
diff --git a/lib/private/share/helper.php b/lib/private/share/helper.php
index fde55667281..515ec85909a 100644
--- a/lib/private/share/helper.php
+++ b/lib/private/share/helper.php
@@ -199,4 +199,53 @@ class Helper extends \OC\Share\Constants {
$query->execute();
}
}
+
+ /**
+ * @brief get default expire settings defined by the admin
+ * @return array contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
+ */
+ public static function getDefaultExpireSetting() {
+
+ $defaultExpireSettings = array('defaultExpireDateSet' => false);
+
+ // get default expire settings
+ $defaultExpireDate = \OC_Appconfig::getValue('core', 'shareapi_default_expire_date', 'no');
+ if ($defaultExpireDate === 'yes') {
+ $enforceExpireDate = \OC_Appconfig::getValue('core', 'shareapi_enforce_expire_date', 'no');
+ $defaultExpireSettings['defaultExpireDateSet'] = true;
+ $defaultExpireSettings['expireAfterDays'] = (int)\OC_Appconfig::getValue('core', 'shareapi_expire_after_n_days', '7');
+ $defaultExpireSettings['enforceExpireDate'] = $enforceExpireDate === 'yes' ? true : false;
+ }
+
+ return $defaultExpireSettings;
+ }
+
+ /**
+ * @brief calculate expire date
+ * @param array $defaultExpireSettings contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
+ * @param int $creationTime timestamp when the share was created
+ * @param int $userExpireDate expire timestamp set by the user
+ * @return mixed integer timestamp or False
+ */
+ public static function calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate = null) {
+
+ $expires = false;
+
+ if (isset($defaultExpireSettings['defaultExpireDateSet']) && $defaultExpireSettings['defaultExpireDateSet']) {
+ $expires = $creationTime + $defaultExpireSettings['expireAfterDays'] * 86400;
+ }
+
+
+ if (isset($userExpireDate)) {
+ // if the admin decided to enforce the default expire date then we only take
+ // the user defined expire date of it is before the default expire date
+ if ($expires && isset($defaultExpireSettings['enforceExpireDate']) && $defaultExpireSettings['enforceExpireDate']) {
+ $expires = ($userExpireDate < $expires) ? $userExpireDate : $expires;
+ } else {
+ $expires = $userExpireDate;
+ }
+ }
+
+ return $expires;
+ }
}
diff --git a/lib/private/share/share.php b/lib/private/share/share.php
index d4f08e8e016..c0ce3a1d8af 100644
--- a/lib/private/share/share.php
+++ b/lib/private/share/share.php
@@ -91,23 +91,25 @@ class Share extends \OC\Share\Constants {
/**
* Find which users can access a shared item
- * @param $path to the file
- * @param $user owner of the file
- * @param include owner to the list of users with access to the file
+ * @param string $path to the file
+ * @param string $ownerUser owner of the file
+ * @param bool $includeOwner include owner to the list of users with access to the file
+ * @param bool $returnUserPaths Return an array with the user => path map
* @return array
* @note $path needs to be relative to user data dir, e.g. 'file.txt'
* not '/admin/data/file.txt'
*/
- public static function getUsersSharingFile($path, $user, $includeOwner = false) {
+ public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false) {
- $shares = array();
+ $shares = $sharePaths = $fileTargets = array();
$publicShare = false;
$source = -1;
$cache = false;
- $view = new \OC\Files\View('/' . $user . '/files');
+ $view = new \OC\Files\View('/' . $ownerUser . '/files');
if ($view->file_exists($path)) {
$meta = $view->getFileInfo($path);
+ $path = substr($meta->getPath(), strlen('/' . $ownerUser . '/files'));
} else {
// if the file doesn't exists yet we start with the parent folder
$meta = $view->getFileInfo(dirname($path));
@@ -119,10 +121,9 @@ class Share extends \OC\Share\Constants {
}
while ($source !== -1) {
-
// Fetch all shares with another user
$query = \OC_DB::prepare(
- 'SELECT `share_with`
+ 'SELECT `share_with`, `file_source`, `file_target`
FROM
`*PREFIX*share`
WHERE
@@ -136,12 +137,15 @@ class Share extends \OC\Share\Constants {
} else {
while ($row = $result->fetchRow()) {
$shares[] = $row['share_with'];
+ if ($returnUserPaths) {
+ $fileTargets[(int) $row['file_source']][$row['share_with']] = $row;
+ }
}
}
- // We also need to take group shares into account
+ // We also need to take group shares into account
$query = \OC_DB::prepare(
- 'SELECT `share_with`
+ 'SELECT `share_with`, `file_source`, `file_target`
FROM
`*PREFIX*share`
WHERE
@@ -156,6 +160,11 @@ class Share extends \OC\Share\Constants {
while ($row = $result->fetchRow()) {
$usersInGroup = \OC_Group::usersInGroup($row['share_with']);
$shares = array_merge($shares, $usersInGroup);
+ if ($returnUserPaths) {
+ foreach ($usersInGroup as $user) {
+ $fileTargets[(int) $row['file_source']][$user] = $row;
+ }
+ }
}
}
@@ -188,9 +197,41 @@ class Share extends \OC\Share\Constants {
$source = -1;
}
}
+
// Include owner in list of users, if requested
if ($includeOwner) {
- $shares[] = $user;
+ $shares[] = $ownerUser;
+ if ($returnUserPaths) {
+ $sharePaths[$ownerUser] = $path;
+ }
+ }
+
+ if ($returnUserPaths) {
+ $fileTargetIDs = array_keys($fileTargets);
+ $fileTargetIDs = array_unique($fileTargetIDs);
+
+ if (!empty($fileTargetIDs)) {
+ $query = \OC_DB::prepare(
+ 'SELECT `fileid`, `path`
+ FROM `*PREFIX*filecache`
+ WHERE `fileid` IN (' . implode(',', $fileTargetIDs) . ')'
+ );
+ $result = $query->execute();
+
+ if (\OCP\DB::isError($result)) {
+ \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
+ } else {
+ while ($row = $result->fetchRow()) {
+ foreach ($fileTargets[$row['fileid']] as $uid => $shareData) {
+ $sharedPath = $shareData['file_target'];
+ $sharedPath .= substr($path, strlen($row['path']) -5);
+ $sharePaths[$uid] = $sharedPath;
+ }
+ }
+ }
+ }
+
+ return $sharePaths;
}
return array("users" => array_unique($shares), "public" => $publicShare);
@@ -212,6 +253,22 @@ class Share extends \OC\Share\Constants {
}
/**
+ * Get the items of item type shared with a user
+ * @param string Item type
+ * @param sting user id for which user we want the shares
+ * @param int Format (optional) Format type must be defined by the backend
+ * @param mixed Parameters (optional)
+ * @param int Number of items to return (optional) Returns all by default
+ * @param bool include collections (optional)
+ * @return Return depends on format
+ */
+ public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE,
+ $parameters = null, $limit = -1, $includeCollections = false) {
+ return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format,
+ $parameters, $limit, $includeCollections);
+ }
+
+ /**
* Get the item of item type shared with the current user
* @param string $itemType
* @param string $itemTarget
@@ -426,40 +483,61 @@ class Share extends \OC\Share\Constants {
public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null) {
$uidOwner = \OC_User::getUser();
$sharingPolicy = \OC_Appconfig::getValue('core', 'shareapi_share_policy', 'global');
+ $l = \OC_L10N::get('lib');
if (is_null($itemSourceName)) {
$itemSourceName = $itemSource;
}
+
// verify that the file exists before we try to share it
if ($itemType === 'file' or $itemType === 'folder') {
$path = \OC\Files\Filesystem::getPath($itemSource);
if (!$path) {
- $message = 'Sharing ' . $itemSourceName . ' failed, because the file does not exist';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because the file does not exist';
+ $message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName), \OC_Log::ERROR);
+ throw new \Exception($message_t);
+ }
+ }
+
+ //verify that we don't share a folder which already contains a share mount point
+ if ($itemType === 'folder') {
+ $path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/';
+ $mountManager = \OC\Files\Filesystem::getMountManager();
+ $mounts = $mountManager->getAll();
+ foreach ($mounts as $mountPoint => $mount) {
+ if ($mount->getStorage() instanceof \OC\Files\Storage\Shared && strpos($mountPoint, $path) === 0) {
+ $message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!';
+ \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
+ throw new \Exception($message);
+ }
+
}
}
// Verify share type and sharing conditions are met
if ($shareType === self::SHARE_TYPE_USER) {
if ($shareWith == $uidOwner) {
- $message = 'Sharing '.$itemSourceName.' failed, because the user '.$shareWith.' is the item owner';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because the user %s is the item owner';
+ $message_t = $l->t('Sharing %s failed, because the user %s is the item owner', array($itemSourceName, $shareWith));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
if (!\OC_User::userExists($shareWith)) {
- $message = 'Sharing '.$itemSourceName.' failed, because the user '.$shareWith.' does not exist';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because the user %s does not exist';
+ $message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
if ($sharingPolicy == 'groups_only') {
$inGroup = array_intersect(\OC_Group::getUserGroups($uidOwner), \OC_Group::getUserGroups($shareWith));
if (empty($inGroup)) {
- $message = 'Sharing '.$itemSourceName.' failed, because the user '
- .$shareWith.' is not a member of any groups that '.$uidOwner.' is a member of';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because the user '
+ .'%s is not a member of any groups that %s is a member of';
+ $message_t = $l->t('Sharing %s failed, because the user %s is not a member of any groups that %s is a member of', array($itemSourceName, $shareWith, $uidOwner));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName, $shareWith, $uidOwner), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
}
// Check if the item source is already shared with the user, either from the same owner or a different user
@@ -469,22 +547,25 @@ class Share extends \OC\Share\Constants {
// owner and is not a user share, this use case is for increasing
// permissions for a specific user
if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) {
- $message = 'Sharing '.$itemSourceName.' failed, because this item is already shared with '.$shareWith;
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because this item is already shared with %s';
+ $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
}
} else if ($shareType === self::SHARE_TYPE_GROUP) {
if (!\OC_Group::groupExists($shareWith)) {
- $message = 'Sharing '.$itemSourceName.' failed, because the group '.$shareWith.' does not exist';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because the group %s does not exist';
+ $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
if ($sharingPolicy == 'groups_only' && !\OC_Group::inGroup($uidOwner, $shareWith)) {
- $message = 'Sharing '.$itemSourceName.' failed, because '
- .$uidOwner.' is not a member of the group '.$shareWith;
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because '
+ .'%s is not a member of the group %s';
+ $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
// Check if the item source is already shared with the group, either from the same owner or a different user
// The check for each user in the group is done inside the put() function
@@ -494,9 +575,10 @@ class Share extends \OC\Share\Constants {
// owner and is not a group share, this use case is for increasing
// permissions for a specific user
if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) {
- $message = 'Sharing '.$itemSourceName.' failed, because this item is already shared with '.$shareWith;
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because this item is already shared with %s';
+ $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
}
// Convert share with into an array with the keys group and users
@@ -543,15 +625,17 @@ class Share extends \OC\Share\Constants {
return false;
}
}
- $message = 'Sharing '.$itemSourceName.' failed, because sharing with links is not allowed';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because sharing with links is not allowed';
+ $message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName), \OC_Log::ERROR);
+ throw new \Exception($message_t);
return false;
} else {
// Future share types need to include their own conditions
- $message = 'Share type '.$shareType.' is not valid for '.$itemSource;
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Share type %s is not valid for %s';
+ $message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource));
+ \OC_Log::write('OCP\Share', sprintf($message, $shareType, $itemSource), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
// Put the item into the database
return self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName);
@@ -675,6 +759,7 @@ class Share extends \OC\Share\Constants {
* @return Returns true on success or false on failure
*/
public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) {
+ $l = \OC_L10N::get('lib');
if ($item = self::getItems($itemType, $itemSource, $shareType, $shareWith,
\OC_User::getUser(), self::FORMAT_NONE, null, 1, false)) {
// Check if this item is a reshare and verify that the permissions
@@ -683,10 +768,11 @@ class Share extends \OC\Share\Constants {
$query = \OC_DB::prepare('SELECT `permissions` FROM `*PREFIX*share` WHERE `id` = ?', 1);
$result = $query->execute(array($item['parent']))->fetchRow();
if (~(int)$result['permissions'] & $permissions) {
- $message = 'Setting permissions for '.$itemSource.' failed,'
- .' because the permissions exceed permissions granted to '.\OC_User::getUser();
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Setting permissions for %s failed,'
+ .' because the permissions exceed permissions granted to %s';
+ $message_t = $l->t('Setting permissions for %s failed, because the permissions exceed permissions granted to %s', array($itemSource, \OC_User::getUser()));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSource, \OC_User::getUser()), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
}
$query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = ? WHERE `id` = ?');
@@ -744,9 +830,11 @@ class Share extends \OC\Share\Constants {
}
return true;
}
- $message = 'Setting permissions for '.$itemSource.' failed, because the item was not found';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Setting permissions for %s failed, because the item was not found';
+ $message_t = $l->t('Setting permissions for %s failed, because the item was not found', array($itemSource));
+
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSource), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
/**
@@ -788,9 +876,20 @@ class Share extends \OC\Share\Constants {
* @return bool True if item was expired, false otherwise.
*/
protected static function expireItem(array $item) {
+
+ // get default expire settings
+ $defaultSettings = Helper::getDefaultExpireSetting();
+ // calculate expire date
if (!empty($item['expiration'])) {
- $now = new \DateTime();
- $expires = new \DateTime($item['expiration']);
+ $userDefinedExpire = new \DateTime($item['expiration']);
+ $userDefinedExpireTimestamp = $userDefinedExpire->getTimestamp();
+ } else {
+ $userDefinedExpireTimestamp = null;
+ }
+ $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $userDefinedExpireTimestamp);
+
+ if (is_int($expires)) {
+ $now = time();
if ($now > $expires) {
self::unshareItem($item);
return true;
@@ -809,6 +908,7 @@ class Share extends \OC\Share\Constants {
$hookParams = array(
'itemType' => $item['item_type'],
'itemSource' => $item['item_source'],
+ 'fileSource' => $item['file_source'],
'shareType' => $item['share_type'],
'shareWith' => $item['share_with'],
'itemParent' => $item['parent'],
@@ -828,6 +928,7 @@ class Share extends \OC\Share\Constants {
* @return \OCP\Share_Backend
*/
public static function getBackend($itemType) {
+ $l = \OC_L10N::get('lib');
if (isset(self::$backends[$itemType])) {
return self::$backends[$itemType];
} else if (isset(self::$backendTypes[$itemType]['class'])) {
@@ -835,20 +936,23 @@ class Share extends \OC\Share\Constants {
if (class_exists($class)) {
self::$backends[$itemType] = new $class;
if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) {
- $message = 'Sharing backend '.$class.' must implement the interface OCP\Share_Backend';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing backend %s must implement the interface OCP\Share_Backend';
+ $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class));
+ \OC_Log::write('OCP\Share', sprintf($message, $class), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
return self::$backends[$itemType];
} else {
- $message = 'Sharing backend '.$class.' not found';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing backend %s not found';
+ $message_t = $l->t('Sharing backend %s not found', array($class));
+ \OC_Log::write('OCP\Share', sprintf($message, $class), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
}
- $message = 'Sharing backend for '.$itemType.' not found';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing backend for %s not found';
+ $message_t = $l->t('Sharing backend for %s not found', array($itemType));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemType), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
/**
@@ -1064,6 +1168,7 @@ class Share extends \OC\Share\Constants {
// Filter out duplicate group shares for users with unique targets
if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) {
$row['share_type'] = self::SHARE_TYPE_GROUP;
+ $row['unique_name'] = true; // remember that we use a unique name for this user
$row['share_with'] = $items[$row['parent']]['share_with'];
// Remove the parent group share
unset($items[$row['parent']]);
@@ -1102,10 +1207,6 @@ class Share extends \OC\Share\Constants {
// Remove root from file source paths if retrieving own shared items
if (isset($uidOwner) && isset($row['path'])) {
if (isset($row['parent'])) {
- // FIXME: Doesn't always construct the correct path, example:
- // Folder '/a/b', share '/a' and '/a/b' to user2
- // user2 reshares /Shared/b and ask for share status of /Shared/a/b
- // expected result: path=/Shared/a/b; actual result /Shared/b because of the parent
$query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?');
$parentResult = $query->execute(array($row['parent']));
if (\OC_DB::isError($result)) {
@@ -1114,7 +1215,7 @@ class Share extends \OC\Share\Constants {
\OC_Log::ERROR);
} else {
$parentRow = $parentResult->fetchRow();
- $tmpPath = '/Shared' . $parentRow['file_target'];
+ $tmpPath = $parentRow['file_target'];
// find the right position where the row path continues from the target path
$pos = strrpos($row['path'], $parentRow['file_target']);
$subPath = substr($row['path'], $pos);
@@ -1256,23 +1357,26 @@ class Share extends \OC\Share\Constants {
private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner,
$permissions, $parentFolder = null, $token = null, $itemSourceName = null) {
$backend = self::getBackend($itemType);
-
+ $l = \OC_L10N::get('lib');
// Check if this is a reshare
if ($checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true)) {
// Check if attempting to share back to owner
if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) {
- $message = 'Sharing '.$itemSourceName.' failed, because the user '.$shareWith.' is the original sharer';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because the user %s is the original sharer';
+ $message_t = $l->t('Sharing %s failed, because the user %s is the original sharer', array($itemSourceName, $shareWith));
+
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
// Check if share permissions is granted
if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\PERMISSION_SHARE) {
if (~(int)$checkReshare['permissions'] & $permissions) {
- $message = 'Sharing '.$itemSourceName
- .' failed, because the permissions exceed permissions granted to '.$uidOwner;
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because the permissions exceed permissions granted to %s';
+ $message_t = $l->t('Sharing %s failed, because the permissions exceed permissions granted to %s', array($itemSourceName, $uidOwner));
+
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName, $uidOwner), \OC_Log::ERROR);
+ throw new \Exception($message_t);
} else {
// TODO Don't check if inside folder
$parent = $checkReshare['id'];
@@ -1283,19 +1387,22 @@ class Share extends \OC\Share\Constants {
$filePath = $checkReshare['file_target'];
}
} else {
- $message = 'Sharing '.$itemSourceName.' failed, because resharing is not allowed';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because resharing is not allowed';
+ $message_t = $l->t('Sharing %s failed, because resharing is not allowed', array($itemSourceName));
+
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSourceName), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
} else {
$parent = null;
$suggestedItemTarget = null;
$suggestedFileTarget = null;
if (!$backend->isValidSource($itemSource, $uidOwner)) {
- $message = 'Sharing '.$itemSource.' failed, because the sharing backend for '
- .$itemType.' could not find its source';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because the sharing backend for '
+ .'%s could not find its source';
+ $message_t = $l->t('Sharing %s failed, because the sharing backend for %s could not find its source', array($itemSource, $itemType));
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSource, $itemType), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
if ($backend instanceof \OCP\Share_Backend_File_Dependent) {
$filePath = $backend->getFilePath($itemSource, $uidOwner);
@@ -1306,9 +1413,11 @@ class Share extends \OC\Share\Constants {
$fileSource = $meta['fileid'];
}
if ($fileSource == -1) {
- $message = 'Sharing '.$itemSource.' failed, because the file could not be found in the file cache';
- \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR);
- throw new \Exception($message);
+ $message = 'Sharing %s failed, because the file could not be found in the file cache';
+ $message_t = $l->t('Sharing %s failed, because the file could not be found in the file cache', array($itemSource));
+
+ \OC_Log::write('OCP\Share', sprintf($message, $itemSource), \OC_Log::ERROR);
+ throw new \Exception($message_t);
}
} else {
$filePath = null;
@@ -1316,8 +1425,8 @@ class Share extends \OC\Share\Constants {
}
}
$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_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
+ .' `file_target`, `token`, `parent`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)');
// Share with a group
if ($shareType == self::SHARE_TYPE_GROUP) {
$groupItemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith['group'],
@@ -1361,10 +1470,9 @@ class Share extends \OC\Share\Constants {
} else {
$groupFileTarget = null;
}
- $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');
+ $queriesToExecute = array();
+ $queriesToExecute['groupShare'] = array($itemType, $itemSource, $groupItemTarget, $shareType,
+ $shareWith['group'], $uidOwner, $permissions, time(), $fileSource, $groupFileTarget, $token, $parent);
// Loop through all users of this group in case we need to add an extra row
foreach ($shareWith['users'] as $uid) {
$itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $uid,
@@ -1390,12 +1498,21 @@ class Share extends \OC\Share\Constants {
}
// 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,
+ $queriesToExecute[] = array($itemType, $itemSource, $itemTarget,
self::$shareTypeGroupUserUnique, $uid, $uidOwner, $permissions, time(),
- $fileSource, $fileTarget, $token));
+ $fileSource, $fileTarget, $token);
$id = \OC_DB::insertid('*PREFIX*share');
}
}
+ $query->execute($queriesToExecute['groupShare']);
+ // Save this id, any extra rows for this group share will need to reference it
+ $parent = \OC_DB::insertid('*PREFIX*share');
+ unset($queriesToExecute['groupShare']);
+ foreach ($queriesToExecute as $qe) {
+ $qe[] = $parent;
+ $query->execute($qe);
+ }
+
\OC_Hook::emit('OCP\Share', 'post_shared', array(
'itemType' => $itemType,
'itemSource' => $itemSource,
@@ -1455,8 +1572,8 @@ class Share extends \OC\Share\Constants {
} else {
$fileTarget = null;
}
- $query->execute(array($itemType, $itemSource, $itemTarget, $parent, $shareType, $shareWith, $uidOwner,
- $permissions, time(), $fileSource, $fileTarget, $token));
+ $query->execute(array($itemType, $itemSource, $itemTarget, $shareType, $shareWith, $uidOwner,
+ $permissions, time(), $fileSource, $fileTarget, $token, $parent));
$id = \OC_DB::insertid('*PREFIX*share');
\OC_Hook::emit('OCP\Share', 'post_shared', array(
'itemType' => $itemType,
@@ -1533,9 +1650,9 @@ class Share extends \OC\Share\Constants {
$select = '*';
if ($format == self::FORMAT_STATUSES) {
if ($fileDependent) {
- $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, `share_with`, `uid_owner` , `file_source`';
+ $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, `share_with`, `uid_owner` , `file_source`, `stime`';
} else {
- $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`';
+ $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`';
}
} else {
if (isset($uidOwner)) {
@@ -1551,7 +1668,7 @@ class Share extends \OC\Share\Constants {
if ($fileDependent) {
if ($format == \OC_Share_Backend_File::FORMAT_GET_FOLDER_CONTENTS || $format == \OC_Share_Backend_File::FORMAT_FILE_APP_ROOT) {
$select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, '
- . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, '
+ . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, '
. '`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, '
. '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `unencrypted_size`, `encrypted`, `etag`, `mail_send`';
} else {
diff --git a/lib/private/user/database.php b/lib/private/user/database.php
index 994a47011e6..681f03981f5 100644
--- a/lib/private/user/database.php
+++ b/lib/private/user/database.php
@@ -42,7 +42,9 @@ class OC_User_Database extends OC_User_Backend {
/**
* @var PasswordHash
*/
- static private $hasher = null;
+ private static $hasher = null;
+
+ private $cache = array();
private function getHasher() {
if (!self::$hasher) {
@@ -51,7 +53,6 @@ class OC_User_Database extends OC_User_Backend {
self::$hasher = new PasswordHash(8, $forcePortable);
}
return self::$hasher;
-
}
/**
@@ -64,9 +65,7 @@ class OC_User_Database extends OC_User_Backend {
* itself, not in its subclasses.
*/
public function createUser($uid, $password) {
- if ($this->userExists($uid)) {
- return false;
- } else {
+ if (!$this->userExists($uid)) {
$hasher = $this->getHasher();
$hash = $hasher->HashPassword($password . OC_Config::getValue('passwordsalt', ''));
$query = OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )');
@@ -74,6 +73,8 @@ class OC_User_Database extends OC_User_Backend {
return $result ? true : false;
}
+
+ return false;
}
/**
@@ -86,8 +87,13 @@ class OC_User_Database extends OC_User_Backend {
public function deleteUser($uid) {
// Delete user-group-relation
$query = OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?');
- $query->execute(array($uid));
- return true;
+ $result = $query->execute(array($uid));
+
+ if (isset($this->cache[$uid])) {
+ unset($this->cache[$uid]);
+ }
+
+ return $result ? true : false;
}
/**
@@ -103,12 +109,12 @@ class OC_User_Database extends OC_User_Backend {
$hasher = $this->getHasher();
$hash = $hasher->HashPassword($password . OC_Config::getValue('passwordsalt', ''));
$query = OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?');
- $query->execute(array($hash, $uid));
+ $result = $query->execute(array($hash, $uid));
- return true;
- } else {
- return false;
+ return $result ? true : false;
}
+
+ return false;
}
/**
@@ -123,10 +129,12 @@ class OC_User_Database extends OC_User_Backend {
if ($this->userExists($uid)) {
$query = OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)');
$query->execute(array($displayName, $uid));
+ $this->cache[$uid]['displayname'] = $displayName;
+
return true;
- } else {
- return false;
}
+
+ return false;
}
/**
@@ -135,14 +143,8 @@ class OC_User_Database extends OC_User_Backend {
* @return string display name
*/
public function getDisplayName($uid) {
- $query = OC_DB::prepare('SELECT `displayname` FROM `*PREFIX*users` WHERE `uid` = ?');
- $result = $query->execute(array($uid))->fetchAll();
- $displayName = trim($result[0]['displayname'], ' ');
- if (!empty($displayName)) {
- return $displayName;
- } else {
- return $uid;
- }
+ $this->loadUser($uid);
+ return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname'];
}
/**
@@ -185,21 +187,41 @@ class OC_User_Database extends OC_User_Backend {
$hasher = $this->getHasher();
if ($hasher->CheckPassword($password . OC_Config::getValue('passwordsalt', ''), $storedHash)) {
return $row['uid'];
- } else {
- return false;
- }
- } else { //old sha1 based hashing
- if (sha1($password) == $storedHash) {
- //upgrade to new hashing
- $this->setPassword($row['uid'], $password);
- return $row['uid'];
- } else {
- return false;
}
+
+ //old sha1 based hashing
+ } elseif (sha1($password) == $storedHash) {
+ //upgrade to new hashing
+ $this->setPassword($row['uid'], $password);
+ return $row['uid'];
}
- } else {
- return false;
}
+
+ return false;
+ }
+
+ /**
+ * @brief Load an user in the cache
+ * @param string $uid the username
+ * @returns boolean
+ */
+ private function loadUser($uid) {
+ if (empty($this->cache[$uid])) {
+ $query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
+ $result = $query->execute(array($uid));
+
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ return false;
+ }
+
+ while ($row = $result->fetchRow()) {
+ $this->cache[$uid]['uid'] = $row['uid'];
+ $this->cache[$uid]['displayname'] = $row['displayname'];
+ }
+ }
+
+ return true;
}
/**
@@ -224,13 +246,8 @@ class OC_User_Database extends OC_User_Backend {
* @return boolean
*/
public function userExists($uid) {
- $query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
- $result = $query->execute(array($uid));
- if (OC_DB::isError($result)) {
- OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR);
- return false;
- }
- return $result->fetchOne() > 0;
+ $this->loadUser($uid);
+ return !empty($this->cache[$uid]);
}
/**
@@ -241,9 +258,9 @@ class OC_User_Database extends OC_User_Backend {
public function getHome($uid) {
if ($this->userExists($uid)) {
return OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data") . '/' . $uid;
- } else {
- return false;
}
+
+ return false;
}
/**
@@ -256,7 +273,7 @@ class OC_User_Database extends OC_User_Backend {
/**
* counts the users in the database
*
- * @return false|string | bool
+ * @return int | bool
*/
public function countUsers() {
$query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`');