summaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php11
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php5
-rw-r--r--lib/private/DB/QueryBuilder/QueryBuilder.php62
-rw-r--r--lib/private/Files/Cache/Cache.php432
-rw-r--r--lib/private/Files/Cache/CacheEntry.php12
-rw-r--r--lib/private/Files/Cache/CacheQueryBuilder.php92
-rw-r--r--lib/private/Files/Cache/MoveFromCacheTrait.php5
-rw-r--r--lib/private/Files/FileInfo.php8
-rw-r--r--lib/private/Files/Node/LazyRoot.php14
-rw-r--r--lib/private/Files/Node/Node.php8
-rw-r--r--lib/private/Share20/DefaultShareProvider.php130
-rw-r--r--lib/private/Share20/Manager.php24
-rw-r--r--lib/private/Share20/Share.php17
13 files changed, 581 insertions, 239 deletions
diff --git a/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php b/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php
index 4f380f07d91..db3157bda64 100644
--- a/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php
+++ b/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php
@@ -37,13 +37,8 @@ use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
use OC\AppFramework\Middleware\Security\Exceptions\StrictCookieMissingException;
use OC\AppFramework\Utility\ControllerMethodReflector;
-use OC\Security\CSP\ContentSecurityPolicyManager;
-use OC\Security\CSP\ContentSecurityPolicyNonceManager;
-use OC\Security\CSRF\CsrfTokenManager;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
-use OCP\AppFramework\Http\ContentSecurityPolicy;
-use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Middleware;
@@ -121,6 +116,8 @@ class SecurityMiddleware extends Middleware {
* @param Controller $controller the controller
* @param string $methodName the name of the method
* @throws SecurityException when a security check fails
+ *
+ * @suppress PhanUndeclaredClassConstant
*/
public function beforeController($controller, $methodName) {
@@ -128,6 +125,10 @@ class SecurityMiddleware extends Middleware {
// for normal HTML requests and not for AJAX requests
$this->navigationManager->setActiveEntry($this->appName);
+ if ($controller === \OCA\Talk\Controller\PageController::class && $methodName === 'showCall') {
+ $this->navigationManager->setActiveEntry('spreed');
+ }
+
// security checks
$isPublicPage = $this->reflector->hasAnnotation('PublicPage');
if(!$isPublicPage) {
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
index 3b67661c8b0..46bb536dfd2 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
@@ -72,9 +72,10 @@ class FunctionBuilder implements IFunctionBuilder {
return new QueryFunction($this->helper->quoteColumnName($x) . ' - ' . $this->helper->quoteColumnName($y));
}
- public function count($count, $alias = '') {
+ public function count($count = '', $alias = '') {
$alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
- return new QueryFunction('COUNT(' . $this->helper->quoteColumnName($count) . ')' . $alias);
+ $quotedName = $count === '' ? '*' : $this->helper->quoteColumnName($count);
+ return new QueryFunction('COUNT(' . $quotedName . ')' . $alias);
}
public function max($field) {
diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php
index 25d59fb7d7d..bff38c0c8d6 100644
--- a/lib/private/DB/QueryBuilder/QueryBuilder.php
+++ b/lib/private/DB/QueryBuilder/QueryBuilder.php
@@ -245,7 +245,7 @@ class QueryBuilder implements IQueryBuilder {
* @param mixed $value The parameter value.
* @param string|null|int $type One of the IQueryBuilder::PARAM_* constants.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function setParameter($key, $value, $type = null) {
$this->queryBuilder->setParameter($key, $value, $type);
@@ -270,7 +270,7 @@ class QueryBuilder implements IQueryBuilder {
* @param array $params The query parameters to set.
* @param array $types The query parameters types to set.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function setParameters(array $params, array $types = array()) {
$this->queryBuilder->setParameters($params, $types);
@@ -323,7 +323,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param integer $firstResult The first result to return.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function setFirstResult($firstResult) {
$this->queryBuilder->setFirstResult($firstResult);
@@ -350,7 +350,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param integer $maxResults The maximum number of results to retrieve.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function setMaxResults($maxResults) {
$this->queryBuilder->setMaxResults($maxResults);
@@ -381,7 +381,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$selects The selection expressions.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * '@return $this This QueryBuilder instance.
*/
public function select(...$selects) {
if (count($selects) === 1 && is_array($selects[0])) {
@@ -408,7 +408,7 @@ class QueryBuilder implements IQueryBuilder {
* @param mixed $select The selection expressions.
* @param string $alias The column alias used in the constructed query.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function selectAlias($select, $alias) {
@@ -430,7 +430,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed $select The selection expressions.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function selectDistinct($select) {
@@ -454,7 +454,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$selects The selection expression.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function addSelect(...$selects) {
if (count($selects) === 1 && is_array($selects[0])) {
@@ -482,7 +482,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $delete The table whose rows are subject to the deletion.
* @param string $alias The table alias used in the constructed query.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function delete($delete = null, $alias = null) {
$this->queryBuilder->delete(
@@ -507,7 +507,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $update The table whose rows are subject to the update.
* @param string $alias The table alias used in the constructed query.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function update($update = null, $alias = null) {
$this->queryBuilder->update(
@@ -535,7 +535,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param string $insert The table into which the rows should be inserted.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function insert($insert = null) {
$this->queryBuilder->insert(
@@ -560,7 +560,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $from The table.
* @param string|null $alias The alias of the table.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function from($from, $alias = null) {
$this->queryBuilder->from(
@@ -586,7 +586,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $alias The alias of the join table.
* @param string $condition The condition for the join.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function join($fromAlias, $join, $alias, $condition = null) {
$this->queryBuilder->join(
@@ -614,7 +614,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $alias The alias of the join table.
* @param string $condition The condition for the join.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function innerJoin($fromAlias, $join, $alias, $condition = null) {
$this->queryBuilder->innerJoin(
@@ -642,7 +642,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $alias The alias of the join table.
* @param string $condition The condition for the join.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function leftJoin($fromAlias, $join, $alias, $condition = null) {
$this->queryBuilder->leftJoin(
@@ -670,7 +670,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $alias The alias of the join table.
* @param string $condition The condition for the join.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function rightJoin($fromAlias, $join, $alias, $condition = null) {
$this->queryBuilder->rightJoin(
@@ -696,7 +696,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $key The column to set.
* @param string $value The value, expression, placeholder, etc.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function set($key, $value) {
$this->queryBuilder->set(
@@ -731,7 +731,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$predicates The restriction predicates.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function where(...$predicates) {
call_user_func_array(
@@ -756,7 +756,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$where The query restrictions.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*
* @see where()
*/
@@ -783,7 +783,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$where The WHERE statement.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*
* @see where()
*/
@@ -809,7 +809,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$groupBys The grouping expression.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function groupBy(...$groupBys) {
if (count($groupBys) === 1 && is_array($groupBys[0])) {
@@ -837,7 +837,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$groupBy The grouping expression.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function addGroupBy(...$groupBys) {
if (count($groupBys) === 1 && is_array($groupBys[0])) {
@@ -869,7 +869,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $column The column into which the value should be inserted.
* @param string $value The value that should be inserted into the column.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function setValue($column, $value) {
$this->queryBuilder->setValue(
@@ -897,7 +897,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param array $values The values to specify for the insert query indexed by column names.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function values(array $values) {
$quotedValues = [];
@@ -916,7 +916,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$having The restriction over the groups.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function having(...$having) {
call_user_func_array(
@@ -933,7 +933,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$having The restriction to append.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function andHaving(...$having) {
call_user_func_array(
@@ -950,7 +950,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param mixed ...$having The restriction to add.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function orHaving(...$having) {
call_user_func_array(
@@ -968,7 +968,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $sort The ordering expression.
* @param string $order The ordering direction.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function orderBy($sort, $order = null) {
$this->queryBuilder->orderBy(
@@ -985,7 +985,7 @@ class QueryBuilder implements IQueryBuilder {
* @param string $sort The ordering expression.
* @param string $order The ordering direction.
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function addOrderBy($sort, $order = null) {
$this->queryBuilder->addOrderBy(
@@ -1021,7 +1021,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param array|null $queryPartNames
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function resetQueryParts($queryPartNames = null) {
$this->queryBuilder->resetQueryParts($queryPartNames);
@@ -1034,7 +1034,7 @@ class QueryBuilder implements IQueryBuilder {
*
* @param string $queryPartName
*
- * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
+ * @return $this This QueryBuilder instance.
*/
public function resetQueryPart($queryPartName) {
$this->queryBuilder->resetQueryPart($queryPartName);
diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php
index f6139d8abed..f1d2c64e8fd 100644
--- a/lib/private/Files/Cache/Cache.php
+++ b/lib/private/Files/Cache/Cache.php
@@ -45,6 +45,7 @@ use OCP\Files\Cache\CacheInsertEvent;
use OCP\Files\Cache\CacheUpdateEvent;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
+use OCP\Files\FileInfo;
use \OCP\Files\IMimeTypeLoader;
use OCP\Files\Search\ISearchQuery;
use OCP\Files\Storage\IStorage;
@@ -68,7 +69,7 @@ class Cache implements ICache {
/**
* @var array partial data for the cache
*/
- protected $partial = array();
+ protected $partial = [];
/**
* @var string
@@ -112,6 +113,15 @@ class Cache implements ICache {
$this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader);
}
+ private function getQueryBuilder() {
+ return new CacheQueryBuilder(
+ $this->connection,
+ \OC::$server->getSystemConfig(),
+ \OC::$server->getLogger(),
+ $this
+ );
+ }
+
/**
* Get the numeric storage id for this cache's storage
*
@@ -128,34 +138,24 @@ class Cache implements ICache {
* @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache
*/
public function get($file) {
+ $query = $this->getQueryBuilder();
+ $query->selectFileCache();
+
if (is_string($file) or $file == '') {
// normalize file
$file = $this->normalize($file);
- $where = 'WHERE `storage` = ? AND `path_hash` = ?';
- $params = array($this->getNumericStorageId(), md5($file));
+ $query->whereStorageId()
+ ->wherePath($file);
} else { //file id
- $where = 'WHERE `fileid` = ?';
- $params = array($file);
+ $query->whereFileId($file);
}
- $sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
- `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum`
- FROM `*PREFIX*filecache` ' . $where;
- $result = $this->connection->executeQuery($sql, $params);
- $data = $result->fetch();
- //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO
- //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false
- if ($data === null) {
- $data = false;
- }
+ $data = $query->execute()->fetch();
//merge partial data
- if (!$data and is_string($file)) {
- if (isset($this->partial[$file])) {
- $data = $this->partial[$file];
- }
- return $data;
+ if (!$data and is_string($file) and isset($this->partial[$file])) {
+ return $this->partial[$file];
} else if (!$data) {
return $data;
} else {
@@ -187,6 +187,12 @@ class Cache implements ICache {
$data['storage_mtime'] = $data['mtime'];
}
$data['permissions'] = (int)$data['permissions'];
+ if (isset($data['creation_time'])) {
+ $data['creation_time'] = (int) $data['creation_time'];
+ }
+ if (isset($data['upload_time'])) {
+ $data['upload_time'] = (int) $data['upload_time'];
+ }
return new CacheEntry($data);
}
@@ -209,11 +215,12 @@ class Cache implements ICache {
*/
public function getFolderContentsById($fileId) {
if ($fileId > -1) {
- $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
- `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum`
- FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC';
- $result = $this->connection->executeQuery($sql, [$fileId]);
- $files = $result->fetchAll();
+ $query = $this->getQueryBuilder();
+ $query->selectFileCache()
+ ->whereParent($fileId)
+ ->orderBy('name', 'ASC');
+
+ $files = $query->execute()->fetchAll();
return array_map(function (array $data) {
return self::cacheEntryFromData($data, $this->mimetypeLoader);
}, $files);
@@ -259,7 +266,7 @@ class Cache implements ICache {
unset($this->partial[$file]);
}
- $requiredFields = array('size', 'mtime', 'mimetype');
+ $requiredFields = ['size', 'mtime', 'mimetype'];
foreach ($requiredFields as $field) {
if (!isset($data[$field])) { //data not complete save as partial and return
$this->partial[$file] = $data;
@@ -271,14 +278,8 @@ class Cache implements ICache {
$data['parent'] = $this->getParentId($file);
$data['name'] = basename($file);
- list($queryParts, $params) = $this->buildParts($data);
- $queryParts[] = '`storage`';
- $params[] = $this->getNumericStorageId();
-
- $queryParts = array_map(function ($item) {
- return trim($item, "`");
- }, $queryParts);
- $values = array_combine($queryParts, $params);
+ [$values, $extensionValues] = $this->normalizeData($data);
+ $values['storage'] = $this->getNumericStorageId();
try {
$builder = $this->connection->getQueryBuilder();
@@ -289,7 +290,19 @@ class Cache implements ICache {
}
if ($builder->execute()) {
- $fileId = (int)$this->connection->lastInsertId('*PREFIX*filecache');
+ $fileId = $builder->getLastInsertId();
+
+ if (count($extensionValues)) {
+ $query = $this->getQueryBuilder();
+ $query->insert('filecache_extended');
+
+ $query->setValue('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT));
+ foreach ($extensionValues as $column => $value) {
+ $query->setValue($column, $query->createNamedParameter($value));
+ }
+ $query->execute();
+ }
+
$this->eventDispatcher->dispatch(CacheInsertEvent::class, new CacheInsertEvent($this->storage, $file, $fileId));
return $fileId;
}
@@ -324,20 +337,56 @@ class Cache implements ICache {
$data['name'] = $this->normalize($data['name']);
}
- list($queryParts, $params) = $this->buildParts($data);
- // duplicate $params because we need the parts twice in the SQL statement
- // once for the SET part, once in the WHERE clause
- $params = array_merge($params, $params);
- $params[] = $id;
+ [$values, $extensionValues] = $this->normalizeData($data);
- // don't update if the data we try to set is the same as the one in the record
- // some databases (Postgres) don't like superfluous updates
- $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' .
- 'WHERE (' .
- implode(' <> ? OR ', $queryParts) . ' <> ? OR ' .
- implode(' IS NULL OR ', $queryParts) . ' IS NULL' .
- ') AND `fileid` = ? ';
- $this->connection->executeQuery($sql, $params);
+ if (count($values)) {
+ $query = $this->getQueryBuilder();
+
+ $query->update('filecache')
+ ->whereFileId($id)
+ ->andWhere($query->expr()->orX(...array_map(function ($key, $value) use ($query) {
+ return $query->expr()->orX(
+ $query->expr()->neq($key, $query->createNamedParameter($value)),
+ $query->expr()->isNull($key)
+ );
+ }, array_keys($values), array_values($values))));
+
+ foreach ($values as $key => $value) {
+ $query->set($key, $query->createNamedParameter($value));
+ }
+
+ $query->execute();
+ }
+
+ if (count($extensionValues)) {
+ try {
+ $query = $this->getQueryBuilder();
+ $query->insert('filecache_extended');
+
+ $query->setValue('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT));
+ foreach ($extensionValues as $column => $value) {
+ $query->setValue($column, $query->createNamedParameter($value));
+ }
+
+ $query->execute();
+ } catch (UniqueConstraintViolationException $e) {
+ $query = $this->getQueryBuilder();
+ $query->update('filecache_extended')
+ ->whereFileId($id)
+ ->andWhere($query->expr()->orX(...array_map(function ($key, $value) use ($query) {
+ return $query->expr()->orX(
+ $query->expr()->neq($key, $query->createNamedParameter($value)),
+ $query->expr()->isNull($key)
+ );
+ }, array_keys($extensionValues), array_values($extensionValues))));
+
+ foreach ($extensionValues as $key => $value) {
+ $query->set($key, $query->createNamedParameter($value));
+ }
+
+ $query->execute();
+ }
+ }
$path = $this->getPathById($id);
// path can still be null if the file doesn't exist
@@ -350,14 +399,13 @@ class Cache implements ICache {
* extract query parts and params array from data array
*
* @param array $data
- * @return array [$queryParts, $params]
- * $queryParts: string[], the (escaped) column names to be set in the query
- * $params: mixed[], the new values for the columns, to be passed as params to the query
+ * @return array
*/
- protected function buildParts(array $data) {
- $fields = array(
+ protected function normalizeData(array $data): array {
+ $fields = [
'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted',
- 'etag', 'permissions', 'checksum', 'storage');
+ 'etag', 'permissions', 'checksum', 'storage'];
+ $extensionFields = ['metadata_etag', 'creation_time', 'upload_time'];
$doNotCopyStorageMTime = false;
if (array_key_exists('mtime', $data) && $data['mtime'] === null) {
@@ -366,23 +414,20 @@ class Cache implements ICache {
$doNotCopyStorageMTime = true;
}
- $params = array();
- $queryParts = array();
+ $params = [];
+ $extensionParams = [];
foreach ($data as $name => $value) {
if (array_search($name, $fields) !== false) {
if ($name === 'path') {
- $params[] = md5($value);
- $queryParts[] = '`path_hash`';
- } elseif ($name === 'mimetype') {
- $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/')));
- $queryParts[] = '`mimepart`';
+ $params['path_hash'] = md5($value);
+ } else if ($name === 'mimetype') {
+ $params['mimepart'] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/')));
$value = $this->mimetypeLoader->getId($value);
- } elseif ($name === 'storage_mtime') {
+ } else if ($name === 'storage_mtime') {
if (!$doNotCopyStorageMTime && !isset($data['mtime'])) {
- $params[] = $value;
- $queryParts[] = '`mtime`';
+ $params['mtime'] = $value;
}
- } elseif ($name === 'encrypted') {
+ } else if ($name === 'encrypted') {
if (isset($data['encryptedVersion'])) {
$value = $data['encryptedVersion'];
} else {
@@ -390,11 +435,13 @@ class Cache implements ICache {
$value = $value ? 1 : 0;
}
}
- $params[] = $value;
- $queryParts[] = '`' . $name . '`';
+ $params[$name] = $value;
+ }
+ if (array_search($name, $extensionFields) !== false) {
+ $extensionParams[$name] = $value;
}
}
- return array($queryParts, $params);
+ return [$params, array_filter($extensionParams)];
}
/**
@@ -411,15 +458,14 @@ class Cache implements ICache {
// normalize file
$file = $this->normalize($file);
- $pathHash = md5($file);
+ $query = $this->getQueryBuilder();
+ $query->select('fileid')
+ ->from('filecache')
+ ->whereStorageId()
+ ->wherePath($file);
- $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
- $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
- if ($row = $result->fetch()) {
- return (int)$row['fileid'];
- } else {
- return -1;
- }
+ $id = $query->execute()->fetchColumn();
+ return $id === false ? -1 : (int)$id;
}
/**
@@ -464,39 +510,64 @@ class Cache implements ICache {
*/
public function remove($file) {
$entry = $this->get($file);
- $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
- $this->connection->executeQuery($sql, array($entry['fileid']));
- if ($entry['mimetype'] === 'httpd/unix-directory') {
- $this->removeChildren($entry);
+
+ if ($entry) {
+ $query = $this->getQueryBuilder();
+ $query->delete('filecache')
+ ->whereFileId($entry->getId());
+ $query->execute();
+
+ $query = $this->getQueryBuilder();
+ $query->delete('filecache_extended')
+ ->whereFileId($entry->getId());
+ $query->execute();
+
+ if ($entry->getMimeType() == FileInfo::MIMETYPE_FOLDER) {
+ $this->removeChildren($entry);
+ }
}
}
/**
* Get all sub folders of a folder
*
- * @param array $entry the cache entry of the folder to get the subfolders for
- * @return array[] the cache entries for the subfolders
+ * @param ICacheEntry $entry the cache entry of the folder to get the subfolders for
+ * @return ICacheEntry[] the cache entries for the subfolders
*/
- private function getSubFolders($entry) {
- $children = $this->getFolderContentsById($entry['fileid']);
+ private function getSubFolders(ICacheEntry $entry) {
+ $children = $this->getFolderContentsById($entry->getId());
return array_filter($children, function ($child) {
- return $child['mimetype'] === 'httpd/unix-directory';
+ return $child->getMimeType() == FileInfo::MIMETYPE_FOLDER;
});
}
/**
* Recursively remove all children of a folder
*
- * @param array $entry the cache entry of the folder to remove the children of
+ * @param ICacheEntry $entry the cache entry of the folder to remove the children of
* @throws \OC\DatabaseException
*/
- private function removeChildren($entry) {
- $subFolders = $this->getSubFolders($entry);
- foreach ($subFolders as $folder) {
+ private function removeChildren(ICacheEntry $entry) {
+ $children = $this->getFolderContentsById($entry->getId());
+ $childIds = array_map(function(ICacheEntry $cacheEntry) {
+ return $cacheEntry->getId();
+ }, $children);
+ $childFolders = array_filter($children, function ($child) {
+ return $child->getMimeType() == FileInfo::MIMETYPE_FOLDER;
+ });
+ foreach ($childFolders as $folder) {
$this->removeChildren($folder);
}
- $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?';
- $this->connection->executeQuery($sql, array($entry['fileid']));
+
+ $query = $this->getQueryBuilder();
+ $query->delete('filecache')
+ ->whereParent($entry->getId());
+ $query->execute();
+
+ $query = $this->getQueryBuilder();
+ $query->delete('filecache_extended')
+ ->where($query->expr()->in('fileid', $query->createNamedParameter($childIds, IQueryBuilder::PARAM_INT_ARRAY)));
+ $query->execute();
}
/**
@@ -575,8 +646,16 @@ class Cache implements ICache {
}
}
- $sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?';
- $this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId));
+ $query = $this->getQueryBuilder();
+ $query->update('filecache')
+ ->set('storage', $query->createNamedParameter($targetStorageId))
+ ->set('path', $query->createNamedParameter($targetPath))
+ ->set('path_hash', $query->createNamedParameter(md5($targetPath)))
+ ->set('name', $query->createNamedParameter(basename($targetPath)))
+ ->set('parent', $query->createNamedParameter($newParentId, IQueryBuilder::PARAM_INT))
+ ->whereFileId($sourceId);
+ $query->execute();
+
$this->connection->commit();
} else {
$this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
@@ -587,11 +666,15 @@ class Cache implements ICache {
* remove all entries for files that are stored on the storage from the cache
*/
public function clear() {
- $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
- $this->connection->executeQuery($sql, array($this->getNumericStorageId()));
+ $query = $this->getQueryBuilder();
+ $query->delete('filecache')
+ ->whereStorageId();
+ $query->execute();
- $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
- $this->connection->executeQuery($sql, array($this->storageId));
+ $query = $this->connection->getQueryBuilder();
+ $query->delete('storages')
+ ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
+ $query->execute();
}
/**
@@ -610,11 +693,14 @@ class Cache implements ICache {
// normalize file
$file = $this->normalize($file);
- $pathHash = md5($file);
- $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
- $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
- if ($row = $result->fetch()) {
- if ((int)$row['size'] === -1) {
+ $query = $this->getQueryBuilder();
+ $query->select('size')
+ ->from('filecache')
+ ->whereStorageId()
+ ->wherePath($file);
+ $size = $query->execute()->fetchColumn();
+ if ($size !== false) {
+ if ((int)$size === -1) {
return self::SHALLOW;
} else {
return self::COMPLETE;
@@ -642,18 +728,14 @@ class Cache implements ICache {
return [];
}
+ $query = $this->getQueryBuilder();
+ $query->selectFileCache()
+ ->whereStorageId()
+ ->andWhere($query->expr()->iLike('name', $query->createNamedParameter($pattern)));
- $sql = '
- SELECT `fileid`, `storage`, `path`, `parent`, `name`,
- `mimetype`, `storage_mtime`, `mimepart`, `size`, `mtime`,
- `encrypted`, `etag`, `permissions`, `checksum`
- FROM `*PREFIX*filecache`
- WHERE `storage` = ? AND `name` ILIKE ?';
- $result = $this->connection->executeQuery($sql,
- [$this->getNumericStorageId(), $pattern]
- );
-
- return $this->searchResultToCacheEntries($result);
+ return array_map(function (array $data) {
+ return self::cacheEntryFromData($data, $this->mimetypeLoader);
+ }, $query->execute()->fetchAll());
}
/**
@@ -676,26 +758,29 @@ class Cache implements ICache {
* @return ICacheEntry[] an array of cache entries where the mimetype matches the search
*/
public function searchByMime($mimetype) {
+ $mimeId = $this->mimetypeLoader->getId($mimetype);
+
+ $query = $this->getQueryBuilder();
+ $query->selectFileCache()
+ ->whereStorageId();
+
if (strpos($mimetype, '/')) {
- $where = '`mimetype` = ?';
+ $query->andWhere($query->expr()->eq('mimetype', $query->createNamedParameter($mimeId, IQueryBuilder::PARAM_INT)));
} else {
- $where = '`mimepart` = ?';
+ $query->andWhere($query->expr()->eq('mimepart', $query->createNamedParameter($mimeId, IQueryBuilder::PARAM_INT)));
}
- $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum`
- FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?';
- $mimetype = $this->mimetypeLoader->getId($mimetype);
- $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId()));
- return $this->searchResultToCacheEntries($result);
+ return array_map(function (array $data) {
+ return self::cacheEntryFromData($data, $this->mimetypeLoader);
+ }, $query->execute()->fetchAll());
}
public function searchQuery(ISearchQuery $searchQuery) {
- $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
+ $builder = $this->getQueryBuilder();
- $query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum'])
- ->from('filecache', 'file');
+ $query = $builder->selectFileCache('file');
- $query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId())));
+ $query->whereStorageId();
if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) {
$query
@@ -755,10 +840,13 @@ class Cache implements ICache {
*/
public function getIncompleteChildrenCount($fileId) {
if ($fileId > -1) {
- $sql = 'SELECT count(*)
- FROM `*PREFIX*filecache` WHERE `parent` = ? AND size = -1';
- $result = $this->connection->executeQuery($sql, [$fileId]);
- return (int)$result->fetchColumn();
+ $query = $this->getQueryBuilder();
+ $query->select($query->func()->count())
+ ->from('filecache')
+ ->whereParent($fileId)
+ ->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
+
+ return (int)$query->execute()->fetchColumn();
}
return -1;
}
@@ -775,14 +863,17 @@ class Cache implements ICache {
if (is_null($entry) or !isset($entry['fileid'])) {
$entry = $this->get($path);
}
- if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') {
+ if (isset($entry['mimetype']) && $entry['mimetype'] === FileInfo::MIMETYPE_FOLDER) {
$id = $entry['fileid'];
- $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' .
- 'FROM `*PREFIX*filecache` ' .
- 'WHERE `parent` = ? AND `storage` = ?';
- $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
- if ($row = $result->fetch()) {
- $result->closeCursor();
+
+ $query = $this->getQueryBuilder();
+ $query->selectAlias($query->func()->sum('size'), 'f1')
+ ->selectAlias($query->func()->min('size'), 'f2')
+ ->from('filecache')
+ ->whereStorageId()
+ ->whereParent($id);
+
+ if ($row = $query->execute()->fetch()) {
list($sum, $min) = array_values($row);
$sum = 0 + $sum;
$min = 0 + $min;
@@ -791,15 +882,9 @@ class Cache implements ICache {
} else {
$totalSize = $sum;
}
- $update = array();
if ($entry['size'] !== $totalSize) {
- $update['size'] = $totalSize;
+ $this->update($id, ['size' => $totalSize]);
}
- if (count($update) > 0) {
- $this->update($id, $update);
- }
- } else {
- $result->closeCursor();
}
}
return $totalSize;
@@ -811,13 +896,14 @@ class Cache implements ICache {
* @return int[]
*/
public function getAll() {
- $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?';
- $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId()));
- $ids = array();
- while ($row = $result->fetch()) {
- $ids[] = $row['fileid'];
- }
- return $ids;
+ $query = $this->getQueryBuilder();
+ $query->select('fileid')
+ ->from('filecache')
+ ->whereStorageId();
+
+ return array_map(function ($id) {
+ return (int)$id;
+ }, $query->execute()->fetchAll(\PDO::FETCH_COLUMN));
}
/**
@@ -830,14 +916,14 @@ class Cache implements ICache {
* @return string|bool the path of the folder or false when no folder matched
*/
public function getIncomplete() {
- $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`'
- . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1);
- $query->execute([$this->getNumericStorageId()]);
- if ($row = $query->fetch()) {
- return $row['path'];
- } else {
- return false;
- }
+ $query = $this->getQueryBuilder();
+ $query->select('path')
+ ->from('filecache')
+ ->whereStorageId()
+ ->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
+ ->orderBy('fileid', 'DESC');
+
+ return $query->execute()->fetchColumn();
}
/**
@@ -847,17 +933,14 @@ class Cache implements ICache {
* @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache
*/
public function getPathById($id) {
- $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?';
- $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
- if ($row = $result->fetch()) {
- // Oracle stores empty strings as null...
- if ($row['path'] === null) {
- return '';
- }
- return $row['path'];
- } else {
- return null;
- }
+ $query = $this->getQueryBuilder();
+ $query->select('path')
+ ->from('filecache')
+ ->whereStorageId()
+ ->whereFileId($id);
+
+ $path = $query->execute()->fetchColumn();
+ return $path === false ? null : $path;
}
/**
@@ -866,14 +949,15 @@ class Cache implements ICache {
* instead does a global search in the cache table
*
* @param int $id
- * @deprecated use getPathById() instead
* @return array first element holding the storage id, second the path
+ * @deprecated use getPathById() instead
*/
static public function getById($id) {
- $connection = \OC::$server->getDatabaseConnection();
- $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
- $result = $connection->executeQuery($sql, array($id));
- if ($row = $result->fetch()) {
+ $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
+ $query->select('path', 'storage')
+ ->from('filecache')
+ ->where($query->expr()->eq('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
+ if ($row = $query->execute()->fetch()) {
$numericId = $row['storage'];
$path = $row['path'];
} else {
@@ -881,7 +965,7 @@ class Cache implements ICache {
}
if ($id = Storage::getStorageId($numericId)) {
- return array($id, $path);
+ return [$id, $path];
} else {
return null;
}
diff --git a/lib/private/Files/Cache/CacheEntry.php b/lib/private/Files/Cache/CacheEntry.php
index 4a2579a88f8..176a0bf27ed 100644
--- a/lib/private/Files/Cache/CacheEntry.php
+++ b/lib/private/Files/Cache/CacheEntry.php
@@ -109,6 +109,18 @@ class CacheEntry implements ICacheEntry, \ArrayAccess {
return isset($this->data['encrypted']) && $this->data['encrypted'];
}
+ public function getMetadataEtag(): ?string {
+ return $this->data['metadata_etag'];
+ }
+
+ public function getCreationTime(): ?int {
+ return $this->data['creation_time'];
+ }
+
+ public function getUploadTime(): ?int {
+ return $this->data['upload_time'];
+ }
+
public function getData() {
return $this->data;
}
diff --git a/lib/private/Files/Cache/CacheQueryBuilder.php b/lib/private/Files/Cache/CacheQueryBuilder.php
new file mode 100644
index 00000000000..a5ff2129de8
--- /dev/null
+++ b/lib/private/Files/Cache/CacheQueryBuilder.php
@@ -0,0 +1,92 @@
+<?php declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OC\Files\Cache;
+
+use OC\DB\QueryBuilder\QueryBuilder;
+use OC\SystemConfig;
+use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\IDBConnection;
+use OCP\ILogger;
+
+/**
+ * Query builder with commonly used helpers for filecache queries
+ */
+class CacheQueryBuilder extends QueryBuilder {
+ private $cache;
+ private $alias = null;
+
+ public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger, Cache $cache) {
+ parent::__construct($connection, $systemConfig, $logger);
+
+ $this->cache = $cache;
+ }
+
+ public function selectFileCache(string $alias = null) {
+ $name = $alias ? $alias : 'filecache';
+ $this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", 'name', 'mimetype', 'mimepart', 'size', 'mtime',
+ 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum', 'metadata_etag', 'creation_time', 'upload_time')
+ ->from('filecache', $name)
+ ->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
+
+ $this->alias = $name;
+
+ return $this;
+ }
+
+ public function whereStorageId() {
+ $this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)));
+
+ return $this;
+ }
+
+ public function whereFileId(int $fileId) {
+ $alias = $this->alias;
+ if ($alias) {
+ $alias .= '.';
+ } else {
+ $alias = '';
+ }
+
+ $this->andWhere($this->expr()->eq("{$alias}fileid", $this->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
+
+ return $this;
+ }
+
+ public function wherePath(string $path) {
+ $this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));
+
+ return $this;
+ }
+
+ public function whereParent(int $parent) {
+ $alias = $this->alias;
+ if ($alias) {
+ $alias .= '.';
+ } else {
+ $alias = '';
+ }
+
+ $this->andWhere($this->expr()->eq("{$alias}parent", $this->createNamedParameter($parent, IQueryBuilder::PARAM_INT)));
+
+ return $this;
+ }
+}
diff --git a/lib/private/Files/Cache/MoveFromCacheTrait.php b/lib/private/Files/Cache/MoveFromCacheTrait.php
index a814a081dca..e9b41bd9b37 100644
--- a/lib/private/Files/Cache/MoveFromCacheTrait.php
+++ b/lib/private/Files/Cache/MoveFromCacheTrait.php
@@ -82,7 +82,10 @@ trait MoveFromCacheTrait {
'mimepart' => $entry->getMimePart(),
'etag' => $entry->getEtag(),
'permissions' => $entry->getPermissions(),
- 'encrypted' => $entry->isEncrypted()
+ 'encrypted' => $entry->isEncrypted(),
+ 'creation_time' => $entry->getCreationTime(),
+ 'upload_time' => $entry->getUploadTime(),
+ 'metadata_etag' => $entry->getMetadataEtag(),
];
}
}
diff --git a/lib/private/Files/FileInfo.php b/lib/private/Files/FileInfo.php
index 19b95cd0355..93f876db17b 100644
--- a/lib/private/Files/FileInfo.php
+++ b/lib/private/Files/FileInfo.php
@@ -406,4 +406,12 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
public function getExtension(): string {
return pathinfo($this->getName(), PATHINFO_EXTENSION);
}
+
+ public function getCreationTime(): int {
+ return (int) $this->data['creation_time'];
+ }
+
+ public function getUploadTime(): int {
+ return (int) $this->data['upload_time'];
+ }
}
diff --git a/lib/private/Files/Node/LazyRoot.php b/lib/private/Files/Node/LazyRoot.php
index 01b4ca52765..f5d8cdc91a9 100644
--- a/lib/private/Files/Node/LazyRoot.php
+++ b/lib/private/Files/Node/LazyRoot.php
@@ -480,4 +480,18 @@ class LazyRoot implements IRootFolder {
public function getRecent($limit, $offset = 0) {
return $this->__call(__FUNCTION__, func_get_args());
}
+
+ /**
+ * @inheritDoc
+ */
+ public function getCreationTime(): int {
+ return $this->__call(__FUNCTION__, func_get_args());
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function getUploadTime(): int {
+ return $this->__call(__FUNCTION__, func_get_args());
+ }
}
diff --git a/lib/private/Files/Node/Node.php b/lib/private/Files/Node/Node.php
index c440dd4a8f1..95d16cf5c99 100644
--- a/lib/private/Files/Node/Node.php
+++ b/lib/private/Files/Node/Node.php
@@ -444,4 +444,12 @@ class Node implements \OCP\Files\Node {
}
}
+ public function getCreationTime(): int {
+ return $this->getFileInfo()->getCreationTime();
+ }
+
+ public function getUploadTime(): int {
+ return $this->getFileInfo()->getUploadTime();
+ }
+
}
diff --git a/lib/private/Share20/DefaultShareProvider.php b/lib/private/Share20/DefaultShareProvider.php
index 05b3094e6a2..7a146bf7890 100644
--- a/lib/private/Share20/DefaultShareProvider.php
+++ b/lib/private/Share20/DefaultShareProvider.php
@@ -142,6 +142,7 @@ class DefaultShareProvider implements IShareProvider {
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
//Set the UID of the user we share with
$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
+ $qb->setValue('accepted', $qb->createNamedParameter(IShare::STATUS_PENDING));
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
//Set the GID of the group we share with
$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
@@ -252,6 +253,7 @@ class DefaultShareProvider implements IShareProvider {
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
->set('note', $qb->createNamedParameter($share->getNote()))
+ ->set('accepted', $qb->createNamedParameter($share->getStatus()))
->execute();
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
$qb = $this->dbConn->getQueryBuilder();
@@ -319,6 +321,72 @@ class DefaultShareProvider implements IShareProvider {
}
/**
+ * Accept a share.
+ *
+ * @param IShare $share
+ * @param string $recipient
+ * @return IShare The share object
+ * @since 9.0.0
+ */
+ public function acceptShare(IShare $share, string $recipient): IShare {
+ if ($share->getShareType() === IShare::TYPE_GROUP) {
+ $group = $this->groupManager->get($share->getSharedWith());
+ $user = $this->userManager->get($recipient);
+
+ if (is_null($group)) {
+ throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
+ }
+
+ if (!$group->inGroup($user)) {
+ throw new ProviderException('Recipient not in receiving group');
+ }
+
+ // Try to fetch user specific share
+ $qb = $this->dbConn->getQueryBuilder();
+ $stmt = $qb->select('*')
+ ->from('share')
+ ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
+ ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
+ ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
+ ->andWhere($qb->expr()->orX(
+ $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
+ $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
+ ))
+ ->execute();
+
+ $data = $stmt->fetch();
+ $stmt->closeCursor();
+
+ /*
+ * Check if there already is a user specific group share.
+ * If there is update it (if required).
+ */
+ if ($data === false) {
+ $id = $this->createUserSpecificGroupShare($share, $recipient);
+ } else {
+ $id = $data['id'];
+ }
+
+ } else if ($share->getShareType() === IShare::TYPE_USER) {
+ if ($share->getSharedWith() !== $recipient) {
+ throw new ProviderException('Recipient does not match');
+ }
+
+ $id = $share->getId();
+ } else {
+ throw new ProviderException('Invalid shareType');
+ }
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->update('share')
+ ->set('accepted', $qb->createNamedParameter(IShare::STATUS_ACCEPTED))
+ ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
+ ->execute();
+
+ return $share;
+ }
+
+ /**
* Get all children of this share
* FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
*
@@ -382,13 +450,13 @@ class DefaultShareProvider implements IShareProvider {
* Unshare a share from the recipient. If this is a group share
* this means we need a special entry in the share db.
*
- * @param \OCP\Share\IShare $share
+ * @param IShare $share
* @param string $recipient UserId of recipient
* @throws BackendError
* @throws ProviderException
*/
- public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
- if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
+ public function deleteFromSelf(IShare $share, $recipient) {
+ if ($share->getShareType() === IShare::TYPE_GROUP) {
$group = $this->groupManager->get($share->getSharedWith());
$user = $this->userManager->get($recipient);
@@ -421,37 +489,23 @@ class DefaultShareProvider implements IShareProvider {
* If there is update it (if required).
*/
if ($data === false) {
- $qb = $this->dbConn->getQueryBuilder();
-
- $type = $share->getNodeType();
-
- //Insert new share
- $qb->insert('share')
- ->values([
- 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
- 'share_with' => $qb->createNamedParameter($recipient),
- 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
- 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
- 'parent' => $qb->createNamedParameter($share->getId()),
- 'item_type' => $qb->createNamedParameter($type),
- 'item_source' => $qb->createNamedParameter($share->getNodeId()),
- 'file_source' => $qb->createNamedParameter($share->getNodeId()),
- 'file_target' => $qb->createNamedParameter($share->getTarget()),
- 'permissions' => $qb->createNamedParameter(0),
- 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
- ])->execute();
-
- } else if ($data['permissions'] !== 0) {
+ $id = $this->createUserSpecificGroupShare($share, $recipient);
+ $permissions = $share->getPermissions();
+ } else {
+ $permissions = $data['permissions'];
+ $id = $data['id'];
+ }
+ if ($permissions !== 0) {
// Update existing usergroup share
$qb = $this->dbConn->getQueryBuilder();
$qb->update('share')
->set('permissions', $qb->createNamedParameter(0))
- ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
+ ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
->execute();
}
- } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
+ } else if ($share->getShareType() === IShare::TYPE_USER) {
if ($share->getSharedWith() !== $recipient) {
throw new ProviderException('Recipient does not match');
@@ -464,6 +518,28 @@ class DefaultShareProvider implements IShareProvider {
}
}
+ protected function createUserSpecificGroupShare(IShare $share, string $recipient): int {
+ $type = $share->getNodeType();
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
+ 'share_with' => $qb->createNamedParameter($recipient),
+ 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
+ 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
+ 'parent' => $qb->createNamedParameter($share->getId()),
+ 'item_type' => $qb->createNamedParameter($type),
+ 'item_source' => $qb->createNamedParameter($share->getNodeId()),
+ 'file_source' => $qb->createNamedParameter($share->getNodeId()),
+ 'file_target' => $qb->createNamedParameter($share->getTarget()),
+ 'permissions' => $qb->createNamedParameter($share->getPermissions()),
+ 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
+ ])->execute();
+
+ return $qb->getLastInsertId();
+ }
+
/**
* @inheritdoc
*
@@ -932,6 +1008,7 @@ class DefaultShareProvider implements IShareProvider {
->setTarget($data['file_target'])
->setNote($data['note'])
->setMailSend((bool)$data['mail_send'])
+ ->setStatus((int)$data['accepted'])
->setLabel($data['label']);
$shareTime = new \DateTime();
@@ -1020,6 +1097,7 @@ class DefaultShareProvider implements IShareProvider {
while($data = $stmt->fetch()) {
$shareMap[$data['parent']]->setPermissions((int)$data['permissions']);
+ $shareMap[$data['parent']]->setStatus((int)$data['accepted']);
$shareMap[$data['parent']]->setTarget($data['file_target']);
$shareMap[$data['parent']]->setParent($data['parent']);
}
diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php
index 728dd60e759..298aa6f0f57 100644
--- a/lib/private/Share20/Manager.php
+++ b/lib/private/Share20/Manager.php
@@ -929,6 +929,30 @@ class Manager implements IManager {
}
/**
+ * Accept a share.
+ *
+ * @param IShare $share
+ * @param string $recipientId
+ * @return IShare The share object
+ * @throws \InvalidArgumentException
+ * @since 9.0.0
+ */
+ public function acceptShare(IShare $share, string $recipientId): IShare {
+ [$providerId, ] = $this->splitFullId($share->getFullId());
+ $provider = $this->factory->getProvider($providerId);
+
+ if (!method_exists($provider, 'acceptShare')) {
+ // TODO FIX ME
+ throw new \InvalidArgumentException('Share provider does not support accepting');
+ }
+ $provider->acceptShare($share, $recipientId);
+ $event = new GenericEvent($share);
+ $this->eventDispatcher->dispatch('OCP\Share::postAcceptShare', $event);
+
+ return $share;
+ }
+
+ /**
* Updates the password of the given share if it is not the same as the
* password of the original share.
*
diff --git a/lib/private/Share20/Share.php b/lib/private/Share20/Share.php
index 9ce88b5af22..57b5304b102 100644
--- a/lib/private/Share20/Share.php
+++ b/lib/private/Share20/Share.php
@@ -58,6 +58,8 @@ class Share implements \OCP\Share\IShare {
private $shareOwner;
/** @var int */
private $permissions;
+ /** @var int */
+ private $status;
/** @var string */
private $note = '';
/** @var \DateTime */
@@ -321,6 +323,21 @@ class Share implements \OCP\Share\IShare {
/**
* @inheritdoc
*/
+ public function setStatus(int $status): IShare {
+ $this->status = $status;
+ return $this;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getStatus(): int {
+ return $this->status;
+ }
+
+ /**
+ * @inheritdoc
+ */
public function setNote($note) {
$this->note = $note;
return $this;