@@ -191,9 +191,9 @@ discard block |
||
| 191 | 191 | $params = []; |
| 192 | 192 | foreach ($this->getParameters() as $placeholder => $value) { |
| 193 | 193 | if (is_array($value)) { |
| 194 | - $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
| 194 | + $params[] = $placeholder.' => (\''.implode('\', \'', $value).'\')'; |
|
| 195 | 195 | } else { |
| 196 | - $params[] = $placeholder . ' => \'' . $value . '\''; |
|
| 196 | + $params[] = $placeholder.' => \''.$value.'\''; |
|
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | 199 | if (empty($params)) { |
@@ -410,7 +410,7 @@ discard block |
||
| 410 | 410 | public function selectAlias($select, $alias) { |
| 411 | 411 | |
| 412 | 412 | $this->queryBuilder->addSelect( |
| 413 | - $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
| 413 | + $this->helper->quoteColumnName($select).' AS '.$this->helper->quoteColumnName($alias) |
|
| 414 | 414 | ); |
| 415 | 415 | |
| 416 | 416 | return $this; |
@@ -432,7 +432,7 @@ discard block |
||
| 432 | 432 | public function selectDistinct($select) { |
| 433 | 433 | |
| 434 | 434 | $this->queryBuilder->addSelect( |
| 435 | - 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
| 435 | + 'DISTINCT '.$this->helper->quoteColumnName($select) |
|
| 436 | 436 | ); |
| 437 | 437 | |
| 438 | 438 | return $this; |
@@ -1109,7 +1109,7 @@ discard block |
||
| 1109 | 1109 | * @return IParameter |
| 1110 | 1110 | */ |
| 1111 | 1111 | public function createParameter($name) { |
| 1112 | - return new Parameter(':' . $name); |
|
| 1112 | + return new Parameter(':'.$name); |
|
| 1113 | 1113 | } |
| 1114 | 1114 | |
| 1115 | 1115 | /** |
@@ -1176,7 +1176,7 @@ discard block |
||
| 1176 | 1176 | return $table; |
| 1177 | 1177 | } |
| 1178 | 1178 | |
| 1179 | - return '*PREFIX*' . $table; |
|
| 1179 | + return '*PREFIX*'.$table; |
|
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | 1182 | /** |
@@ -1191,7 +1191,7 @@ discard block |
||
| 1191 | 1191 | $tableAlias .= '.'; |
| 1192 | 1192 | } |
| 1193 | 1193 | |
| 1194 | - return $this->helper->quoteColumnName($tableAlias . $column); |
|
| 1194 | + return $this->helper->quoteColumnName($tableAlias.$column); |
|
| 1195 | 1195 | } |
| 1196 | 1196 | |
| 1197 | 1197 | /** |
@@ -46,1165 +46,1165 @@ |
||
| 46 | 46 | |
| 47 | 47 | class QueryBuilder implements IQueryBuilder { |
| 48 | 48 | |
| 49 | - /** @var \OCP\IDBConnection */ |
|
| 50 | - private $connection; |
|
| 51 | - |
|
| 52 | - /** @var SystemConfig */ |
|
| 53 | - private $systemConfig; |
|
| 54 | - |
|
| 55 | - /** @var ILogger */ |
|
| 56 | - private $logger; |
|
| 57 | - |
|
| 58 | - /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
| 59 | - private $queryBuilder; |
|
| 60 | - |
|
| 61 | - /** @var QuoteHelper */ |
|
| 62 | - private $helper; |
|
| 63 | - |
|
| 64 | - /** @var bool */ |
|
| 65 | - private $automaticTablePrefix = true; |
|
| 66 | - |
|
| 67 | - /** @var string */ |
|
| 68 | - protected $lastInsertedTable; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Initializes a new QueryBuilder. |
|
| 72 | - * |
|
| 73 | - * @param IDBConnection $connection |
|
| 74 | - * @param SystemConfig $systemConfig |
|
| 75 | - * @param ILogger $logger |
|
| 76 | - */ |
|
| 77 | - public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) { |
|
| 78 | - $this->connection = $connection; |
|
| 79 | - $this->systemConfig = $systemConfig; |
|
| 80 | - $this->logger = $logger; |
|
| 81 | - $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); |
|
| 82 | - $this->helper = new QuoteHelper(); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 87 | - * |
|
| 88 | - * @param bool $enabled If set to true table names will be prefixed with the |
|
| 89 | - * owncloud database prefix automatically. |
|
| 90 | - * @since 8.2.0 |
|
| 91 | - */ |
|
| 92 | - public function automaticTablePrefix($enabled) { |
|
| 93 | - $this->automaticTablePrefix = (bool) $enabled; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 98 | - * This producer method is intended for convenient inline usage. Example: |
|
| 99 | - * |
|
| 100 | - * <code> |
|
| 101 | - * $qb = $conn->getQueryBuilder() |
|
| 102 | - * ->select('u') |
|
| 103 | - * ->from('users', 'u') |
|
| 104 | - * ->where($qb->expr()->eq('u.id', 1)); |
|
| 105 | - * </code> |
|
| 106 | - * |
|
| 107 | - * For more complex expression construction, consider storing the expression |
|
| 108 | - * builder object in a local variable. |
|
| 109 | - * |
|
| 110 | - * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 111 | - */ |
|
| 112 | - public function expr() { |
|
| 113 | - if ($this->connection instanceof OracleConnection) { |
|
| 114 | - return new OCIExpressionBuilder($this->connection); |
|
| 115 | - } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 116 | - return new PgSqlExpressionBuilder($this->connection); |
|
| 117 | - } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 118 | - return new MySqlExpressionBuilder($this->connection); |
|
| 119 | - } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 120 | - return new SqliteExpressionBuilder($this->connection); |
|
| 121 | - } else { |
|
| 122 | - return new ExpressionBuilder($this->connection); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 128 | - * This producer method is intended for convenient inline usage. Example: |
|
| 129 | - * |
|
| 130 | - * <code> |
|
| 131 | - * $qb = $conn->getQueryBuilder() |
|
| 132 | - * ->select('u') |
|
| 133 | - * ->from('users', 'u') |
|
| 134 | - * ->where($qb->fun()->md5('u.id')); |
|
| 135 | - * </code> |
|
| 136 | - * |
|
| 137 | - * For more complex function construction, consider storing the function |
|
| 138 | - * builder object in a local variable. |
|
| 139 | - * |
|
| 140 | - * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 141 | - */ |
|
| 142 | - public function func() { |
|
| 143 | - if ($this->connection instanceof OracleConnection) { |
|
| 144 | - return new OCIFunctionBuilder($this->helper); |
|
| 145 | - } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 146 | - return new SqliteFunctionBuilder($this->helper); |
|
| 147 | - } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 148 | - return new PgSqlFunctionBuilder($this->helper); |
|
| 149 | - } else { |
|
| 150 | - return new FunctionBuilder($this->helper); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Gets the type of the currently built query. |
|
| 156 | - * |
|
| 157 | - * @return integer |
|
| 158 | - */ |
|
| 159 | - public function getType() { |
|
| 160 | - return $this->queryBuilder->getType(); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Gets the associated DBAL Connection for this query builder. |
|
| 165 | - * |
|
| 166 | - * @return \OCP\IDBConnection |
|
| 167 | - */ |
|
| 168 | - public function getConnection() { |
|
| 169 | - return $this->connection; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Gets the state of this query builder instance. |
|
| 174 | - * |
|
| 175 | - * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
| 176 | - */ |
|
| 177 | - public function getState() { |
|
| 178 | - return $this->queryBuilder->getState(); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Executes this query using the bound parameters and their types. |
|
| 183 | - * |
|
| 184 | - * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 185 | - * for insert, update and delete statements. |
|
| 186 | - * |
|
| 187 | - * @return \Doctrine\DBAL\Driver\Statement|int |
|
| 188 | - */ |
|
| 189 | - public function execute() { |
|
| 190 | - if ($this->systemConfig->getValue('log_query', false)) { |
|
| 191 | - $params = []; |
|
| 192 | - foreach ($this->getParameters() as $placeholder => $value) { |
|
| 193 | - if (is_array($value)) { |
|
| 194 | - $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
| 195 | - } else { |
|
| 196 | - $params[] = $placeholder . ' => \'' . $value . '\''; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - if (empty($params)) { |
|
| 200 | - $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
| 201 | - 'query' => $this->getSQL(), |
|
| 202 | - 'app' => 'core', |
|
| 203 | - ]); |
|
| 204 | - } else { |
|
| 205 | - $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
| 206 | - 'query' => $this->getSQL(), |
|
| 207 | - 'params' => implode(', ', $params), |
|
| 208 | - 'app' => 'core', |
|
| 209 | - ]); |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return $this->queryBuilder->execute(); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 218 | - * |
|
| 219 | - * <code> |
|
| 220 | - * $qb = $conn->getQueryBuilder() |
|
| 221 | - * ->select('u') |
|
| 222 | - * ->from('User', 'u') |
|
| 223 | - * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 224 | - * </code> |
|
| 225 | - * |
|
| 226 | - * @return string The SQL query string. |
|
| 227 | - */ |
|
| 228 | - public function getSQL() { |
|
| 229 | - return $this->queryBuilder->getSQL(); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Sets a query parameter for the query being constructed. |
|
| 234 | - * |
|
| 235 | - * <code> |
|
| 236 | - * $qb = $conn->getQueryBuilder() |
|
| 237 | - * ->select('u') |
|
| 238 | - * ->from('users', 'u') |
|
| 239 | - * ->where('u.id = :user_id') |
|
| 240 | - * ->setParameter(':user_id', 1); |
|
| 241 | - * </code> |
|
| 242 | - * |
|
| 243 | - * @param string|integer $key The parameter position or name. |
|
| 244 | - * @param mixed $value The parameter value. |
|
| 245 | - * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants. |
|
| 246 | - * |
|
| 247 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 248 | - */ |
|
| 249 | - public function setParameter($key, $value, $type = null) { |
|
| 250 | - $this->queryBuilder->setParameter($key, $value, $type); |
|
| 251 | - |
|
| 252 | - return $this; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Sets a collection of query parameters for the query being constructed. |
|
| 257 | - * |
|
| 258 | - * <code> |
|
| 259 | - * $qb = $conn->getQueryBuilder() |
|
| 260 | - * ->select('u') |
|
| 261 | - * ->from('users', 'u') |
|
| 262 | - * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 263 | - * ->setParameters(array( |
|
| 264 | - * ':user_id1' => 1, |
|
| 265 | - * ':user_id2' => 2 |
|
| 266 | - * )); |
|
| 267 | - * </code> |
|
| 268 | - * |
|
| 269 | - * @param array $params The query parameters to set. |
|
| 270 | - * @param array $types The query parameters types to set. |
|
| 271 | - * |
|
| 272 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 273 | - */ |
|
| 274 | - public function setParameters(array $params, array $types = array()) { |
|
| 275 | - $this->queryBuilder->setParameters($params, $types); |
|
| 276 | - |
|
| 277 | - return $this; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 282 | - * |
|
| 283 | - * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 284 | - */ |
|
| 285 | - public function getParameters() { |
|
| 286 | - return $this->queryBuilder->getParameters(); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Gets a (previously set) query parameter of the query being constructed. |
|
| 291 | - * |
|
| 292 | - * @param mixed $key The key (index or name) of the bound parameter. |
|
| 293 | - * |
|
| 294 | - * @return mixed The value of the bound parameter. |
|
| 295 | - */ |
|
| 296 | - public function getParameter($key) { |
|
| 297 | - return $this->queryBuilder->getParameter($key); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 302 | - * |
|
| 303 | - * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 304 | - */ |
|
| 305 | - public function getParameterTypes() { |
|
| 306 | - return $this->queryBuilder->getParameterTypes(); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Gets a (previously set) query parameter type of the query being constructed. |
|
| 311 | - * |
|
| 312 | - * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 313 | - * |
|
| 314 | - * @return mixed The value of the bound parameter type. |
|
| 315 | - */ |
|
| 316 | - public function getParameterType($key) { |
|
| 317 | - return $this->queryBuilder->getParameterType($key); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Sets the position of the first result to retrieve (the "offset"). |
|
| 322 | - * |
|
| 323 | - * @param integer $firstResult The first result to return. |
|
| 324 | - * |
|
| 325 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 326 | - */ |
|
| 327 | - public function setFirstResult($firstResult) { |
|
| 328 | - $this->queryBuilder->setFirstResult($firstResult); |
|
| 329 | - |
|
| 330 | - return $this; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 335 | - * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 336 | - * |
|
| 337 | - * @return integer The position of the first result. |
|
| 338 | - */ |
|
| 339 | - public function getFirstResult() { |
|
| 340 | - return $this->queryBuilder->getFirstResult(); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Sets the maximum number of results to retrieve (the "limit"). |
|
| 345 | - * |
|
| 346 | - * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
| 347 | - * of the databases will just return an empty result set, Oracle will return |
|
| 348 | - * all entries. |
|
| 349 | - * |
|
| 350 | - * @param integer $maxResults The maximum number of results to retrieve. |
|
| 351 | - * |
|
| 352 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 353 | - */ |
|
| 354 | - public function setMaxResults($maxResults) { |
|
| 355 | - $this->queryBuilder->setMaxResults($maxResults); |
|
| 356 | - |
|
| 357 | - return $this; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 362 | - * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 363 | - * |
|
| 364 | - * @return integer The maximum number of results. |
|
| 365 | - */ |
|
| 366 | - public function getMaxResults() { |
|
| 367 | - return $this->queryBuilder->getMaxResults(); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Specifies an item that is to be returned in the query result. |
|
| 372 | - * Replaces any previously specified selections, if any. |
|
| 373 | - * |
|
| 374 | - * <code> |
|
| 375 | - * $qb = $conn->getQueryBuilder() |
|
| 376 | - * ->select('u.id', 'p.id') |
|
| 377 | - * ->from('users', 'u') |
|
| 378 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 379 | - * </code> |
|
| 380 | - * |
|
| 381 | - * @param mixed $select The selection expressions. |
|
| 382 | - * |
|
| 383 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 384 | - */ |
|
| 385 | - public function select($select = null) { |
|
| 386 | - $selects = is_array($select) ? $select : func_get_args(); |
|
| 387 | - |
|
| 388 | - $this->queryBuilder->select( |
|
| 389 | - $this->helper->quoteColumnNames($selects) |
|
| 390 | - ); |
|
| 391 | - |
|
| 392 | - return $this; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Specifies an item that is to be returned with a different name in the query result. |
|
| 397 | - * |
|
| 398 | - * <code> |
|
| 399 | - * $qb = $conn->getQueryBuilder() |
|
| 400 | - * ->selectAlias('u.id', 'user_id') |
|
| 401 | - * ->from('users', 'u') |
|
| 402 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 403 | - * </code> |
|
| 404 | - * |
|
| 405 | - * @param mixed $select The selection expressions. |
|
| 406 | - * @param string $alias The column alias used in the constructed query. |
|
| 407 | - * |
|
| 408 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 409 | - */ |
|
| 410 | - public function selectAlias($select, $alias) { |
|
| 411 | - |
|
| 412 | - $this->queryBuilder->addSelect( |
|
| 413 | - $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
| 414 | - ); |
|
| 415 | - |
|
| 416 | - return $this; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Specifies an item that is to be returned uniquely in the query result. |
|
| 421 | - * |
|
| 422 | - * <code> |
|
| 423 | - * $qb = $conn->getQueryBuilder() |
|
| 424 | - * ->selectDistinct('type') |
|
| 425 | - * ->from('users'); |
|
| 426 | - * </code> |
|
| 427 | - * |
|
| 428 | - * @param mixed $select The selection expressions. |
|
| 429 | - * |
|
| 430 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 431 | - */ |
|
| 432 | - public function selectDistinct($select) { |
|
| 433 | - |
|
| 434 | - $this->queryBuilder->addSelect( |
|
| 435 | - 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
| 436 | - ); |
|
| 437 | - |
|
| 438 | - return $this; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Adds an item that is to be returned in the query result. |
|
| 443 | - * |
|
| 444 | - * <code> |
|
| 445 | - * $qb = $conn->getQueryBuilder() |
|
| 446 | - * ->select('u.id') |
|
| 447 | - * ->addSelect('p.id') |
|
| 448 | - * ->from('users', 'u') |
|
| 449 | - * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 450 | - * </code> |
|
| 451 | - * |
|
| 452 | - * @param mixed $select The selection expression. |
|
| 453 | - * |
|
| 454 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 455 | - */ |
|
| 456 | - public function addSelect($select = null) { |
|
| 457 | - $selects = is_array($select) ? $select : func_get_args(); |
|
| 458 | - |
|
| 459 | - $this->queryBuilder->addSelect( |
|
| 460 | - $this->helper->quoteColumnNames($selects) |
|
| 461 | - ); |
|
| 462 | - |
|
| 463 | - return $this; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * Turns the query being built into a bulk delete query that ranges over |
|
| 468 | - * a certain table. |
|
| 469 | - * |
|
| 470 | - * <code> |
|
| 471 | - * $qb = $conn->getQueryBuilder() |
|
| 472 | - * ->delete('users', 'u') |
|
| 473 | - * ->where('u.id = :user_id'); |
|
| 474 | - * ->setParameter(':user_id', 1); |
|
| 475 | - * </code> |
|
| 476 | - * |
|
| 477 | - * @param string $delete The table whose rows are subject to the deletion. |
|
| 478 | - * @param string $alias The table alias used in the constructed query. |
|
| 479 | - * |
|
| 480 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 481 | - */ |
|
| 482 | - public function delete($delete = null, $alias = null) { |
|
| 483 | - $this->queryBuilder->delete( |
|
| 484 | - $this->getTableName($delete), |
|
| 485 | - $alias |
|
| 486 | - ); |
|
| 487 | - |
|
| 488 | - return $this; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Turns the query being built into a bulk update query that ranges over |
|
| 493 | - * a certain table |
|
| 494 | - * |
|
| 495 | - * <code> |
|
| 496 | - * $qb = $conn->getQueryBuilder() |
|
| 497 | - * ->update('users', 'u') |
|
| 498 | - * ->set('u.password', md5('password')) |
|
| 499 | - * ->where('u.id = ?'); |
|
| 500 | - * </code> |
|
| 501 | - * |
|
| 502 | - * @param string $update The table whose rows are subject to the update. |
|
| 503 | - * @param string $alias The table alias used in the constructed query. |
|
| 504 | - * |
|
| 505 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 506 | - */ |
|
| 507 | - public function update($update = null, $alias = null) { |
|
| 508 | - $this->queryBuilder->update( |
|
| 509 | - $this->getTableName($update), |
|
| 510 | - $alias |
|
| 511 | - ); |
|
| 512 | - |
|
| 513 | - return $this; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Turns the query being built into an insert query that inserts into |
|
| 518 | - * a certain table |
|
| 519 | - * |
|
| 520 | - * <code> |
|
| 521 | - * $qb = $conn->getQueryBuilder() |
|
| 522 | - * ->insert('users') |
|
| 523 | - * ->values( |
|
| 524 | - * array( |
|
| 525 | - * 'name' => '?', |
|
| 526 | - * 'password' => '?' |
|
| 527 | - * ) |
|
| 528 | - * ); |
|
| 529 | - * </code> |
|
| 530 | - * |
|
| 531 | - * @param string $insert The table into which the rows should be inserted. |
|
| 532 | - * |
|
| 533 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 534 | - */ |
|
| 535 | - public function insert($insert = null) { |
|
| 536 | - $this->queryBuilder->insert( |
|
| 537 | - $this->getTableName($insert) |
|
| 538 | - ); |
|
| 539 | - |
|
| 540 | - $this->lastInsertedTable = $insert; |
|
| 541 | - |
|
| 542 | - return $this; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Creates and adds a query root corresponding to the table identified by the |
|
| 547 | - * given alias, forming a cartesian product with any existing query roots. |
|
| 548 | - * |
|
| 549 | - * <code> |
|
| 550 | - * $qb = $conn->getQueryBuilder() |
|
| 551 | - * ->select('u.id') |
|
| 552 | - * ->from('users', 'u') |
|
| 553 | - * </code> |
|
| 554 | - * |
|
| 555 | - * @param string $from The table. |
|
| 556 | - * @param string|null $alias The alias of the table. |
|
| 557 | - * |
|
| 558 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 559 | - */ |
|
| 560 | - public function from($from, $alias = null) { |
|
| 561 | - $this->queryBuilder->from( |
|
| 562 | - $this->getTableName($from), |
|
| 563 | - $this->quoteAlias($alias) |
|
| 564 | - ); |
|
| 565 | - |
|
| 566 | - return $this; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Creates and adds a join to the query. |
|
| 571 | - * |
|
| 572 | - * <code> |
|
| 573 | - * $qb = $conn->getQueryBuilder() |
|
| 574 | - * ->select('u.name') |
|
| 575 | - * ->from('users', 'u') |
|
| 576 | - * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 577 | - * </code> |
|
| 578 | - * |
|
| 579 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 580 | - * @param string $join The table name to join. |
|
| 581 | - * @param string $alias The alias of the join table. |
|
| 582 | - * @param string $condition The condition for the join. |
|
| 583 | - * |
|
| 584 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 585 | - */ |
|
| 586 | - public function join($fromAlias, $join, $alias, $condition = null) { |
|
| 587 | - $this->queryBuilder->join( |
|
| 588 | - $this->quoteAlias($fromAlias), |
|
| 589 | - $this->getTableName($join), |
|
| 590 | - $this->quoteAlias($alias), |
|
| 591 | - $condition |
|
| 592 | - ); |
|
| 593 | - |
|
| 594 | - return $this; |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Creates and adds a join to the query. |
|
| 599 | - * |
|
| 600 | - * <code> |
|
| 601 | - * $qb = $conn->getQueryBuilder() |
|
| 602 | - * ->select('u.name') |
|
| 603 | - * ->from('users', 'u') |
|
| 604 | - * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 605 | - * </code> |
|
| 606 | - * |
|
| 607 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 608 | - * @param string $join The table name to join. |
|
| 609 | - * @param string $alias The alias of the join table. |
|
| 610 | - * @param string $condition The condition for the join. |
|
| 611 | - * |
|
| 612 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 613 | - */ |
|
| 614 | - public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 615 | - $this->queryBuilder->innerJoin( |
|
| 616 | - $this->quoteAlias($fromAlias), |
|
| 617 | - $this->getTableName($join), |
|
| 618 | - $this->quoteAlias($alias), |
|
| 619 | - $condition |
|
| 620 | - ); |
|
| 621 | - |
|
| 622 | - return $this; |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * Creates and adds a left join to the query. |
|
| 627 | - * |
|
| 628 | - * <code> |
|
| 629 | - * $qb = $conn->getQueryBuilder() |
|
| 630 | - * ->select('u.name') |
|
| 631 | - * ->from('users', 'u') |
|
| 632 | - * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 633 | - * </code> |
|
| 634 | - * |
|
| 635 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 636 | - * @param string $join The table name to join. |
|
| 637 | - * @param string $alias The alias of the join table. |
|
| 638 | - * @param string $condition The condition for the join. |
|
| 639 | - * |
|
| 640 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 641 | - */ |
|
| 642 | - public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 643 | - $this->queryBuilder->leftJoin( |
|
| 644 | - $this->quoteAlias($fromAlias), |
|
| 645 | - $this->getTableName($join), |
|
| 646 | - $this->quoteAlias($alias), |
|
| 647 | - $condition |
|
| 648 | - ); |
|
| 649 | - |
|
| 650 | - return $this; |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * Creates and adds a right join to the query. |
|
| 655 | - * |
|
| 656 | - * <code> |
|
| 657 | - * $qb = $conn->getQueryBuilder() |
|
| 658 | - * ->select('u.name') |
|
| 659 | - * ->from('users', 'u') |
|
| 660 | - * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 661 | - * </code> |
|
| 662 | - * |
|
| 663 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 664 | - * @param string $join The table name to join. |
|
| 665 | - * @param string $alias The alias of the join table. |
|
| 666 | - * @param string $condition The condition for the join. |
|
| 667 | - * |
|
| 668 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 669 | - */ |
|
| 670 | - public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 671 | - $this->queryBuilder->rightJoin( |
|
| 672 | - $this->quoteAlias($fromAlias), |
|
| 673 | - $this->getTableName($join), |
|
| 674 | - $this->quoteAlias($alias), |
|
| 675 | - $condition |
|
| 676 | - ); |
|
| 677 | - |
|
| 678 | - return $this; |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - /** |
|
| 682 | - * Sets a new value for a column in a bulk update query. |
|
| 683 | - * |
|
| 684 | - * <code> |
|
| 685 | - * $qb = $conn->getQueryBuilder() |
|
| 686 | - * ->update('users', 'u') |
|
| 687 | - * ->set('u.password', md5('password')) |
|
| 688 | - * ->where('u.id = ?'); |
|
| 689 | - * </code> |
|
| 690 | - * |
|
| 691 | - * @param string $key The column to set. |
|
| 692 | - * @param string $value The value, expression, placeholder, etc. |
|
| 693 | - * |
|
| 694 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 695 | - */ |
|
| 696 | - public function set($key, $value) { |
|
| 697 | - $this->queryBuilder->set( |
|
| 698 | - $this->helper->quoteColumnName($key), |
|
| 699 | - $this->helper->quoteColumnName($value) |
|
| 700 | - ); |
|
| 701 | - |
|
| 702 | - return $this; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * Specifies one or more restrictions to the query result. |
|
| 707 | - * Replaces any previously specified restrictions, if any. |
|
| 708 | - * |
|
| 709 | - * <code> |
|
| 710 | - * $qb = $conn->getQueryBuilder() |
|
| 711 | - * ->select('u.name') |
|
| 712 | - * ->from('users', 'u') |
|
| 713 | - * ->where('u.id = ?'); |
|
| 714 | - * |
|
| 715 | - * // You can optionally programatically build and/or expressions |
|
| 716 | - * $qb = $conn->getQueryBuilder(); |
|
| 717 | - * |
|
| 718 | - * $or = $qb->expr()->orx(); |
|
| 719 | - * $or->add($qb->expr()->eq('u.id', 1)); |
|
| 720 | - * $or->add($qb->expr()->eq('u.id', 2)); |
|
| 721 | - * |
|
| 722 | - * $qb->update('users', 'u') |
|
| 723 | - * ->set('u.password', md5('password')) |
|
| 724 | - * ->where($or); |
|
| 725 | - * </code> |
|
| 726 | - * |
|
| 727 | - * @param mixed $predicates The restriction predicates. |
|
| 728 | - * |
|
| 729 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 730 | - */ |
|
| 731 | - public function where($predicates) { |
|
| 732 | - call_user_func_array( |
|
| 733 | - [$this->queryBuilder, 'where'], |
|
| 734 | - func_get_args() |
|
| 735 | - ); |
|
| 736 | - |
|
| 737 | - return $this; |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 742 | - * conjunction with any previously specified restrictions. |
|
| 743 | - * |
|
| 744 | - * <code> |
|
| 745 | - * $qb = $conn->getQueryBuilder() |
|
| 746 | - * ->select('u') |
|
| 747 | - * ->from('users', 'u') |
|
| 748 | - * ->where('u.username LIKE ?') |
|
| 749 | - * ->andWhere('u.is_active = 1'); |
|
| 750 | - * </code> |
|
| 751 | - * |
|
| 752 | - * @param mixed $where The query restrictions. |
|
| 753 | - * |
|
| 754 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 755 | - * |
|
| 756 | - * @see where() |
|
| 757 | - */ |
|
| 758 | - public function andWhere($where) { |
|
| 759 | - call_user_func_array( |
|
| 760 | - [$this->queryBuilder, 'andWhere'], |
|
| 761 | - func_get_args() |
|
| 762 | - ); |
|
| 763 | - |
|
| 764 | - return $this; |
|
| 765 | - } |
|
| 766 | - |
|
| 767 | - /** |
|
| 768 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 769 | - * disjunction with any previously specified restrictions. |
|
| 770 | - * |
|
| 771 | - * <code> |
|
| 772 | - * $qb = $conn->getQueryBuilder() |
|
| 773 | - * ->select('u.name') |
|
| 774 | - * ->from('users', 'u') |
|
| 775 | - * ->where('u.id = 1') |
|
| 776 | - * ->orWhere('u.id = 2'); |
|
| 777 | - * </code> |
|
| 778 | - * |
|
| 779 | - * @param mixed $where The WHERE statement. |
|
| 780 | - * |
|
| 781 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 782 | - * |
|
| 783 | - * @see where() |
|
| 784 | - */ |
|
| 785 | - public function orWhere($where) { |
|
| 786 | - call_user_func_array( |
|
| 787 | - [$this->queryBuilder, 'orWhere'], |
|
| 788 | - func_get_args() |
|
| 789 | - ); |
|
| 790 | - |
|
| 791 | - return $this; |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - /** |
|
| 795 | - * Specifies a grouping over the results of the query. |
|
| 796 | - * Replaces any previously specified groupings, if any. |
|
| 797 | - * |
|
| 798 | - * <code> |
|
| 799 | - * $qb = $conn->getQueryBuilder() |
|
| 800 | - * ->select('u.name') |
|
| 801 | - * ->from('users', 'u') |
|
| 802 | - * ->groupBy('u.id'); |
|
| 803 | - * </code> |
|
| 804 | - * |
|
| 805 | - * @param mixed $groupBy The grouping expression. |
|
| 806 | - * |
|
| 807 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 808 | - */ |
|
| 809 | - public function groupBy($groupBy) { |
|
| 810 | - $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 811 | - |
|
| 812 | - call_user_func_array( |
|
| 813 | - [$this->queryBuilder, 'groupBy'], |
|
| 814 | - $this->helper->quoteColumnNames($groupBys) |
|
| 815 | - ); |
|
| 816 | - |
|
| 817 | - return $this; |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - /** |
|
| 821 | - * Adds a grouping expression to the query. |
|
| 822 | - * |
|
| 823 | - * <code> |
|
| 824 | - * $qb = $conn->getQueryBuilder() |
|
| 825 | - * ->select('u.name') |
|
| 826 | - * ->from('users', 'u') |
|
| 827 | - * ->groupBy('u.lastLogin'); |
|
| 828 | - * ->addGroupBy('u.createdAt') |
|
| 829 | - * </code> |
|
| 830 | - * |
|
| 831 | - * @param mixed $groupBy The grouping expression. |
|
| 832 | - * |
|
| 833 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 834 | - */ |
|
| 835 | - public function addGroupBy($groupBy) { |
|
| 836 | - $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 837 | - |
|
| 838 | - call_user_func_array( |
|
| 839 | - [$this->queryBuilder, 'addGroupBy'], |
|
| 840 | - $this->helper->quoteColumnNames($groupBys) |
|
| 841 | - ); |
|
| 842 | - |
|
| 843 | - return $this; |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - /** |
|
| 847 | - * Sets a value for a column in an insert query. |
|
| 848 | - * |
|
| 849 | - * <code> |
|
| 850 | - * $qb = $conn->getQueryBuilder() |
|
| 851 | - * ->insert('users') |
|
| 852 | - * ->values( |
|
| 853 | - * array( |
|
| 854 | - * 'name' => '?' |
|
| 855 | - * ) |
|
| 856 | - * ) |
|
| 857 | - * ->setValue('password', '?'); |
|
| 858 | - * </code> |
|
| 859 | - * |
|
| 860 | - * @param string $column The column into which the value should be inserted. |
|
| 861 | - * @param string $value The value that should be inserted into the column. |
|
| 862 | - * |
|
| 863 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 864 | - */ |
|
| 865 | - public function setValue($column, $value) { |
|
| 866 | - $this->queryBuilder->setValue( |
|
| 867 | - $this->helper->quoteColumnName($column), |
|
| 868 | - $value |
|
| 869 | - ); |
|
| 870 | - |
|
| 871 | - return $this; |
|
| 872 | - } |
|
| 873 | - |
|
| 874 | - /** |
|
| 875 | - * Specifies values for an insert query indexed by column names. |
|
| 876 | - * Replaces any previous values, if any. |
|
| 877 | - * |
|
| 878 | - * <code> |
|
| 879 | - * $qb = $conn->getQueryBuilder() |
|
| 880 | - * ->insert('users') |
|
| 881 | - * ->values( |
|
| 882 | - * array( |
|
| 883 | - * 'name' => '?', |
|
| 884 | - * 'password' => '?' |
|
| 885 | - * ) |
|
| 886 | - * ); |
|
| 887 | - * </code> |
|
| 888 | - * |
|
| 889 | - * @param array $values The values to specify for the insert query indexed by column names. |
|
| 890 | - * |
|
| 891 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 892 | - */ |
|
| 893 | - public function values(array $values) { |
|
| 894 | - $quotedValues = []; |
|
| 895 | - foreach ($values as $key => $value) { |
|
| 896 | - $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - $this->queryBuilder->values($quotedValues); |
|
| 900 | - |
|
| 901 | - return $this; |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - /** |
|
| 905 | - * Specifies a restriction over the groups of the query. |
|
| 906 | - * Replaces any previous having restrictions, if any. |
|
| 907 | - * |
|
| 908 | - * @param mixed $having The restriction over the groups. |
|
| 909 | - * |
|
| 910 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 911 | - */ |
|
| 912 | - public function having($having) { |
|
| 913 | - call_user_func_array( |
|
| 914 | - [$this->queryBuilder, 'having'], |
|
| 915 | - func_get_args() |
|
| 916 | - ); |
|
| 917 | - |
|
| 918 | - return $this; |
|
| 919 | - } |
|
| 920 | - |
|
| 921 | - /** |
|
| 922 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 923 | - * conjunction with any existing having restrictions. |
|
| 924 | - * |
|
| 925 | - * @param mixed $having The restriction to append. |
|
| 926 | - * |
|
| 927 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 928 | - */ |
|
| 929 | - public function andHaving($having) { |
|
| 930 | - call_user_func_array( |
|
| 931 | - [$this->queryBuilder, 'andHaving'], |
|
| 932 | - func_get_args() |
|
| 933 | - ); |
|
| 934 | - |
|
| 935 | - return $this; |
|
| 936 | - } |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 940 | - * disjunction with any existing having restrictions. |
|
| 941 | - * |
|
| 942 | - * @param mixed $having The restriction to add. |
|
| 943 | - * |
|
| 944 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 945 | - */ |
|
| 946 | - public function orHaving($having) { |
|
| 947 | - call_user_func_array( |
|
| 948 | - [$this->queryBuilder, 'orHaving'], |
|
| 949 | - func_get_args() |
|
| 950 | - ); |
|
| 951 | - |
|
| 952 | - return $this; |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - /** |
|
| 956 | - * Specifies an ordering for the query results. |
|
| 957 | - * Replaces any previously specified orderings, if any. |
|
| 958 | - * |
|
| 959 | - * @param string $sort The ordering expression. |
|
| 960 | - * @param string $order The ordering direction. |
|
| 961 | - * |
|
| 962 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 963 | - */ |
|
| 964 | - public function orderBy($sort, $order = null) { |
|
| 965 | - $this->queryBuilder->orderBy( |
|
| 966 | - $this->helper->quoteColumnName($sort), |
|
| 967 | - $order |
|
| 968 | - ); |
|
| 969 | - |
|
| 970 | - return $this; |
|
| 971 | - } |
|
| 972 | - |
|
| 973 | - /** |
|
| 974 | - * Adds an ordering to the query results. |
|
| 975 | - * |
|
| 976 | - * @param string $sort The ordering expression. |
|
| 977 | - * @param string $order The ordering direction. |
|
| 978 | - * |
|
| 979 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 980 | - */ |
|
| 981 | - public function addOrderBy($sort, $order = null) { |
|
| 982 | - $this->queryBuilder->addOrderBy( |
|
| 983 | - $this->helper->quoteColumnName($sort), |
|
| 984 | - $order |
|
| 985 | - ); |
|
| 986 | - |
|
| 987 | - return $this; |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * Gets a query part by its name. |
|
| 992 | - * |
|
| 993 | - * @param string $queryPartName |
|
| 994 | - * |
|
| 995 | - * @return mixed |
|
| 996 | - */ |
|
| 997 | - public function getQueryPart($queryPartName) { |
|
| 998 | - return $this->queryBuilder->getQueryPart($queryPartName); |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - /** |
|
| 1002 | - * Gets all query parts. |
|
| 1003 | - * |
|
| 1004 | - * @return array |
|
| 1005 | - */ |
|
| 1006 | - public function getQueryParts() { |
|
| 1007 | - return $this->queryBuilder->getQueryParts(); |
|
| 1008 | - } |
|
| 1009 | - |
|
| 1010 | - /** |
|
| 1011 | - * Resets SQL parts. |
|
| 1012 | - * |
|
| 1013 | - * @param array|null $queryPartNames |
|
| 1014 | - * |
|
| 1015 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1016 | - */ |
|
| 1017 | - public function resetQueryParts($queryPartNames = null) { |
|
| 1018 | - $this->queryBuilder->resetQueryParts($queryPartNames); |
|
| 1019 | - |
|
| 1020 | - return $this; |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - /** |
|
| 1024 | - * Resets a single SQL part. |
|
| 1025 | - * |
|
| 1026 | - * @param string $queryPartName |
|
| 1027 | - * |
|
| 1028 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1029 | - */ |
|
| 1030 | - public function resetQueryPart($queryPartName) { |
|
| 1031 | - $this->queryBuilder->resetQueryPart($queryPartName); |
|
| 1032 | - |
|
| 1033 | - return $this; |
|
| 1034 | - } |
|
| 1035 | - |
|
| 1036 | - /** |
|
| 1037 | - * Creates a new named parameter and bind the value $value to it. |
|
| 1038 | - * |
|
| 1039 | - * This method provides a shortcut for PDOStatement::bindValue |
|
| 1040 | - * when using prepared statements. |
|
| 1041 | - * |
|
| 1042 | - * The parameter $value specifies the value that you want to bind. If |
|
| 1043 | - * $placeholder is not provided bindValue() will automatically create a |
|
| 1044 | - * placeholder for you. An automatic placeholder will be of the name |
|
| 1045 | - * ':dcValue1', ':dcValue2' etc. |
|
| 1046 | - * |
|
| 1047 | - * For more information see {@link http://php.net/pdostatement-bindparam} |
|
| 1048 | - * |
|
| 1049 | - * Example: |
|
| 1050 | - * <code> |
|
| 1051 | - * $value = 2; |
|
| 1052 | - * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 1053 | - * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 1054 | - * </code> |
|
| 1055 | - * |
|
| 1056 | - * @license New BSD License |
|
| 1057 | - * @link http://www.zetacomponents.org |
|
| 1058 | - * |
|
| 1059 | - * @param mixed $value |
|
| 1060 | - * @param mixed $type |
|
| 1061 | - * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 1062 | - * |
|
| 1063 | - * @return IParameter the placeholder name used. |
|
| 1064 | - */ |
|
| 1065 | - public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
| 1066 | - return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
| 1067 | - } |
|
| 1068 | - |
|
| 1069 | - /** |
|
| 1070 | - * Creates a new positional parameter and bind the given value to it. |
|
| 1071 | - * |
|
| 1072 | - * Attention: If you are using positional parameters with the query builder you have |
|
| 1073 | - * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 1074 | - * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 1075 | - * bugs in your code. |
|
| 1076 | - * |
|
| 1077 | - * Example: |
|
| 1078 | - * <code> |
|
| 1079 | - * $qb = $conn->getQueryBuilder(); |
|
| 1080 | - * $qb->select('u.*') |
|
| 1081 | - * ->from('users', 'u') |
|
| 1082 | - * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 1083 | - * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 1084 | - * </code> |
|
| 1085 | - * |
|
| 1086 | - * @param mixed $value |
|
| 1087 | - * @param integer $type |
|
| 1088 | - * |
|
| 1089 | - * @return IParameter |
|
| 1090 | - */ |
|
| 1091 | - public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
| 1092 | - return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - /** |
|
| 1096 | - * Creates a new parameter |
|
| 1097 | - * |
|
| 1098 | - * Example: |
|
| 1099 | - * <code> |
|
| 1100 | - * $qb = $conn->getQueryBuilder(); |
|
| 1101 | - * $qb->select('u.*') |
|
| 1102 | - * ->from('users', 'u') |
|
| 1103 | - * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 1104 | - * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 1105 | - * </code> |
|
| 1106 | - * |
|
| 1107 | - * @param string $name |
|
| 1108 | - * |
|
| 1109 | - * @return IParameter |
|
| 1110 | - */ |
|
| 1111 | - public function createParameter($name) { |
|
| 1112 | - return new Parameter(':' . $name); |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - /** |
|
| 1116 | - * Creates a new function |
|
| 1117 | - * |
|
| 1118 | - * Attention: Column names inside the call have to be quoted before hand |
|
| 1119 | - * |
|
| 1120 | - * Example: |
|
| 1121 | - * <code> |
|
| 1122 | - * $qb = $conn->getQueryBuilder(); |
|
| 1123 | - * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 1124 | - * ->from('users', 'u') |
|
| 1125 | - * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 1126 | - * </code> |
|
| 1127 | - * <code> |
|
| 1128 | - * $qb = $conn->getQueryBuilder(); |
|
| 1129 | - * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 1130 | - * ->from('users', 'u') |
|
| 1131 | - * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 1132 | - * </code> |
|
| 1133 | - * |
|
| 1134 | - * @param string $call |
|
| 1135 | - * |
|
| 1136 | - * @return IQueryFunction |
|
| 1137 | - */ |
|
| 1138 | - public function createFunction($call) { |
|
| 1139 | - return new QueryFunction($call); |
|
| 1140 | - } |
|
| 1141 | - |
|
| 1142 | - /** |
|
| 1143 | - * Used to get the id of the last inserted element |
|
| 1144 | - * @return int |
|
| 1145 | - * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 1146 | - */ |
|
| 1147 | - public function getLastInsertId() { |
|
| 1148 | - if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
| 1149 | - // lastInsertId() needs the prefix but no quotes |
|
| 1150 | - $table = $this->prefixTableName($this->lastInsertedTable); |
|
| 1151 | - return (int) $this->connection->lastInsertId($table); |
|
| 1152 | - } |
|
| 1153 | - |
|
| 1154 | - throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - /** |
|
| 1158 | - * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 1159 | - * |
|
| 1160 | - * @param string $table |
|
| 1161 | - * @return string |
|
| 1162 | - */ |
|
| 1163 | - public function getTableName($table) { |
|
| 1164 | - $table = $this->prefixTableName($table); |
|
| 1165 | - return $this->helper->quoteColumnName($table); |
|
| 1166 | - } |
|
| 1167 | - |
|
| 1168 | - /** |
|
| 1169 | - * Returns the table name with database prefix as needed by the implementation |
|
| 1170 | - * |
|
| 1171 | - * @param string $table |
|
| 1172 | - * @return string |
|
| 1173 | - */ |
|
| 1174 | - protected function prefixTableName($table) { |
|
| 1175 | - if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) { |
|
| 1176 | - return $table; |
|
| 1177 | - } |
|
| 1178 | - |
|
| 1179 | - return '*PREFIX*' . $table; |
|
| 1180 | - } |
|
| 1181 | - |
|
| 1182 | - /** |
|
| 1183 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1184 | - * |
|
| 1185 | - * @param string $column |
|
| 1186 | - * @param string $tableAlias |
|
| 1187 | - * @return string |
|
| 1188 | - */ |
|
| 1189 | - public function getColumnName($column, $tableAlias = '') { |
|
| 1190 | - if ($tableAlias !== '') { |
|
| 1191 | - $tableAlias .= '.'; |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - return $this->helper->quoteColumnName($tableAlias . $column); |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - /** |
|
| 1198 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1199 | - * |
|
| 1200 | - * @param string $alias |
|
| 1201 | - * @return string |
|
| 1202 | - */ |
|
| 1203 | - public function quoteAlias($alias) { |
|
| 1204 | - if ($alias === '' || $alias === null) { |
|
| 1205 | - return $alias; |
|
| 1206 | - } |
|
| 1207 | - |
|
| 1208 | - return $this->helper->quoteColumnName($alias); |
|
| 1209 | - } |
|
| 49 | + /** @var \OCP\IDBConnection */ |
|
| 50 | + private $connection; |
|
| 51 | + |
|
| 52 | + /** @var SystemConfig */ |
|
| 53 | + private $systemConfig; |
|
| 54 | + |
|
| 55 | + /** @var ILogger */ |
|
| 56 | + private $logger; |
|
| 57 | + |
|
| 58 | + /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
| 59 | + private $queryBuilder; |
|
| 60 | + |
|
| 61 | + /** @var QuoteHelper */ |
|
| 62 | + private $helper; |
|
| 63 | + |
|
| 64 | + /** @var bool */ |
|
| 65 | + private $automaticTablePrefix = true; |
|
| 66 | + |
|
| 67 | + /** @var string */ |
|
| 68 | + protected $lastInsertedTable; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Initializes a new QueryBuilder. |
|
| 72 | + * |
|
| 73 | + * @param IDBConnection $connection |
|
| 74 | + * @param SystemConfig $systemConfig |
|
| 75 | + * @param ILogger $logger |
|
| 76 | + */ |
|
| 77 | + public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) { |
|
| 78 | + $this->connection = $connection; |
|
| 79 | + $this->systemConfig = $systemConfig; |
|
| 80 | + $this->logger = $logger; |
|
| 81 | + $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); |
|
| 82 | + $this->helper = new QuoteHelper(); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 87 | + * |
|
| 88 | + * @param bool $enabled If set to true table names will be prefixed with the |
|
| 89 | + * owncloud database prefix automatically. |
|
| 90 | + * @since 8.2.0 |
|
| 91 | + */ |
|
| 92 | + public function automaticTablePrefix($enabled) { |
|
| 93 | + $this->automaticTablePrefix = (bool) $enabled; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 98 | + * This producer method is intended for convenient inline usage. Example: |
|
| 99 | + * |
|
| 100 | + * <code> |
|
| 101 | + * $qb = $conn->getQueryBuilder() |
|
| 102 | + * ->select('u') |
|
| 103 | + * ->from('users', 'u') |
|
| 104 | + * ->where($qb->expr()->eq('u.id', 1)); |
|
| 105 | + * </code> |
|
| 106 | + * |
|
| 107 | + * For more complex expression construction, consider storing the expression |
|
| 108 | + * builder object in a local variable. |
|
| 109 | + * |
|
| 110 | + * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 111 | + */ |
|
| 112 | + public function expr() { |
|
| 113 | + if ($this->connection instanceof OracleConnection) { |
|
| 114 | + return new OCIExpressionBuilder($this->connection); |
|
| 115 | + } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 116 | + return new PgSqlExpressionBuilder($this->connection); |
|
| 117 | + } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 118 | + return new MySqlExpressionBuilder($this->connection); |
|
| 119 | + } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 120 | + return new SqliteExpressionBuilder($this->connection); |
|
| 121 | + } else { |
|
| 122 | + return new ExpressionBuilder($this->connection); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 128 | + * This producer method is intended for convenient inline usage. Example: |
|
| 129 | + * |
|
| 130 | + * <code> |
|
| 131 | + * $qb = $conn->getQueryBuilder() |
|
| 132 | + * ->select('u') |
|
| 133 | + * ->from('users', 'u') |
|
| 134 | + * ->where($qb->fun()->md5('u.id')); |
|
| 135 | + * </code> |
|
| 136 | + * |
|
| 137 | + * For more complex function construction, consider storing the function |
|
| 138 | + * builder object in a local variable. |
|
| 139 | + * |
|
| 140 | + * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 141 | + */ |
|
| 142 | + public function func() { |
|
| 143 | + if ($this->connection instanceof OracleConnection) { |
|
| 144 | + return new OCIFunctionBuilder($this->helper); |
|
| 145 | + } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 146 | + return new SqliteFunctionBuilder($this->helper); |
|
| 147 | + } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 148 | + return new PgSqlFunctionBuilder($this->helper); |
|
| 149 | + } else { |
|
| 150 | + return new FunctionBuilder($this->helper); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Gets the type of the currently built query. |
|
| 156 | + * |
|
| 157 | + * @return integer |
|
| 158 | + */ |
|
| 159 | + public function getType() { |
|
| 160 | + return $this->queryBuilder->getType(); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Gets the associated DBAL Connection for this query builder. |
|
| 165 | + * |
|
| 166 | + * @return \OCP\IDBConnection |
|
| 167 | + */ |
|
| 168 | + public function getConnection() { |
|
| 169 | + return $this->connection; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Gets the state of this query builder instance. |
|
| 174 | + * |
|
| 175 | + * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
| 176 | + */ |
|
| 177 | + public function getState() { |
|
| 178 | + return $this->queryBuilder->getState(); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Executes this query using the bound parameters and their types. |
|
| 183 | + * |
|
| 184 | + * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 185 | + * for insert, update and delete statements. |
|
| 186 | + * |
|
| 187 | + * @return \Doctrine\DBAL\Driver\Statement|int |
|
| 188 | + */ |
|
| 189 | + public function execute() { |
|
| 190 | + if ($this->systemConfig->getValue('log_query', false)) { |
|
| 191 | + $params = []; |
|
| 192 | + foreach ($this->getParameters() as $placeholder => $value) { |
|
| 193 | + if (is_array($value)) { |
|
| 194 | + $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
| 195 | + } else { |
|
| 196 | + $params[] = $placeholder . ' => \'' . $value . '\''; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + if (empty($params)) { |
|
| 200 | + $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
| 201 | + 'query' => $this->getSQL(), |
|
| 202 | + 'app' => 'core', |
|
| 203 | + ]); |
|
| 204 | + } else { |
|
| 205 | + $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
| 206 | + 'query' => $this->getSQL(), |
|
| 207 | + 'params' => implode(', ', $params), |
|
| 208 | + 'app' => 'core', |
|
| 209 | + ]); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return $this->queryBuilder->execute(); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 218 | + * |
|
| 219 | + * <code> |
|
| 220 | + * $qb = $conn->getQueryBuilder() |
|
| 221 | + * ->select('u') |
|
| 222 | + * ->from('User', 'u') |
|
| 223 | + * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 224 | + * </code> |
|
| 225 | + * |
|
| 226 | + * @return string The SQL query string. |
|
| 227 | + */ |
|
| 228 | + public function getSQL() { |
|
| 229 | + return $this->queryBuilder->getSQL(); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Sets a query parameter for the query being constructed. |
|
| 234 | + * |
|
| 235 | + * <code> |
|
| 236 | + * $qb = $conn->getQueryBuilder() |
|
| 237 | + * ->select('u') |
|
| 238 | + * ->from('users', 'u') |
|
| 239 | + * ->where('u.id = :user_id') |
|
| 240 | + * ->setParameter(':user_id', 1); |
|
| 241 | + * </code> |
|
| 242 | + * |
|
| 243 | + * @param string|integer $key The parameter position or name. |
|
| 244 | + * @param mixed $value The parameter value. |
|
| 245 | + * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants. |
|
| 246 | + * |
|
| 247 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 248 | + */ |
|
| 249 | + public function setParameter($key, $value, $type = null) { |
|
| 250 | + $this->queryBuilder->setParameter($key, $value, $type); |
|
| 251 | + |
|
| 252 | + return $this; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Sets a collection of query parameters for the query being constructed. |
|
| 257 | + * |
|
| 258 | + * <code> |
|
| 259 | + * $qb = $conn->getQueryBuilder() |
|
| 260 | + * ->select('u') |
|
| 261 | + * ->from('users', 'u') |
|
| 262 | + * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 263 | + * ->setParameters(array( |
|
| 264 | + * ':user_id1' => 1, |
|
| 265 | + * ':user_id2' => 2 |
|
| 266 | + * )); |
|
| 267 | + * </code> |
|
| 268 | + * |
|
| 269 | + * @param array $params The query parameters to set. |
|
| 270 | + * @param array $types The query parameters types to set. |
|
| 271 | + * |
|
| 272 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 273 | + */ |
|
| 274 | + public function setParameters(array $params, array $types = array()) { |
|
| 275 | + $this->queryBuilder->setParameters($params, $types); |
|
| 276 | + |
|
| 277 | + return $this; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 282 | + * |
|
| 283 | + * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 284 | + */ |
|
| 285 | + public function getParameters() { |
|
| 286 | + return $this->queryBuilder->getParameters(); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Gets a (previously set) query parameter of the query being constructed. |
|
| 291 | + * |
|
| 292 | + * @param mixed $key The key (index or name) of the bound parameter. |
|
| 293 | + * |
|
| 294 | + * @return mixed The value of the bound parameter. |
|
| 295 | + */ |
|
| 296 | + public function getParameter($key) { |
|
| 297 | + return $this->queryBuilder->getParameter($key); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 302 | + * |
|
| 303 | + * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 304 | + */ |
|
| 305 | + public function getParameterTypes() { |
|
| 306 | + return $this->queryBuilder->getParameterTypes(); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Gets a (previously set) query parameter type of the query being constructed. |
|
| 311 | + * |
|
| 312 | + * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 313 | + * |
|
| 314 | + * @return mixed The value of the bound parameter type. |
|
| 315 | + */ |
|
| 316 | + public function getParameterType($key) { |
|
| 317 | + return $this->queryBuilder->getParameterType($key); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Sets the position of the first result to retrieve (the "offset"). |
|
| 322 | + * |
|
| 323 | + * @param integer $firstResult The first result to return. |
|
| 324 | + * |
|
| 325 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 326 | + */ |
|
| 327 | + public function setFirstResult($firstResult) { |
|
| 328 | + $this->queryBuilder->setFirstResult($firstResult); |
|
| 329 | + |
|
| 330 | + return $this; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 335 | + * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 336 | + * |
|
| 337 | + * @return integer The position of the first result. |
|
| 338 | + */ |
|
| 339 | + public function getFirstResult() { |
|
| 340 | + return $this->queryBuilder->getFirstResult(); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Sets the maximum number of results to retrieve (the "limit"). |
|
| 345 | + * |
|
| 346 | + * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
| 347 | + * of the databases will just return an empty result set, Oracle will return |
|
| 348 | + * all entries. |
|
| 349 | + * |
|
| 350 | + * @param integer $maxResults The maximum number of results to retrieve. |
|
| 351 | + * |
|
| 352 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 353 | + */ |
|
| 354 | + public function setMaxResults($maxResults) { |
|
| 355 | + $this->queryBuilder->setMaxResults($maxResults); |
|
| 356 | + |
|
| 357 | + return $this; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 362 | + * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 363 | + * |
|
| 364 | + * @return integer The maximum number of results. |
|
| 365 | + */ |
|
| 366 | + public function getMaxResults() { |
|
| 367 | + return $this->queryBuilder->getMaxResults(); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Specifies an item that is to be returned in the query result. |
|
| 372 | + * Replaces any previously specified selections, if any. |
|
| 373 | + * |
|
| 374 | + * <code> |
|
| 375 | + * $qb = $conn->getQueryBuilder() |
|
| 376 | + * ->select('u.id', 'p.id') |
|
| 377 | + * ->from('users', 'u') |
|
| 378 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 379 | + * </code> |
|
| 380 | + * |
|
| 381 | + * @param mixed $select The selection expressions. |
|
| 382 | + * |
|
| 383 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 384 | + */ |
|
| 385 | + public function select($select = null) { |
|
| 386 | + $selects = is_array($select) ? $select : func_get_args(); |
|
| 387 | + |
|
| 388 | + $this->queryBuilder->select( |
|
| 389 | + $this->helper->quoteColumnNames($selects) |
|
| 390 | + ); |
|
| 391 | + |
|
| 392 | + return $this; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Specifies an item that is to be returned with a different name in the query result. |
|
| 397 | + * |
|
| 398 | + * <code> |
|
| 399 | + * $qb = $conn->getQueryBuilder() |
|
| 400 | + * ->selectAlias('u.id', 'user_id') |
|
| 401 | + * ->from('users', 'u') |
|
| 402 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 403 | + * </code> |
|
| 404 | + * |
|
| 405 | + * @param mixed $select The selection expressions. |
|
| 406 | + * @param string $alias The column alias used in the constructed query. |
|
| 407 | + * |
|
| 408 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 409 | + */ |
|
| 410 | + public function selectAlias($select, $alias) { |
|
| 411 | + |
|
| 412 | + $this->queryBuilder->addSelect( |
|
| 413 | + $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
| 414 | + ); |
|
| 415 | + |
|
| 416 | + return $this; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Specifies an item that is to be returned uniquely in the query result. |
|
| 421 | + * |
|
| 422 | + * <code> |
|
| 423 | + * $qb = $conn->getQueryBuilder() |
|
| 424 | + * ->selectDistinct('type') |
|
| 425 | + * ->from('users'); |
|
| 426 | + * </code> |
|
| 427 | + * |
|
| 428 | + * @param mixed $select The selection expressions. |
|
| 429 | + * |
|
| 430 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 431 | + */ |
|
| 432 | + public function selectDistinct($select) { |
|
| 433 | + |
|
| 434 | + $this->queryBuilder->addSelect( |
|
| 435 | + 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
| 436 | + ); |
|
| 437 | + |
|
| 438 | + return $this; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Adds an item that is to be returned in the query result. |
|
| 443 | + * |
|
| 444 | + * <code> |
|
| 445 | + * $qb = $conn->getQueryBuilder() |
|
| 446 | + * ->select('u.id') |
|
| 447 | + * ->addSelect('p.id') |
|
| 448 | + * ->from('users', 'u') |
|
| 449 | + * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 450 | + * </code> |
|
| 451 | + * |
|
| 452 | + * @param mixed $select The selection expression. |
|
| 453 | + * |
|
| 454 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 455 | + */ |
|
| 456 | + public function addSelect($select = null) { |
|
| 457 | + $selects = is_array($select) ? $select : func_get_args(); |
|
| 458 | + |
|
| 459 | + $this->queryBuilder->addSelect( |
|
| 460 | + $this->helper->quoteColumnNames($selects) |
|
| 461 | + ); |
|
| 462 | + |
|
| 463 | + return $this; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * Turns the query being built into a bulk delete query that ranges over |
|
| 468 | + * a certain table. |
|
| 469 | + * |
|
| 470 | + * <code> |
|
| 471 | + * $qb = $conn->getQueryBuilder() |
|
| 472 | + * ->delete('users', 'u') |
|
| 473 | + * ->where('u.id = :user_id'); |
|
| 474 | + * ->setParameter(':user_id', 1); |
|
| 475 | + * </code> |
|
| 476 | + * |
|
| 477 | + * @param string $delete The table whose rows are subject to the deletion. |
|
| 478 | + * @param string $alias The table alias used in the constructed query. |
|
| 479 | + * |
|
| 480 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 481 | + */ |
|
| 482 | + public function delete($delete = null, $alias = null) { |
|
| 483 | + $this->queryBuilder->delete( |
|
| 484 | + $this->getTableName($delete), |
|
| 485 | + $alias |
|
| 486 | + ); |
|
| 487 | + |
|
| 488 | + return $this; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Turns the query being built into a bulk update query that ranges over |
|
| 493 | + * a certain table |
|
| 494 | + * |
|
| 495 | + * <code> |
|
| 496 | + * $qb = $conn->getQueryBuilder() |
|
| 497 | + * ->update('users', 'u') |
|
| 498 | + * ->set('u.password', md5('password')) |
|
| 499 | + * ->where('u.id = ?'); |
|
| 500 | + * </code> |
|
| 501 | + * |
|
| 502 | + * @param string $update The table whose rows are subject to the update. |
|
| 503 | + * @param string $alias The table alias used in the constructed query. |
|
| 504 | + * |
|
| 505 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 506 | + */ |
|
| 507 | + public function update($update = null, $alias = null) { |
|
| 508 | + $this->queryBuilder->update( |
|
| 509 | + $this->getTableName($update), |
|
| 510 | + $alias |
|
| 511 | + ); |
|
| 512 | + |
|
| 513 | + return $this; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Turns the query being built into an insert query that inserts into |
|
| 518 | + * a certain table |
|
| 519 | + * |
|
| 520 | + * <code> |
|
| 521 | + * $qb = $conn->getQueryBuilder() |
|
| 522 | + * ->insert('users') |
|
| 523 | + * ->values( |
|
| 524 | + * array( |
|
| 525 | + * 'name' => '?', |
|
| 526 | + * 'password' => '?' |
|
| 527 | + * ) |
|
| 528 | + * ); |
|
| 529 | + * </code> |
|
| 530 | + * |
|
| 531 | + * @param string $insert The table into which the rows should be inserted. |
|
| 532 | + * |
|
| 533 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 534 | + */ |
|
| 535 | + public function insert($insert = null) { |
|
| 536 | + $this->queryBuilder->insert( |
|
| 537 | + $this->getTableName($insert) |
|
| 538 | + ); |
|
| 539 | + |
|
| 540 | + $this->lastInsertedTable = $insert; |
|
| 541 | + |
|
| 542 | + return $this; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Creates and adds a query root corresponding to the table identified by the |
|
| 547 | + * given alias, forming a cartesian product with any existing query roots. |
|
| 548 | + * |
|
| 549 | + * <code> |
|
| 550 | + * $qb = $conn->getQueryBuilder() |
|
| 551 | + * ->select('u.id') |
|
| 552 | + * ->from('users', 'u') |
|
| 553 | + * </code> |
|
| 554 | + * |
|
| 555 | + * @param string $from The table. |
|
| 556 | + * @param string|null $alias The alias of the table. |
|
| 557 | + * |
|
| 558 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 559 | + */ |
|
| 560 | + public function from($from, $alias = null) { |
|
| 561 | + $this->queryBuilder->from( |
|
| 562 | + $this->getTableName($from), |
|
| 563 | + $this->quoteAlias($alias) |
|
| 564 | + ); |
|
| 565 | + |
|
| 566 | + return $this; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Creates and adds a join to the query. |
|
| 571 | + * |
|
| 572 | + * <code> |
|
| 573 | + * $qb = $conn->getQueryBuilder() |
|
| 574 | + * ->select('u.name') |
|
| 575 | + * ->from('users', 'u') |
|
| 576 | + * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 577 | + * </code> |
|
| 578 | + * |
|
| 579 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 580 | + * @param string $join The table name to join. |
|
| 581 | + * @param string $alias The alias of the join table. |
|
| 582 | + * @param string $condition The condition for the join. |
|
| 583 | + * |
|
| 584 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 585 | + */ |
|
| 586 | + public function join($fromAlias, $join, $alias, $condition = null) { |
|
| 587 | + $this->queryBuilder->join( |
|
| 588 | + $this->quoteAlias($fromAlias), |
|
| 589 | + $this->getTableName($join), |
|
| 590 | + $this->quoteAlias($alias), |
|
| 591 | + $condition |
|
| 592 | + ); |
|
| 593 | + |
|
| 594 | + return $this; |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Creates and adds a join to the query. |
|
| 599 | + * |
|
| 600 | + * <code> |
|
| 601 | + * $qb = $conn->getQueryBuilder() |
|
| 602 | + * ->select('u.name') |
|
| 603 | + * ->from('users', 'u') |
|
| 604 | + * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 605 | + * </code> |
|
| 606 | + * |
|
| 607 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 608 | + * @param string $join The table name to join. |
|
| 609 | + * @param string $alias The alias of the join table. |
|
| 610 | + * @param string $condition The condition for the join. |
|
| 611 | + * |
|
| 612 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 613 | + */ |
|
| 614 | + public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 615 | + $this->queryBuilder->innerJoin( |
|
| 616 | + $this->quoteAlias($fromAlias), |
|
| 617 | + $this->getTableName($join), |
|
| 618 | + $this->quoteAlias($alias), |
|
| 619 | + $condition |
|
| 620 | + ); |
|
| 621 | + |
|
| 622 | + return $this; |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * Creates and adds a left join to the query. |
|
| 627 | + * |
|
| 628 | + * <code> |
|
| 629 | + * $qb = $conn->getQueryBuilder() |
|
| 630 | + * ->select('u.name') |
|
| 631 | + * ->from('users', 'u') |
|
| 632 | + * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 633 | + * </code> |
|
| 634 | + * |
|
| 635 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 636 | + * @param string $join The table name to join. |
|
| 637 | + * @param string $alias The alias of the join table. |
|
| 638 | + * @param string $condition The condition for the join. |
|
| 639 | + * |
|
| 640 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 641 | + */ |
|
| 642 | + public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 643 | + $this->queryBuilder->leftJoin( |
|
| 644 | + $this->quoteAlias($fromAlias), |
|
| 645 | + $this->getTableName($join), |
|
| 646 | + $this->quoteAlias($alias), |
|
| 647 | + $condition |
|
| 648 | + ); |
|
| 649 | + |
|
| 650 | + return $this; |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * Creates and adds a right join to the query. |
|
| 655 | + * |
|
| 656 | + * <code> |
|
| 657 | + * $qb = $conn->getQueryBuilder() |
|
| 658 | + * ->select('u.name') |
|
| 659 | + * ->from('users', 'u') |
|
| 660 | + * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 661 | + * </code> |
|
| 662 | + * |
|
| 663 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 664 | + * @param string $join The table name to join. |
|
| 665 | + * @param string $alias The alias of the join table. |
|
| 666 | + * @param string $condition The condition for the join. |
|
| 667 | + * |
|
| 668 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 669 | + */ |
|
| 670 | + public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 671 | + $this->queryBuilder->rightJoin( |
|
| 672 | + $this->quoteAlias($fromAlias), |
|
| 673 | + $this->getTableName($join), |
|
| 674 | + $this->quoteAlias($alias), |
|
| 675 | + $condition |
|
| 676 | + ); |
|
| 677 | + |
|
| 678 | + return $this; |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + /** |
|
| 682 | + * Sets a new value for a column in a bulk update query. |
|
| 683 | + * |
|
| 684 | + * <code> |
|
| 685 | + * $qb = $conn->getQueryBuilder() |
|
| 686 | + * ->update('users', 'u') |
|
| 687 | + * ->set('u.password', md5('password')) |
|
| 688 | + * ->where('u.id = ?'); |
|
| 689 | + * </code> |
|
| 690 | + * |
|
| 691 | + * @param string $key The column to set. |
|
| 692 | + * @param string $value The value, expression, placeholder, etc. |
|
| 693 | + * |
|
| 694 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 695 | + */ |
|
| 696 | + public function set($key, $value) { |
|
| 697 | + $this->queryBuilder->set( |
|
| 698 | + $this->helper->quoteColumnName($key), |
|
| 699 | + $this->helper->quoteColumnName($value) |
|
| 700 | + ); |
|
| 701 | + |
|
| 702 | + return $this; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * Specifies one or more restrictions to the query result. |
|
| 707 | + * Replaces any previously specified restrictions, if any. |
|
| 708 | + * |
|
| 709 | + * <code> |
|
| 710 | + * $qb = $conn->getQueryBuilder() |
|
| 711 | + * ->select('u.name') |
|
| 712 | + * ->from('users', 'u') |
|
| 713 | + * ->where('u.id = ?'); |
|
| 714 | + * |
|
| 715 | + * // You can optionally programatically build and/or expressions |
|
| 716 | + * $qb = $conn->getQueryBuilder(); |
|
| 717 | + * |
|
| 718 | + * $or = $qb->expr()->orx(); |
|
| 719 | + * $or->add($qb->expr()->eq('u.id', 1)); |
|
| 720 | + * $or->add($qb->expr()->eq('u.id', 2)); |
|
| 721 | + * |
|
| 722 | + * $qb->update('users', 'u') |
|
| 723 | + * ->set('u.password', md5('password')) |
|
| 724 | + * ->where($or); |
|
| 725 | + * </code> |
|
| 726 | + * |
|
| 727 | + * @param mixed $predicates The restriction predicates. |
|
| 728 | + * |
|
| 729 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 730 | + */ |
|
| 731 | + public function where($predicates) { |
|
| 732 | + call_user_func_array( |
|
| 733 | + [$this->queryBuilder, 'where'], |
|
| 734 | + func_get_args() |
|
| 735 | + ); |
|
| 736 | + |
|
| 737 | + return $this; |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 742 | + * conjunction with any previously specified restrictions. |
|
| 743 | + * |
|
| 744 | + * <code> |
|
| 745 | + * $qb = $conn->getQueryBuilder() |
|
| 746 | + * ->select('u') |
|
| 747 | + * ->from('users', 'u') |
|
| 748 | + * ->where('u.username LIKE ?') |
|
| 749 | + * ->andWhere('u.is_active = 1'); |
|
| 750 | + * </code> |
|
| 751 | + * |
|
| 752 | + * @param mixed $where The query restrictions. |
|
| 753 | + * |
|
| 754 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 755 | + * |
|
| 756 | + * @see where() |
|
| 757 | + */ |
|
| 758 | + public function andWhere($where) { |
|
| 759 | + call_user_func_array( |
|
| 760 | + [$this->queryBuilder, 'andWhere'], |
|
| 761 | + func_get_args() |
|
| 762 | + ); |
|
| 763 | + |
|
| 764 | + return $this; |
|
| 765 | + } |
|
| 766 | + |
|
| 767 | + /** |
|
| 768 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 769 | + * disjunction with any previously specified restrictions. |
|
| 770 | + * |
|
| 771 | + * <code> |
|
| 772 | + * $qb = $conn->getQueryBuilder() |
|
| 773 | + * ->select('u.name') |
|
| 774 | + * ->from('users', 'u') |
|
| 775 | + * ->where('u.id = 1') |
|
| 776 | + * ->orWhere('u.id = 2'); |
|
| 777 | + * </code> |
|
| 778 | + * |
|
| 779 | + * @param mixed $where The WHERE statement. |
|
| 780 | + * |
|
| 781 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 782 | + * |
|
| 783 | + * @see where() |
|
| 784 | + */ |
|
| 785 | + public function orWhere($where) { |
|
| 786 | + call_user_func_array( |
|
| 787 | + [$this->queryBuilder, 'orWhere'], |
|
| 788 | + func_get_args() |
|
| 789 | + ); |
|
| 790 | + |
|
| 791 | + return $this; |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + /** |
|
| 795 | + * Specifies a grouping over the results of the query. |
|
| 796 | + * Replaces any previously specified groupings, if any. |
|
| 797 | + * |
|
| 798 | + * <code> |
|
| 799 | + * $qb = $conn->getQueryBuilder() |
|
| 800 | + * ->select('u.name') |
|
| 801 | + * ->from('users', 'u') |
|
| 802 | + * ->groupBy('u.id'); |
|
| 803 | + * </code> |
|
| 804 | + * |
|
| 805 | + * @param mixed $groupBy The grouping expression. |
|
| 806 | + * |
|
| 807 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 808 | + */ |
|
| 809 | + public function groupBy($groupBy) { |
|
| 810 | + $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 811 | + |
|
| 812 | + call_user_func_array( |
|
| 813 | + [$this->queryBuilder, 'groupBy'], |
|
| 814 | + $this->helper->quoteColumnNames($groupBys) |
|
| 815 | + ); |
|
| 816 | + |
|
| 817 | + return $this; |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + /** |
|
| 821 | + * Adds a grouping expression to the query. |
|
| 822 | + * |
|
| 823 | + * <code> |
|
| 824 | + * $qb = $conn->getQueryBuilder() |
|
| 825 | + * ->select('u.name') |
|
| 826 | + * ->from('users', 'u') |
|
| 827 | + * ->groupBy('u.lastLogin'); |
|
| 828 | + * ->addGroupBy('u.createdAt') |
|
| 829 | + * </code> |
|
| 830 | + * |
|
| 831 | + * @param mixed $groupBy The grouping expression. |
|
| 832 | + * |
|
| 833 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 834 | + */ |
|
| 835 | + public function addGroupBy($groupBy) { |
|
| 836 | + $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 837 | + |
|
| 838 | + call_user_func_array( |
|
| 839 | + [$this->queryBuilder, 'addGroupBy'], |
|
| 840 | + $this->helper->quoteColumnNames($groupBys) |
|
| 841 | + ); |
|
| 842 | + |
|
| 843 | + return $this; |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + /** |
|
| 847 | + * Sets a value for a column in an insert query. |
|
| 848 | + * |
|
| 849 | + * <code> |
|
| 850 | + * $qb = $conn->getQueryBuilder() |
|
| 851 | + * ->insert('users') |
|
| 852 | + * ->values( |
|
| 853 | + * array( |
|
| 854 | + * 'name' => '?' |
|
| 855 | + * ) |
|
| 856 | + * ) |
|
| 857 | + * ->setValue('password', '?'); |
|
| 858 | + * </code> |
|
| 859 | + * |
|
| 860 | + * @param string $column The column into which the value should be inserted. |
|
| 861 | + * @param string $value The value that should be inserted into the column. |
|
| 862 | + * |
|
| 863 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 864 | + */ |
|
| 865 | + public function setValue($column, $value) { |
|
| 866 | + $this->queryBuilder->setValue( |
|
| 867 | + $this->helper->quoteColumnName($column), |
|
| 868 | + $value |
|
| 869 | + ); |
|
| 870 | + |
|
| 871 | + return $this; |
|
| 872 | + } |
|
| 873 | + |
|
| 874 | + /** |
|
| 875 | + * Specifies values for an insert query indexed by column names. |
|
| 876 | + * Replaces any previous values, if any. |
|
| 877 | + * |
|
| 878 | + * <code> |
|
| 879 | + * $qb = $conn->getQueryBuilder() |
|
| 880 | + * ->insert('users') |
|
| 881 | + * ->values( |
|
| 882 | + * array( |
|
| 883 | + * 'name' => '?', |
|
| 884 | + * 'password' => '?' |
|
| 885 | + * ) |
|
| 886 | + * ); |
|
| 887 | + * </code> |
|
| 888 | + * |
|
| 889 | + * @param array $values The values to specify for the insert query indexed by column names. |
|
| 890 | + * |
|
| 891 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 892 | + */ |
|
| 893 | + public function values(array $values) { |
|
| 894 | + $quotedValues = []; |
|
| 895 | + foreach ($values as $key => $value) { |
|
| 896 | + $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + $this->queryBuilder->values($quotedValues); |
|
| 900 | + |
|
| 901 | + return $this; |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + /** |
|
| 905 | + * Specifies a restriction over the groups of the query. |
|
| 906 | + * Replaces any previous having restrictions, if any. |
|
| 907 | + * |
|
| 908 | + * @param mixed $having The restriction over the groups. |
|
| 909 | + * |
|
| 910 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 911 | + */ |
|
| 912 | + public function having($having) { |
|
| 913 | + call_user_func_array( |
|
| 914 | + [$this->queryBuilder, 'having'], |
|
| 915 | + func_get_args() |
|
| 916 | + ); |
|
| 917 | + |
|
| 918 | + return $this; |
|
| 919 | + } |
|
| 920 | + |
|
| 921 | + /** |
|
| 922 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 923 | + * conjunction with any existing having restrictions. |
|
| 924 | + * |
|
| 925 | + * @param mixed $having The restriction to append. |
|
| 926 | + * |
|
| 927 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 928 | + */ |
|
| 929 | + public function andHaving($having) { |
|
| 930 | + call_user_func_array( |
|
| 931 | + [$this->queryBuilder, 'andHaving'], |
|
| 932 | + func_get_args() |
|
| 933 | + ); |
|
| 934 | + |
|
| 935 | + return $this; |
|
| 936 | + } |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 940 | + * disjunction with any existing having restrictions. |
|
| 941 | + * |
|
| 942 | + * @param mixed $having The restriction to add. |
|
| 943 | + * |
|
| 944 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 945 | + */ |
|
| 946 | + public function orHaving($having) { |
|
| 947 | + call_user_func_array( |
|
| 948 | + [$this->queryBuilder, 'orHaving'], |
|
| 949 | + func_get_args() |
|
| 950 | + ); |
|
| 951 | + |
|
| 952 | + return $this; |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + /** |
|
| 956 | + * Specifies an ordering for the query results. |
|
| 957 | + * Replaces any previously specified orderings, if any. |
|
| 958 | + * |
|
| 959 | + * @param string $sort The ordering expression. |
|
| 960 | + * @param string $order The ordering direction. |
|
| 961 | + * |
|
| 962 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 963 | + */ |
|
| 964 | + public function orderBy($sort, $order = null) { |
|
| 965 | + $this->queryBuilder->orderBy( |
|
| 966 | + $this->helper->quoteColumnName($sort), |
|
| 967 | + $order |
|
| 968 | + ); |
|
| 969 | + |
|
| 970 | + return $this; |
|
| 971 | + } |
|
| 972 | + |
|
| 973 | + /** |
|
| 974 | + * Adds an ordering to the query results. |
|
| 975 | + * |
|
| 976 | + * @param string $sort The ordering expression. |
|
| 977 | + * @param string $order The ordering direction. |
|
| 978 | + * |
|
| 979 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 980 | + */ |
|
| 981 | + public function addOrderBy($sort, $order = null) { |
|
| 982 | + $this->queryBuilder->addOrderBy( |
|
| 983 | + $this->helper->quoteColumnName($sort), |
|
| 984 | + $order |
|
| 985 | + ); |
|
| 986 | + |
|
| 987 | + return $this; |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * Gets a query part by its name. |
|
| 992 | + * |
|
| 993 | + * @param string $queryPartName |
|
| 994 | + * |
|
| 995 | + * @return mixed |
|
| 996 | + */ |
|
| 997 | + public function getQueryPart($queryPartName) { |
|
| 998 | + return $this->queryBuilder->getQueryPart($queryPartName); |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + /** |
|
| 1002 | + * Gets all query parts. |
|
| 1003 | + * |
|
| 1004 | + * @return array |
|
| 1005 | + */ |
|
| 1006 | + public function getQueryParts() { |
|
| 1007 | + return $this->queryBuilder->getQueryParts(); |
|
| 1008 | + } |
|
| 1009 | + |
|
| 1010 | + /** |
|
| 1011 | + * Resets SQL parts. |
|
| 1012 | + * |
|
| 1013 | + * @param array|null $queryPartNames |
|
| 1014 | + * |
|
| 1015 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1016 | + */ |
|
| 1017 | + public function resetQueryParts($queryPartNames = null) { |
|
| 1018 | + $this->queryBuilder->resetQueryParts($queryPartNames); |
|
| 1019 | + |
|
| 1020 | + return $this; |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + /** |
|
| 1024 | + * Resets a single SQL part. |
|
| 1025 | + * |
|
| 1026 | + * @param string $queryPartName |
|
| 1027 | + * |
|
| 1028 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1029 | + */ |
|
| 1030 | + public function resetQueryPart($queryPartName) { |
|
| 1031 | + $this->queryBuilder->resetQueryPart($queryPartName); |
|
| 1032 | + |
|
| 1033 | + return $this; |
|
| 1034 | + } |
|
| 1035 | + |
|
| 1036 | + /** |
|
| 1037 | + * Creates a new named parameter and bind the value $value to it. |
|
| 1038 | + * |
|
| 1039 | + * This method provides a shortcut for PDOStatement::bindValue |
|
| 1040 | + * when using prepared statements. |
|
| 1041 | + * |
|
| 1042 | + * The parameter $value specifies the value that you want to bind. If |
|
| 1043 | + * $placeholder is not provided bindValue() will automatically create a |
|
| 1044 | + * placeholder for you. An automatic placeholder will be of the name |
|
| 1045 | + * ':dcValue1', ':dcValue2' etc. |
|
| 1046 | + * |
|
| 1047 | + * For more information see {@link http://php.net/pdostatement-bindparam} |
|
| 1048 | + * |
|
| 1049 | + * Example: |
|
| 1050 | + * <code> |
|
| 1051 | + * $value = 2; |
|
| 1052 | + * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 1053 | + * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 1054 | + * </code> |
|
| 1055 | + * |
|
| 1056 | + * @license New BSD License |
|
| 1057 | + * @link http://www.zetacomponents.org |
|
| 1058 | + * |
|
| 1059 | + * @param mixed $value |
|
| 1060 | + * @param mixed $type |
|
| 1061 | + * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 1062 | + * |
|
| 1063 | + * @return IParameter the placeholder name used. |
|
| 1064 | + */ |
|
| 1065 | + public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
| 1066 | + return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
| 1067 | + } |
|
| 1068 | + |
|
| 1069 | + /** |
|
| 1070 | + * Creates a new positional parameter and bind the given value to it. |
|
| 1071 | + * |
|
| 1072 | + * Attention: If you are using positional parameters with the query builder you have |
|
| 1073 | + * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 1074 | + * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 1075 | + * bugs in your code. |
|
| 1076 | + * |
|
| 1077 | + * Example: |
|
| 1078 | + * <code> |
|
| 1079 | + * $qb = $conn->getQueryBuilder(); |
|
| 1080 | + * $qb->select('u.*') |
|
| 1081 | + * ->from('users', 'u') |
|
| 1082 | + * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 1083 | + * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 1084 | + * </code> |
|
| 1085 | + * |
|
| 1086 | + * @param mixed $value |
|
| 1087 | + * @param integer $type |
|
| 1088 | + * |
|
| 1089 | + * @return IParameter |
|
| 1090 | + */ |
|
| 1091 | + public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
| 1092 | + return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + /** |
|
| 1096 | + * Creates a new parameter |
|
| 1097 | + * |
|
| 1098 | + * Example: |
|
| 1099 | + * <code> |
|
| 1100 | + * $qb = $conn->getQueryBuilder(); |
|
| 1101 | + * $qb->select('u.*') |
|
| 1102 | + * ->from('users', 'u') |
|
| 1103 | + * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 1104 | + * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 1105 | + * </code> |
|
| 1106 | + * |
|
| 1107 | + * @param string $name |
|
| 1108 | + * |
|
| 1109 | + * @return IParameter |
|
| 1110 | + */ |
|
| 1111 | + public function createParameter($name) { |
|
| 1112 | + return new Parameter(':' . $name); |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + /** |
|
| 1116 | + * Creates a new function |
|
| 1117 | + * |
|
| 1118 | + * Attention: Column names inside the call have to be quoted before hand |
|
| 1119 | + * |
|
| 1120 | + * Example: |
|
| 1121 | + * <code> |
|
| 1122 | + * $qb = $conn->getQueryBuilder(); |
|
| 1123 | + * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 1124 | + * ->from('users', 'u') |
|
| 1125 | + * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 1126 | + * </code> |
|
| 1127 | + * <code> |
|
| 1128 | + * $qb = $conn->getQueryBuilder(); |
|
| 1129 | + * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 1130 | + * ->from('users', 'u') |
|
| 1131 | + * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 1132 | + * </code> |
|
| 1133 | + * |
|
| 1134 | + * @param string $call |
|
| 1135 | + * |
|
| 1136 | + * @return IQueryFunction |
|
| 1137 | + */ |
|
| 1138 | + public function createFunction($call) { |
|
| 1139 | + return new QueryFunction($call); |
|
| 1140 | + } |
|
| 1141 | + |
|
| 1142 | + /** |
|
| 1143 | + * Used to get the id of the last inserted element |
|
| 1144 | + * @return int |
|
| 1145 | + * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 1146 | + */ |
|
| 1147 | + public function getLastInsertId() { |
|
| 1148 | + if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
| 1149 | + // lastInsertId() needs the prefix but no quotes |
|
| 1150 | + $table = $this->prefixTableName($this->lastInsertedTable); |
|
| 1151 | + return (int) $this->connection->lastInsertId($table); |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + /** |
|
| 1158 | + * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 1159 | + * |
|
| 1160 | + * @param string $table |
|
| 1161 | + * @return string |
|
| 1162 | + */ |
|
| 1163 | + public function getTableName($table) { |
|
| 1164 | + $table = $this->prefixTableName($table); |
|
| 1165 | + return $this->helper->quoteColumnName($table); |
|
| 1166 | + } |
|
| 1167 | + |
|
| 1168 | + /** |
|
| 1169 | + * Returns the table name with database prefix as needed by the implementation |
|
| 1170 | + * |
|
| 1171 | + * @param string $table |
|
| 1172 | + * @return string |
|
| 1173 | + */ |
|
| 1174 | + protected function prefixTableName($table) { |
|
| 1175 | + if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) { |
|
| 1176 | + return $table; |
|
| 1177 | + } |
|
| 1178 | + |
|
| 1179 | + return '*PREFIX*' . $table; |
|
| 1180 | + } |
|
| 1181 | + |
|
| 1182 | + /** |
|
| 1183 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1184 | + * |
|
| 1185 | + * @param string $column |
|
| 1186 | + * @param string $tableAlias |
|
| 1187 | + * @return string |
|
| 1188 | + */ |
|
| 1189 | + public function getColumnName($column, $tableAlias = '') { |
|
| 1190 | + if ($tableAlias !== '') { |
|
| 1191 | + $tableAlias .= '.'; |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + return $this->helper->quoteColumnName($tableAlias . $column); |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + /** |
|
| 1198 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1199 | + * |
|
| 1200 | + * @param string $alias |
|
| 1201 | + * @return string |
|
| 1202 | + */ |
|
| 1203 | + public function quoteAlias($alias) { |
|
| 1204 | + if ($alias === '' || $alias === null) { |
|
| 1205 | + return $alias; |
|
| 1206 | + } |
|
| 1207 | + |
|
| 1208 | + return $this->helper->quoteColumnName($alias); |
|
| 1209 | + } |
|
| 1210 | 1210 | } |
@@ -27,55 +27,55 @@ |
||
| 27 | 27 | use OCP\DB\QueryBuilder\IQueryFunction; |
| 28 | 28 | |
| 29 | 29 | class QuoteHelper { |
| 30 | - /** |
|
| 31 | - * @param array|string|ILiteral|IParameter|IQueryFunction $strings string, Literal or Parameter |
|
| 32 | - * @return array|string |
|
| 33 | - */ |
|
| 34 | - public function quoteColumnNames($strings) { |
|
| 35 | - if (!is_array($strings)) { |
|
| 36 | - return $this->quoteColumnName($strings); |
|
| 37 | - } |
|
| 30 | + /** |
|
| 31 | + * @param array|string|ILiteral|IParameter|IQueryFunction $strings string, Literal or Parameter |
|
| 32 | + * @return array|string |
|
| 33 | + */ |
|
| 34 | + public function quoteColumnNames($strings) { |
|
| 35 | + if (!is_array($strings)) { |
|
| 36 | + return $this->quoteColumnName($strings); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - $return = []; |
|
| 40 | - foreach ($strings as $string) { |
|
| 41 | - $return[] = $this->quoteColumnName($string); |
|
| 42 | - } |
|
| 39 | + $return = []; |
|
| 40 | + foreach ($strings as $string) { |
|
| 41 | + $return[] = $this->quoteColumnName($string); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - return $return; |
|
| 45 | - } |
|
| 44 | + return $return; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @param string|ILiteral|IParameter|IQueryFunction $string string, Literal or Parameter |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - public function quoteColumnName($string) { |
|
| 52 | - if ($string instanceof IParameter || $string instanceof ILiteral || $string instanceof IQueryFunction) { |
|
| 53 | - return (string) $string; |
|
| 54 | - } |
|
| 47 | + /** |
|
| 48 | + * @param string|ILiteral|IParameter|IQueryFunction $string string, Literal or Parameter |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + public function quoteColumnName($string) { |
|
| 52 | + if ($string instanceof IParameter || $string instanceof ILiteral || $string instanceof IQueryFunction) { |
|
| 53 | + return (string) $string; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - if ($string === null || $string === 'null' || $string === '*') { |
|
| 57 | - return $string; |
|
| 58 | - } |
|
| 56 | + if ($string === null || $string === 'null' || $string === '*') { |
|
| 57 | + return $string; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - if (!is_string($string)) { |
|
| 61 | - throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed'); |
|
| 62 | - } |
|
| 60 | + if (!is_string($string)) { |
|
| 61 | + throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed'); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - $string = str_replace(' AS ', ' as ', $string); |
|
| 65 | - if (substr_count($string, ' as ')) { |
|
| 66 | - return implode(' as ', array_map([$this, 'quoteColumnName'], explode(' as ', $string, 2))); |
|
| 67 | - } |
|
| 64 | + $string = str_replace(' AS ', ' as ', $string); |
|
| 65 | + if (substr_count($string, ' as ')) { |
|
| 66 | + return implode(' as ', array_map([$this, 'quoteColumnName'], explode(' as ', $string, 2))); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - if (substr_count($string, '.')) { |
|
| 70 | - list($alias, $columnName) = explode('.', $string, 2); |
|
| 69 | + if (substr_count($string, '.')) { |
|
| 70 | + list($alias, $columnName) = explode('.', $string, 2); |
|
| 71 | 71 | |
| 72 | - if ($columnName === '*') { |
|
| 73 | - return '`' . $alias . '`.*'; |
|
| 74 | - } |
|
| 72 | + if ($columnName === '*') { |
|
| 73 | + return '`' . $alias . '`.*'; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - return '`' . $alias . '`.`' . $columnName . '`'; |
|
| 77 | - } |
|
| 76 | + return '`' . $alias . '`.`' . $columnName . '`'; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - return '`' . $string . '`'; |
|
| 80 | - } |
|
| 79 | + return '`' . $string . '`'; |
|
| 80 | + } |
|
| 81 | 81 | } |
@@ -70,12 +70,12 @@ |
||
| 70 | 70 | list($alias, $columnName) = explode('.', $string, 2); |
| 71 | 71 | |
| 72 | 72 | if ($columnName === '*') { |
| 73 | - return '`' . $alias . '`.*'; |
|
| 73 | + return '`'.$alias.'`.*'; |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - return '`' . $alias . '`.`' . $columnName . '`'; |
|
| 76 | + return '`'.$alias.'`.`'.$columnName.'`'; |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - return '`' . $string . '`'; |
|
| 79 | + return '`'.$string.'`'; |
|
| 80 | 80 | } |
| 81 | 81 | } |
@@ -42,334 +42,334 @@ |
||
| 42 | 42 | * Cache mounts points per user in the cache so we can easilly look them up |
| 43 | 43 | */ |
| 44 | 44 | class UserMountCache implements IUserMountCache { |
| 45 | - /** |
|
| 46 | - * @var IDBConnection |
|
| 47 | - */ |
|
| 48 | - private $connection; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var IUserManager |
|
| 52 | - */ |
|
| 53 | - private $userManager; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Cached mount info. |
|
| 57 | - * Map of $userId to ICachedMountInfo. |
|
| 58 | - * |
|
| 59 | - * @var ICache |
|
| 60 | - **/ |
|
| 61 | - private $mountsForUsers; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @var ILogger |
|
| 65 | - */ |
|
| 66 | - private $logger; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @var ICache |
|
| 70 | - */ |
|
| 71 | - private $cacheInfoCache; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * UserMountCache constructor. |
|
| 75 | - * |
|
| 76 | - * @param IDBConnection $connection |
|
| 77 | - * @param IUserManager $userManager |
|
| 78 | - * @param ILogger $logger |
|
| 79 | - */ |
|
| 80 | - public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { |
|
| 81 | - $this->connection = $connection; |
|
| 82 | - $this->userManager = $userManager; |
|
| 83 | - $this->logger = $logger; |
|
| 84 | - $this->cacheInfoCache = new CappedMemoryCache(); |
|
| 85 | - $this->mountsForUsers = new CappedMemoryCache(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - public function registerMounts(IUser $user, array $mounts) { |
|
| 89 | - // filter out non-proper storages coming from unit tests |
|
| 90 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 91 | - return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
|
| 92 | - }); |
|
| 93 | - /** @var ICachedMountInfo[] $newMounts */ |
|
| 94 | - $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
|
| 95 | - // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
|
| 96 | - if ($mount->getStorageRootId() === -1) { |
|
| 97 | - return null; |
|
| 98 | - } else { |
|
| 99 | - return new LazyStorageMountInfo($user, $mount); |
|
| 100 | - } |
|
| 101 | - }, $mounts); |
|
| 102 | - $newMounts = array_values(array_filter($newMounts)); |
|
| 103 | - |
|
| 104 | - $cachedMounts = $this->getMountsForUser($user); |
|
| 105 | - $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
| 106 | - // since we are only looking for mounts for a specific user comparing on root id is enough |
|
| 107 | - return $mount1->getRootId() - $mount2->getRootId(); |
|
| 108 | - }; |
|
| 109 | - |
|
| 110 | - /** @var ICachedMountInfo[] $addedMounts */ |
|
| 111 | - $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff); |
|
| 112 | - /** @var ICachedMountInfo[] $removedMounts */ |
|
| 113 | - $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff); |
|
| 114 | - |
|
| 115 | - $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); |
|
| 116 | - |
|
| 117 | - foreach ($addedMounts as $mount) { |
|
| 118 | - $this->addToCache($mount); |
|
| 119 | - $this->mountsForUsers[$user->getUID()][] = $mount; |
|
| 120 | - } |
|
| 121 | - foreach ($removedMounts as $mount) { |
|
| 122 | - $this->removeFromCache($mount); |
|
| 123 | - $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); |
|
| 124 | - unset($this->mountsForUsers[$user->getUID()][$index]); |
|
| 125 | - } |
|
| 126 | - foreach ($changedMounts as $mount) { |
|
| 127 | - $this->updateCachedMount($mount); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param ICachedMountInfo[] $newMounts |
|
| 133 | - * @param ICachedMountInfo[] $cachedMounts |
|
| 134 | - * @return ICachedMountInfo[] |
|
| 135 | - */ |
|
| 136 | - private function findChangedMounts(array $newMounts, array $cachedMounts) { |
|
| 137 | - $changed = []; |
|
| 138 | - foreach ($newMounts as $newMount) { |
|
| 139 | - foreach ($cachedMounts as $cachedMount) { |
|
| 140 | - if ( |
|
| 141 | - $newMount->getRootId() === $cachedMount->getRootId() && |
|
| 142 | - ( |
|
| 143 | - $newMount->getMountPoint() !== $cachedMount->getMountPoint() || |
|
| 144 | - $newMount->getStorageId() !== $cachedMount->getStorageId() || |
|
| 145 | - $newMount->getMountId() !== $cachedMount->getMountId() |
|
| 146 | - ) |
|
| 147 | - ) { |
|
| 148 | - $changed[] = $newMount; |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - return $changed; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - private function addToCache(ICachedMountInfo $mount) { |
|
| 156 | - if ($mount->getStorageId() !== -1) { |
|
| 157 | - $this->connection->insertIfNotExist('*PREFIX*mounts', [ |
|
| 158 | - 'storage_id' => $mount->getStorageId(), |
|
| 159 | - 'root_id' => $mount->getRootId(), |
|
| 160 | - 'user_id' => $mount->getUser()->getUID(), |
|
| 161 | - 'mount_point' => $mount->getMountPoint(), |
|
| 162 | - 'mount_id' => $mount->getMountId() |
|
| 163 | - ], ['root_id', 'user_id']); |
|
| 164 | - } else { |
|
| 165 | - // in some cases this is legitimate, like orphaned shares |
|
| 166 | - $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - private function updateCachedMount(ICachedMountInfo $mount) { |
|
| 171 | - $builder = $this->connection->getQueryBuilder(); |
|
| 172 | - |
|
| 173 | - $query = $builder->update('mounts') |
|
| 174 | - ->set('storage_id', $builder->createNamedParameter($mount->getStorageId())) |
|
| 175 | - ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) |
|
| 176 | - ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT)) |
|
| 177 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
| 178 | - ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
| 179 | - |
|
| 180 | - $query->execute(); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - private function removeFromCache(ICachedMountInfo $mount) { |
|
| 184 | - $builder = $this->connection->getQueryBuilder(); |
|
| 185 | - |
|
| 186 | - $query = $builder->delete('mounts') |
|
| 187 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
| 188 | - ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
| 189 | - $query->execute(); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - private function dbRowToMountInfo(array $row) { |
|
| 193 | - $user = $this->userManager->get($row['user_id']); |
|
| 194 | - if (is_null($user)) { |
|
| 195 | - return null; |
|
| 196 | - } |
|
| 197 | - return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path']) ? $row['path'] : ''); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @param IUser $user |
|
| 202 | - * @return ICachedMountInfo[] |
|
| 203 | - */ |
|
| 204 | - public function getMountsForUser(IUser $user) { |
|
| 205 | - if (!isset($this->mountsForUsers[$user->getUID()])) { |
|
| 206 | - $builder = $this->connection->getQueryBuilder(); |
|
| 207 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
| 208 | - ->from('mounts', 'm') |
|
| 209 | - ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 210 | - ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); |
|
| 211 | - |
|
| 212 | - $rows = $query->execute()->fetchAll(); |
|
| 213 | - |
|
| 214 | - $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 215 | - } |
|
| 216 | - return $this->mountsForUsers[$user->getUID()]; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @param int $numericStorageId |
|
| 221 | - * @param string|null $user limit the results to a single user |
|
| 222 | - * @return CachedMountInfo[] |
|
| 223 | - */ |
|
| 224 | - public function getMountsForStorageId($numericStorageId, $user = null) { |
|
| 225 | - $builder = $this->connection->getQueryBuilder(); |
|
| 226 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
| 227 | - ->from('mounts', 'm') |
|
| 228 | - ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 229 | - ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); |
|
| 230 | - |
|
| 231 | - if ($user) { |
|
| 232 | - $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user))); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $rows = $query->execute()->fetchAll(); |
|
| 236 | - |
|
| 237 | - return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * @param int $rootFileId |
|
| 242 | - * @return CachedMountInfo[] |
|
| 243 | - */ |
|
| 244 | - public function getMountsForRootId($rootFileId) { |
|
| 245 | - $builder = $this->connection->getQueryBuilder(); |
|
| 246 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
| 247 | - ->from('mounts', 'm') |
|
| 248 | - ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 249 | - ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); |
|
| 250 | - |
|
| 251 | - $rows = $query->execute()->fetchAll(); |
|
| 252 | - |
|
| 253 | - return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * @param $fileId |
|
| 258 | - * @return array |
|
| 259 | - * @throws \OCP\Files\NotFoundException |
|
| 260 | - */ |
|
| 261 | - private function getCacheInfoFromFileId($fileId) { |
|
| 262 | - if (!isset($this->cacheInfoCache[$fileId])) { |
|
| 263 | - $builder = $this->connection->getQueryBuilder(); |
|
| 264 | - $query = $builder->select('storage', 'path', 'mimetype') |
|
| 265 | - ->from('filecache') |
|
| 266 | - ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
| 267 | - |
|
| 268 | - $row = $query->execute()->fetch(); |
|
| 269 | - if (is_array($row)) { |
|
| 270 | - $this->cacheInfoCache[$fileId] = [ |
|
| 271 | - (int)$row['storage'], |
|
| 272 | - $row['path'], |
|
| 273 | - (int)$row['mimetype'] |
|
| 274 | - ]; |
|
| 275 | - } else { |
|
| 276 | - throw new NotFoundException('File with id "' . $fileId . '" not found'); |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - return $this->cacheInfoCache[$fileId]; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * @param int $fileId |
|
| 284 | - * @param string|null $user optionally restrict the results to a single user |
|
| 285 | - * @return ICachedMountInfo[] |
|
| 286 | - * @since 9.0.0 |
|
| 287 | - */ |
|
| 288 | - public function getMountsForFileId($fileId, $user = null) { |
|
| 289 | - try { |
|
| 290 | - list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); |
|
| 291 | - } catch (NotFoundException $e) { |
|
| 292 | - return []; |
|
| 293 | - } |
|
| 294 | - $mountsForStorage = $this->getMountsForStorageId($storageId, $user); |
|
| 295 | - |
|
| 296 | - // filter mounts that are from the same storage but a different directory |
|
| 297 | - return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
| 298 | - if ($fileId === $mount->getRootId()) { |
|
| 299 | - return true; |
|
| 300 | - } |
|
| 301 | - $internalMountPath = $mount->getRootInternalPath(); |
|
| 302 | - |
|
| 303 | - return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
|
| 304 | - }); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Remove all cached mounts for a user |
|
| 309 | - * |
|
| 310 | - * @param IUser $user |
|
| 311 | - */ |
|
| 312 | - public function removeUserMounts(IUser $user) { |
|
| 313 | - $builder = $this->connection->getQueryBuilder(); |
|
| 314 | - |
|
| 315 | - $query = $builder->delete('mounts') |
|
| 316 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); |
|
| 317 | - $query->execute(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - public function removeUserStorageMount($storageId, $userId) { |
|
| 321 | - $builder = $this->connection->getQueryBuilder(); |
|
| 322 | - |
|
| 323 | - $query = $builder->delete('mounts') |
|
| 324 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId))) |
|
| 325 | - ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
| 326 | - $query->execute(); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - public function remoteStorageMounts($storageId) { |
|
| 330 | - $builder = $this->connection->getQueryBuilder(); |
|
| 331 | - |
|
| 332 | - $query = $builder->delete('mounts') |
|
| 333 | - ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
| 334 | - $query->execute(); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * @param array $users |
|
| 339 | - * @return array |
|
| 340 | - * @suppress SqlInjectionChecker |
|
| 341 | - */ |
|
| 342 | - public function getUsedSpaceForUsers(array $users) { |
|
| 343 | - $builder = $this->connection->getQueryBuilder(); |
|
| 344 | - |
|
| 345 | - $slash = $builder->createNamedParameter('/'); |
|
| 346 | - |
|
| 347 | - $mountPoint = $builder->func()->concat( |
|
| 348 | - $builder->func()->concat($slash, 'user_id'), |
|
| 349 | - $slash |
|
| 350 | - ); |
|
| 351 | - |
|
| 352 | - $userIds = array_map(function (IUser $user) { |
|
| 353 | - return $user->getUID(); |
|
| 354 | - }, $users); |
|
| 355 | - |
|
| 356 | - $query = $builder->select('m.user_id', 'f.size') |
|
| 357 | - ->from('mounts', 'm') |
|
| 358 | - ->innerJoin('m', 'filecache', 'f', |
|
| 359 | - $builder->expr()->andX( |
|
| 360 | - $builder->expr()->eq('m.storage_id', 'f.storage'), |
|
| 361 | - $builder->expr()->eq('f.path', $builder->createNamedParameter('files')) |
|
| 362 | - )) |
|
| 363 | - ->where($builder->expr()->eq('m.mount_point', $mountPoint)) |
|
| 364 | - ->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 365 | - |
|
| 366 | - $result = $query->execute(); |
|
| 367 | - |
|
| 368 | - $results = []; |
|
| 369 | - while ($row = $result->fetch()) { |
|
| 370 | - $results[$row['user_id']] = $row['size']; |
|
| 371 | - } |
|
| 372 | - $result->closeCursor(); |
|
| 373 | - return $results; |
|
| 374 | - } |
|
| 45 | + /** |
|
| 46 | + * @var IDBConnection |
|
| 47 | + */ |
|
| 48 | + private $connection; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var IUserManager |
|
| 52 | + */ |
|
| 53 | + private $userManager; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Cached mount info. |
|
| 57 | + * Map of $userId to ICachedMountInfo. |
|
| 58 | + * |
|
| 59 | + * @var ICache |
|
| 60 | + **/ |
|
| 61 | + private $mountsForUsers; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @var ILogger |
|
| 65 | + */ |
|
| 66 | + private $logger; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @var ICache |
|
| 70 | + */ |
|
| 71 | + private $cacheInfoCache; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * UserMountCache constructor. |
|
| 75 | + * |
|
| 76 | + * @param IDBConnection $connection |
|
| 77 | + * @param IUserManager $userManager |
|
| 78 | + * @param ILogger $logger |
|
| 79 | + */ |
|
| 80 | + public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { |
|
| 81 | + $this->connection = $connection; |
|
| 82 | + $this->userManager = $userManager; |
|
| 83 | + $this->logger = $logger; |
|
| 84 | + $this->cacheInfoCache = new CappedMemoryCache(); |
|
| 85 | + $this->mountsForUsers = new CappedMemoryCache(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + public function registerMounts(IUser $user, array $mounts) { |
|
| 89 | + // filter out non-proper storages coming from unit tests |
|
| 90 | + $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 91 | + return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
|
| 92 | + }); |
|
| 93 | + /** @var ICachedMountInfo[] $newMounts */ |
|
| 94 | + $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
|
| 95 | + // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
|
| 96 | + if ($mount->getStorageRootId() === -1) { |
|
| 97 | + return null; |
|
| 98 | + } else { |
|
| 99 | + return new LazyStorageMountInfo($user, $mount); |
|
| 100 | + } |
|
| 101 | + }, $mounts); |
|
| 102 | + $newMounts = array_values(array_filter($newMounts)); |
|
| 103 | + |
|
| 104 | + $cachedMounts = $this->getMountsForUser($user); |
|
| 105 | + $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
| 106 | + // since we are only looking for mounts for a specific user comparing on root id is enough |
|
| 107 | + return $mount1->getRootId() - $mount2->getRootId(); |
|
| 108 | + }; |
|
| 109 | + |
|
| 110 | + /** @var ICachedMountInfo[] $addedMounts */ |
|
| 111 | + $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff); |
|
| 112 | + /** @var ICachedMountInfo[] $removedMounts */ |
|
| 113 | + $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff); |
|
| 114 | + |
|
| 115 | + $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); |
|
| 116 | + |
|
| 117 | + foreach ($addedMounts as $mount) { |
|
| 118 | + $this->addToCache($mount); |
|
| 119 | + $this->mountsForUsers[$user->getUID()][] = $mount; |
|
| 120 | + } |
|
| 121 | + foreach ($removedMounts as $mount) { |
|
| 122 | + $this->removeFromCache($mount); |
|
| 123 | + $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); |
|
| 124 | + unset($this->mountsForUsers[$user->getUID()][$index]); |
|
| 125 | + } |
|
| 126 | + foreach ($changedMounts as $mount) { |
|
| 127 | + $this->updateCachedMount($mount); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param ICachedMountInfo[] $newMounts |
|
| 133 | + * @param ICachedMountInfo[] $cachedMounts |
|
| 134 | + * @return ICachedMountInfo[] |
|
| 135 | + */ |
|
| 136 | + private function findChangedMounts(array $newMounts, array $cachedMounts) { |
|
| 137 | + $changed = []; |
|
| 138 | + foreach ($newMounts as $newMount) { |
|
| 139 | + foreach ($cachedMounts as $cachedMount) { |
|
| 140 | + if ( |
|
| 141 | + $newMount->getRootId() === $cachedMount->getRootId() && |
|
| 142 | + ( |
|
| 143 | + $newMount->getMountPoint() !== $cachedMount->getMountPoint() || |
|
| 144 | + $newMount->getStorageId() !== $cachedMount->getStorageId() || |
|
| 145 | + $newMount->getMountId() !== $cachedMount->getMountId() |
|
| 146 | + ) |
|
| 147 | + ) { |
|
| 148 | + $changed[] = $newMount; |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + return $changed; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + private function addToCache(ICachedMountInfo $mount) { |
|
| 156 | + if ($mount->getStorageId() !== -1) { |
|
| 157 | + $this->connection->insertIfNotExist('*PREFIX*mounts', [ |
|
| 158 | + 'storage_id' => $mount->getStorageId(), |
|
| 159 | + 'root_id' => $mount->getRootId(), |
|
| 160 | + 'user_id' => $mount->getUser()->getUID(), |
|
| 161 | + 'mount_point' => $mount->getMountPoint(), |
|
| 162 | + 'mount_id' => $mount->getMountId() |
|
| 163 | + ], ['root_id', 'user_id']); |
|
| 164 | + } else { |
|
| 165 | + // in some cases this is legitimate, like orphaned shares |
|
| 166 | + $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + private function updateCachedMount(ICachedMountInfo $mount) { |
|
| 171 | + $builder = $this->connection->getQueryBuilder(); |
|
| 172 | + |
|
| 173 | + $query = $builder->update('mounts') |
|
| 174 | + ->set('storage_id', $builder->createNamedParameter($mount->getStorageId())) |
|
| 175 | + ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) |
|
| 176 | + ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT)) |
|
| 177 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
| 178 | + ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
| 179 | + |
|
| 180 | + $query->execute(); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + private function removeFromCache(ICachedMountInfo $mount) { |
|
| 184 | + $builder = $this->connection->getQueryBuilder(); |
|
| 185 | + |
|
| 186 | + $query = $builder->delete('mounts') |
|
| 187 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
| 188 | + ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
| 189 | + $query->execute(); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + private function dbRowToMountInfo(array $row) { |
|
| 193 | + $user = $this->userManager->get($row['user_id']); |
|
| 194 | + if (is_null($user)) { |
|
| 195 | + return null; |
|
| 196 | + } |
|
| 197 | + return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path']) ? $row['path'] : ''); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @param IUser $user |
|
| 202 | + * @return ICachedMountInfo[] |
|
| 203 | + */ |
|
| 204 | + public function getMountsForUser(IUser $user) { |
|
| 205 | + if (!isset($this->mountsForUsers[$user->getUID()])) { |
|
| 206 | + $builder = $this->connection->getQueryBuilder(); |
|
| 207 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
| 208 | + ->from('mounts', 'm') |
|
| 209 | + ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 210 | + ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); |
|
| 211 | + |
|
| 212 | + $rows = $query->execute()->fetchAll(); |
|
| 213 | + |
|
| 214 | + $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 215 | + } |
|
| 216 | + return $this->mountsForUsers[$user->getUID()]; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @param int $numericStorageId |
|
| 221 | + * @param string|null $user limit the results to a single user |
|
| 222 | + * @return CachedMountInfo[] |
|
| 223 | + */ |
|
| 224 | + public function getMountsForStorageId($numericStorageId, $user = null) { |
|
| 225 | + $builder = $this->connection->getQueryBuilder(); |
|
| 226 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
| 227 | + ->from('mounts', 'm') |
|
| 228 | + ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 229 | + ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); |
|
| 230 | + |
|
| 231 | + if ($user) { |
|
| 232 | + $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user))); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $rows = $query->execute()->fetchAll(); |
|
| 236 | + |
|
| 237 | + return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * @param int $rootFileId |
|
| 242 | + * @return CachedMountInfo[] |
|
| 243 | + */ |
|
| 244 | + public function getMountsForRootId($rootFileId) { |
|
| 245 | + $builder = $this->connection->getQueryBuilder(); |
|
| 246 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
| 247 | + ->from('mounts', 'm') |
|
| 248 | + ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
| 249 | + ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); |
|
| 250 | + |
|
| 251 | + $rows = $query->execute()->fetchAll(); |
|
| 252 | + |
|
| 253 | + return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * @param $fileId |
|
| 258 | + * @return array |
|
| 259 | + * @throws \OCP\Files\NotFoundException |
|
| 260 | + */ |
|
| 261 | + private function getCacheInfoFromFileId($fileId) { |
|
| 262 | + if (!isset($this->cacheInfoCache[$fileId])) { |
|
| 263 | + $builder = $this->connection->getQueryBuilder(); |
|
| 264 | + $query = $builder->select('storage', 'path', 'mimetype') |
|
| 265 | + ->from('filecache') |
|
| 266 | + ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
| 267 | + |
|
| 268 | + $row = $query->execute()->fetch(); |
|
| 269 | + if (is_array($row)) { |
|
| 270 | + $this->cacheInfoCache[$fileId] = [ |
|
| 271 | + (int)$row['storage'], |
|
| 272 | + $row['path'], |
|
| 273 | + (int)$row['mimetype'] |
|
| 274 | + ]; |
|
| 275 | + } else { |
|
| 276 | + throw new NotFoundException('File with id "' . $fileId . '" not found'); |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + return $this->cacheInfoCache[$fileId]; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * @param int $fileId |
|
| 284 | + * @param string|null $user optionally restrict the results to a single user |
|
| 285 | + * @return ICachedMountInfo[] |
|
| 286 | + * @since 9.0.0 |
|
| 287 | + */ |
|
| 288 | + public function getMountsForFileId($fileId, $user = null) { |
|
| 289 | + try { |
|
| 290 | + list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); |
|
| 291 | + } catch (NotFoundException $e) { |
|
| 292 | + return []; |
|
| 293 | + } |
|
| 294 | + $mountsForStorage = $this->getMountsForStorageId($storageId, $user); |
|
| 295 | + |
|
| 296 | + // filter mounts that are from the same storage but a different directory |
|
| 297 | + return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
| 298 | + if ($fileId === $mount->getRootId()) { |
|
| 299 | + return true; |
|
| 300 | + } |
|
| 301 | + $internalMountPath = $mount->getRootInternalPath(); |
|
| 302 | + |
|
| 303 | + return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
|
| 304 | + }); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Remove all cached mounts for a user |
|
| 309 | + * |
|
| 310 | + * @param IUser $user |
|
| 311 | + */ |
|
| 312 | + public function removeUserMounts(IUser $user) { |
|
| 313 | + $builder = $this->connection->getQueryBuilder(); |
|
| 314 | + |
|
| 315 | + $query = $builder->delete('mounts') |
|
| 316 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); |
|
| 317 | + $query->execute(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + public function removeUserStorageMount($storageId, $userId) { |
|
| 321 | + $builder = $this->connection->getQueryBuilder(); |
|
| 322 | + |
|
| 323 | + $query = $builder->delete('mounts') |
|
| 324 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId))) |
|
| 325 | + ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
| 326 | + $query->execute(); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + public function remoteStorageMounts($storageId) { |
|
| 330 | + $builder = $this->connection->getQueryBuilder(); |
|
| 331 | + |
|
| 332 | + $query = $builder->delete('mounts') |
|
| 333 | + ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
| 334 | + $query->execute(); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * @param array $users |
|
| 339 | + * @return array |
|
| 340 | + * @suppress SqlInjectionChecker |
|
| 341 | + */ |
|
| 342 | + public function getUsedSpaceForUsers(array $users) { |
|
| 343 | + $builder = $this->connection->getQueryBuilder(); |
|
| 344 | + |
|
| 345 | + $slash = $builder->createNamedParameter('/'); |
|
| 346 | + |
|
| 347 | + $mountPoint = $builder->func()->concat( |
|
| 348 | + $builder->func()->concat($slash, 'user_id'), |
|
| 349 | + $slash |
|
| 350 | + ); |
|
| 351 | + |
|
| 352 | + $userIds = array_map(function (IUser $user) { |
|
| 353 | + return $user->getUID(); |
|
| 354 | + }, $users); |
|
| 355 | + |
|
| 356 | + $query = $builder->select('m.user_id', 'f.size') |
|
| 357 | + ->from('mounts', 'm') |
|
| 358 | + ->innerJoin('m', 'filecache', 'f', |
|
| 359 | + $builder->expr()->andX( |
|
| 360 | + $builder->expr()->eq('m.storage_id', 'f.storage'), |
|
| 361 | + $builder->expr()->eq('f.path', $builder->createNamedParameter('files')) |
|
| 362 | + )) |
|
| 363 | + ->where($builder->expr()->eq('m.mount_point', $mountPoint)) |
|
| 364 | + ->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 365 | + |
|
| 366 | + $result = $query->execute(); |
|
| 367 | + |
|
| 368 | + $results = []; |
|
| 369 | + while ($row = $result->fetch()) { |
|
| 370 | + $results[$row['user_id']] = $row['size']; |
|
| 371 | + } |
|
| 372 | + $result->closeCursor(); |
|
| 373 | + return $results; |
|
| 374 | + } |
|
| 375 | 375 | } |
@@ -40,156 +40,156 @@ |
||
| 40 | 40 | |
| 41 | 41 | class Install extends Command { |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @var SystemConfig |
|
| 45 | - */ |
|
| 46 | - private $config; |
|
| 47 | - |
|
| 48 | - public function __construct(SystemConfig $config) { |
|
| 49 | - parent::__construct(); |
|
| 50 | - $this->config = $config; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - protected function configure() { |
|
| 54 | - $this |
|
| 55 | - ->setName('maintenance:install') |
|
| 56 | - ->setDescription('install Nextcloud') |
|
| 57 | - ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite') |
|
| 58 | - ->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database') |
|
| 59 | - ->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost') |
|
| 60 | - ->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on') |
|
| 61 | - ->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database') |
|
| 62 | - ->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null) |
|
| 63 | - ->addOption('database-table-prefix', null, InputOption::VALUE_OPTIONAL, 'Prefix for all tables (default: oc_)', null) |
|
| 64 | - ->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null) |
|
| 65 | - ->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin') |
|
| 66 | - ->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account') |
|
| 67 | - ->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data"); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 71 | - |
|
| 72 | - // validate the environment |
|
| 73 | - $server = \OC::$server; |
|
| 74 | - $setupHelper = new Setup($this->config, $server->getIniWrapper(), |
|
| 75 | - $server->getL10N('lib'), $server->query(Defaults::class), $server->getLogger(), |
|
| 76 | - $server->getSecureRandom()); |
|
| 77 | - $sysInfo = $setupHelper->getSystemInfo(true); |
|
| 78 | - $errors = $sysInfo['errors']; |
|
| 79 | - if (count($errors) > 0) { |
|
| 80 | - $this->printErrors($output, $errors); |
|
| 81 | - |
|
| 82 | - // ignore the OS X setup warning |
|
| 83 | - if(count($errors) !== 1 || |
|
| 84 | - (string)($errors[0]['error']) !== 'Mac OS X is not supported and Nextcloud will not work properly on this platform. Use it at your own risk! ') { |
|
| 85 | - return 1; |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - // validate user input |
|
| 90 | - $options = $this->validateInput($input, $output, array_keys($sysInfo['databases'])); |
|
| 91 | - |
|
| 92 | - // perform installation |
|
| 93 | - $errors = $setupHelper->install($options); |
|
| 94 | - if (count($errors) > 0) { |
|
| 95 | - $this->printErrors($output, $errors); |
|
| 96 | - return 1; |
|
| 97 | - } |
|
| 98 | - $output->writeln("Nextcloud was successfully installed"); |
|
| 99 | - return 0; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param InputInterface $input |
|
| 104 | - * @param OutputInterface $output |
|
| 105 | - * @param string[] $supportedDatabases |
|
| 106 | - * @return array |
|
| 107 | - */ |
|
| 108 | - protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) { |
|
| 109 | - $db = strtolower($input->getOption('database')); |
|
| 110 | - |
|
| 111 | - if (!in_array($db, $supportedDatabases)) { |
|
| 112 | - throw new InvalidArgumentException("Database <$db> is not supported."); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $dbUser = $input->getOption('database-user'); |
|
| 116 | - $dbPass = $input->getOption('database-pass'); |
|
| 117 | - $dbName = $input->getOption('database-name'); |
|
| 118 | - $dbPort = $input->getOption('database-port'); |
|
| 119 | - if ($db === 'oci') { |
|
| 120 | - // an empty hostname needs to be read from the raw parameters |
|
| 121 | - $dbHost = $input->getParameterOption('--database-host', ''); |
|
| 122 | - } else { |
|
| 123 | - $dbHost = $input->getOption('database-host'); |
|
| 124 | - } |
|
| 125 | - $dbTablePrefix = 'oc_'; |
|
| 126 | - if ($input->hasParameterOption('--database-table-prefix')) { |
|
| 127 | - $dbTablePrefix = (string) $input->getOption('database-table-prefix'); |
|
| 128 | - $dbTablePrefix = trim($dbTablePrefix); |
|
| 129 | - } |
|
| 130 | - if ($input->hasParameterOption('--database-pass')) { |
|
| 131 | - $dbPass = (string) $input->getOption('database-pass'); |
|
| 132 | - } |
|
| 133 | - $adminLogin = $input->getOption('admin-user'); |
|
| 134 | - $adminPassword = $input->getOption('admin-pass'); |
|
| 135 | - $dataDir = $input->getOption('data-dir'); |
|
| 136 | - |
|
| 137 | - if ($db !== 'sqlite') { |
|
| 138 | - if (is_null($dbUser)) { |
|
| 139 | - throw new InvalidArgumentException("Database user not provided."); |
|
| 140 | - } |
|
| 141 | - if (is_null($dbName)) { |
|
| 142 | - throw new InvalidArgumentException("Database name not provided."); |
|
| 143 | - } |
|
| 144 | - if (is_null($dbPass)) { |
|
| 145 | - /** @var QuestionHelper $helper */ |
|
| 146 | - $helper = $this->getHelper('question'); |
|
| 147 | - $question = new Question('What is the password to access the database with user <'.$dbUser.'>?'); |
|
| 148 | - $question->setHidden(true); |
|
| 149 | - $question->setHiddenFallback(false); |
|
| 150 | - $dbPass = $helper->ask($input, $output, $question); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - if (is_null($adminPassword)) { |
|
| 155 | - /** @var QuestionHelper $helper */ |
|
| 156 | - $helper = $this->getHelper('question'); |
|
| 157 | - $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?'); |
|
| 158 | - $question->setHidden(true); |
|
| 159 | - $question->setHiddenFallback(false); |
|
| 160 | - $adminPassword = $helper->ask($input, $output, $question); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $options = [ |
|
| 164 | - 'dbtype' => $db, |
|
| 165 | - 'dbuser' => $dbUser, |
|
| 166 | - 'dbpass' => $dbPass, |
|
| 167 | - 'dbname' => $dbName, |
|
| 168 | - 'dbhost' => $dbHost, |
|
| 169 | - 'dbport' => $dbPort, |
|
| 170 | - 'dbtableprefix' => $dbTablePrefix, |
|
| 171 | - 'adminlogin' => $adminLogin, |
|
| 172 | - 'adminpass' => $adminPassword, |
|
| 173 | - 'directory' => $dataDir |
|
| 174 | - ]; |
|
| 175 | - if ($db === 'oci') { |
|
| 176 | - $options['dbtablespace'] = $input->getParameterOption('--database-table-space', ''); |
|
| 177 | - } |
|
| 178 | - return $options; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * @param OutputInterface $output |
|
| 183 | - * @param $errors |
|
| 184 | - */ |
|
| 185 | - protected function printErrors(OutputInterface $output, $errors) { |
|
| 186 | - foreach ($errors as $error) { |
|
| 187 | - if (is_array($error)) { |
|
| 188 | - $output->writeln('<error>' . (string)$error['error'] . '</error>'); |
|
| 189 | - $output->writeln('<info> -> ' . (string)$error['hint'] . '</info>'); |
|
| 190 | - } else { |
|
| 191 | - $output->writeln('<error>' . (string)$error . '</error>'); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - } |
|
| 43 | + /** |
|
| 44 | + * @var SystemConfig |
|
| 45 | + */ |
|
| 46 | + private $config; |
|
| 47 | + |
|
| 48 | + public function __construct(SystemConfig $config) { |
|
| 49 | + parent::__construct(); |
|
| 50 | + $this->config = $config; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + protected function configure() { |
|
| 54 | + $this |
|
| 55 | + ->setName('maintenance:install') |
|
| 56 | + ->setDescription('install Nextcloud') |
|
| 57 | + ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite') |
|
| 58 | + ->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database') |
|
| 59 | + ->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost') |
|
| 60 | + ->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on') |
|
| 61 | + ->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database') |
|
| 62 | + ->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null) |
|
| 63 | + ->addOption('database-table-prefix', null, InputOption::VALUE_OPTIONAL, 'Prefix for all tables (default: oc_)', null) |
|
| 64 | + ->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null) |
|
| 65 | + ->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin') |
|
| 66 | + ->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account') |
|
| 67 | + ->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data"); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 71 | + |
|
| 72 | + // validate the environment |
|
| 73 | + $server = \OC::$server; |
|
| 74 | + $setupHelper = new Setup($this->config, $server->getIniWrapper(), |
|
| 75 | + $server->getL10N('lib'), $server->query(Defaults::class), $server->getLogger(), |
|
| 76 | + $server->getSecureRandom()); |
|
| 77 | + $sysInfo = $setupHelper->getSystemInfo(true); |
|
| 78 | + $errors = $sysInfo['errors']; |
|
| 79 | + if (count($errors) > 0) { |
|
| 80 | + $this->printErrors($output, $errors); |
|
| 81 | + |
|
| 82 | + // ignore the OS X setup warning |
|
| 83 | + if(count($errors) !== 1 || |
|
| 84 | + (string)($errors[0]['error']) !== 'Mac OS X is not supported and Nextcloud will not work properly on this platform. Use it at your own risk! ') { |
|
| 85 | + return 1; |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + // validate user input |
|
| 90 | + $options = $this->validateInput($input, $output, array_keys($sysInfo['databases'])); |
|
| 91 | + |
|
| 92 | + // perform installation |
|
| 93 | + $errors = $setupHelper->install($options); |
|
| 94 | + if (count($errors) > 0) { |
|
| 95 | + $this->printErrors($output, $errors); |
|
| 96 | + return 1; |
|
| 97 | + } |
|
| 98 | + $output->writeln("Nextcloud was successfully installed"); |
|
| 99 | + return 0; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param InputInterface $input |
|
| 104 | + * @param OutputInterface $output |
|
| 105 | + * @param string[] $supportedDatabases |
|
| 106 | + * @return array |
|
| 107 | + */ |
|
| 108 | + protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) { |
|
| 109 | + $db = strtolower($input->getOption('database')); |
|
| 110 | + |
|
| 111 | + if (!in_array($db, $supportedDatabases)) { |
|
| 112 | + throw new InvalidArgumentException("Database <$db> is not supported."); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $dbUser = $input->getOption('database-user'); |
|
| 116 | + $dbPass = $input->getOption('database-pass'); |
|
| 117 | + $dbName = $input->getOption('database-name'); |
|
| 118 | + $dbPort = $input->getOption('database-port'); |
|
| 119 | + if ($db === 'oci') { |
|
| 120 | + // an empty hostname needs to be read from the raw parameters |
|
| 121 | + $dbHost = $input->getParameterOption('--database-host', ''); |
|
| 122 | + } else { |
|
| 123 | + $dbHost = $input->getOption('database-host'); |
|
| 124 | + } |
|
| 125 | + $dbTablePrefix = 'oc_'; |
|
| 126 | + if ($input->hasParameterOption('--database-table-prefix')) { |
|
| 127 | + $dbTablePrefix = (string) $input->getOption('database-table-prefix'); |
|
| 128 | + $dbTablePrefix = trim($dbTablePrefix); |
|
| 129 | + } |
|
| 130 | + if ($input->hasParameterOption('--database-pass')) { |
|
| 131 | + $dbPass = (string) $input->getOption('database-pass'); |
|
| 132 | + } |
|
| 133 | + $adminLogin = $input->getOption('admin-user'); |
|
| 134 | + $adminPassword = $input->getOption('admin-pass'); |
|
| 135 | + $dataDir = $input->getOption('data-dir'); |
|
| 136 | + |
|
| 137 | + if ($db !== 'sqlite') { |
|
| 138 | + if (is_null($dbUser)) { |
|
| 139 | + throw new InvalidArgumentException("Database user not provided."); |
|
| 140 | + } |
|
| 141 | + if (is_null($dbName)) { |
|
| 142 | + throw new InvalidArgumentException("Database name not provided."); |
|
| 143 | + } |
|
| 144 | + if (is_null($dbPass)) { |
|
| 145 | + /** @var QuestionHelper $helper */ |
|
| 146 | + $helper = $this->getHelper('question'); |
|
| 147 | + $question = new Question('What is the password to access the database with user <'.$dbUser.'>?'); |
|
| 148 | + $question->setHidden(true); |
|
| 149 | + $question->setHiddenFallback(false); |
|
| 150 | + $dbPass = $helper->ask($input, $output, $question); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + if (is_null($adminPassword)) { |
|
| 155 | + /** @var QuestionHelper $helper */ |
|
| 156 | + $helper = $this->getHelper('question'); |
|
| 157 | + $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?'); |
|
| 158 | + $question->setHidden(true); |
|
| 159 | + $question->setHiddenFallback(false); |
|
| 160 | + $adminPassword = $helper->ask($input, $output, $question); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $options = [ |
|
| 164 | + 'dbtype' => $db, |
|
| 165 | + 'dbuser' => $dbUser, |
|
| 166 | + 'dbpass' => $dbPass, |
|
| 167 | + 'dbname' => $dbName, |
|
| 168 | + 'dbhost' => $dbHost, |
|
| 169 | + 'dbport' => $dbPort, |
|
| 170 | + 'dbtableprefix' => $dbTablePrefix, |
|
| 171 | + 'adminlogin' => $adminLogin, |
|
| 172 | + 'adminpass' => $adminPassword, |
|
| 173 | + 'directory' => $dataDir |
|
| 174 | + ]; |
|
| 175 | + if ($db === 'oci') { |
|
| 176 | + $options['dbtablespace'] = $input->getParameterOption('--database-table-space', ''); |
|
| 177 | + } |
|
| 178 | + return $options; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @param OutputInterface $output |
|
| 183 | + * @param $errors |
|
| 184 | + */ |
|
| 185 | + protected function printErrors(OutputInterface $output, $errors) { |
|
| 186 | + foreach ($errors as $error) { |
|
| 187 | + if (is_array($error)) { |
|
| 188 | + $output->writeln('<error>' . (string)$error['error'] . '</error>'); |
|
| 189 | + $output->writeln('<info> -> ' . (string)$error['hint'] . '</info>'); |
|
| 190 | + } else { |
|
| 191 | + $output->writeln('<error>' . (string)$error . '</error>'); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | 195 | } |
@@ -30,61 +30,61 @@ |
||
| 30 | 30 | use OCP\Migration\IRepairStep; |
| 31 | 31 | |
| 32 | 32 | class UpdateLanguageCodes implements IRepairStep { |
| 33 | - /** @var IDBConnection */ |
|
| 34 | - private $connection; |
|
| 33 | + /** @var IDBConnection */ |
|
| 34 | + private $connection; |
|
| 35 | 35 | |
| 36 | - /** @var IConfig */ |
|
| 37 | - private $config; |
|
| 36 | + /** @var IConfig */ |
|
| 37 | + private $config; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param IDBConnection $connection |
|
| 41 | - * @param IConfig $config |
|
| 42 | - */ |
|
| 43 | - public function __construct(IDBConnection $connection, |
|
| 44 | - IConfig $config) { |
|
| 45 | - $this->connection = $connection; |
|
| 46 | - $this->config = $config; |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * @param IDBConnection $connection |
|
| 41 | + * @param IConfig $config |
|
| 42 | + */ |
|
| 43 | + public function __construct(IDBConnection $connection, |
|
| 44 | + IConfig $config) { |
|
| 45 | + $this->connection = $connection; |
|
| 46 | + $this->config = $config; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * {@inheritdoc} |
|
| 51 | - */ |
|
| 52 | - public function getName() { |
|
| 53 | - return 'Repair language codes'; |
|
| 54 | - } |
|
| 49 | + /** |
|
| 50 | + * {@inheritdoc} |
|
| 51 | + */ |
|
| 52 | + public function getName() { |
|
| 53 | + return 'Repair language codes'; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * {@inheritdoc} |
|
| 58 | - */ |
|
| 59 | - public function run(IOutput $output) { |
|
| 56 | + /** |
|
| 57 | + * {@inheritdoc} |
|
| 58 | + */ |
|
| 59 | + public function run(IOutput $output) { |
|
| 60 | 60 | |
| 61 | - $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
|
| 61 | + $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
|
| 62 | 62 | |
| 63 | - if (version_compare($versionFromBeforeUpdate, '12.0.0.13', '>')) { |
|
| 64 | - return; |
|
| 65 | - } |
|
| 63 | + if (version_compare($versionFromBeforeUpdate, '12.0.0.13', '>')) { |
|
| 64 | + return; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $languages = [ |
|
| 68 | - 'bg_BG' => 'bg', |
|
| 69 | - 'cs_CZ' => 'cs', |
|
| 70 | - 'fi_FI' => 'fi', |
|
| 71 | - 'hu_HU' => 'hu', |
|
| 72 | - 'nb_NO' => 'nb', |
|
| 73 | - 'sk_SK' => 'sk', |
|
| 74 | - 'th_TH' => 'th', |
|
| 75 | - ]; |
|
| 67 | + $languages = [ |
|
| 68 | + 'bg_BG' => 'bg', |
|
| 69 | + 'cs_CZ' => 'cs', |
|
| 70 | + 'fi_FI' => 'fi', |
|
| 71 | + 'hu_HU' => 'hu', |
|
| 72 | + 'nb_NO' => 'nb', |
|
| 73 | + 'sk_SK' => 'sk', |
|
| 74 | + 'th_TH' => 'th', |
|
| 75 | + ]; |
|
| 76 | 76 | |
| 77 | - foreach ($languages as $oldCode => $newCode) { |
|
| 78 | - $qb = $this->connection->getQueryBuilder(); |
|
| 77 | + foreach ($languages as $oldCode => $newCode) { |
|
| 78 | + $qb = $this->connection->getQueryBuilder(); |
|
| 79 | 79 | |
| 80 | - $affectedRows = $qb->update('preferences') |
|
| 81 | - ->set('configvalue', $qb->createNamedParameter($newCode)) |
|
| 82 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 83 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 84 | - ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($oldCode), IQueryBuilder::PARAM_STR)) |
|
| 85 | - ->execute(); |
|
| 80 | + $affectedRows = $qb->update('preferences') |
|
| 81 | + ->set('configvalue', $qb->createNamedParameter($newCode)) |
|
| 82 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 83 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 84 | + ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($oldCode), IQueryBuilder::PARAM_STR)) |
|
| 85 | + ->execute(); |
|
| 86 | 86 | |
| 87 | - $output->info('Changed ' . $affectedRows . ' setting(s) from "' . $oldCode . '" to "' . $newCode . '" in preferences table.'); |
|
| 88 | - } |
|
| 89 | - } |
|
| 87 | + $output->info('Changed ' . $affectedRows . ' setting(s) from "' . $oldCode . '" to "' . $newCode . '" in preferences table.'); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -84,7 +84,7 @@ |
||
| 84 | 84 | ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($oldCode), IQueryBuilder::PARAM_STR)) |
| 85 | 85 | ->execute(); |
| 86 | 86 | |
| 87 | - $output->info('Changed ' . $affectedRows . ' setting(s) from "' . $oldCode . '" to "' . $newCode . '" in preferences table.'); |
|
| 87 | + $output->info('Changed '.$affectedRows.' setting(s) from "'.$oldCode.'" to "'.$newCode.'" in preferences table.'); |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | } |
@@ -56,493 +56,493 @@ |
||
| 56 | 56 | * @package OC\User |
| 57 | 57 | */ |
| 58 | 58 | class Manager extends PublicEmitter implements IUserManager { |
| 59 | - /** |
|
| 60 | - * @var \OCP\UserInterface[] $backends |
|
| 61 | - */ |
|
| 62 | - private $backends = array(); |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var \OC\User\User[] $cachedUsers |
|
| 66 | - */ |
|
| 67 | - private $cachedUsers = array(); |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var \OCP\IConfig $config |
|
| 71 | - */ |
|
| 72 | - private $config; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param \OCP\IConfig $config |
|
| 76 | - */ |
|
| 77 | - public function __construct(IConfig $config) { |
|
| 78 | - $this->config = $config; |
|
| 79 | - $cachedUsers = &$this->cachedUsers; |
|
| 80 | - $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 81 | - /** @var \OC\User\User $user */ |
|
| 82 | - unset($cachedUsers[$user->getUID()]); |
|
| 83 | - }); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Get the active backends |
|
| 88 | - * @return \OCP\UserInterface[] |
|
| 89 | - */ |
|
| 90 | - public function getBackends() { |
|
| 91 | - return $this->backends; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * register a user backend |
|
| 96 | - * |
|
| 97 | - * @param \OCP\UserInterface $backend |
|
| 98 | - */ |
|
| 99 | - public function registerBackend($backend) { |
|
| 100 | - $this->backends[] = $backend; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * remove a user backend |
|
| 105 | - * |
|
| 106 | - * @param \OCP\UserInterface $backend |
|
| 107 | - */ |
|
| 108 | - public function removeBackend($backend) { |
|
| 109 | - $this->cachedUsers = array(); |
|
| 110 | - if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 111 | - unset($this->backends[$i]); |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * remove all user backends |
|
| 117 | - */ |
|
| 118 | - public function clearBackends() { |
|
| 119 | - $this->cachedUsers = array(); |
|
| 120 | - $this->backends = array(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * get a user by user id |
|
| 125 | - * |
|
| 126 | - * @param string $uid |
|
| 127 | - * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 128 | - */ |
|
| 129 | - public function get($uid) { |
|
| 130 | - if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 131 | - return null; |
|
| 132 | - } |
|
| 133 | - if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 134 | - return $this->cachedUsers[$uid]; |
|
| 135 | - } |
|
| 136 | - foreach ($this->backends as $backend) { |
|
| 137 | - if ($backend->userExists($uid)) { |
|
| 138 | - return $this->getUserObject($uid, $backend); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - return null; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * get or construct the user object |
|
| 146 | - * |
|
| 147 | - * @param string $uid |
|
| 148 | - * @param \OCP\UserInterface $backend |
|
| 149 | - * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 150 | - * @return \OC\User\User |
|
| 151 | - */ |
|
| 152 | - protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 153 | - if (isset($this->cachedUsers[$uid])) { |
|
| 154 | - return $this->cachedUsers[$uid]; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if (method_exists($backend, 'loginName2UserName')) { |
|
| 158 | - $loginName = $backend->loginName2UserName($uid); |
|
| 159 | - if ($loginName !== false) { |
|
| 160 | - $uid = $loginName; |
|
| 161 | - } |
|
| 162 | - if (isset($this->cachedUsers[$uid])) { |
|
| 163 | - return $this->cachedUsers[$uid]; |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $user = new User($uid, $backend, $this, $this->config); |
|
| 168 | - if ($cacheUser) { |
|
| 169 | - $this->cachedUsers[$uid] = $user; |
|
| 170 | - } |
|
| 171 | - return $user; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * check if a user exists |
|
| 176 | - * |
|
| 177 | - * @param string $uid |
|
| 178 | - * @return bool |
|
| 179 | - */ |
|
| 180 | - public function userExists($uid) { |
|
| 181 | - $user = $this->get($uid); |
|
| 182 | - return ($user !== null); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Check if the password is valid for the user |
|
| 187 | - * |
|
| 188 | - * @param string $loginName |
|
| 189 | - * @param string $password |
|
| 190 | - * @return mixed the User object on success, false otherwise |
|
| 191 | - */ |
|
| 192 | - public function checkPassword($loginName, $password) { |
|
| 193 | - $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 194 | - |
|
| 195 | - if ($result === false) { |
|
| 196 | - \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $result; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Check if the password is valid for the user |
|
| 204 | - * |
|
| 205 | - * @internal |
|
| 206 | - * @param string $loginName |
|
| 207 | - * @param string $password |
|
| 208 | - * @return mixed the User object on success, false otherwise |
|
| 209 | - */ |
|
| 210 | - public function checkPasswordNoLogging($loginName, $password) { |
|
| 211 | - $loginName = str_replace("\0", '', $loginName); |
|
| 212 | - $password = str_replace("\0", '', $password); |
|
| 213 | - |
|
| 214 | - foreach ($this->backends as $backend) { |
|
| 215 | - if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 216 | - $uid = $backend->checkPassword($loginName, $password); |
|
| 217 | - if ($uid !== false) { |
|
| 218 | - return $this->getUserObject($uid, $backend); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return false; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * search by user id |
|
| 228 | - * |
|
| 229 | - * @param string $pattern |
|
| 230 | - * @param int $limit |
|
| 231 | - * @param int $offset |
|
| 232 | - * @return \OC\User\User[] |
|
| 233 | - */ |
|
| 234 | - public function search($pattern, $limit = null, $offset = null) { |
|
| 235 | - $users = array(); |
|
| 236 | - foreach ($this->backends as $backend) { |
|
| 237 | - $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 238 | - if (is_array($backendUsers)) { |
|
| 239 | - foreach ($backendUsers as $uid) { |
|
| 240 | - $users[$uid] = $this->getUserObject($uid, $backend); |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - uasort($users, function ($a, $b) { |
|
| 246 | - /** |
|
| 247 | - * @var \OC\User\User $a |
|
| 248 | - * @var \OC\User\User $b |
|
| 249 | - */ |
|
| 250 | - return strcmp($a->getUID(), $b->getUID()); |
|
| 251 | - }); |
|
| 252 | - return $users; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * search by displayName |
|
| 257 | - * |
|
| 258 | - * @param string $pattern |
|
| 259 | - * @param int $limit |
|
| 260 | - * @param int $offset |
|
| 261 | - * @return \OC\User\User[] |
|
| 262 | - */ |
|
| 263 | - public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 264 | - $users = array(); |
|
| 265 | - foreach ($this->backends as $backend) { |
|
| 266 | - $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 267 | - if (is_array($backendUsers)) { |
|
| 268 | - foreach ($backendUsers as $uid => $displayName) { |
|
| 269 | - $users[] = $this->getUserObject($uid, $backend); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - usort($users, function ($a, $b) { |
|
| 275 | - /** |
|
| 276 | - * @var \OC\User\User $a |
|
| 277 | - * @var \OC\User\User $b |
|
| 278 | - */ |
|
| 279 | - return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName())); |
|
| 280 | - }); |
|
| 281 | - return $users; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * @param string $uid |
|
| 286 | - * @param string $password |
|
| 287 | - * @throws \InvalidArgumentException |
|
| 288 | - * @return bool|IUser the created user or false |
|
| 289 | - */ |
|
| 290 | - public function createUser($uid, $password) { |
|
| 291 | - $localBackends = []; |
|
| 292 | - foreach ($this->backends as $backend) { |
|
| 293 | - if ($backend instanceof Database) { |
|
| 294 | - // First check if there is another user backend |
|
| 295 | - $localBackends[] = $backend; |
|
| 296 | - continue; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 300 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - foreach ($localBackends as $backend) { |
|
| 305 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 306 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return false; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * @param string $uid |
|
| 315 | - * @param string $password |
|
| 316 | - * @param UserInterface $backend |
|
| 317 | - * @return IUser|null |
|
| 318 | - * @throws \InvalidArgumentException |
|
| 319 | - */ |
|
| 320 | - public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 321 | - $l = \OC::$server->getL10N('lib'); |
|
| 322 | - |
|
| 323 | - // Check the name for bad characters |
|
| 324 | - // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
| 325 | - if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
| 326 | - throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
| 327 | - . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
| 328 | - } |
|
| 329 | - // No empty username |
|
| 330 | - if (trim($uid) === '') { |
|
| 331 | - throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
| 332 | - } |
|
| 333 | - // No whitespace at the beginning or at the end |
|
| 334 | - if (trim($uid) !== $uid) { |
|
| 335 | - throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
| 336 | - } |
|
| 337 | - // Username only consists of 1 or 2 dots (directory traversal) |
|
| 338 | - if ($uid === '.' || $uid === '..') { |
|
| 339 | - throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
| 340 | - } |
|
| 341 | - // No empty password |
|
| 342 | - if (trim($password) === '') { |
|
| 343 | - throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - // Check if user already exists |
|
| 347 | - if ($this->userExists($uid)) { |
|
| 348 | - throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 352 | - $backend->createUser($uid, $password); |
|
| 353 | - $user = $this->getUserObject($uid, $backend); |
|
| 354 | - if ($user instanceof IUser) { |
|
| 355 | - $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 356 | - } |
|
| 357 | - return $user; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * returns how many users per backend exist (if supported by backend) |
|
| 362 | - * |
|
| 363 | - * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 364 | - * entry in the preferences table will be affected |
|
| 365 | - * @return array|int an array of backend class as key and count number as value |
|
| 366 | - * if $hasLoggedIn is true only an int is returned |
|
| 367 | - */ |
|
| 368 | - public function countUsers($hasLoggedIn = false) { |
|
| 369 | - if ($hasLoggedIn) { |
|
| 370 | - return $this->countSeenUsers(); |
|
| 371 | - } |
|
| 372 | - $userCountStatistics = []; |
|
| 373 | - foreach ($this->backends as $backend) { |
|
| 374 | - if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 375 | - $backendUsers = $backend->countUsers(); |
|
| 376 | - if($backendUsers !== false) { |
|
| 377 | - if($backend instanceof IUserBackend) { |
|
| 378 | - $name = $backend->getBackendName(); |
|
| 379 | - } else { |
|
| 380 | - $name = get_class($backend); |
|
| 381 | - } |
|
| 382 | - if(isset($userCountStatistics[$name])) { |
|
| 383 | - $userCountStatistics[$name] += $backendUsers; |
|
| 384 | - } else { |
|
| 385 | - $userCountStatistics[$name] = $backendUsers; |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - } |
|
| 390 | - return $userCountStatistics; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * The callback is executed for each user on each backend. |
|
| 395 | - * If the callback returns false no further users will be retrieved. |
|
| 396 | - * |
|
| 397 | - * @param \Closure $callback |
|
| 398 | - * @param string $search |
|
| 399 | - * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 400 | - * in the preferences table will be affected |
|
| 401 | - * @since 9.0.0 |
|
| 402 | - */ |
|
| 403 | - public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 404 | - if ($onlySeen) { |
|
| 405 | - $this->callForSeenUsers($callback); |
|
| 406 | - } else { |
|
| 407 | - foreach ($this->getBackends() as $backend) { |
|
| 408 | - $limit = 500; |
|
| 409 | - $offset = 0; |
|
| 410 | - do { |
|
| 411 | - $users = $backend->getUsers($search, $limit, $offset); |
|
| 412 | - foreach ($users as $uid) { |
|
| 413 | - if (!$backend->userExists($uid)) { |
|
| 414 | - continue; |
|
| 415 | - } |
|
| 416 | - $user = $this->getUserObject($uid, $backend, false); |
|
| 417 | - $return = $callback($user); |
|
| 418 | - if ($return === false) { |
|
| 419 | - break; |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - $offset += $limit; |
|
| 423 | - } while (count($users) >= $limit); |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * returns how many users have logged in once |
|
| 430 | - * |
|
| 431 | - * @return int |
|
| 432 | - * @since 12.0.0 |
|
| 433 | - */ |
|
| 434 | - public function countDisabledUsers() { |
|
| 435 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 436 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 437 | - ->from('preferences') |
|
| 438 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 439 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 440 | - ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 441 | - |
|
| 442 | - $query = $queryBuilder->execute(); |
|
| 443 | - |
|
| 444 | - $result = (int)$query->fetchColumn(); |
|
| 445 | - $query->closeCursor(); |
|
| 446 | - |
|
| 447 | - return $result; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * returns how many users have logged in once |
|
| 452 | - * |
|
| 453 | - * @return int |
|
| 454 | - * @since 11.0.0 |
|
| 455 | - */ |
|
| 456 | - public function countSeenUsers() { |
|
| 457 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 458 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 459 | - ->from('preferences') |
|
| 460 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 461 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
| 462 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
| 463 | - |
|
| 464 | - $query = $queryBuilder->execute(); |
|
| 465 | - |
|
| 466 | - $result = (int)$query->fetchColumn(); |
|
| 467 | - $query->closeCursor(); |
|
| 468 | - |
|
| 469 | - return $result; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * @param \Closure $callback |
|
| 474 | - * @since 11.0.0 |
|
| 475 | - */ |
|
| 476 | - public function callForSeenUsers(\Closure $callback) { |
|
| 477 | - $limit = 1000; |
|
| 478 | - $offset = 0; |
|
| 479 | - do { |
|
| 480 | - $userIds = $this->getSeenUserIds($limit, $offset); |
|
| 481 | - $offset += $limit; |
|
| 482 | - foreach ($userIds as $userId) { |
|
| 483 | - foreach ($this->backends as $backend) { |
|
| 484 | - if ($backend->userExists($userId)) { |
|
| 485 | - $user = $this->getUserObject($userId, $backend, false); |
|
| 486 | - $return = $callback($user); |
|
| 487 | - if ($return === false) { |
|
| 488 | - return; |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - } |
|
| 492 | - } |
|
| 493 | - } while (count($userIds) >= $limit); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * Getting all userIds that have a listLogin value requires checking the |
|
| 498 | - * value in php because on oracle you cannot use a clob in a where clause, |
|
| 499 | - * preventing us from doing a not null or length(value) > 0 check. |
|
| 500 | - * |
|
| 501 | - * @param int $limit |
|
| 502 | - * @param int $offset |
|
| 503 | - * @return string[] with user ids |
|
| 504 | - */ |
|
| 505 | - private function getSeenUserIds($limit = null, $offset = null) { |
|
| 506 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 507 | - $queryBuilder->select(['userid']) |
|
| 508 | - ->from('preferences') |
|
| 509 | - ->where($queryBuilder->expr()->eq( |
|
| 510 | - 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 511 | - ) |
|
| 512 | - ->andWhere($queryBuilder->expr()->eq( |
|
| 513 | - 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 514 | - ) |
|
| 515 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 516 | - ); |
|
| 517 | - |
|
| 518 | - if ($limit !== null) { |
|
| 519 | - $queryBuilder->setMaxResults($limit); |
|
| 520 | - } |
|
| 521 | - if ($offset !== null) { |
|
| 522 | - $queryBuilder->setFirstResult($offset); |
|
| 523 | - } |
|
| 524 | - $query = $queryBuilder->execute(); |
|
| 525 | - $result = []; |
|
| 526 | - |
|
| 527 | - while ($row = $query->fetch()) { |
|
| 528 | - $result[] = $row['userid']; |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - $query->closeCursor(); |
|
| 532 | - |
|
| 533 | - return $result; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * @param string $email |
|
| 538 | - * @return IUser[] |
|
| 539 | - * @since 9.1.0 |
|
| 540 | - */ |
|
| 541 | - public function getByEmail($email) { |
|
| 542 | - $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
| 543 | - |
|
| 544 | - return array_map(function($uid) { |
|
| 545 | - return $this->get($uid); |
|
| 546 | - }, $userIds); |
|
| 547 | - } |
|
| 59 | + /** |
|
| 60 | + * @var \OCP\UserInterface[] $backends |
|
| 61 | + */ |
|
| 62 | + private $backends = array(); |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var \OC\User\User[] $cachedUsers |
|
| 66 | + */ |
|
| 67 | + private $cachedUsers = array(); |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var \OCP\IConfig $config |
|
| 71 | + */ |
|
| 72 | + private $config; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param \OCP\IConfig $config |
|
| 76 | + */ |
|
| 77 | + public function __construct(IConfig $config) { |
|
| 78 | + $this->config = $config; |
|
| 79 | + $cachedUsers = &$this->cachedUsers; |
|
| 80 | + $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 81 | + /** @var \OC\User\User $user */ |
|
| 82 | + unset($cachedUsers[$user->getUID()]); |
|
| 83 | + }); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Get the active backends |
|
| 88 | + * @return \OCP\UserInterface[] |
|
| 89 | + */ |
|
| 90 | + public function getBackends() { |
|
| 91 | + return $this->backends; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * register a user backend |
|
| 96 | + * |
|
| 97 | + * @param \OCP\UserInterface $backend |
|
| 98 | + */ |
|
| 99 | + public function registerBackend($backend) { |
|
| 100 | + $this->backends[] = $backend; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * remove a user backend |
|
| 105 | + * |
|
| 106 | + * @param \OCP\UserInterface $backend |
|
| 107 | + */ |
|
| 108 | + public function removeBackend($backend) { |
|
| 109 | + $this->cachedUsers = array(); |
|
| 110 | + if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 111 | + unset($this->backends[$i]); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * remove all user backends |
|
| 117 | + */ |
|
| 118 | + public function clearBackends() { |
|
| 119 | + $this->cachedUsers = array(); |
|
| 120 | + $this->backends = array(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * get a user by user id |
|
| 125 | + * |
|
| 126 | + * @param string $uid |
|
| 127 | + * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 128 | + */ |
|
| 129 | + public function get($uid) { |
|
| 130 | + if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 131 | + return null; |
|
| 132 | + } |
|
| 133 | + if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 134 | + return $this->cachedUsers[$uid]; |
|
| 135 | + } |
|
| 136 | + foreach ($this->backends as $backend) { |
|
| 137 | + if ($backend->userExists($uid)) { |
|
| 138 | + return $this->getUserObject($uid, $backend); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + return null; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * get or construct the user object |
|
| 146 | + * |
|
| 147 | + * @param string $uid |
|
| 148 | + * @param \OCP\UserInterface $backend |
|
| 149 | + * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 150 | + * @return \OC\User\User |
|
| 151 | + */ |
|
| 152 | + protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 153 | + if (isset($this->cachedUsers[$uid])) { |
|
| 154 | + return $this->cachedUsers[$uid]; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if (method_exists($backend, 'loginName2UserName')) { |
|
| 158 | + $loginName = $backend->loginName2UserName($uid); |
|
| 159 | + if ($loginName !== false) { |
|
| 160 | + $uid = $loginName; |
|
| 161 | + } |
|
| 162 | + if (isset($this->cachedUsers[$uid])) { |
|
| 163 | + return $this->cachedUsers[$uid]; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $user = new User($uid, $backend, $this, $this->config); |
|
| 168 | + if ($cacheUser) { |
|
| 169 | + $this->cachedUsers[$uid] = $user; |
|
| 170 | + } |
|
| 171 | + return $user; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * check if a user exists |
|
| 176 | + * |
|
| 177 | + * @param string $uid |
|
| 178 | + * @return bool |
|
| 179 | + */ |
|
| 180 | + public function userExists($uid) { |
|
| 181 | + $user = $this->get($uid); |
|
| 182 | + return ($user !== null); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Check if the password is valid for the user |
|
| 187 | + * |
|
| 188 | + * @param string $loginName |
|
| 189 | + * @param string $password |
|
| 190 | + * @return mixed the User object on success, false otherwise |
|
| 191 | + */ |
|
| 192 | + public function checkPassword($loginName, $password) { |
|
| 193 | + $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 194 | + |
|
| 195 | + if ($result === false) { |
|
| 196 | + \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $result; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Check if the password is valid for the user |
|
| 204 | + * |
|
| 205 | + * @internal |
|
| 206 | + * @param string $loginName |
|
| 207 | + * @param string $password |
|
| 208 | + * @return mixed the User object on success, false otherwise |
|
| 209 | + */ |
|
| 210 | + public function checkPasswordNoLogging($loginName, $password) { |
|
| 211 | + $loginName = str_replace("\0", '', $loginName); |
|
| 212 | + $password = str_replace("\0", '', $password); |
|
| 213 | + |
|
| 214 | + foreach ($this->backends as $backend) { |
|
| 215 | + if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 216 | + $uid = $backend->checkPassword($loginName, $password); |
|
| 217 | + if ($uid !== false) { |
|
| 218 | + return $this->getUserObject($uid, $backend); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return false; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * search by user id |
|
| 228 | + * |
|
| 229 | + * @param string $pattern |
|
| 230 | + * @param int $limit |
|
| 231 | + * @param int $offset |
|
| 232 | + * @return \OC\User\User[] |
|
| 233 | + */ |
|
| 234 | + public function search($pattern, $limit = null, $offset = null) { |
|
| 235 | + $users = array(); |
|
| 236 | + foreach ($this->backends as $backend) { |
|
| 237 | + $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 238 | + if (is_array($backendUsers)) { |
|
| 239 | + foreach ($backendUsers as $uid) { |
|
| 240 | + $users[$uid] = $this->getUserObject($uid, $backend); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + uasort($users, function ($a, $b) { |
|
| 246 | + /** |
|
| 247 | + * @var \OC\User\User $a |
|
| 248 | + * @var \OC\User\User $b |
|
| 249 | + */ |
|
| 250 | + return strcmp($a->getUID(), $b->getUID()); |
|
| 251 | + }); |
|
| 252 | + return $users; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * search by displayName |
|
| 257 | + * |
|
| 258 | + * @param string $pattern |
|
| 259 | + * @param int $limit |
|
| 260 | + * @param int $offset |
|
| 261 | + * @return \OC\User\User[] |
|
| 262 | + */ |
|
| 263 | + public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 264 | + $users = array(); |
|
| 265 | + foreach ($this->backends as $backend) { |
|
| 266 | + $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 267 | + if (is_array($backendUsers)) { |
|
| 268 | + foreach ($backendUsers as $uid => $displayName) { |
|
| 269 | + $users[] = $this->getUserObject($uid, $backend); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + usort($users, function ($a, $b) { |
|
| 275 | + /** |
|
| 276 | + * @var \OC\User\User $a |
|
| 277 | + * @var \OC\User\User $b |
|
| 278 | + */ |
|
| 279 | + return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName())); |
|
| 280 | + }); |
|
| 281 | + return $users; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * @param string $uid |
|
| 286 | + * @param string $password |
|
| 287 | + * @throws \InvalidArgumentException |
|
| 288 | + * @return bool|IUser the created user or false |
|
| 289 | + */ |
|
| 290 | + public function createUser($uid, $password) { |
|
| 291 | + $localBackends = []; |
|
| 292 | + foreach ($this->backends as $backend) { |
|
| 293 | + if ($backend instanceof Database) { |
|
| 294 | + // First check if there is another user backend |
|
| 295 | + $localBackends[] = $backend; |
|
| 296 | + continue; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 300 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + foreach ($localBackends as $backend) { |
|
| 305 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 306 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return false; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * @param string $uid |
|
| 315 | + * @param string $password |
|
| 316 | + * @param UserInterface $backend |
|
| 317 | + * @return IUser|null |
|
| 318 | + * @throws \InvalidArgumentException |
|
| 319 | + */ |
|
| 320 | + public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 321 | + $l = \OC::$server->getL10N('lib'); |
|
| 322 | + |
|
| 323 | + // Check the name for bad characters |
|
| 324 | + // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
| 325 | + if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
| 326 | + throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
| 327 | + . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
| 328 | + } |
|
| 329 | + // No empty username |
|
| 330 | + if (trim($uid) === '') { |
|
| 331 | + throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
| 332 | + } |
|
| 333 | + // No whitespace at the beginning or at the end |
|
| 334 | + if (trim($uid) !== $uid) { |
|
| 335 | + throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
| 336 | + } |
|
| 337 | + // Username only consists of 1 or 2 dots (directory traversal) |
|
| 338 | + if ($uid === '.' || $uid === '..') { |
|
| 339 | + throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
| 340 | + } |
|
| 341 | + // No empty password |
|
| 342 | + if (trim($password) === '') { |
|
| 343 | + throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + // Check if user already exists |
|
| 347 | + if ($this->userExists($uid)) { |
|
| 348 | + throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 352 | + $backend->createUser($uid, $password); |
|
| 353 | + $user = $this->getUserObject($uid, $backend); |
|
| 354 | + if ($user instanceof IUser) { |
|
| 355 | + $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 356 | + } |
|
| 357 | + return $user; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * returns how many users per backend exist (if supported by backend) |
|
| 362 | + * |
|
| 363 | + * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 364 | + * entry in the preferences table will be affected |
|
| 365 | + * @return array|int an array of backend class as key and count number as value |
|
| 366 | + * if $hasLoggedIn is true only an int is returned |
|
| 367 | + */ |
|
| 368 | + public function countUsers($hasLoggedIn = false) { |
|
| 369 | + if ($hasLoggedIn) { |
|
| 370 | + return $this->countSeenUsers(); |
|
| 371 | + } |
|
| 372 | + $userCountStatistics = []; |
|
| 373 | + foreach ($this->backends as $backend) { |
|
| 374 | + if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 375 | + $backendUsers = $backend->countUsers(); |
|
| 376 | + if($backendUsers !== false) { |
|
| 377 | + if($backend instanceof IUserBackend) { |
|
| 378 | + $name = $backend->getBackendName(); |
|
| 379 | + } else { |
|
| 380 | + $name = get_class($backend); |
|
| 381 | + } |
|
| 382 | + if(isset($userCountStatistics[$name])) { |
|
| 383 | + $userCountStatistics[$name] += $backendUsers; |
|
| 384 | + } else { |
|
| 385 | + $userCountStatistics[$name] = $backendUsers; |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + } |
|
| 390 | + return $userCountStatistics; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * The callback is executed for each user on each backend. |
|
| 395 | + * If the callback returns false no further users will be retrieved. |
|
| 396 | + * |
|
| 397 | + * @param \Closure $callback |
|
| 398 | + * @param string $search |
|
| 399 | + * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 400 | + * in the preferences table will be affected |
|
| 401 | + * @since 9.0.0 |
|
| 402 | + */ |
|
| 403 | + public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 404 | + if ($onlySeen) { |
|
| 405 | + $this->callForSeenUsers($callback); |
|
| 406 | + } else { |
|
| 407 | + foreach ($this->getBackends() as $backend) { |
|
| 408 | + $limit = 500; |
|
| 409 | + $offset = 0; |
|
| 410 | + do { |
|
| 411 | + $users = $backend->getUsers($search, $limit, $offset); |
|
| 412 | + foreach ($users as $uid) { |
|
| 413 | + if (!$backend->userExists($uid)) { |
|
| 414 | + continue; |
|
| 415 | + } |
|
| 416 | + $user = $this->getUserObject($uid, $backend, false); |
|
| 417 | + $return = $callback($user); |
|
| 418 | + if ($return === false) { |
|
| 419 | + break; |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + $offset += $limit; |
|
| 423 | + } while (count($users) >= $limit); |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * returns how many users have logged in once |
|
| 430 | + * |
|
| 431 | + * @return int |
|
| 432 | + * @since 12.0.0 |
|
| 433 | + */ |
|
| 434 | + public function countDisabledUsers() { |
|
| 435 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 436 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 437 | + ->from('preferences') |
|
| 438 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 439 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 440 | + ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 441 | + |
|
| 442 | + $query = $queryBuilder->execute(); |
|
| 443 | + |
|
| 444 | + $result = (int)$query->fetchColumn(); |
|
| 445 | + $query->closeCursor(); |
|
| 446 | + |
|
| 447 | + return $result; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * returns how many users have logged in once |
|
| 452 | + * |
|
| 453 | + * @return int |
|
| 454 | + * @since 11.0.0 |
|
| 455 | + */ |
|
| 456 | + public function countSeenUsers() { |
|
| 457 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 458 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 459 | + ->from('preferences') |
|
| 460 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 461 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
| 462 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
| 463 | + |
|
| 464 | + $query = $queryBuilder->execute(); |
|
| 465 | + |
|
| 466 | + $result = (int)$query->fetchColumn(); |
|
| 467 | + $query->closeCursor(); |
|
| 468 | + |
|
| 469 | + return $result; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * @param \Closure $callback |
|
| 474 | + * @since 11.0.0 |
|
| 475 | + */ |
|
| 476 | + public function callForSeenUsers(\Closure $callback) { |
|
| 477 | + $limit = 1000; |
|
| 478 | + $offset = 0; |
|
| 479 | + do { |
|
| 480 | + $userIds = $this->getSeenUserIds($limit, $offset); |
|
| 481 | + $offset += $limit; |
|
| 482 | + foreach ($userIds as $userId) { |
|
| 483 | + foreach ($this->backends as $backend) { |
|
| 484 | + if ($backend->userExists($userId)) { |
|
| 485 | + $user = $this->getUserObject($userId, $backend, false); |
|
| 486 | + $return = $callback($user); |
|
| 487 | + if ($return === false) { |
|
| 488 | + return; |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + } |
|
| 492 | + } |
|
| 493 | + } while (count($userIds) >= $limit); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * Getting all userIds that have a listLogin value requires checking the |
|
| 498 | + * value in php because on oracle you cannot use a clob in a where clause, |
|
| 499 | + * preventing us from doing a not null or length(value) > 0 check. |
|
| 500 | + * |
|
| 501 | + * @param int $limit |
|
| 502 | + * @param int $offset |
|
| 503 | + * @return string[] with user ids |
|
| 504 | + */ |
|
| 505 | + private function getSeenUserIds($limit = null, $offset = null) { |
|
| 506 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 507 | + $queryBuilder->select(['userid']) |
|
| 508 | + ->from('preferences') |
|
| 509 | + ->where($queryBuilder->expr()->eq( |
|
| 510 | + 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 511 | + ) |
|
| 512 | + ->andWhere($queryBuilder->expr()->eq( |
|
| 513 | + 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 514 | + ) |
|
| 515 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 516 | + ); |
|
| 517 | + |
|
| 518 | + if ($limit !== null) { |
|
| 519 | + $queryBuilder->setMaxResults($limit); |
|
| 520 | + } |
|
| 521 | + if ($offset !== null) { |
|
| 522 | + $queryBuilder->setFirstResult($offset); |
|
| 523 | + } |
|
| 524 | + $query = $queryBuilder->execute(); |
|
| 525 | + $result = []; |
|
| 526 | + |
|
| 527 | + while ($row = $query->fetch()) { |
|
| 528 | + $result[] = $row['userid']; |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + $query->closeCursor(); |
|
| 532 | + |
|
| 533 | + return $result; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * @param string $email |
|
| 538 | + * @return IUser[] |
|
| 539 | + * @since 9.1.0 |
|
| 540 | + */ |
|
| 541 | + public function getByEmail($email) { |
|
| 542 | + $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
| 543 | + |
|
| 544 | + return array_map(function($uid) { |
|
| 545 | + return $this->get($uid); |
|
| 546 | + }, $userIds); |
|
| 547 | + } |
|
| 548 | 548 | } |
@@ -38,850 +38,850 @@ |
||
| 38 | 38 | |
| 39 | 39 | class Manager implements ICommentsManager { |
| 40 | 40 | |
| 41 | - /** @var IDBConnection */ |
|
| 42 | - protected $dbConn; |
|
| 43 | - |
|
| 44 | - /** @var ILogger */ |
|
| 45 | - protected $logger; |
|
| 46 | - |
|
| 47 | - /** @var IConfig */ |
|
| 48 | - protected $config; |
|
| 49 | - |
|
| 50 | - /** @var IComment[] */ |
|
| 51 | - protected $commentsCache = []; |
|
| 52 | - |
|
| 53 | - /** @var \Closure[] */ |
|
| 54 | - protected $eventHandlerClosures = []; |
|
| 55 | - |
|
| 56 | - /** @var ICommentsEventHandler[] */ |
|
| 57 | - protected $eventHandlers = []; |
|
| 58 | - |
|
| 59 | - /** @var \Closure[] */ |
|
| 60 | - protected $displayNameResolvers = []; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Manager constructor. |
|
| 64 | - * |
|
| 65 | - * @param IDBConnection $dbConn |
|
| 66 | - * @param ILogger $logger |
|
| 67 | - * @param IConfig $config |
|
| 68 | - */ |
|
| 69 | - public function __construct( |
|
| 70 | - IDBConnection $dbConn, |
|
| 71 | - ILogger $logger, |
|
| 72 | - IConfig $config |
|
| 73 | - ) { |
|
| 74 | - $this->dbConn = $dbConn; |
|
| 75 | - $this->logger = $logger; |
|
| 76 | - $this->config = $config; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * converts data base data into PHP native, proper types as defined by |
|
| 81 | - * IComment interface. |
|
| 82 | - * |
|
| 83 | - * @param array $data |
|
| 84 | - * @return array |
|
| 85 | - */ |
|
| 86 | - protected function normalizeDatabaseData(array $data) { |
|
| 87 | - $data['id'] = strval($data['id']); |
|
| 88 | - $data['parent_id'] = strval($data['parent_id']); |
|
| 89 | - $data['topmost_parent_id'] = strval($data['topmost_parent_id']); |
|
| 90 | - $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
|
| 91 | - if (!is_null($data['latest_child_timestamp'])) { |
|
| 92 | - $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
|
| 93 | - } |
|
| 94 | - $data['children_count'] = intval($data['children_count']); |
|
| 95 | - return $data; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * prepares a comment for an insert or update operation after making sure |
|
| 100 | - * all necessary fields have a value assigned. |
|
| 101 | - * |
|
| 102 | - * @param IComment $comment |
|
| 103 | - * @return IComment returns the same updated IComment instance as provided |
|
| 104 | - * by parameter for convenience |
|
| 105 | - * @throws \UnexpectedValueException |
|
| 106 | - */ |
|
| 107 | - protected function prepareCommentForDatabaseWrite(IComment $comment) { |
|
| 108 | - if (!$comment->getActorType() |
|
| 109 | - || !$comment->getActorId() |
|
| 110 | - || !$comment->getObjectType() |
|
| 111 | - || !$comment->getObjectId() |
|
| 112 | - || !$comment->getVerb() |
|
| 113 | - ) { |
|
| 114 | - throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if ($comment->getId() === '') { |
|
| 118 | - $comment->setChildrenCount(0); |
|
| 119 | - $comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC'))); |
|
| 120 | - $comment->setLatestChildDateTime(null); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if (is_null($comment->getCreationDateTime())) { |
|
| 124 | - $comment->setCreationDateTime(new \DateTime()); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if ($comment->getParentId() !== '0') { |
|
| 128 | - $comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId())); |
|
| 129 | - } else { |
|
| 130 | - $comment->setTopmostParentId('0'); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $this->cache($comment); |
|
| 134 | - |
|
| 135 | - return $comment; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * returns the topmost parent id of a given comment identified by ID |
|
| 140 | - * |
|
| 141 | - * @param string $id |
|
| 142 | - * @return string |
|
| 143 | - * @throws NotFoundException |
|
| 144 | - */ |
|
| 145 | - protected function determineTopmostParentId($id) { |
|
| 146 | - $comment = $this->get($id); |
|
| 147 | - if ($comment->getParentId() === '0') { |
|
| 148 | - return $comment->getId(); |
|
| 149 | - } else { |
|
| 150 | - return $this->determineTopmostParentId($comment->getId()); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * updates child information of a comment |
|
| 156 | - * |
|
| 157 | - * @param string $id |
|
| 158 | - * @param \DateTime $cDateTime the date time of the most recent child |
|
| 159 | - * @throws NotFoundException |
|
| 160 | - */ |
|
| 161 | - protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
|
| 162 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 163 | - $query = $qb->select($qb->createFunction('COUNT(`id`)')) |
|
| 164 | - ->from('comments') |
|
| 165 | - ->where($qb->expr()->eq('parent_id', $qb->createParameter('id'))) |
|
| 166 | - ->setParameter('id', $id); |
|
| 167 | - |
|
| 168 | - $resultStatement = $query->execute(); |
|
| 169 | - $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
| 170 | - $resultStatement->closeCursor(); |
|
| 171 | - $children = intval($data[0]); |
|
| 172 | - |
|
| 173 | - $comment = $this->get($id); |
|
| 174 | - $comment->setChildrenCount($children); |
|
| 175 | - $comment->setLatestChildDateTime($cDateTime); |
|
| 176 | - $this->save($comment); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Tests whether actor or object type and id parameters are acceptable. |
|
| 181 | - * Throws exception if not. |
|
| 182 | - * |
|
| 183 | - * @param string $role |
|
| 184 | - * @param string $type |
|
| 185 | - * @param string $id |
|
| 186 | - * @throws \InvalidArgumentException |
|
| 187 | - */ |
|
| 188 | - protected function checkRoleParameters($role, $type, $id) { |
|
| 189 | - if ( |
|
| 190 | - !is_string($type) || empty($type) |
|
| 191 | - || !is_string($id) || empty($id) |
|
| 192 | - ) { |
|
| 193 | - throw new \InvalidArgumentException($role . ' parameters must be string and not empty'); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * run-time caches a comment |
|
| 199 | - * |
|
| 200 | - * @param IComment $comment |
|
| 201 | - */ |
|
| 202 | - protected function cache(IComment $comment) { |
|
| 203 | - $id = $comment->getId(); |
|
| 204 | - if (empty($id)) { |
|
| 205 | - return; |
|
| 206 | - } |
|
| 207 | - $this->commentsCache[strval($id)] = $comment; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * removes an entry from the comments run time cache |
|
| 212 | - * |
|
| 213 | - * @param mixed $id the comment's id |
|
| 214 | - */ |
|
| 215 | - protected function uncache($id) { |
|
| 216 | - $id = strval($id); |
|
| 217 | - if (isset($this->commentsCache[$id])) { |
|
| 218 | - unset($this->commentsCache[$id]); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * returns a comment instance |
|
| 224 | - * |
|
| 225 | - * @param string $id the ID of the comment |
|
| 226 | - * @return IComment |
|
| 227 | - * @throws NotFoundException |
|
| 228 | - * @throws \InvalidArgumentException |
|
| 229 | - * @since 9.0.0 |
|
| 230 | - */ |
|
| 231 | - public function get($id) { |
|
| 232 | - if (intval($id) === 0) { |
|
| 233 | - throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.'); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - if (isset($this->commentsCache[$id])) { |
|
| 237 | - return $this->commentsCache[$id]; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 241 | - $resultStatement = $qb->select('*') |
|
| 242 | - ->from('comments') |
|
| 243 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 244 | - ->setParameter('id', $id, IQueryBuilder::PARAM_INT) |
|
| 245 | - ->execute(); |
|
| 246 | - |
|
| 247 | - $data = $resultStatement->fetch(); |
|
| 248 | - $resultStatement->closeCursor(); |
|
| 249 | - if (!$data) { |
|
| 250 | - throw new NotFoundException(); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 254 | - $this->cache($comment); |
|
| 255 | - return $comment; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * returns the comment specified by the id and all it's child comments. |
|
| 260 | - * At this point of time, we do only support one level depth. |
|
| 261 | - * |
|
| 262 | - * @param string $id |
|
| 263 | - * @param int $limit max number of entries to return, 0 returns all |
|
| 264 | - * @param int $offset the start entry |
|
| 265 | - * @return array |
|
| 266 | - * @since 9.0.0 |
|
| 267 | - * |
|
| 268 | - * The return array looks like this |
|
| 269 | - * [ |
|
| 270 | - * 'comment' => IComment, // root comment |
|
| 271 | - * 'replies' => |
|
| 272 | - * [ |
|
| 273 | - * 0 => |
|
| 274 | - * [ |
|
| 275 | - * 'comment' => IComment, |
|
| 276 | - * 'replies' => [] |
|
| 277 | - * ] |
|
| 278 | - * 1 => |
|
| 279 | - * [ |
|
| 280 | - * 'comment' => IComment, |
|
| 281 | - * 'replies'=> [] |
|
| 282 | - * ], |
|
| 283 | - * … |
|
| 284 | - * ] |
|
| 285 | - * ] |
|
| 286 | - */ |
|
| 287 | - public function getTree($id, $limit = 0, $offset = 0) { |
|
| 288 | - $tree = []; |
|
| 289 | - $tree['comment'] = $this->get($id); |
|
| 290 | - $tree['replies'] = []; |
|
| 291 | - |
|
| 292 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 293 | - $query = $qb->select('*') |
|
| 294 | - ->from('comments') |
|
| 295 | - ->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id'))) |
|
| 296 | - ->orderBy('creation_timestamp', 'DESC') |
|
| 297 | - ->setParameter('id', $id); |
|
| 298 | - |
|
| 299 | - if ($limit > 0) { |
|
| 300 | - $query->setMaxResults($limit); |
|
| 301 | - } |
|
| 302 | - if ($offset > 0) { |
|
| 303 | - $query->setFirstResult($offset); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - $resultStatement = $query->execute(); |
|
| 307 | - while ($data = $resultStatement->fetch()) { |
|
| 308 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 309 | - $this->cache($comment); |
|
| 310 | - $tree['replies'][] = [ |
|
| 311 | - 'comment' => $comment, |
|
| 312 | - 'replies' => [] |
|
| 313 | - ]; |
|
| 314 | - } |
|
| 315 | - $resultStatement->closeCursor(); |
|
| 316 | - |
|
| 317 | - return $tree; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * returns comments for a specific object (e.g. a file). |
|
| 322 | - * |
|
| 323 | - * The sort order is always newest to oldest. |
|
| 324 | - * |
|
| 325 | - * @param string $objectType the object type, e.g. 'files' |
|
| 326 | - * @param string $objectId the id of the object |
|
| 327 | - * @param int $limit optional, number of maximum comments to be returned. if |
|
| 328 | - * not specified, all comments are returned. |
|
| 329 | - * @param int $offset optional, starting point |
|
| 330 | - * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
| 331 | - * that may be returned |
|
| 332 | - * @return IComment[] |
|
| 333 | - * @since 9.0.0 |
|
| 334 | - */ |
|
| 335 | - public function getForObject( |
|
| 336 | - $objectType, |
|
| 337 | - $objectId, |
|
| 338 | - $limit = 0, |
|
| 339 | - $offset = 0, |
|
| 340 | - \DateTime $notOlderThan = null |
|
| 341 | - ) { |
|
| 342 | - $comments = []; |
|
| 343 | - |
|
| 344 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 345 | - $query = $qb->select('*') |
|
| 346 | - ->from('comments') |
|
| 347 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 348 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 349 | - ->orderBy('creation_timestamp', 'DESC') |
|
| 350 | - ->setParameter('type', $objectType) |
|
| 351 | - ->setParameter('id', $objectId); |
|
| 352 | - |
|
| 353 | - if ($limit > 0) { |
|
| 354 | - $query->setMaxResults($limit); |
|
| 355 | - } |
|
| 356 | - if ($offset > 0) { |
|
| 357 | - $query->setFirstResult($offset); |
|
| 358 | - } |
|
| 359 | - if (!is_null($notOlderThan)) { |
|
| 360 | - $query |
|
| 361 | - ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
| 362 | - ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - $resultStatement = $query->execute(); |
|
| 366 | - while ($data = $resultStatement->fetch()) { |
|
| 367 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 368 | - $this->cache($comment); |
|
| 369 | - $comments[] = $comment; |
|
| 370 | - } |
|
| 371 | - $resultStatement->closeCursor(); |
|
| 372 | - |
|
| 373 | - return $comments; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * @param $objectType string the object type, e.g. 'files' |
|
| 378 | - * @param $objectId string the id of the object |
|
| 379 | - * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
| 380 | - * that may be returned |
|
| 381 | - * @return Int |
|
| 382 | - * @since 9.0.0 |
|
| 383 | - */ |
|
| 384 | - public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) { |
|
| 385 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 386 | - $query = $qb->select($qb->createFunction('COUNT(`id`)')) |
|
| 387 | - ->from('comments') |
|
| 388 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 389 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 390 | - ->setParameter('type', $objectType) |
|
| 391 | - ->setParameter('id', $objectId); |
|
| 392 | - |
|
| 393 | - if (!is_null($notOlderThan)) { |
|
| 394 | - $query |
|
| 395 | - ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
| 396 | - ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - $resultStatement = $query->execute(); |
|
| 400 | - $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
| 401 | - $resultStatement->closeCursor(); |
|
| 402 | - return intval($data[0]); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Get the number of unread comments for all files in a folder |
|
| 407 | - * |
|
| 408 | - * @param int $folderId |
|
| 409 | - * @param IUser $user |
|
| 410 | - * @return array [$fileId => $unreadCount] |
|
| 411 | - */ |
|
| 412 | - public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { |
|
| 413 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 414 | - $query = $qb->select('f.fileid') |
|
| 415 | - ->selectAlias( |
|
| 416 | - $qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'), |
|
| 417 | - 'num_ids' |
|
| 418 | - ) |
|
| 419 | - ->from('comments', 'c') |
|
| 420 | - ->innerJoin('c', 'filecache', 'f', $qb->expr()->andX( |
|
| 421 | - $qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')), |
|
| 422 | - $qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)) |
|
| 423 | - )) |
|
| 424 | - ->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX( |
|
| 425 | - $qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')), |
|
| 426 | - $qb->expr()->eq('m.object_id', 'c.object_id'), |
|
| 427 | - $qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())) |
|
| 428 | - )) |
|
| 429 | - ->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId))) |
|
| 430 | - ->andWhere($qb->expr()->orX( |
|
| 431 | - $qb->expr()->gt('c.creation_timestamp', 'marker_datetime'), |
|
| 432 | - $qb->expr()->isNull('marker_datetime') |
|
| 433 | - )) |
|
| 434 | - ->groupBy('f.fileid'); |
|
| 435 | - |
|
| 436 | - $resultStatement = $query->execute(); |
|
| 437 | - |
|
| 438 | - $results = []; |
|
| 439 | - while ($row = $resultStatement->fetch()) { |
|
| 440 | - $results[$row['fileid']] = (int) $row['num_ids']; |
|
| 441 | - } |
|
| 442 | - $resultStatement->closeCursor(); |
|
| 443 | - return $results; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * creates a new comment and returns it. At this point of time, it is not |
|
| 448 | - * saved in the used data storage. Use save() after setting other fields |
|
| 449 | - * of the comment (e.g. message or verb). |
|
| 450 | - * |
|
| 451 | - * @param string $actorType the actor type (e.g. 'users') |
|
| 452 | - * @param string $actorId a user id |
|
| 453 | - * @param string $objectType the object type the comment is attached to |
|
| 454 | - * @param string $objectId the object id the comment is attached to |
|
| 455 | - * @return IComment |
|
| 456 | - * @since 9.0.0 |
|
| 457 | - */ |
|
| 458 | - public function create($actorType, $actorId, $objectType, $objectId) { |
|
| 459 | - $comment = new Comment(); |
|
| 460 | - $comment |
|
| 461 | - ->setActor($actorType, $actorId) |
|
| 462 | - ->setObject($objectType, $objectId); |
|
| 463 | - return $comment; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * permanently deletes the comment specified by the ID |
|
| 468 | - * |
|
| 469 | - * When the comment has child comments, their parent ID will be changed to |
|
| 470 | - * the parent ID of the item that is to be deleted. |
|
| 471 | - * |
|
| 472 | - * @param string $id |
|
| 473 | - * @return bool |
|
| 474 | - * @throws \InvalidArgumentException |
|
| 475 | - * @since 9.0.0 |
|
| 476 | - */ |
|
| 477 | - public function delete($id) { |
|
| 478 | - if (!is_string($id)) { |
|
| 479 | - throw new \InvalidArgumentException('Parameter must be string'); |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - try { |
|
| 483 | - $comment = $this->get($id); |
|
| 484 | - } catch (\Exception $e) { |
|
| 485 | - // Ignore exceptions, we just don't fire a hook then |
|
| 486 | - $comment = null; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 490 | - $query = $qb->delete('comments') |
|
| 491 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 492 | - ->setParameter('id', $id); |
|
| 493 | - |
|
| 494 | - try { |
|
| 495 | - $affectedRows = $query->execute(); |
|
| 496 | - $this->uncache($id); |
|
| 497 | - } catch (DriverException $e) { |
|
| 498 | - $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 499 | - return false; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - if ($affectedRows > 0 && $comment instanceof IComment) { |
|
| 503 | - $this->sendEvent(CommentsEvent::EVENT_DELETE, $comment); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - return ($affectedRows > 0); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * saves the comment permanently |
|
| 511 | - * |
|
| 512 | - * if the supplied comment has an empty ID, a new entry comment will be |
|
| 513 | - * saved and the instance updated with the new ID. |
|
| 514 | - * |
|
| 515 | - * Otherwise, an existing comment will be updated. |
|
| 516 | - * |
|
| 517 | - * Throws NotFoundException when a comment that is to be updated does not |
|
| 518 | - * exist anymore at this point of time. |
|
| 519 | - * |
|
| 520 | - * @param IComment $comment |
|
| 521 | - * @return bool |
|
| 522 | - * @throws NotFoundException |
|
| 523 | - * @since 9.0.0 |
|
| 524 | - */ |
|
| 525 | - public function save(IComment $comment) { |
|
| 526 | - if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') { |
|
| 527 | - $result = $this->insert($comment); |
|
| 528 | - } else { |
|
| 529 | - $result = $this->update($comment); |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - if ($result && !!$comment->getParentId()) { |
|
| 533 | - $this->updateChildrenInformation( |
|
| 534 | - $comment->getParentId(), |
|
| 535 | - $comment->getCreationDateTime() |
|
| 536 | - ); |
|
| 537 | - $this->cache($comment); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - return $result; |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - /** |
|
| 544 | - * inserts the provided comment in the database |
|
| 545 | - * |
|
| 546 | - * @param IComment $comment |
|
| 547 | - * @return bool |
|
| 548 | - */ |
|
| 549 | - protected function insert(IComment &$comment) { |
|
| 550 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 551 | - $affectedRows = $qb |
|
| 552 | - ->insert('comments') |
|
| 553 | - ->values([ |
|
| 554 | - 'parent_id' => $qb->createNamedParameter($comment->getParentId()), |
|
| 555 | - 'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()), |
|
| 556 | - 'children_count' => $qb->createNamedParameter($comment->getChildrenCount()), |
|
| 557 | - 'actor_type' => $qb->createNamedParameter($comment->getActorType()), |
|
| 558 | - 'actor_id' => $qb->createNamedParameter($comment->getActorId()), |
|
| 559 | - 'message' => $qb->createNamedParameter($comment->getMessage()), |
|
| 560 | - 'verb' => $qb->createNamedParameter($comment->getVerb()), |
|
| 561 | - 'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'), |
|
| 562 | - 'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'), |
|
| 563 | - 'object_type' => $qb->createNamedParameter($comment->getObjectType()), |
|
| 564 | - 'object_id' => $qb->createNamedParameter($comment->getObjectId()), |
|
| 565 | - ]) |
|
| 566 | - ->execute(); |
|
| 567 | - |
|
| 568 | - if ($affectedRows > 0) { |
|
| 569 | - $comment->setId(strval($qb->getLastInsertId())); |
|
| 570 | - $this->sendEvent(CommentsEvent::EVENT_ADD, $comment); |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - return $affectedRows > 0; |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - /** |
|
| 577 | - * updates a Comment data row |
|
| 578 | - * |
|
| 579 | - * @param IComment $comment |
|
| 580 | - * @return bool |
|
| 581 | - * @throws NotFoundException |
|
| 582 | - */ |
|
| 583 | - protected function update(IComment $comment) { |
|
| 584 | - // for properly working preUpdate Events we need the old comments as is |
|
| 585 | - // in the DB and overcome caching. Also avoid that outdated information stays. |
|
| 586 | - $this->uncache($comment->getId()); |
|
| 587 | - $this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId())); |
|
| 588 | - $this->uncache($comment->getId()); |
|
| 589 | - |
|
| 590 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 591 | - $affectedRows = $qb |
|
| 592 | - ->update('comments') |
|
| 593 | - ->set('parent_id', $qb->createNamedParameter($comment->getParentId())) |
|
| 594 | - ->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId())) |
|
| 595 | - ->set('children_count', $qb->createNamedParameter($comment->getChildrenCount())) |
|
| 596 | - ->set('actor_type', $qb->createNamedParameter($comment->getActorType())) |
|
| 597 | - ->set('actor_id', $qb->createNamedParameter($comment->getActorId())) |
|
| 598 | - ->set('message', $qb->createNamedParameter($comment->getMessage())) |
|
| 599 | - ->set('verb', $qb->createNamedParameter($comment->getVerb())) |
|
| 600 | - ->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime')) |
|
| 601 | - ->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime')) |
|
| 602 | - ->set('object_type', $qb->createNamedParameter($comment->getObjectType())) |
|
| 603 | - ->set('object_id', $qb->createNamedParameter($comment->getObjectId())) |
|
| 604 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 605 | - ->setParameter('id', $comment->getId()) |
|
| 606 | - ->execute(); |
|
| 607 | - |
|
| 608 | - if ($affectedRows === 0) { |
|
| 609 | - throw new NotFoundException('Comment to update does ceased to exist'); |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment); |
|
| 613 | - |
|
| 614 | - return $affectedRows > 0; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * removes references to specific actor (e.g. on user delete) of a comment. |
|
| 619 | - * The comment itself must not get lost/deleted. |
|
| 620 | - * |
|
| 621 | - * @param string $actorType the actor type (e.g. 'users') |
|
| 622 | - * @param string $actorId a user id |
|
| 623 | - * @return boolean |
|
| 624 | - * @since 9.0.0 |
|
| 625 | - */ |
|
| 626 | - public function deleteReferencesOfActor($actorType, $actorId) { |
|
| 627 | - $this->checkRoleParameters('Actor', $actorType, $actorId); |
|
| 628 | - |
|
| 629 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 630 | - $affectedRows = $qb |
|
| 631 | - ->update('comments') |
|
| 632 | - ->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
| 633 | - ->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
| 634 | - ->where($qb->expr()->eq('actor_type', $qb->createParameter('type'))) |
|
| 635 | - ->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id'))) |
|
| 636 | - ->setParameter('type', $actorType) |
|
| 637 | - ->setParameter('id', $actorId) |
|
| 638 | - ->execute(); |
|
| 639 | - |
|
| 640 | - $this->commentsCache = []; |
|
| 641 | - |
|
| 642 | - return is_int($affectedRows); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - /** |
|
| 646 | - * deletes all comments made of a specific object (e.g. on file delete) |
|
| 647 | - * |
|
| 648 | - * @param string $objectType the object type (e.g. 'files') |
|
| 649 | - * @param string $objectId e.g. the file id |
|
| 650 | - * @return boolean |
|
| 651 | - * @since 9.0.0 |
|
| 652 | - */ |
|
| 653 | - public function deleteCommentsAtObject($objectType, $objectId) { |
|
| 654 | - $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 655 | - |
|
| 656 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 657 | - $affectedRows = $qb |
|
| 658 | - ->delete('comments') |
|
| 659 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 660 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 661 | - ->setParameter('type', $objectType) |
|
| 662 | - ->setParameter('id', $objectId) |
|
| 663 | - ->execute(); |
|
| 664 | - |
|
| 665 | - $this->commentsCache = []; |
|
| 666 | - |
|
| 667 | - return is_int($affectedRows); |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * deletes the read markers for the specified user |
|
| 672 | - * |
|
| 673 | - * @param \OCP\IUser $user |
|
| 674 | - * @return bool |
|
| 675 | - * @since 9.0.0 |
|
| 676 | - */ |
|
| 677 | - public function deleteReadMarksFromUser(IUser $user) { |
|
| 678 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 679 | - $query = $qb->delete('comments_read_markers') |
|
| 680 | - ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 681 | - ->setParameter('user_id', $user->getUID()); |
|
| 682 | - |
|
| 683 | - try { |
|
| 684 | - $affectedRows = $query->execute(); |
|
| 685 | - } catch (DriverException $e) { |
|
| 686 | - $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 687 | - return false; |
|
| 688 | - } |
|
| 689 | - return ($affectedRows > 0); |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * sets the read marker for a given file to the specified date for the |
|
| 694 | - * provided user |
|
| 695 | - * |
|
| 696 | - * @param string $objectType |
|
| 697 | - * @param string $objectId |
|
| 698 | - * @param \DateTime $dateTime |
|
| 699 | - * @param IUser $user |
|
| 700 | - * @since 9.0.0 |
|
| 701 | - * @suppress SqlInjectionChecker |
|
| 702 | - */ |
|
| 703 | - public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) { |
|
| 704 | - $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 705 | - |
|
| 706 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 707 | - $values = [ |
|
| 708 | - 'user_id' => $qb->createNamedParameter($user->getUID()), |
|
| 709 | - 'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'), |
|
| 710 | - 'object_type' => $qb->createNamedParameter($objectType), |
|
| 711 | - 'object_id' => $qb->createNamedParameter($objectId), |
|
| 712 | - ]; |
|
| 713 | - |
|
| 714 | - // Strategy: try to update, if this does not return affected rows, do an insert. |
|
| 715 | - $affectedRows = $qb |
|
| 716 | - ->update('comments_read_markers') |
|
| 717 | - ->set('user_id', $values['user_id']) |
|
| 718 | - ->set('marker_datetime', $values['marker_datetime']) |
|
| 719 | - ->set('object_type', $values['object_type']) |
|
| 720 | - ->set('object_id', $values['object_id']) |
|
| 721 | - ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 722 | - ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 723 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 724 | - ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
| 725 | - ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
| 726 | - ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
| 727 | - ->execute(); |
|
| 728 | - |
|
| 729 | - if ($affectedRows > 0) { |
|
| 730 | - return; |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - $qb->insert('comments_read_markers') |
|
| 734 | - ->values($values) |
|
| 735 | - ->execute(); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - /** |
|
| 739 | - * returns the read marker for a given file to the specified date for the |
|
| 740 | - * provided user. It returns null, when the marker is not present, i.e. |
|
| 741 | - * no comments were marked as read. |
|
| 742 | - * |
|
| 743 | - * @param string $objectType |
|
| 744 | - * @param string $objectId |
|
| 745 | - * @param IUser $user |
|
| 746 | - * @return \DateTime|null |
|
| 747 | - * @since 9.0.0 |
|
| 748 | - */ |
|
| 749 | - public function getReadMark($objectType, $objectId, IUser $user) { |
|
| 750 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 751 | - $resultStatement = $qb->select('marker_datetime') |
|
| 752 | - ->from('comments_read_markers') |
|
| 753 | - ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 754 | - ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 755 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 756 | - ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
| 757 | - ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
| 758 | - ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
| 759 | - ->execute(); |
|
| 760 | - |
|
| 761 | - $data = $resultStatement->fetch(); |
|
| 762 | - $resultStatement->closeCursor(); |
|
| 763 | - if (!$data || is_null($data['marker_datetime'])) { |
|
| 764 | - return null; |
|
| 765 | - } |
|
| 766 | - |
|
| 767 | - return new \DateTime($data['marker_datetime']); |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - /** |
|
| 771 | - * deletes the read markers on the specified object |
|
| 772 | - * |
|
| 773 | - * @param string $objectType |
|
| 774 | - * @param string $objectId |
|
| 775 | - * @return bool |
|
| 776 | - * @since 9.0.0 |
|
| 777 | - */ |
|
| 778 | - public function deleteReadMarksOnObject($objectType, $objectId) { |
|
| 779 | - $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 780 | - |
|
| 781 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 782 | - $query = $qb->delete('comments_read_markers') |
|
| 783 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 784 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 785 | - ->setParameter('object_type', $objectType) |
|
| 786 | - ->setParameter('object_id', $objectId); |
|
| 787 | - |
|
| 788 | - try { |
|
| 789 | - $affectedRows = $query->execute(); |
|
| 790 | - } catch (DriverException $e) { |
|
| 791 | - $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 792 | - return false; |
|
| 793 | - } |
|
| 794 | - return ($affectedRows > 0); |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - /** |
|
| 798 | - * registers an Entity to the manager, so event notifications can be send |
|
| 799 | - * to consumers of the comments infrastructure |
|
| 800 | - * |
|
| 801 | - * @param \Closure $closure |
|
| 802 | - */ |
|
| 803 | - public function registerEventHandler(\Closure $closure) { |
|
| 804 | - $this->eventHandlerClosures[] = $closure; |
|
| 805 | - $this->eventHandlers = []; |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - /** |
|
| 809 | - * registers a method that resolves an ID to a display name for a given type |
|
| 810 | - * |
|
| 811 | - * @param string $type |
|
| 812 | - * @param \Closure $closure |
|
| 813 | - * @throws \OutOfBoundsException |
|
| 814 | - * @since 11.0.0 |
|
| 815 | - * |
|
| 816 | - * Only one resolver shall be registered per type. Otherwise a |
|
| 817 | - * \OutOfBoundsException has to thrown. |
|
| 818 | - */ |
|
| 819 | - public function registerDisplayNameResolver($type, \Closure $closure) { |
|
| 820 | - if (!is_string($type)) { |
|
| 821 | - throw new \InvalidArgumentException('String expected.'); |
|
| 822 | - } |
|
| 823 | - if (isset($this->displayNameResolvers[$type])) { |
|
| 824 | - throw new \OutOfBoundsException('Displayname resolver for this type already registered'); |
|
| 825 | - } |
|
| 826 | - $this->displayNameResolvers[$type] = $closure; |
|
| 827 | - } |
|
| 828 | - |
|
| 829 | - /** |
|
| 830 | - * resolves a given ID of a given Type to a display name. |
|
| 831 | - * |
|
| 832 | - * @param string $type |
|
| 833 | - * @param string $id |
|
| 834 | - * @return string |
|
| 835 | - * @throws \OutOfBoundsException |
|
| 836 | - * @since 11.0.0 |
|
| 837 | - * |
|
| 838 | - * If a provided type was not registered, an \OutOfBoundsException shall |
|
| 839 | - * be thrown. It is upon the resolver discretion what to return of the |
|
| 840 | - * provided ID is unknown. It must be ensured that a string is returned. |
|
| 841 | - */ |
|
| 842 | - public function resolveDisplayName($type, $id) { |
|
| 843 | - if (!is_string($type)) { |
|
| 844 | - throw new \InvalidArgumentException('String expected.'); |
|
| 845 | - } |
|
| 846 | - if (!isset($this->displayNameResolvers[$type])) { |
|
| 847 | - throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
|
| 848 | - } |
|
| 849 | - return (string)$this->displayNameResolvers[$type]($id); |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - /** |
|
| 853 | - * returns valid, registered entities |
|
| 854 | - * |
|
| 855 | - * @return \OCP\Comments\ICommentsEventHandler[] |
|
| 856 | - */ |
|
| 857 | - private function getEventHandlers() { |
|
| 858 | - if (!empty($this->eventHandlers)) { |
|
| 859 | - return $this->eventHandlers; |
|
| 860 | - } |
|
| 861 | - |
|
| 862 | - $this->eventHandlers = []; |
|
| 863 | - foreach ($this->eventHandlerClosures as $name => $closure) { |
|
| 864 | - $entity = $closure(); |
|
| 865 | - if (!($entity instanceof ICommentsEventHandler)) { |
|
| 866 | - throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface'); |
|
| 867 | - } |
|
| 868 | - $this->eventHandlers[$name] = $entity; |
|
| 869 | - } |
|
| 870 | - |
|
| 871 | - return $this->eventHandlers; |
|
| 872 | - } |
|
| 873 | - |
|
| 874 | - /** |
|
| 875 | - * sends notifications to the registered entities |
|
| 876 | - * |
|
| 877 | - * @param $eventType |
|
| 878 | - * @param IComment $comment |
|
| 879 | - */ |
|
| 880 | - private function sendEvent($eventType, IComment $comment) { |
|
| 881 | - $entities = $this->getEventHandlers(); |
|
| 882 | - $event = new CommentsEvent($eventType, $comment); |
|
| 883 | - foreach ($entities as $entity) { |
|
| 884 | - $entity->handle($event); |
|
| 885 | - } |
|
| 886 | - } |
|
| 41 | + /** @var IDBConnection */ |
|
| 42 | + protected $dbConn; |
|
| 43 | + |
|
| 44 | + /** @var ILogger */ |
|
| 45 | + protected $logger; |
|
| 46 | + |
|
| 47 | + /** @var IConfig */ |
|
| 48 | + protected $config; |
|
| 49 | + |
|
| 50 | + /** @var IComment[] */ |
|
| 51 | + protected $commentsCache = []; |
|
| 52 | + |
|
| 53 | + /** @var \Closure[] */ |
|
| 54 | + protected $eventHandlerClosures = []; |
|
| 55 | + |
|
| 56 | + /** @var ICommentsEventHandler[] */ |
|
| 57 | + protected $eventHandlers = []; |
|
| 58 | + |
|
| 59 | + /** @var \Closure[] */ |
|
| 60 | + protected $displayNameResolvers = []; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Manager constructor. |
|
| 64 | + * |
|
| 65 | + * @param IDBConnection $dbConn |
|
| 66 | + * @param ILogger $logger |
|
| 67 | + * @param IConfig $config |
|
| 68 | + */ |
|
| 69 | + public function __construct( |
|
| 70 | + IDBConnection $dbConn, |
|
| 71 | + ILogger $logger, |
|
| 72 | + IConfig $config |
|
| 73 | + ) { |
|
| 74 | + $this->dbConn = $dbConn; |
|
| 75 | + $this->logger = $logger; |
|
| 76 | + $this->config = $config; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * converts data base data into PHP native, proper types as defined by |
|
| 81 | + * IComment interface. |
|
| 82 | + * |
|
| 83 | + * @param array $data |
|
| 84 | + * @return array |
|
| 85 | + */ |
|
| 86 | + protected function normalizeDatabaseData(array $data) { |
|
| 87 | + $data['id'] = strval($data['id']); |
|
| 88 | + $data['parent_id'] = strval($data['parent_id']); |
|
| 89 | + $data['topmost_parent_id'] = strval($data['topmost_parent_id']); |
|
| 90 | + $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
|
| 91 | + if (!is_null($data['latest_child_timestamp'])) { |
|
| 92 | + $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
|
| 93 | + } |
|
| 94 | + $data['children_count'] = intval($data['children_count']); |
|
| 95 | + return $data; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * prepares a comment for an insert or update operation after making sure |
|
| 100 | + * all necessary fields have a value assigned. |
|
| 101 | + * |
|
| 102 | + * @param IComment $comment |
|
| 103 | + * @return IComment returns the same updated IComment instance as provided |
|
| 104 | + * by parameter for convenience |
|
| 105 | + * @throws \UnexpectedValueException |
|
| 106 | + */ |
|
| 107 | + protected function prepareCommentForDatabaseWrite(IComment $comment) { |
|
| 108 | + if (!$comment->getActorType() |
|
| 109 | + || !$comment->getActorId() |
|
| 110 | + || !$comment->getObjectType() |
|
| 111 | + || !$comment->getObjectId() |
|
| 112 | + || !$comment->getVerb() |
|
| 113 | + ) { |
|
| 114 | + throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if ($comment->getId() === '') { |
|
| 118 | + $comment->setChildrenCount(0); |
|
| 119 | + $comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC'))); |
|
| 120 | + $comment->setLatestChildDateTime(null); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if (is_null($comment->getCreationDateTime())) { |
|
| 124 | + $comment->setCreationDateTime(new \DateTime()); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if ($comment->getParentId() !== '0') { |
|
| 128 | + $comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId())); |
|
| 129 | + } else { |
|
| 130 | + $comment->setTopmostParentId('0'); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $this->cache($comment); |
|
| 134 | + |
|
| 135 | + return $comment; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * returns the topmost parent id of a given comment identified by ID |
|
| 140 | + * |
|
| 141 | + * @param string $id |
|
| 142 | + * @return string |
|
| 143 | + * @throws NotFoundException |
|
| 144 | + */ |
|
| 145 | + protected function determineTopmostParentId($id) { |
|
| 146 | + $comment = $this->get($id); |
|
| 147 | + if ($comment->getParentId() === '0') { |
|
| 148 | + return $comment->getId(); |
|
| 149 | + } else { |
|
| 150 | + return $this->determineTopmostParentId($comment->getId()); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * updates child information of a comment |
|
| 156 | + * |
|
| 157 | + * @param string $id |
|
| 158 | + * @param \DateTime $cDateTime the date time of the most recent child |
|
| 159 | + * @throws NotFoundException |
|
| 160 | + */ |
|
| 161 | + protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
|
| 162 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 163 | + $query = $qb->select($qb->createFunction('COUNT(`id`)')) |
|
| 164 | + ->from('comments') |
|
| 165 | + ->where($qb->expr()->eq('parent_id', $qb->createParameter('id'))) |
|
| 166 | + ->setParameter('id', $id); |
|
| 167 | + |
|
| 168 | + $resultStatement = $query->execute(); |
|
| 169 | + $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
| 170 | + $resultStatement->closeCursor(); |
|
| 171 | + $children = intval($data[0]); |
|
| 172 | + |
|
| 173 | + $comment = $this->get($id); |
|
| 174 | + $comment->setChildrenCount($children); |
|
| 175 | + $comment->setLatestChildDateTime($cDateTime); |
|
| 176 | + $this->save($comment); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Tests whether actor or object type and id parameters are acceptable. |
|
| 181 | + * Throws exception if not. |
|
| 182 | + * |
|
| 183 | + * @param string $role |
|
| 184 | + * @param string $type |
|
| 185 | + * @param string $id |
|
| 186 | + * @throws \InvalidArgumentException |
|
| 187 | + */ |
|
| 188 | + protected function checkRoleParameters($role, $type, $id) { |
|
| 189 | + if ( |
|
| 190 | + !is_string($type) || empty($type) |
|
| 191 | + || !is_string($id) || empty($id) |
|
| 192 | + ) { |
|
| 193 | + throw new \InvalidArgumentException($role . ' parameters must be string and not empty'); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * run-time caches a comment |
|
| 199 | + * |
|
| 200 | + * @param IComment $comment |
|
| 201 | + */ |
|
| 202 | + protected function cache(IComment $comment) { |
|
| 203 | + $id = $comment->getId(); |
|
| 204 | + if (empty($id)) { |
|
| 205 | + return; |
|
| 206 | + } |
|
| 207 | + $this->commentsCache[strval($id)] = $comment; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * removes an entry from the comments run time cache |
|
| 212 | + * |
|
| 213 | + * @param mixed $id the comment's id |
|
| 214 | + */ |
|
| 215 | + protected function uncache($id) { |
|
| 216 | + $id = strval($id); |
|
| 217 | + if (isset($this->commentsCache[$id])) { |
|
| 218 | + unset($this->commentsCache[$id]); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * returns a comment instance |
|
| 224 | + * |
|
| 225 | + * @param string $id the ID of the comment |
|
| 226 | + * @return IComment |
|
| 227 | + * @throws NotFoundException |
|
| 228 | + * @throws \InvalidArgumentException |
|
| 229 | + * @since 9.0.0 |
|
| 230 | + */ |
|
| 231 | + public function get($id) { |
|
| 232 | + if (intval($id) === 0) { |
|
| 233 | + throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.'); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + if (isset($this->commentsCache[$id])) { |
|
| 237 | + return $this->commentsCache[$id]; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 241 | + $resultStatement = $qb->select('*') |
|
| 242 | + ->from('comments') |
|
| 243 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 244 | + ->setParameter('id', $id, IQueryBuilder::PARAM_INT) |
|
| 245 | + ->execute(); |
|
| 246 | + |
|
| 247 | + $data = $resultStatement->fetch(); |
|
| 248 | + $resultStatement->closeCursor(); |
|
| 249 | + if (!$data) { |
|
| 250 | + throw new NotFoundException(); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 254 | + $this->cache($comment); |
|
| 255 | + return $comment; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * returns the comment specified by the id and all it's child comments. |
|
| 260 | + * At this point of time, we do only support one level depth. |
|
| 261 | + * |
|
| 262 | + * @param string $id |
|
| 263 | + * @param int $limit max number of entries to return, 0 returns all |
|
| 264 | + * @param int $offset the start entry |
|
| 265 | + * @return array |
|
| 266 | + * @since 9.0.0 |
|
| 267 | + * |
|
| 268 | + * The return array looks like this |
|
| 269 | + * [ |
|
| 270 | + * 'comment' => IComment, // root comment |
|
| 271 | + * 'replies' => |
|
| 272 | + * [ |
|
| 273 | + * 0 => |
|
| 274 | + * [ |
|
| 275 | + * 'comment' => IComment, |
|
| 276 | + * 'replies' => [] |
|
| 277 | + * ] |
|
| 278 | + * 1 => |
|
| 279 | + * [ |
|
| 280 | + * 'comment' => IComment, |
|
| 281 | + * 'replies'=> [] |
|
| 282 | + * ], |
|
| 283 | + * … |
|
| 284 | + * ] |
|
| 285 | + * ] |
|
| 286 | + */ |
|
| 287 | + public function getTree($id, $limit = 0, $offset = 0) { |
|
| 288 | + $tree = []; |
|
| 289 | + $tree['comment'] = $this->get($id); |
|
| 290 | + $tree['replies'] = []; |
|
| 291 | + |
|
| 292 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 293 | + $query = $qb->select('*') |
|
| 294 | + ->from('comments') |
|
| 295 | + ->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id'))) |
|
| 296 | + ->orderBy('creation_timestamp', 'DESC') |
|
| 297 | + ->setParameter('id', $id); |
|
| 298 | + |
|
| 299 | + if ($limit > 0) { |
|
| 300 | + $query->setMaxResults($limit); |
|
| 301 | + } |
|
| 302 | + if ($offset > 0) { |
|
| 303 | + $query->setFirstResult($offset); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + $resultStatement = $query->execute(); |
|
| 307 | + while ($data = $resultStatement->fetch()) { |
|
| 308 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 309 | + $this->cache($comment); |
|
| 310 | + $tree['replies'][] = [ |
|
| 311 | + 'comment' => $comment, |
|
| 312 | + 'replies' => [] |
|
| 313 | + ]; |
|
| 314 | + } |
|
| 315 | + $resultStatement->closeCursor(); |
|
| 316 | + |
|
| 317 | + return $tree; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * returns comments for a specific object (e.g. a file). |
|
| 322 | + * |
|
| 323 | + * The sort order is always newest to oldest. |
|
| 324 | + * |
|
| 325 | + * @param string $objectType the object type, e.g. 'files' |
|
| 326 | + * @param string $objectId the id of the object |
|
| 327 | + * @param int $limit optional, number of maximum comments to be returned. if |
|
| 328 | + * not specified, all comments are returned. |
|
| 329 | + * @param int $offset optional, starting point |
|
| 330 | + * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
| 331 | + * that may be returned |
|
| 332 | + * @return IComment[] |
|
| 333 | + * @since 9.0.0 |
|
| 334 | + */ |
|
| 335 | + public function getForObject( |
|
| 336 | + $objectType, |
|
| 337 | + $objectId, |
|
| 338 | + $limit = 0, |
|
| 339 | + $offset = 0, |
|
| 340 | + \DateTime $notOlderThan = null |
|
| 341 | + ) { |
|
| 342 | + $comments = []; |
|
| 343 | + |
|
| 344 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 345 | + $query = $qb->select('*') |
|
| 346 | + ->from('comments') |
|
| 347 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 348 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 349 | + ->orderBy('creation_timestamp', 'DESC') |
|
| 350 | + ->setParameter('type', $objectType) |
|
| 351 | + ->setParameter('id', $objectId); |
|
| 352 | + |
|
| 353 | + if ($limit > 0) { |
|
| 354 | + $query->setMaxResults($limit); |
|
| 355 | + } |
|
| 356 | + if ($offset > 0) { |
|
| 357 | + $query->setFirstResult($offset); |
|
| 358 | + } |
|
| 359 | + if (!is_null($notOlderThan)) { |
|
| 360 | + $query |
|
| 361 | + ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
| 362 | + ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + $resultStatement = $query->execute(); |
|
| 366 | + while ($data = $resultStatement->fetch()) { |
|
| 367 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 368 | + $this->cache($comment); |
|
| 369 | + $comments[] = $comment; |
|
| 370 | + } |
|
| 371 | + $resultStatement->closeCursor(); |
|
| 372 | + |
|
| 373 | + return $comments; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * @param $objectType string the object type, e.g. 'files' |
|
| 378 | + * @param $objectId string the id of the object |
|
| 379 | + * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
| 380 | + * that may be returned |
|
| 381 | + * @return Int |
|
| 382 | + * @since 9.0.0 |
|
| 383 | + */ |
|
| 384 | + public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) { |
|
| 385 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 386 | + $query = $qb->select($qb->createFunction('COUNT(`id`)')) |
|
| 387 | + ->from('comments') |
|
| 388 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 389 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 390 | + ->setParameter('type', $objectType) |
|
| 391 | + ->setParameter('id', $objectId); |
|
| 392 | + |
|
| 393 | + if (!is_null($notOlderThan)) { |
|
| 394 | + $query |
|
| 395 | + ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
| 396 | + ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + $resultStatement = $query->execute(); |
|
| 400 | + $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
| 401 | + $resultStatement->closeCursor(); |
|
| 402 | + return intval($data[0]); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Get the number of unread comments for all files in a folder |
|
| 407 | + * |
|
| 408 | + * @param int $folderId |
|
| 409 | + * @param IUser $user |
|
| 410 | + * @return array [$fileId => $unreadCount] |
|
| 411 | + */ |
|
| 412 | + public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { |
|
| 413 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 414 | + $query = $qb->select('f.fileid') |
|
| 415 | + ->selectAlias( |
|
| 416 | + $qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'), |
|
| 417 | + 'num_ids' |
|
| 418 | + ) |
|
| 419 | + ->from('comments', 'c') |
|
| 420 | + ->innerJoin('c', 'filecache', 'f', $qb->expr()->andX( |
|
| 421 | + $qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')), |
|
| 422 | + $qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)) |
|
| 423 | + )) |
|
| 424 | + ->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX( |
|
| 425 | + $qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')), |
|
| 426 | + $qb->expr()->eq('m.object_id', 'c.object_id'), |
|
| 427 | + $qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())) |
|
| 428 | + )) |
|
| 429 | + ->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId))) |
|
| 430 | + ->andWhere($qb->expr()->orX( |
|
| 431 | + $qb->expr()->gt('c.creation_timestamp', 'marker_datetime'), |
|
| 432 | + $qb->expr()->isNull('marker_datetime') |
|
| 433 | + )) |
|
| 434 | + ->groupBy('f.fileid'); |
|
| 435 | + |
|
| 436 | + $resultStatement = $query->execute(); |
|
| 437 | + |
|
| 438 | + $results = []; |
|
| 439 | + while ($row = $resultStatement->fetch()) { |
|
| 440 | + $results[$row['fileid']] = (int) $row['num_ids']; |
|
| 441 | + } |
|
| 442 | + $resultStatement->closeCursor(); |
|
| 443 | + return $results; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * creates a new comment and returns it. At this point of time, it is not |
|
| 448 | + * saved in the used data storage. Use save() after setting other fields |
|
| 449 | + * of the comment (e.g. message or verb). |
|
| 450 | + * |
|
| 451 | + * @param string $actorType the actor type (e.g. 'users') |
|
| 452 | + * @param string $actorId a user id |
|
| 453 | + * @param string $objectType the object type the comment is attached to |
|
| 454 | + * @param string $objectId the object id the comment is attached to |
|
| 455 | + * @return IComment |
|
| 456 | + * @since 9.0.0 |
|
| 457 | + */ |
|
| 458 | + public function create($actorType, $actorId, $objectType, $objectId) { |
|
| 459 | + $comment = new Comment(); |
|
| 460 | + $comment |
|
| 461 | + ->setActor($actorType, $actorId) |
|
| 462 | + ->setObject($objectType, $objectId); |
|
| 463 | + return $comment; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * permanently deletes the comment specified by the ID |
|
| 468 | + * |
|
| 469 | + * When the comment has child comments, their parent ID will be changed to |
|
| 470 | + * the parent ID of the item that is to be deleted. |
|
| 471 | + * |
|
| 472 | + * @param string $id |
|
| 473 | + * @return bool |
|
| 474 | + * @throws \InvalidArgumentException |
|
| 475 | + * @since 9.0.0 |
|
| 476 | + */ |
|
| 477 | + public function delete($id) { |
|
| 478 | + if (!is_string($id)) { |
|
| 479 | + throw new \InvalidArgumentException('Parameter must be string'); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + try { |
|
| 483 | + $comment = $this->get($id); |
|
| 484 | + } catch (\Exception $e) { |
|
| 485 | + // Ignore exceptions, we just don't fire a hook then |
|
| 486 | + $comment = null; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 490 | + $query = $qb->delete('comments') |
|
| 491 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 492 | + ->setParameter('id', $id); |
|
| 493 | + |
|
| 494 | + try { |
|
| 495 | + $affectedRows = $query->execute(); |
|
| 496 | + $this->uncache($id); |
|
| 497 | + } catch (DriverException $e) { |
|
| 498 | + $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 499 | + return false; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + if ($affectedRows > 0 && $comment instanceof IComment) { |
|
| 503 | + $this->sendEvent(CommentsEvent::EVENT_DELETE, $comment); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + return ($affectedRows > 0); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * saves the comment permanently |
|
| 511 | + * |
|
| 512 | + * if the supplied comment has an empty ID, a new entry comment will be |
|
| 513 | + * saved and the instance updated with the new ID. |
|
| 514 | + * |
|
| 515 | + * Otherwise, an existing comment will be updated. |
|
| 516 | + * |
|
| 517 | + * Throws NotFoundException when a comment that is to be updated does not |
|
| 518 | + * exist anymore at this point of time. |
|
| 519 | + * |
|
| 520 | + * @param IComment $comment |
|
| 521 | + * @return bool |
|
| 522 | + * @throws NotFoundException |
|
| 523 | + * @since 9.0.0 |
|
| 524 | + */ |
|
| 525 | + public function save(IComment $comment) { |
|
| 526 | + if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') { |
|
| 527 | + $result = $this->insert($comment); |
|
| 528 | + } else { |
|
| 529 | + $result = $this->update($comment); |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + if ($result && !!$comment->getParentId()) { |
|
| 533 | + $this->updateChildrenInformation( |
|
| 534 | + $comment->getParentId(), |
|
| 535 | + $comment->getCreationDateTime() |
|
| 536 | + ); |
|
| 537 | + $this->cache($comment); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + return $result; |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + /** |
|
| 544 | + * inserts the provided comment in the database |
|
| 545 | + * |
|
| 546 | + * @param IComment $comment |
|
| 547 | + * @return bool |
|
| 548 | + */ |
|
| 549 | + protected function insert(IComment &$comment) { |
|
| 550 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 551 | + $affectedRows = $qb |
|
| 552 | + ->insert('comments') |
|
| 553 | + ->values([ |
|
| 554 | + 'parent_id' => $qb->createNamedParameter($comment->getParentId()), |
|
| 555 | + 'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()), |
|
| 556 | + 'children_count' => $qb->createNamedParameter($comment->getChildrenCount()), |
|
| 557 | + 'actor_type' => $qb->createNamedParameter($comment->getActorType()), |
|
| 558 | + 'actor_id' => $qb->createNamedParameter($comment->getActorId()), |
|
| 559 | + 'message' => $qb->createNamedParameter($comment->getMessage()), |
|
| 560 | + 'verb' => $qb->createNamedParameter($comment->getVerb()), |
|
| 561 | + 'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'), |
|
| 562 | + 'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'), |
|
| 563 | + 'object_type' => $qb->createNamedParameter($comment->getObjectType()), |
|
| 564 | + 'object_id' => $qb->createNamedParameter($comment->getObjectId()), |
|
| 565 | + ]) |
|
| 566 | + ->execute(); |
|
| 567 | + |
|
| 568 | + if ($affectedRows > 0) { |
|
| 569 | + $comment->setId(strval($qb->getLastInsertId())); |
|
| 570 | + $this->sendEvent(CommentsEvent::EVENT_ADD, $comment); |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + return $affectedRows > 0; |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + /** |
|
| 577 | + * updates a Comment data row |
|
| 578 | + * |
|
| 579 | + * @param IComment $comment |
|
| 580 | + * @return bool |
|
| 581 | + * @throws NotFoundException |
|
| 582 | + */ |
|
| 583 | + protected function update(IComment $comment) { |
|
| 584 | + // for properly working preUpdate Events we need the old comments as is |
|
| 585 | + // in the DB and overcome caching. Also avoid that outdated information stays. |
|
| 586 | + $this->uncache($comment->getId()); |
|
| 587 | + $this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId())); |
|
| 588 | + $this->uncache($comment->getId()); |
|
| 589 | + |
|
| 590 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 591 | + $affectedRows = $qb |
|
| 592 | + ->update('comments') |
|
| 593 | + ->set('parent_id', $qb->createNamedParameter($comment->getParentId())) |
|
| 594 | + ->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId())) |
|
| 595 | + ->set('children_count', $qb->createNamedParameter($comment->getChildrenCount())) |
|
| 596 | + ->set('actor_type', $qb->createNamedParameter($comment->getActorType())) |
|
| 597 | + ->set('actor_id', $qb->createNamedParameter($comment->getActorId())) |
|
| 598 | + ->set('message', $qb->createNamedParameter($comment->getMessage())) |
|
| 599 | + ->set('verb', $qb->createNamedParameter($comment->getVerb())) |
|
| 600 | + ->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime')) |
|
| 601 | + ->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime')) |
|
| 602 | + ->set('object_type', $qb->createNamedParameter($comment->getObjectType())) |
|
| 603 | + ->set('object_id', $qb->createNamedParameter($comment->getObjectId())) |
|
| 604 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 605 | + ->setParameter('id', $comment->getId()) |
|
| 606 | + ->execute(); |
|
| 607 | + |
|
| 608 | + if ($affectedRows === 0) { |
|
| 609 | + throw new NotFoundException('Comment to update does ceased to exist'); |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment); |
|
| 613 | + |
|
| 614 | + return $affectedRows > 0; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * removes references to specific actor (e.g. on user delete) of a comment. |
|
| 619 | + * The comment itself must not get lost/deleted. |
|
| 620 | + * |
|
| 621 | + * @param string $actorType the actor type (e.g. 'users') |
|
| 622 | + * @param string $actorId a user id |
|
| 623 | + * @return boolean |
|
| 624 | + * @since 9.0.0 |
|
| 625 | + */ |
|
| 626 | + public function deleteReferencesOfActor($actorType, $actorId) { |
|
| 627 | + $this->checkRoleParameters('Actor', $actorType, $actorId); |
|
| 628 | + |
|
| 629 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 630 | + $affectedRows = $qb |
|
| 631 | + ->update('comments') |
|
| 632 | + ->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
| 633 | + ->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
| 634 | + ->where($qb->expr()->eq('actor_type', $qb->createParameter('type'))) |
|
| 635 | + ->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id'))) |
|
| 636 | + ->setParameter('type', $actorType) |
|
| 637 | + ->setParameter('id', $actorId) |
|
| 638 | + ->execute(); |
|
| 639 | + |
|
| 640 | + $this->commentsCache = []; |
|
| 641 | + |
|
| 642 | + return is_int($affectedRows); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + /** |
|
| 646 | + * deletes all comments made of a specific object (e.g. on file delete) |
|
| 647 | + * |
|
| 648 | + * @param string $objectType the object type (e.g. 'files') |
|
| 649 | + * @param string $objectId e.g. the file id |
|
| 650 | + * @return boolean |
|
| 651 | + * @since 9.0.0 |
|
| 652 | + */ |
|
| 653 | + public function deleteCommentsAtObject($objectType, $objectId) { |
|
| 654 | + $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 655 | + |
|
| 656 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 657 | + $affectedRows = $qb |
|
| 658 | + ->delete('comments') |
|
| 659 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 660 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 661 | + ->setParameter('type', $objectType) |
|
| 662 | + ->setParameter('id', $objectId) |
|
| 663 | + ->execute(); |
|
| 664 | + |
|
| 665 | + $this->commentsCache = []; |
|
| 666 | + |
|
| 667 | + return is_int($affectedRows); |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * deletes the read markers for the specified user |
|
| 672 | + * |
|
| 673 | + * @param \OCP\IUser $user |
|
| 674 | + * @return bool |
|
| 675 | + * @since 9.0.0 |
|
| 676 | + */ |
|
| 677 | + public function deleteReadMarksFromUser(IUser $user) { |
|
| 678 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 679 | + $query = $qb->delete('comments_read_markers') |
|
| 680 | + ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 681 | + ->setParameter('user_id', $user->getUID()); |
|
| 682 | + |
|
| 683 | + try { |
|
| 684 | + $affectedRows = $query->execute(); |
|
| 685 | + } catch (DriverException $e) { |
|
| 686 | + $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 687 | + return false; |
|
| 688 | + } |
|
| 689 | + return ($affectedRows > 0); |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * sets the read marker for a given file to the specified date for the |
|
| 694 | + * provided user |
|
| 695 | + * |
|
| 696 | + * @param string $objectType |
|
| 697 | + * @param string $objectId |
|
| 698 | + * @param \DateTime $dateTime |
|
| 699 | + * @param IUser $user |
|
| 700 | + * @since 9.0.0 |
|
| 701 | + * @suppress SqlInjectionChecker |
|
| 702 | + */ |
|
| 703 | + public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) { |
|
| 704 | + $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 705 | + |
|
| 706 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 707 | + $values = [ |
|
| 708 | + 'user_id' => $qb->createNamedParameter($user->getUID()), |
|
| 709 | + 'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'), |
|
| 710 | + 'object_type' => $qb->createNamedParameter($objectType), |
|
| 711 | + 'object_id' => $qb->createNamedParameter($objectId), |
|
| 712 | + ]; |
|
| 713 | + |
|
| 714 | + // Strategy: try to update, if this does not return affected rows, do an insert. |
|
| 715 | + $affectedRows = $qb |
|
| 716 | + ->update('comments_read_markers') |
|
| 717 | + ->set('user_id', $values['user_id']) |
|
| 718 | + ->set('marker_datetime', $values['marker_datetime']) |
|
| 719 | + ->set('object_type', $values['object_type']) |
|
| 720 | + ->set('object_id', $values['object_id']) |
|
| 721 | + ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 722 | + ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 723 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 724 | + ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
| 725 | + ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
| 726 | + ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
| 727 | + ->execute(); |
|
| 728 | + |
|
| 729 | + if ($affectedRows > 0) { |
|
| 730 | + return; |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + $qb->insert('comments_read_markers') |
|
| 734 | + ->values($values) |
|
| 735 | + ->execute(); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + /** |
|
| 739 | + * returns the read marker for a given file to the specified date for the |
|
| 740 | + * provided user. It returns null, when the marker is not present, i.e. |
|
| 741 | + * no comments were marked as read. |
|
| 742 | + * |
|
| 743 | + * @param string $objectType |
|
| 744 | + * @param string $objectId |
|
| 745 | + * @param IUser $user |
|
| 746 | + * @return \DateTime|null |
|
| 747 | + * @since 9.0.0 |
|
| 748 | + */ |
|
| 749 | + public function getReadMark($objectType, $objectId, IUser $user) { |
|
| 750 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 751 | + $resultStatement = $qb->select('marker_datetime') |
|
| 752 | + ->from('comments_read_markers') |
|
| 753 | + ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 754 | + ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 755 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 756 | + ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
| 757 | + ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
| 758 | + ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
| 759 | + ->execute(); |
|
| 760 | + |
|
| 761 | + $data = $resultStatement->fetch(); |
|
| 762 | + $resultStatement->closeCursor(); |
|
| 763 | + if (!$data || is_null($data['marker_datetime'])) { |
|
| 764 | + return null; |
|
| 765 | + } |
|
| 766 | + |
|
| 767 | + return new \DateTime($data['marker_datetime']); |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + /** |
|
| 771 | + * deletes the read markers on the specified object |
|
| 772 | + * |
|
| 773 | + * @param string $objectType |
|
| 774 | + * @param string $objectId |
|
| 775 | + * @return bool |
|
| 776 | + * @since 9.0.0 |
|
| 777 | + */ |
|
| 778 | + public function deleteReadMarksOnObject($objectType, $objectId) { |
|
| 779 | + $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 780 | + |
|
| 781 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 782 | + $query = $qb->delete('comments_read_markers') |
|
| 783 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 784 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 785 | + ->setParameter('object_type', $objectType) |
|
| 786 | + ->setParameter('object_id', $objectId); |
|
| 787 | + |
|
| 788 | + try { |
|
| 789 | + $affectedRows = $query->execute(); |
|
| 790 | + } catch (DriverException $e) { |
|
| 791 | + $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 792 | + return false; |
|
| 793 | + } |
|
| 794 | + return ($affectedRows > 0); |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + /** |
|
| 798 | + * registers an Entity to the manager, so event notifications can be send |
|
| 799 | + * to consumers of the comments infrastructure |
|
| 800 | + * |
|
| 801 | + * @param \Closure $closure |
|
| 802 | + */ |
|
| 803 | + public function registerEventHandler(\Closure $closure) { |
|
| 804 | + $this->eventHandlerClosures[] = $closure; |
|
| 805 | + $this->eventHandlers = []; |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + /** |
|
| 809 | + * registers a method that resolves an ID to a display name for a given type |
|
| 810 | + * |
|
| 811 | + * @param string $type |
|
| 812 | + * @param \Closure $closure |
|
| 813 | + * @throws \OutOfBoundsException |
|
| 814 | + * @since 11.0.0 |
|
| 815 | + * |
|
| 816 | + * Only one resolver shall be registered per type. Otherwise a |
|
| 817 | + * \OutOfBoundsException has to thrown. |
|
| 818 | + */ |
|
| 819 | + public function registerDisplayNameResolver($type, \Closure $closure) { |
|
| 820 | + if (!is_string($type)) { |
|
| 821 | + throw new \InvalidArgumentException('String expected.'); |
|
| 822 | + } |
|
| 823 | + if (isset($this->displayNameResolvers[$type])) { |
|
| 824 | + throw new \OutOfBoundsException('Displayname resolver for this type already registered'); |
|
| 825 | + } |
|
| 826 | + $this->displayNameResolvers[$type] = $closure; |
|
| 827 | + } |
|
| 828 | + |
|
| 829 | + /** |
|
| 830 | + * resolves a given ID of a given Type to a display name. |
|
| 831 | + * |
|
| 832 | + * @param string $type |
|
| 833 | + * @param string $id |
|
| 834 | + * @return string |
|
| 835 | + * @throws \OutOfBoundsException |
|
| 836 | + * @since 11.0.0 |
|
| 837 | + * |
|
| 838 | + * If a provided type was not registered, an \OutOfBoundsException shall |
|
| 839 | + * be thrown. It is upon the resolver discretion what to return of the |
|
| 840 | + * provided ID is unknown. It must be ensured that a string is returned. |
|
| 841 | + */ |
|
| 842 | + public function resolveDisplayName($type, $id) { |
|
| 843 | + if (!is_string($type)) { |
|
| 844 | + throw new \InvalidArgumentException('String expected.'); |
|
| 845 | + } |
|
| 846 | + if (!isset($this->displayNameResolvers[$type])) { |
|
| 847 | + throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
|
| 848 | + } |
|
| 849 | + return (string)$this->displayNameResolvers[$type]($id); |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + /** |
|
| 853 | + * returns valid, registered entities |
|
| 854 | + * |
|
| 855 | + * @return \OCP\Comments\ICommentsEventHandler[] |
|
| 856 | + */ |
|
| 857 | + private function getEventHandlers() { |
|
| 858 | + if (!empty($this->eventHandlers)) { |
|
| 859 | + return $this->eventHandlers; |
|
| 860 | + } |
|
| 861 | + |
|
| 862 | + $this->eventHandlers = []; |
|
| 863 | + foreach ($this->eventHandlerClosures as $name => $closure) { |
|
| 864 | + $entity = $closure(); |
|
| 865 | + if (!($entity instanceof ICommentsEventHandler)) { |
|
| 866 | + throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface'); |
|
| 867 | + } |
|
| 868 | + $this->eventHandlers[$name] = $entity; |
|
| 869 | + } |
|
| 870 | + |
|
| 871 | + return $this->eventHandlers; |
|
| 872 | + } |
|
| 873 | + |
|
| 874 | + /** |
|
| 875 | + * sends notifications to the registered entities |
|
| 876 | + * |
|
| 877 | + * @param $eventType |
|
| 878 | + * @param IComment $comment |
|
| 879 | + */ |
|
| 880 | + private function sendEvent($eventType, IComment $comment) { |
|
| 881 | + $entities = $this->getEventHandlers(); |
|
| 882 | + $event = new CommentsEvent($eventType, $comment); |
|
| 883 | + foreach ($entities as $entity) { |
|
| 884 | + $entity->handle($event); |
|
| 885 | + } |
|
| 886 | + } |
|
| 887 | 887 | } |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | !is_string($type) || empty($type) |
| 191 | 191 | || !is_string($id) || empty($id) |
| 192 | 192 | ) { |
| 193 | - throw new \InvalidArgumentException($role . ' parameters must be string and not empty'); |
|
| 193 | + throw new \InvalidArgumentException($role.' parameters must be string and not empty'); |
|
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | 196 | |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | $qb = $this->dbConn->getQueryBuilder(); |
| 414 | 414 | $query = $qb->select('f.fileid') |
| 415 | 415 | ->selectAlias( |
| 416 | - $qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'), |
|
| 416 | + $qb->createFunction('COUNT('.$qb->getColumnName('c.id').')'), |
|
| 417 | 417 | 'num_ids' |
| 418 | 418 | ) |
| 419 | 419 | ->from('comments', 'c') |
@@ -546,7 +546,7 @@ discard block |
||
| 546 | 546 | * @param IComment $comment |
| 547 | 547 | * @return bool |
| 548 | 548 | */ |
| 549 | - protected function insert(IComment &$comment) { |
|
| 549 | + protected function insert(IComment & $comment) { |
|
| 550 | 550 | $qb = $this->dbConn->getQueryBuilder(); |
| 551 | 551 | $affectedRows = $qb |
| 552 | 552 | ->insert('comments') |
@@ -846,7 +846,7 @@ discard block |
||
| 846 | 846 | if (!isset($this->displayNameResolvers[$type])) { |
| 847 | 847 | throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
| 848 | 848 | } |
| 849 | - return (string)$this->displayNameResolvers[$type]($id); |
|
| 849 | + return (string) $this->displayNameResolvers[$type]($id); |
|
| 850 | 850 | } |
| 851 | 851 | |
| 852 | 852 | /** |
@@ -60,2866 +60,2866 @@ |
||
| 60 | 60 | */ |
| 61 | 61 | class Share extends Constants { |
| 62 | 62 | |
| 63 | - /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask |
|
| 64 | - * Construct permissions for share() and setPermissions with Or (|) e.g. |
|
| 65 | - * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE |
|
| 66 | - * |
|
| 67 | - * Check if permission is granted with And (&) e.g. Check if delete is |
|
| 68 | - * granted: if ($permissions & PERMISSION_DELETE) |
|
| 69 | - * |
|
| 70 | - * Remove permissions with And (&) and Not (~) e.g. Remove the update |
|
| 71 | - * permission: $permissions &= ~PERMISSION_UPDATE |
|
| 72 | - * |
|
| 73 | - * Apps are required to handle permissions on their own, this class only |
|
| 74 | - * stores and manages the permissions of shares |
|
| 75 | - * @see lib/public/constants.php |
|
| 76 | - */ |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Register a sharing backend class that implements OCP\Share_Backend for an item type |
|
| 80 | - * @param string $itemType Item type |
|
| 81 | - * @param string $class Backend class |
|
| 82 | - * @param string $collectionOf (optional) Depends on item type |
|
| 83 | - * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files |
|
| 84 | - * @return boolean true if backend is registered or false if error |
|
| 85 | - */ |
|
| 86 | - public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) { |
|
| 87 | - if (self::isEnabled()) { |
|
| 88 | - if (!isset(self::$backendTypes[$itemType])) { |
|
| 89 | - self::$backendTypes[$itemType] = array( |
|
| 90 | - 'class' => $class, |
|
| 91 | - 'collectionOf' => $collectionOf, |
|
| 92 | - 'supportedFileExtensions' => $supportedFileExtensions |
|
| 93 | - ); |
|
| 94 | - if(count(self::$backendTypes) === 1) { |
|
| 95 | - Util::addScript('core', 'merged-share-backend'); |
|
| 96 | - \OC_Util::addStyle('core', 'share'); |
|
| 97 | - } |
|
| 98 | - return true; |
|
| 99 | - } |
|
| 100 | - \OCP\Util::writeLog('OCP\Share', |
|
| 101 | - 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class'] |
|
| 102 | - .' is already registered for '.$itemType, |
|
| 103 | - \OCP\Util::WARN); |
|
| 104 | - } |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Check if the Share API is enabled |
|
| 110 | - * @return boolean true if enabled or false |
|
| 111 | - * |
|
| 112 | - * The Share API is enabled by default if not configured |
|
| 113 | - */ |
|
| 114 | - public static function isEnabled() { |
|
| 115 | - if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_enabled', 'yes') == 'yes') { |
|
| 116 | - return true; |
|
| 117 | - } |
|
| 118 | - return false; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Find which users can access a shared item |
|
| 123 | - * @param string $path to the file |
|
| 124 | - * @param string $ownerUser owner of the file |
|
| 125 | - * @param IUserManager $userManager |
|
| 126 | - * @param ILogger $logger |
|
| 127 | - * @param boolean $includeOwner include owner to the list of users with access to the file |
|
| 128 | - * @param boolean $returnUserPaths Return an array with the user => path map |
|
| 129 | - * @param boolean $recursive take all parent folders into account (default true) |
|
| 130 | - * @return array |
|
| 131 | - * @note $path needs to be relative to user data dir, e.g. 'file.txt' |
|
| 132 | - * not '/admin/data/file.txt' |
|
| 133 | - * @throws \OC\User\NoUserException |
|
| 134 | - */ |
|
| 135 | - public static function getUsersSharingFile($path, |
|
| 136 | - $ownerUser, |
|
| 137 | - IUserManager $userManager, |
|
| 138 | - ILogger $logger, |
|
| 139 | - $includeOwner = false, |
|
| 140 | - $returnUserPaths = false, |
|
| 141 | - $recursive = true) { |
|
| 142 | - $userObject = $userManager->get($ownerUser); |
|
| 143 | - |
|
| 144 | - if (is_null($userObject)) { |
|
| 145 | - $logger->error( |
|
| 146 | - sprintf( |
|
| 147 | - 'Backends provided no user object for %s', |
|
| 148 | - $ownerUser |
|
| 149 | - ), |
|
| 150 | - [ |
|
| 151 | - 'app' => 'files', |
|
| 152 | - ] |
|
| 153 | - ); |
|
| 154 | - throw new \OC\User\NoUserException('Backends provided no user object'); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $ownerUser = $userObject->getUID(); |
|
| 158 | - |
|
| 159 | - Filesystem::initMountPoints($ownerUser); |
|
| 160 | - $shares = $sharePaths = $fileTargets = array(); |
|
| 161 | - $publicShare = false; |
|
| 162 | - $remoteShare = false; |
|
| 163 | - $source = -1; |
|
| 164 | - $cache = $mountPath = false; |
|
| 165 | - |
|
| 166 | - $view = new \OC\Files\View('/' . $ownerUser . '/files'); |
|
| 167 | - $meta = $view->getFileInfo($path); |
|
| 168 | - if ($meta) { |
|
| 169 | - $path = substr($meta->getPath(), strlen('/' . $ownerUser . '/files')); |
|
| 170 | - } else { |
|
| 171 | - // if the file doesn't exists yet we start with the parent folder |
|
| 172 | - $meta = $view->getFileInfo(dirname($path)); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - if($meta !== false) { |
|
| 176 | - $source = $meta['fileid']; |
|
| 177 | - $cache = new \OC\Files\Cache\Cache($meta['storage']); |
|
| 178 | - |
|
| 179 | - $mountPath = $meta->getMountPoint()->getMountPoint(); |
|
| 180 | - if ($mountPath !== false) { |
|
| 181 | - $mountPath = substr($mountPath, strlen('/' . $ownerUser . '/files')); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $paths = []; |
|
| 186 | - while ($source !== -1) { |
|
| 187 | - // Fetch all shares with another user |
|
| 188 | - if (!$returnUserPaths) { |
|
| 189 | - $query = \OC_DB::prepare( |
|
| 190 | - 'SELECT `share_with`, `file_source`, `file_target` |
|
| 63 | + /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask |
|
| 64 | + * Construct permissions for share() and setPermissions with Or (|) e.g. |
|
| 65 | + * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE |
|
| 66 | + * |
|
| 67 | + * Check if permission is granted with And (&) e.g. Check if delete is |
|
| 68 | + * granted: if ($permissions & PERMISSION_DELETE) |
|
| 69 | + * |
|
| 70 | + * Remove permissions with And (&) and Not (~) e.g. Remove the update |
|
| 71 | + * permission: $permissions &= ~PERMISSION_UPDATE |
|
| 72 | + * |
|
| 73 | + * Apps are required to handle permissions on their own, this class only |
|
| 74 | + * stores and manages the permissions of shares |
|
| 75 | + * @see lib/public/constants.php |
|
| 76 | + */ |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Register a sharing backend class that implements OCP\Share_Backend for an item type |
|
| 80 | + * @param string $itemType Item type |
|
| 81 | + * @param string $class Backend class |
|
| 82 | + * @param string $collectionOf (optional) Depends on item type |
|
| 83 | + * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files |
|
| 84 | + * @return boolean true if backend is registered or false if error |
|
| 85 | + */ |
|
| 86 | + public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) { |
|
| 87 | + if (self::isEnabled()) { |
|
| 88 | + if (!isset(self::$backendTypes[$itemType])) { |
|
| 89 | + self::$backendTypes[$itemType] = array( |
|
| 90 | + 'class' => $class, |
|
| 91 | + 'collectionOf' => $collectionOf, |
|
| 92 | + 'supportedFileExtensions' => $supportedFileExtensions |
|
| 93 | + ); |
|
| 94 | + if(count(self::$backendTypes) === 1) { |
|
| 95 | + Util::addScript('core', 'merged-share-backend'); |
|
| 96 | + \OC_Util::addStyle('core', 'share'); |
|
| 97 | + } |
|
| 98 | + return true; |
|
| 99 | + } |
|
| 100 | + \OCP\Util::writeLog('OCP\Share', |
|
| 101 | + 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class'] |
|
| 102 | + .' is already registered for '.$itemType, |
|
| 103 | + \OCP\Util::WARN); |
|
| 104 | + } |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Check if the Share API is enabled |
|
| 110 | + * @return boolean true if enabled or false |
|
| 111 | + * |
|
| 112 | + * The Share API is enabled by default if not configured |
|
| 113 | + */ |
|
| 114 | + public static function isEnabled() { |
|
| 115 | + if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_enabled', 'yes') == 'yes') { |
|
| 116 | + return true; |
|
| 117 | + } |
|
| 118 | + return false; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Find which users can access a shared item |
|
| 123 | + * @param string $path to the file |
|
| 124 | + * @param string $ownerUser owner of the file |
|
| 125 | + * @param IUserManager $userManager |
|
| 126 | + * @param ILogger $logger |
|
| 127 | + * @param boolean $includeOwner include owner to the list of users with access to the file |
|
| 128 | + * @param boolean $returnUserPaths Return an array with the user => path map |
|
| 129 | + * @param boolean $recursive take all parent folders into account (default true) |
|
| 130 | + * @return array |
|
| 131 | + * @note $path needs to be relative to user data dir, e.g. 'file.txt' |
|
| 132 | + * not '/admin/data/file.txt' |
|
| 133 | + * @throws \OC\User\NoUserException |
|
| 134 | + */ |
|
| 135 | + public static function getUsersSharingFile($path, |
|
| 136 | + $ownerUser, |
|
| 137 | + IUserManager $userManager, |
|
| 138 | + ILogger $logger, |
|
| 139 | + $includeOwner = false, |
|
| 140 | + $returnUserPaths = false, |
|
| 141 | + $recursive = true) { |
|
| 142 | + $userObject = $userManager->get($ownerUser); |
|
| 143 | + |
|
| 144 | + if (is_null($userObject)) { |
|
| 145 | + $logger->error( |
|
| 146 | + sprintf( |
|
| 147 | + 'Backends provided no user object for %s', |
|
| 148 | + $ownerUser |
|
| 149 | + ), |
|
| 150 | + [ |
|
| 151 | + 'app' => 'files', |
|
| 152 | + ] |
|
| 153 | + ); |
|
| 154 | + throw new \OC\User\NoUserException('Backends provided no user object'); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $ownerUser = $userObject->getUID(); |
|
| 158 | + |
|
| 159 | + Filesystem::initMountPoints($ownerUser); |
|
| 160 | + $shares = $sharePaths = $fileTargets = array(); |
|
| 161 | + $publicShare = false; |
|
| 162 | + $remoteShare = false; |
|
| 163 | + $source = -1; |
|
| 164 | + $cache = $mountPath = false; |
|
| 165 | + |
|
| 166 | + $view = new \OC\Files\View('/' . $ownerUser . '/files'); |
|
| 167 | + $meta = $view->getFileInfo($path); |
|
| 168 | + if ($meta) { |
|
| 169 | + $path = substr($meta->getPath(), strlen('/' . $ownerUser . '/files')); |
|
| 170 | + } else { |
|
| 171 | + // if the file doesn't exists yet we start with the parent folder |
|
| 172 | + $meta = $view->getFileInfo(dirname($path)); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + if($meta !== false) { |
|
| 176 | + $source = $meta['fileid']; |
|
| 177 | + $cache = new \OC\Files\Cache\Cache($meta['storage']); |
|
| 178 | + |
|
| 179 | + $mountPath = $meta->getMountPoint()->getMountPoint(); |
|
| 180 | + if ($mountPath !== false) { |
|
| 181 | + $mountPath = substr($mountPath, strlen('/' . $ownerUser . '/files')); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $paths = []; |
|
| 186 | + while ($source !== -1) { |
|
| 187 | + // Fetch all shares with another user |
|
| 188 | + if (!$returnUserPaths) { |
|
| 189 | + $query = \OC_DB::prepare( |
|
| 190 | + 'SELECT `share_with`, `file_source`, `file_target` |
|
| 191 | 191 | FROM |
| 192 | 192 | `*PREFIX*share` |
| 193 | 193 | WHERE |
| 194 | 194 | `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')' |
| 195 | - ); |
|
| 196 | - $result = $query->execute(array($source, self::SHARE_TYPE_USER)); |
|
| 197 | - } else { |
|
| 198 | - $query = \OC_DB::prepare( |
|
| 199 | - 'SELECT `share_with`, `file_source`, `file_target` |
|
| 195 | + ); |
|
| 196 | + $result = $query->execute(array($source, self::SHARE_TYPE_USER)); |
|
| 197 | + } else { |
|
| 198 | + $query = \OC_DB::prepare( |
|
| 199 | + 'SELECT `share_with`, `file_source`, `file_target` |
|
| 200 | 200 | FROM |
| 201 | 201 | `*PREFIX*share` |
| 202 | 202 | WHERE |
| 203 | 203 | `item_source` = ? AND `share_type` IN (?, ?) AND `item_type` IN (\'file\', \'folder\')' |
| 204 | - ); |
|
| 205 | - $result = $query->execute(array($source, self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique)); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - if (\OCP\DB::isError($result)) { |
|
| 209 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 210 | - } else { |
|
| 211 | - while ($row = $result->fetchRow()) { |
|
| 212 | - $shares[] = $row['share_with']; |
|
| 213 | - if ($returnUserPaths) { |
|
| 214 | - $fileTargets[(int) $row['file_source']][$row['share_with']] = $row; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // We also need to take group shares into account |
|
| 220 | - $query = \OC_DB::prepare( |
|
| 221 | - 'SELECT `share_with`, `file_source`, `file_target` |
|
| 204 | + ); |
|
| 205 | + $result = $query->execute(array($source, self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique)); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + if (\OCP\DB::isError($result)) { |
|
| 209 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 210 | + } else { |
|
| 211 | + while ($row = $result->fetchRow()) { |
|
| 212 | + $shares[] = $row['share_with']; |
|
| 213 | + if ($returnUserPaths) { |
|
| 214 | + $fileTargets[(int) $row['file_source']][$row['share_with']] = $row; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // We also need to take group shares into account |
|
| 220 | + $query = \OC_DB::prepare( |
|
| 221 | + 'SELECT `share_with`, `file_source`, `file_target` |
|
| 222 | 222 | FROM |
| 223 | 223 | `*PREFIX*share` |
| 224 | 224 | WHERE |
| 225 | 225 | `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')' |
| 226 | - ); |
|
| 227 | - |
|
| 228 | - $result = $query->execute(array($source, self::SHARE_TYPE_GROUP)); |
|
| 229 | - |
|
| 230 | - if (\OCP\DB::isError($result)) { |
|
| 231 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 232 | - } else { |
|
| 233 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 234 | - while ($row = $result->fetchRow()) { |
|
| 235 | - |
|
| 236 | - $usersInGroup = []; |
|
| 237 | - $group = $groupManager->get($row['share_with']); |
|
| 238 | - if ($group) { |
|
| 239 | - $users = $group->searchUsers('', -1, 0); |
|
| 240 | - $userIds = array(); |
|
| 241 | - foreach ($users as $user) { |
|
| 242 | - $userIds[] = $user->getUID(); |
|
| 243 | - } |
|
| 244 | - $usersInGroup = $userIds; |
|
| 245 | - } |
|
| 246 | - $shares = array_merge($shares, $usersInGroup); |
|
| 247 | - if ($returnUserPaths) { |
|
| 248 | - foreach ($usersInGroup as $user) { |
|
| 249 | - if (!isset($fileTargets[(int) $row['file_source']][$user])) { |
|
| 250 | - // When the user already has an entry for this file source |
|
| 251 | - // the file is either shared directly with him as well, or |
|
| 252 | - // he has an exception entry (because of naming conflict). |
|
| 253 | - $fileTargets[(int) $row['file_source']][$user] = $row; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - //check for public link shares |
|
| 261 | - if (!$publicShare) { |
|
| 262 | - $query = \OC_DB::prepare(' |
|
| 226 | + ); |
|
| 227 | + |
|
| 228 | + $result = $query->execute(array($source, self::SHARE_TYPE_GROUP)); |
|
| 229 | + |
|
| 230 | + if (\OCP\DB::isError($result)) { |
|
| 231 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 232 | + } else { |
|
| 233 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 234 | + while ($row = $result->fetchRow()) { |
|
| 235 | + |
|
| 236 | + $usersInGroup = []; |
|
| 237 | + $group = $groupManager->get($row['share_with']); |
|
| 238 | + if ($group) { |
|
| 239 | + $users = $group->searchUsers('', -1, 0); |
|
| 240 | + $userIds = array(); |
|
| 241 | + foreach ($users as $user) { |
|
| 242 | + $userIds[] = $user->getUID(); |
|
| 243 | + } |
|
| 244 | + $usersInGroup = $userIds; |
|
| 245 | + } |
|
| 246 | + $shares = array_merge($shares, $usersInGroup); |
|
| 247 | + if ($returnUserPaths) { |
|
| 248 | + foreach ($usersInGroup as $user) { |
|
| 249 | + if (!isset($fileTargets[(int) $row['file_source']][$user])) { |
|
| 250 | + // When the user already has an entry for this file source |
|
| 251 | + // the file is either shared directly with him as well, or |
|
| 252 | + // he has an exception entry (because of naming conflict). |
|
| 253 | + $fileTargets[(int) $row['file_source']][$user] = $row; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + //check for public link shares |
|
| 261 | + if (!$publicShare) { |
|
| 262 | + $query = \OC_DB::prepare(' |
|
| 263 | 263 | SELECT `share_with` |
| 264 | 264 | FROM `*PREFIX*share` |
| 265 | 265 | WHERE `item_source` = ? AND `share_type` IN (?, ?) AND `item_type` IN (\'file\', \'folder\')', 1 |
| 266 | - ); |
|
| 267 | - |
|
| 268 | - $result = $query->execute(array($source, self::SHARE_TYPE_LINK, self::SHARE_TYPE_EMAIL)); |
|
| 269 | - |
|
| 270 | - if (\OCP\DB::isError($result)) { |
|
| 271 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 272 | - } else { |
|
| 273 | - if ($result->fetchRow()) { |
|
| 274 | - $publicShare = true; |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - //check for remote share |
|
| 280 | - if (!$remoteShare) { |
|
| 281 | - $query = \OC_DB::prepare(' |
|
| 266 | + ); |
|
| 267 | + |
|
| 268 | + $result = $query->execute(array($source, self::SHARE_TYPE_LINK, self::SHARE_TYPE_EMAIL)); |
|
| 269 | + |
|
| 270 | + if (\OCP\DB::isError($result)) { |
|
| 271 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 272 | + } else { |
|
| 273 | + if ($result->fetchRow()) { |
|
| 274 | + $publicShare = true; |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + //check for remote share |
|
| 280 | + if (!$remoteShare) { |
|
| 281 | + $query = \OC_DB::prepare(' |
|
| 282 | 282 | SELECT `share_with` |
| 283 | 283 | FROM `*PREFIX*share` |
| 284 | 284 | WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')', 1 |
| 285 | - ); |
|
| 286 | - |
|
| 287 | - $result = $query->execute(array($source, self::SHARE_TYPE_REMOTE)); |
|
| 288 | - |
|
| 289 | - if (\OCP\DB::isError($result)) { |
|
| 290 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 291 | - } else { |
|
| 292 | - if ($result->fetchRow()) { |
|
| 293 | - $remoteShare = true; |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // let's get the parent for the next round |
|
| 299 | - $meta = $cache->get((int)$source); |
|
| 300 | - if ($recursive === true && $meta !== false) { |
|
| 301 | - $paths[$source] = $meta['path']; |
|
| 302 | - $source = (int)$meta['parent']; |
|
| 303 | - } else { |
|
| 304 | - $source = -1; |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - // Include owner in list of users, if requested |
|
| 309 | - if ($includeOwner) { |
|
| 310 | - $shares[] = $ownerUser; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - if ($returnUserPaths) { |
|
| 314 | - $fileTargetIDs = array_keys($fileTargets); |
|
| 315 | - $fileTargetIDs = array_unique($fileTargetIDs); |
|
| 316 | - |
|
| 317 | - if (!empty($fileTargetIDs)) { |
|
| 318 | - $query = \OC_DB::prepare( |
|
| 319 | - 'SELECT `fileid`, `path` |
|
| 285 | + ); |
|
| 286 | + |
|
| 287 | + $result = $query->execute(array($source, self::SHARE_TYPE_REMOTE)); |
|
| 288 | + |
|
| 289 | + if (\OCP\DB::isError($result)) { |
|
| 290 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 291 | + } else { |
|
| 292 | + if ($result->fetchRow()) { |
|
| 293 | + $remoteShare = true; |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // let's get the parent for the next round |
|
| 299 | + $meta = $cache->get((int)$source); |
|
| 300 | + if ($recursive === true && $meta !== false) { |
|
| 301 | + $paths[$source] = $meta['path']; |
|
| 302 | + $source = (int)$meta['parent']; |
|
| 303 | + } else { |
|
| 304 | + $source = -1; |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + // Include owner in list of users, if requested |
|
| 309 | + if ($includeOwner) { |
|
| 310 | + $shares[] = $ownerUser; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + if ($returnUserPaths) { |
|
| 314 | + $fileTargetIDs = array_keys($fileTargets); |
|
| 315 | + $fileTargetIDs = array_unique($fileTargetIDs); |
|
| 316 | + |
|
| 317 | + if (!empty($fileTargetIDs)) { |
|
| 318 | + $query = \OC_DB::prepare( |
|
| 319 | + 'SELECT `fileid`, `path` |
|
| 320 | 320 | FROM `*PREFIX*filecache` |
| 321 | 321 | WHERE `fileid` IN (' . implode(',', $fileTargetIDs) . ')' |
| 322 | - ); |
|
| 323 | - $result = $query->execute(); |
|
| 324 | - |
|
| 325 | - if (\OCP\DB::isError($result)) { |
|
| 326 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 327 | - } else { |
|
| 328 | - while ($row = $result->fetchRow()) { |
|
| 329 | - foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { |
|
| 330 | - if ($mountPath !== false) { |
|
| 331 | - $sharedPath = $shareData['file_target']; |
|
| 332 | - $sharedPath .= substr($path, strlen($mountPath) + strlen($paths[$row['fileid']])); |
|
| 333 | - $sharePaths[$uid] = $sharedPath; |
|
| 334 | - } else { |
|
| 335 | - $sharedPath = $shareData['file_target']; |
|
| 336 | - $sharedPath .= substr($path, strlen($row['path']) -5); |
|
| 337 | - $sharePaths[$uid] = $sharedPath; |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - $result->closeCursor(); |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - if ($includeOwner) { |
|
| 346 | - $sharePaths[$ownerUser] = $path; |
|
| 347 | - } else { |
|
| 348 | - unset($sharePaths[$ownerUser]); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - return $sharePaths; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - return array('users' => array_unique($shares), 'public' => $publicShare, 'remote' => $remoteShare); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Get the items of item type shared with the current user |
|
| 359 | - * @param string $itemType |
|
| 360 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 361 | - * @param mixed $parameters (optional) |
|
| 362 | - * @param int $limit Number of items to return (optional) Returns all by default |
|
| 363 | - * @param boolean $includeCollections (optional) |
|
| 364 | - * @return mixed Return depends on format |
|
| 365 | - */ |
|
| 366 | - public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE, |
|
| 367 | - $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 368 | - return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, |
|
| 369 | - $parameters, $limit, $includeCollections); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Get the items of item type shared with a user |
|
| 374 | - * @param string $itemType |
|
| 375 | - * @param string $user id for which user we want the shares |
|
| 376 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 377 | - * @param mixed $parameters (optional) |
|
| 378 | - * @param int $limit Number of items to return (optional) Returns all by default |
|
| 379 | - * @param boolean $includeCollections (optional) |
|
| 380 | - * @return mixed Return depends on format |
|
| 381 | - */ |
|
| 382 | - public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE, |
|
| 383 | - $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 384 | - return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format, |
|
| 385 | - $parameters, $limit, $includeCollections); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Get the item of item type shared with the current user |
|
| 390 | - * @param string $itemType |
|
| 391 | - * @param string $itemTarget |
|
| 392 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 393 | - * @param mixed $parameters (optional) |
|
| 394 | - * @param boolean $includeCollections (optional) |
|
| 395 | - * @return mixed Return depends on format |
|
| 396 | - */ |
|
| 397 | - public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE, |
|
| 398 | - $parameters = null, $includeCollections = false) { |
|
| 399 | - return self::getItems($itemType, $itemTarget, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, |
|
| 400 | - $parameters, 1, $includeCollections); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Get the item of item type shared with a given user by source |
|
| 405 | - * @param string $itemType |
|
| 406 | - * @param string $itemSource |
|
| 407 | - * @param string $user User to whom the item was shared |
|
| 408 | - * @param string $owner Owner of the share |
|
| 409 | - * @param int $shareType only look for a specific share type |
|
| 410 | - * @return array Return list of items with file_target, permissions and expiration |
|
| 411 | - */ |
|
| 412 | - public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null, $shareType = null) { |
|
| 413 | - $shares = array(); |
|
| 414 | - $fileDependent = false; |
|
| 415 | - |
|
| 416 | - $where = 'WHERE'; |
|
| 417 | - $fileDependentWhere = ''; |
|
| 418 | - if ($itemType === 'file' || $itemType === 'folder') { |
|
| 419 | - $fileDependent = true; |
|
| 420 | - $column = 'file_source'; |
|
| 421 | - $fileDependentWhere = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 422 | - $fileDependentWhere .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 423 | - } else { |
|
| 424 | - $column = 'item_source'; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - $select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent); |
|
| 428 | - |
|
| 429 | - $where .= ' `' . $column . '` = ? AND `item_type` = ? '; |
|
| 430 | - $arguments = array($itemSource, $itemType); |
|
| 431 | - // for link shares $user === null |
|
| 432 | - if ($user !== null) { |
|
| 433 | - $where .= ' AND `share_with` = ? '; |
|
| 434 | - $arguments[] = $user; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - if ($shareType !== null) { |
|
| 438 | - $where .= ' AND `share_type` = ? '; |
|
| 439 | - $arguments[] = $shareType; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - if ($owner !== null) { |
|
| 443 | - $where .= ' AND `uid_owner` = ? '; |
|
| 444 | - $arguments[] = $owner; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where); |
|
| 448 | - |
|
| 449 | - $result = \OC_DB::executeAudited($query, $arguments); |
|
| 450 | - |
|
| 451 | - while ($row = $result->fetchRow()) { |
|
| 452 | - if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 453 | - continue; |
|
| 454 | - } |
|
| 455 | - if ($fileDependent && (int)$row['file_parent'] === -1) { |
|
| 456 | - // if it is a mount point we need to get the path from the mount manager |
|
| 457 | - $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 458 | - $mountPoint = $mountManager->findByStorageId($row['storage_id']); |
|
| 459 | - if (!empty($mountPoint)) { |
|
| 460 | - $path = $mountPoint[0]->getMountPoint(); |
|
| 461 | - $path = trim($path, '/'); |
|
| 462 | - $path = substr($path, strlen($owner) + 1); //normalize path to 'files/foo.txt` |
|
| 463 | - $row['path'] = $path; |
|
| 464 | - } else { |
|
| 465 | - \OC::$server->getLogger()->warning( |
|
| 466 | - 'Could not resolve mount point for ' . $row['storage_id'], |
|
| 467 | - ['app' => 'OCP\Share'] |
|
| 468 | - ); |
|
| 469 | - } |
|
| 470 | - } |
|
| 471 | - $shares[] = $row; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - //if didn't found a result than let's look for a group share. |
|
| 475 | - if(empty($shares) && $user !== null) { |
|
| 476 | - $userObject = \OC::$server->getUserManager()->get($user); |
|
| 477 | - $groups = []; |
|
| 478 | - if ($userObject) { |
|
| 479 | - $groups = \OC::$server->getGroupManager()->getUserGroupIds($userObject); |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - if (!empty($groups)) { |
|
| 483 | - $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)'; |
|
| 484 | - $arguments = array($itemSource, $itemType, $groups); |
|
| 485 | - $types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 486 | - |
|
| 487 | - if ($owner !== null) { |
|
| 488 | - $where .= ' AND `uid_owner` = ?'; |
|
| 489 | - $arguments[] = $owner; |
|
| 490 | - $types[] = null; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - // TODO: inject connection, hopefully one day in the future when this |
|
| 494 | - // class isn't static anymore... |
|
| 495 | - $conn = \OC::$server->getDatabaseConnection(); |
|
| 496 | - $result = $conn->executeQuery( |
|
| 497 | - 'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where, |
|
| 498 | - $arguments, |
|
| 499 | - $types |
|
| 500 | - ); |
|
| 501 | - |
|
| 502 | - while ($row = $result->fetch()) { |
|
| 503 | - $shares[] = $row; |
|
| 504 | - } |
|
| 505 | - } |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - return $shares; |
|
| 509 | - |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * Get the item of item type shared with the current user by source |
|
| 514 | - * @param string $itemType |
|
| 515 | - * @param string $itemSource |
|
| 516 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 517 | - * @param mixed $parameters |
|
| 518 | - * @param boolean $includeCollections |
|
| 519 | - * @param string $shareWith (optional) define against which user should be checked, default: current user |
|
| 520 | - * @return array |
|
| 521 | - */ |
|
| 522 | - public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 523 | - $parameters = null, $includeCollections = false, $shareWith = null) { |
|
| 524 | - $shareWith = ($shareWith === null) ? \OC_User::getUser() : $shareWith; |
|
| 525 | - return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, $format, |
|
| 526 | - $parameters, 1, $includeCollections, true); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Get the item of item type shared by a link |
|
| 531 | - * @param string $itemType |
|
| 532 | - * @param string $itemSource |
|
| 533 | - * @param string $uidOwner Owner of link |
|
| 534 | - * @return array |
|
| 535 | - */ |
|
| 536 | - public static function getItemSharedWithByLink($itemType, $itemSource, $uidOwner) { |
|
| 537 | - return self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, $uidOwner, self::FORMAT_NONE, |
|
| 538 | - null, 1); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - /** |
|
| 542 | - * Based on the given token the share information will be returned - password protected shares will be verified |
|
| 543 | - * @param string $token |
|
| 544 | - * @param bool $checkPasswordProtection |
|
| 545 | - * @return array|boolean false will be returned in case the token is unknown or unauthorized |
|
| 546 | - */ |
|
| 547 | - public static function getShareByToken($token, $checkPasswordProtection = true) { |
|
| 548 | - $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `token` = ?', 1); |
|
| 549 | - $result = $query->execute(array($token)); |
|
| 550 | - if ($result === false) { |
|
| 551 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage() . ', token=' . $token, \OCP\Util::ERROR); |
|
| 552 | - } |
|
| 553 | - $row = $result->fetchRow(); |
|
| 554 | - if ($row === false) { |
|
| 555 | - return false; |
|
| 556 | - } |
|
| 557 | - if (is_array($row) and self::expireItem($row)) { |
|
| 558 | - return false; |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - // password protected shares need to be authenticated |
|
| 562 | - if ($checkPasswordProtection && !\OCP\Share::checkPasswordProtectedShare($row)) { |
|
| 563 | - return false; |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - return $row; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * resolves reshares down to the last real share |
|
| 571 | - * @param array $linkItem |
|
| 572 | - * @return array file owner |
|
| 573 | - */ |
|
| 574 | - public static function resolveReShare($linkItem) |
|
| 575 | - { |
|
| 576 | - if (isset($linkItem['parent'])) { |
|
| 577 | - $parent = $linkItem['parent']; |
|
| 578 | - while (isset($parent)) { |
|
| 579 | - $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ?', 1); |
|
| 580 | - $item = $query->execute(array($parent))->fetchRow(); |
|
| 581 | - if (isset($item['parent'])) { |
|
| 582 | - $parent = $item['parent']; |
|
| 583 | - } else { |
|
| 584 | - return $item; |
|
| 585 | - } |
|
| 586 | - } |
|
| 587 | - } |
|
| 588 | - return $linkItem; |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - |
|
| 592 | - /** |
|
| 593 | - * Get the shared items of item type owned by the current user |
|
| 594 | - * @param string $itemType |
|
| 595 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 596 | - * @param mixed $parameters |
|
| 597 | - * @param int $limit Number of items to return (optional) Returns all by default |
|
| 598 | - * @param boolean $includeCollections |
|
| 599 | - * @return mixed Return depends on format |
|
| 600 | - */ |
|
| 601 | - public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null, |
|
| 602 | - $limit = -1, $includeCollections = false) { |
|
| 603 | - return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format, |
|
| 604 | - $parameters, $limit, $includeCollections); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * Get the shared item of item type owned by the current user |
|
| 609 | - * @param string $itemType |
|
| 610 | - * @param string $itemSource |
|
| 611 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 612 | - * @param mixed $parameters |
|
| 613 | - * @param boolean $includeCollections |
|
| 614 | - * @return mixed Return depends on format |
|
| 615 | - */ |
|
| 616 | - public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 617 | - $parameters = null, $includeCollections = false) { |
|
| 618 | - return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format, |
|
| 619 | - $parameters, -1, $includeCollections); |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - /** |
|
| 623 | - * Get all users an item is shared with |
|
| 624 | - * @param string $itemType |
|
| 625 | - * @param string $itemSource |
|
| 626 | - * @param string $uidOwner |
|
| 627 | - * @param boolean $includeCollections |
|
| 628 | - * @param boolean $checkExpireDate |
|
| 629 | - * @return array Return array of users |
|
| 630 | - */ |
|
| 631 | - public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections = false, $checkExpireDate = true) { |
|
| 632 | - |
|
| 633 | - $users = array(); |
|
| 634 | - $items = self::getItems($itemType, $itemSource, null, null, $uidOwner, self::FORMAT_NONE, null, -1, $includeCollections, false, $checkExpireDate); |
|
| 635 | - if ($items) { |
|
| 636 | - foreach ($items as $item) { |
|
| 637 | - if ((int)$item['share_type'] === self::SHARE_TYPE_USER) { |
|
| 638 | - $users[] = $item['share_with']; |
|
| 639 | - } else if ((int)$item['share_type'] === self::SHARE_TYPE_GROUP) { |
|
| 640 | - |
|
| 641 | - $group = \OC::$server->getGroupManager()->get($item['share_with']); |
|
| 642 | - $userIds = []; |
|
| 643 | - if ($group) { |
|
| 644 | - $users = $group->searchUsers('', -1, 0); |
|
| 645 | - foreach ($users as $user) { |
|
| 646 | - $userIds[] = $user->getUID(); |
|
| 647 | - } |
|
| 648 | - return $userIds; |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - $users = array_merge($users, $userIds); |
|
| 652 | - } |
|
| 653 | - } |
|
| 654 | - } |
|
| 655 | - return $users; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Share an item with a user, group, or via private link |
|
| 660 | - * @param string $itemType |
|
| 661 | - * @param string $itemSource |
|
| 662 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 663 | - * @param string $shareWith User or group the item is being shared with |
|
| 664 | - * @param int $permissions CRUDS |
|
| 665 | - * @param string $itemSourceName |
|
| 666 | - * @param \DateTime $expirationDate |
|
| 667 | - * @param bool $passwordChanged |
|
| 668 | - * @return boolean|string Returns true on success or false on failure, Returns token on success for links |
|
| 669 | - * @throws \OC\HintException when the share type is remote and the shareWith is invalid |
|
| 670 | - * @throws \Exception |
|
| 671 | - */ |
|
| 672 | - public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) { |
|
| 673 | - |
|
| 674 | - $backend = self::getBackend($itemType); |
|
| 675 | - $l = \OC::$server->getL10N('lib'); |
|
| 676 | - |
|
| 677 | - if ($backend->isShareTypeAllowed($shareType) === false) { |
|
| 678 | - $message = 'Sharing %s failed, because the backend does not allow shares from type %i'; |
|
| 679 | - $message_t = $l->t('Sharing %s failed, because the backend does not allow shares from type %i', array($itemSourceName, $shareType)); |
|
| 680 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareType), \OCP\Util::DEBUG); |
|
| 681 | - throw new \Exception($message_t); |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - $uidOwner = \OC_User::getUser(); |
|
| 685 | - $shareWithinGroupOnly = self::shareWithGroupMembersOnly(); |
|
| 686 | - |
|
| 687 | - if (is_null($itemSourceName)) { |
|
| 688 | - $itemSourceName = $itemSource; |
|
| 689 | - } |
|
| 690 | - $itemName = $itemSourceName; |
|
| 691 | - |
|
| 692 | - // check if file can be shared |
|
| 693 | - if ($itemType === 'file' or $itemType === 'folder') { |
|
| 694 | - $path = \OC\Files\Filesystem::getPath($itemSource); |
|
| 695 | - $itemName = $path; |
|
| 696 | - |
|
| 697 | - // verify that the file exists before we try to share it |
|
| 698 | - if (!$path) { |
|
| 699 | - $message = 'Sharing %s failed, because the file does not exist'; |
|
| 700 | - $message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName)); |
|
| 701 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 702 | - throw new \Exception($message_t); |
|
| 703 | - } |
|
| 704 | - // verify that the user has share permission |
|
| 705 | - if (!\OC\Files\Filesystem::isSharable($path) || \OCP\Util::isSharingDisabledForUser()) { |
|
| 706 | - $message = 'You are not allowed to share %s'; |
|
| 707 | - $message_t = $l->t('You are not allowed to share %s', [$path]); |
|
| 708 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $path), \OCP\Util::DEBUG); |
|
| 709 | - throw new \Exception($message_t); |
|
| 710 | - } |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - //verify that we don't share a folder which already contains a share mount point |
|
| 714 | - if ($itemType === 'folder') { |
|
| 715 | - $path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/'; |
|
| 716 | - $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 717 | - $mounts = $mountManager->findIn($path); |
|
| 718 | - foreach ($mounts as $mount) { |
|
| 719 | - if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 720 | - $message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!'; |
|
| 721 | - \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 722 | - throw new \Exception($message); |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - } |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - // single file shares should never have delete permissions |
|
| 729 | - if ($itemType === 'file') { |
|
| 730 | - $permissions = (int)$permissions & ~\OCP\Constants::PERMISSION_DELETE; |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - //Validate expirationDate |
|
| 734 | - if ($expirationDate !== null) { |
|
| 735 | - try { |
|
| 736 | - /* |
|
| 322 | + ); |
|
| 323 | + $result = $query->execute(); |
|
| 324 | + |
|
| 325 | + if (\OCP\DB::isError($result)) { |
|
| 326 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 327 | + } else { |
|
| 328 | + while ($row = $result->fetchRow()) { |
|
| 329 | + foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { |
|
| 330 | + if ($mountPath !== false) { |
|
| 331 | + $sharedPath = $shareData['file_target']; |
|
| 332 | + $sharedPath .= substr($path, strlen($mountPath) + strlen($paths[$row['fileid']])); |
|
| 333 | + $sharePaths[$uid] = $sharedPath; |
|
| 334 | + } else { |
|
| 335 | + $sharedPath = $shareData['file_target']; |
|
| 336 | + $sharedPath .= substr($path, strlen($row['path']) -5); |
|
| 337 | + $sharePaths[$uid] = $sharedPath; |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + $result->closeCursor(); |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + if ($includeOwner) { |
|
| 346 | + $sharePaths[$ownerUser] = $path; |
|
| 347 | + } else { |
|
| 348 | + unset($sharePaths[$ownerUser]); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + return $sharePaths; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + return array('users' => array_unique($shares), 'public' => $publicShare, 'remote' => $remoteShare); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Get the items of item type shared with the current user |
|
| 359 | + * @param string $itemType |
|
| 360 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 361 | + * @param mixed $parameters (optional) |
|
| 362 | + * @param int $limit Number of items to return (optional) Returns all by default |
|
| 363 | + * @param boolean $includeCollections (optional) |
|
| 364 | + * @return mixed Return depends on format |
|
| 365 | + */ |
|
| 366 | + public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE, |
|
| 367 | + $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 368 | + return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, |
|
| 369 | + $parameters, $limit, $includeCollections); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Get the items of item type shared with a user |
|
| 374 | + * @param string $itemType |
|
| 375 | + * @param string $user id for which user we want the shares |
|
| 376 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 377 | + * @param mixed $parameters (optional) |
|
| 378 | + * @param int $limit Number of items to return (optional) Returns all by default |
|
| 379 | + * @param boolean $includeCollections (optional) |
|
| 380 | + * @return mixed Return depends on format |
|
| 381 | + */ |
|
| 382 | + public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE, |
|
| 383 | + $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 384 | + return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format, |
|
| 385 | + $parameters, $limit, $includeCollections); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Get the item of item type shared with the current user |
|
| 390 | + * @param string $itemType |
|
| 391 | + * @param string $itemTarget |
|
| 392 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 393 | + * @param mixed $parameters (optional) |
|
| 394 | + * @param boolean $includeCollections (optional) |
|
| 395 | + * @return mixed Return depends on format |
|
| 396 | + */ |
|
| 397 | + public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE, |
|
| 398 | + $parameters = null, $includeCollections = false) { |
|
| 399 | + return self::getItems($itemType, $itemTarget, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, |
|
| 400 | + $parameters, 1, $includeCollections); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Get the item of item type shared with a given user by source |
|
| 405 | + * @param string $itemType |
|
| 406 | + * @param string $itemSource |
|
| 407 | + * @param string $user User to whom the item was shared |
|
| 408 | + * @param string $owner Owner of the share |
|
| 409 | + * @param int $shareType only look for a specific share type |
|
| 410 | + * @return array Return list of items with file_target, permissions and expiration |
|
| 411 | + */ |
|
| 412 | + public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null, $shareType = null) { |
|
| 413 | + $shares = array(); |
|
| 414 | + $fileDependent = false; |
|
| 415 | + |
|
| 416 | + $where = 'WHERE'; |
|
| 417 | + $fileDependentWhere = ''; |
|
| 418 | + if ($itemType === 'file' || $itemType === 'folder') { |
|
| 419 | + $fileDependent = true; |
|
| 420 | + $column = 'file_source'; |
|
| 421 | + $fileDependentWhere = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 422 | + $fileDependentWhere .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 423 | + } else { |
|
| 424 | + $column = 'item_source'; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + $select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent); |
|
| 428 | + |
|
| 429 | + $where .= ' `' . $column . '` = ? AND `item_type` = ? '; |
|
| 430 | + $arguments = array($itemSource, $itemType); |
|
| 431 | + // for link shares $user === null |
|
| 432 | + if ($user !== null) { |
|
| 433 | + $where .= ' AND `share_with` = ? '; |
|
| 434 | + $arguments[] = $user; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + if ($shareType !== null) { |
|
| 438 | + $where .= ' AND `share_type` = ? '; |
|
| 439 | + $arguments[] = $shareType; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + if ($owner !== null) { |
|
| 443 | + $where .= ' AND `uid_owner` = ? '; |
|
| 444 | + $arguments[] = $owner; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where); |
|
| 448 | + |
|
| 449 | + $result = \OC_DB::executeAudited($query, $arguments); |
|
| 450 | + |
|
| 451 | + while ($row = $result->fetchRow()) { |
|
| 452 | + if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 453 | + continue; |
|
| 454 | + } |
|
| 455 | + if ($fileDependent && (int)$row['file_parent'] === -1) { |
|
| 456 | + // if it is a mount point we need to get the path from the mount manager |
|
| 457 | + $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 458 | + $mountPoint = $mountManager->findByStorageId($row['storage_id']); |
|
| 459 | + if (!empty($mountPoint)) { |
|
| 460 | + $path = $mountPoint[0]->getMountPoint(); |
|
| 461 | + $path = trim($path, '/'); |
|
| 462 | + $path = substr($path, strlen($owner) + 1); //normalize path to 'files/foo.txt` |
|
| 463 | + $row['path'] = $path; |
|
| 464 | + } else { |
|
| 465 | + \OC::$server->getLogger()->warning( |
|
| 466 | + 'Could not resolve mount point for ' . $row['storage_id'], |
|
| 467 | + ['app' => 'OCP\Share'] |
|
| 468 | + ); |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | + $shares[] = $row; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + //if didn't found a result than let's look for a group share. |
|
| 475 | + if(empty($shares) && $user !== null) { |
|
| 476 | + $userObject = \OC::$server->getUserManager()->get($user); |
|
| 477 | + $groups = []; |
|
| 478 | + if ($userObject) { |
|
| 479 | + $groups = \OC::$server->getGroupManager()->getUserGroupIds($userObject); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + if (!empty($groups)) { |
|
| 483 | + $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)'; |
|
| 484 | + $arguments = array($itemSource, $itemType, $groups); |
|
| 485 | + $types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 486 | + |
|
| 487 | + if ($owner !== null) { |
|
| 488 | + $where .= ' AND `uid_owner` = ?'; |
|
| 489 | + $arguments[] = $owner; |
|
| 490 | + $types[] = null; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + // TODO: inject connection, hopefully one day in the future when this |
|
| 494 | + // class isn't static anymore... |
|
| 495 | + $conn = \OC::$server->getDatabaseConnection(); |
|
| 496 | + $result = $conn->executeQuery( |
|
| 497 | + 'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where, |
|
| 498 | + $arguments, |
|
| 499 | + $types |
|
| 500 | + ); |
|
| 501 | + |
|
| 502 | + while ($row = $result->fetch()) { |
|
| 503 | + $shares[] = $row; |
|
| 504 | + } |
|
| 505 | + } |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + return $shares; |
|
| 509 | + |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * Get the item of item type shared with the current user by source |
|
| 514 | + * @param string $itemType |
|
| 515 | + * @param string $itemSource |
|
| 516 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 517 | + * @param mixed $parameters |
|
| 518 | + * @param boolean $includeCollections |
|
| 519 | + * @param string $shareWith (optional) define against which user should be checked, default: current user |
|
| 520 | + * @return array |
|
| 521 | + */ |
|
| 522 | + public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 523 | + $parameters = null, $includeCollections = false, $shareWith = null) { |
|
| 524 | + $shareWith = ($shareWith === null) ? \OC_User::getUser() : $shareWith; |
|
| 525 | + return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, $format, |
|
| 526 | + $parameters, 1, $includeCollections, true); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Get the item of item type shared by a link |
|
| 531 | + * @param string $itemType |
|
| 532 | + * @param string $itemSource |
|
| 533 | + * @param string $uidOwner Owner of link |
|
| 534 | + * @return array |
|
| 535 | + */ |
|
| 536 | + public static function getItemSharedWithByLink($itemType, $itemSource, $uidOwner) { |
|
| 537 | + return self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, $uidOwner, self::FORMAT_NONE, |
|
| 538 | + null, 1); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + /** |
|
| 542 | + * Based on the given token the share information will be returned - password protected shares will be verified |
|
| 543 | + * @param string $token |
|
| 544 | + * @param bool $checkPasswordProtection |
|
| 545 | + * @return array|boolean false will be returned in case the token is unknown or unauthorized |
|
| 546 | + */ |
|
| 547 | + public static function getShareByToken($token, $checkPasswordProtection = true) { |
|
| 548 | + $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `token` = ?', 1); |
|
| 549 | + $result = $query->execute(array($token)); |
|
| 550 | + if ($result === false) { |
|
| 551 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage() . ', token=' . $token, \OCP\Util::ERROR); |
|
| 552 | + } |
|
| 553 | + $row = $result->fetchRow(); |
|
| 554 | + if ($row === false) { |
|
| 555 | + return false; |
|
| 556 | + } |
|
| 557 | + if (is_array($row) and self::expireItem($row)) { |
|
| 558 | + return false; |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + // password protected shares need to be authenticated |
|
| 562 | + if ($checkPasswordProtection && !\OCP\Share::checkPasswordProtectedShare($row)) { |
|
| 563 | + return false; |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + return $row; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * resolves reshares down to the last real share |
|
| 571 | + * @param array $linkItem |
|
| 572 | + * @return array file owner |
|
| 573 | + */ |
|
| 574 | + public static function resolveReShare($linkItem) |
|
| 575 | + { |
|
| 576 | + if (isset($linkItem['parent'])) { |
|
| 577 | + $parent = $linkItem['parent']; |
|
| 578 | + while (isset($parent)) { |
|
| 579 | + $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ?', 1); |
|
| 580 | + $item = $query->execute(array($parent))->fetchRow(); |
|
| 581 | + if (isset($item['parent'])) { |
|
| 582 | + $parent = $item['parent']; |
|
| 583 | + } else { |
|
| 584 | + return $item; |
|
| 585 | + } |
|
| 586 | + } |
|
| 587 | + } |
|
| 588 | + return $linkItem; |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + |
|
| 592 | + /** |
|
| 593 | + * Get the shared items of item type owned by the current user |
|
| 594 | + * @param string $itemType |
|
| 595 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 596 | + * @param mixed $parameters |
|
| 597 | + * @param int $limit Number of items to return (optional) Returns all by default |
|
| 598 | + * @param boolean $includeCollections |
|
| 599 | + * @return mixed Return depends on format |
|
| 600 | + */ |
|
| 601 | + public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null, |
|
| 602 | + $limit = -1, $includeCollections = false) { |
|
| 603 | + return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format, |
|
| 604 | + $parameters, $limit, $includeCollections); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * Get the shared item of item type owned by the current user |
|
| 609 | + * @param string $itemType |
|
| 610 | + * @param string $itemSource |
|
| 611 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 612 | + * @param mixed $parameters |
|
| 613 | + * @param boolean $includeCollections |
|
| 614 | + * @return mixed Return depends on format |
|
| 615 | + */ |
|
| 616 | + public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 617 | + $parameters = null, $includeCollections = false) { |
|
| 618 | + return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format, |
|
| 619 | + $parameters, -1, $includeCollections); |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + /** |
|
| 623 | + * Get all users an item is shared with |
|
| 624 | + * @param string $itemType |
|
| 625 | + * @param string $itemSource |
|
| 626 | + * @param string $uidOwner |
|
| 627 | + * @param boolean $includeCollections |
|
| 628 | + * @param boolean $checkExpireDate |
|
| 629 | + * @return array Return array of users |
|
| 630 | + */ |
|
| 631 | + public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections = false, $checkExpireDate = true) { |
|
| 632 | + |
|
| 633 | + $users = array(); |
|
| 634 | + $items = self::getItems($itemType, $itemSource, null, null, $uidOwner, self::FORMAT_NONE, null, -1, $includeCollections, false, $checkExpireDate); |
|
| 635 | + if ($items) { |
|
| 636 | + foreach ($items as $item) { |
|
| 637 | + if ((int)$item['share_type'] === self::SHARE_TYPE_USER) { |
|
| 638 | + $users[] = $item['share_with']; |
|
| 639 | + } else if ((int)$item['share_type'] === self::SHARE_TYPE_GROUP) { |
|
| 640 | + |
|
| 641 | + $group = \OC::$server->getGroupManager()->get($item['share_with']); |
|
| 642 | + $userIds = []; |
|
| 643 | + if ($group) { |
|
| 644 | + $users = $group->searchUsers('', -1, 0); |
|
| 645 | + foreach ($users as $user) { |
|
| 646 | + $userIds[] = $user->getUID(); |
|
| 647 | + } |
|
| 648 | + return $userIds; |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + $users = array_merge($users, $userIds); |
|
| 652 | + } |
|
| 653 | + } |
|
| 654 | + } |
|
| 655 | + return $users; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Share an item with a user, group, or via private link |
|
| 660 | + * @param string $itemType |
|
| 661 | + * @param string $itemSource |
|
| 662 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 663 | + * @param string $shareWith User or group the item is being shared with |
|
| 664 | + * @param int $permissions CRUDS |
|
| 665 | + * @param string $itemSourceName |
|
| 666 | + * @param \DateTime $expirationDate |
|
| 667 | + * @param bool $passwordChanged |
|
| 668 | + * @return boolean|string Returns true on success or false on failure, Returns token on success for links |
|
| 669 | + * @throws \OC\HintException when the share type is remote and the shareWith is invalid |
|
| 670 | + * @throws \Exception |
|
| 671 | + */ |
|
| 672 | + public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) { |
|
| 673 | + |
|
| 674 | + $backend = self::getBackend($itemType); |
|
| 675 | + $l = \OC::$server->getL10N('lib'); |
|
| 676 | + |
|
| 677 | + if ($backend->isShareTypeAllowed($shareType) === false) { |
|
| 678 | + $message = 'Sharing %s failed, because the backend does not allow shares from type %i'; |
|
| 679 | + $message_t = $l->t('Sharing %s failed, because the backend does not allow shares from type %i', array($itemSourceName, $shareType)); |
|
| 680 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareType), \OCP\Util::DEBUG); |
|
| 681 | + throw new \Exception($message_t); |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + $uidOwner = \OC_User::getUser(); |
|
| 685 | + $shareWithinGroupOnly = self::shareWithGroupMembersOnly(); |
|
| 686 | + |
|
| 687 | + if (is_null($itemSourceName)) { |
|
| 688 | + $itemSourceName = $itemSource; |
|
| 689 | + } |
|
| 690 | + $itemName = $itemSourceName; |
|
| 691 | + |
|
| 692 | + // check if file can be shared |
|
| 693 | + if ($itemType === 'file' or $itemType === 'folder') { |
|
| 694 | + $path = \OC\Files\Filesystem::getPath($itemSource); |
|
| 695 | + $itemName = $path; |
|
| 696 | + |
|
| 697 | + // verify that the file exists before we try to share it |
|
| 698 | + if (!$path) { |
|
| 699 | + $message = 'Sharing %s failed, because the file does not exist'; |
|
| 700 | + $message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName)); |
|
| 701 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 702 | + throw new \Exception($message_t); |
|
| 703 | + } |
|
| 704 | + // verify that the user has share permission |
|
| 705 | + if (!\OC\Files\Filesystem::isSharable($path) || \OCP\Util::isSharingDisabledForUser()) { |
|
| 706 | + $message = 'You are not allowed to share %s'; |
|
| 707 | + $message_t = $l->t('You are not allowed to share %s', [$path]); |
|
| 708 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $path), \OCP\Util::DEBUG); |
|
| 709 | + throw new \Exception($message_t); |
|
| 710 | + } |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + //verify that we don't share a folder which already contains a share mount point |
|
| 714 | + if ($itemType === 'folder') { |
|
| 715 | + $path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/'; |
|
| 716 | + $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 717 | + $mounts = $mountManager->findIn($path); |
|
| 718 | + foreach ($mounts as $mount) { |
|
| 719 | + if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 720 | + $message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!'; |
|
| 721 | + \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 722 | + throw new \Exception($message); |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + } |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + // single file shares should never have delete permissions |
|
| 729 | + if ($itemType === 'file') { |
|
| 730 | + $permissions = (int)$permissions & ~\OCP\Constants::PERMISSION_DELETE; |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + //Validate expirationDate |
|
| 734 | + if ($expirationDate !== null) { |
|
| 735 | + try { |
|
| 736 | + /* |
|
| 737 | 737 | * Reuse the validateExpireDate. |
| 738 | 738 | * We have to pass time() since the second arg is the time |
| 739 | 739 | * the file was shared, since it is not shared yet we just use |
| 740 | 740 | * the current time. |
| 741 | 741 | */ |
| 742 | - $expirationDate = self::validateExpireDate($expirationDate->format('Y-m-d'), time(), $itemType, $itemSource); |
|
| 743 | - } catch (\Exception $e) { |
|
| 744 | - throw new \OC\HintException($e->getMessage(), $e->getMessage(), 404); |
|
| 745 | - } |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - // Verify share type and sharing conditions are met |
|
| 749 | - if ($shareType === self::SHARE_TYPE_USER) { |
|
| 750 | - if ($shareWith == $uidOwner) { |
|
| 751 | - $message = 'Sharing %s failed, because you can not share with yourself'; |
|
| 752 | - $message_t = $l->t('Sharing %s failed, because you can not share with yourself', [$itemName]); |
|
| 753 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 754 | - throw new \Exception($message_t); |
|
| 755 | - } |
|
| 756 | - if (!\OC_User::userExists($shareWith)) { |
|
| 757 | - $message = 'Sharing %s failed, because the user %s does not exist'; |
|
| 758 | - $message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith)); |
|
| 759 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 760 | - throw new \Exception($message_t); |
|
| 761 | - } |
|
| 762 | - if ($shareWithinGroupOnly) { |
|
| 763 | - $userManager = \OC::$server->getUserManager(); |
|
| 764 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 765 | - $userOwner = $userManager->get($uidOwner); |
|
| 766 | - $userShareWith = $userManager->get($shareWith); |
|
| 767 | - $groupsOwner = []; |
|
| 768 | - $groupsShareWith = []; |
|
| 769 | - if ($userOwner) { |
|
| 770 | - $groupsOwner = $groupManager->getUserGroupIds($userOwner); |
|
| 771 | - } |
|
| 772 | - if ($userShareWith) { |
|
| 773 | - $groupsShareWith = $groupManager->getUserGroupIds($userShareWith); |
|
| 774 | - } |
|
| 775 | - $inGroup = array_intersect($groupsOwner, $groupsShareWith); |
|
| 776 | - if (empty($inGroup)) { |
|
| 777 | - $message = 'Sharing %s failed, because the user ' |
|
| 778 | - .'%s is not a member of any groups that %s is a member of'; |
|
| 779 | - $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($itemName, $shareWith, $uidOwner)); |
|
| 780 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemName, $shareWith, $uidOwner), \OCP\Util::DEBUG); |
|
| 781 | - throw new \Exception($message_t); |
|
| 782 | - } |
|
| 783 | - } |
|
| 784 | - // Check if the item source is already shared with the user, either from the same owner or a different user |
|
| 785 | - if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, |
|
| 786 | - $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 787 | - // Only allow the same share to occur again if it is the same |
|
| 788 | - // owner and is not a user share, this use case is for increasing |
|
| 789 | - // permissions for a specific user |
|
| 790 | - if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 791 | - $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 792 | - $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 793 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 794 | - throw new \Exception($message_t); |
|
| 795 | - } |
|
| 796 | - } |
|
| 797 | - if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER, |
|
| 798 | - $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 799 | - // Only allow the same share to occur again if it is the same |
|
| 800 | - // owner and is not a user share, this use case is for increasing |
|
| 801 | - // permissions for a specific user |
|
| 802 | - if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 803 | - $message = 'Sharing %s failed, because this item is already shared with user %s'; |
|
| 804 | - $message_t = $l->t('Sharing %s failed, because this item is already shared with user %s', array($itemSourceName, $shareWith)); |
|
| 805 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::ERROR); |
|
| 806 | - throw new \Exception($message_t); |
|
| 807 | - } |
|
| 808 | - } |
|
| 809 | - } else if ($shareType === self::SHARE_TYPE_GROUP) { |
|
| 810 | - if (!\OC::$server->getGroupManager()->groupExists($shareWith)) { |
|
| 811 | - $message = 'Sharing %s failed, because the group %s does not exist'; |
|
| 812 | - $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith)); |
|
| 813 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 814 | - throw new \Exception($message_t); |
|
| 815 | - } |
|
| 816 | - if ($shareWithinGroupOnly) { |
|
| 817 | - $group = \OC::$server->getGroupManager()->get($shareWith); |
|
| 818 | - $user = \OC::$server->getUserManager()->get($uidOwner); |
|
| 819 | - if (!$group || !$user || !$group->inGroup($user)) { |
|
| 820 | - $message = 'Sharing %s failed, because ' |
|
| 821 | - . '%s is not a member of the group %s'; |
|
| 822 | - $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith)); |
|
| 823 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG); |
|
| 824 | - throw new \Exception($message_t); |
|
| 825 | - } |
|
| 826 | - } |
|
| 827 | - // Check if the item source is already shared with the group, either from the same owner or a different user |
|
| 828 | - // The check for each user in the group is done inside the put() function |
|
| 829 | - if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith, |
|
| 830 | - null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 831 | - |
|
| 832 | - if ($checkExists['share_with'] === $shareWith && $checkExists['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 833 | - $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 834 | - $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 835 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 836 | - throw new \Exception($message_t); |
|
| 837 | - } |
|
| 838 | - } |
|
| 839 | - // Convert share with into an array with the keys group and users |
|
| 840 | - $group = $shareWith; |
|
| 841 | - $shareWith = array(); |
|
| 842 | - $shareWith['group'] = $group; |
|
| 843 | - |
|
| 844 | - |
|
| 845 | - $groupObject = \OC::$server->getGroupManager()->get($group); |
|
| 846 | - $userIds = []; |
|
| 847 | - if ($groupObject) { |
|
| 848 | - $users = $groupObject->searchUsers('', -1, 0); |
|
| 849 | - foreach ($users as $user) { |
|
| 850 | - $userIds[] = $user->getUID(); |
|
| 851 | - } |
|
| 852 | - } |
|
| 853 | - |
|
| 854 | - $shareWith['users'] = array_diff($userIds, array($uidOwner)); |
|
| 855 | - } else if ($shareType === self::SHARE_TYPE_LINK) { |
|
| 856 | - $updateExistingShare = false; |
|
| 857 | - if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') == 'yes') { |
|
| 858 | - |
|
| 859 | - // IF the password is changed via the old ajax endpoint verify it before deleting the old share |
|
| 860 | - if ($passwordChanged === true) { |
|
| 861 | - self::verifyPassword($shareWith); |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - // when updating a link share |
|
| 865 | - // FIXME Don't delete link if we update it |
|
| 866 | - if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, |
|
| 867 | - $uidOwner, self::FORMAT_NONE, null, 1)) { |
|
| 868 | - // remember old token |
|
| 869 | - $oldToken = $checkExists['token']; |
|
| 870 | - $oldPermissions = $checkExists['permissions']; |
|
| 871 | - //delete the old share |
|
| 872 | - Helper::delete($checkExists['id']); |
|
| 873 | - $updateExistingShare = true; |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - if ($passwordChanged === null) { |
|
| 877 | - // Generate hash of password - same method as user passwords |
|
| 878 | - if (is_string($shareWith) && $shareWith !== '') { |
|
| 879 | - self::verifyPassword($shareWith); |
|
| 880 | - $shareWith = \OC::$server->getHasher()->hash($shareWith); |
|
| 881 | - } else { |
|
| 882 | - // reuse the already set password, but only if we change permissions |
|
| 883 | - // otherwise the user disabled the password protection |
|
| 884 | - if ($checkExists && (int)$permissions !== (int)$oldPermissions) { |
|
| 885 | - $shareWith = $checkExists['share_with']; |
|
| 886 | - } |
|
| 887 | - } |
|
| 888 | - } else { |
|
| 889 | - if ($passwordChanged === true) { |
|
| 890 | - if (is_string($shareWith) && $shareWith !== '') { |
|
| 891 | - self::verifyPassword($shareWith); |
|
| 892 | - $shareWith = \OC::$server->getHasher()->hash($shareWith); |
|
| 893 | - } |
|
| 894 | - } else if ($updateExistingShare) { |
|
| 895 | - $shareWith = $checkExists['share_with']; |
|
| 896 | - } |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - if (\OCP\Util::isPublicLinkPasswordRequired() && empty($shareWith)) { |
|
| 900 | - $message = 'You need to provide a password to create a public link, only protected links are allowed'; |
|
| 901 | - $message_t = $l->t('You need to provide a password to create a public link, only protected links are allowed'); |
|
| 902 | - \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 903 | - throw new \Exception($message_t); |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - if ($updateExistingShare === false && |
|
| 907 | - self::isDefaultExpireDateEnabled() && |
|
| 908 | - empty($expirationDate)) { |
|
| 909 | - $expirationDate = Helper::calcExpireDate(); |
|
| 910 | - } |
|
| 911 | - |
|
| 912 | - // Generate token |
|
| 913 | - if (isset($oldToken)) { |
|
| 914 | - $token = $oldToken; |
|
| 915 | - } else { |
|
| 916 | - $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, |
|
| 917 | - \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
| 918 | - ); |
|
| 919 | - } |
|
| 920 | - $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, |
|
| 921 | - null, $token, $itemSourceName, $expirationDate); |
|
| 922 | - if ($result) { |
|
| 923 | - return $token; |
|
| 924 | - } else { |
|
| 925 | - return false; |
|
| 926 | - } |
|
| 927 | - } |
|
| 928 | - $message = 'Sharing %s failed, because sharing with links is not allowed'; |
|
| 929 | - $message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName)); |
|
| 930 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 931 | - throw new \Exception($message_t); |
|
| 932 | - } else if ($shareType === self::SHARE_TYPE_REMOTE) { |
|
| 933 | - |
|
| 934 | - /* |
|
| 742 | + $expirationDate = self::validateExpireDate($expirationDate->format('Y-m-d'), time(), $itemType, $itemSource); |
|
| 743 | + } catch (\Exception $e) { |
|
| 744 | + throw new \OC\HintException($e->getMessage(), $e->getMessage(), 404); |
|
| 745 | + } |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + // Verify share type and sharing conditions are met |
|
| 749 | + if ($shareType === self::SHARE_TYPE_USER) { |
|
| 750 | + if ($shareWith == $uidOwner) { |
|
| 751 | + $message = 'Sharing %s failed, because you can not share with yourself'; |
|
| 752 | + $message_t = $l->t('Sharing %s failed, because you can not share with yourself', [$itemName]); |
|
| 753 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 754 | + throw new \Exception($message_t); |
|
| 755 | + } |
|
| 756 | + if (!\OC_User::userExists($shareWith)) { |
|
| 757 | + $message = 'Sharing %s failed, because the user %s does not exist'; |
|
| 758 | + $message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith)); |
|
| 759 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 760 | + throw new \Exception($message_t); |
|
| 761 | + } |
|
| 762 | + if ($shareWithinGroupOnly) { |
|
| 763 | + $userManager = \OC::$server->getUserManager(); |
|
| 764 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 765 | + $userOwner = $userManager->get($uidOwner); |
|
| 766 | + $userShareWith = $userManager->get($shareWith); |
|
| 767 | + $groupsOwner = []; |
|
| 768 | + $groupsShareWith = []; |
|
| 769 | + if ($userOwner) { |
|
| 770 | + $groupsOwner = $groupManager->getUserGroupIds($userOwner); |
|
| 771 | + } |
|
| 772 | + if ($userShareWith) { |
|
| 773 | + $groupsShareWith = $groupManager->getUserGroupIds($userShareWith); |
|
| 774 | + } |
|
| 775 | + $inGroup = array_intersect($groupsOwner, $groupsShareWith); |
|
| 776 | + if (empty($inGroup)) { |
|
| 777 | + $message = 'Sharing %s failed, because the user ' |
|
| 778 | + .'%s is not a member of any groups that %s is a member of'; |
|
| 779 | + $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($itemName, $shareWith, $uidOwner)); |
|
| 780 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemName, $shareWith, $uidOwner), \OCP\Util::DEBUG); |
|
| 781 | + throw new \Exception($message_t); |
|
| 782 | + } |
|
| 783 | + } |
|
| 784 | + // Check if the item source is already shared with the user, either from the same owner or a different user |
|
| 785 | + if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, |
|
| 786 | + $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 787 | + // Only allow the same share to occur again if it is the same |
|
| 788 | + // owner and is not a user share, this use case is for increasing |
|
| 789 | + // permissions for a specific user |
|
| 790 | + if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 791 | + $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 792 | + $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 793 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 794 | + throw new \Exception($message_t); |
|
| 795 | + } |
|
| 796 | + } |
|
| 797 | + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER, |
|
| 798 | + $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 799 | + // Only allow the same share to occur again if it is the same |
|
| 800 | + // owner and is not a user share, this use case is for increasing |
|
| 801 | + // permissions for a specific user |
|
| 802 | + if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 803 | + $message = 'Sharing %s failed, because this item is already shared with user %s'; |
|
| 804 | + $message_t = $l->t('Sharing %s failed, because this item is already shared with user %s', array($itemSourceName, $shareWith)); |
|
| 805 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::ERROR); |
|
| 806 | + throw new \Exception($message_t); |
|
| 807 | + } |
|
| 808 | + } |
|
| 809 | + } else if ($shareType === self::SHARE_TYPE_GROUP) { |
|
| 810 | + if (!\OC::$server->getGroupManager()->groupExists($shareWith)) { |
|
| 811 | + $message = 'Sharing %s failed, because the group %s does not exist'; |
|
| 812 | + $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith)); |
|
| 813 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 814 | + throw new \Exception($message_t); |
|
| 815 | + } |
|
| 816 | + if ($shareWithinGroupOnly) { |
|
| 817 | + $group = \OC::$server->getGroupManager()->get($shareWith); |
|
| 818 | + $user = \OC::$server->getUserManager()->get($uidOwner); |
|
| 819 | + if (!$group || !$user || !$group->inGroup($user)) { |
|
| 820 | + $message = 'Sharing %s failed, because ' |
|
| 821 | + . '%s is not a member of the group %s'; |
|
| 822 | + $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith)); |
|
| 823 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG); |
|
| 824 | + throw new \Exception($message_t); |
|
| 825 | + } |
|
| 826 | + } |
|
| 827 | + // Check if the item source is already shared with the group, either from the same owner or a different user |
|
| 828 | + // The check for each user in the group is done inside the put() function |
|
| 829 | + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith, |
|
| 830 | + null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 831 | + |
|
| 832 | + if ($checkExists['share_with'] === $shareWith && $checkExists['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 833 | + $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 834 | + $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 835 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 836 | + throw new \Exception($message_t); |
|
| 837 | + } |
|
| 838 | + } |
|
| 839 | + // Convert share with into an array with the keys group and users |
|
| 840 | + $group = $shareWith; |
|
| 841 | + $shareWith = array(); |
|
| 842 | + $shareWith['group'] = $group; |
|
| 843 | + |
|
| 844 | + |
|
| 845 | + $groupObject = \OC::$server->getGroupManager()->get($group); |
|
| 846 | + $userIds = []; |
|
| 847 | + if ($groupObject) { |
|
| 848 | + $users = $groupObject->searchUsers('', -1, 0); |
|
| 849 | + foreach ($users as $user) { |
|
| 850 | + $userIds[] = $user->getUID(); |
|
| 851 | + } |
|
| 852 | + } |
|
| 853 | + |
|
| 854 | + $shareWith['users'] = array_diff($userIds, array($uidOwner)); |
|
| 855 | + } else if ($shareType === self::SHARE_TYPE_LINK) { |
|
| 856 | + $updateExistingShare = false; |
|
| 857 | + if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') == 'yes') { |
|
| 858 | + |
|
| 859 | + // IF the password is changed via the old ajax endpoint verify it before deleting the old share |
|
| 860 | + if ($passwordChanged === true) { |
|
| 861 | + self::verifyPassword($shareWith); |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + // when updating a link share |
|
| 865 | + // FIXME Don't delete link if we update it |
|
| 866 | + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, |
|
| 867 | + $uidOwner, self::FORMAT_NONE, null, 1)) { |
|
| 868 | + // remember old token |
|
| 869 | + $oldToken = $checkExists['token']; |
|
| 870 | + $oldPermissions = $checkExists['permissions']; |
|
| 871 | + //delete the old share |
|
| 872 | + Helper::delete($checkExists['id']); |
|
| 873 | + $updateExistingShare = true; |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + if ($passwordChanged === null) { |
|
| 877 | + // Generate hash of password - same method as user passwords |
|
| 878 | + if (is_string($shareWith) && $shareWith !== '') { |
|
| 879 | + self::verifyPassword($shareWith); |
|
| 880 | + $shareWith = \OC::$server->getHasher()->hash($shareWith); |
|
| 881 | + } else { |
|
| 882 | + // reuse the already set password, but only if we change permissions |
|
| 883 | + // otherwise the user disabled the password protection |
|
| 884 | + if ($checkExists && (int)$permissions !== (int)$oldPermissions) { |
|
| 885 | + $shareWith = $checkExists['share_with']; |
|
| 886 | + } |
|
| 887 | + } |
|
| 888 | + } else { |
|
| 889 | + if ($passwordChanged === true) { |
|
| 890 | + if (is_string($shareWith) && $shareWith !== '') { |
|
| 891 | + self::verifyPassword($shareWith); |
|
| 892 | + $shareWith = \OC::$server->getHasher()->hash($shareWith); |
|
| 893 | + } |
|
| 894 | + } else if ($updateExistingShare) { |
|
| 895 | + $shareWith = $checkExists['share_with']; |
|
| 896 | + } |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + if (\OCP\Util::isPublicLinkPasswordRequired() && empty($shareWith)) { |
|
| 900 | + $message = 'You need to provide a password to create a public link, only protected links are allowed'; |
|
| 901 | + $message_t = $l->t('You need to provide a password to create a public link, only protected links are allowed'); |
|
| 902 | + \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 903 | + throw new \Exception($message_t); |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + if ($updateExistingShare === false && |
|
| 907 | + self::isDefaultExpireDateEnabled() && |
|
| 908 | + empty($expirationDate)) { |
|
| 909 | + $expirationDate = Helper::calcExpireDate(); |
|
| 910 | + } |
|
| 911 | + |
|
| 912 | + // Generate token |
|
| 913 | + if (isset($oldToken)) { |
|
| 914 | + $token = $oldToken; |
|
| 915 | + } else { |
|
| 916 | + $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, |
|
| 917 | + \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
| 918 | + ); |
|
| 919 | + } |
|
| 920 | + $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, |
|
| 921 | + null, $token, $itemSourceName, $expirationDate); |
|
| 922 | + if ($result) { |
|
| 923 | + return $token; |
|
| 924 | + } else { |
|
| 925 | + return false; |
|
| 926 | + } |
|
| 927 | + } |
|
| 928 | + $message = 'Sharing %s failed, because sharing with links is not allowed'; |
|
| 929 | + $message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName)); |
|
| 930 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 931 | + throw new \Exception($message_t); |
|
| 932 | + } else if ($shareType === self::SHARE_TYPE_REMOTE) { |
|
| 933 | + |
|
| 934 | + /* |
|
| 935 | 935 | * Check if file is not already shared with the remote user |
| 936 | 936 | */ |
| 937 | - if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_REMOTE, |
|
| 938 | - $shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 939 | - $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 940 | - $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 941 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 942 | - throw new \Exception($message_t); |
|
| 943 | - } |
|
| 944 | - |
|
| 945 | - // don't allow federated shares if source and target server are the same |
|
| 946 | - list($user, $remote) = Helper::splitUserRemote($shareWith); |
|
| 947 | - $currentServer = self::removeProtocolFromUrl(\OC::$server->getURLGenerator()->getAbsoluteURL('/')); |
|
| 948 | - $currentUser = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 949 | - if (Helper::isSameUserOnSameServer($user, $remote, $currentUser, $currentServer)) { |
|
| 950 | - $message = 'Not allowed to create a federated share with the same user.'; |
|
| 951 | - $message_t = $l->t('Not allowed to create a federated share with the same user'); |
|
| 952 | - \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 953 | - throw new \Exception($message_t); |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER . |
|
| 957 | - \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 958 | - |
|
| 959 | - $shareWith = $user . '@' . $remote; |
|
| 960 | - $shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName); |
|
| 961 | - |
|
| 962 | - $send = false; |
|
| 963 | - if ($shareId) { |
|
| 964 | - $send = self::sendRemoteShare($token, $shareWith, $itemSourceName, $shareId, $uidOwner); |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - if ($send === false) { |
|
| 968 | - $currentUser = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 969 | - self::unshare($itemType, $itemSource, $shareType, $shareWith, $currentUser); |
|
| 970 | - $message_t = $l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', array($itemSourceName, $shareWith)); |
|
| 971 | - throw new \Exception($message_t); |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - return $send; |
|
| 975 | - } else { |
|
| 976 | - // Future share types need to include their own conditions |
|
| 977 | - $message = 'Share type %s is not valid for %s'; |
|
| 978 | - $message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource)); |
|
| 979 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $shareType, $itemSource), \OCP\Util::DEBUG); |
|
| 980 | - throw new \Exception($message_t); |
|
| 981 | - } |
|
| 982 | - |
|
| 983 | - // Put the item into the database |
|
| 984 | - $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName, $expirationDate); |
|
| 985 | - |
|
| 986 | - return $result ? true : false; |
|
| 987 | - } |
|
| 988 | - |
|
| 989 | - /** |
|
| 990 | - * Unshare an item from a user, group, or delete a private link |
|
| 991 | - * @param string $itemType |
|
| 992 | - * @param string $itemSource |
|
| 993 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 994 | - * @param string $shareWith User or group the item is being shared with |
|
| 995 | - * @param string $owner owner of the share, if null the current user is used |
|
| 996 | - * @return boolean true on success or false on failure |
|
| 997 | - */ |
|
| 998 | - public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) { |
|
| 999 | - |
|
| 1000 | - // check if it is a valid itemType |
|
| 1001 | - self::getBackend($itemType); |
|
| 1002 | - |
|
| 1003 | - $items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType); |
|
| 1004 | - |
|
| 1005 | - $toDelete = array(); |
|
| 1006 | - $newParent = null; |
|
| 1007 | - $currentUser = $owner ? $owner : \OC_User::getUser(); |
|
| 1008 | - foreach ($items as $item) { |
|
| 1009 | - // delete the item with the expected share_type and owner |
|
| 1010 | - if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) { |
|
| 1011 | - $toDelete = $item; |
|
| 1012 | - // if there is more then one result we don't have to delete the children |
|
| 1013 | - // but update their parent. For group shares the new parent should always be |
|
| 1014 | - // the original group share and not the db entry with the unique name |
|
| 1015 | - } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) { |
|
| 1016 | - $newParent = $item['parent']; |
|
| 1017 | - } else { |
|
| 1018 | - $newParent = $item['id']; |
|
| 1019 | - } |
|
| 1020 | - } |
|
| 1021 | - |
|
| 1022 | - if (!empty($toDelete)) { |
|
| 1023 | - self::unshareItem($toDelete, $newParent); |
|
| 1024 | - return true; |
|
| 1025 | - } |
|
| 1026 | - return false; |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - /** |
|
| 1030 | - * Unshare an item from all users, groups, and remove all links |
|
| 1031 | - * @param string $itemType |
|
| 1032 | - * @param string $itemSource |
|
| 1033 | - * @return boolean true on success or false on failure |
|
| 1034 | - */ |
|
| 1035 | - public static function unshareAll($itemType, $itemSource) { |
|
| 1036 | - // Get all of the owners of shares of this item. |
|
| 1037 | - $query = \OC_DB::prepare( 'SELECT `uid_owner` from `*PREFIX*share` WHERE `item_type`=? AND `item_source`=?' ); |
|
| 1038 | - $result = $query->execute(array($itemType, $itemSource)); |
|
| 1039 | - $shares = array(); |
|
| 1040 | - // Add each owner's shares to the array of all shares for this item. |
|
| 1041 | - while ($row = $result->fetchRow()) { |
|
| 1042 | - $shares = array_merge($shares, self::getItems($itemType, $itemSource, null, null, $row['uid_owner'])); |
|
| 1043 | - } |
|
| 1044 | - if (!empty($shares)) { |
|
| 1045 | - // Pass all the vars we have for now, they may be useful |
|
| 1046 | - $hookParams = array( |
|
| 1047 | - 'itemType' => $itemType, |
|
| 1048 | - 'itemSource' => $itemSource, |
|
| 1049 | - 'shares' => $shares, |
|
| 1050 | - ); |
|
| 1051 | - \OC_Hook::emit('OCP\Share', 'pre_unshareAll', $hookParams); |
|
| 1052 | - foreach ($shares as $share) { |
|
| 1053 | - self::unshareItem($share); |
|
| 1054 | - } |
|
| 1055 | - \OC_Hook::emit('OCP\Share', 'post_unshareAll', $hookParams); |
|
| 1056 | - return true; |
|
| 1057 | - } |
|
| 1058 | - return false; |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - /** |
|
| 1062 | - * Unshare an item shared with the current user |
|
| 1063 | - * @param string $itemType |
|
| 1064 | - * @param string $itemOrigin Item target or source |
|
| 1065 | - * @param boolean $originIsSource true if $itemOrigin is the source, false if $itemOrigin is the target (optional) |
|
| 1066 | - * @return boolean true on success or false on failure |
|
| 1067 | - * |
|
| 1068 | - * Unsharing from self is not allowed for items inside collections |
|
| 1069 | - */ |
|
| 1070 | - public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) { |
|
| 1071 | - $originType = ($originIsSource) ? 'source' : 'target'; |
|
| 1072 | - $uid = \OCP\User::getUser(); |
|
| 1073 | - |
|
| 1074 | - if ($itemType === 'file' || $itemType === 'folder') { |
|
| 1075 | - $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_' . $originType . '` = ?'; |
|
| 1076 | - } else { |
|
| 1077 | - $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `item_' . $originType . '` = ?'; |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - $query = \OCP\DB::prepare($statement); |
|
| 1081 | - $result = $query->execute(array($itemType, $itemOrigin)); |
|
| 1082 | - |
|
| 1083 | - $shares = $result->fetchAll(); |
|
| 1084 | - |
|
| 1085 | - $listOfUnsharedItems = array(); |
|
| 1086 | - |
|
| 1087 | - $itemUnshared = false; |
|
| 1088 | - foreach ($shares as $share) { |
|
| 1089 | - if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER && |
|
| 1090 | - $share['share_with'] === $uid) { |
|
| 1091 | - $deletedShares = Helper::delete($share['id']); |
|
| 1092 | - $shareTmp = array( |
|
| 1093 | - 'id' => $share['id'], |
|
| 1094 | - 'shareWith' => $share['share_with'], |
|
| 1095 | - 'itemTarget' => $share['item_target'], |
|
| 1096 | - 'itemType' => $share['item_type'], |
|
| 1097 | - 'shareType' => (int)$share['share_type'], |
|
| 1098 | - ); |
|
| 1099 | - if (isset($share['file_target'])) { |
|
| 1100 | - $shareTmp['fileTarget'] = $share['file_target']; |
|
| 1101 | - } |
|
| 1102 | - $listOfUnsharedItems = array_merge($listOfUnsharedItems, $deletedShares, array($shareTmp)); |
|
| 1103 | - $itemUnshared = true; |
|
| 1104 | - break; |
|
| 1105 | - } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 1106 | - $group = \OC::$server->getGroupManager()->get($share['share_with']); |
|
| 1107 | - $user = \OC::$server->getUserManager()->get($uid); |
|
| 1108 | - if ($group && $user && $group->inGroup($user)) { |
|
| 1109 | - $groupShare = $share; |
|
| 1110 | - } |
|
| 1111 | - } elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique && |
|
| 1112 | - $share['share_with'] === $uid) { |
|
| 1113 | - $uniqueGroupShare = $share; |
|
| 1114 | - } |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - if (!$itemUnshared && isset($groupShare) && !isset($uniqueGroupShare)) { |
|
| 1118 | - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share`' |
|
| 1119 | - .' (`item_type`, `item_source`, `item_target`, `parent`, `share_type`,' |
|
| 1120 | - .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`, `file_target`)' |
|
| 1121 | - .' VALUES (?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 1122 | - $query->execute(array($groupShare['item_type'], $groupShare['item_source'], $groupShare['item_target'], |
|
| 1123 | - $groupShare['id'], self::$shareTypeGroupUserUnique, |
|
| 1124 | - \OC_User::getUser(), $groupShare['uid_owner'], 0, $groupShare['stime'], $groupShare['file_source'], |
|
| 1125 | - $groupShare['file_target'])); |
|
| 1126 | - $shareTmp = array( |
|
| 1127 | - 'id' => $groupShare['id'], |
|
| 1128 | - 'shareWith' => $groupShare['share_with'], |
|
| 1129 | - 'itemTarget' => $groupShare['item_target'], |
|
| 1130 | - 'itemType' => $groupShare['item_type'], |
|
| 1131 | - 'shareType' => (int)$groupShare['share_type'], |
|
| 1132 | - ); |
|
| 1133 | - if (isset($groupShare['file_target'])) { |
|
| 1134 | - $shareTmp['fileTarget'] = $groupShare['file_target']; |
|
| 1135 | - } |
|
| 1136 | - $listOfUnsharedItems = array_merge($listOfUnsharedItems, [$shareTmp]); |
|
| 1137 | - $itemUnshared = true; |
|
| 1138 | - } elseif (!$itemUnshared && isset($uniqueGroupShare)) { |
|
| 1139 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = ? WHERE `id` = ?'); |
|
| 1140 | - $query->execute(array(0, $uniqueGroupShare['id'])); |
|
| 1141 | - $shareTmp = array( |
|
| 1142 | - 'id' => $uniqueGroupShare['id'], |
|
| 1143 | - 'shareWith' => $uniqueGroupShare['share_with'], |
|
| 1144 | - 'itemTarget' => $uniqueGroupShare['item_target'], |
|
| 1145 | - 'itemType' => $uniqueGroupShare['item_type'], |
|
| 1146 | - 'shareType' => (int)$uniqueGroupShare['share_type'], |
|
| 1147 | - ); |
|
| 1148 | - if (isset($uniqueGroupShare['file_target'])) { |
|
| 1149 | - $shareTmp['fileTarget'] = $uniqueGroupShare['file_target']; |
|
| 1150 | - } |
|
| 1151 | - $listOfUnsharedItems = array_merge($listOfUnsharedItems, [$shareTmp]); |
|
| 1152 | - $itemUnshared = true; |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - if ($itemUnshared) { |
|
| 1156 | - \OC_Hook::emit('OCP\Share', 'post_unshareFromSelf', |
|
| 1157 | - array('unsharedItems' => $listOfUnsharedItems, 'itemType' => $itemType)); |
|
| 1158 | - } |
|
| 1159 | - |
|
| 1160 | - return $itemUnshared; |
|
| 1161 | - } |
|
| 1162 | - |
|
| 1163 | - /** |
|
| 1164 | - * sent status if users got informed by mail about share |
|
| 1165 | - * @param string $itemType |
|
| 1166 | - * @param string $itemSource |
|
| 1167 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1168 | - * @param string $recipient with whom was the file shared |
|
| 1169 | - * @param boolean $status |
|
| 1170 | - */ |
|
| 1171 | - public static function setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status) { |
|
| 1172 | - $status = $status ? 1 : 0; |
|
| 1173 | - |
|
| 1174 | - $query = \OC_DB::prepare( |
|
| 1175 | - 'UPDATE `*PREFIX*share` |
|
| 937 | + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_REMOTE, |
|
| 938 | + $shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 939 | + $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 940 | + $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 941 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 942 | + throw new \Exception($message_t); |
|
| 943 | + } |
|
| 944 | + |
|
| 945 | + // don't allow federated shares if source and target server are the same |
|
| 946 | + list($user, $remote) = Helper::splitUserRemote($shareWith); |
|
| 947 | + $currentServer = self::removeProtocolFromUrl(\OC::$server->getURLGenerator()->getAbsoluteURL('/')); |
|
| 948 | + $currentUser = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 949 | + if (Helper::isSameUserOnSameServer($user, $remote, $currentUser, $currentServer)) { |
|
| 950 | + $message = 'Not allowed to create a federated share with the same user.'; |
|
| 951 | + $message_t = $l->t('Not allowed to create a federated share with the same user'); |
|
| 952 | + \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 953 | + throw new \Exception($message_t); |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER . |
|
| 957 | + \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 958 | + |
|
| 959 | + $shareWith = $user . '@' . $remote; |
|
| 960 | + $shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName); |
|
| 961 | + |
|
| 962 | + $send = false; |
|
| 963 | + if ($shareId) { |
|
| 964 | + $send = self::sendRemoteShare($token, $shareWith, $itemSourceName, $shareId, $uidOwner); |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + if ($send === false) { |
|
| 968 | + $currentUser = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 969 | + self::unshare($itemType, $itemSource, $shareType, $shareWith, $currentUser); |
|
| 970 | + $message_t = $l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', array($itemSourceName, $shareWith)); |
|
| 971 | + throw new \Exception($message_t); |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + return $send; |
|
| 975 | + } else { |
|
| 976 | + // Future share types need to include their own conditions |
|
| 977 | + $message = 'Share type %s is not valid for %s'; |
|
| 978 | + $message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource)); |
|
| 979 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $shareType, $itemSource), \OCP\Util::DEBUG); |
|
| 980 | + throw new \Exception($message_t); |
|
| 981 | + } |
|
| 982 | + |
|
| 983 | + // Put the item into the database |
|
| 984 | + $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName, $expirationDate); |
|
| 985 | + |
|
| 986 | + return $result ? true : false; |
|
| 987 | + } |
|
| 988 | + |
|
| 989 | + /** |
|
| 990 | + * Unshare an item from a user, group, or delete a private link |
|
| 991 | + * @param string $itemType |
|
| 992 | + * @param string $itemSource |
|
| 993 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 994 | + * @param string $shareWith User or group the item is being shared with |
|
| 995 | + * @param string $owner owner of the share, if null the current user is used |
|
| 996 | + * @return boolean true on success or false on failure |
|
| 997 | + */ |
|
| 998 | + public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) { |
|
| 999 | + |
|
| 1000 | + // check if it is a valid itemType |
|
| 1001 | + self::getBackend($itemType); |
|
| 1002 | + |
|
| 1003 | + $items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType); |
|
| 1004 | + |
|
| 1005 | + $toDelete = array(); |
|
| 1006 | + $newParent = null; |
|
| 1007 | + $currentUser = $owner ? $owner : \OC_User::getUser(); |
|
| 1008 | + foreach ($items as $item) { |
|
| 1009 | + // delete the item with the expected share_type and owner |
|
| 1010 | + if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) { |
|
| 1011 | + $toDelete = $item; |
|
| 1012 | + // if there is more then one result we don't have to delete the children |
|
| 1013 | + // but update their parent. For group shares the new parent should always be |
|
| 1014 | + // the original group share and not the db entry with the unique name |
|
| 1015 | + } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) { |
|
| 1016 | + $newParent = $item['parent']; |
|
| 1017 | + } else { |
|
| 1018 | + $newParent = $item['id']; |
|
| 1019 | + } |
|
| 1020 | + } |
|
| 1021 | + |
|
| 1022 | + if (!empty($toDelete)) { |
|
| 1023 | + self::unshareItem($toDelete, $newParent); |
|
| 1024 | + return true; |
|
| 1025 | + } |
|
| 1026 | + return false; |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + /** |
|
| 1030 | + * Unshare an item from all users, groups, and remove all links |
|
| 1031 | + * @param string $itemType |
|
| 1032 | + * @param string $itemSource |
|
| 1033 | + * @return boolean true on success or false on failure |
|
| 1034 | + */ |
|
| 1035 | + public static function unshareAll($itemType, $itemSource) { |
|
| 1036 | + // Get all of the owners of shares of this item. |
|
| 1037 | + $query = \OC_DB::prepare( 'SELECT `uid_owner` from `*PREFIX*share` WHERE `item_type`=? AND `item_source`=?' ); |
|
| 1038 | + $result = $query->execute(array($itemType, $itemSource)); |
|
| 1039 | + $shares = array(); |
|
| 1040 | + // Add each owner's shares to the array of all shares for this item. |
|
| 1041 | + while ($row = $result->fetchRow()) { |
|
| 1042 | + $shares = array_merge($shares, self::getItems($itemType, $itemSource, null, null, $row['uid_owner'])); |
|
| 1043 | + } |
|
| 1044 | + if (!empty($shares)) { |
|
| 1045 | + // Pass all the vars we have for now, they may be useful |
|
| 1046 | + $hookParams = array( |
|
| 1047 | + 'itemType' => $itemType, |
|
| 1048 | + 'itemSource' => $itemSource, |
|
| 1049 | + 'shares' => $shares, |
|
| 1050 | + ); |
|
| 1051 | + \OC_Hook::emit('OCP\Share', 'pre_unshareAll', $hookParams); |
|
| 1052 | + foreach ($shares as $share) { |
|
| 1053 | + self::unshareItem($share); |
|
| 1054 | + } |
|
| 1055 | + \OC_Hook::emit('OCP\Share', 'post_unshareAll', $hookParams); |
|
| 1056 | + return true; |
|
| 1057 | + } |
|
| 1058 | + return false; |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + /** |
|
| 1062 | + * Unshare an item shared with the current user |
|
| 1063 | + * @param string $itemType |
|
| 1064 | + * @param string $itemOrigin Item target or source |
|
| 1065 | + * @param boolean $originIsSource true if $itemOrigin is the source, false if $itemOrigin is the target (optional) |
|
| 1066 | + * @return boolean true on success or false on failure |
|
| 1067 | + * |
|
| 1068 | + * Unsharing from self is not allowed for items inside collections |
|
| 1069 | + */ |
|
| 1070 | + public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) { |
|
| 1071 | + $originType = ($originIsSource) ? 'source' : 'target'; |
|
| 1072 | + $uid = \OCP\User::getUser(); |
|
| 1073 | + |
|
| 1074 | + if ($itemType === 'file' || $itemType === 'folder') { |
|
| 1075 | + $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_' . $originType . '` = ?'; |
|
| 1076 | + } else { |
|
| 1077 | + $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `item_' . $originType . '` = ?'; |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + $query = \OCP\DB::prepare($statement); |
|
| 1081 | + $result = $query->execute(array($itemType, $itemOrigin)); |
|
| 1082 | + |
|
| 1083 | + $shares = $result->fetchAll(); |
|
| 1084 | + |
|
| 1085 | + $listOfUnsharedItems = array(); |
|
| 1086 | + |
|
| 1087 | + $itemUnshared = false; |
|
| 1088 | + foreach ($shares as $share) { |
|
| 1089 | + if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER && |
|
| 1090 | + $share['share_with'] === $uid) { |
|
| 1091 | + $deletedShares = Helper::delete($share['id']); |
|
| 1092 | + $shareTmp = array( |
|
| 1093 | + 'id' => $share['id'], |
|
| 1094 | + 'shareWith' => $share['share_with'], |
|
| 1095 | + 'itemTarget' => $share['item_target'], |
|
| 1096 | + 'itemType' => $share['item_type'], |
|
| 1097 | + 'shareType' => (int)$share['share_type'], |
|
| 1098 | + ); |
|
| 1099 | + if (isset($share['file_target'])) { |
|
| 1100 | + $shareTmp['fileTarget'] = $share['file_target']; |
|
| 1101 | + } |
|
| 1102 | + $listOfUnsharedItems = array_merge($listOfUnsharedItems, $deletedShares, array($shareTmp)); |
|
| 1103 | + $itemUnshared = true; |
|
| 1104 | + break; |
|
| 1105 | + } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 1106 | + $group = \OC::$server->getGroupManager()->get($share['share_with']); |
|
| 1107 | + $user = \OC::$server->getUserManager()->get($uid); |
|
| 1108 | + if ($group && $user && $group->inGroup($user)) { |
|
| 1109 | + $groupShare = $share; |
|
| 1110 | + } |
|
| 1111 | + } elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique && |
|
| 1112 | + $share['share_with'] === $uid) { |
|
| 1113 | + $uniqueGroupShare = $share; |
|
| 1114 | + } |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + if (!$itemUnshared && isset($groupShare) && !isset($uniqueGroupShare)) { |
|
| 1118 | + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share`' |
|
| 1119 | + .' (`item_type`, `item_source`, `item_target`, `parent`, `share_type`,' |
|
| 1120 | + .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`, `file_target`)' |
|
| 1121 | + .' VALUES (?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 1122 | + $query->execute(array($groupShare['item_type'], $groupShare['item_source'], $groupShare['item_target'], |
|
| 1123 | + $groupShare['id'], self::$shareTypeGroupUserUnique, |
|
| 1124 | + \OC_User::getUser(), $groupShare['uid_owner'], 0, $groupShare['stime'], $groupShare['file_source'], |
|
| 1125 | + $groupShare['file_target'])); |
|
| 1126 | + $shareTmp = array( |
|
| 1127 | + 'id' => $groupShare['id'], |
|
| 1128 | + 'shareWith' => $groupShare['share_with'], |
|
| 1129 | + 'itemTarget' => $groupShare['item_target'], |
|
| 1130 | + 'itemType' => $groupShare['item_type'], |
|
| 1131 | + 'shareType' => (int)$groupShare['share_type'], |
|
| 1132 | + ); |
|
| 1133 | + if (isset($groupShare['file_target'])) { |
|
| 1134 | + $shareTmp['fileTarget'] = $groupShare['file_target']; |
|
| 1135 | + } |
|
| 1136 | + $listOfUnsharedItems = array_merge($listOfUnsharedItems, [$shareTmp]); |
|
| 1137 | + $itemUnshared = true; |
|
| 1138 | + } elseif (!$itemUnshared && isset($uniqueGroupShare)) { |
|
| 1139 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = ? WHERE `id` = ?'); |
|
| 1140 | + $query->execute(array(0, $uniqueGroupShare['id'])); |
|
| 1141 | + $shareTmp = array( |
|
| 1142 | + 'id' => $uniqueGroupShare['id'], |
|
| 1143 | + 'shareWith' => $uniqueGroupShare['share_with'], |
|
| 1144 | + 'itemTarget' => $uniqueGroupShare['item_target'], |
|
| 1145 | + 'itemType' => $uniqueGroupShare['item_type'], |
|
| 1146 | + 'shareType' => (int)$uniqueGroupShare['share_type'], |
|
| 1147 | + ); |
|
| 1148 | + if (isset($uniqueGroupShare['file_target'])) { |
|
| 1149 | + $shareTmp['fileTarget'] = $uniqueGroupShare['file_target']; |
|
| 1150 | + } |
|
| 1151 | + $listOfUnsharedItems = array_merge($listOfUnsharedItems, [$shareTmp]); |
|
| 1152 | + $itemUnshared = true; |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + if ($itemUnshared) { |
|
| 1156 | + \OC_Hook::emit('OCP\Share', 'post_unshareFromSelf', |
|
| 1157 | + array('unsharedItems' => $listOfUnsharedItems, 'itemType' => $itemType)); |
|
| 1158 | + } |
|
| 1159 | + |
|
| 1160 | + return $itemUnshared; |
|
| 1161 | + } |
|
| 1162 | + |
|
| 1163 | + /** |
|
| 1164 | + * sent status if users got informed by mail about share |
|
| 1165 | + * @param string $itemType |
|
| 1166 | + * @param string $itemSource |
|
| 1167 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1168 | + * @param string $recipient with whom was the file shared |
|
| 1169 | + * @param boolean $status |
|
| 1170 | + */ |
|
| 1171 | + public static function setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status) { |
|
| 1172 | + $status = $status ? 1 : 0; |
|
| 1173 | + |
|
| 1174 | + $query = \OC_DB::prepare( |
|
| 1175 | + 'UPDATE `*PREFIX*share` |
|
| 1176 | 1176 | SET `mail_send` = ? |
| 1177 | 1177 | WHERE `item_type` = ? AND `item_source` = ? AND `share_type` = ? AND `share_with` = ?'); |
| 1178 | 1178 | |
| 1179 | - $result = $query->execute(array($status, $itemType, $itemSource, $shareType, $recipient)); |
|
| 1180 | - |
|
| 1181 | - if($result === false) { |
|
| 1182 | - \OCP\Util::writeLog('OCP\Share', 'Couldn\'t set send mail status', \OCP\Util::ERROR); |
|
| 1183 | - } |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - /** |
|
| 1187 | - * Set the permissions of an item for a specific user or group |
|
| 1188 | - * @param string $itemType |
|
| 1189 | - * @param string $itemSource |
|
| 1190 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1191 | - * @param string $shareWith User or group the item is being shared with |
|
| 1192 | - * @param int $permissions CRUDS permissions |
|
| 1193 | - * @return boolean true on success or false on failure |
|
| 1194 | - * @throws \Exception when trying to grant more permissions then the user has himself |
|
| 1195 | - */ |
|
| 1196 | - public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) { |
|
| 1197 | - $l = \OC::$server->getL10N('lib'); |
|
| 1198 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 1199 | - |
|
| 1200 | - $intArrayToLiteralArray = function($intArray, $eb) { |
|
| 1201 | - return array_map(function($int) use ($eb) { |
|
| 1202 | - return $eb->literal((int)$int, 'integer'); |
|
| 1203 | - }, $intArray); |
|
| 1204 | - }; |
|
| 1205 | - $sanitizeItem = function($item) { |
|
| 1206 | - $item['id'] = (int)$item['id']; |
|
| 1207 | - $item['premissions'] = (int)$item['permissions']; |
|
| 1208 | - return $item; |
|
| 1209 | - }; |
|
| 1210 | - |
|
| 1211 | - if ($rootItem = self::getItems($itemType, $itemSource, $shareType, $shareWith, |
|
| 1212 | - \OC_User::getUser(), self::FORMAT_NONE, null, 1, false)) { |
|
| 1213 | - // Check if this item is a reshare and verify that the permissions |
|
| 1214 | - // granted don't exceed the parent shared item |
|
| 1215 | - if (isset($rootItem['parent'])) { |
|
| 1216 | - $qb = $connection->getQueryBuilder(); |
|
| 1217 | - $qb->select('permissions') |
|
| 1218 | - ->from('share') |
|
| 1219 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 1220 | - ->setParameter(':id', $rootItem['parent']); |
|
| 1221 | - $dbresult = $qb->execute(); |
|
| 1222 | - |
|
| 1223 | - $result = $dbresult->fetch(); |
|
| 1224 | - $dbresult->closeCursor(); |
|
| 1225 | - if (~(int)$result['permissions'] & $permissions) { |
|
| 1226 | - $message = 'Setting permissions for %s failed,' |
|
| 1227 | - .' because the permissions exceed permissions granted to %s'; |
|
| 1228 | - $message_t = $l->t('Setting permissions for %s failed, because the permissions exceed permissions granted to %s', array($itemSource, \OC_User::getUser())); |
|
| 1229 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, \OC_User::getUser()), \OCP\Util::DEBUG); |
|
| 1230 | - throw new \Exception($message_t); |
|
| 1231 | - } |
|
| 1232 | - } |
|
| 1233 | - $qb = $connection->getQueryBuilder(); |
|
| 1234 | - $qb->update('share') |
|
| 1235 | - ->set('permissions', $qb->createParameter('permissions')) |
|
| 1236 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 1237 | - ->setParameter(':id', $rootItem['id']) |
|
| 1238 | - ->setParameter(':permissions', $permissions); |
|
| 1239 | - $qb->execute(); |
|
| 1240 | - if ($itemType === 'file' || $itemType === 'folder') { |
|
| 1241 | - \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( |
|
| 1242 | - 'itemType' => $itemType, |
|
| 1243 | - 'itemSource' => $itemSource, |
|
| 1244 | - 'shareType' => $shareType, |
|
| 1245 | - 'shareWith' => $shareWith, |
|
| 1246 | - 'uidOwner' => \OC_User::getUser(), |
|
| 1247 | - 'permissions' => $permissions, |
|
| 1248 | - 'path' => $rootItem['path'], |
|
| 1249 | - 'share' => $rootItem |
|
| 1250 | - )); |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - // Share id's to update with the new permissions |
|
| 1254 | - $ids = []; |
|
| 1255 | - $items = []; |
|
| 1256 | - |
|
| 1257 | - // Check if permissions were removed |
|
| 1258 | - if ((int)$rootItem['permissions'] & ~$permissions) { |
|
| 1259 | - // If share permission is removed all reshares must be deleted |
|
| 1260 | - if (($rootItem['permissions'] & \OCP\Constants::PERMISSION_SHARE) && (~$permissions & \OCP\Constants::PERMISSION_SHARE)) { |
|
| 1261 | - // delete all shares, keep parent and group children |
|
| 1262 | - Helper::delete($rootItem['id'], true, null, null, true); |
|
| 1263 | - } |
|
| 1264 | - |
|
| 1265 | - // Remove permission from all children |
|
| 1266 | - $parents = [$rootItem['id']]; |
|
| 1267 | - while (!empty($parents)) { |
|
| 1268 | - $parents = $intArrayToLiteralArray($parents, $qb->expr()); |
|
| 1269 | - $qb = $connection->getQueryBuilder(); |
|
| 1270 | - $qb->select('id', 'permissions', 'item_type') |
|
| 1271 | - ->from('share') |
|
| 1272 | - ->where($qb->expr()->in('parent', $parents)); |
|
| 1273 | - $result = $qb->execute(); |
|
| 1274 | - // Reset parents array, only go through loop again if |
|
| 1275 | - // items are found that need permissions removed |
|
| 1276 | - $parents = []; |
|
| 1277 | - while ($item = $result->fetch()) { |
|
| 1278 | - $item = $sanitizeItem($item); |
|
| 1279 | - |
|
| 1280 | - $items[] = $item; |
|
| 1281 | - // Check if permissions need to be removed |
|
| 1282 | - if ($item['permissions'] & ~$permissions) { |
|
| 1283 | - // Add to list of items that need permissions removed |
|
| 1284 | - $ids[] = $item['id']; |
|
| 1285 | - $parents[] = $item['id']; |
|
| 1286 | - } |
|
| 1287 | - } |
|
| 1288 | - $result->closeCursor(); |
|
| 1289 | - } |
|
| 1290 | - |
|
| 1291 | - // Remove the permissions for all reshares of this item |
|
| 1292 | - if (!empty($ids)) { |
|
| 1293 | - $ids = "'".implode("','", $ids)."'"; |
|
| 1294 | - // TODO this should be done with Doctrine platform objects |
|
| 1295 | - if (\OC::$server->getConfig()->getSystemValue("dbtype") === 'oci') { |
|
| 1296 | - $andOp = 'BITAND(`permissions`, ?)'; |
|
| 1297 | - } else { |
|
| 1298 | - $andOp = '`permissions` & ?'; |
|
| 1299 | - } |
|
| 1300 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = '.$andOp |
|
| 1301 | - .' WHERE `id` IN ('.$ids.')'); |
|
| 1302 | - $query->execute(array($permissions)); |
|
| 1303 | - } |
|
| 1304 | - |
|
| 1305 | - } |
|
| 1306 | - |
|
| 1307 | - /* |
|
| 1179 | + $result = $query->execute(array($status, $itemType, $itemSource, $shareType, $recipient)); |
|
| 1180 | + |
|
| 1181 | + if($result === false) { |
|
| 1182 | + \OCP\Util::writeLog('OCP\Share', 'Couldn\'t set send mail status', \OCP\Util::ERROR); |
|
| 1183 | + } |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + /** |
|
| 1187 | + * Set the permissions of an item for a specific user or group |
|
| 1188 | + * @param string $itemType |
|
| 1189 | + * @param string $itemSource |
|
| 1190 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1191 | + * @param string $shareWith User or group the item is being shared with |
|
| 1192 | + * @param int $permissions CRUDS permissions |
|
| 1193 | + * @return boolean true on success or false on failure |
|
| 1194 | + * @throws \Exception when trying to grant more permissions then the user has himself |
|
| 1195 | + */ |
|
| 1196 | + public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) { |
|
| 1197 | + $l = \OC::$server->getL10N('lib'); |
|
| 1198 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 1199 | + |
|
| 1200 | + $intArrayToLiteralArray = function($intArray, $eb) { |
|
| 1201 | + return array_map(function($int) use ($eb) { |
|
| 1202 | + return $eb->literal((int)$int, 'integer'); |
|
| 1203 | + }, $intArray); |
|
| 1204 | + }; |
|
| 1205 | + $sanitizeItem = function($item) { |
|
| 1206 | + $item['id'] = (int)$item['id']; |
|
| 1207 | + $item['premissions'] = (int)$item['permissions']; |
|
| 1208 | + return $item; |
|
| 1209 | + }; |
|
| 1210 | + |
|
| 1211 | + if ($rootItem = self::getItems($itemType, $itemSource, $shareType, $shareWith, |
|
| 1212 | + \OC_User::getUser(), self::FORMAT_NONE, null, 1, false)) { |
|
| 1213 | + // Check if this item is a reshare and verify that the permissions |
|
| 1214 | + // granted don't exceed the parent shared item |
|
| 1215 | + if (isset($rootItem['parent'])) { |
|
| 1216 | + $qb = $connection->getQueryBuilder(); |
|
| 1217 | + $qb->select('permissions') |
|
| 1218 | + ->from('share') |
|
| 1219 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 1220 | + ->setParameter(':id', $rootItem['parent']); |
|
| 1221 | + $dbresult = $qb->execute(); |
|
| 1222 | + |
|
| 1223 | + $result = $dbresult->fetch(); |
|
| 1224 | + $dbresult->closeCursor(); |
|
| 1225 | + if (~(int)$result['permissions'] & $permissions) { |
|
| 1226 | + $message = 'Setting permissions for %s failed,' |
|
| 1227 | + .' because the permissions exceed permissions granted to %s'; |
|
| 1228 | + $message_t = $l->t('Setting permissions for %s failed, because the permissions exceed permissions granted to %s', array($itemSource, \OC_User::getUser())); |
|
| 1229 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, \OC_User::getUser()), \OCP\Util::DEBUG); |
|
| 1230 | + throw new \Exception($message_t); |
|
| 1231 | + } |
|
| 1232 | + } |
|
| 1233 | + $qb = $connection->getQueryBuilder(); |
|
| 1234 | + $qb->update('share') |
|
| 1235 | + ->set('permissions', $qb->createParameter('permissions')) |
|
| 1236 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 1237 | + ->setParameter(':id', $rootItem['id']) |
|
| 1238 | + ->setParameter(':permissions', $permissions); |
|
| 1239 | + $qb->execute(); |
|
| 1240 | + if ($itemType === 'file' || $itemType === 'folder') { |
|
| 1241 | + \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( |
|
| 1242 | + 'itemType' => $itemType, |
|
| 1243 | + 'itemSource' => $itemSource, |
|
| 1244 | + 'shareType' => $shareType, |
|
| 1245 | + 'shareWith' => $shareWith, |
|
| 1246 | + 'uidOwner' => \OC_User::getUser(), |
|
| 1247 | + 'permissions' => $permissions, |
|
| 1248 | + 'path' => $rootItem['path'], |
|
| 1249 | + 'share' => $rootItem |
|
| 1250 | + )); |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + // Share id's to update with the new permissions |
|
| 1254 | + $ids = []; |
|
| 1255 | + $items = []; |
|
| 1256 | + |
|
| 1257 | + // Check if permissions were removed |
|
| 1258 | + if ((int)$rootItem['permissions'] & ~$permissions) { |
|
| 1259 | + // If share permission is removed all reshares must be deleted |
|
| 1260 | + if (($rootItem['permissions'] & \OCP\Constants::PERMISSION_SHARE) && (~$permissions & \OCP\Constants::PERMISSION_SHARE)) { |
|
| 1261 | + // delete all shares, keep parent and group children |
|
| 1262 | + Helper::delete($rootItem['id'], true, null, null, true); |
|
| 1263 | + } |
|
| 1264 | + |
|
| 1265 | + // Remove permission from all children |
|
| 1266 | + $parents = [$rootItem['id']]; |
|
| 1267 | + while (!empty($parents)) { |
|
| 1268 | + $parents = $intArrayToLiteralArray($parents, $qb->expr()); |
|
| 1269 | + $qb = $connection->getQueryBuilder(); |
|
| 1270 | + $qb->select('id', 'permissions', 'item_type') |
|
| 1271 | + ->from('share') |
|
| 1272 | + ->where($qb->expr()->in('parent', $parents)); |
|
| 1273 | + $result = $qb->execute(); |
|
| 1274 | + // Reset parents array, only go through loop again if |
|
| 1275 | + // items are found that need permissions removed |
|
| 1276 | + $parents = []; |
|
| 1277 | + while ($item = $result->fetch()) { |
|
| 1278 | + $item = $sanitizeItem($item); |
|
| 1279 | + |
|
| 1280 | + $items[] = $item; |
|
| 1281 | + // Check if permissions need to be removed |
|
| 1282 | + if ($item['permissions'] & ~$permissions) { |
|
| 1283 | + // Add to list of items that need permissions removed |
|
| 1284 | + $ids[] = $item['id']; |
|
| 1285 | + $parents[] = $item['id']; |
|
| 1286 | + } |
|
| 1287 | + } |
|
| 1288 | + $result->closeCursor(); |
|
| 1289 | + } |
|
| 1290 | + |
|
| 1291 | + // Remove the permissions for all reshares of this item |
|
| 1292 | + if (!empty($ids)) { |
|
| 1293 | + $ids = "'".implode("','", $ids)."'"; |
|
| 1294 | + // TODO this should be done with Doctrine platform objects |
|
| 1295 | + if (\OC::$server->getConfig()->getSystemValue("dbtype") === 'oci') { |
|
| 1296 | + $andOp = 'BITAND(`permissions`, ?)'; |
|
| 1297 | + } else { |
|
| 1298 | + $andOp = '`permissions` & ?'; |
|
| 1299 | + } |
|
| 1300 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = '.$andOp |
|
| 1301 | + .' WHERE `id` IN ('.$ids.')'); |
|
| 1302 | + $query->execute(array($permissions)); |
|
| 1303 | + } |
|
| 1304 | + |
|
| 1305 | + } |
|
| 1306 | + |
|
| 1307 | + /* |
|
| 1308 | 1308 | * Permissions were added |
| 1309 | 1309 | * Update all USERGROUP shares. (So group shares where the user moved their mountpoint). |
| 1310 | 1310 | */ |
| 1311 | - if ($permissions & ~(int)$rootItem['permissions']) { |
|
| 1312 | - $qb = $connection->getQueryBuilder(); |
|
| 1313 | - $qb->select('id', 'permissions', 'item_type') |
|
| 1314 | - ->from('share') |
|
| 1315 | - ->where($qb->expr()->eq('parent', $qb->createParameter('parent'))) |
|
| 1316 | - ->andWhere($qb->expr()->eq('share_type', $qb->createParameter('share_type'))) |
|
| 1317 | - ->andWhere($qb->expr()->neq('permissions', $qb->createParameter('shareDeleted'))) |
|
| 1318 | - ->setParameter(':parent', (int)$rootItem['id']) |
|
| 1319 | - ->setParameter(':share_type', 2) |
|
| 1320 | - ->setParameter(':shareDeleted', 0); |
|
| 1321 | - $result = $qb->execute(); |
|
| 1322 | - |
|
| 1323 | - $ids = []; |
|
| 1324 | - while ($item = $result->fetch()) { |
|
| 1325 | - $item = $sanitizeItem($item); |
|
| 1326 | - $items[] = $item; |
|
| 1327 | - $ids[] = $item['id']; |
|
| 1328 | - } |
|
| 1329 | - $result->closeCursor(); |
|
| 1330 | - |
|
| 1331 | - // Add permssions for all USERGROUP shares of this item |
|
| 1332 | - if (!empty($ids)) { |
|
| 1333 | - $ids = $intArrayToLiteralArray($ids, $qb->expr()); |
|
| 1334 | - |
|
| 1335 | - $qb = $connection->getQueryBuilder(); |
|
| 1336 | - $qb->update('share') |
|
| 1337 | - ->set('permissions', $qb->createParameter('permissions')) |
|
| 1338 | - ->where($qb->expr()->in('id', $ids)) |
|
| 1339 | - ->setParameter(':permissions', $permissions); |
|
| 1340 | - $qb->execute(); |
|
| 1341 | - } |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - foreach ($items as $item) { |
|
| 1345 | - \OC_Hook::emit('OCP\Share', 'post_update_permissions', ['share' => $item]); |
|
| 1346 | - } |
|
| 1347 | - |
|
| 1348 | - return true; |
|
| 1349 | - } |
|
| 1350 | - $message = 'Setting permissions for %s failed, because the item was not found'; |
|
| 1351 | - $message_t = $l->t('Setting permissions for %s failed, because the item was not found', array($itemSource)); |
|
| 1352 | - |
|
| 1353 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG); |
|
| 1354 | - throw new \Exception($message_t); |
|
| 1355 | - } |
|
| 1356 | - |
|
| 1357 | - /** |
|
| 1358 | - * validate expiration date if it meets all constraints |
|
| 1359 | - * |
|
| 1360 | - * @param string $expireDate well formatted date string, e.g. "DD-MM-YYYY" |
|
| 1361 | - * @param string $shareTime timestamp when the file was shared |
|
| 1362 | - * @param string $itemType |
|
| 1363 | - * @param string $itemSource |
|
| 1364 | - * @return \DateTime validated date |
|
| 1365 | - * @throws \Exception when the expire date is in the past or further in the future then the enforced date |
|
| 1366 | - */ |
|
| 1367 | - private static function validateExpireDate($expireDate, $shareTime, $itemType, $itemSource) { |
|
| 1368 | - $l = \OC::$server->getL10N('lib'); |
|
| 1369 | - $date = new \DateTime($expireDate); |
|
| 1370 | - $today = new \DateTime('now'); |
|
| 1371 | - |
|
| 1372 | - // if the user doesn't provide a share time we need to get it from the database |
|
| 1373 | - // fall-back mode to keep API stable, because the $shareTime parameter was added later |
|
| 1374 | - $defaultExpireDateEnforced = \OCP\Util::isDefaultExpireDateEnforced(); |
|
| 1375 | - if ($defaultExpireDateEnforced && $shareTime === null) { |
|
| 1376 | - $items = self::getItemShared($itemType, $itemSource); |
|
| 1377 | - $firstItem = reset($items); |
|
| 1378 | - $shareTime = (int)$firstItem['stime']; |
|
| 1379 | - } |
|
| 1380 | - |
|
| 1381 | - if ($defaultExpireDateEnforced) { |
|
| 1382 | - // initialize max date with share time |
|
| 1383 | - $maxDate = new \DateTime(); |
|
| 1384 | - $maxDate->setTimestamp($shareTime); |
|
| 1385 | - $maxDays = \OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1386 | - $maxDate->add(new \DateInterval('P' . $maxDays . 'D')); |
|
| 1387 | - if ($date > $maxDate) { |
|
| 1388 | - $warning = 'Cannot set expiration date. Shares cannot expire later than ' . $maxDays . ' after they have been shared'; |
|
| 1389 | - $warning_t = $l->t('Cannot set expiration date. Shares cannot expire later than %s after they have been shared', array($maxDays)); |
|
| 1390 | - \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN); |
|
| 1391 | - throw new \Exception($warning_t); |
|
| 1392 | - } |
|
| 1393 | - } |
|
| 1394 | - |
|
| 1395 | - if ($date < $today) { |
|
| 1396 | - $message = 'Cannot set expiration date. Expiration date is in the past'; |
|
| 1397 | - $message_t = $l->t('Cannot set expiration date. Expiration date is in the past'); |
|
| 1398 | - \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::WARN); |
|
| 1399 | - throw new \Exception($message_t); |
|
| 1400 | - } |
|
| 1401 | - |
|
| 1402 | - return $date; |
|
| 1403 | - } |
|
| 1404 | - |
|
| 1405 | - /** |
|
| 1406 | - * Set expiration date for a share |
|
| 1407 | - * @param string $itemType |
|
| 1408 | - * @param string $itemSource |
|
| 1409 | - * @param string $date expiration date |
|
| 1410 | - * @param int $shareTime timestamp from when the file was shared |
|
| 1411 | - * @return boolean |
|
| 1412 | - * @throws \Exception when the expire date is not set, in the past or further in the future then the enforced date |
|
| 1413 | - */ |
|
| 1414 | - public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) { |
|
| 1415 | - $user = \OC_User::getUser(); |
|
| 1416 | - $l = \OC::$server->getL10N('lib'); |
|
| 1417 | - |
|
| 1418 | - if ($date == '') { |
|
| 1419 | - if (\OCP\Util::isDefaultExpireDateEnforced()) { |
|
| 1420 | - $warning = 'Cannot clear expiration date. Shares are required to have an expiration date.'; |
|
| 1421 | - $warning_t = $l->t('Cannot clear expiration date. Shares are required to have an expiration date.'); |
|
| 1422 | - \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN); |
|
| 1423 | - throw new \Exception($warning_t); |
|
| 1424 | - } else { |
|
| 1425 | - $date = null; |
|
| 1426 | - } |
|
| 1427 | - } else { |
|
| 1428 | - $date = self::validateExpireDate($date, $shareTime, $itemType, $itemSource); |
|
| 1429 | - } |
|
| 1430 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ? AND `uid_owner` = ? AND `share_type` = ?'); |
|
| 1431 | - $query->bindValue(1, $date, 'datetime'); |
|
| 1432 | - $query->bindValue(2, $itemType); |
|
| 1433 | - $query->bindValue(3, $itemSource); |
|
| 1434 | - $query->bindValue(4, $user); |
|
| 1435 | - $query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK); |
|
| 1436 | - |
|
| 1437 | - $query->execute(); |
|
| 1438 | - |
|
| 1439 | - \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', array( |
|
| 1440 | - 'itemType' => $itemType, |
|
| 1441 | - 'itemSource' => $itemSource, |
|
| 1442 | - 'date' => $date, |
|
| 1443 | - 'uidOwner' => $user |
|
| 1444 | - )); |
|
| 1445 | - |
|
| 1446 | - return true; |
|
| 1447 | - } |
|
| 1448 | - |
|
| 1449 | - /** |
|
| 1450 | - * Retrieve the owner of a connection |
|
| 1451 | - * |
|
| 1452 | - * @param IDBConnection $connection |
|
| 1453 | - * @param int $shareId |
|
| 1454 | - * @throws \Exception |
|
| 1455 | - * @return string uid of share owner |
|
| 1456 | - */ |
|
| 1457 | - private static function getShareOwner(IDBConnection $connection, $shareId) { |
|
| 1458 | - $qb = $connection->getQueryBuilder(); |
|
| 1459 | - |
|
| 1460 | - $qb->select('uid_owner') |
|
| 1461 | - ->from('share') |
|
| 1462 | - ->where($qb->expr()->eq('id', $qb->createParameter('shareId'))) |
|
| 1463 | - ->setParameter(':shareId', $shareId); |
|
| 1464 | - $dbResult = $qb->execute(); |
|
| 1465 | - $result = $dbResult->fetch(); |
|
| 1466 | - $dbResult->closeCursor(); |
|
| 1467 | - |
|
| 1468 | - if (empty($result)) { |
|
| 1469 | - throw new \Exception('Share not found'); |
|
| 1470 | - } |
|
| 1471 | - |
|
| 1472 | - return $result['uid_owner']; |
|
| 1473 | - } |
|
| 1474 | - |
|
| 1475 | - /** |
|
| 1476 | - * Set password for a public link share |
|
| 1477 | - * |
|
| 1478 | - * @param IUserSession $userSession |
|
| 1479 | - * @param IDBConnection $connection |
|
| 1480 | - * @param IConfig $config |
|
| 1481 | - * @param int $shareId |
|
| 1482 | - * @param string $password |
|
| 1483 | - * @throws \Exception |
|
| 1484 | - * @return boolean |
|
| 1485 | - */ |
|
| 1486 | - public static function setPassword(IUserSession $userSession, |
|
| 1487 | - IDBConnection $connection, |
|
| 1488 | - IConfig $config, |
|
| 1489 | - $shareId, $password) { |
|
| 1490 | - $user = $userSession->getUser(); |
|
| 1491 | - if (is_null($user)) { |
|
| 1492 | - throw new \Exception("User not logged in"); |
|
| 1493 | - } |
|
| 1494 | - |
|
| 1495 | - $uid = self::getShareOwner($connection, $shareId); |
|
| 1496 | - |
|
| 1497 | - if ($uid !== $user->getUID()) { |
|
| 1498 | - throw new \Exception('Cannot update share of a different user'); |
|
| 1499 | - } |
|
| 1500 | - |
|
| 1501 | - if ($password === '') { |
|
| 1502 | - $password = null; |
|
| 1503 | - } |
|
| 1504 | - |
|
| 1505 | - //If passwords are enforced the password can't be null |
|
| 1506 | - if (self::enforcePassword($config) && is_null($password)) { |
|
| 1507 | - throw new \Exception('Cannot remove password'); |
|
| 1508 | - } |
|
| 1509 | - |
|
| 1510 | - self::verifyPassword($password); |
|
| 1511 | - |
|
| 1512 | - $qb = $connection->getQueryBuilder(); |
|
| 1513 | - $qb->update('share') |
|
| 1514 | - ->set('share_with', $qb->createParameter('pass')) |
|
| 1515 | - ->where($qb->expr()->eq('id', $qb->createParameter('shareId'))) |
|
| 1516 | - ->setParameter(':pass', is_null($password) ? null : \OC::$server->getHasher()->hash($password)) |
|
| 1517 | - ->setParameter(':shareId', $shareId); |
|
| 1518 | - |
|
| 1519 | - $qb->execute(); |
|
| 1520 | - |
|
| 1521 | - return true; |
|
| 1522 | - } |
|
| 1523 | - |
|
| 1524 | - /** |
|
| 1525 | - * Checks whether a share has expired, calls unshareItem() if yes. |
|
| 1526 | - * @param array $item Share data (usually database row) |
|
| 1527 | - * @return boolean True if item was expired, false otherwise. |
|
| 1528 | - */ |
|
| 1529 | - protected static function expireItem(array $item) { |
|
| 1530 | - |
|
| 1531 | - $result = false; |
|
| 1532 | - |
|
| 1533 | - // only use default expiration date for link shares |
|
| 1534 | - if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 1535 | - |
|
| 1536 | - // calculate expiration date |
|
| 1537 | - if (!empty($item['expiration'])) { |
|
| 1538 | - $userDefinedExpire = new \DateTime($item['expiration']); |
|
| 1539 | - $expires = $userDefinedExpire->getTimestamp(); |
|
| 1540 | - } else { |
|
| 1541 | - $expires = null; |
|
| 1542 | - } |
|
| 1543 | - |
|
| 1544 | - |
|
| 1545 | - // get default expiration settings |
|
| 1546 | - $defaultSettings = Helper::getDefaultExpireSetting(); |
|
| 1547 | - $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires); |
|
| 1548 | - |
|
| 1549 | - |
|
| 1550 | - if (is_int($expires)) { |
|
| 1551 | - $now = time(); |
|
| 1552 | - if ($now > $expires) { |
|
| 1553 | - self::unshareItem($item); |
|
| 1554 | - $result = true; |
|
| 1555 | - } |
|
| 1556 | - } |
|
| 1557 | - } |
|
| 1558 | - return $result; |
|
| 1559 | - } |
|
| 1560 | - |
|
| 1561 | - /** |
|
| 1562 | - * Unshares a share given a share data array |
|
| 1563 | - * @param array $item Share data (usually database row) |
|
| 1564 | - * @param int $newParent parent ID |
|
| 1565 | - * @return null |
|
| 1566 | - */ |
|
| 1567 | - protected static function unshareItem(array $item, $newParent = null) { |
|
| 1568 | - |
|
| 1569 | - $shareType = (int)$item['share_type']; |
|
| 1570 | - $shareWith = null; |
|
| 1571 | - if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) { |
|
| 1572 | - $shareWith = $item['share_with']; |
|
| 1573 | - } |
|
| 1574 | - |
|
| 1575 | - // Pass all the vars we have for now, they may be useful |
|
| 1576 | - $hookParams = array( |
|
| 1577 | - 'id' => $item['id'], |
|
| 1578 | - 'itemType' => $item['item_type'], |
|
| 1579 | - 'itemSource' => $item['item_source'], |
|
| 1580 | - 'shareType' => $shareType, |
|
| 1581 | - 'shareWith' => $shareWith, |
|
| 1582 | - 'itemParent' => $item['parent'], |
|
| 1583 | - 'uidOwner' => $item['uid_owner'], |
|
| 1584 | - ); |
|
| 1585 | - if($item['item_type'] === 'file' || $item['item_type'] === 'folder') { |
|
| 1586 | - $hookParams['fileSource'] = $item['file_source']; |
|
| 1587 | - $hookParams['fileTarget'] = $item['file_target']; |
|
| 1588 | - } |
|
| 1589 | - |
|
| 1590 | - \OC_Hook::emit('OCP\Share', 'pre_unshare', $hookParams); |
|
| 1591 | - $deletedShares = Helper::delete($item['id'], false, null, $newParent); |
|
| 1592 | - $deletedShares[] = $hookParams; |
|
| 1593 | - $hookParams['deletedShares'] = $deletedShares; |
|
| 1594 | - \OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams); |
|
| 1595 | - if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) { |
|
| 1596 | - list(, $remote) = Helper::splitUserRemote($item['share_with']); |
|
| 1597 | - self::sendRemoteUnshare($remote, $item['id'], $item['token']); |
|
| 1598 | - } |
|
| 1599 | - } |
|
| 1600 | - |
|
| 1601 | - /** |
|
| 1602 | - * Get the backend class for the specified item type |
|
| 1603 | - * @param string $itemType |
|
| 1604 | - * @throws \Exception |
|
| 1605 | - * @return \OCP\Share_Backend |
|
| 1606 | - */ |
|
| 1607 | - public static function getBackend($itemType) { |
|
| 1608 | - $l = \OC::$server->getL10N('lib'); |
|
| 1609 | - if (isset(self::$backends[$itemType])) { |
|
| 1610 | - return self::$backends[$itemType]; |
|
| 1611 | - } else if (isset(self::$backendTypes[$itemType]['class'])) { |
|
| 1612 | - $class = self::$backendTypes[$itemType]['class']; |
|
| 1613 | - if (class_exists($class)) { |
|
| 1614 | - self::$backends[$itemType] = new $class; |
|
| 1615 | - if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) { |
|
| 1616 | - $message = 'Sharing backend %s must implement the interface OCP\Share_Backend'; |
|
| 1617 | - $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class)); |
|
| 1618 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR); |
|
| 1619 | - throw new \Exception($message_t); |
|
| 1620 | - } |
|
| 1621 | - return self::$backends[$itemType]; |
|
| 1622 | - } else { |
|
| 1623 | - $message = 'Sharing backend %s not found'; |
|
| 1624 | - $message_t = $l->t('Sharing backend %s not found', array($class)); |
|
| 1625 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR); |
|
| 1626 | - throw new \Exception($message_t); |
|
| 1627 | - } |
|
| 1628 | - } |
|
| 1629 | - $message = 'Sharing backend for %s not found'; |
|
| 1630 | - $message_t = $l->t('Sharing backend for %s not found', array($itemType)); |
|
| 1631 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemType), \OCP\Util::ERROR); |
|
| 1632 | - throw new \Exception($message_t); |
|
| 1633 | - } |
|
| 1634 | - |
|
| 1635 | - /** |
|
| 1636 | - * Check if resharing is allowed |
|
| 1637 | - * @return boolean true if allowed or false |
|
| 1638 | - * |
|
| 1639 | - * Resharing is allowed by default if not configured |
|
| 1640 | - */ |
|
| 1641 | - public static function isResharingAllowed() { |
|
| 1642 | - if (!isset(self::$isResharingAllowed)) { |
|
| 1643 | - if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') { |
|
| 1644 | - self::$isResharingAllowed = true; |
|
| 1645 | - } else { |
|
| 1646 | - self::$isResharingAllowed = false; |
|
| 1647 | - } |
|
| 1648 | - } |
|
| 1649 | - return self::$isResharingAllowed; |
|
| 1650 | - } |
|
| 1651 | - |
|
| 1652 | - /** |
|
| 1653 | - * Get a list of collection item types for the specified item type |
|
| 1654 | - * @param string $itemType |
|
| 1655 | - * @return array |
|
| 1656 | - */ |
|
| 1657 | - private static function getCollectionItemTypes($itemType) { |
|
| 1658 | - $collectionTypes = array($itemType); |
|
| 1659 | - foreach (self::$backendTypes as $type => $backend) { |
|
| 1660 | - if (in_array($backend['collectionOf'], $collectionTypes)) { |
|
| 1661 | - $collectionTypes[] = $type; |
|
| 1662 | - } |
|
| 1663 | - } |
|
| 1664 | - // TODO Add option for collections to be collection of themselves, only 'folder' does it now... |
|
| 1665 | - if (isset(self::$backendTypes[$itemType]) && (!self::getBackend($itemType) instanceof \OCP\Share_Backend_Collection || $itemType != 'folder')) { |
|
| 1666 | - unset($collectionTypes[0]); |
|
| 1667 | - } |
|
| 1668 | - // Return array if collections were found or the item type is a |
|
| 1669 | - // collection itself - collections can be inside collections |
|
| 1670 | - if (count($collectionTypes) > 0) { |
|
| 1671 | - return $collectionTypes; |
|
| 1672 | - } |
|
| 1673 | - return false; |
|
| 1674 | - } |
|
| 1675 | - |
|
| 1676 | - /** |
|
| 1677 | - * Get the owners of items shared with a user. |
|
| 1678 | - * |
|
| 1679 | - * @param string $user The user the items are shared with. |
|
| 1680 | - * @param string $type The type of the items shared with the user. |
|
| 1681 | - * @param boolean $includeCollections Include collection item types (optional) |
|
| 1682 | - * @param boolean $includeOwner include owner in the list of users the item is shared with (optional) |
|
| 1683 | - * @return array |
|
| 1684 | - */ |
|
| 1685 | - public static function getSharedItemsOwners($user, $type, $includeCollections = false, $includeOwner = false) { |
|
| 1686 | - // First, we find out if $type is part of a collection (and if that collection is part of |
|
| 1687 | - // another one and so on). |
|
| 1688 | - $collectionTypes = array(); |
|
| 1689 | - if (!$includeCollections || !$collectionTypes = self::getCollectionItemTypes($type)) { |
|
| 1690 | - $collectionTypes[] = $type; |
|
| 1691 | - } |
|
| 1692 | - |
|
| 1693 | - // Of these collection types, along with our original $type, we make a |
|
| 1694 | - // list of the ones for which a sharing backend has been registered. |
|
| 1695 | - // FIXME: Ideally, we wouldn't need to nest getItemsSharedWith in this loop but just call it |
|
| 1696 | - // with its $includeCollections parameter set to true. Unfortunately, this fails currently. |
|
| 1697 | - $allMaybeSharedItems = array(); |
|
| 1698 | - foreach ($collectionTypes as $collectionType) { |
|
| 1699 | - if (isset(self::$backends[$collectionType])) { |
|
| 1700 | - $allMaybeSharedItems[$collectionType] = self::getItemsSharedWithUser( |
|
| 1701 | - $collectionType, |
|
| 1702 | - $user, |
|
| 1703 | - self::FORMAT_NONE |
|
| 1704 | - ); |
|
| 1705 | - } |
|
| 1706 | - } |
|
| 1707 | - |
|
| 1708 | - $owners = array(); |
|
| 1709 | - if ($includeOwner) { |
|
| 1710 | - $owners[] = $user; |
|
| 1711 | - } |
|
| 1712 | - |
|
| 1713 | - // We take a look at all shared items of the given $type (or of the collections it is part of) |
|
| 1714 | - // and find out their owners. Then, we gather the tags for the original $type from all owners, |
|
| 1715 | - // and return them as elements of a list that look like "Tag (owner)". |
|
| 1716 | - foreach ($allMaybeSharedItems as $collectionType => $maybeSharedItems) { |
|
| 1717 | - foreach ($maybeSharedItems as $sharedItem) { |
|
| 1718 | - if (isset($sharedItem['id'])) { //workaround for https://github.com/owncloud/core/issues/2814 |
|
| 1719 | - $owners[] = $sharedItem['uid_owner']; |
|
| 1720 | - } |
|
| 1721 | - } |
|
| 1722 | - } |
|
| 1723 | - |
|
| 1724 | - return $owners; |
|
| 1725 | - } |
|
| 1726 | - |
|
| 1727 | - /** |
|
| 1728 | - * Get shared items from the database |
|
| 1729 | - * @param string $itemType |
|
| 1730 | - * @param string $item Item source or target (optional) |
|
| 1731 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique |
|
| 1732 | - * @param string $shareWith User or group the item is being shared with |
|
| 1733 | - * @param string $uidOwner User that is the owner of shared items (optional) |
|
| 1734 | - * @param int $format Format to convert items to with formatItems() (optional) |
|
| 1735 | - * @param mixed $parameters to pass to formatItems() (optional) |
|
| 1736 | - * @param int $limit Number of items to return, -1 to return all matches (optional) |
|
| 1737 | - * @param boolean $includeCollections Include collection item types (optional) |
|
| 1738 | - * @param boolean $itemShareWithBySource (optional) |
|
| 1739 | - * @param boolean $checkExpireDate |
|
| 1740 | - * @return array |
|
| 1741 | - * |
|
| 1742 | - * See public functions getItem(s)... for parameter usage |
|
| 1743 | - * |
|
| 1744 | - */ |
|
| 1745 | - public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, |
|
| 1746 | - $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, |
|
| 1747 | - $includeCollections = false, $itemShareWithBySource = false, $checkExpireDate = true) { |
|
| 1748 | - if (!self::isEnabled()) { |
|
| 1749 | - return array(); |
|
| 1750 | - } |
|
| 1751 | - $backend = self::getBackend($itemType); |
|
| 1752 | - $collectionTypes = false; |
|
| 1753 | - // Get filesystem root to add it to the file target and remove from the |
|
| 1754 | - // file source, match file_source with the file cache |
|
| 1755 | - if ($itemType == 'file' || $itemType == 'folder') { |
|
| 1756 | - if(!is_null($uidOwner)) { |
|
| 1757 | - $root = \OC\Files\Filesystem::getRoot(); |
|
| 1758 | - } else { |
|
| 1759 | - $root = ''; |
|
| 1760 | - } |
|
| 1761 | - $where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 1762 | - if (!isset($item)) { |
|
| 1763 | - $where .= ' AND `file_target` IS NOT NULL '; |
|
| 1764 | - } |
|
| 1765 | - $where .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 1766 | - $fileDependent = true; |
|
| 1767 | - $queryArgs = array(); |
|
| 1768 | - } else { |
|
| 1769 | - $fileDependent = false; |
|
| 1770 | - $root = ''; |
|
| 1771 | - $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 1772 | - if ($includeCollections && !isset($item) && $collectionTypes) { |
|
| 1773 | - // If includeCollections is true, find collections of this item type, e.g. a music album contains songs |
|
| 1774 | - if (!in_array($itemType, $collectionTypes)) { |
|
| 1775 | - $itemTypes = array_merge(array($itemType), $collectionTypes); |
|
| 1776 | - } else { |
|
| 1777 | - $itemTypes = $collectionTypes; |
|
| 1778 | - } |
|
| 1779 | - $placeholders = join(',', array_fill(0, count($itemTypes), '?')); |
|
| 1780 | - $where = ' WHERE `item_type` IN ('.$placeholders.'))'; |
|
| 1781 | - $queryArgs = $itemTypes; |
|
| 1782 | - } else { |
|
| 1783 | - $where = ' WHERE `item_type` = ?'; |
|
| 1784 | - $queryArgs = array($itemType); |
|
| 1785 | - } |
|
| 1786 | - } |
|
| 1787 | - if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 1788 | - $where .= ' AND `share_type` != ?'; |
|
| 1789 | - $queryArgs[] = self::SHARE_TYPE_LINK; |
|
| 1790 | - } |
|
| 1791 | - if (isset($shareType)) { |
|
| 1792 | - // Include all user and group items |
|
| 1793 | - if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) { |
|
| 1794 | - $where .= ' AND ((`share_type` in (?, ?) AND `share_with` = ?) '; |
|
| 1795 | - $queryArgs[] = self::SHARE_TYPE_USER; |
|
| 1796 | - $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 1797 | - $queryArgs[] = $shareWith; |
|
| 1798 | - |
|
| 1799 | - $user = \OC::$server->getUserManager()->get($shareWith); |
|
| 1800 | - $groups = []; |
|
| 1801 | - if ($user) { |
|
| 1802 | - $groups = \OC::$server->getGroupManager()->getUserGroupIds($user); |
|
| 1803 | - } |
|
| 1804 | - if (!empty($groups)) { |
|
| 1805 | - $placeholders = join(',', array_fill(0, count($groups), '?')); |
|
| 1806 | - $where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) '; |
|
| 1807 | - $queryArgs[] = self::SHARE_TYPE_GROUP; |
|
| 1808 | - $queryArgs = array_merge($queryArgs, $groups); |
|
| 1809 | - } |
|
| 1810 | - $where .= ')'; |
|
| 1811 | - // Don't include own group shares |
|
| 1812 | - $where .= ' AND `uid_owner` != ?'; |
|
| 1813 | - $queryArgs[] = $shareWith; |
|
| 1814 | - } else { |
|
| 1815 | - $where .= ' AND `share_type` = ?'; |
|
| 1816 | - $queryArgs[] = $shareType; |
|
| 1817 | - if (isset($shareWith)) { |
|
| 1818 | - $where .= ' AND `share_with` = ?'; |
|
| 1819 | - $queryArgs[] = $shareWith; |
|
| 1820 | - } |
|
| 1821 | - } |
|
| 1822 | - } |
|
| 1823 | - if (isset($uidOwner)) { |
|
| 1824 | - $where .= ' AND `uid_owner` = ?'; |
|
| 1825 | - $queryArgs[] = $uidOwner; |
|
| 1826 | - if (!isset($shareType)) { |
|
| 1827 | - // Prevent unique user targets for group shares from being selected |
|
| 1828 | - $where .= ' AND `share_type` != ?'; |
|
| 1829 | - $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 1830 | - } |
|
| 1831 | - if ($fileDependent) { |
|
| 1832 | - $column = 'file_source'; |
|
| 1833 | - } else { |
|
| 1834 | - $column = 'item_source'; |
|
| 1835 | - } |
|
| 1836 | - } else { |
|
| 1837 | - if ($fileDependent) { |
|
| 1838 | - $column = 'file_target'; |
|
| 1839 | - } else { |
|
| 1840 | - $column = 'item_target'; |
|
| 1841 | - } |
|
| 1842 | - } |
|
| 1843 | - if (isset($item)) { |
|
| 1844 | - $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 1845 | - if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 1846 | - $where .= ' AND ('; |
|
| 1847 | - } else { |
|
| 1848 | - $where .= ' AND'; |
|
| 1849 | - } |
|
| 1850 | - // If looking for own shared items, check item_source else check item_target |
|
| 1851 | - if (isset($uidOwner) || $itemShareWithBySource) { |
|
| 1852 | - // If item type is a file, file source needs to be checked in case the item was converted |
|
| 1853 | - if ($fileDependent) { |
|
| 1854 | - $where .= ' `file_source` = ?'; |
|
| 1855 | - $column = 'file_source'; |
|
| 1856 | - } else { |
|
| 1857 | - $where .= ' `item_source` = ?'; |
|
| 1858 | - $column = 'item_source'; |
|
| 1859 | - } |
|
| 1860 | - } else { |
|
| 1861 | - if ($fileDependent) { |
|
| 1862 | - $where .= ' `file_target` = ?'; |
|
| 1863 | - $item = \OC\Files\Filesystem::normalizePath($item); |
|
| 1864 | - } else { |
|
| 1865 | - $where .= ' `item_target` = ?'; |
|
| 1866 | - } |
|
| 1867 | - } |
|
| 1868 | - $queryArgs[] = $item; |
|
| 1869 | - if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 1870 | - $placeholders = join(',', array_fill(0, count($collectionTypes), '?')); |
|
| 1871 | - $where .= ' OR `item_type` IN ('.$placeholders.'))'; |
|
| 1872 | - $queryArgs = array_merge($queryArgs, $collectionTypes); |
|
| 1873 | - } |
|
| 1874 | - } |
|
| 1875 | - |
|
| 1876 | - if ($shareType == self::$shareTypeUserAndGroups && $limit === 1) { |
|
| 1877 | - // Make sure the unique user target is returned if it exists, |
|
| 1878 | - // unique targets should follow the group share in the database |
|
| 1879 | - // If the limit is not 1, the filtering can be done later |
|
| 1880 | - $where .= ' ORDER BY `*PREFIX*share`.`id` DESC'; |
|
| 1881 | - } else { |
|
| 1882 | - $where .= ' ORDER BY `*PREFIX*share`.`id` ASC'; |
|
| 1883 | - } |
|
| 1884 | - |
|
| 1885 | - if ($limit != -1 && !$includeCollections) { |
|
| 1886 | - // The limit must be at least 3, because filtering needs to be done |
|
| 1887 | - if ($limit < 3) { |
|
| 1888 | - $queryLimit = 3; |
|
| 1889 | - } else { |
|
| 1890 | - $queryLimit = $limit; |
|
| 1891 | - } |
|
| 1892 | - } else { |
|
| 1893 | - $queryLimit = null; |
|
| 1894 | - } |
|
| 1895 | - $select = self::createSelectStatement($format, $fileDependent, $uidOwner); |
|
| 1896 | - $root = strlen($root); |
|
| 1897 | - $query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$where, $queryLimit); |
|
| 1898 | - $result = $query->execute($queryArgs); |
|
| 1899 | - if ($result === false) { |
|
| 1900 | - \OCP\Util::writeLog('OCP\Share', |
|
| 1901 | - \OC_DB::getErrorMessage() . ', select=' . $select . ' where=', |
|
| 1902 | - \OCP\Util::ERROR); |
|
| 1903 | - } |
|
| 1904 | - $items = array(); |
|
| 1905 | - $targets = array(); |
|
| 1906 | - $switchedItems = array(); |
|
| 1907 | - $mounts = array(); |
|
| 1908 | - while ($row = $result->fetchRow()) { |
|
| 1909 | - self::transformDBResults($row); |
|
| 1910 | - // Filter out duplicate group shares for users with unique targets |
|
| 1911 | - if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 1912 | - continue; |
|
| 1913 | - } |
|
| 1914 | - if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) { |
|
| 1915 | - $row['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 1916 | - $row['unique_name'] = true; // remember that we use a unique name for this user |
|
| 1917 | - $row['share_with'] = $items[$row['parent']]['share_with']; |
|
| 1918 | - // if the group share was unshared from the user we keep the permission, otherwise |
|
| 1919 | - // we take the permission from the parent because this is always the up-to-date |
|
| 1920 | - // permission for the group share |
|
| 1921 | - if ($row['permissions'] > 0) { |
|
| 1922 | - $row['permissions'] = $items[$row['parent']]['permissions']; |
|
| 1923 | - } |
|
| 1924 | - // Remove the parent group share |
|
| 1925 | - unset($items[$row['parent']]); |
|
| 1926 | - if ($row['permissions'] == 0) { |
|
| 1927 | - continue; |
|
| 1928 | - } |
|
| 1929 | - } else if (!isset($uidOwner)) { |
|
| 1930 | - // Check if the same target already exists |
|
| 1931 | - if (isset($targets[$row['id']])) { |
|
| 1932 | - // Check if the same owner shared with the user twice |
|
| 1933 | - // through a group and user share - this is allowed |
|
| 1934 | - $id = $targets[$row['id']]; |
|
| 1935 | - if (isset($items[$id]) && $items[$id]['uid_owner'] == $row['uid_owner']) { |
|
| 1936 | - // Switch to group share type to ensure resharing conditions aren't bypassed |
|
| 1937 | - if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) { |
|
| 1938 | - $items[$id]['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 1939 | - $items[$id]['share_with'] = $row['share_with']; |
|
| 1940 | - } |
|
| 1941 | - // Switch ids if sharing permission is granted on only |
|
| 1942 | - // one share to ensure correct parent is used if resharing |
|
| 1943 | - if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE |
|
| 1944 | - && (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 1945 | - $items[$row['id']] = $items[$id]; |
|
| 1946 | - $switchedItems[$id] = $row['id']; |
|
| 1947 | - unset($items[$id]); |
|
| 1948 | - $id = $row['id']; |
|
| 1949 | - } |
|
| 1950 | - $items[$id]['permissions'] |= (int)$row['permissions']; |
|
| 1951 | - |
|
| 1952 | - } |
|
| 1953 | - continue; |
|
| 1954 | - } elseif (!empty($row['parent'])) { |
|
| 1955 | - $targets[$row['parent']] = $row['id']; |
|
| 1956 | - } |
|
| 1957 | - } |
|
| 1958 | - // Remove root from file source paths if retrieving own shared items |
|
| 1959 | - if (isset($uidOwner) && isset($row['path'])) { |
|
| 1960 | - if (isset($row['parent'])) { |
|
| 1961 | - $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); |
|
| 1962 | - $parentResult = $query->execute(array($row['parent'])); |
|
| 1963 | - if ($result === false) { |
|
| 1964 | - \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' . |
|
| 1965 | - \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where, |
|
| 1966 | - \OCP\Util::ERROR); |
|
| 1967 | - } else { |
|
| 1968 | - $parentRow = $parentResult->fetchRow(); |
|
| 1969 | - $tmpPath = $parentRow['file_target']; |
|
| 1970 | - // find the right position where the row path continues from the target path |
|
| 1971 | - $pos = strrpos($row['path'], $parentRow['file_target']); |
|
| 1972 | - $subPath = substr($row['path'], $pos); |
|
| 1973 | - $splitPath = explode('/', $subPath); |
|
| 1974 | - foreach (array_slice($splitPath, 2) as $pathPart) { |
|
| 1975 | - $tmpPath = $tmpPath . '/' . $pathPart; |
|
| 1976 | - } |
|
| 1977 | - $row['path'] = $tmpPath; |
|
| 1978 | - } |
|
| 1979 | - } else { |
|
| 1980 | - if (!isset($mounts[$row['storage']])) { |
|
| 1981 | - $mountPoints = \OC\Files\Filesystem::getMountByNumericId($row['storage']); |
|
| 1982 | - if (is_array($mountPoints) && !empty($mountPoints)) { |
|
| 1983 | - $mounts[$row['storage']] = current($mountPoints); |
|
| 1984 | - } |
|
| 1985 | - } |
|
| 1986 | - if (!empty($mounts[$row['storage']])) { |
|
| 1987 | - $path = $mounts[$row['storage']]->getMountPoint().$row['path']; |
|
| 1988 | - $relPath = substr($path, $root); // path relative to data/user |
|
| 1989 | - $row['path'] = rtrim($relPath, '/'); |
|
| 1990 | - } |
|
| 1991 | - } |
|
| 1992 | - } |
|
| 1993 | - |
|
| 1994 | - if($checkExpireDate) { |
|
| 1995 | - if (self::expireItem($row)) { |
|
| 1996 | - continue; |
|
| 1997 | - } |
|
| 1998 | - } |
|
| 1999 | - // Check if resharing is allowed, if not remove share permission |
|
| 2000 | - if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) { |
|
| 2001 | - $row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE; |
|
| 2002 | - } |
|
| 2003 | - // Add display names to result |
|
| 2004 | - $row['share_with_displayname'] = $row['share_with']; |
|
| 2005 | - if ( isset($row['share_with']) && $row['share_with'] != '' && |
|
| 2006 | - $row['share_type'] === self::SHARE_TYPE_USER) { |
|
| 2007 | - $row['share_with_displayname'] = \OCP\User::getDisplayName($row['share_with']); |
|
| 2008 | - } else if(isset($row['share_with']) && $row['share_with'] != '' && |
|
| 2009 | - $row['share_type'] === self::SHARE_TYPE_REMOTE) { |
|
| 2010 | - $addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']); |
|
| 2011 | - foreach ($addressBookEntries as $entry) { |
|
| 2012 | - foreach ($entry['CLOUD'] as $cloudID) { |
|
| 2013 | - if ($cloudID === $row['share_with']) { |
|
| 2014 | - $row['share_with_displayname'] = $entry['FN']; |
|
| 2015 | - } |
|
| 2016 | - } |
|
| 2017 | - } |
|
| 2018 | - } |
|
| 2019 | - if ( isset($row['uid_owner']) && $row['uid_owner'] != '') { |
|
| 2020 | - $row['displayname_owner'] = \OCP\User::getDisplayName($row['uid_owner']); |
|
| 2021 | - } |
|
| 2022 | - |
|
| 2023 | - if ($row['permissions'] > 0) { |
|
| 2024 | - $items[$row['id']] = $row; |
|
| 2025 | - } |
|
| 2026 | - |
|
| 2027 | - } |
|
| 2028 | - |
|
| 2029 | - // group items if we are looking for items shared with the current user |
|
| 2030 | - if (isset($shareWith) && $shareWith === \OCP\User::getUser()) { |
|
| 2031 | - $items = self::groupItems($items, $itemType); |
|
| 2032 | - } |
|
| 2033 | - |
|
| 2034 | - if (!empty($items)) { |
|
| 2035 | - $collectionItems = array(); |
|
| 2036 | - foreach ($items as &$row) { |
|
| 2037 | - // Return only the item instead of a 2-dimensional array |
|
| 2038 | - if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) { |
|
| 2039 | - if ($format == self::FORMAT_NONE) { |
|
| 2040 | - return $row; |
|
| 2041 | - } else { |
|
| 2042 | - break; |
|
| 2043 | - } |
|
| 2044 | - } |
|
| 2045 | - // Check if this is a collection of the requested item type |
|
| 2046 | - if ($includeCollections && $collectionTypes && $row['item_type'] !== 'folder' && in_array($row['item_type'], $collectionTypes)) { |
|
| 2047 | - if (($collectionBackend = self::getBackend($row['item_type'])) |
|
| 2048 | - && $collectionBackend instanceof \OCP\Share_Backend_Collection) { |
|
| 2049 | - // Collections can be inside collections, check if the item is a collection |
|
| 2050 | - if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) { |
|
| 2051 | - $collectionItems[] = $row; |
|
| 2052 | - } else { |
|
| 2053 | - $collection = array(); |
|
| 2054 | - $collection['item_type'] = $row['item_type']; |
|
| 2055 | - if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 2056 | - $collection['path'] = basename($row['path']); |
|
| 2057 | - } |
|
| 2058 | - $row['collection'] = $collection; |
|
| 2059 | - // Fetch all of the children sources |
|
| 2060 | - $children = $collectionBackend->getChildren($row[$column]); |
|
| 2061 | - foreach ($children as $child) { |
|
| 2062 | - $childItem = $row; |
|
| 2063 | - $childItem['item_type'] = $itemType; |
|
| 2064 | - if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') { |
|
| 2065 | - $childItem['item_source'] = $child['source']; |
|
| 2066 | - $childItem['item_target'] = $child['target']; |
|
| 2067 | - } |
|
| 2068 | - if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 2069 | - if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 2070 | - $childItem['file_source'] = $child['source']; |
|
| 2071 | - } else { // TODO is this really needed if we already know that we use the file backend? |
|
| 2072 | - $meta = \OC\Files\Filesystem::getFileInfo($child['file_path']); |
|
| 2073 | - $childItem['file_source'] = $meta['fileid']; |
|
| 2074 | - } |
|
| 2075 | - $childItem['file_target'] = |
|
| 2076 | - \OC\Files\Filesystem::normalizePath($child['file_path']); |
|
| 2077 | - } |
|
| 2078 | - if (isset($item)) { |
|
| 2079 | - if ($childItem[$column] == $item) { |
|
| 2080 | - // Return only the item instead of a 2-dimensional array |
|
| 2081 | - if ($limit == 1) { |
|
| 2082 | - if ($format == self::FORMAT_NONE) { |
|
| 2083 | - return $childItem; |
|
| 2084 | - } else { |
|
| 2085 | - // Unset the items array and break out of both loops |
|
| 2086 | - $items = array(); |
|
| 2087 | - $items[] = $childItem; |
|
| 2088 | - break 2; |
|
| 2089 | - } |
|
| 2090 | - } else { |
|
| 2091 | - $collectionItems[] = $childItem; |
|
| 2092 | - } |
|
| 2093 | - } |
|
| 2094 | - } else { |
|
| 2095 | - $collectionItems[] = $childItem; |
|
| 2096 | - } |
|
| 2097 | - } |
|
| 2098 | - } |
|
| 2099 | - } |
|
| 2100 | - // Remove collection item |
|
| 2101 | - $toRemove = $row['id']; |
|
| 2102 | - if (array_key_exists($toRemove, $switchedItems)) { |
|
| 2103 | - $toRemove = $switchedItems[$toRemove]; |
|
| 2104 | - } |
|
| 2105 | - unset($items[$toRemove]); |
|
| 2106 | - } elseif ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) { |
|
| 2107 | - // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 2108 | - // see github issue #10588 for more details |
|
| 2109 | - // Need to find a solution which works for all back-ends |
|
| 2110 | - $collectionBackend = self::getBackend($row['item_type']); |
|
| 2111 | - $sharedParents = $collectionBackend->getParents($row['item_source']); |
|
| 2112 | - foreach ($sharedParents as $parent) { |
|
| 2113 | - $collectionItems[] = $parent; |
|
| 2114 | - } |
|
| 2115 | - } |
|
| 2116 | - } |
|
| 2117 | - if (!empty($collectionItems)) { |
|
| 2118 | - $collectionItems = array_unique($collectionItems, SORT_REGULAR); |
|
| 2119 | - $items = array_merge($items, $collectionItems); |
|
| 2120 | - } |
|
| 2121 | - |
|
| 2122 | - // filter out invalid items, these can appear when subshare entries exist |
|
| 2123 | - // for a group in which the requested user isn't a member any more |
|
| 2124 | - $items = array_filter($items, function($item) { |
|
| 2125 | - return $item['share_type'] !== self::$shareTypeGroupUserUnique; |
|
| 2126 | - }); |
|
| 2127 | - |
|
| 2128 | - return self::formatResult($items, $column, $backend, $format, $parameters); |
|
| 2129 | - } elseif ($includeCollections && $collectionTypes && in_array('folder', $collectionTypes)) { |
|
| 2130 | - // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 2131 | - // see github issue #10588 for more details |
|
| 2132 | - // Need to find a solution which works for all back-ends |
|
| 2133 | - $collectionItems = array(); |
|
| 2134 | - $collectionBackend = self::getBackend('folder'); |
|
| 2135 | - $sharedParents = $collectionBackend->getParents($item, $shareWith, $uidOwner); |
|
| 2136 | - foreach ($sharedParents as $parent) { |
|
| 2137 | - $collectionItems[] = $parent; |
|
| 2138 | - } |
|
| 2139 | - if ($limit === 1) { |
|
| 2140 | - return reset($collectionItems); |
|
| 2141 | - } |
|
| 2142 | - return self::formatResult($collectionItems, $column, $backend, $format, $parameters); |
|
| 2143 | - } |
|
| 2144 | - |
|
| 2145 | - return array(); |
|
| 2146 | - } |
|
| 2147 | - |
|
| 2148 | - /** |
|
| 2149 | - * group items with link to the same source |
|
| 2150 | - * |
|
| 2151 | - * @param array $items |
|
| 2152 | - * @param string $itemType |
|
| 2153 | - * @return array of grouped items |
|
| 2154 | - */ |
|
| 2155 | - protected static function groupItems($items, $itemType) { |
|
| 2156 | - |
|
| 2157 | - $fileSharing = ($itemType === 'file' || $itemType === 'folder') ? true : false; |
|
| 2158 | - |
|
| 2159 | - $result = array(); |
|
| 2160 | - |
|
| 2161 | - foreach ($items as $item) { |
|
| 2162 | - $grouped = false; |
|
| 2163 | - foreach ($result as $key => $r) { |
|
| 2164 | - // for file/folder shares we need to compare file_source, otherwise we compare item_source |
|
| 2165 | - // only group shares if they already point to the same target, otherwise the file where shared |
|
| 2166 | - // before grouping of shares was added. In this case we don't group them toi avoid confusions |
|
| 2167 | - if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) || |
|
| 2168 | - (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) { |
|
| 2169 | - // add the first item to the list of grouped shares |
|
| 2170 | - if (!isset($result[$key]['grouped'])) { |
|
| 2171 | - $result[$key]['grouped'][] = $result[$key]; |
|
| 2172 | - } |
|
| 2173 | - $result[$key]['permissions'] = (int) $item['permissions'] | (int) $r['permissions']; |
|
| 2174 | - $result[$key]['grouped'][] = $item; |
|
| 2175 | - $grouped = true; |
|
| 2176 | - break; |
|
| 2177 | - } |
|
| 2178 | - } |
|
| 2179 | - |
|
| 2180 | - if (!$grouped) { |
|
| 2181 | - $result[] = $item; |
|
| 2182 | - } |
|
| 2183 | - |
|
| 2184 | - } |
|
| 2185 | - |
|
| 2186 | - return $result; |
|
| 2187 | - } |
|
| 2188 | - |
|
| 2189 | - /** |
|
| 2190 | - * Put shared item into the database |
|
| 2191 | - * @param string $itemType Item type |
|
| 2192 | - * @param string $itemSource Item source |
|
| 2193 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 2194 | - * @param string $shareWith User or group the item is being shared with |
|
| 2195 | - * @param string $uidOwner User that is the owner of shared item |
|
| 2196 | - * @param int $permissions CRUDS permissions |
|
| 2197 | - * @param boolean|array $parentFolder Parent folder target (optional) |
|
| 2198 | - * @param string $token (optional) |
|
| 2199 | - * @param string $itemSourceName name of the source item (optional) |
|
| 2200 | - * @param \DateTime $expirationDate (optional) |
|
| 2201 | - * @throws \Exception |
|
| 2202 | - * @return mixed id of the new share or false |
|
| 2203 | - */ |
|
| 2204 | - private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 2205 | - $permissions, $parentFolder = null, $token = null, $itemSourceName = null, \DateTime $expirationDate = null) { |
|
| 2206 | - |
|
| 2207 | - $queriesToExecute = array(); |
|
| 2208 | - $suggestedItemTarget = null; |
|
| 2209 | - $groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = ''; |
|
| 2210 | - $groupItemTarget = $itemTarget = $fileSource = $parent = 0; |
|
| 2211 | - |
|
| 2212 | - $result = self::checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate); |
|
| 2213 | - if(!empty($result)) { |
|
| 2214 | - $parent = $result['parent']; |
|
| 2215 | - $itemSource = $result['itemSource']; |
|
| 2216 | - $fileSource = $result['fileSource']; |
|
| 2217 | - $suggestedItemTarget = $result['suggestedItemTarget']; |
|
| 2218 | - $suggestedFileTarget = $result['suggestedFileTarget']; |
|
| 2219 | - $filePath = $result['filePath']; |
|
| 2220 | - } |
|
| 2221 | - |
|
| 2222 | - $isGroupShare = false; |
|
| 2223 | - if ($shareType == self::SHARE_TYPE_GROUP) { |
|
| 2224 | - $isGroupShare = true; |
|
| 2225 | - if (isset($shareWith['users'])) { |
|
| 2226 | - $users = $shareWith['users']; |
|
| 2227 | - } else { |
|
| 2228 | - $group = \OC::$server->getGroupManager()->get($shareWith['group']); |
|
| 2229 | - if ($group) { |
|
| 2230 | - $users = $group->searchUsers('', -1, 0); |
|
| 2231 | - $userIds = []; |
|
| 2232 | - foreach ($users as $user) { |
|
| 2233 | - $userIds[] = $user->getUID(); |
|
| 2234 | - } |
|
| 2235 | - $users = $userIds; |
|
| 2236 | - } else { |
|
| 2237 | - $users = []; |
|
| 2238 | - } |
|
| 2239 | - } |
|
| 2240 | - // remove current user from list |
|
| 2241 | - if (in_array(\OCP\User::getUser(), $users)) { |
|
| 2242 | - unset($users[array_search(\OCP\User::getUser(), $users)]); |
|
| 2243 | - } |
|
| 2244 | - $groupItemTarget = Helper::generateTarget($itemType, $itemSource, |
|
| 2245 | - $shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget); |
|
| 2246 | - $groupFileTarget = Helper::generateTarget($itemType, $itemSource, |
|
| 2247 | - $shareType, $shareWith['group'], $uidOwner, $filePath); |
|
| 2248 | - |
|
| 2249 | - // add group share to table and remember the id as parent |
|
| 2250 | - $queriesToExecute['groupShare'] = array( |
|
| 2251 | - 'itemType' => $itemType, |
|
| 2252 | - 'itemSource' => $itemSource, |
|
| 2253 | - 'itemTarget' => $groupItemTarget, |
|
| 2254 | - 'shareType' => $shareType, |
|
| 2255 | - 'shareWith' => $shareWith['group'], |
|
| 2256 | - 'uidOwner' => $uidOwner, |
|
| 2257 | - 'permissions' => $permissions, |
|
| 2258 | - 'shareTime' => time(), |
|
| 2259 | - 'fileSource' => $fileSource, |
|
| 2260 | - 'fileTarget' => $groupFileTarget, |
|
| 2261 | - 'token' => $token, |
|
| 2262 | - 'parent' => $parent, |
|
| 2263 | - 'expiration' => $expirationDate, |
|
| 2264 | - ); |
|
| 2265 | - |
|
| 2266 | - } else { |
|
| 2267 | - $users = array($shareWith); |
|
| 2268 | - $itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 2269 | - $suggestedItemTarget); |
|
| 2270 | - } |
|
| 2271 | - |
|
| 2272 | - $run = true; |
|
| 2273 | - $error = ''; |
|
| 2274 | - $preHookData = array( |
|
| 2275 | - 'itemType' => $itemType, |
|
| 2276 | - 'itemSource' => $itemSource, |
|
| 2277 | - 'shareType' => $shareType, |
|
| 2278 | - 'uidOwner' => $uidOwner, |
|
| 2279 | - 'permissions' => $permissions, |
|
| 2280 | - 'fileSource' => $fileSource, |
|
| 2281 | - 'expiration' => $expirationDate, |
|
| 2282 | - 'token' => $token, |
|
| 2283 | - 'run' => &$run, |
|
| 2284 | - 'error' => &$error |
|
| 2285 | - ); |
|
| 2286 | - |
|
| 2287 | - $preHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget; |
|
| 2288 | - $preHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith; |
|
| 2289 | - |
|
| 2290 | - \OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData); |
|
| 2291 | - |
|
| 2292 | - if ($run === false) { |
|
| 2293 | - throw new \Exception($error); |
|
| 2294 | - } |
|
| 2295 | - |
|
| 2296 | - foreach ($users as $user) { |
|
| 2297 | - $sourceId = ($itemType === 'file' || $itemType === 'folder') ? $fileSource : $itemSource; |
|
| 2298 | - $sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user); |
|
| 2299 | - |
|
| 2300 | - $userShareType = ($isGroupShare) ? self::$shareTypeGroupUserUnique : $shareType; |
|
| 2301 | - |
|
| 2302 | - if ($sourceExists && $sourceExists['item_source'] === $itemSource) { |
|
| 2303 | - $fileTarget = $sourceExists['file_target']; |
|
| 2304 | - $itemTarget = $sourceExists['item_target']; |
|
| 2305 | - |
|
| 2306 | - // for group shares we don't need a additional entry if the target is the same |
|
| 2307 | - if($isGroupShare && $groupItemTarget === $itemTarget) { |
|
| 2308 | - continue; |
|
| 2309 | - } |
|
| 2310 | - |
|
| 2311 | - } elseif(!$sourceExists && !$isGroupShare) { |
|
| 2312 | - |
|
| 2313 | - $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user, |
|
| 2314 | - $uidOwner, $suggestedItemTarget, $parent); |
|
| 2315 | - if (isset($fileSource)) { |
|
| 2316 | - if ($parentFolder) { |
|
| 2317 | - if ($parentFolder === true) { |
|
| 2318 | - $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, $user, |
|
| 2319 | - $uidOwner, $suggestedFileTarget, $parent); |
|
| 2320 | - if ($fileTarget != $groupFileTarget) { |
|
| 2321 | - $parentFolders[$user]['folder'] = $fileTarget; |
|
| 2322 | - } |
|
| 2323 | - } else if (isset($parentFolder[$user])) { |
|
| 2324 | - $fileTarget = $parentFolder[$user]['folder'].$itemSource; |
|
| 2325 | - $parent = $parentFolder[$user]['id']; |
|
| 2326 | - } |
|
| 2327 | - } else { |
|
| 2328 | - $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, |
|
| 2329 | - $user, $uidOwner, $suggestedFileTarget, $parent); |
|
| 2330 | - } |
|
| 2331 | - } else { |
|
| 2332 | - $fileTarget = null; |
|
| 2333 | - } |
|
| 2334 | - |
|
| 2335 | - } else { |
|
| 2336 | - |
|
| 2337 | - // group share which doesn't exists until now, check if we need a unique target for this user |
|
| 2338 | - |
|
| 2339 | - $itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user, |
|
| 2340 | - $uidOwner, $suggestedItemTarget, $parent); |
|
| 2341 | - |
|
| 2342 | - // do we also need a file target |
|
| 2343 | - if (isset($fileSource)) { |
|
| 2344 | - $fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user, |
|
| 2345 | - $uidOwner, $suggestedFileTarget, $parent); |
|
| 2346 | - } else { |
|
| 2347 | - $fileTarget = null; |
|
| 2348 | - } |
|
| 2349 | - |
|
| 2350 | - if (($itemTarget === $groupItemTarget) && |
|
| 2351 | - (!isset($fileSource) || $fileTarget === $groupFileTarget)) { |
|
| 2352 | - continue; |
|
| 2353 | - } |
|
| 2354 | - } |
|
| 2355 | - |
|
| 2356 | - $queriesToExecute[] = array( |
|
| 2357 | - 'itemType' => $itemType, |
|
| 2358 | - 'itemSource' => $itemSource, |
|
| 2359 | - 'itemTarget' => $itemTarget, |
|
| 2360 | - 'shareType' => $userShareType, |
|
| 2361 | - 'shareWith' => $user, |
|
| 2362 | - 'uidOwner' => $uidOwner, |
|
| 2363 | - 'permissions' => $permissions, |
|
| 2364 | - 'shareTime' => time(), |
|
| 2365 | - 'fileSource' => $fileSource, |
|
| 2366 | - 'fileTarget' => $fileTarget, |
|
| 2367 | - 'token' => $token, |
|
| 2368 | - 'parent' => $parent, |
|
| 2369 | - 'expiration' => $expirationDate, |
|
| 2370 | - ); |
|
| 2371 | - |
|
| 2372 | - } |
|
| 2373 | - |
|
| 2374 | - $id = false; |
|
| 2375 | - if ($isGroupShare) { |
|
| 2376 | - $id = self::insertShare($queriesToExecute['groupShare']); |
|
| 2377 | - // Save this id, any extra rows for this group share will need to reference it |
|
| 2378 | - $parent = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 2379 | - unset($queriesToExecute['groupShare']); |
|
| 2380 | - } |
|
| 2381 | - |
|
| 2382 | - foreach ($queriesToExecute as $shareQuery) { |
|
| 2383 | - $shareQuery['parent'] = $parent; |
|
| 2384 | - $id = self::insertShare($shareQuery); |
|
| 2385 | - } |
|
| 2386 | - |
|
| 2387 | - $postHookData = array( |
|
| 2388 | - 'itemType' => $itemType, |
|
| 2389 | - 'itemSource' => $itemSource, |
|
| 2390 | - 'parent' => $parent, |
|
| 2391 | - 'shareType' => $shareType, |
|
| 2392 | - 'uidOwner' => $uidOwner, |
|
| 2393 | - 'permissions' => $permissions, |
|
| 2394 | - 'fileSource' => $fileSource, |
|
| 2395 | - 'id' => $parent, |
|
| 2396 | - 'token' => $token, |
|
| 2397 | - 'expirationDate' => $expirationDate, |
|
| 2398 | - ); |
|
| 2399 | - |
|
| 2400 | - $postHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith; |
|
| 2401 | - $postHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget; |
|
| 2402 | - $postHookData['fileTarget'] = ($isGroupShare) ? $groupFileTarget : $fileTarget; |
|
| 2403 | - |
|
| 2404 | - \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData); |
|
| 2405 | - |
|
| 2406 | - |
|
| 2407 | - return $id ? $id : false; |
|
| 2408 | - } |
|
| 2409 | - |
|
| 2410 | - /** |
|
| 2411 | - * @param string $itemType |
|
| 2412 | - * @param string $itemSource |
|
| 2413 | - * @param int $shareType |
|
| 2414 | - * @param string $shareWith |
|
| 2415 | - * @param string $uidOwner |
|
| 2416 | - * @param int $permissions |
|
| 2417 | - * @param string|null $itemSourceName |
|
| 2418 | - * @param null|\DateTime $expirationDate |
|
| 2419 | - */ |
|
| 2420 | - private static function checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate) { |
|
| 2421 | - $backend = self::getBackend($itemType); |
|
| 2422 | - |
|
| 2423 | - $l = \OC::$server->getL10N('lib'); |
|
| 2424 | - $result = array(); |
|
| 2425 | - |
|
| 2426 | - $column = ($itemType === 'file' || $itemType === 'folder') ? 'file_source' : 'item_source'; |
|
| 2427 | - |
|
| 2428 | - $checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true); |
|
| 2429 | - if ($checkReshare) { |
|
| 2430 | - // Check if attempting to share back to owner |
|
| 2431 | - if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) { |
|
| 2432 | - $message = 'Sharing %s failed, because the user %s is the original sharer'; |
|
| 2433 | - $message_t = $l->t('Sharing failed, because the user %s is the original sharer', [$shareWith]); |
|
| 2434 | - |
|
| 2435 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 2436 | - throw new \Exception($message_t); |
|
| 2437 | - } |
|
| 2438 | - } |
|
| 2439 | - |
|
| 2440 | - if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) { |
|
| 2441 | - // Check if share permissions is granted |
|
| 2442 | - if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 2443 | - if (~(int)$checkReshare['permissions'] & $permissions) { |
|
| 2444 | - $message = 'Sharing %s failed, because the permissions exceed permissions granted to %s'; |
|
| 2445 | - $message_t = $l->t('Sharing %s failed, because the permissions exceed permissions granted to %s', array($itemSourceName, $uidOwner)); |
|
| 2446 | - |
|
| 2447 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner), \OCP\Util::DEBUG); |
|
| 2448 | - throw new \Exception($message_t); |
|
| 2449 | - } else { |
|
| 2450 | - // TODO Don't check if inside folder |
|
| 2451 | - $result['parent'] = $checkReshare['id']; |
|
| 2452 | - |
|
| 2453 | - $result['expirationDate'] = $expirationDate; |
|
| 2454 | - // $checkReshare['expiration'] could be null and then is always less than any value |
|
| 2455 | - if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) { |
|
| 2456 | - $result['expirationDate'] = $checkReshare['expiration']; |
|
| 2457 | - } |
|
| 2458 | - |
|
| 2459 | - // only suggest the same name as new target if it is a reshare of the |
|
| 2460 | - // same file/folder and not the reshare of a child |
|
| 2461 | - if ($checkReshare[$column] === $itemSource) { |
|
| 2462 | - $result['filePath'] = $checkReshare['file_target']; |
|
| 2463 | - $result['itemSource'] = $checkReshare['item_source']; |
|
| 2464 | - $result['fileSource'] = $checkReshare['file_source']; |
|
| 2465 | - $result['suggestedItemTarget'] = $checkReshare['item_target']; |
|
| 2466 | - $result['suggestedFileTarget'] = $checkReshare['file_target']; |
|
| 2467 | - } else { |
|
| 2468 | - $result['filePath'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $backend->getFilePath($itemSource, $uidOwner) : null; |
|
| 2469 | - $result['suggestedItemTarget'] = null; |
|
| 2470 | - $result['suggestedFileTarget'] = null; |
|
| 2471 | - $result['itemSource'] = $itemSource; |
|
| 2472 | - $result['fileSource'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $itemSource : null; |
|
| 2473 | - } |
|
| 2474 | - } |
|
| 2475 | - } else { |
|
| 2476 | - $message = 'Sharing %s failed, because resharing is not allowed'; |
|
| 2477 | - $message_t = $l->t('Sharing %s failed, because resharing is not allowed', array($itemSourceName)); |
|
| 2478 | - |
|
| 2479 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 2480 | - throw new \Exception($message_t); |
|
| 2481 | - } |
|
| 2482 | - } else { |
|
| 2483 | - $result['parent'] = null; |
|
| 2484 | - $result['suggestedItemTarget'] = null; |
|
| 2485 | - $result['suggestedFileTarget'] = null; |
|
| 2486 | - $result['itemSource'] = $itemSource; |
|
| 2487 | - $result['expirationDate'] = $expirationDate; |
|
| 2488 | - if (!$backend->isValidSource($itemSource, $uidOwner)) { |
|
| 2489 | - $message = 'Sharing %s failed, because the sharing backend for ' |
|
| 2490 | - .'%s could not find its source'; |
|
| 2491 | - $message_t = $l->t('Sharing %s failed, because the sharing backend for %s could not find its source', array($itemSource, $itemType)); |
|
| 2492 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, $itemType), \OCP\Util::DEBUG); |
|
| 2493 | - throw new \Exception($message_t); |
|
| 2494 | - } |
|
| 2495 | - if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 2496 | - $result['filePath'] = $backend->getFilePath($itemSource, $uidOwner); |
|
| 2497 | - if ($itemType == 'file' || $itemType == 'folder') { |
|
| 2498 | - $result['fileSource'] = $itemSource; |
|
| 2499 | - } else { |
|
| 2500 | - $meta = \OC\Files\Filesystem::getFileInfo($result['filePath']); |
|
| 2501 | - $result['fileSource'] = $meta['fileid']; |
|
| 2502 | - } |
|
| 2503 | - if ($result['fileSource'] == -1) { |
|
| 2504 | - $message = 'Sharing %s failed, because the file could not be found in the file cache'; |
|
| 2505 | - $message_t = $l->t('Sharing %s failed, because the file could not be found in the file cache', array($itemSource)); |
|
| 2506 | - |
|
| 2507 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG); |
|
| 2508 | - throw new \Exception($message_t); |
|
| 2509 | - } |
|
| 2510 | - } else { |
|
| 2511 | - $result['filePath'] = null; |
|
| 2512 | - $result['fileSource'] = null; |
|
| 2513 | - } |
|
| 2514 | - } |
|
| 2515 | - |
|
| 2516 | - return $result; |
|
| 2517 | - } |
|
| 2518 | - |
|
| 2519 | - /** |
|
| 2520 | - * |
|
| 2521 | - * @param array $shareData |
|
| 2522 | - * @return mixed false in case of a failure or the id of the new share |
|
| 2523 | - */ |
|
| 2524 | - private static function insertShare(array $shareData) { |
|
| 2525 | - |
|
| 2526 | - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' |
|
| 2527 | - .' `item_type`, `item_source`, `item_target`, `share_type`,' |
|
| 2528 | - .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,' |
|
| 2529 | - .' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 2530 | - $query->bindValue(1, $shareData['itemType']); |
|
| 2531 | - $query->bindValue(2, $shareData['itemSource']); |
|
| 2532 | - $query->bindValue(3, $shareData['itemTarget']); |
|
| 2533 | - $query->bindValue(4, $shareData['shareType']); |
|
| 2534 | - $query->bindValue(5, $shareData['shareWith']); |
|
| 2535 | - $query->bindValue(6, $shareData['uidOwner']); |
|
| 2536 | - $query->bindValue(7, $shareData['permissions']); |
|
| 2537 | - $query->bindValue(8, $shareData['shareTime']); |
|
| 2538 | - $query->bindValue(9, $shareData['fileSource']); |
|
| 2539 | - $query->bindValue(10, $shareData['fileTarget']); |
|
| 2540 | - $query->bindValue(11, $shareData['token']); |
|
| 2541 | - $query->bindValue(12, $shareData['parent']); |
|
| 2542 | - $query->bindValue(13, $shareData['expiration'], 'datetime'); |
|
| 2543 | - $result = $query->execute(); |
|
| 2544 | - |
|
| 2545 | - $id = false; |
|
| 2546 | - if ($result) { |
|
| 2547 | - $id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 2548 | - } |
|
| 2549 | - |
|
| 2550 | - return $id; |
|
| 2551 | - |
|
| 2552 | - } |
|
| 2553 | - |
|
| 2554 | - /** |
|
| 2555 | - * Delete all shares with type SHARE_TYPE_LINK |
|
| 2556 | - */ |
|
| 2557 | - public static function removeAllLinkShares() { |
|
| 2558 | - // Delete any link shares |
|
| 2559 | - $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*share` WHERE `share_type` = ?'); |
|
| 2560 | - $result = $query->execute(array(self::SHARE_TYPE_LINK)); |
|
| 2561 | - while ($item = $result->fetchRow()) { |
|
| 2562 | - Helper::delete($item['id']); |
|
| 2563 | - } |
|
| 2564 | - } |
|
| 2565 | - |
|
| 2566 | - /** |
|
| 2567 | - * In case a password protected link is not yet authenticated this function will return false |
|
| 2568 | - * |
|
| 2569 | - * @param array $linkItem |
|
| 2570 | - * @return boolean |
|
| 2571 | - */ |
|
| 2572 | - public static function checkPasswordProtectedShare(array $linkItem) { |
|
| 2573 | - if (!isset($linkItem['share_with'])) { |
|
| 2574 | - return true; |
|
| 2575 | - } |
|
| 2576 | - if (!isset($linkItem['share_type'])) { |
|
| 2577 | - return true; |
|
| 2578 | - } |
|
| 2579 | - if (!isset($linkItem['id'])) { |
|
| 2580 | - return true; |
|
| 2581 | - } |
|
| 2582 | - |
|
| 2583 | - if ($linkItem['share_type'] != \OCP\Share::SHARE_TYPE_LINK) { |
|
| 2584 | - return true; |
|
| 2585 | - } |
|
| 2586 | - |
|
| 2587 | - if ( \OC::$server->getSession()->exists('public_link_authenticated') |
|
| 2588 | - && \OC::$server->getSession()->get('public_link_authenticated') === (string)$linkItem['id'] ) { |
|
| 2589 | - return true; |
|
| 2590 | - } |
|
| 2591 | - |
|
| 2592 | - return false; |
|
| 2593 | - } |
|
| 2594 | - |
|
| 2595 | - /** |
|
| 2596 | - * construct select statement |
|
| 2597 | - * @param int $format |
|
| 2598 | - * @param boolean $fileDependent ist it a file/folder share or a generla share |
|
| 2599 | - * @param string $uidOwner |
|
| 2600 | - * @return string select statement |
|
| 2601 | - */ |
|
| 2602 | - private static function createSelectStatement($format, $fileDependent, $uidOwner = null) { |
|
| 2603 | - $select = '*'; |
|
| 2604 | - if ($format == self::FORMAT_STATUSES) { |
|
| 2605 | - if ($fileDependent) { |
|
| 2606 | - $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, ' |
|
| 2607 | - . '`share_with`, `uid_owner` , `file_source`, `stime`, `*PREFIX*share`.`permissions`, ' |
|
| 2608 | - . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 2609 | - . '`uid_initiator`'; |
|
| 2610 | - } else { |
|
| 2611 | - $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`, `*PREFIX*share`.`permissions`'; |
|
| 2612 | - } |
|
| 2613 | - } else { |
|
| 2614 | - if (isset($uidOwner)) { |
|
| 2615 | - if ($fileDependent) { |
|
| 2616 | - $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`,' |
|
| 2617 | - . ' `share_type`, `share_with`, `file_source`, `file_target`, `path`, `*PREFIX*share`.`permissions`, `stime`,' |
|
| 2618 | - . ' `expiration`, `token`, `storage`, `mail_send`, `uid_owner`, ' |
|
| 2619 | - . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 2620 | - } else { |
|
| 2621 | - $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `*PREFIX*share`.`permissions`,' |
|
| 2622 | - . ' `stime`, `file_source`, `expiration`, `token`, `mail_send`, `uid_owner`'; |
|
| 2623 | - } |
|
| 2624 | - } else { |
|
| 2625 | - if ($fileDependent) { |
|
| 2626 | - if ($format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_GET_FOLDER_CONTENTS || $format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_FILE_APP_ROOT) { |
|
| 2627 | - $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, ' |
|
| 2628 | - . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, ' |
|
| 2629 | - . '`*PREFIX*share`.`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 2630 | - . '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`, `mail_send`'; |
|
| 2631 | - } else { |
|
| 2632 | - $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`,' |
|
| 2633 | - . '`*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`,' |
|
| 2634 | - . '`file_source`, `path`, `file_target`, `*PREFIX*share`.`permissions`,' |
|
| 2635 | - . '`stime`, `expiration`, `token`, `storage`, `mail_send`,' |
|
| 2636 | - . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 2637 | - } |
|
| 2638 | - } |
|
| 2639 | - } |
|
| 2640 | - } |
|
| 2641 | - return $select; |
|
| 2642 | - } |
|
| 2643 | - |
|
| 2644 | - |
|
| 2645 | - /** |
|
| 2646 | - * transform db results |
|
| 2647 | - * @param array $row result |
|
| 2648 | - */ |
|
| 2649 | - private static function transformDBResults(&$row) { |
|
| 2650 | - if (isset($row['id'])) { |
|
| 2651 | - $row['id'] = (int) $row['id']; |
|
| 2652 | - } |
|
| 2653 | - if (isset($row['share_type'])) { |
|
| 2654 | - $row['share_type'] = (int) $row['share_type']; |
|
| 2655 | - } |
|
| 2656 | - if (isset($row['parent'])) { |
|
| 2657 | - $row['parent'] = (int) $row['parent']; |
|
| 2658 | - } |
|
| 2659 | - if (isset($row['file_parent'])) { |
|
| 2660 | - $row['file_parent'] = (int) $row['file_parent']; |
|
| 2661 | - } |
|
| 2662 | - if (isset($row['file_source'])) { |
|
| 2663 | - $row['file_source'] = (int) $row['file_source']; |
|
| 2664 | - } |
|
| 2665 | - if (isset($row['permissions'])) { |
|
| 2666 | - $row['permissions'] = (int) $row['permissions']; |
|
| 2667 | - } |
|
| 2668 | - if (isset($row['storage'])) { |
|
| 2669 | - $row['storage'] = (int) $row['storage']; |
|
| 2670 | - } |
|
| 2671 | - if (isset($row['stime'])) { |
|
| 2672 | - $row['stime'] = (int) $row['stime']; |
|
| 2673 | - } |
|
| 2674 | - if (isset($row['expiration']) && $row['share_type'] !== self::SHARE_TYPE_LINK) { |
|
| 2675 | - // discard expiration date for non-link shares, which might have been |
|
| 2676 | - // set by ancient bugs |
|
| 2677 | - $row['expiration'] = null; |
|
| 2678 | - } |
|
| 2679 | - } |
|
| 2680 | - |
|
| 2681 | - /** |
|
| 2682 | - * format result |
|
| 2683 | - * @param array $items result |
|
| 2684 | - * @param string $column is it a file share or a general share ('file_target' or 'item_target') |
|
| 2685 | - * @param \OCP\Share_Backend $backend sharing backend |
|
| 2686 | - * @param int $format |
|
| 2687 | - * @param array $parameters additional format parameters |
|
| 2688 | - * @return array format result |
|
| 2689 | - */ |
|
| 2690 | - private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) { |
|
| 2691 | - if ($format === self::FORMAT_NONE) { |
|
| 2692 | - return $items; |
|
| 2693 | - } else if ($format === self::FORMAT_STATUSES) { |
|
| 2694 | - $statuses = array(); |
|
| 2695 | - foreach ($items as $item) { |
|
| 2696 | - if ($item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 2697 | - if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) { |
|
| 2698 | - continue; |
|
| 2699 | - } |
|
| 2700 | - $statuses[$item[$column]]['link'] = true; |
|
| 2701 | - } else if (!isset($statuses[$item[$column]])) { |
|
| 2702 | - $statuses[$item[$column]]['link'] = false; |
|
| 2703 | - } |
|
| 2704 | - if (!empty($item['file_target'])) { |
|
| 2705 | - $statuses[$item[$column]]['path'] = $item['path']; |
|
| 2706 | - } |
|
| 2707 | - } |
|
| 2708 | - return $statuses; |
|
| 2709 | - } else { |
|
| 2710 | - return $backend->formatItems($items, $format, $parameters); |
|
| 2711 | - } |
|
| 2712 | - } |
|
| 2713 | - |
|
| 2714 | - /** |
|
| 2715 | - * remove protocol from URL |
|
| 2716 | - * |
|
| 2717 | - * @param string $url |
|
| 2718 | - * @return string |
|
| 2719 | - */ |
|
| 2720 | - public static function removeProtocolFromUrl($url) { |
|
| 2721 | - if (strpos($url, 'https://') === 0) { |
|
| 2722 | - return substr($url, strlen('https://')); |
|
| 2723 | - } else if (strpos($url, 'http://') === 0) { |
|
| 2724 | - return substr($url, strlen('http://')); |
|
| 2725 | - } |
|
| 2726 | - |
|
| 2727 | - return $url; |
|
| 2728 | - } |
|
| 2729 | - |
|
| 2730 | - /** |
|
| 2731 | - * try http post first with https and then with http as a fallback |
|
| 2732 | - * |
|
| 2733 | - * @param string $remoteDomain |
|
| 2734 | - * @param string $urlSuffix |
|
| 2735 | - * @param array $fields post parameters |
|
| 2736 | - * @return array |
|
| 2737 | - */ |
|
| 2738 | - private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) { |
|
| 2739 | - $protocol = 'https://'; |
|
| 2740 | - $result = [ |
|
| 2741 | - 'success' => false, |
|
| 2742 | - 'result' => '', |
|
| 2743 | - ]; |
|
| 2744 | - $try = 0; |
|
| 2745 | - $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
|
| 2746 | - while ($result['success'] === false && $try < 2) { |
|
| 2747 | - $federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING'); |
|
| 2748 | - $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
| 2749 | - $result = \OC::$server->getHTTPHelper()->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, $fields); |
|
| 2750 | - $try++; |
|
| 2751 | - $protocol = 'http://'; |
|
| 2752 | - } |
|
| 2753 | - |
|
| 2754 | - return $result; |
|
| 2755 | - } |
|
| 2756 | - |
|
| 2757 | - /** |
|
| 2758 | - * send server-to-server share to remote server |
|
| 2759 | - * |
|
| 2760 | - * @param string $token |
|
| 2761 | - * @param string $shareWith |
|
| 2762 | - * @param string $name |
|
| 2763 | - * @param int $remote_id |
|
| 2764 | - * @param string $owner |
|
| 2765 | - * @return bool |
|
| 2766 | - */ |
|
| 2767 | - private static function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner) { |
|
| 2768 | - |
|
| 2769 | - list($user, $remote) = Helper::splitUserRemote($shareWith); |
|
| 2770 | - |
|
| 2771 | - if ($user && $remote) { |
|
| 2772 | - $url = $remote; |
|
| 2773 | - |
|
| 2774 | - $local = \OC::$server->getURLGenerator()->getAbsoluteURL('/'); |
|
| 2775 | - |
|
| 2776 | - $fields = array( |
|
| 2777 | - 'shareWith' => $user, |
|
| 2778 | - 'token' => $token, |
|
| 2779 | - 'name' => $name, |
|
| 2780 | - 'remoteId' => $remote_id, |
|
| 2781 | - 'owner' => $owner, |
|
| 2782 | - 'remote' => $local, |
|
| 2783 | - ); |
|
| 2784 | - |
|
| 2785 | - $url = self::removeProtocolFromUrl($url); |
|
| 2786 | - $result = self::tryHttpPostToShareEndpoint($url, '', $fields); |
|
| 2787 | - $status = json_decode($result['result'], true); |
|
| 2788 | - |
|
| 2789 | - if ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)) { |
|
| 2790 | - \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]); |
|
| 2791 | - return true; |
|
| 2792 | - } |
|
| 2793 | - |
|
| 2794 | - } |
|
| 2795 | - |
|
| 2796 | - return false; |
|
| 2797 | - } |
|
| 2798 | - |
|
| 2799 | - /** |
|
| 2800 | - * send server-to-server unshare to remote server |
|
| 2801 | - * |
|
| 2802 | - * @param string $remote url |
|
| 2803 | - * @param int $id share id |
|
| 2804 | - * @param string $token |
|
| 2805 | - * @return bool |
|
| 2806 | - */ |
|
| 2807 | - private static function sendRemoteUnshare($remote, $id, $token) { |
|
| 2808 | - $url = rtrim($remote, '/'); |
|
| 2809 | - $fields = array('token' => $token, 'format' => 'json'); |
|
| 2810 | - $url = self::removeProtocolFromUrl($url); |
|
| 2811 | - $result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields); |
|
| 2812 | - $status = json_decode($result['result'], true); |
|
| 2813 | - |
|
| 2814 | - return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)); |
|
| 2815 | - } |
|
| 2816 | - |
|
| 2817 | - /** |
|
| 2818 | - * check if user can only share with group members |
|
| 2819 | - * @return bool |
|
| 2820 | - */ |
|
| 2821 | - public static function shareWithGroupMembersOnly() { |
|
| 2822 | - $value = \OC::$server->getAppConfig()->getValue('core', 'shareapi_only_share_with_group_members', 'no'); |
|
| 2823 | - return ($value === 'yes') ? true : false; |
|
| 2824 | - } |
|
| 2825 | - |
|
| 2826 | - /** |
|
| 2827 | - * @return bool |
|
| 2828 | - */ |
|
| 2829 | - public static function isDefaultExpireDateEnabled() { |
|
| 2830 | - $defaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no'); |
|
| 2831 | - return ($defaultExpireDateEnabled === "yes") ? true : false; |
|
| 2832 | - } |
|
| 2833 | - |
|
| 2834 | - /** |
|
| 2835 | - * @return bool |
|
| 2836 | - */ |
|
| 2837 | - public static function enforceDefaultExpireDate() { |
|
| 2838 | - $enforceDefaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no'); |
|
| 2839 | - return ($enforceDefaultExpireDate === "yes") ? true : false; |
|
| 2840 | - } |
|
| 2841 | - |
|
| 2842 | - /** |
|
| 2843 | - * @return int |
|
| 2844 | - */ |
|
| 2845 | - public static function getExpireInterval() { |
|
| 2846 | - return (int)\OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 2847 | - } |
|
| 2848 | - |
|
| 2849 | - /** |
|
| 2850 | - * Checks whether the given path is reachable for the given owner |
|
| 2851 | - * |
|
| 2852 | - * @param string $path path relative to files |
|
| 2853 | - * @param string $ownerStorageId storage id of the owner |
|
| 2854 | - * |
|
| 2855 | - * @return boolean true if file is reachable, false otherwise |
|
| 2856 | - */ |
|
| 2857 | - private static function isFileReachable($path, $ownerStorageId) { |
|
| 2858 | - // if outside the home storage, file is always considered reachable |
|
| 2859 | - if (!(substr($ownerStorageId, 0, 6) === 'home::' || |
|
| 2860 | - substr($ownerStorageId, 0, 13) === 'object::user:' |
|
| 2861 | - )) { |
|
| 2862 | - return true; |
|
| 2863 | - } |
|
| 2864 | - |
|
| 2865 | - // if inside the home storage, the file has to be under "/files/" |
|
| 2866 | - $path = ltrim($path, '/'); |
|
| 2867 | - if (substr($path, 0, 6) === 'files/') { |
|
| 2868 | - return true; |
|
| 2869 | - } |
|
| 2870 | - |
|
| 2871 | - return false; |
|
| 2872 | - } |
|
| 2873 | - |
|
| 2874 | - /** |
|
| 2875 | - * @param IConfig $config |
|
| 2876 | - * @return bool |
|
| 2877 | - */ |
|
| 2878 | - public static function enforcePassword(IConfig $config) { |
|
| 2879 | - $enforcePassword = $config->getAppValue('core', 'shareapi_enforce_links_password', 'no'); |
|
| 2880 | - return ($enforcePassword === "yes") ? true : false; |
|
| 2881 | - } |
|
| 2882 | - |
|
| 2883 | - /** |
|
| 2884 | - * Get all share entries, including non-unique group items |
|
| 2885 | - * |
|
| 2886 | - * @param string $owner |
|
| 2887 | - * @return array |
|
| 2888 | - */ |
|
| 2889 | - public static function getAllSharesForOwner($owner) { |
|
| 2890 | - $query = 'SELECT * FROM `*PREFIX*share` WHERE `uid_owner` = ?'; |
|
| 2891 | - $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$owner]); |
|
| 2892 | - return $result->fetchAll(); |
|
| 2893 | - } |
|
| 2894 | - |
|
| 2895 | - /** |
|
| 2896 | - * Get all share entries, including non-unique group items for a file |
|
| 2897 | - * |
|
| 2898 | - * @param int $id |
|
| 2899 | - * @return array |
|
| 2900 | - */ |
|
| 2901 | - public static function getAllSharesForFileId($id) { |
|
| 2902 | - $query = 'SELECT * FROM `*PREFIX*share` WHERE `file_source` = ?'; |
|
| 2903 | - $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$id]); |
|
| 2904 | - return $result->fetchAll(); |
|
| 2905 | - } |
|
| 2906 | - |
|
| 2907 | - /** |
|
| 2908 | - * @param string $password |
|
| 2909 | - * @throws \Exception |
|
| 2910 | - */ |
|
| 2911 | - private static function verifyPassword($password) { |
|
| 2912 | - |
|
| 2913 | - $accepted = true; |
|
| 2914 | - $message = ''; |
|
| 2915 | - \OCP\Util::emitHook('\OC\Share', 'verifyPassword', [ |
|
| 2916 | - 'password' => $password, |
|
| 2917 | - 'accepted' => &$accepted, |
|
| 2918 | - 'message' => &$message |
|
| 2919 | - ]); |
|
| 2920 | - |
|
| 2921 | - if (!$accepted) { |
|
| 2922 | - throw new \Exception($message); |
|
| 2923 | - } |
|
| 2924 | - } |
|
| 1311 | + if ($permissions & ~(int)$rootItem['permissions']) { |
|
| 1312 | + $qb = $connection->getQueryBuilder(); |
|
| 1313 | + $qb->select('id', 'permissions', 'item_type') |
|
| 1314 | + ->from('share') |
|
| 1315 | + ->where($qb->expr()->eq('parent', $qb->createParameter('parent'))) |
|
| 1316 | + ->andWhere($qb->expr()->eq('share_type', $qb->createParameter('share_type'))) |
|
| 1317 | + ->andWhere($qb->expr()->neq('permissions', $qb->createParameter('shareDeleted'))) |
|
| 1318 | + ->setParameter(':parent', (int)$rootItem['id']) |
|
| 1319 | + ->setParameter(':share_type', 2) |
|
| 1320 | + ->setParameter(':shareDeleted', 0); |
|
| 1321 | + $result = $qb->execute(); |
|
| 1322 | + |
|
| 1323 | + $ids = []; |
|
| 1324 | + while ($item = $result->fetch()) { |
|
| 1325 | + $item = $sanitizeItem($item); |
|
| 1326 | + $items[] = $item; |
|
| 1327 | + $ids[] = $item['id']; |
|
| 1328 | + } |
|
| 1329 | + $result->closeCursor(); |
|
| 1330 | + |
|
| 1331 | + // Add permssions for all USERGROUP shares of this item |
|
| 1332 | + if (!empty($ids)) { |
|
| 1333 | + $ids = $intArrayToLiteralArray($ids, $qb->expr()); |
|
| 1334 | + |
|
| 1335 | + $qb = $connection->getQueryBuilder(); |
|
| 1336 | + $qb->update('share') |
|
| 1337 | + ->set('permissions', $qb->createParameter('permissions')) |
|
| 1338 | + ->where($qb->expr()->in('id', $ids)) |
|
| 1339 | + ->setParameter(':permissions', $permissions); |
|
| 1340 | + $qb->execute(); |
|
| 1341 | + } |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + foreach ($items as $item) { |
|
| 1345 | + \OC_Hook::emit('OCP\Share', 'post_update_permissions', ['share' => $item]); |
|
| 1346 | + } |
|
| 1347 | + |
|
| 1348 | + return true; |
|
| 1349 | + } |
|
| 1350 | + $message = 'Setting permissions for %s failed, because the item was not found'; |
|
| 1351 | + $message_t = $l->t('Setting permissions for %s failed, because the item was not found', array($itemSource)); |
|
| 1352 | + |
|
| 1353 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG); |
|
| 1354 | + throw new \Exception($message_t); |
|
| 1355 | + } |
|
| 1356 | + |
|
| 1357 | + /** |
|
| 1358 | + * validate expiration date if it meets all constraints |
|
| 1359 | + * |
|
| 1360 | + * @param string $expireDate well formatted date string, e.g. "DD-MM-YYYY" |
|
| 1361 | + * @param string $shareTime timestamp when the file was shared |
|
| 1362 | + * @param string $itemType |
|
| 1363 | + * @param string $itemSource |
|
| 1364 | + * @return \DateTime validated date |
|
| 1365 | + * @throws \Exception when the expire date is in the past or further in the future then the enforced date |
|
| 1366 | + */ |
|
| 1367 | + private static function validateExpireDate($expireDate, $shareTime, $itemType, $itemSource) { |
|
| 1368 | + $l = \OC::$server->getL10N('lib'); |
|
| 1369 | + $date = new \DateTime($expireDate); |
|
| 1370 | + $today = new \DateTime('now'); |
|
| 1371 | + |
|
| 1372 | + // if the user doesn't provide a share time we need to get it from the database |
|
| 1373 | + // fall-back mode to keep API stable, because the $shareTime parameter was added later |
|
| 1374 | + $defaultExpireDateEnforced = \OCP\Util::isDefaultExpireDateEnforced(); |
|
| 1375 | + if ($defaultExpireDateEnforced && $shareTime === null) { |
|
| 1376 | + $items = self::getItemShared($itemType, $itemSource); |
|
| 1377 | + $firstItem = reset($items); |
|
| 1378 | + $shareTime = (int)$firstItem['stime']; |
|
| 1379 | + } |
|
| 1380 | + |
|
| 1381 | + if ($defaultExpireDateEnforced) { |
|
| 1382 | + // initialize max date with share time |
|
| 1383 | + $maxDate = new \DateTime(); |
|
| 1384 | + $maxDate->setTimestamp($shareTime); |
|
| 1385 | + $maxDays = \OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1386 | + $maxDate->add(new \DateInterval('P' . $maxDays . 'D')); |
|
| 1387 | + if ($date > $maxDate) { |
|
| 1388 | + $warning = 'Cannot set expiration date. Shares cannot expire later than ' . $maxDays . ' after they have been shared'; |
|
| 1389 | + $warning_t = $l->t('Cannot set expiration date. Shares cannot expire later than %s after they have been shared', array($maxDays)); |
|
| 1390 | + \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN); |
|
| 1391 | + throw new \Exception($warning_t); |
|
| 1392 | + } |
|
| 1393 | + } |
|
| 1394 | + |
|
| 1395 | + if ($date < $today) { |
|
| 1396 | + $message = 'Cannot set expiration date. Expiration date is in the past'; |
|
| 1397 | + $message_t = $l->t('Cannot set expiration date. Expiration date is in the past'); |
|
| 1398 | + \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::WARN); |
|
| 1399 | + throw new \Exception($message_t); |
|
| 1400 | + } |
|
| 1401 | + |
|
| 1402 | + return $date; |
|
| 1403 | + } |
|
| 1404 | + |
|
| 1405 | + /** |
|
| 1406 | + * Set expiration date for a share |
|
| 1407 | + * @param string $itemType |
|
| 1408 | + * @param string $itemSource |
|
| 1409 | + * @param string $date expiration date |
|
| 1410 | + * @param int $shareTime timestamp from when the file was shared |
|
| 1411 | + * @return boolean |
|
| 1412 | + * @throws \Exception when the expire date is not set, in the past or further in the future then the enforced date |
|
| 1413 | + */ |
|
| 1414 | + public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) { |
|
| 1415 | + $user = \OC_User::getUser(); |
|
| 1416 | + $l = \OC::$server->getL10N('lib'); |
|
| 1417 | + |
|
| 1418 | + if ($date == '') { |
|
| 1419 | + if (\OCP\Util::isDefaultExpireDateEnforced()) { |
|
| 1420 | + $warning = 'Cannot clear expiration date. Shares are required to have an expiration date.'; |
|
| 1421 | + $warning_t = $l->t('Cannot clear expiration date. Shares are required to have an expiration date.'); |
|
| 1422 | + \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN); |
|
| 1423 | + throw new \Exception($warning_t); |
|
| 1424 | + } else { |
|
| 1425 | + $date = null; |
|
| 1426 | + } |
|
| 1427 | + } else { |
|
| 1428 | + $date = self::validateExpireDate($date, $shareTime, $itemType, $itemSource); |
|
| 1429 | + } |
|
| 1430 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ? AND `uid_owner` = ? AND `share_type` = ?'); |
|
| 1431 | + $query->bindValue(1, $date, 'datetime'); |
|
| 1432 | + $query->bindValue(2, $itemType); |
|
| 1433 | + $query->bindValue(3, $itemSource); |
|
| 1434 | + $query->bindValue(4, $user); |
|
| 1435 | + $query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK); |
|
| 1436 | + |
|
| 1437 | + $query->execute(); |
|
| 1438 | + |
|
| 1439 | + \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', array( |
|
| 1440 | + 'itemType' => $itemType, |
|
| 1441 | + 'itemSource' => $itemSource, |
|
| 1442 | + 'date' => $date, |
|
| 1443 | + 'uidOwner' => $user |
|
| 1444 | + )); |
|
| 1445 | + |
|
| 1446 | + return true; |
|
| 1447 | + } |
|
| 1448 | + |
|
| 1449 | + /** |
|
| 1450 | + * Retrieve the owner of a connection |
|
| 1451 | + * |
|
| 1452 | + * @param IDBConnection $connection |
|
| 1453 | + * @param int $shareId |
|
| 1454 | + * @throws \Exception |
|
| 1455 | + * @return string uid of share owner |
|
| 1456 | + */ |
|
| 1457 | + private static function getShareOwner(IDBConnection $connection, $shareId) { |
|
| 1458 | + $qb = $connection->getQueryBuilder(); |
|
| 1459 | + |
|
| 1460 | + $qb->select('uid_owner') |
|
| 1461 | + ->from('share') |
|
| 1462 | + ->where($qb->expr()->eq('id', $qb->createParameter('shareId'))) |
|
| 1463 | + ->setParameter(':shareId', $shareId); |
|
| 1464 | + $dbResult = $qb->execute(); |
|
| 1465 | + $result = $dbResult->fetch(); |
|
| 1466 | + $dbResult->closeCursor(); |
|
| 1467 | + |
|
| 1468 | + if (empty($result)) { |
|
| 1469 | + throw new \Exception('Share not found'); |
|
| 1470 | + } |
|
| 1471 | + |
|
| 1472 | + return $result['uid_owner']; |
|
| 1473 | + } |
|
| 1474 | + |
|
| 1475 | + /** |
|
| 1476 | + * Set password for a public link share |
|
| 1477 | + * |
|
| 1478 | + * @param IUserSession $userSession |
|
| 1479 | + * @param IDBConnection $connection |
|
| 1480 | + * @param IConfig $config |
|
| 1481 | + * @param int $shareId |
|
| 1482 | + * @param string $password |
|
| 1483 | + * @throws \Exception |
|
| 1484 | + * @return boolean |
|
| 1485 | + */ |
|
| 1486 | + public static function setPassword(IUserSession $userSession, |
|
| 1487 | + IDBConnection $connection, |
|
| 1488 | + IConfig $config, |
|
| 1489 | + $shareId, $password) { |
|
| 1490 | + $user = $userSession->getUser(); |
|
| 1491 | + if (is_null($user)) { |
|
| 1492 | + throw new \Exception("User not logged in"); |
|
| 1493 | + } |
|
| 1494 | + |
|
| 1495 | + $uid = self::getShareOwner($connection, $shareId); |
|
| 1496 | + |
|
| 1497 | + if ($uid !== $user->getUID()) { |
|
| 1498 | + throw new \Exception('Cannot update share of a different user'); |
|
| 1499 | + } |
|
| 1500 | + |
|
| 1501 | + if ($password === '') { |
|
| 1502 | + $password = null; |
|
| 1503 | + } |
|
| 1504 | + |
|
| 1505 | + //If passwords are enforced the password can't be null |
|
| 1506 | + if (self::enforcePassword($config) && is_null($password)) { |
|
| 1507 | + throw new \Exception('Cannot remove password'); |
|
| 1508 | + } |
|
| 1509 | + |
|
| 1510 | + self::verifyPassword($password); |
|
| 1511 | + |
|
| 1512 | + $qb = $connection->getQueryBuilder(); |
|
| 1513 | + $qb->update('share') |
|
| 1514 | + ->set('share_with', $qb->createParameter('pass')) |
|
| 1515 | + ->where($qb->expr()->eq('id', $qb->createParameter('shareId'))) |
|
| 1516 | + ->setParameter(':pass', is_null($password) ? null : \OC::$server->getHasher()->hash($password)) |
|
| 1517 | + ->setParameter(':shareId', $shareId); |
|
| 1518 | + |
|
| 1519 | + $qb->execute(); |
|
| 1520 | + |
|
| 1521 | + return true; |
|
| 1522 | + } |
|
| 1523 | + |
|
| 1524 | + /** |
|
| 1525 | + * Checks whether a share has expired, calls unshareItem() if yes. |
|
| 1526 | + * @param array $item Share data (usually database row) |
|
| 1527 | + * @return boolean True if item was expired, false otherwise. |
|
| 1528 | + */ |
|
| 1529 | + protected static function expireItem(array $item) { |
|
| 1530 | + |
|
| 1531 | + $result = false; |
|
| 1532 | + |
|
| 1533 | + // only use default expiration date for link shares |
|
| 1534 | + if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 1535 | + |
|
| 1536 | + // calculate expiration date |
|
| 1537 | + if (!empty($item['expiration'])) { |
|
| 1538 | + $userDefinedExpire = new \DateTime($item['expiration']); |
|
| 1539 | + $expires = $userDefinedExpire->getTimestamp(); |
|
| 1540 | + } else { |
|
| 1541 | + $expires = null; |
|
| 1542 | + } |
|
| 1543 | + |
|
| 1544 | + |
|
| 1545 | + // get default expiration settings |
|
| 1546 | + $defaultSettings = Helper::getDefaultExpireSetting(); |
|
| 1547 | + $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires); |
|
| 1548 | + |
|
| 1549 | + |
|
| 1550 | + if (is_int($expires)) { |
|
| 1551 | + $now = time(); |
|
| 1552 | + if ($now > $expires) { |
|
| 1553 | + self::unshareItem($item); |
|
| 1554 | + $result = true; |
|
| 1555 | + } |
|
| 1556 | + } |
|
| 1557 | + } |
|
| 1558 | + return $result; |
|
| 1559 | + } |
|
| 1560 | + |
|
| 1561 | + /** |
|
| 1562 | + * Unshares a share given a share data array |
|
| 1563 | + * @param array $item Share data (usually database row) |
|
| 1564 | + * @param int $newParent parent ID |
|
| 1565 | + * @return null |
|
| 1566 | + */ |
|
| 1567 | + protected static function unshareItem(array $item, $newParent = null) { |
|
| 1568 | + |
|
| 1569 | + $shareType = (int)$item['share_type']; |
|
| 1570 | + $shareWith = null; |
|
| 1571 | + if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) { |
|
| 1572 | + $shareWith = $item['share_with']; |
|
| 1573 | + } |
|
| 1574 | + |
|
| 1575 | + // Pass all the vars we have for now, they may be useful |
|
| 1576 | + $hookParams = array( |
|
| 1577 | + 'id' => $item['id'], |
|
| 1578 | + 'itemType' => $item['item_type'], |
|
| 1579 | + 'itemSource' => $item['item_source'], |
|
| 1580 | + 'shareType' => $shareType, |
|
| 1581 | + 'shareWith' => $shareWith, |
|
| 1582 | + 'itemParent' => $item['parent'], |
|
| 1583 | + 'uidOwner' => $item['uid_owner'], |
|
| 1584 | + ); |
|
| 1585 | + if($item['item_type'] === 'file' || $item['item_type'] === 'folder') { |
|
| 1586 | + $hookParams['fileSource'] = $item['file_source']; |
|
| 1587 | + $hookParams['fileTarget'] = $item['file_target']; |
|
| 1588 | + } |
|
| 1589 | + |
|
| 1590 | + \OC_Hook::emit('OCP\Share', 'pre_unshare', $hookParams); |
|
| 1591 | + $deletedShares = Helper::delete($item['id'], false, null, $newParent); |
|
| 1592 | + $deletedShares[] = $hookParams; |
|
| 1593 | + $hookParams['deletedShares'] = $deletedShares; |
|
| 1594 | + \OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams); |
|
| 1595 | + if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) { |
|
| 1596 | + list(, $remote) = Helper::splitUserRemote($item['share_with']); |
|
| 1597 | + self::sendRemoteUnshare($remote, $item['id'], $item['token']); |
|
| 1598 | + } |
|
| 1599 | + } |
|
| 1600 | + |
|
| 1601 | + /** |
|
| 1602 | + * Get the backend class for the specified item type |
|
| 1603 | + * @param string $itemType |
|
| 1604 | + * @throws \Exception |
|
| 1605 | + * @return \OCP\Share_Backend |
|
| 1606 | + */ |
|
| 1607 | + public static function getBackend($itemType) { |
|
| 1608 | + $l = \OC::$server->getL10N('lib'); |
|
| 1609 | + if (isset(self::$backends[$itemType])) { |
|
| 1610 | + return self::$backends[$itemType]; |
|
| 1611 | + } else if (isset(self::$backendTypes[$itemType]['class'])) { |
|
| 1612 | + $class = self::$backendTypes[$itemType]['class']; |
|
| 1613 | + if (class_exists($class)) { |
|
| 1614 | + self::$backends[$itemType] = new $class; |
|
| 1615 | + if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) { |
|
| 1616 | + $message = 'Sharing backend %s must implement the interface OCP\Share_Backend'; |
|
| 1617 | + $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class)); |
|
| 1618 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR); |
|
| 1619 | + throw new \Exception($message_t); |
|
| 1620 | + } |
|
| 1621 | + return self::$backends[$itemType]; |
|
| 1622 | + } else { |
|
| 1623 | + $message = 'Sharing backend %s not found'; |
|
| 1624 | + $message_t = $l->t('Sharing backend %s not found', array($class)); |
|
| 1625 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR); |
|
| 1626 | + throw new \Exception($message_t); |
|
| 1627 | + } |
|
| 1628 | + } |
|
| 1629 | + $message = 'Sharing backend for %s not found'; |
|
| 1630 | + $message_t = $l->t('Sharing backend for %s not found', array($itemType)); |
|
| 1631 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemType), \OCP\Util::ERROR); |
|
| 1632 | + throw new \Exception($message_t); |
|
| 1633 | + } |
|
| 1634 | + |
|
| 1635 | + /** |
|
| 1636 | + * Check if resharing is allowed |
|
| 1637 | + * @return boolean true if allowed or false |
|
| 1638 | + * |
|
| 1639 | + * Resharing is allowed by default if not configured |
|
| 1640 | + */ |
|
| 1641 | + public static function isResharingAllowed() { |
|
| 1642 | + if (!isset(self::$isResharingAllowed)) { |
|
| 1643 | + if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') { |
|
| 1644 | + self::$isResharingAllowed = true; |
|
| 1645 | + } else { |
|
| 1646 | + self::$isResharingAllowed = false; |
|
| 1647 | + } |
|
| 1648 | + } |
|
| 1649 | + return self::$isResharingAllowed; |
|
| 1650 | + } |
|
| 1651 | + |
|
| 1652 | + /** |
|
| 1653 | + * Get a list of collection item types for the specified item type |
|
| 1654 | + * @param string $itemType |
|
| 1655 | + * @return array |
|
| 1656 | + */ |
|
| 1657 | + private static function getCollectionItemTypes($itemType) { |
|
| 1658 | + $collectionTypes = array($itemType); |
|
| 1659 | + foreach (self::$backendTypes as $type => $backend) { |
|
| 1660 | + if (in_array($backend['collectionOf'], $collectionTypes)) { |
|
| 1661 | + $collectionTypes[] = $type; |
|
| 1662 | + } |
|
| 1663 | + } |
|
| 1664 | + // TODO Add option for collections to be collection of themselves, only 'folder' does it now... |
|
| 1665 | + if (isset(self::$backendTypes[$itemType]) && (!self::getBackend($itemType) instanceof \OCP\Share_Backend_Collection || $itemType != 'folder')) { |
|
| 1666 | + unset($collectionTypes[0]); |
|
| 1667 | + } |
|
| 1668 | + // Return array if collections were found or the item type is a |
|
| 1669 | + // collection itself - collections can be inside collections |
|
| 1670 | + if (count($collectionTypes) > 0) { |
|
| 1671 | + return $collectionTypes; |
|
| 1672 | + } |
|
| 1673 | + return false; |
|
| 1674 | + } |
|
| 1675 | + |
|
| 1676 | + /** |
|
| 1677 | + * Get the owners of items shared with a user. |
|
| 1678 | + * |
|
| 1679 | + * @param string $user The user the items are shared with. |
|
| 1680 | + * @param string $type The type of the items shared with the user. |
|
| 1681 | + * @param boolean $includeCollections Include collection item types (optional) |
|
| 1682 | + * @param boolean $includeOwner include owner in the list of users the item is shared with (optional) |
|
| 1683 | + * @return array |
|
| 1684 | + */ |
|
| 1685 | + public static function getSharedItemsOwners($user, $type, $includeCollections = false, $includeOwner = false) { |
|
| 1686 | + // First, we find out if $type is part of a collection (and if that collection is part of |
|
| 1687 | + // another one and so on). |
|
| 1688 | + $collectionTypes = array(); |
|
| 1689 | + if (!$includeCollections || !$collectionTypes = self::getCollectionItemTypes($type)) { |
|
| 1690 | + $collectionTypes[] = $type; |
|
| 1691 | + } |
|
| 1692 | + |
|
| 1693 | + // Of these collection types, along with our original $type, we make a |
|
| 1694 | + // list of the ones for which a sharing backend has been registered. |
|
| 1695 | + // FIXME: Ideally, we wouldn't need to nest getItemsSharedWith in this loop but just call it |
|
| 1696 | + // with its $includeCollections parameter set to true. Unfortunately, this fails currently. |
|
| 1697 | + $allMaybeSharedItems = array(); |
|
| 1698 | + foreach ($collectionTypes as $collectionType) { |
|
| 1699 | + if (isset(self::$backends[$collectionType])) { |
|
| 1700 | + $allMaybeSharedItems[$collectionType] = self::getItemsSharedWithUser( |
|
| 1701 | + $collectionType, |
|
| 1702 | + $user, |
|
| 1703 | + self::FORMAT_NONE |
|
| 1704 | + ); |
|
| 1705 | + } |
|
| 1706 | + } |
|
| 1707 | + |
|
| 1708 | + $owners = array(); |
|
| 1709 | + if ($includeOwner) { |
|
| 1710 | + $owners[] = $user; |
|
| 1711 | + } |
|
| 1712 | + |
|
| 1713 | + // We take a look at all shared items of the given $type (or of the collections it is part of) |
|
| 1714 | + // and find out their owners. Then, we gather the tags for the original $type from all owners, |
|
| 1715 | + // and return them as elements of a list that look like "Tag (owner)". |
|
| 1716 | + foreach ($allMaybeSharedItems as $collectionType => $maybeSharedItems) { |
|
| 1717 | + foreach ($maybeSharedItems as $sharedItem) { |
|
| 1718 | + if (isset($sharedItem['id'])) { //workaround for https://github.com/owncloud/core/issues/2814 |
|
| 1719 | + $owners[] = $sharedItem['uid_owner']; |
|
| 1720 | + } |
|
| 1721 | + } |
|
| 1722 | + } |
|
| 1723 | + |
|
| 1724 | + return $owners; |
|
| 1725 | + } |
|
| 1726 | + |
|
| 1727 | + /** |
|
| 1728 | + * Get shared items from the database |
|
| 1729 | + * @param string $itemType |
|
| 1730 | + * @param string $item Item source or target (optional) |
|
| 1731 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique |
|
| 1732 | + * @param string $shareWith User or group the item is being shared with |
|
| 1733 | + * @param string $uidOwner User that is the owner of shared items (optional) |
|
| 1734 | + * @param int $format Format to convert items to with formatItems() (optional) |
|
| 1735 | + * @param mixed $parameters to pass to formatItems() (optional) |
|
| 1736 | + * @param int $limit Number of items to return, -1 to return all matches (optional) |
|
| 1737 | + * @param boolean $includeCollections Include collection item types (optional) |
|
| 1738 | + * @param boolean $itemShareWithBySource (optional) |
|
| 1739 | + * @param boolean $checkExpireDate |
|
| 1740 | + * @return array |
|
| 1741 | + * |
|
| 1742 | + * See public functions getItem(s)... for parameter usage |
|
| 1743 | + * |
|
| 1744 | + */ |
|
| 1745 | + public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, |
|
| 1746 | + $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, |
|
| 1747 | + $includeCollections = false, $itemShareWithBySource = false, $checkExpireDate = true) { |
|
| 1748 | + if (!self::isEnabled()) { |
|
| 1749 | + return array(); |
|
| 1750 | + } |
|
| 1751 | + $backend = self::getBackend($itemType); |
|
| 1752 | + $collectionTypes = false; |
|
| 1753 | + // Get filesystem root to add it to the file target and remove from the |
|
| 1754 | + // file source, match file_source with the file cache |
|
| 1755 | + if ($itemType == 'file' || $itemType == 'folder') { |
|
| 1756 | + if(!is_null($uidOwner)) { |
|
| 1757 | + $root = \OC\Files\Filesystem::getRoot(); |
|
| 1758 | + } else { |
|
| 1759 | + $root = ''; |
|
| 1760 | + } |
|
| 1761 | + $where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 1762 | + if (!isset($item)) { |
|
| 1763 | + $where .= ' AND `file_target` IS NOT NULL '; |
|
| 1764 | + } |
|
| 1765 | + $where .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 1766 | + $fileDependent = true; |
|
| 1767 | + $queryArgs = array(); |
|
| 1768 | + } else { |
|
| 1769 | + $fileDependent = false; |
|
| 1770 | + $root = ''; |
|
| 1771 | + $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 1772 | + if ($includeCollections && !isset($item) && $collectionTypes) { |
|
| 1773 | + // If includeCollections is true, find collections of this item type, e.g. a music album contains songs |
|
| 1774 | + if (!in_array($itemType, $collectionTypes)) { |
|
| 1775 | + $itemTypes = array_merge(array($itemType), $collectionTypes); |
|
| 1776 | + } else { |
|
| 1777 | + $itemTypes = $collectionTypes; |
|
| 1778 | + } |
|
| 1779 | + $placeholders = join(',', array_fill(0, count($itemTypes), '?')); |
|
| 1780 | + $where = ' WHERE `item_type` IN ('.$placeholders.'))'; |
|
| 1781 | + $queryArgs = $itemTypes; |
|
| 1782 | + } else { |
|
| 1783 | + $where = ' WHERE `item_type` = ?'; |
|
| 1784 | + $queryArgs = array($itemType); |
|
| 1785 | + } |
|
| 1786 | + } |
|
| 1787 | + if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 1788 | + $where .= ' AND `share_type` != ?'; |
|
| 1789 | + $queryArgs[] = self::SHARE_TYPE_LINK; |
|
| 1790 | + } |
|
| 1791 | + if (isset($shareType)) { |
|
| 1792 | + // Include all user and group items |
|
| 1793 | + if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) { |
|
| 1794 | + $where .= ' AND ((`share_type` in (?, ?) AND `share_with` = ?) '; |
|
| 1795 | + $queryArgs[] = self::SHARE_TYPE_USER; |
|
| 1796 | + $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 1797 | + $queryArgs[] = $shareWith; |
|
| 1798 | + |
|
| 1799 | + $user = \OC::$server->getUserManager()->get($shareWith); |
|
| 1800 | + $groups = []; |
|
| 1801 | + if ($user) { |
|
| 1802 | + $groups = \OC::$server->getGroupManager()->getUserGroupIds($user); |
|
| 1803 | + } |
|
| 1804 | + if (!empty($groups)) { |
|
| 1805 | + $placeholders = join(',', array_fill(0, count($groups), '?')); |
|
| 1806 | + $where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) '; |
|
| 1807 | + $queryArgs[] = self::SHARE_TYPE_GROUP; |
|
| 1808 | + $queryArgs = array_merge($queryArgs, $groups); |
|
| 1809 | + } |
|
| 1810 | + $where .= ')'; |
|
| 1811 | + // Don't include own group shares |
|
| 1812 | + $where .= ' AND `uid_owner` != ?'; |
|
| 1813 | + $queryArgs[] = $shareWith; |
|
| 1814 | + } else { |
|
| 1815 | + $where .= ' AND `share_type` = ?'; |
|
| 1816 | + $queryArgs[] = $shareType; |
|
| 1817 | + if (isset($shareWith)) { |
|
| 1818 | + $where .= ' AND `share_with` = ?'; |
|
| 1819 | + $queryArgs[] = $shareWith; |
|
| 1820 | + } |
|
| 1821 | + } |
|
| 1822 | + } |
|
| 1823 | + if (isset($uidOwner)) { |
|
| 1824 | + $where .= ' AND `uid_owner` = ?'; |
|
| 1825 | + $queryArgs[] = $uidOwner; |
|
| 1826 | + if (!isset($shareType)) { |
|
| 1827 | + // Prevent unique user targets for group shares from being selected |
|
| 1828 | + $where .= ' AND `share_type` != ?'; |
|
| 1829 | + $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 1830 | + } |
|
| 1831 | + if ($fileDependent) { |
|
| 1832 | + $column = 'file_source'; |
|
| 1833 | + } else { |
|
| 1834 | + $column = 'item_source'; |
|
| 1835 | + } |
|
| 1836 | + } else { |
|
| 1837 | + if ($fileDependent) { |
|
| 1838 | + $column = 'file_target'; |
|
| 1839 | + } else { |
|
| 1840 | + $column = 'item_target'; |
|
| 1841 | + } |
|
| 1842 | + } |
|
| 1843 | + if (isset($item)) { |
|
| 1844 | + $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 1845 | + if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 1846 | + $where .= ' AND ('; |
|
| 1847 | + } else { |
|
| 1848 | + $where .= ' AND'; |
|
| 1849 | + } |
|
| 1850 | + // If looking for own shared items, check item_source else check item_target |
|
| 1851 | + if (isset($uidOwner) || $itemShareWithBySource) { |
|
| 1852 | + // If item type is a file, file source needs to be checked in case the item was converted |
|
| 1853 | + if ($fileDependent) { |
|
| 1854 | + $where .= ' `file_source` = ?'; |
|
| 1855 | + $column = 'file_source'; |
|
| 1856 | + } else { |
|
| 1857 | + $where .= ' `item_source` = ?'; |
|
| 1858 | + $column = 'item_source'; |
|
| 1859 | + } |
|
| 1860 | + } else { |
|
| 1861 | + if ($fileDependent) { |
|
| 1862 | + $where .= ' `file_target` = ?'; |
|
| 1863 | + $item = \OC\Files\Filesystem::normalizePath($item); |
|
| 1864 | + } else { |
|
| 1865 | + $where .= ' `item_target` = ?'; |
|
| 1866 | + } |
|
| 1867 | + } |
|
| 1868 | + $queryArgs[] = $item; |
|
| 1869 | + if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 1870 | + $placeholders = join(',', array_fill(0, count($collectionTypes), '?')); |
|
| 1871 | + $where .= ' OR `item_type` IN ('.$placeholders.'))'; |
|
| 1872 | + $queryArgs = array_merge($queryArgs, $collectionTypes); |
|
| 1873 | + } |
|
| 1874 | + } |
|
| 1875 | + |
|
| 1876 | + if ($shareType == self::$shareTypeUserAndGroups && $limit === 1) { |
|
| 1877 | + // Make sure the unique user target is returned if it exists, |
|
| 1878 | + // unique targets should follow the group share in the database |
|
| 1879 | + // If the limit is not 1, the filtering can be done later |
|
| 1880 | + $where .= ' ORDER BY `*PREFIX*share`.`id` DESC'; |
|
| 1881 | + } else { |
|
| 1882 | + $where .= ' ORDER BY `*PREFIX*share`.`id` ASC'; |
|
| 1883 | + } |
|
| 1884 | + |
|
| 1885 | + if ($limit != -1 && !$includeCollections) { |
|
| 1886 | + // The limit must be at least 3, because filtering needs to be done |
|
| 1887 | + if ($limit < 3) { |
|
| 1888 | + $queryLimit = 3; |
|
| 1889 | + } else { |
|
| 1890 | + $queryLimit = $limit; |
|
| 1891 | + } |
|
| 1892 | + } else { |
|
| 1893 | + $queryLimit = null; |
|
| 1894 | + } |
|
| 1895 | + $select = self::createSelectStatement($format, $fileDependent, $uidOwner); |
|
| 1896 | + $root = strlen($root); |
|
| 1897 | + $query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$where, $queryLimit); |
|
| 1898 | + $result = $query->execute($queryArgs); |
|
| 1899 | + if ($result === false) { |
|
| 1900 | + \OCP\Util::writeLog('OCP\Share', |
|
| 1901 | + \OC_DB::getErrorMessage() . ', select=' . $select . ' where=', |
|
| 1902 | + \OCP\Util::ERROR); |
|
| 1903 | + } |
|
| 1904 | + $items = array(); |
|
| 1905 | + $targets = array(); |
|
| 1906 | + $switchedItems = array(); |
|
| 1907 | + $mounts = array(); |
|
| 1908 | + while ($row = $result->fetchRow()) { |
|
| 1909 | + self::transformDBResults($row); |
|
| 1910 | + // Filter out duplicate group shares for users with unique targets |
|
| 1911 | + if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 1912 | + continue; |
|
| 1913 | + } |
|
| 1914 | + if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) { |
|
| 1915 | + $row['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 1916 | + $row['unique_name'] = true; // remember that we use a unique name for this user |
|
| 1917 | + $row['share_with'] = $items[$row['parent']]['share_with']; |
|
| 1918 | + // if the group share was unshared from the user we keep the permission, otherwise |
|
| 1919 | + // we take the permission from the parent because this is always the up-to-date |
|
| 1920 | + // permission for the group share |
|
| 1921 | + if ($row['permissions'] > 0) { |
|
| 1922 | + $row['permissions'] = $items[$row['parent']]['permissions']; |
|
| 1923 | + } |
|
| 1924 | + // Remove the parent group share |
|
| 1925 | + unset($items[$row['parent']]); |
|
| 1926 | + if ($row['permissions'] == 0) { |
|
| 1927 | + continue; |
|
| 1928 | + } |
|
| 1929 | + } else if (!isset($uidOwner)) { |
|
| 1930 | + // Check if the same target already exists |
|
| 1931 | + if (isset($targets[$row['id']])) { |
|
| 1932 | + // Check if the same owner shared with the user twice |
|
| 1933 | + // through a group and user share - this is allowed |
|
| 1934 | + $id = $targets[$row['id']]; |
|
| 1935 | + if (isset($items[$id]) && $items[$id]['uid_owner'] == $row['uid_owner']) { |
|
| 1936 | + // Switch to group share type to ensure resharing conditions aren't bypassed |
|
| 1937 | + if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) { |
|
| 1938 | + $items[$id]['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 1939 | + $items[$id]['share_with'] = $row['share_with']; |
|
| 1940 | + } |
|
| 1941 | + // Switch ids if sharing permission is granted on only |
|
| 1942 | + // one share to ensure correct parent is used if resharing |
|
| 1943 | + if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE |
|
| 1944 | + && (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 1945 | + $items[$row['id']] = $items[$id]; |
|
| 1946 | + $switchedItems[$id] = $row['id']; |
|
| 1947 | + unset($items[$id]); |
|
| 1948 | + $id = $row['id']; |
|
| 1949 | + } |
|
| 1950 | + $items[$id]['permissions'] |= (int)$row['permissions']; |
|
| 1951 | + |
|
| 1952 | + } |
|
| 1953 | + continue; |
|
| 1954 | + } elseif (!empty($row['parent'])) { |
|
| 1955 | + $targets[$row['parent']] = $row['id']; |
|
| 1956 | + } |
|
| 1957 | + } |
|
| 1958 | + // Remove root from file source paths if retrieving own shared items |
|
| 1959 | + if (isset($uidOwner) && isset($row['path'])) { |
|
| 1960 | + if (isset($row['parent'])) { |
|
| 1961 | + $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); |
|
| 1962 | + $parentResult = $query->execute(array($row['parent'])); |
|
| 1963 | + if ($result === false) { |
|
| 1964 | + \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' . |
|
| 1965 | + \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where, |
|
| 1966 | + \OCP\Util::ERROR); |
|
| 1967 | + } else { |
|
| 1968 | + $parentRow = $parentResult->fetchRow(); |
|
| 1969 | + $tmpPath = $parentRow['file_target']; |
|
| 1970 | + // find the right position where the row path continues from the target path |
|
| 1971 | + $pos = strrpos($row['path'], $parentRow['file_target']); |
|
| 1972 | + $subPath = substr($row['path'], $pos); |
|
| 1973 | + $splitPath = explode('/', $subPath); |
|
| 1974 | + foreach (array_slice($splitPath, 2) as $pathPart) { |
|
| 1975 | + $tmpPath = $tmpPath . '/' . $pathPart; |
|
| 1976 | + } |
|
| 1977 | + $row['path'] = $tmpPath; |
|
| 1978 | + } |
|
| 1979 | + } else { |
|
| 1980 | + if (!isset($mounts[$row['storage']])) { |
|
| 1981 | + $mountPoints = \OC\Files\Filesystem::getMountByNumericId($row['storage']); |
|
| 1982 | + if (is_array($mountPoints) && !empty($mountPoints)) { |
|
| 1983 | + $mounts[$row['storage']] = current($mountPoints); |
|
| 1984 | + } |
|
| 1985 | + } |
|
| 1986 | + if (!empty($mounts[$row['storage']])) { |
|
| 1987 | + $path = $mounts[$row['storage']]->getMountPoint().$row['path']; |
|
| 1988 | + $relPath = substr($path, $root); // path relative to data/user |
|
| 1989 | + $row['path'] = rtrim($relPath, '/'); |
|
| 1990 | + } |
|
| 1991 | + } |
|
| 1992 | + } |
|
| 1993 | + |
|
| 1994 | + if($checkExpireDate) { |
|
| 1995 | + if (self::expireItem($row)) { |
|
| 1996 | + continue; |
|
| 1997 | + } |
|
| 1998 | + } |
|
| 1999 | + // Check if resharing is allowed, if not remove share permission |
|
| 2000 | + if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) { |
|
| 2001 | + $row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE; |
|
| 2002 | + } |
|
| 2003 | + // Add display names to result |
|
| 2004 | + $row['share_with_displayname'] = $row['share_with']; |
|
| 2005 | + if ( isset($row['share_with']) && $row['share_with'] != '' && |
|
| 2006 | + $row['share_type'] === self::SHARE_TYPE_USER) { |
|
| 2007 | + $row['share_with_displayname'] = \OCP\User::getDisplayName($row['share_with']); |
|
| 2008 | + } else if(isset($row['share_with']) && $row['share_with'] != '' && |
|
| 2009 | + $row['share_type'] === self::SHARE_TYPE_REMOTE) { |
|
| 2010 | + $addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']); |
|
| 2011 | + foreach ($addressBookEntries as $entry) { |
|
| 2012 | + foreach ($entry['CLOUD'] as $cloudID) { |
|
| 2013 | + if ($cloudID === $row['share_with']) { |
|
| 2014 | + $row['share_with_displayname'] = $entry['FN']; |
|
| 2015 | + } |
|
| 2016 | + } |
|
| 2017 | + } |
|
| 2018 | + } |
|
| 2019 | + if ( isset($row['uid_owner']) && $row['uid_owner'] != '') { |
|
| 2020 | + $row['displayname_owner'] = \OCP\User::getDisplayName($row['uid_owner']); |
|
| 2021 | + } |
|
| 2022 | + |
|
| 2023 | + if ($row['permissions'] > 0) { |
|
| 2024 | + $items[$row['id']] = $row; |
|
| 2025 | + } |
|
| 2026 | + |
|
| 2027 | + } |
|
| 2028 | + |
|
| 2029 | + // group items if we are looking for items shared with the current user |
|
| 2030 | + if (isset($shareWith) && $shareWith === \OCP\User::getUser()) { |
|
| 2031 | + $items = self::groupItems($items, $itemType); |
|
| 2032 | + } |
|
| 2033 | + |
|
| 2034 | + if (!empty($items)) { |
|
| 2035 | + $collectionItems = array(); |
|
| 2036 | + foreach ($items as &$row) { |
|
| 2037 | + // Return only the item instead of a 2-dimensional array |
|
| 2038 | + if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) { |
|
| 2039 | + if ($format == self::FORMAT_NONE) { |
|
| 2040 | + return $row; |
|
| 2041 | + } else { |
|
| 2042 | + break; |
|
| 2043 | + } |
|
| 2044 | + } |
|
| 2045 | + // Check if this is a collection of the requested item type |
|
| 2046 | + if ($includeCollections && $collectionTypes && $row['item_type'] !== 'folder' && in_array($row['item_type'], $collectionTypes)) { |
|
| 2047 | + if (($collectionBackend = self::getBackend($row['item_type'])) |
|
| 2048 | + && $collectionBackend instanceof \OCP\Share_Backend_Collection) { |
|
| 2049 | + // Collections can be inside collections, check if the item is a collection |
|
| 2050 | + if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) { |
|
| 2051 | + $collectionItems[] = $row; |
|
| 2052 | + } else { |
|
| 2053 | + $collection = array(); |
|
| 2054 | + $collection['item_type'] = $row['item_type']; |
|
| 2055 | + if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 2056 | + $collection['path'] = basename($row['path']); |
|
| 2057 | + } |
|
| 2058 | + $row['collection'] = $collection; |
|
| 2059 | + // Fetch all of the children sources |
|
| 2060 | + $children = $collectionBackend->getChildren($row[$column]); |
|
| 2061 | + foreach ($children as $child) { |
|
| 2062 | + $childItem = $row; |
|
| 2063 | + $childItem['item_type'] = $itemType; |
|
| 2064 | + if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') { |
|
| 2065 | + $childItem['item_source'] = $child['source']; |
|
| 2066 | + $childItem['item_target'] = $child['target']; |
|
| 2067 | + } |
|
| 2068 | + if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 2069 | + if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 2070 | + $childItem['file_source'] = $child['source']; |
|
| 2071 | + } else { // TODO is this really needed if we already know that we use the file backend? |
|
| 2072 | + $meta = \OC\Files\Filesystem::getFileInfo($child['file_path']); |
|
| 2073 | + $childItem['file_source'] = $meta['fileid']; |
|
| 2074 | + } |
|
| 2075 | + $childItem['file_target'] = |
|
| 2076 | + \OC\Files\Filesystem::normalizePath($child['file_path']); |
|
| 2077 | + } |
|
| 2078 | + if (isset($item)) { |
|
| 2079 | + if ($childItem[$column] == $item) { |
|
| 2080 | + // Return only the item instead of a 2-dimensional array |
|
| 2081 | + if ($limit == 1) { |
|
| 2082 | + if ($format == self::FORMAT_NONE) { |
|
| 2083 | + return $childItem; |
|
| 2084 | + } else { |
|
| 2085 | + // Unset the items array and break out of both loops |
|
| 2086 | + $items = array(); |
|
| 2087 | + $items[] = $childItem; |
|
| 2088 | + break 2; |
|
| 2089 | + } |
|
| 2090 | + } else { |
|
| 2091 | + $collectionItems[] = $childItem; |
|
| 2092 | + } |
|
| 2093 | + } |
|
| 2094 | + } else { |
|
| 2095 | + $collectionItems[] = $childItem; |
|
| 2096 | + } |
|
| 2097 | + } |
|
| 2098 | + } |
|
| 2099 | + } |
|
| 2100 | + // Remove collection item |
|
| 2101 | + $toRemove = $row['id']; |
|
| 2102 | + if (array_key_exists($toRemove, $switchedItems)) { |
|
| 2103 | + $toRemove = $switchedItems[$toRemove]; |
|
| 2104 | + } |
|
| 2105 | + unset($items[$toRemove]); |
|
| 2106 | + } elseif ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) { |
|
| 2107 | + // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 2108 | + // see github issue #10588 for more details |
|
| 2109 | + // Need to find a solution which works for all back-ends |
|
| 2110 | + $collectionBackend = self::getBackend($row['item_type']); |
|
| 2111 | + $sharedParents = $collectionBackend->getParents($row['item_source']); |
|
| 2112 | + foreach ($sharedParents as $parent) { |
|
| 2113 | + $collectionItems[] = $parent; |
|
| 2114 | + } |
|
| 2115 | + } |
|
| 2116 | + } |
|
| 2117 | + if (!empty($collectionItems)) { |
|
| 2118 | + $collectionItems = array_unique($collectionItems, SORT_REGULAR); |
|
| 2119 | + $items = array_merge($items, $collectionItems); |
|
| 2120 | + } |
|
| 2121 | + |
|
| 2122 | + // filter out invalid items, these can appear when subshare entries exist |
|
| 2123 | + // for a group in which the requested user isn't a member any more |
|
| 2124 | + $items = array_filter($items, function($item) { |
|
| 2125 | + return $item['share_type'] !== self::$shareTypeGroupUserUnique; |
|
| 2126 | + }); |
|
| 2127 | + |
|
| 2128 | + return self::formatResult($items, $column, $backend, $format, $parameters); |
|
| 2129 | + } elseif ($includeCollections && $collectionTypes && in_array('folder', $collectionTypes)) { |
|
| 2130 | + // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 2131 | + // see github issue #10588 for more details |
|
| 2132 | + // Need to find a solution which works for all back-ends |
|
| 2133 | + $collectionItems = array(); |
|
| 2134 | + $collectionBackend = self::getBackend('folder'); |
|
| 2135 | + $sharedParents = $collectionBackend->getParents($item, $shareWith, $uidOwner); |
|
| 2136 | + foreach ($sharedParents as $parent) { |
|
| 2137 | + $collectionItems[] = $parent; |
|
| 2138 | + } |
|
| 2139 | + if ($limit === 1) { |
|
| 2140 | + return reset($collectionItems); |
|
| 2141 | + } |
|
| 2142 | + return self::formatResult($collectionItems, $column, $backend, $format, $parameters); |
|
| 2143 | + } |
|
| 2144 | + |
|
| 2145 | + return array(); |
|
| 2146 | + } |
|
| 2147 | + |
|
| 2148 | + /** |
|
| 2149 | + * group items with link to the same source |
|
| 2150 | + * |
|
| 2151 | + * @param array $items |
|
| 2152 | + * @param string $itemType |
|
| 2153 | + * @return array of grouped items |
|
| 2154 | + */ |
|
| 2155 | + protected static function groupItems($items, $itemType) { |
|
| 2156 | + |
|
| 2157 | + $fileSharing = ($itemType === 'file' || $itemType === 'folder') ? true : false; |
|
| 2158 | + |
|
| 2159 | + $result = array(); |
|
| 2160 | + |
|
| 2161 | + foreach ($items as $item) { |
|
| 2162 | + $grouped = false; |
|
| 2163 | + foreach ($result as $key => $r) { |
|
| 2164 | + // for file/folder shares we need to compare file_source, otherwise we compare item_source |
|
| 2165 | + // only group shares if they already point to the same target, otherwise the file where shared |
|
| 2166 | + // before grouping of shares was added. In this case we don't group them toi avoid confusions |
|
| 2167 | + if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) || |
|
| 2168 | + (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) { |
|
| 2169 | + // add the first item to the list of grouped shares |
|
| 2170 | + if (!isset($result[$key]['grouped'])) { |
|
| 2171 | + $result[$key]['grouped'][] = $result[$key]; |
|
| 2172 | + } |
|
| 2173 | + $result[$key]['permissions'] = (int) $item['permissions'] | (int) $r['permissions']; |
|
| 2174 | + $result[$key]['grouped'][] = $item; |
|
| 2175 | + $grouped = true; |
|
| 2176 | + break; |
|
| 2177 | + } |
|
| 2178 | + } |
|
| 2179 | + |
|
| 2180 | + if (!$grouped) { |
|
| 2181 | + $result[] = $item; |
|
| 2182 | + } |
|
| 2183 | + |
|
| 2184 | + } |
|
| 2185 | + |
|
| 2186 | + return $result; |
|
| 2187 | + } |
|
| 2188 | + |
|
| 2189 | + /** |
|
| 2190 | + * Put shared item into the database |
|
| 2191 | + * @param string $itemType Item type |
|
| 2192 | + * @param string $itemSource Item source |
|
| 2193 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 2194 | + * @param string $shareWith User or group the item is being shared with |
|
| 2195 | + * @param string $uidOwner User that is the owner of shared item |
|
| 2196 | + * @param int $permissions CRUDS permissions |
|
| 2197 | + * @param boolean|array $parentFolder Parent folder target (optional) |
|
| 2198 | + * @param string $token (optional) |
|
| 2199 | + * @param string $itemSourceName name of the source item (optional) |
|
| 2200 | + * @param \DateTime $expirationDate (optional) |
|
| 2201 | + * @throws \Exception |
|
| 2202 | + * @return mixed id of the new share or false |
|
| 2203 | + */ |
|
| 2204 | + private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 2205 | + $permissions, $parentFolder = null, $token = null, $itemSourceName = null, \DateTime $expirationDate = null) { |
|
| 2206 | + |
|
| 2207 | + $queriesToExecute = array(); |
|
| 2208 | + $suggestedItemTarget = null; |
|
| 2209 | + $groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = ''; |
|
| 2210 | + $groupItemTarget = $itemTarget = $fileSource = $parent = 0; |
|
| 2211 | + |
|
| 2212 | + $result = self::checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate); |
|
| 2213 | + if(!empty($result)) { |
|
| 2214 | + $parent = $result['parent']; |
|
| 2215 | + $itemSource = $result['itemSource']; |
|
| 2216 | + $fileSource = $result['fileSource']; |
|
| 2217 | + $suggestedItemTarget = $result['suggestedItemTarget']; |
|
| 2218 | + $suggestedFileTarget = $result['suggestedFileTarget']; |
|
| 2219 | + $filePath = $result['filePath']; |
|
| 2220 | + } |
|
| 2221 | + |
|
| 2222 | + $isGroupShare = false; |
|
| 2223 | + if ($shareType == self::SHARE_TYPE_GROUP) { |
|
| 2224 | + $isGroupShare = true; |
|
| 2225 | + if (isset($shareWith['users'])) { |
|
| 2226 | + $users = $shareWith['users']; |
|
| 2227 | + } else { |
|
| 2228 | + $group = \OC::$server->getGroupManager()->get($shareWith['group']); |
|
| 2229 | + if ($group) { |
|
| 2230 | + $users = $group->searchUsers('', -1, 0); |
|
| 2231 | + $userIds = []; |
|
| 2232 | + foreach ($users as $user) { |
|
| 2233 | + $userIds[] = $user->getUID(); |
|
| 2234 | + } |
|
| 2235 | + $users = $userIds; |
|
| 2236 | + } else { |
|
| 2237 | + $users = []; |
|
| 2238 | + } |
|
| 2239 | + } |
|
| 2240 | + // remove current user from list |
|
| 2241 | + if (in_array(\OCP\User::getUser(), $users)) { |
|
| 2242 | + unset($users[array_search(\OCP\User::getUser(), $users)]); |
|
| 2243 | + } |
|
| 2244 | + $groupItemTarget = Helper::generateTarget($itemType, $itemSource, |
|
| 2245 | + $shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget); |
|
| 2246 | + $groupFileTarget = Helper::generateTarget($itemType, $itemSource, |
|
| 2247 | + $shareType, $shareWith['group'], $uidOwner, $filePath); |
|
| 2248 | + |
|
| 2249 | + // add group share to table and remember the id as parent |
|
| 2250 | + $queriesToExecute['groupShare'] = array( |
|
| 2251 | + 'itemType' => $itemType, |
|
| 2252 | + 'itemSource' => $itemSource, |
|
| 2253 | + 'itemTarget' => $groupItemTarget, |
|
| 2254 | + 'shareType' => $shareType, |
|
| 2255 | + 'shareWith' => $shareWith['group'], |
|
| 2256 | + 'uidOwner' => $uidOwner, |
|
| 2257 | + 'permissions' => $permissions, |
|
| 2258 | + 'shareTime' => time(), |
|
| 2259 | + 'fileSource' => $fileSource, |
|
| 2260 | + 'fileTarget' => $groupFileTarget, |
|
| 2261 | + 'token' => $token, |
|
| 2262 | + 'parent' => $parent, |
|
| 2263 | + 'expiration' => $expirationDate, |
|
| 2264 | + ); |
|
| 2265 | + |
|
| 2266 | + } else { |
|
| 2267 | + $users = array($shareWith); |
|
| 2268 | + $itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 2269 | + $suggestedItemTarget); |
|
| 2270 | + } |
|
| 2271 | + |
|
| 2272 | + $run = true; |
|
| 2273 | + $error = ''; |
|
| 2274 | + $preHookData = array( |
|
| 2275 | + 'itemType' => $itemType, |
|
| 2276 | + 'itemSource' => $itemSource, |
|
| 2277 | + 'shareType' => $shareType, |
|
| 2278 | + 'uidOwner' => $uidOwner, |
|
| 2279 | + 'permissions' => $permissions, |
|
| 2280 | + 'fileSource' => $fileSource, |
|
| 2281 | + 'expiration' => $expirationDate, |
|
| 2282 | + 'token' => $token, |
|
| 2283 | + 'run' => &$run, |
|
| 2284 | + 'error' => &$error |
|
| 2285 | + ); |
|
| 2286 | + |
|
| 2287 | + $preHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget; |
|
| 2288 | + $preHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith; |
|
| 2289 | + |
|
| 2290 | + \OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData); |
|
| 2291 | + |
|
| 2292 | + if ($run === false) { |
|
| 2293 | + throw new \Exception($error); |
|
| 2294 | + } |
|
| 2295 | + |
|
| 2296 | + foreach ($users as $user) { |
|
| 2297 | + $sourceId = ($itemType === 'file' || $itemType === 'folder') ? $fileSource : $itemSource; |
|
| 2298 | + $sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user); |
|
| 2299 | + |
|
| 2300 | + $userShareType = ($isGroupShare) ? self::$shareTypeGroupUserUnique : $shareType; |
|
| 2301 | + |
|
| 2302 | + if ($sourceExists && $sourceExists['item_source'] === $itemSource) { |
|
| 2303 | + $fileTarget = $sourceExists['file_target']; |
|
| 2304 | + $itemTarget = $sourceExists['item_target']; |
|
| 2305 | + |
|
| 2306 | + // for group shares we don't need a additional entry if the target is the same |
|
| 2307 | + if($isGroupShare && $groupItemTarget === $itemTarget) { |
|
| 2308 | + continue; |
|
| 2309 | + } |
|
| 2310 | + |
|
| 2311 | + } elseif(!$sourceExists && !$isGroupShare) { |
|
| 2312 | + |
|
| 2313 | + $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user, |
|
| 2314 | + $uidOwner, $suggestedItemTarget, $parent); |
|
| 2315 | + if (isset($fileSource)) { |
|
| 2316 | + if ($parentFolder) { |
|
| 2317 | + if ($parentFolder === true) { |
|
| 2318 | + $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, $user, |
|
| 2319 | + $uidOwner, $suggestedFileTarget, $parent); |
|
| 2320 | + if ($fileTarget != $groupFileTarget) { |
|
| 2321 | + $parentFolders[$user]['folder'] = $fileTarget; |
|
| 2322 | + } |
|
| 2323 | + } else if (isset($parentFolder[$user])) { |
|
| 2324 | + $fileTarget = $parentFolder[$user]['folder'].$itemSource; |
|
| 2325 | + $parent = $parentFolder[$user]['id']; |
|
| 2326 | + } |
|
| 2327 | + } else { |
|
| 2328 | + $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, |
|
| 2329 | + $user, $uidOwner, $suggestedFileTarget, $parent); |
|
| 2330 | + } |
|
| 2331 | + } else { |
|
| 2332 | + $fileTarget = null; |
|
| 2333 | + } |
|
| 2334 | + |
|
| 2335 | + } else { |
|
| 2336 | + |
|
| 2337 | + // group share which doesn't exists until now, check if we need a unique target for this user |
|
| 2338 | + |
|
| 2339 | + $itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user, |
|
| 2340 | + $uidOwner, $suggestedItemTarget, $parent); |
|
| 2341 | + |
|
| 2342 | + // do we also need a file target |
|
| 2343 | + if (isset($fileSource)) { |
|
| 2344 | + $fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user, |
|
| 2345 | + $uidOwner, $suggestedFileTarget, $parent); |
|
| 2346 | + } else { |
|
| 2347 | + $fileTarget = null; |
|
| 2348 | + } |
|
| 2349 | + |
|
| 2350 | + if (($itemTarget === $groupItemTarget) && |
|
| 2351 | + (!isset($fileSource) || $fileTarget === $groupFileTarget)) { |
|
| 2352 | + continue; |
|
| 2353 | + } |
|
| 2354 | + } |
|
| 2355 | + |
|
| 2356 | + $queriesToExecute[] = array( |
|
| 2357 | + 'itemType' => $itemType, |
|
| 2358 | + 'itemSource' => $itemSource, |
|
| 2359 | + 'itemTarget' => $itemTarget, |
|
| 2360 | + 'shareType' => $userShareType, |
|
| 2361 | + 'shareWith' => $user, |
|
| 2362 | + 'uidOwner' => $uidOwner, |
|
| 2363 | + 'permissions' => $permissions, |
|
| 2364 | + 'shareTime' => time(), |
|
| 2365 | + 'fileSource' => $fileSource, |
|
| 2366 | + 'fileTarget' => $fileTarget, |
|
| 2367 | + 'token' => $token, |
|
| 2368 | + 'parent' => $parent, |
|
| 2369 | + 'expiration' => $expirationDate, |
|
| 2370 | + ); |
|
| 2371 | + |
|
| 2372 | + } |
|
| 2373 | + |
|
| 2374 | + $id = false; |
|
| 2375 | + if ($isGroupShare) { |
|
| 2376 | + $id = self::insertShare($queriesToExecute['groupShare']); |
|
| 2377 | + // Save this id, any extra rows for this group share will need to reference it |
|
| 2378 | + $parent = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 2379 | + unset($queriesToExecute['groupShare']); |
|
| 2380 | + } |
|
| 2381 | + |
|
| 2382 | + foreach ($queriesToExecute as $shareQuery) { |
|
| 2383 | + $shareQuery['parent'] = $parent; |
|
| 2384 | + $id = self::insertShare($shareQuery); |
|
| 2385 | + } |
|
| 2386 | + |
|
| 2387 | + $postHookData = array( |
|
| 2388 | + 'itemType' => $itemType, |
|
| 2389 | + 'itemSource' => $itemSource, |
|
| 2390 | + 'parent' => $parent, |
|
| 2391 | + 'shareType' => $shareType, |
|
| 2392 | + 'uidOwner' => $uidOwner, |
|
| 2393 | + 'permissions' => $permissions, |
|
| 2394 | + 'fileSource' => $fileSource, |
|
| 2395 | + 'id' => $parent, |
|
| 2396 | + 'token' => $token, |
|
| 2397 | + 'expirationDate' => $expirationDate, |
|
| 2398 | + ); |
|
| 2399 | + |
|
| 2400 | + $postHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith; |
|
| 2401 | + $postHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget; |
|
| 2402 | + $postHookData['fileTarget'] = ($isGroupShare) ? $groupFileTarget : $fileTarget; |
|
| 2403 | + |
|
| 2404 | + \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData); |
|
| 2405 | + |
|
| 2406 | + |
|
| 2407 | + return $id ? $id : false; |
|
| 2408 | + } |
|
| 2409 | + |
|
| 2410 | + /** |
|
| 2411 | + * @param string $itemType |
|
| 2412 | + * @param string $itemSource |
|
| 2413 | + * @param int $shareType |
|
| 2414 | + * @param string $shareWith |
|
| 2415 | + * @param string $uidOwner |
|
| 2416 | + * @param int $permissions |
|
| 2417 | + * @param string|null $itemSourceName |
|
| 2418 | + * @param null|\DateTime $expirationDate |
|
| 2419 | + */ |
|
| 2420 | + private static function checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate) { |
|
| 2421 | + $backend = self::getBackend($itemType); |
|
| 2422 | + |
|
| 2423 | + $l = \OC::$server->getL10N('lib'); |
|
| 2424 | + $result = array(); |
|
| 2425 | + |
|
| 2426 | + $column = ($itemType === 'file' || $itemType === 'folder') ? 'file_source' : 'item_source'; |
|
| 2427 | + |
|
| 2428 | + $checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true); |
|
| 2429 | + if ($checkReshare) { |
|
| 2430 | + // Check if attempting to share back to owner |
|
| 2431 | + if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) { |
|
| 2432 | + $message = 'Sharing %s failed, because the user %s is the original sharer'; |
|
| 2433 | + $message_t = $l->t('Sharing failed, because the user %s is the original sharer', [$shareWith]); |
|
| 2434 | + |
|
| 2435 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 2436 | + throw new \Exception($message_t); |
|
| 2437 | + } |
|
| 2438 | + } |
|
| 2439 | + |
|
| 2440 | + if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) { |
|
| 2441 | + // Check if share permissions is granted |
|
| 2442 | + if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 2443 | + if (~(int)$checkReshare['permissions'] & $permissions) { |
|
| 2444 | + $message = 'Sharing %s failed, because the permissions exceed permissions granted to %s'; |
|
| 2445 | + $message_t = $l->t('Sharing %s failed, because the permissions exceed permissions granted to %s', array($itemSourceName, $uidOwner)); |
|
| 2446 | + |
|
| 2447 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner), \OCP\Util::DEBUG); |
|
| 2448 | + throw new \Exception($message_t); |
|
| 2449 | + } else { |
|
| 2450 | + // TODO Don't check if inside folder |
|
| 2451 | + $result['parent'] = $checkReshare['id']; |
|
| 2452 | + |
|
| 2453 | + $result['expirationDate'] = $expirationDate; |
|
| 2454 | + // $checkReshare['expiration'] could be null and then is always less than any value |
|
| 2455 | + if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) { |
|
| 2456 | + $result['expirationDate'] = $checkReshare['expiration']; |
|
| 2457 | + } |
|
| 2458 | + |
|
| 2459 | + // only suggest the same name as new target if it is a reshare of the |
|
| 2460 | + // same file/folder and not the reshare of a child |
|
| 2461 | + if ($checkReshare[$column] === $itemSource) { |
|
| 2462 | + $result['filePath'] = $checkReshare['file_target']; |
|
| 2463 | + $result['itemSource'] = $checkReshare['item_source']; |
|
| 2464 | + $result['fileSource'] = $checkReshare['file_source']; |
|
| 2465 | + $result['suggestedItemTarget'] = $checkReshare['item_target']; |
|
| 2466 | + $result['suggestedFileTarget'] = $checkReshare['file_target']; |
|
| 2467 | + } else { |
|
| 2468 | + $result['filePath'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $backend->getFilePath($itemSource, $uidOwner) : null; |
|
| 2469 | + $result['suggestedItemTarget'] = null; |
|
| 2470 | + $result['suggestedFileTarget'] = null; |
|
| 2471 | + $result['itemSource'] = $itemSource; |
|
| 2472 | + $result['fileSource'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $itemSource : null; |
|
| 2473 | + } |
|
| 2474 | + } |
|
| 2475 | + } else { |
|
| 2476 | + $message = 'Sharing %s failed, because resharing is not allowed'; |
|
| 2477 | + $message_t = $l->t('Sharing %s failed, because resharing is not allowed', array($itemSourceName)); |
|
| 2478 | + |
|
| 2479 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 2480 | + throw new \Exception($message_t); |
|
| 2481 | + } |
|
| 2482 | + } else { |
|
| 2483 | + $result['parent'] = null; |
|
| 2484 | + $result['suggestedItemTarget'] = null; |
|
| 2485 | + $result['suggestedFileTarget'] = null; |
|
| 2486 | + $result['itemSource'] = $itemSource; |
|
| 2487 | + $result['expirationDate'] = $expirationDate; |
|
| 2488 | + if (!$backend->isValidSource($itemSource, $uidOwner)) { |
|
| 2489 | + $message = 'Sharing %s failed, because the sharing backend for ' |
|
| 2490 | + .'%s could not find its source'; |
|
| 2491 | + $message_t = $l->t('Sharing %s failed, because the sharing backend for %s could not find its source', array($itemSource, $itemType)); |
|
| 2492 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, $itemType), \OCP\Util::DEBUG); |
|
| 2493 | + throw new \Exception($message_t); |
|
| 2494 | + } |
|
| 2495 | + if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 2496 | + $result['filePath'] = $backend->getFilePath($itemSource, $uidOwner); |
|
| 2497 | + if ($itemType == 'file' || $itemType == 'folder') { |
|
| 2498 | + $result['fileSource'] = $itemSource; |
|
| 2499 | + } else { |
|
| 2500 | + $meta = \OC\Files\Filesystem::getFileInfo($result['filePath']); |
|
| 2501 | + $result['fileSource'] = $meta['fileid']; |
|
| 2502 | + } |
|
| 2503 | + if ($result['fileSource'] == -1) { |
|
| 2504 | + $message = 'Sharing %s failed, because the file could not be found in the file cache'; |
|
| 2505 | + $message_t = $l->t('Sharing %s failed, because the file could not be found in the file cache', array($itemSource)); |
|
| 2506 | + |
|
| 2507 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG); |
|
| 2508 | + throw new \Exception($message_t); |
|
| 2509 | + } |
|
| 2510 | + } else { |
|
| 2511 | + $result['filePath'] = null; |
|
| 2512 | + $result['fileSource'] = null; |
|
| 2513 | + } |
|
| 2514 | + } |
|
| 2515 | + |
|
| 2516 | + return $result; |
|
| 2517 | + } |
|
| 2518 | + |
|
| 2519 | + /** |
|
| 2520 | + * |
|
| 2521 | + * @param array $shareData |
|
| 2522 | + * @return mixed false in case of a failure or the id of the new share |
|
| 2523 | + */ |
|
| 2524 | + private static function insertShare(array $shareData) { |
|
| 2525 | + |
|
| 2526 | + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' |
|
| 2527 | + .' `item_type`, `item_source`, `item_target`, `share_type`,' |
|
| 2528 | + .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,' |
|
| 2529 | + .' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 2530 | + $query->bindValue(1, $shareData['itemType']); |
|
| 2531 | + $query->bindValue(2, $shareData['itemSource']); |
|
| 2532 | + $query->bindValue(3, $shareData['itemTarget']); |
|
| 2533 | + $query->bindValue(4, $shareData['shareType']); |
|
| 2534 | + $query->bindValue(5, $shareData['shareWith']); |
|
| 2535 | + $query->bindValue(6, $shareData['uidOwner']); |
|
| 2536 | + $query->bindValue(7, $shareData['permissions']); |
|
| 2537 | + $query->bindValue(8, $shareData['shareTime']); |
|
| 2538 | + $query->bindValue(9, $shareData['fileSource']); |
|
| 2539 | + $query->bindValue(10, $shareData['fileTarget']); |
|
| 2540 | + $query->bindValue(11, $shareData['token']); |
|
| 2541 | + $query->bindValue(12, $shareData['parent']); |
|
| 2542 | + $query->bindValue(13, $shareData['expiration'], 'datetime'); |
|
| 2543 | + $result = $query->execute(); |
|
| 2544 | + |
|
| 2545 | + $id = false; |
|
| 2546 | + if ($result) { |
|
| 2547 | + $id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 2548 | + } |
|
| 2549 | + |
|
| 2550 | + return $id; |
|
| 2551 | + |
|
| 2552 | + } |
|
| 2553 | + |
|
| 2554 | + /** |
|
| 2555 | + * Delete all shares with type SHARE_TYPE_LINK |
|
| 2556 | + */ |
|
| 2557 | + public static function removeAllLinkShares() { |
|
| 2558 | + // Delete any link shares |
|
| 2559 | + $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*share` WHERE `share_type` = ?'); |
|
| 2560 | + $result = $query->execute(array(self::SHARE_TYPE_LINK)); |
|
| 2561 | + while ($item = $result->fetchRow()) { |
|
| 2562 | + Helper::delete($item['id']); |
|
| 2563 | + } |
|
| 2564 | + } |
|
| 2565 | + |
|
| 2566 | + /** |
|
| 2567 | + * In case a password protected link is not yet authenticated this function will return false |
|
| 2568 | + * |
|
| 2569 | + * @param array $linkItem |
|
| 2570 | + * @return boolean |
|
| 2571 | + */ |
|
| 2572 | + public static function checkPasswordProtectedShare(array $linkItem) { |
|
| 2573 | + if (!isset($linkItem['share_with'])) { |
|
| 2574 | + return true; |
|
| 2575 | + } |
|
| 2576 | + if (!isset($linkItem['share_type'])) { |
|
| 2577 | + return true; |
|
| 2578 | + } |
|
| 2579 | + if (!isset($linkItem['id'])) { |
|
| 2580 | + return true; |
|
| 2581 | + } |
|
| 2582 | + |
|
| 2583 | + if ($linkItem['share_type'] != \OCP\Share::SHARE_TYPE_LINK) { |
|
| 2584 | + return true; |
|
| 2585 | + } |
|
| 2586 | + |
|
| 2587 | + if ( \OC::$server->getSession()->exists('public_link_authenticated') |
|
| 2588 | + && \OC::$server->getSession()->get('public_link_authenticated') === (string)$linkItem['id'] ) { |
|
| 2589 | + return true; |
|
| 2590 | + } |
|
| 2591 | + |
|
| 2592 | + return false; |
|
| 2593 | + } |
|
| 2594 | + |
|
| 2595 | + /** |
|
| 2596 | + * construct select statement |
|
| 2597 | + * @param int $format |
|
| 2598 | + * @param boolean $fileDependent ist it a file/folder share or a generla share |
|
| 2599 | + * @param string $uidOwner |
|
| 2600 | + * @return string select statement |
|
| 2601 | + */ |
|
| 2602 | + private static function createSelectStatement($format, $fileDependent, $uidOwner = null) { |
|
| 2603 | + $select = '*'; |
|
| 2604 | + if ($format == self::FORMAT_STATUSES) { |
|
| 2605 | + if ($fileDependent) { |
|
| 2606 | + $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, ' |
|
| 2607 | + . '`share_with`, `uid_owner` , `file_source`, `stime`, `*PREFIX*share`.`permissions`, ' |
|
| 2608 | + . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 2609 | + . '`uid_initiator`'; |
|
| 2610 | + } else { |
|
| 2611 | + $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`, `*PREFIX*share`.`permissions`'; |
|
| 2612 | + } |
|
| 2613 | + } else { |
|
| 2614 | + if (isset($uidOwner)) { |
|
| 2615 | + if ($fileDependent) { |
|
| 2616 | + $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`,' |
|
| 2617 | + . ' `share_type`, `share_with`, `file_source`, `file_target`, `path`, `*PREFIX*share`.`permissions`, `stime`,' |
|
| 2618 | + . ' `expiration`, `token`, `storage`, `mail_send`, `uid_owner`, ' |
|
| 2619 | + . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 2620 | + } else { |
|
| 2621 | + $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `*PREFIX*share`.`permissions`,' |
|
| 2622 | + . ' `stime`, `file_source`, `expiration`, `token`, `mail_send`, `uid_owner`'; |
|
| 2623 | + } |
|
| 2624 | + } else { |
|
| 2625 | + if ($fileDependent) { |
|
| 2626 | + if ($format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_GET_FOLDER_CONTENTS || $format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_FILE_APP_ROOT) { |
|
| 2627 | + $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, ' |
|
| 2628 | + . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, ' |
|
| 2629 | + . '`*PREFIX*share`.`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 2630 | + . '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`, `mail_send`'; |
|
| 2631 | + } else { |
|
| 2632 | + $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`,' |
|
| 2633 | + . '`*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`,' |
|
| 2634 | + . '`file_source`, `path`, `file_target`, `*PREFIX*share`.`permissions`,' |
|
| 2635 | + . '`stime`, `expiration`, `token`, `storage`, `mail_send`,' |
|
| 2636 | + . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 2637 | + } |
|
| 2638 | + } |
|
| 2639 | + } |
|
| 2640 | + } |
|
| 2641 | + return $select; |
|
| 2642 | + } |
|
| 2643 | + |
|
| 2644 | + |
|
| 2645 | + /** |
|
| 2646 | + * transform db results |
|
| 2647 | + * @param array $row result |
|
| 2648 | + */ |
|
| 2649 | + private static function transformDBResults(&$row) { |
|
| 2650 | + if (isset($row['id'])) { |
|
| 2651 | + $row['id'] = (int) $row['id']; |
|
| 2652 | + } |
|
| 2653 | + if (isset($row['share_type'])) { |
|
| 2654 | + $row['share_type'] = (int) $row['share_type']; |
|
| 2655 | + } |
|
| 2656 | + if (isset($row['parent'])) { |
|
| 2657 | + $row['parent'] = (int) $row['parent']; |
|
| 2658 | + } |
|
| 2659 | + if (isset($row['file_parent'])) { |
|
| 2660 | + $row['file_parent'] = (int) $row['file_parent']; |
|
| 2661 | + } |
|
| 2662 | + if (isset($row['file_source'])) { |
|
| 2663 | + $row['file_source'] = (int) $row['file_source']; |
|
| 2664 | + } |
|
| 2665 | + if (isset($row['permissions'])) { |
|
| 2666 | + $row['permissions'] = (int) $row['permissions']; |
|
| 2667 | + } |
|
| 2668 | + if (isset($row['storage'])) { |
|
| 2669 | + $row['storage'] = (int) $row['storage']; |
|
| 2670 | + } |
|
| 2671 | + if (isset($row['stime'])) { |
|
| 2672 | + $row['stime'] = (int) $row['stime']; |
|
| 2673 | + } |
|
| 2674 | + if (isset($row['expiration']) && $row['share_type'] !== self::SHARE_TYPE_LINK) { |
|
| 2675 | + // discard expiration date for non-link shares, which might have been |
|
| 2676 | + // set by ancient bugs |
|
| 2677 | + $row['expiration'] = null; |
|
| 2678 | + } |
|
| 2679 | + } |
|
| 2680 | + |
|
| 2681 | + /** |
|
| 2682 | + * format result |
|
| 2683 | + * @param array $items result |
|
| 2684 | + * @param string $column is it a file share or a general share ('file_target' or 'item_target') |
|
| 2685 | + * @param \OCP\Share_Backend $backend sharing backend |
|
| 2686 | + * @param int $format |
|
| 2687 | + * @param array $parameters additional format parameters |
|
| 2688 | + * @return array format result |
|
| 2689 | + */ |
|
| 2690 | + private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) { |
|
| 2691 | + if ($format === self::FORMAT_NONE) { |
|
| 2692 | + return $items; |
|
| 2693 | + } else if ($format === self::FORMAT_STATUSES) { |
|
| 2694 | + $statuses = array(); |
|
| 2695 | + foreach ($items as $item) { |
|
| 2696 | + if ($item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 2697 | + if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) { |
|
| 2698 | + continue; |
|
| 2699 | + } |
|
| 2700 | + $statuses[$item[$column]]['link'] = true; |
|
| 2701 | + } else if (!isset($statuses[$item[$column]])) { |
|
| 2702 | + $statuses[$item[$column]]['link'] = false; |
|
| 2703 | + } |
|
| 2704 | + if (!empty($item['file_target'])) { |
|
| 2705 | + $statuses[$item[$column]]['path'] = $item['path']; |
|
| 2706 | + } |
|
| 2707 | + } |
|
| 2708 | + return $statuses; |
|
| 2709 | + } else { |
|
| 2710 | + return $backend->formatItems($items, $format, $parameters); |
|
| 2711 | + } |
|
| 2712 | + } |
|
| 2713 | + |
|
| 2714 | + /** |
|
| 2715 | + * remove protocol from URL |
|
| 2716 | + * |
|
| 2717 | + * @param string $url |
|
| 2718 | + * @return string |
|
| 2719 | + */ |
|
| 2720 | + public static function removeProtocolFromUrl($url) { |
|
| 2721 | + if (strpos($url, 'https://') === 0) { |
|
| 2722 | + return substr($url, strlen('https://')); |
|
| 2723 | + } else if (strpos($url, 'http://') === 0) { |
|
| 2724 | + return substr($url, strlen('http://')); |
|
| 2725 | + } |
|
| 2726 | + |
|
| 2727 | + return $url; |
|
| 2728 | + } |
|
| 2729 | + |
|
| 2730 | + /** |
|
| 2731 | + * try http post first with https and then with http as a fallback |
|
| 2732 | + * |
|
| 2733 | + * @param string $remoteDomain |
|
| 2734 | + * @param string $urlSuffix |
|
| 2735 | + * @param array $fields post parameters |
|
| 2736 | + * @return array |
|
| 2737 | + */ |
|
| 2738 | + private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) { |
|
| 2739 | + $protocol = 'https://'; |
|
| 2740 | + $result = [ |
|
| 2741 | + 'success' => false, |
|
| 2742 | + 'result' => '', |
|
| 2743 | + ]; |
|
| 2744 | + $try = 0; |
|
| 2745 | + $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
|
| 2746 | + while ($result['success'] === false && $try < 2) { |
|
| 2747 | + $federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING'); |
|
| 2748 | + $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
| 2749 | + $result = \OC::$server->getHTTPHelper()->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, $fields); |
|
| 2750 | + $try++; |
|
| 2751 | + $protocol = 'http://'; |
|
| 2752 | + } |
|
| 2753 | + |
|
| 2754 | + return $result; |
|
| 2755 | + } |
|
| 2756 | + |
|
| 2757 | + /** |
|
| 2758 | + * send server-to-server share to remote server |
|
| 2759 | + * |
|
| 2760 | + * @param string $token |
|
| 2761 | + * @param string $shareWith |
|
| 2762 | + * @param string $name |
|
| 2763 | + * @param int $remote_id |
|
| 2764 | + * @param string $owner |
|
| 2765 | + * @return bool |
|
| 2766 | + */ |
|
| 2767 | + private static function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner) { |
|
| 2768 | + |
|
| 2769 | + list($user, $remote) = Helper::splitUserRemote($shareWith); |
|
| 2770 | + |
|
| 2771 | + if ($user && $remote) { |
|
| 2772 | + $url = $remote; |
|
| 2773 | + |
|
| 2774 | + $local = \OC::$server->getURLGenerator()->getAbsoluteURL('/'); |
|
| 2775 | + |
|
| 2776 | + $fields = array( |
|
| 2777 | + 'shareWith' => $user, |
|
| 2778 | + 'token' => $token, |
|
| 2779 | + 'name' => $name, |
|
| 2780 | + 'remoteId' => $remote_id, |
|
| 2781 | + 'owner' => $owner, |
|
| 2782 | + 'remote' => $local, |
|
| 2783 | + ); |
|
| 2784 | + |
|
| 2785 | + $url = self::removeProtocolFromUrl($url); |
|
| 2786 | + $result = self::tryHttpPostToShareEndpoint($url, '', $fields); |
|
| 2787 | + $status = json_decode($result['result'], true); |
|
| 2788 | + |
|
| 2789 | + if ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)) { |
|
| 2790 | + \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]); |
|
| 2791 | + return true; |
|
| 2792 | + } |
|
| 2793 | + |
|
| 2794 | + } |
|
| 2795 | + |
|
| 2796 | + return false; |
|
| 2797 | + } |
|
| 2798 | + |
|
| 2799 | + /** |
|
| 2800 | + * send server-to-server unshare to remote server |
|
| 2801 | + * |
|
| 2802 | + * @param string $remote url |
|
| 2803 | + * @param int $id share id |
|
| 2804 | + * @param string $token |
|
| 2805 | + * @return bool |
|
| 2806 | + */ |
|
| 2807 | + private static function sendRemoteUnshare($remote, $id, $token) { |
|
| 2808 | + $url = rtrim($remote, '/'); |
|
| 2809 | + $fields = array('token' => $token, 'format' => 'json'); |
|
| 2810 | + $url = self::removeProtocolFromUrl($url); |
|
| 2811 | + $result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields); |
|
| 2812 | + $status = json_decode($result['result'], true); |
|
| 2813 | + |
|
| 2814 | + return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)); |
|
| 2815 | + } |
|
| 2816 | + |
|
| 2817 | + /** |
|
| 2818 | + * check if user can only share with group members |
|
| 2819 | + * @return bool |
|
| 2820 | + */ |
|
| 2821 | + public static function shareWithGroupMembersOnly() { |
|
| 2822 | + $value = \OC::$server->getAppConfig()->getValue('core', 'shareapi_only_share_with_group_members', 'no'); |
|
| 2823 | + return ($value === 'yes') ? true : false; |
|
| 2824 | + } |
|
| 2825 | + |
|
| 2826 | + /** |
|
| 2827 | + * @return bool |
|
| 2828 | + */ |
|
| 2829 | + public static function isDefaultExpireDateEnabled() { |
|
| 2830 | + $defaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no'); |
|
| 2831 | + return ($defaultExpireDateEnabled === "yes") ? true : false; |
|
| 2832 | + } |
|
| 2833 | + |
|
| 2834 | + /** |
|
| 2835 | + * @return bool |
|
| 2836 | + */ |
|
| 2837 | + public static function enforceDefaultExpireDate() { |
|
| 2838 | + $enforceDefaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no'); |
|
| 2839 | + return ($enforceDefaultExpireDate === "yes") ? true : false; |
|
| 2840 | + } |
|
| 2841 | + |
|
| 2842 | + /** |
|
| 2843 | + * @return int |
|
| 2844 | + */ |
|
| 2845 | + public static function getExpireInterval() { |
|
| 2846 | + return (int)\OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 2847 | + } |
|
| 2848 | + |
|
| 2849 | + /** |
|
| 2850 | + * Checks whether the given path is reachable for the given owner |
|
| 2851 | + * |
|
| 2852 | + * @param string $path path relative to files |
|
| 2853 | + * @param string $ownerStorageId storage id of the owner |
|
| 2854 | + * |
|
| 2855 | + * @return boolean true if file is reachable, false otherwise |
|
| 2856 | + */ |
|
| 2857 | + private static function isFileReachable($path, $ownerStorageId) { |
|
| 2858 | + // if outside the home storage, file is always considered reachable |
|
| 2859 | + if (!(substr($ownerStorageId, 0, 6) === 'home::' || |
|
| 2860 | + substr($ownerStorageId, 0, 13) === 'object::user:' |
|
| 2861 | + )) { |
|
| 2862 | + return true; |
|
| 2863 | + } |
|
| 2864 | + |
|
| 2865 | + // if inside the home storage, the file has to be under "/files/" |
|
| 2866 | + $path = ltrim($path, '/'); |
|
| 2867 | + if (substr($path, 0, 6) === 'files/') { |
|
| 2868 | + return true; |
|
| 2869 | + } |
|
| 2870 | + |
|
| 2871 | + return false; |
|
| 2872 | + } |
|
| 2873 | + |
|
| 2874 | + /** |
|
| 2875 | + * @param IConfig $config |
|
| 2876 | + * @return bool |
|
| 2877 | + */ |
|
| 2878 | + public static function enforcePassword(IConfig $config) { |
|
| 2879 | + $enforcePassword = $config->getAppValue('core', 'shareapi_enforce_links_password', 'no'); |
|
| 2880 | + return ($enforcePassword === "yes") ? true : false; |
|
| 2881 | + } |
|
| 2882 | + |
|
| 2883 | + /** |
|
| 2884 | + * Get all share entries, including non-unique group items |
|
| 2885 | + * |
|
| 2886 | + * @param string $owner |
|
| 2887 | + * @return array |
|
| 2888 | + */ |
|
| 2889 | + public static function getAllSharesForOwner($owner) { |
|
| 2890 | + $query = 'SELECT * FROM `*PREFIX*share` WHERE `uid_owner` = ?'; |
|
| 2891 | + $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$owner]); |
|
| 2892 | + return $result->fetchAll(); |
|
| 2893 | + } |
|
| 2894 | + |
|
| 2895 | + /** |
|
| 2896 | + * Get all share entries, including non-unique group items for a file |
|
| 2897 | + * |
|
| 2898 | + * @param int $id |
|
| 2899 | + * @return array |
|
| 2900 | + */ |
|
| 2901 | + public static function getAllSharesForFileId($id) { |
|
| 2902 | + $query = 'SELECT * FROM `*PREFIX*share` WHERE `file_source` = ?'; |
|
| 2903 | + $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$id]); |
|
| 2904 | + return $result->fetchAll(); |
|
| 2905 | + } |
|
| 2906 | + |
|
| 2907 | + /** |
|
| 2908 | + * @param string $password |
|
| 2909 | + * @throws \Exception |
|
| 2910 | + */ |
|
| 2911 | + private static function verifyPassword($password) { |
|
| 2912 | + |
|
| 2913 | + $accepted = true; |
|
| 2914 | + $message = ''; |
|
| 2915 | + \OCP\Util::emitHook('\OC\Share', 'verifyPassword', [ |
|
| 2916 | + 'password' => $password, |
|
| 2917 | + 'accepted' => &$accepted, |
|
| 2918 | + 'message' => &$message |
|
| 2919 | + ]); |
|
| 2920 | + |
|
| 2921 | + if (!$accepted) { |
|
| 2922 | + throw new \Exception($message); |
|
| 2923 | + } |
|
| 2924 | + } |
|
| 2925 | 2925 | } |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | public function castColumn($column, $type) { |
| 148 | 148 | if ($type === IQueryBuilder::PARAM_STR) { |
| 149 | 149 | $column = $this->helper->quoteColumnName($column); |
| 150 | - return new QueryFunction('to_char(' . $column . ')'); |
|
| 150 | + return new QueryFunction('to_char('.$column.')'); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | return parent::castColumn($column, $type); |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | * @inheritdoc |
| 158 | 158 | */ |
| 159 | 159 | public function like($x, $y, $type = null) { |
| 160 | - return parent::like($x, $y, $type) . " ESCAPE '\\'"; |
|
| 160 | + return parent::like($x, $y, $type)." ESCAPE '\\'"; |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -166,6 +166,6 @@ discard block |
||
| 166 | 166 | public function iLike($x, $y, $type = null) { |
| 167 | 167 | $x = $this->helper->quoteColumnName($x); |
| 168 | 168 | $y = $this->helper->quoteColumnName($y); |
| 169 | - return new QueryFunction('REGEXP_LIKE(' . $x . ', \'^\' || REPLACE(REPLACE(' . $y . ', \'%\', \'.*\'), \'_\', \'.\') || \'$\', \'i\')'); |
|
| 169 | + return new QueryFunction('REGEXP_LIKE('.$x.', \'^\' || REPLACE(REPLACE('.$y.', \'%\', \'.*\'), \'_\', \'.\') || \'$\', \'i\')'); |
|
| 170 | 170 | } |
| 171 | 171 | } |
@@ -33,161 +33,161 @@ |
||
| 33 | 33 | |
| 34 | 34 | class OCIExpressionBuilder extends ExpressionBuilder { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param mixed $column |
|
| 38 | - * @param mixed|null $type |
|
| 39 | - * @return array|IQueryFunction|string |
|
| 40 | - */ |
|
| 41 | - protected function prepareColumn($column, $type) { |
|
| 42 | - if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) { |
|
| 43 | - $column = $this->castColumn($column, $type); |
|
| 44 | - } else { |
|
| 45 | - $column = $this->helper->quoteColumnNames($column); |
|
| 46 | - } |
|
| 47 | - return $column; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @inheritdoc |
|
| 52 | - */ |
|
| 53 | - public function comparison($x, $operator, $y, $type = null) { |
|
| 54 | - $x = $this->prepareColumn($x, $type); |
|
| 55 | - $y = $this->prepareColumn($y, $type); |
|
| 56 | - |
|
| 57 | - return $this->expressionBuilder->comparison($x, $operator, $y); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @inheritdoc |
|
| 62 | - */ |
|
| 63 | - public function eq($x, $y, $type = null) { |
|
| 64 | - $x = $this->prepareColumn($x, $type); |
|
| 65 | - $y = $this->prepareColumn($y, $type); |
|
| 66 | - |
|
| 67 | - return $this->expressionBuilder->eq($x, $y); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @inheritdoc |
|
| 72 | - */ |
|
| 73 | - public function neq($x, $y, $type = null) { |
|
| 74 | - $x = $this->prepareColumn($x, $type); |
|
| 75 | - $y = $this->prepareColumn($y, $type); |
|
| 76 | - |
|
| 77 | - return $this->expressionBuilder->neq($x, $y); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @inheritdoc |
|
| 82 | - */ |
|
| 83 | - public function lt($x, $y, $type = null) { |
|
| 84 | - $x = $this->prepareColumn($x, $type); |
|
| 85 | - $y = $this->prepareColumn($y, $type); |
|
| 86 | - |
|
| 87 | - return $this->expressionBuilder->lt($x, $y); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @inheritdoc |
|
| 92 | - */ |
|
| 93 | - public function lte($x, $y, $type = null) { |
|
| 94 | - $x = $this->prepareColumn($x, $type); |
|
| 95 | - $y = $this->prepareColumn($y, $type); |
|
| 96 | - |
|
| 97 | - return $this->expressionBuilder->lte($x, $y); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @inheritdoc |
|
| 102 | - */ |
|
| 103 | - public function gt($x, $y, $type = null) { |
|
| 104 | - $x = $this->prepareColumn($x, $type); |
|
| 105 | - $y = $this->prepareColumn($y, $type); |
|
| 106 | - |
|
| 107 | - return $this->expressionBuilder->gt($x, $y); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @inheritdoc |
|
| 112 | - */ |
|
| 113 | - public function gte($x, $y, $type = null) { |
|
| 114 | - $x = $this->prepareColumn($x, $type); |
|
| 115 | - $y = $this->prepareColumn($y, $type); |
|
| 116 | - |
|
| 117 | - return $this->expressionBuilder->gte($x, $y); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @inheritdoc |
|
| 122 | - */ |
|
| 123 | - public function in($x, $y, $type = null) { |
|
| 124 | - $x = $this->prepareColumn($x, $type); |
|
| 125 | - $y = $this->prepareColumn($y, $type); |
|
| 126 | - |
|
| 127 | - return $this->expressionBuilder->in($x, $y); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @inheritdoc |
|
| 132 | - */ |
|
| 133 | - public function notIn($x, $y, $type = null) { |
|
| 134 | - $x = $this->prepareColumn($x, $type); |
|
| 135 | - $y = $this->prepareColumn($y, $type); |
|
| 136 | - |
|
| 137 | - return $this->expressionBuilder->notIn($x, $y); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Creates a $x = '' statement, because Oracle needs a different check |
|
| 142 | - * |
|
| 143 | - * @param string $x The field in string format to be inspected by the comparison. |
|
| 144 | - * @return string |
|
| 145 | - * @since 13.0.0 |
|
| 146 | - */ |
|
| 147 | - public function emptyString($x) { |
|
| 148 | - return $this->isNull($x); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Creates a `$x <> ''` statement, because Oracle needs a different check |
|
| 153 | - * |
|
| 154 | - * @param string $x The field in string format to be inspected by the comparison. |
|
| 155 | - * @return string |
|
| 156 | - * @since 13.0.0 |
|
| 157 | - */ |
|
| 158 | - public function nonEmptyString($x) { |
|
| 159 | - return $this->isNotNull($x); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns a IQueryFunction that casts the column to the given type |
|
| 164 | - * |
|
| 165 | - * @param string $column |
|
| 166 | - * @param mixed $type One of IQueryBuilder::PARAM_* |
|
| 167 | - * @return IQueryFunction |
|
| 168 | - */ |
|
| 169 | - public function castColumn($column, $type) { |
|
| 170 | - if ($type === IQueryBuilder::PARAM_STR) { |
|
| 171 | - $column = $this->helper->quoteColumnName($column); |
|
| 172 | - return new QueryFunction('to_char(' . $column . ')'); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - return parent::castColumn($column, $type); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @inheritdoc |
|
| 180 | - */ |
|
| 181 | - public function like($x, $y, $type = null) { |
|
| 182 | - return parent::like($x, $y, $type) . " ESCAPE '\\'"; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @inheritdoc |
|
| 187 | - */ |
|
| 188 | - public function iLike($x, $y, $type = null) { |
|
| 189 | - $x = $this->helper->quoteColumnName($x); |
|
| 190 | - $y = $this->helper->quoteColumnName($y); |
|
| 191 | - return new QueryFunction('REGEXP_LIKE(' . $x . ', \'^\' || REPLACE(REPLACE(' . $y . ', \'%\', \'.*\'), \'_\', \'.\') || \'$\', \'i\')'); |
|
| 192 | - } |
|
| 36 | + /** |
|
| 37 | + * @param mixed $column |
|
| 38 | + * @param mixed|null $type |
|
| 39 | + * @return array|IQueryFunction|string |
|
| 40 | + */ |
|
| 41 | + protected function prepareColumn($column, $type) { |
|
| 42 | + if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) { |
|
| 43 | + $column = $this->castColumn($column, $type); |
|
| 44 | + } else { |
|
| 45 | + $column = $this->helper->quoteColumnNames($column); |
|
| 46 | + } |
|
| 47 | + return $column; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @inheritdoc |
|
| 52 | + */ |
|
| 53 | + public function comparison($x, $operator, $y, $type = null) { |
|
| 54 | + $x = $this->prepareColumn($x, $type); |
|
| 55 | + $y = $this->prepareColumn($y, $type); |
|
| 56 | + |
|
| 57 | + return $this->expressionBuilder->comparison($x, $operator, $y); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @inheritdoc |
|
| 62 | + */ |
|
| 63 | + public function eq($x, $y, $type = null) { |
|
| 64 | + $x = $this->prepareColumn($x, $type); |
|
| 65 | + $y = $this->prepareColumn($y, $type); |
|
| 66 | + |
|
| 67 | + return $this->expressionBuilder->eq($x, $y); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @inheritdoc |
|
| 72 | + */ |
|
| 73 | + public function neq($x, $y, $type = null) { |
|
| 74 | + $x = $this->prepareColumn($x, $type); |
|
| 75 | + $y = $this->prepareColumn($y, $type); |
|
| 76 | + |
|
| 77 | + return $this->expressionBuilder->neq($x, $y); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @inheritdoc |
|
| 82 | + */ |
|
| 83 | + public function lt($x, $y, $type = null) { |
|
| 84 | + $x = $this->prepareColumn($x, $type); |
|
| 85 | + $y = $this->prepareColumn($y, $type); |
|
| 86 | + |
|
| 87 | + return $this->expressionBuilder->lt($x, $y); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @inheritdoc |
|
| 92 | + */ |
|
| 93 | + public function lte($x, $y, $type = null) { |
|
| 94 | + $x = $this->prepareColumn($x, $type); |
|
| 95 | + $y = $this->prepareColumn($y, $type); |
|
| 96 | + |
|
| 97 | + return $this->expressionBuilder->lte($x, $y); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @inheritdoc |
|
| 102 | + */ |
|
| 103 | + public function gt($x, $y, $type = null) { |
|
| 104 | + $x = $this->prepareColumn($x, $type); |
|
| 105 | + $y = $this->prepareColumn($y, $type); |
|
| 106 | + |
|
| 107 | + return $this->expressionBuilder->gt($x, $y); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @inheritdoc |
|
| 112 | + */ |
|
| 113 | + public function gte($x, $y, $type = null) { |
|
| 114 | + $x = $this->prepareColumn($x, $type); |
|
| 115 | + $y = $this->prepareColumn($y, $type); |
|
| 116 | + |
|
| 117 | + return $this->expressionBuilder->gte($x, $y); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @inheritdoc |
|
| 122 | + */ |
|
| 123 | + public function in($x, $y, $type = null) { |
|
| 124 | + $x = $this->prepareColumn($x, $type); |
|
| 125 | + $y = $this->prepareColumn($y, $type); |
|
| 126 | + |
|
| 127 | + return $this->expressionBuilder->in($x, $y); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @inheritdoc |
|
| 132 | + */ |
|
| 133 | + public function notIn($x, $y, $type = null) { |
|
| 134 | + $x = $this->prepareColumn($x, $type); |
|
| 135 | + $y = $this->prepareColumn($y, $type); |
|
| 136 | + |
|
| 137 | + return $this->expressionBuilder->notIn($x, $y); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Creates a $x = '' statement, because Oracle needs a different check |
|
| 142 | + * |
|
| 143 | + * @param string $x The field in string format to be inspected by the comparison. |
|
| 144 | + * @return string |
|
| 145 | + * @since 13.0.0 |
|
| 146 | + */ |
|
| 147 | + public function emptyString($x) { |
|
| 148 | + return $this->isNull($x); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Creates a `$x <> ''` statement, because Oracle needs a different check |
|
| 153 | + * |
|
| 154 | + * @param string $x The field in string format to be inspected by the comparison. |
|
| 155 | + * @return string |
|
| 156 | + * @since 13.0.0 |
|
| 157 | + */ |
|
| 158 | + public function nonEmptyString($x) { |
|
| 159 | + return $this->isNotNull($x); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns a IQueryFunction that casts the column to the given type |
|
| 164 | + * |
|
| 165 | + * @param string $column |
|
| 166 | + * @param mixed $type One of IQueryBuilder::PARAM_* |
|
| 167 | + * @return IQueryFunction |
|
| 168 | + */ |
|
| 169 | + public function castColumn($column, $type) { |
|
| 170 | + if ($type === IQueryBuilder::PARAM_STR) { |
|
| 171 | + $column = $this->helper->quoteColumnName($column); |
|
| 172 | + return new QueryFunction('to_char(' . $column . ')'); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + return parent::castColumn($column, $type); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @inheritdoc |
|
| 180 | + */ |
|
| 181 | + public function like($x, $y, $type = null) { |
|
| 182 | + return parent::like($x, $y, $type) . " ESCAPE '\\'"; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @inheritdoc |
|
| 187 | + */ |
|
| 188 | + public function iLike($x, $y, $type = null) { |
|
| 189 | + $x = $this->helper->quoteColumnName($x); |
|
| 190 | + $y = $this->helper->quoteColumnName($y); |
|
| 191 | + return new QueryFunction('REGEXP_LIKE(' . $x . ', \'^\' || REPLACE(REPLACE(' . $y . ', \'%\', \'.*\'), \'_\', \'.\') || \'$\', \'i\')'); |
|
| 192 | + } |
|
| 193 | 193 | } |