@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | * If an SQLLogger is configured, the execution is logged. |
174 | 174 | * |
175 | 175 | * @param string $query The SQL query to execute. |
176 | - * @param array $params The parameters to bind to the query, if any. |
|
176 | + * @param string[] $params The parameters to bind to the query, if any. |
|
177 | 177 | * @param array $types The types the previous parameters are in. |
178 | 178 | * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
179 | 179 | * |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | * columns or sequences. |
219 | 219 | * |
220 | 220 | * @param string $seqName Name of the sequence object from which the ID should be returned. |
221 | - * @return string A string representation of the last inserted ID. |
|
221 | + * @return integer A string representation of the last inserted ID. |
|
222 | 222 | */ |
223 | 223 | public function lastInsertId($seqName = null) { |
224 | 224 | if ($seqName) { |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | return parent::connect(); |
59 | 59 | } catch (DBALException $e) { |
60 | 60 | // throw a new exception to prevent leaking info from the stacktrace |
61 | - throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
61 | + throw new DBALException('Failed to connect to the database: '.$e->getMessage(), $e->getCode()); |
|
62 | 62 | } |
63 | 63 | } |
64 | 64 | |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | // 0 is the method where we use `getCallerBacktrace` |
111 | 111 | // 1 is the target method which uses the method we want to log |
112 | 112 | if (isset($traces[1])) { |
113 | - return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
113 | + return $traces[1]['file'].':'.$traces[1]['line']; |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | return ''; |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | * @param int $offset |
157 | 157 | * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
158 | 158 | */ |
159 | - public function prepare( $statement, $limit=null, $offset=null ) { |
|
159 | + public function prepare($statement, $limit = null, $offset = null) { |
|
160 | 160 | if ($limit === -1) { |
161 | 161 | $limit = null; |
162 | 162 | } |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
322 | 322 | } |
323 | 323 | |
324 | - $tableName = $this->tablePrefix . $tableName; |
|
324 | + $tableName = $this->tablePrefix.$tableName; |
|
325 | 325 | $this->lockedTable = $tableName; |
326 | 326 | $this->adapter->lockTable($tableName); |
327 | 327 | } |
@@ -342,11 +342,11 @@ discard block |
||
342 | 342 | * @return string |
343 | 343 | */ |
344 | 344 | public function getError() { |
345 | - $msg = $this->errorCode() . ': '; |
|
345 | + $msg = $this->errorCode().': '; |
|
346 | 346 | $errorInfo = $this->errorInfo(); |
347 | 347 | if (is_array($errorInfo)) { |
348 | - $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
349 | - $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
348 | + $msg .= 'SQLSTATE = '.$errorInfo[0].', '; |
|
349 | + $msg .= 'Driver Code = '.$errorInfo[1].', '; |
|
350 | 350 | $msg .= 'Driver Message = '.$errorInfo[2]; |
351 | 351 | } |
352 | 352 | return $msg; |
@@ -358,9 +358,9 @@ discard block |
||
358 | 358 | * @param string $table table name without the prefix |
359 | 359 | */ |
360 | 360 | public function dropTable($table) { |
361 | - $table = $this->tablePrefix . trim($table); |
|
361 | + $table = $this->tablePrefix.trim($table); |
|
362 | 362 | $schema = $this->getSchemaManager(); |
363 | - if($schema->tablesExist(array($table))) { |
|
363 | + if ($schema->tablesExist(array($table))) { |
|
364 | 364 | $schema->dropTable($table); |
365 | 365 | } |
366 | 366 | } |
@@ -371,8 +371,8 @@ discard block |
||
371 | 371 | * @param string $table table name without the prefix |
372 | 372 | * @return bool |
373 | 373 | */ |
374 | - public function tableExists($table){ |
|
375 | - $table = $this->tablePrefix . trim($table); |
|
374 | + public function tableExists($table) { |
|
375 | + $table = $this->tablePrefix.trim($table); |
|
376 | 376 | $schema = $this->getSchemaManager(); |
377 | 377 | return $schema->tablesExist(array($table)); |
378 | 378 | } |
@@ -383,7 +383,7 @@ discard block |
||
383 | 383 | * @return string |
384 | 384 | */ |
385 | 385 | protected function replaceTablePrefix($statement) { |
386 | - return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
386 | + return str_replace('*PREFIX*', $this->tablePrefix, $statement); |
|
387 | 387 | } |
388 | 388 | |
389 | 389 | /** |
@@ -42,404 +42,404 @@ |
||
42 | 42 | use OCP\PreConditionNotMetException; |
43 | 43 | |
44 | 44 | class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { |
45 | - /** |
|
46 | - * @var string $tablePrefix |
|
47 | - */ |
|
48 | - protected $tablePrefix; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var \OC\DB\Adapter $adapter |
|
52 | - */ |
|
53 | - protected $adapter; |
|
54 | - |
|
55 | - protected $lockedTable = null; |
|
56 | - |
|
57 | - public function connect() { |
|
58 | - try { |
|
59 | - return parent::connect(); |
|
60 | - } catch (DBALException $e) { |
|
61 | - // throw a new exception to prevent leaking info from the stacktrace |
|
62 | - throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
63 | - } |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Returns a QueryBuilder for the connection. |
|
68 | - * |
|
69 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
70 | - */ |
|
71 | - public function getQueryBuilder() { |
|
72 | - return new QueryBuilder( |
|
73 | - $this, |
|
74 | - \OC::$server->getSystemConfig(), |
|
75 | - \OC::$server->getLogger() |
|
76 | - ); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Gets the QueryBuilder for the connection. |
|
81 | - * |
|
82 | - * @return \Doctrine\DBAL\Query\QueryBuilder |
|
83 | - * @deprecated please use $this->getQueryBuilder() instead |
|
84 | - */ |
|
85 | - public function createQueryBuilder() { |
|
86 | - $backtrace = $this->getCallerBacktrace(); |
|
87 | - \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
88 | - return parent::createQueryBuilder(); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Gets the ExpressionBuilder for the connection. |
|
93 | - * |
|
94 | - * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
95 | - * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
96 | - */ |
|
97 | - public function getExpressionBuilder() { |
|
98 | - $backtrace = $this->getCallerBacktrace(); |
|
99 | - \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
100 | - return parent::getExpressionBuilder(); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
105 | - * |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - protected function getCallerBacktrace() { |
|
109 | - $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
110 | - |
|
111 | - // 0 is the method where we use `getCallerBacktrace` |
|
112 | - // 1 is the target method which uses the method we want to log |
|
113 | - if (isset($traces[1])) { |
|
114 | - return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
115 | - } |
|
116 | - |
|
117 | - return ''; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - public function getPrefix() { |
|
124 | - return $this->tablePrefix; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Initializes a new instance of the Connection class. |
|
129 | - * |
|
130 | - * @param array $params The connection parameters. |
|
131 | - * @param \Doctrine\DBAL\Driver $driver |
|
132 | - * @param \Doctrine\DBAL\Configuration $config |
|
133 | - * @param \Doctrine\Common\EventManager $eventManager |
|
134 | - * @throws \Exception |
|
135 | - */ |
|
136 | - public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
137 | - EventManager $eventManager = null) |
|
138 | - { |
|
139 | - if (!isset($params['adapter'])) { |
|
140 | - throw new \Exception('adapter not set'); |
|
141 | - } |
|
142 | - if (!isset($params['tablePrefix'])) { |
|
143 | - throw new \Exception('tablePrefix not set'); |
|
144 | - } |
|
145 | - parent::__construct($params, $driver, $config, $eventManager); |
|
146 | - $this->adapter = new $params['adapter']($this); |
|
147 | - $this->tablePrefix = $params['tablePrefix']; |
|
148 | - |
|
149 | - parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Prepares an SQL statement. |
|
154 | - * |
|
155 | - * @param string $statement The SQL statement to prepare. |
|
156 | - * @param int $limit |
|
157 | - * @param int $offset |
|
158 | - * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
159 | - */ |
|
160 | - public function prepare( $statement, $limit=null, $offset=null ) { |
|
161 | - if ($limit === -1) { |
|
162 | - $limit = null; |
|
163 | - } |
|
164 | - if (!is_null($limit)) { |
|
165 | - $platform = $this->getDatabasePlatform(); |
|
166 | - $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
167 | - } |
|
168 | - $statement = $this->replaceTablePrefix($statement); |
|
169 | - $statement = $this->adapter->fixupStatement($statement); |
|
170 | - |
|
171 | - return parent::prepare($statement); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Executes an, optionally parametrized, SQL query. |
|
176 | - * |
|
177 | - * If the query is parametrized, a prepared statement is used. |
|
178 | - * If an SQLLogger is configured, the execution is logged. |
|
179 | - * |
|
180 | - * @param string $query The SQL query to execute. |
|
181 | - * @param array $params The parameters to bind to the query, if any. |
|
182 | - * @param array $types The types the previous parameters are in. |
|
183 | - * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
184 | - * |
|
185 | - * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
186 | - * |
|
187 | - * @throws \Doctrine\DBAL\DBALException |
|
188 | - */ |
|
189 | - public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
190 | - { |
|
191 | - $query = $this->replaceTablePrefix($query); |
|
192 | - $query = $this->adapter->fixupStatement($query); |
|
193 | - return parent::executeQuery($query, $params, $types, $qcp); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
198 | - * and returns the number of affected rows. |
|
199 | - * |
|
200 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
201 | - * |
|
202 | - * @param string $query The SQL query. |
|
203 | - * @param array $params The query parameters. |
|
204 | - * @param array $types The parameter types. |
|
205 | - * |
|
206 | - * @return integer The number of affected rows. |
|
207 | - * |
|
208 | - * @throws \Doctrine\DBAL\DBALException |
|
209 | - */ |
|
210 | - public function executeUpdate($query, array $params = array(), array $types = array()) |
|
211 | - { |
|
212 | - $query = $this->replaceTablePrefix($query); |
|
213 | - $query = $this->adapter->fixupStatement($query); |
|
214 | - return parent::executeUpdate($query, $params, $types); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
219 | - * depending on the underlying driver. |
|
220 | - * |
|
221 | - * Note: This method may not return a meaningful or consistent result across different drivers, |
|
222 | - * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
223 | - * columns or sequences. |
|
224 | - * |
|
225 | - * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
226 | - * @return string A string representation of the last inserted ID. |
|
227 | - */ |
|
228 | - public function lastInsertId($seqName = null) { |
|
229 | - if ($seqName) { |
|
230 | - $seqName = $this->replaceTablePrefix($seqName); |
|
231 | - } |
|
232 | - return $this->adapter->lastInsertId($seqName); |
|
233 | - } |
|
234 | - |
|
235 | - // internal use |
|
236 | - public function realLastInsertId($seqName = null) { |
|
237 | - return parent::lastInsertId($seqName); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Insert a row if the matching row does not exists. |
|
242 | - * |
|
243 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
244 | - * @param array $input data that should be inserted into the table (column name => value) |
|
245 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
246 | - * If this is null or an empty array, all keys of $input will be compared |
|
247 | - * Please note: text fields (clob) must not be used in the compare array |
|
248 | - * @return int number of inserted rows |
|
249 | - * @throws \Doctrine\DBAL\DBALException |
|
250 | - */ |
|
251 | - public function insertIfNotExist($table, $input, array $compare = null) { |
|
252 | - return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
253 | - } |
|
254 | - |
|
255 | - private function getType($value) { |
|
256 | - if (is_bool($value)) { |
|
257 | - return IQueryBuilder::PARAM_BOOL; |
|
258 | - } else if (is_int($value)) { |
|
259 | - return IQueryBuilder::PARAM_INT; |
|
260 | - } else { |
|
261 | - return IQueryBuilder::PARAM_STR; |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Insert or update a row value |
|
267 | - * |
|
268 | - * @param string $table |
|
269 | - * @param array $keys (column name => value) |
|
270 | - * @param array $values (column name => value) |
|
271 | - * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
272 | - * @return int number of new rows |
|
273 | - * @throws \Doctrine\DBAL\DBALException |
|
274 | - * @throws PreConditionNotMetException |
|
275 | - */ |
|
276 | - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
277 | - try { |
|
278 | - $insertQb = $this->getQueryBuilder(); |
|
279 | - $insertQb->insert($table) |
|
280 | - ->values( |
|
281 | - array_map(function($value) use ($insertQb) { |
|
282 | - return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
283 | - }, array_merge($keys, $values)) |
|
284 | - ); |
|
285 | - return $insertQb->execute(); |
|
286 | - } catch (ConstraintViolationException $e) { |
|
287 | - // value already exists, try update |
|
288 | - $updateQb = $this->getQueryBuilder(); |
|
289 | - $updateQb->update($table); |
|
290 | - foreach ($values as $name => $value) { |
|
291 | - $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
292 | - } |
|
293 | - $where = $updateQb->expr()->andX(); |
|
294 | - $whereValues = array_merge($keys, $updatePreconditionValues); |
|
295 | - foreach ($whereValues as $name => $value) { |
|
296 | - $where->add($updateQb->expr()->eq( |
|
297 | - $name, |
|
298 | - $updateQb->createNamedParameter($value, $this->getType($value)), |
|
299 | - $this->getType($value) |
|
300 | - )); |
|
301 | - } |
|
302 | - $updateQb->where($where); |
|
303 | - $affected = $updateQb->execute(); |
|
304 | - |
|
305 | - if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
306 | - throw new PreConditionNotMetException(); |
|
307 | - } |
|
308 | - |
|
309 | - return 0; |
|
310 | - } |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Create an exclusive read+write lock on a table |
|
315 | - * |
|
316 | - * @param string $tableName |
|
317 | - * @throws \BadMethodCallException When trying to acquire a second lock |
|
318 | - * @since 9.1.0 |
|
319 | - */ |
|
320 | - public function lockTable($tableName) { |
|
321 | - if ($this->lockedTable !== null) { |
|
322 | - throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
323 | - } |
|
324 | - |
|
325 | - $tableName = $this->tablePrefix . $tableName; |
|
326 | - $this->lockedTable = $tableName; |
|
327 | - $this->adapter->lockTable($tableName); |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Release a previous acquired lock again |
|
332 | - * |
|
333 | - * @since 9.1.0 |
|
334 | - */ |
|
335 | - public function unlockTable() { |
|
336 | - $this->adapter->unlockTable(); |
|
337 | - $this->lockedTable = null; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * returns the error code and message as a string for logging |
|
342 | - * works with DoctrineException |
|
343 | - * @return string |
|
344 | - */ |
|
345 | - public function getError() { |
|
346 | - $msg = $this->errorCode() . ': '; |
|
347 | - $errorInfo = $this->errorInfo(); |
|
348 | - if (is_array($errorInfo)) { |
|
349 | - $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
350 | - $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
351 | - $msg .= 'Driver Message = '.$errorInfo[2]; |
|
352 | - } |
|
353 | - return $msg; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Drop a table from the database if it exists |
|
358 | - * |
|
359 | - * @param string $table table name without the prefix |
|
360 | - */ |
|
361 | - public function dropTable($table) { |
|
362 | - $table = $this->tablePrefix . trim($table); |
|
363 | - $schema = $this->getSchemaManager(); |
|
364 | - if($schema->tablesExist(array($table))) { |
|
365 | - $schema->dropTable($table); |
|
366 | - } |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * Check if a table exists |
|
371 | - * |
|
372 | - * @param string $table table name without the prefix |
|
373 | - * @return bool |
|
374 | - */ |
|
375 | - public function tableExists($table){ |
|
376 | - $table = $this->tablePrefix . trim($table); |
|
377 | - $schema = $this->getSchemaManager(); |
|
378 | - return $schema->tablesExist(array($table)); |
|
379 | - } |
|
380 | - |
|
381 | - // internal use |
|
382 | - /** |
|
383 | - * @param string $statement |
|
384 | - * @return string |
|
385 | - */ |
|
386 | - protected function replaceTablePrefix($statement) { |
|
387 | - return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Check if a transaction is active |
|
392 | - * |
|
393 | - * @return bool |
|
394 | - * @since 8.2.0 |
|
395 | - */ |
|
396 | - public function inTransaction() { |
|
397 | - return $this->getTransactionNestingLevel() > 0; |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Espace a parameter to be used in a LIKE query |
|
402 | - * |
|
403 | - * @param string $param |
|
404 | - * @return string |
|
405 | - */ |
|
406 | - public function escapeLikeParameter($param) { |
|
407 | - return addcslashes($param, '\\_%'); |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Check whether or not the current database support 4byte wide unicode |
|
412 | - * |
|
413 | - * @return bool |
|
414 | - * @since 11.0.0 |
|
415 | - */ |
|
416 | - public function supports4ByteText() { |
|
417 | - if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
418 | - return true; |
|
419 | - } |
|
420 | - return $this->getParams()['charset'] === 'utf8mb4'; |
|
421 | - } |
|
422 | - |
|
423 | - |
|
424 | - /** |
|
425 | - * Create the schema of the connected database |
|
426 | - * |
|
427 | - * @return Schema |
|
428 | - */ |
|
429 | - public function createSchema() { |
|
430 | - $schemaManager = new MDB2SchemaManager($this); |
|
431 | - $migrator = $schemaManager->getMigrator(); |
|
432 | - return $migrator->createSchema(); |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Migrate the database to the given schema |
|
437 | - * |
|
438 | - * @param Schema $toSchema |
|
439 | - */ |
|
440 | - public function migrateToSchema(Schema $toSchema) { |
|
441 | - $schemaManager = new MDB2SchemaManager($this); |
|
442 | - $migrator = $schemaManager->getMigrator(); |
|
443 | - $migrator->migrate($toSchema); |
|
444 | - } |
|
45 | + /** |
|
46 | + * @var string $tablePrefix |
|
47 | + */ |
|
48 | + protected $tablePrefix; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var \OC\DB\Adapter $adapter |
|
52 | + */ |
|
53 | + protected $adapter; |
|
54 | + |
|
55 | + protected $lockedTable = null; |
|
56 | + |
|
57 | + public function connect() { |
|
58 | + try { |
|
59 | + return parent::connect(); |
|
60 | + } catch (DBALException $e) { |
|
61 | + // throw a new exception to prevent leaking info from the stacktrace |
|
62 | + throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
63 | + } |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Returns a QueryBuilder for the connection. |
|
68 | + * |
|
69 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
70 | + */ |
|
71 | + public function getQueryBuilder() { |
|
72 | + return new QueryBuilder( |
|
73 | + $this, |
|
74 | + \OC::$server->getSystemConfig(), |
|
75 | + \OC::$server->getLogger() |
|
76 | + ); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Gets the QueryBuilder for the connection. |
|
81 | + * |
|
82 | + * @return \Doctrine\DBAL\Query\QueryBuilder |
|
83 | + * @deprecated please use $this->getQueryBuilder() instead |
|
84 | + */ |
|
85 | + public function createQueryBuilder() { |
|
86 | + $backtrace = $this->getCallerBacktrace(); |
|
87 | + \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
88 | + return parent::createQueryBuilder(); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Gets the ExpressionBuilder for the connection. |
|
93 | + * |
|
94 | + * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
95 | + * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
96 | + */ |
|
97 | + public function getExpressionBuilder() { |
|
98 | + $backtrace = $this->getCallerBacktrace(); |
|
99 | + \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
100 | + return parent::getExpressionBuilder(); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
105 | + * |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + protected function getCallerBacktrace() { |
|
109 | + $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
110 | + |
|
111 | + // 0 is the method where we use `getCallerBacktrace` |
|
112 | + // 1 is the target method which uses the method we want to log |
|
113 | + if (isset($traces[1])) { |
|
114 | + return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
115 | + } |
|
116 | + |
|
117 | + return ''; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + public function getPrefix() { |
|
124 | + return $this->tablePrefix; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Initializes a new instance of the Connection class. |
|
129 | + * |
|
130 | + * @param array $params The connection parameters. |
|
131 | + * @param \Doctrine\DBAL\Driver $driver |
|
132 | + * @param \Doctrine\DBAL\Configuration $config |
|
133 | + * @param \Doctrine\Common\EventManager $eventManager |
|
134 | + * @throws \Exception |
|
135 | + */ |
|
136 | + public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
137 | + EventManager $eventManager = null) |
|
138 | + { |
|
139 | + if (!isset($params['adapter'])) { |
|
140 | + throw new \Exception('adapter not set'); |
|
141 | + } |
|
142 | + if (!isset($params['tablePrefix'])) { |
|
143 | + throw new \Exception('tablePrefix not set'); |
|
144 | + } |
|
145 | + parent::__construct($params, $driver, $config, $eventManager); |
|
146 | + $this->adapter = new $params['adapter']($this); |
|
147 | + $this->tablePrefix = $params['tablePrefix']; |
|
148 | + |
|
149 | + parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Prepares an SQL statement. |
|
154 | + * |
|
155 | + * @param string $statement The SQL statement to prepare. |
|
156 | + * @param int $limit |
|
157 | + * @param int $offset |
|
158 | + * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
159 | + */ |
|
160 | + public function prepare( $statement, $limit=null, $offset=null ) { |
|
161 | + if ($limit === -1) { |
|
162 | + $limit = null; |
|
163 | + } |
|
164 | + if (!is_null($limit)) { |
|
165 | + $platform = $this->getDatabasePlatform(); |
|
166 | + $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
167 | + } |
|
168 | + $statement = $this->replaceTablePrefix($statement); |
|
169 | + $statement = $this->adapter->fixupStatement($statement); |
|
170 | + |
|
171 | + return parent::prepare($statement); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Executes an, optionally parametrized, SQL query. |
|
176 | + * |
|
177 | + * If the query is parametrized, a prepared statement is used. |
|
178 | + * If an SQLLogger is configured, the execution is logged. |
|
179 | + * |
|
180 | + * @param string $query The SQL query to execute. |
|
181 | + * @param array $params The parameters to bind to the query, if any. |
|
182 | + * @param array $types The types the previous parameters are in. |
|
183 | + * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
184 | + * |
|
185 | + * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
186 | + * |
|
187 | + * @throws \Doctrine\DBAL\DBALException |
|
188 | + */ |
|
189 | + public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
190 | + { |
|
191 | + $query = $this->replaceTablePrefix($query); |
|
192 | + $query = $this->adapter->fixupStatement($query); |
|
193 | + return parent::executeQuery($query, $params, $types, $qcp); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
198 | + * and returns the number of affected rows. |
|
199 | + * |
|
200 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
201 | + * |
|
202 | + * @param string $query The SQL query. |
|
203 | + * @param array $params The query parameters. |
|
204 | + * @param array $types The parameter types. |
|
205 | + * |
|
206 | + * @return integer The number of affected rows. |
|
207 | + * |
|
208 | + * @throws \Doctrine\DBAL\DBALException |
|
209 | + */ |
|
210 | + public function executeUpdate($query, array $params = array(), array $types = array()) |
|
211 | + { |
|
212 | + $query = $this->replaceTablePrefix($query); |
|
213 | + $query = $this->adapter->fixupStatement($query); |
|
214 | + return parent::executeUpdate($query, $params, $types); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
219 | + * depending on the underlying driver. |
|
220 | + * |
|
221 | + * Note: This method may not return a meaningful or consistent result across different drivers, |
|
222 | + * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
223 | + * columns or sequences. |
|
224 | + * |
|
225 | + * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
226 | + * @return string A string representation of the last inserted ID. |
|
227 | + */ |
|
228 | + public function lastInsertId($seqName = null) { |
|
229 | + if ($seqName) { |
|
230 | + $seqName = $this->replaceTablePrefix($seqName); |
|
231 | + } |
|
232 | + return $this->adapter->lastInsertId($seqName); |
|
233 | + } |
|
234 | + |
|
235 | + // internal use |
|
236 | + public function realLastInsertId($seqName = null) { |
|
237 | + return parent::lastInsertId($seqName); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Insert a row if the matching row does not exists. |
|
242 | + * |
|
243 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
244 | + * @param array $input data that should be inserted into the table (column name => value) |
|
245 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
246 | + * If this is null or an empty array, all keys of $input will be compared |
|
247 | + * Please note: text fields (clob) must not be used in the compare array |
|
248 | + * @return int number of inserted rows |
|
249 | + * @throws \Doctrine\DBAL\DBALException |
|
250 | + */ |
|
251 | + public function insertIfNotExist($table, $input, array $compare = null) { |
|
252 | + return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
253 | + } |
|
254 | + |
|
255 | + private function getType($value) { |
|
256 | + if (is_bool($value)) { |
|
257 | + return IQueryBuilder::PARAM_BOOL; |
|
258 | + } else if (is_int($value)) { |
|
259 | + return IQueryBuilder::PARAM_INT; |
|
260 | + } else { |
|
261 | + return IQueryBuilder::PARAM_STR; |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Insert or update a row value |
|
267 | + * |
|
268 | + * @param string $table |
|
269 | + * @param array $keys (column name => value) |
|
270 | + * @param array $values (column name => value) |
|
271 | + * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
272 | + * @return int number of new rows |
|
273 | + * @throws \Doctrine\DBAL\DBALException |
|
274 | + * @throws PreConditionNotMetException |
|
275 | + */ |
|
276 | + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
277 | + try { |
|
278 | + $insertQb = $this->getQueryBuilder(); |
|
279 | + $insertQb->insert($table) |
|
280 | + ->values( |
|
281 | + array_map(function($value) use ($insertQb) { |
|
282 | + return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
283 | + }, array_merge($keys, $values)) |
|
284 | + ); |
|
285 | + return $insertQb->execute(); |
|
286 | + } catch (ConstraintViolationException $e) { |
|
287 | + // value already exists, try update |
|
288 | + $updateQb = $this->getQueryBuilder(); |
|
289 | + $updateQb->update($table); |
|
290 | + foreach ($values as $name => $value) { |
|
291 | + $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
292 | + } |
|
293 | + $where = $updateQb->expr()->andX(); |
|
294 | + $whereValues = array_merge($keys, $updatePreconditionValues); |
|
295 | + foreach ($whereValues as $name => $value) { |
|
296 | + $where->add($updateQb->expr()->eq( |
|
297 | + $name, |
|
298 | + $updateQb->createNamedParameter($value, $this->getType($value)), |
|
299 | + $this->getType($value) |
|
300 | + )); |
|
301 | + } |
|
302 | + $updateQb->where($where); |
|
303 | + $affected = $updateQb->execute(); |
|
304 | + |
|
305 | + if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
306 | + throw new PreConditionNotMetException(); |
|
307 | + } |
|
308 | + |
|
309 | + return 0; |
|
310 | + } |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Create an exclusive read+write lock on a table |
|
315 | + * |
|
316 | + * @param string $tableName |
|
317 | + * @throws \BadMethodCallException When trying to acquire a second lock |
|
318 | + * @since 9.1.0 |
|
319 | + */ |
|
320 | + public function lockTable($tableName) { |
|
321 | + if ($this->lockedTable !== null) { |
|
322 | + throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
323 | + } |
|
324 | + |
|
325 | + $tableName = $this->tablePrefix . $tableName; |
|
326 | + $this->lockedTable = $tableName; |
|
327 | + $this->adapter->lockTable($tableName); |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Release a previous acquired lock again |
|
332 | + * |
|
333 | + * @since 9.1.0 |
|
334 | + */ |
|
335 | + public function unlockTable() { |
|
336 | + $this->adapter->unlockTable(); |
|
337 | + $this->lockedTable = null; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * returns the error code and message as a string for logging |
|
342 | + * works with DoctrineException |
|
343 | + * @return string |
|
344 | + */ |
|
345 | + public function getError() { |
|
346 | + $msg = $this->errorCode() . ': '; |
|
347 | + $errorInfo = $this->errorInfo(); |
|
348 | + if (is_array($errorInfo)) { |
|
349 | + $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
350 | + $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
351 | + $msg .= 'Driver Message = '.$errorInfo[2]; |
|
352 | + } |
|
353 | + return $msg; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Drop a table from the database if it exists |
|
358 | + * |
|
359 | + * @param string $table table name without the prefix |
|
360 | + */ |
|
361 | + public function dropTable($table) { |
|
362 | + $table = $this->tablePrefix . trim($table); |
|
363 | + $schema = $this->getSchemaManager(); |
|
364 | + if($schema->tablesExist(array($table))) { |
|
365 | + $schema->dropTable($table); |
|
366 | + } |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Check if a table exists |
|
371 | + * |
|
372 | + * @param string $table table name without the prefix |
|
373 | + * @return bool |
|
374 | + */ |
|
375 | + public function tableExists($table){ |
|
376 | + $table = $this->tablePrefix . trim($table); |
|
377 | + $schema = $this->getSchemaManager(); |
|
378 | + return $schema->tablesExist(array($table)); |
|
379 | + } |
|
380 | + |
|
381 | + // internal use |
|
382 | + /** |
|
383 | + * @param string $statement |
|
384 | + * @return string |
|
385 | + */ |
|
386 | + protected function replaceTablePrefix($statement) { |
|
387 | + return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Check if a transaction is active |
|
392 | + * |
|
393 | + * @return bool |
|
394 | + * @since 8.2.0 |
|
395 | + */ |
|
396 | + public function inTransaction() { |
|
397 | + return $this->getTransactionNestingLevel() > 0; |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Espace a parameter to be used in a LIKE query |
|
402 | + * |
|
403 | + * @param string $param |
|
404 | + * @return string |
|
405 | + */ |
|
406 | + public function escapeLikeParameter($param) { |
|
407 | + return addcslashes($param, '\\_%'); |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Check whether or not the current database support 4byte wide unicode |
|
412 | + * |
|
413 | + * @return bool |
|
414 | + * @since 11.0.0 |
|
415 | + */ |
|
416 | + public function supports4ByteText() { |
|
417 | + if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
418 | + return true; |
|
419 | + } |
|
420 | + return $this->getParams()['charset'] === 'utf8mb4'; |
|
421 | + } |
|
422 | + |
|
423 | + |
|
424 | + /** |
|
425 | + * Create the schema of the connected database |
|
426 | + * |
|
427 | + * @return Schema |
|
428 | + */ |
|
429 | + public function createSchema() { |
|
430 | + $schemaManager = new MDB2SchemaManager($this); |
|
431 | + $migrator = $schemaManager->getMigrator(); |
|
432 | + return $migrator->createSchema(); |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Migrate the database to the given schema |
|
437 | + * |
|
438 | + * @param Schema $toSchema |
|
439 | + */ |
|
440 | + public function migrateToSchema(Schema $toSchema) { |
|
441 | + $schemaManager = new MDB2SchemaManager($this); |
|
442 | + $migrator = $schemaManager->getMigrator(); |
|
443 | + $migrator->migrate($toSchema); |
|
444 | + } |
|
445 | 445 | } |
@@ -386,6 +386,14 @@ discard block |
||
386 | 386 | return $size; |
387 | 387 | } |
388 | 388 | |
389 | + /** |
|
390 | + * @param string $path |
|
391 | + * @param boolean $recursive |
|
392 | + * @param integer $reuse |
|
393 | + * @param integer|null $folderId |
|
394 | + * @param boolean $lock |
|
395 | + * @param integer $size |
|
396 | + */ |
|
389 | 397 | private function handleChildren($path, $recursive, $reuse, $folderId, $lock, &$size) { |
390 | 398 | // we put this in it's own function so it cleans up the memory before we start recursing |
391 | 399 | $existingChildren = $this->getExistingChildren($folderId); |
@@ -485,6 +493,9 @@ discard block |
||
485 | 493 | } |
486 | 494 | } |
487 | 495 | |
496 | + /** |
|
497 | + * @param string|boolean $path |
|
498 | + */ |
|
488 | 499 | private function runBackgroundScanJob(callable $callback, $path) { |
489 | 500 | try { |
490 | 501 | $callback(); |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | } |
314 | 314 | if ($lock) { |
315 | 315 | if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
316 | - $this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
316 | + $this->storage->acquireLock('scanner::'.$path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
317 | 317 | $this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
318 | 318 | } |
319 | 319 | } |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | if ($lock) { |
326 | 326 | if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
327 | 327 | $this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
328 | - $this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
328 | + $this->storage->releaseLock('scanner::'.$path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
329 | 329 | } |
330 | 330 | } |
331 | 331 | return $data; |
@@ -414,7 +414,7 @@ discard block |
||
414 | 414 | $exceptionOccurred = false; |
415 | 415 | $childQueue = []; |
416 | 416 | foreach ($newChildren as $file) { |
417 | - $child = ($path) ? $path . '/' . $file : $file; |
|
417 | + $child = ($path) ? $path.'/'.$file : $file; |
|
418 | 418 | try { |
419 | 419 | $existingData = isset($existingChildren[$file]) ? $existingChildren[$file] : null; |
420 | 420 | $data = $this->scanFile($child, $reuse, $folderId, $existingData, $lock); |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | // might happen if inserting duplicate while a scanning |
435 | 435 | // process is running in parallel |
436 | 436 | // log and ignore |
437 | - \OCP\Util::writeLog('core', 'Exception while scanning file "' . $child . '": ' . $ex->getMessage(), \OCP\Util::DEBUG); |
|
437 | + \OCP\Util::writeLog('core', 'Exception while scanning file "'.$child.'": '.$ex->getMessage(), \OCP\Util::DEBUG); |
|
438 | 438 | $exceptionOccurred = true; |
439 | 439 | } catch (\OCP\Lock\LockedException $e) { |
440 | 440 | if ($this->useTransactions) { |
@@ -445,7 +445,7 @@ discard block |
||
445 | 445 | } |
446 | 446 | $removedChildren = \array_diff(array_keys($existingChildren), $newChildren); |
447 | 447 | foreach ($removedChildren as $childName) { |
448 | - $child = ($path) ? $path . '/' . $childName : $childName; |
|
448 | + $child = ($path) ? $path.'/'.$childName : $childName; |
|
449 | 449 | $this->removeFromCache($child); |
450 | 450 | } |
451 | 451 | if ($this->useTransactions) { |
@@ -485,13 +485,13 @@ discard block |
||
485 | 485 | */ |
486 | 486 | public function backgroundScan() { |
487 | 487 | if (!$this->cache->inCache('')) { |
488 | - $this->runBackgroundScanJob(function () { |
|
488 | + $this->runBackgroundScanJob(function() { |
|
489 | 489 | $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG); |
490 | 490 | }, ''); |
491 | 491 | } else { |
492 | 492 | $lastPath = null; |
493 | 493 | while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) { |
494 | - $this->runBackgroundScanJob(function () use ($path) { |
|
494 | + $this->runBackgroundScanJob(function() use ($path) { |
|
495 | 495 | $this->scan($path, self::SCAN_RECURSIVE_INCOMPLETE, self::REUSE_ETAG | self::REUSE_SIZE); |
496 | 496 | }, $path); |
497 | 497 | // FIXME: this won't proceed with the next item, needs revamping of getIncomplete() |
@@ -54,479 +54,479 @@ |
||
54 | 54 | * @package OC\Files\Cache |
55 | 55 | */ |
56 | 56 | class Scanner extends BasicEmitter implements IScanner { |
57 | - /** |
|
58 | - * @var \OC\Files\Storage\Storage $storage |
|
59 | - */ |
|
60 | - protected $storage; |
|
61 | - |
|
62 | - /** |
|
63 | - * @var string $storageId |
|
64 | - */ |
|
65 | - protected $storageId; |
|
66 | - |
|
67 | - /** |
|
68 | - * @var \OC\Files\Cache\Cache $cache |
|
69 | - */ |
|
70 | - protected $cache; |
|
71 | - |
|
72 | - /** |
|
73 | - * @var boolean $cacheActive If true, perform cache operations, if false, do not affect cache |
|
74 | - */ |
|
75 | - protected $cacheActive; |
|
76 | - |
|
77 | - /** |
|
78 | - * @var bool $useTransactions whether to use transactions |
|
79 | - */ |
|
80 | - protected $useTransactions = true; |
|
81 | - |
|
82 | - /** |
|
83 | - * @var \OCP\Lock\ILockingProvider |
|
84 | - */ |
|
85 | - protected $lockingProvider; |
|
86 | - |
|
87 | - public function __construct(\OC\Files\Storage\Storage $storage) { |
|
88 | - $this->storage = $storage; |
|
89 | - $this->storageId = $this->storage->getId(); |
|
90 | - $this->cache = $storage->getCache(); |
|
91 | - $this->cacheActive = !Config::getSystemValue('filesystem_cache_readonly', false); |
|
92 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Whether to wrap the scanning of a folder in a database transaction |
|
97 | - * On default transactions are used |
|
98 | - * |
|
99 | - * @param bool $useTransactions |
|
100 | - */ |
|
101 | - public function setUseTransactions($useTransactions) { |
|
102 | - $this->useTransactions = $useTransactions; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * get all the metadata of a file or folder |
|
107 | - * * |
|
108 | - * |
|
109 | - * @param string $path |
|
110 | - * @return array an array of metadata of the file |
|
111 | - */ |
|
112 | - protected function getData($path) { |
|
113 | - $data = $this->storage->getMetaData($path); |
|
114 | - if (is_null($data)) { |
|
115 | - \OCP\Util::writeLog('OC\Files\Cache\Scanner', "!!! Path '$path' is not accessible or present !!!", \OCP\Util::DEBUG); |
|
116 | - } |
|
117 | - return $data; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * scan a single file and store it in the cache |
|
122 | - * |
|
123 | - * @param string $file |
|
124 | - * @param int $reuseExisting |
|
125 | - * @param int $parentId |
|
126 | - * @param array | null $cacheData existing data in the cache for the file to be scanned |
|
127 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
128 | - * @return array an array of metadata of the scanned file |
|
129 | - * @throws \OC\ServerNotAvailableException |
|
130 | - * @throws \OCP\Lock\LockedException |
|
131 | - */ |
|
132 | - public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) { |
|
133 | - if ($file !== '') { |
|
134 | - try { |
|
135 | - $this->storage->verifyPath(dirname($file), basename($file)); |
|
136 | - } catch (\Exception $e) { |
|
137 | - return null; |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - // only proceed if $file is not a partial file nor a blacklisted file |
|
142 | - if (!self::isPartialFile($file) and !Filesystem::isFileBlacklisted($file)) { |
|
143 | - |
|
144 | - //acquire a lock |
|
145 | - if ($lock) { |
|
146 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
147 | - $this->storage->acquireLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - try { |
|
152 | - $data = $this->getData($file); |
|
153 | - } catch (ForbiddenException $e) { |
|
154 | - return null; |
|
155 | - } |
|
156 | - |
|
157 | - if ($data) { |
|
158 | - |
|
159 | - // pre-emit only if it was a file. By that we avoid counting/treating folders as files |
|
160 | - if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
161 | - $this->emit('\OC\Files\Cache\Scanner', 'scanFile', array($file, $this->storageId)); |
|
162 | - \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
163 | - } |
|
164 | - |
|
165 | - $parent = dirname($file); |
|
166 | - if ($parent === '.' or $parent === '/') { |
|
167 | - $parent = ''; |
|
168 | - } |
|
169 | - if ($parentId === -1) { |
|
170 | - $parentId = $this->cache->getParentId($file); |
|
171 | - } |
|
172 | - |
|
173 | - // scan the parent if it's not in the cache (id -1) and the current file is not the root folder |
|
174 | - if ($file and $parentId === -1) { |
|
175 | - $parentData = $this->scanFile($parent); |
|
176 | - if (!$parentData) { |
|
177 | - return null; |
|
178 | - } |
|
179 | - $parentId = $parentData['fileid']; |
|
180 | - } |
|
181 | - if ($parent) { |
|
182 | - $data['parent'] = $parentId; |
|
183 | - } |
|
184 | - if (is_null($cacheData)) { |
|
185 | - /** @var CacheEntry $cacheData */ |
|
186 | - $cacheData = $this->cache->get($file); |
|
187 | - } |
|
188 | - if ($cacheData and $reuseExisting and isset($cacheData['fileid'])) { |
|
189 | - // prevent empty etag |
|
190 | - if (empty($cacheData['etag'])) { |
|
191 | - $etag = $data['etag']; |
|
192 | - } else { |
|
193 | - $etag = $cacheData['etag']; |
|
194 | - } |
|
195 | - $fileId = $cacheData['fileid']; |
|
196 | - $data['fileid'] = $fileId; |
|
197 | - // only reuse data if the file hasn't explicitly changed |
|
198 | - if (isset($data['storage_mtime']) && isset($cacheData['storage_mtime']) && $data['storage_mtime'] === $cacheData['storage_mtime']) { |
|
199 | - $data['mtime'] = $cacheData['mtime']; |
|
200 | - if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { |
|
201 | - $data['size'] = $cacheData['size']; |
|
202 | - } |
|
203 | - if ($reuseExisting & self::REUSE_ETAG) { |
|
204 | - $data['etag'] = $etag; |
|
205 | - } |
|
206 | - } |
|
207 | - // Only update metadata that has changed |
|
208 | - $newData = array_diff_assoc($data, $cacheData->getData()); |
|
209 | - } else { |
|
210 | - $newData = $data; |
|
211 | - $fileId = -1; |
|
212 | - } |
|
213 | - if (!empty($newData)) { |
|
214 | - // Reset the checksum if the data has changed |
|
215 | - $newData['checksum'] = ''; |
|
216 | - $data['fileid'] = $this->addToCache($file, $newData, $fileId); |
|
217 | - } |
|
218 | - if (isset($cacheData['size'])) { |
|
219 | - $data['oldSize'] = $cacheData['size']; |
|
220 | - } else { |
|
221 | - $data['oldSize'] = 0; |
|
222 | - } |
|
223 | - |
|
224 | - if (isset($cacheData['encrypted'])) { |
|
225 | - $data['encrypted'] = $cacheData['encrypted']; |
|
226 | - } |
|
227 | - |
|
228 | - // post-emit only if it was a file. By that we avoid counting/treating folders as files |
|
229 | - if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
230 | - $this->emit('\OC\Files\Cache\Scanner', 'postScanFile', array($file, $this->storageId)); |
|
231 | - \OC_Hook::emit('\OC\Files\Cache\Scanner', 'post_scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
232 | - } |
|
233 | - |
|
234 | - } else { |
|
235 | - $this->removeFromCache($file); |
|
236 | - } |
|
237 | - |
|
238 | - //release the acquired lock |
|
239 | - if ($lock) { |
|
240 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
241 | - $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
242 | - } |
|
243 | - } |
|
244 | - |
|
245 | - if ($data && !isset($data['encrypted'])) { |
|
246 | - $data['encrypted'] = false; |
|
247 | - } |
|
248 | - return $data; |
|
249 | - } |
|
250 | - |
|
251 | - return null; |
|
252 | - } |
|
253 | - |
|
254 | - protected function removeFromCache($path) { |
|
255 | - \OC_Hook::emit('Scanner', 'removeFromCache', array('file' => $path)); |
|
256 | - $this->emit('\OC\Files\Cache\Scanner', 'removeFromCache', array($path)); |
|
257 | - if ($this->cacheActive) { |
|
258 | - $this->cache->remove($path); |
|
259 | - } |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * @param string $path |
|
264 | - * @param array $data |
|
265 | - * @param int $fileId |
|
266 | - * @return int the id of the added file |
|
267 | - */ |
|
268 | - protected function addToCache($path, $data, $fileId = -1) { |
|
269 | - if (isset($data['scan_permissions'])) { |
|
270 | - $data['permissions'] = $data['scan_permissions']; |
|
271 | - } |
|
272 | - \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
273 | - $this->emit('\OC\Files\Cache\Scanner', 'addToCache', array($path, $this->storageId, $data)); |
|
274 | - if ($this->cacheActive) { |
|
275 | - if ($fileId !== -1) { |
|
276 | - $this->cache->update($fileId, $data); |
|
277 | - return $fileId; |
|
278 | - } else { |
|
279 | - return $this->cache->put($path, $data); |
|
280 | - } |
|
281 | - } else { |
|
282 | - return -1; |
|
283 | - } |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * @param string $path |
|
288 | - * @param array $data |
|
289 | - * @param int $fileId |
|
290 | - */ |
|
291 | - protected function updateCache($path, $data, $fileId = -1) { |
|
292 | - \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
293 | - $this->emit('\OC\Files\Cache\Scanner', 'updateCache', array($path, $this->storageId, $data)); |
|
294 | - if ($this->cacheActive) { |
|
295 | - if ($fileId !== -1) { |
|
296 | - $this->cache->update($fileId, $data); |
|
297 | - } else { |
|
298 | - $this->cache->put($path, $data); |
|
299 | - } |
|
300 | - } |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * scan a folder and all it's children |
|
305 | - * |
|
306 | - * @param string $path |
|
307 | - * @param bool $recursive |
|
308 | - * @param int $reuse |
|
309 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
310 | - * @return array an array of the meta data of the scanned file or folder |
|
311 | - */ |
|
312 | - public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) { |
|
313 | - if ($reuse === -1) { |
|
314 | - $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
315 | - } |
|
316 | - if ($lock) { |
|
317 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
318 | - $this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
319 | - $this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
320 | - } |
|
321 | - } |
|
322 | - $data = $this->scanFile($path, $reuse, -1, null, $lock); |
|
323 | - if ($data and $data['mimetype'] === 'httpd/unix-directory') { |
|
324 | - $size = $this->scanChildren($path, $recursive, $reuse, $data['fileid'], $lock); |
|
325 | - $data['size'] = $size; |
|
326 | - } |
|
327 | - if ($lock) { |
|
328 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
329 | - $this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
330 | - $this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
331 | - } |
|
332 | - } |
|
333 | - return $data; |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Get the children currently in the cache |
|
338 | - * |
|
339 | - * @param int $folderId |
|
340 | - * @return array[] |
|
341 | - */ |
|
342 | - protected function getExistingChildren($folderId) { |
|
343 | - $existingChildren = array(); |
|
344 | - $children = $this->cache->getFolderContentsById($folderId); |
|
345 | - foreach ($children as $child) { |
|
346 | - $existingChildren[$child['name']] = $child; |
|
347 | - } |
|
348 | - return $existingChildren; |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Get the children from the storage |
|
353 | - * |
|
354 | - * @param string $folder |
|
355 | - * @return string[] |
|
356 | - */ |
|
357 | - protected function getNewChildren($folder) { |
|
358 | - $children = array(); |
|
359 | - if ($dh = $this->storage->opendir($folder)) { |
|
360 | - if (is_resource($dh)) { |
|
361 | - while (($file = readdir($dh)) !== false) { |
|
362 | - if (!Filesystem::isIgnoredDir($file)) { |
|
363 | - $children[] = trim(\OC\Files\Filesystem::normalizePath($file), '/'); |
|
364 | - } |
|
365 | - } |
|
366 | - } |
|
367 | - } |
|
368 | - return $children; |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * scan all the files and folders in a folder |
|
373 | - * |
|
374 | - * @param string $path |
|
375 | - * @param bool $recursive |
|
376 | - * @param int $reuse |
|
377 | - * @param int $folderId id for the folder to be scanned |
|
378 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
379 | - * @return int the size of the scanned folder or -1 if the size is unknown at this stage |
|
380 | - */ |
|
381 | - protected function scanChildren($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $folderId = null, $lock = true) { |
|
382 | - if ($reuse === -1) { |
|
383 | - $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
384 | - } |
|
385 | - $this->emit('\OC\Files\Cache\Scanner', 'scanFolder', array($path, $this->storageId)); |
|
386 | - $size = 0; |
|
387 | - if (!is_null($folderId)) { |
|
388 | - $folderId = $this->cache->getId($path); |
|
389 | - } |
|
390 | - $childQueue = $this->handleChildren($path, $recursive, $reuse, $folderId, $lock, $size); |
|
391 | - |
|
392 | - foreach ($childQueue as $child => $childId) { |
|
393 | - $childSize = $this->scanChildren($child, $recursive, $reuse, $childId, $lock); |
|
394 | - if ($childSize === -1) { |
|
395 | - $size = -1; |
|
396 | - } else if ($size !== -1) { |
|
397 | - $size += $childSize; |
|
398 | - } |
|
399 | - } |
|
400 | - if ($this->cacheActive) { |
|
401 | - $this->cache->update($folderId, array('size' => $size)); |
|
402 | - } |
|
403 | - $this->emit('\OC\Files\Cache\Scanner', 'postScanFolder', array($path, $this->storageId)); |
|
404 | - return $size; |
|
405 | - } |
|
406 | - |
|
407 | - private function handleChildren($path, $recursive, $reuse, $folderId, $lock, &$size) { |
|
408 | - // we put this in it's own function so it cleans up the memory before we start recursing |
|
409 | - $existingChildren = $this->getExistingChildren($folderId); |
|
410 | - $newChildren = $this->getNewChildren($path); |
|
411 | - |
|
412 | - if ($this->useTransactions) { |
|
413 | - \OC::$server->getDatabaseConnection()->beginTransaction(); |
|
414 | - } |
|
415 | - |
|
416 | - $exceptionOccurred = false; |
|
417 | - $childQueue = []; |
|
418 | - foreach ($newChildren as $file) { |
|
419 | - $child = ($path) ? $path . '/' . $file : $file; |
|
420 | - try { |
|
421 | - $existingData = isset($existingChildren[$file]) ? $existingChildren[$file] : null; |
|
422 | - $data = $this->scanFile($child, $reuse, $folderId, $existingData, $lock); |
|
423 | - if ($data) { |
|
424 | - if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE) { |
|
425 | - $childQueue[$child] = $data['fileid']; |
|
426 | - } else if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE_INCOMPLETE and $data['size'] === -1) { |
|
427 | - // only recurse into folders which aren't fully scanned |
|
428 | - $childQueue[$child] = $data['fileid']; |
|
429 | - } else if ($data['size'] === -1) { |
|
430 | - $size = -1; |
|
431 | - } else if ($size !== -1) { |
|
432 | - $size += $data['size']; |
|
433 | - } |
|
434 | - } |
|
435 | - } catch (\Doctrine\DBAL\DBALException $ex) { |
|
436 | - // might happen if inserting duplicate while a scanning |
|
437 | - // process is running in parallel |
|
438 | - // log and ignore |
|
439 | - \OCP\Util::writeLog('core', 'Exception while scanning file "' . $child . '": ' . $ex->getMessage(), \OCP\Util::DEBUG); |
|
440 | - $exceptionOccurred = true; |
|
441 | - } catch (\OCP\Lock\LockedException $e) { |
|
442 | - if ($this->useTransactions) { |
|
443 | - \OC::$server->getDatabaseConnection()->rollback(); |
|
444 | - } |
|
445 | - throw $e; |
|
446 | - } |
|
447 | - } |
|
448 | - $removedChildren = \array_diff(array_keys($existingChildren), $newChildren); |
|
449 | - foreach ($removedChildren as $childName) { |
|
450 | - $child = ($path) ? $path . '/' . $childName : $childName; |
|
451 | - $this->removeFromCache($child); |
|
452 | - } |
|
453 | - if ($this->useTransactions) { |
|
454 | - \OC::$server->getDatabaseConnection()->commit(); |
|
455 | - } |
|
456 | - if ($exceptionOccurred) { |
|
457 | - // It might happen that the parallel scan process has already |
|
458 | - // inserted mimetypes but those weren't available yet inside the transaction |
|
459 | - // To make sure to have the updated mime types in such cases, |
|
460 | - // we reload them here |
|
461 | - \OC::$server->getMimeTypeLoader()->reset(); |
|
462 | - } |
|
463 | - return $childQueue; |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * check if the file should be ignored when scanning |
|
468 | - * NOTE: files with a '.part' extension are ignored as well! |
|
469 | - * prevents unfinished put requests to be scanned |
|
470 | - * |
|
471 | - * @param string $file |
|
472 | - * @return boolean |
|
473 | - */ |
|
474 | - public static function isPartialFile($file) { |
|
475 | - if (pathinfo($file, PATHINFO_EXTENSION) === 'part') { |
|
476 | - return true; |
|
477 | - } |
|
478 | - if (strpos($file, '.part/') !== false) { |
|
479 | - return true; |
|
480 | - } |
|
481 | - |
|
482 | - return false; |
|
483 | - } |
|
484 | - |
|
485 | - /** |
|
486 | - * walk over any folders that are not fully scanned yet and scan them |
|
487 | - */ |
|
488 | - public function backgroundScan() { |
|
489 | - if (!$this->cache->inCache('')) { |
|
490 | - $this->runBackgroundScanJob(function () { |
|
491 | - $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG); |
|
492 | - }, ''); |
|
493 | - } else { |
|
494 | - $lastPath = null; |
|
495 | - while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) { |
|
496 | - $this->runBackgroundScanJob(function () use ($path) { |
|
497 | - $this->scan($path, self::SCAN_RECURSIVE_INCOMPLETE, self::REUSE_ETAG | self::REUSE_SIZE); |
|
498 | - }, $path); |
|
499 | - // FIXME: this won't proceed with the next item, needs revamping of getIncomplete() |
|
500 | - // to make this possible |
|
501 | - $lastPath = $path; |
|
502 | - } |
|
503 | - } |
|
504 | - } |
|
505 | - |
|
506 | - private function runBackgroundScanJob(callable $callback, $path) { |
|
507 | - try { |
|
508 | - $callback(); |
|
509 | - \OC_Hook::emit('Scanner', 'correctFolderSize', array('path' => $path)); |
|
510 | - if ($this->cacheActive && $this->cache instanceof Cache) { |
|
511 | - $this->cache->correctFolderSize($path); |
|
512 | - } |
|
513 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
514 | - // skip unavailable storages |
|
515 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
516 | - // skip unavailable storages |
|
517 | - } catch (\OCP\Files\ForbiddenException $e) { |
|
518 | - // skip forbidden storages |
|
519 | - } catch (\OCP\Lock\LockedException $e) { |
|
520 | - // skip unavailable storages |
|
521 | - } |
|
522 | - } |
|
523 | - |
|
524 | - /** |
|
525 | - * Set whether the cache is affected by scan operations |
|
526 | - * |
|
527 | - * @param boolean $active The active state of the cache |
|
528 | - */ |
|
529 | - public function setCacheActive($active) { |
|
530 | - $this->cacheActive = $active; |
|
531 | - } |
|
57 | + /** |
|
58 | + * @var \OC\Files\Storage\Storage $storage |
|
59 | + */ |
|
60 | + protected $storage; |
|
61 | + |
|
62 | + /** |
|
63 | + * @var string $storageId |
|
64 | + */ |
|
65 | + protected $storageId; |
|
66 | + |
|
67 | + /** |
|
68 | + * @var \OC\Files\Cache\Cache $cache |
|
69 | + */ |
|
70 | + protected $cache; |
|
71 | + |
|
72 | + /** |
|
73 | + * @var boolean $cacheActive If true, perform cache operations, if false, do not affect cache |
|
74 | + */ |
|
75 | + protected $cacheActive; |
|
76 | + |
|
77 | + /** |
|
78 | + * @var bool $useTransactions whether to use transactions |
|
79 | + */ |
|
80 | + protected $useTransactions = true; |
|
81 | + |
|
82 | + /** |
|
83 | + * @var \OCP\Lock\ILockingProvider |
|
84 | + */ |
|
85 | + protected $lockingProvider; |
|
86 | + |
|
87 | + public function __construct(\OC\Files\Storage\Storage $storage) { |
|
88 | + $this->storage = $storage; |
|
89 | + $this->storageId = $this->storage->getId(); |
|
90 | + $this->cache = $storage->getCache(); |
|
91 | + $this->cacheActive = !Config::getSystemValue('filesystem_cache_readonly', false); |
|
92 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Whether to wrap the scanning of a folder in a database transaction |
|
97 | + * On default transactions are used |
|
98 | + * |
|
99 | + * @param bool $useTransactions |
|
100 | + */ |
|
101 | + public function setUseTransactions($useTransactions) { |
|
102 | + $this->useTransactions = $useTransactions; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * get all the metadata of a file or folder |
|
107 | + * * |
|
108 | + * |
|
109 | + * @param string $path |
|
110 | + * @return array an array of metadata of the file |
|
111 | + */ |
|
112 | + protected function getData($path) { |
|
113 | + $data = $this->storage->getMetaData($path); |
|
114 | + if (is_null($data)) { |
|
115 | + \OCP\Util::writeLog('OC\Files\Cache\Scanner', "!!! Path '$path' is not accessible or present !!!", \OCP\Util::DEBUG); |
|
116 | + } |
|
117 | + return $data; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * scan a single file and store it in the cache |
|
122 | + * |
|
123 | + * @param string $file |
|
124 | + * @param int $reuseExisting |
|
125 | + * @param int $parentId |
|
126 | + * @param array | null $cacheData existing data in the cache for the file to be scanned |
|
127 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
128 | + * @return array an array of metadata of the scanned file |
|
129 | + * @throws \OC\ServerNotAvailableException |
|
130 | + * @throws \OCP\Lock\LockedException |
|
131 | + */ |
|
132 | + public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) { |
|
133 | + if ($file !== '') { |
|
134 | + try { |
|
135 | + $this->storage->verifyPath(dirname($file), basename($file)); |
|
136 | + } catch (\Exception $e) { |
|
137 | + return null; |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + // only proceed if $file is not a partial file nor a blacklisted file |
|
142 | + if (!self::isPartialFile($file) and !Filesystem::isFileBlacklisted($file)) { |
|
143 | + |
|
144 | + //acquire a lock |
|
145 | + if ($lock) { |
|
146 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
147 | + $this->storage->acquireLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + try { |
|
152 | + $data = $this->getData($file); |
|
153 | + } catch (ForbiddenException $e) { |
|
154 | + return null; |
|
155 | + } |
|
156 | + |
|
157 | + if ($data) { |
|
158 | + |
|
159 | + // pre-emit only if it was a file. By that we avoid counting/treating folders as files |
|
160 | + if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
161 | + $this->emit('\OC\Files\Cache\Scanner', 'scanFile', array($file, $this->storageId)); |
|
162 | + \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
163 | + } |
|
164 | + |
|
165 | + $parent = dirname($file); |
|
166 | + if ($parent === '.' or $parent === '/') { |
|
167 | + $parent = ''; |
|
168 | + } |
|
169 | + if ($parentId === -1) { |
|
170 | + $parentId = $this->cache->getParentId($file); |
|
171 | + } |
|
172 | + |
|
173 | + // scan the parent if it's not in the cache (id -1) and the current file is not the root folder |
|
174 | + if ($file and $parentId === -1) { |
|
175 | + $parentData = $this->scanFile($parent); |
|
176 | + if (!$parentData) { |
|
177 | + return null; |
|
178 | + } |
|
179 | + $parentId = $parentData['fileid']; |
|
180 | + } |
|
181 | + if ($parent) { |
|
182 | + $data['parent'] = $parentId; |
|
183 | + } |
|
184 | + if (is_null($cacheData)) { |
|
185 | + /** @var CacheEntry $cacheData */ |
|
186 | + $cacheData = $this->cache->get($file); |
|
187 | + } |
|
188 | + if ($cacheData and $reuseExisting and isset($cacheData['fileid'])) { |
|
189 | + // prevent empty etag |
|
190 | + if (empty($cacheData['etag'])) { |
|
191 | + $etag = $data['etag']; |
|
192 | + } else { |
|
193 | + $etag = $cacheData['etag']; |
|
194 | + } |
|
195 | + $fileId = $cacheData['fileid']; |
|
196 | + $data['fileid'] = $fileId; |
|
197 | + // only reuse data if the file hasn't explicitly changed |
|
198 | + if (isset($data['storage_mtime']) && isset($cacheData['storage_mtime']) && $data['storage_mtime'] === $cacheData['storage_mtime']) { |
|
199 | + $data['mtime'] = $cacheData['mtime']; |
|
200 | + if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { |
|
201 | + $data['size'] = $cacheData['size']; |
|
202 | + } |
|
203 | + if ($reuseExisting & self::REUSE_ETAG) { |
|
204 | + $data['etag'] = $etag; |
|
205 | + } |
|
206 | + } |
|
207 | + // Only update metadata that has changed |
|
208 | + $newData = array_diff_assoc($data, $cacheData->getData()); |
|
209 | + } else { |
|
210 | + $newData = $data; |
|
211 | + $fileId = -1; |
|
212 | + } |
|
213 | + if (!empty($newData)) { |
|
214 | + // Reset the checksum if the data has changed |
|
215 | + $newData['checksum'] = ''; |
|
216 | + $data['fileid'] = $this->addToCache($file, $newData, $fileId); |
|
217 | + } |
|
218 | + if (isset($cacheData['size'])) { |
|
219 | + $data['oldSize'] = $cacheData['size']; |
|
220 | + } else { |
|
221 | + $data['oldSize'] = 0; |
|
222 | + } |
|
223 | + |
|
224 | + if (isset($cacheData['encrypted'])) { |
|
225 | + $data['encrypted'] = $cacheData['encrypted']; |
|
226 | + } |
|
227 | + |
|
228 | + // post-emit only if it was a file. By that we avoid counting/treating folders as files |
|
229 | + if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
230 | + $this->emit('\OC\Files\Cache\Scanner', 'postScanFile', array($file, $this->storageId)); |
|
231 | + \OC_Hook::emit('\OC\Files\Cache\Scanner', 'post_scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
232 | + } |
|
233 | + |
|
234 | + } else { |
|
235 | + $this->removeFromCache($file); |
|
236 | + } |
|
237 | + |
|
238 | + //release the acquired lock |
|
239 | + if ($lock) { |
|
240 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
241 | + $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
242 | + } |
|
243 | + } |
|
244 | + |
|
245 | + if ($data && !isset($data['encrypted'])) { |
|
246 | + $data['encrypted'] = false; |
|
247 | + } |
|
248 | + return $data; |
|
249 | + } |
|
250 | + |
|
251 | + return null; |
|
252 | + } |
|
253 | + |
|
254 | + protected function removeFromCache($path) { |
|
255 | + \OC_Hook::emit('Scanner', 'removeFromCache', array('file' => $path)); |
|
256 | + $this->emit('\OC\Files\Cache\Scanner', 'removeFromCache', array($path)); |
|
257 | + if ($this->cacheActive) { |
|
258 | + $this->cache->remove($path); |
|
259 | + } |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * @param string $path |
|
264 | + * @param array $data |
|
265 | + * @param int $fileId |
|
266 | + * @return int the id of the added file |
|
267 | + */ |
|
268 | + protected function addToCache($path, $data, $fileId = -1) { |
|
269 | + if (isset($data['scan_permissions'])) { |
|
270 | + $data['permissions'] = $data['scan_permissions']; |
|
271 | + } |
|
272 | + \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
273 | + $this->emit('\OC\Files\Cache\Scanner', 'addToCache', array($path, $this->storageId, $data)); |
|
274 | + if ($this->cacheActive) { |
|
275 | + if ($fileId !== -1) { |
|
276 | + $this->cache->update($fileId, $data); |
|
277 | + return $fileId; |
|
278 | + } else { |
|
279 | + return $this->cache->put($path, $data); |
|
280 | + } |
|
281 | + } else { |
|
282 | + return -1; |
|
283 | + } |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * @param string $path |
|
288 | + * @param array $data |
|
289 | + * @param int $fileId |
|
290 | + */ |
|
291 | + protected function updateCache($path, $data, $fileId = -1) { |
|
292 | + \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
293 | + $this->emit('\OC\Files\Cache\Scanner', 'updateCache', array($path, $this->storageId, $data)); |
|
294 | + if ($this->cacheActive) { |
|
295 | + if ($fileId !== -1) { |
|
296 | + $this->cache->update($fileId, $data); |
|
297 | + } else { |
|
298 | + $this->cache->put($path, $data); |
|
299 | + } |
|
300 | + } |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * scan a folder and all it's children |
|
305 | + * |
|
306 | + * @param string $path |
|
307 | + * @param bool $recursive |
|
308 | + * @param int $reuse |
|
309 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
310 | + * @return array an array of the meta data of the scanned file or folder |
|
311 | + */ |
|
312 | + public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) { |
|
313 | + if ($reuse === -1) { |
|
314 | + $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
315 | + } |
|
316 | + if ($lock) { |
|
317 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
318 | + $this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
319 | + $this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
320 | + } |
|
321 | + } |
|
322 | + $data = $this->scanFile($path, $reuse, -1, null, $lock); |
|
323 | + if ($data and $data['mimetype'] === 'httpd/unix-directory') { |
|
324 | + $size = $this->scanChildren($path, $recursive, $reuse, $data['fileid'], $lock); |
|
325 | + $data['size'] = $size; |
|
326 | + } |
|
327 | + if ($lock) { |
|
328 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
329 | + $this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
330 | + $this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
331 | + } |
|
332 | + } |
|
333 | + return $data; |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Get the children currently in the cache |
|
338 | + * |
|
339 | + * @param int $folderId |
|
340 | + * @return array[] |
|
341 | + */ |
|
342 | + protected function getExistingChildren($folderId) { |
|
343 | + $existingChildren = array(); |
|
344 | + $children = $this->cache->getFolderContentsById($folderId); |
|
345 | + foreach ($children as $child) { |
|
346 | + $existingChildren[$child['name']] = $child; |
|
347 | + } |
|
348 | + return $existingChildren; |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Get the children from the storage |
|
353 | + * |
|
354 | + * @param string $folder |
|
355 | + * @return string[] |
|
356 | + */ |
|
357 | + protected function getNewChildren($folder) { |
|
358 | + $children = array(); |
|
359 | + if ($dh = $this->storage->opendir($folder)) { |
|
360 | + if (is_resource($dh)) { |
|
361 | + while (($file = readdir($dh)) !== false) { |
|
362 | + if (!Filesystem::isIgnoredDir($file)) { |
|
363 | + $children[] = trim(\OC\Files\Filesystem::normalizePath($file), '/'); |
|
364 | + } |
|
365 | + } |
|
366 | + } |
|
367 | + } |
|
368 | + return $children; |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * scan all the files and folders in a folder |
|
373 | + * |
|
374 | + * @param string $path |
|
375 | + * @param bool $recursive |
|
376 | + * @param int $reuse |
|
377 | + * @param int $folderId id for the folder to be scanned |
|
378 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
379 | + * @return int the size of the scanned folder or -1 if the size is unknown at this stage |
|
380 | + */ |
|
381 | + protected function scanChildren($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $folderId = null, $lock = true) { |
|
382 | + if ($reuse === -1) { |
|
383 | + $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
384 | + } |
|
385 | + $this->emit('\OC\Files\Cache\Scanner', 'scanFolder', array($path, $this->storageId)); |
|
386 | + $size = 0; |
|
387 | + if (!is_null($folderId)) { |
|
388 | + $folderId = $this->cache->getId($path); |
|
389 | + } |
|
390 | + $childQueue = $this->handleChildren($path, $recursive, $reuse, $folderId, $lock, $size); |
|
391 | + |
|
392 | + foreach ($childQueue as $child => $childId) { |
|
393 | + $childSize = $this->scanChildren($child, $recursive, $reuse, $childId, $lock); |
|
394 | + if ($childSize === -1) { |
|
395 | + $size = -1; |
|
396 | + } else if ($size !== -1) { |
|
397 | + $size += $childSize; |
|
398 | + } |
|
399 | + } |
|
400 | + if ($this->cacheActive) { |
|
401 | + $this->cache->update($folderId, array('size' => $size)); |
|
402 | + } |
|
403 | + $this->emit('\OC\Files\Cache\Scanner', 'postScanFolder', array($path, $this->storageId)); |
|
404 | + return $size; |
|
405 | + } |
|
406 | + |
|
407 | + private function handleChildren($path, $recursive, $reuse, $folderId, $lock, &$size) { |
|
408 | + // we put this in it's own function so it cleans up the memory before we start recursing |
|
409 | + $existingChildren = $this->getExistingChildren($folderId); |
|
410 | + $newChildren = $this->getNewChildren($path); |
|
411 | + |
|
412 | + if ($this->useTransactions) { |
|
413 | + \OC::$server->getDatabaseConnection()->beginTransaction(); |
|
414 | + } |
|
415 | + |
|
416 | + $exceptionOccurred = false; |
|
417 | + $childQueue = []; |
|
418 | + foreach ($newChildren as $file) { |
|
419 | + $child = ($path) ? $path . '/' . $file : $file; |
|
420 | + try { |
|
421 | + $existingData = isset($existingChildren[$file]) ? $existingChildren[$file] : null; |
|
422 | + $data = $this->scanFile($child, $reuse, $folderId, $existingData, $lock); |
|
423 | + if ($data) { |
|
424 | + if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE) { |
|
425 | + $childQueue[$child] = $data['fileid']; |
|
426 | + } else if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE_INCOMPLETE and $data['size'] === -1) { |
|
427 | + // only recurse into folders which aren't fully scanned |
|
428 | + $childQueue[$child] = $data['fileid']; |
|
429 | + } else if ($data['size'] === -1) { |
|
430 | + $size = -1; |
|
431 | + } else if ($size !== -1) { |
|
432 | + $size += $data['size']; |
|
433 | + } |
|
434 | + } |
|
435 | + } catch (\Doctrine\DBAL\DBALException $ex) { |
|
436 | + // might happen if inserting duplicate while a scanning |
|
437 | + // process is running in parallel |
|
438 | + // log and ignore |
|
439 | + \OCP\Util::writeLog('core', 'Exception while scanning file "' . $child . '": ' . $ex->getMessage(), \OCP\Util::DEBUG); |
|
440 | + $exceptionOccurred = true; |
|
441 | + } catch (\OCP\Lock\LockedException $e) { |
|
442 | + if ($this->useTransactions) { |
|
443 | + \OC::$server->getDatabaseConnection()->rollback(); |
|
444 | + } |
|
445 | + throw $e; |
|
446 | + } |
|
447 | + } |
|
448 | + $removedChildren = \array_diff(array_keys($existingChildren), $newChildren); |
|
449 | + foreach ($removedChildren as $childName) { |
|
450 | + $child = ($path) ? $path . '/' . $childName : $childName; |
|
451 | + $this->removeFromCache($child); |
|
452 | + } |
|
453 | + if ($this->useTransactions) { |
|
454 | + \OC::$server->getDatabaseConnection()->commit(); |
|
455 | + } |
|
456 | + if ($exceptionOccurred) { |
|
457 | + // It might happen that the parallel scan process has already |
|
458 | + // inserted mimetypes but those weren't available yet inside the transaction |
|
459 | + // To make sure to have the updated mime types in such cases, |
|
460 | + // we reload them here |
|
461 | + \OC::$server->getMimeTypeLoader()->reset(); |
|
462 | + } |
|
463 | + return $childQueue; |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * check if the file should be ignored when scanning |
|
468 | + * NOTE: files with a '.part' extension are ignored as well! |
|
469 | + * prevents unfinished put requests to be scanned |
|
470 | + * |
|
471 | + * @param string $file |
|
472 | + * @return boolean |
|
473 | + */ |
|
474 | + public static function isPartialFile($file) { |
|
475 | + if (pathinfo($file, PATHINFO_EXTENSION) === 'part') { |
|
476 | + return true; |
|
477 | + } |
|
478 | + if (strpos($file, '.part/') !== false) { |
|
479 | + return true; |
|
480 | + } |
|
481 | + |
|
482 | + return false; |
|
483 | + } |
|
484 | + |
|
485 | + /** |
|
486 | + * walk over any folders that are not fully scanned yet and scan them |
|
487 | + */ |
|
488 | + public function backgroundScan() { |
|
489 | + if (!$this->cache->inCache('')) { |
|
490 | + $this->runBackgroundScanJob(function () { |
|
491 | + $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG); |
|
492 | + }, ''); |
|
493 | + } else { |
|
494 | + $lastPath = null; |
|
495 | + while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) { |
|
496 | + $this->runBackgroundScanJob(function () use ($path) { |
|
497 | + $this->scan($path, self::SCAN_RECURSIVE_INCOMPLETE, self::REUSE_ETAG | self::REUSE_SIZE); |
|
498 | + }, $path); |
|
499 | + // FIXME: this won't proceed with the next item, needs revamping of getIncomplete() |
|
500 | + // to make this possible |
|
501 | + $lastPath = $path; |
|
502 | + } |
|
503 | + } |
|
504 | + } |
|
505 | + |
|
506 | + private function runBackgroundScanJob(callable $callback, $path) { |
|
507 | + try { |
|
508 | + $callback(); |
|
509 | + \OC_Hook::emit('Scanner', 'correctFolderSize', array('path' => $path)); |
|
510 | + if ($this->cacheActive && $this->cache instanceof Cache) { |
|
511 | + $this->cache->correctFolderSize($path); |
|
512 | + } |
|
513 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
514 | + // skip unavailable storages |
|
515 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
516 | + // skip unavailable storages |
|
517 | + } catch (\OCP\Files\ForbiddenException $e) { |
|
518 | + // skip forbidden storages |
|
519 | + } catch (\OCP\Lock\LockedException $e) { |
|
520 | + // skip unavailable storages |
|
521 | + } |
|
522 | + } |
|
523 | + |
|
524 | + /** |
|
525 | + * Set whether the cache is affected by scan operations |
|
526 | + * |
|
527 | + * @param boolean $active The active state of the cache |
|
528 | + */ |
|
529 | + public function setCacheActive($active) { |
|
530 | + $this->cacheActive = $active; |
|
531 | + } |
|
532 | 532 | } |
@@ -206,7 +206,7 @@ |
||
206 | 206 | } |
207 | 207 | |
208 | 208 | /** |
209 | - * @param $fileId |
|
209 | + * @param integer $fileId |
|
210 | 210 | * @return array |
211 | 211 | * @throws \OCP\Files\NotFoundException |
212 | 212 | */ |
@@ -24,7 +24,6 @@ |
||
24 | 24 | |
25 | 25 | namespace OC\Files\Config; |
26 | 26 | |
27 | -use OC\DB\QueryBuilder\Literal; |
|
28 | 27 | use OCA\Files_Sharing\SharedMount; |
29 | 28 | use OCP\DB\QueryBuilder\IQueryBuilder; |
30 | 29 | use OCP\Files\Config\ICachedMountInfo; |
@@ -42,324 +42,324 @@ |
||
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 | - public function getUsedSpaceForUsers(array $users) { |
|
338 | - $builder = $this->connection->getQueryBuilder(); |
|
339 | - |
|
340 | - $slash = $builder->createNamedParameter('/'); |
|
341 | - |
|
342 | - $mountPoint = $builder->func()->concat( |
|
343 | - $builder->func()->concat($slash, 'user_id'), |
|
344 | - $slash |
|
345 | - ); |
|
346 | - |
|
347 | - $userIds = array_map(function (IUser $user) { |
|
348 | - return $user->getUID(); |
|
349 | - }, $users); |
|
350 | - |
|
351 | - $query = $builder->select('m.user_id', 'f.size') |
|
352 | - ->from('mounts', 'm') |
|
353 | - ->innerJoin('m', 'filecache', 'f', |
|
354 | - $builder->expr()->andX( |
|
355 | - $builder->expr()->eq('m.storage_id', 'f.storage'), |
|
356 | - $builder->expr()->eq('f.path', $builder->createNamedParameter('files')) |
|
357 | - )) |
|
358 | - ->where($builder->expr()->eq('m.mount_point', $mountPoint)) |
|
359 | - ->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY))); |
|
360 | - |
|
361 | - $result = $query->execute(); |
|
362 | - |
|
363 | - return $result->fetchAll(\PDO::FETCH_KEY_PAIR); |
|
364 | - } |
|
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 | + public function getUsedSpaceForUsers(array $users) { |
|
338 | + $builder = $this->connection->getQueryBuilder(); |
|
339 | + |
|
340 | + $slash = $builder->createNamedParameter('/'); |
|
341 | + |
|
342 | + $mountPoint = $builder->func()->concat( |
|
343 | + $builder->func()->concat($slash, 'user_id'), |
|
344 | + $slash |
|
345 | + ); |
|
346 | + |
|
347 | + $userIds = array_map(function (IUser $user) { |
|
348 | + return $user->getUID(); |
|
349 | + }, $users); |
|
350 | + |
|
351 | + $query = $builder->select('m.user_id', 'f.size') |
|
352 | + ->from('mounts', 'm') |
|
353 | + ->innerJoin('m', 'filecache', 'f', |
|
354 | + $builder->expr()->andX( |
|
355 | + $builder->expr()->eq('m.storage_id', 'f.storage'), |
|
356 | + $builder->expr()->eq('f.path', $builder->createNamedParameter('files')) |
|
357 | + )) |
|
358 | + ->where($builder->expr()->eq('m.mount_point', $mountPoint)) |
|
359 | + ->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY))); |
|
360 | + |
|
361 | + $result = $query->execute(); |
|
362 | + |
|
363 | + return $result->fetchAll(\PDO::FETCH_KEY_PAIR); |
|
364 | + } |
|
365 | 365 | } |
@@ -87,11 +87,11 @@ discard block |
||
87 | 87 | |
88 | 88 | public function registerMounts(IUser $user, array $mounts) { |
89 | 89 | // filter out non-proper storages coming from unit tests |
90 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
90 | + $mounts = array_filter($mounts, function(IMountPoint $mount) { |
|
91 | 91 | return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
92 | 92 | }); |
93 | 93 | /** @var ICachedMountInfo[] $newMounts */ |
94 | - $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
|
94 | + $newMounts = array_map(function(IMountPoint $mount) use ($user) { |
|
95 | 95 | // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
96 | 96 | if ($mount->getStorageRootId() === -1) { |
97 | 97 | return null; |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | $newMounts = array_values(array_filter($newMounts)); |
103 | 103 | |
104 | 104 | $cachedMounts = $this->getMountsForUser($user); |
105 | - $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
105 | + $mountDiff = function(ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
106 | 106 | // since we are only looking for mounts for a specific user comparing on root id is enough |
107 | 107 | return $mount1->getRootId() - $mount2->getRootId(); |
108 | 108 | }; |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | ], ['root_id', 'user_id']); |
164 | 164 | } else { |
165 | 165 | // in some cases this is legitimate, like orphaned shares |
166 | - $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
|
166 | + $this->logger->debug('Could not get storage info for mount at '.$mount->getMountPoint()); |
|
167 | 167 | } |
168 | 168 | } |
169 | 169 | |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | if (is_null($user)) { |
195 | 195 | return null; |
196 | 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'] : ''); |
|
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 | 198 | } |
199 | 199 | |
200 | 200 | /** |
@@ -268,12 +268,12 @@ discard block |
||
268 | 268 | $row = $query->execute()->fetch(); |
269 | 269 | if (is_array($row)) { |
270 | 270 | $this->cacheInfoCache[$fileId] = [ |
271 | - (int)$row['storage'], |
|
271 | + (int) $row['storage'], |
|
272 | 272 | $row['path'], |
273 | - (int)$row['mimetype'] |
|
273 | + (int) $row['mimetype'] |
|
274 | 274 | ]; |
275 | 275 | } else { |
276 | - throw new NotFoundException('File with id "' . $fileId . '" not found'); |
|
276 | + throw new NotFoundException('File with id "'.$fileId.'" not found'); |
|
277 | 277 | } |
278 | 278 | } |
279 | 279 | return $this->cacheInfoCache[$fileId]; |
@@ -294,13 +294,13 @@ discard block |
||
294 | 294 | $mountsForStorage = $this->getMountsForStorageId($storageId, $user); |
295 | 295 | |
296 | 296 | // filter mounts that are from the same storage but a different directory |
297 | - return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
297 | + return array_filter($mountsForStorage, function(ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
298 | 298 | if ($fileId === $mount->getRootId()) { |
299 | 299 | return true; |
300 | 300 | } |
301 | 301 | $internalMountPath = $mount->getRootInternalPath(); |
302 | 302 | |
303 | - return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
|
303 | + return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath.'/'; |
|
304 | 304 | }); |
305 | 305 | } |
306 | 306 | |
@@ -344,7 +344,7 @@ discard block |
||
344 | 344 | $slash |
345 | 345 | ); |
346 | 346 | |
347 | - $userIds = array_map(function (IUser $user) { |
|
347 | + $userIds = array_map(function(IUser $user) { |
|
348 | 348 | return $user->getUID(); |
349 | 349 | }, $users); |
350 | 350 |
@@ -52,7 +52,7 @@ |
||
52 | 52 | * Magic method to first get the real rootFolder and then |
53 | 53 | * call $method with $args on it |
54 | 54 | * |
55 | - * @param $method |
|
55 | + * @param string $method |
|
56 | 56 | * @param $args |
57 | 57 | * @return mixed |
58 | 58 | */ |
@@ -34,447 +34,447 @@ |
||
34 | 34 | * @package OC\Files\Node |
35 | 35 | */ |
36 | 36 | class LazyRoot implements IRootFolder { |
37 | - /** @var \Closure */ |
|
38 | - private $rootFolderClosure; |
|
39 | - |
|
40 | - /** @var IRootFolder */ |
|
41 | - private $rootFolder; |
|
42 | - |
|
43 | - /** |
|
44 | - * LazyRoot constructor. |
|
45 | - * |
|
46 | - * @param \Closure $rootFolderClosure |
|
47 | - */ |
|
48 | - public function __construct(\Closure $rootFolderClosure) { |
|
49 | - $this->rootFolderClosure = $rootFolderClosure; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Magic method to first get the real rootFolder and then |
|
54 | - * call $method with $args on it |
|
55 | - * |
|
56 | - * @param $method |
|
57 | - * @param $args |
|
58 | - * @return mixed |
|
59 | - */ |
|
60 | - public function __call($method, $args) { |
|
61 | - if ($this->rootFolder === null) { |
|
62 | - $this->rootFolder = call_user_func($this->rootFolderClosure); |
|
63 | - } |
|
64 | - |
|
65 | - return call_user_func_array([$this->rootFolder, $method], $args); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @inheritDoc |
|
70 | - */ |
|
71 | - public function getUser() { |
|
72 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @inheritDoc |
|
77 | - */ |
|
78 | - public function listen($scope, $method, callable $callback) { |
|
79 | - $this->__call(__FUNCTION__, func_get_args()); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @inheritDoc |
|
84 | - */ |
|
85 | - public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
86 | - $this->__call(__FUNCTION__, func_get_args()); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @inheritDoc |
|
91 | - */ |
|
92 | - public function emit($scope, $method, $arguments = array()) { |
|
93 | - $this->__call(__FUNCTION__, func_get_args()); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @inheritDoc |
|
98 | - */ |
|
99 | - public function mount($storage, $mountPoint, $arguments = array()) { |
|
100 | - $this->__call(__FUNCTION__, func_get_args()); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @inheritDoc |
|
105 | - */ |
|
106 | - public function getMount($mountPoint) { |
|
107 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @inheritDoc |
|
112 | - */ |
|
113 | - public function getMountsIn($mountPoint) { |
|
114 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @inheritDoc |
|
119 | - */ |
|
120 | - public function getMountByStorageId($storageId) { |
|
121 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @inheritDoc |
|
126 | - */ |
|
127 | - public function getMountByNumericStorageId($numericId) { |
|
128 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @inheritDoc |
|
133 | - */ |
|
134 | - public function unMount($mount) { |
|
135 | - $this->__call(__FUNCTION__, func_get_args()); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @inheritDoc |
|
140 | - */ |
|
141 | - public function get($path) { |
|
142 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @inheritDoc |
|
147 | - */ |
|
148 | - public function rename($targetPath) { |
|
149 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @inheritDoc |
|
154 | - */ |
|
155 | - public function delete() { |
|
156 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @inheritDoc |
|
161 | - */ |
|
162 | - public function copy($targetPath) { |
|
163 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @inheritDoc |
|
168 | - */ |
|
169 | - public function touch($mtime = null) { |
|
170 | - $this->__call(__FUNCTION__, func_get_args()); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @inheritDoc |
|
175 | - */ |
|
176 | - public function getStorage() { |
|
177 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @inheritDoc |
|
182 | - */ |
|
183 | - public function getPath() { |
|
184 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * @inheritDoc |
|
189 | - */ |
|
190 | - public function getInternalPath() { |
|
191 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @inheritDoc |
|
196 | - */ |
|
197 | - public function getId() { |
|
198 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * @inheritDoc |
|
203 | - */ |
|
204 | - public function stat() { |
|
205 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * @inheritDoc |
|
210 | - */ |
|
211 | - public function getMTime() { |
|
212 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * @inheritDoc |
|
217 | - */ |
|
218 | - public function getSize() { |
|
219 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @inheritDoc |
|
224 | - */ |
|
225 | - public function getEtag() { |
|
226 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * @inheritDoc |
|
231 | - */ |
|
232 | - public function getPermissions() { |
|
233 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * @inheritDoc |
|
238 | - */ |
|
239 | - public function isReadable() { |
|
240 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @inheritDoc |
|
245 | - */ |
|
246 | - public function isUpdateable() { |
|
247 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * @inheritDoc |
|
252 | - */ |
|
253 | - public function isDeletable() { |
|
254 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * @inheritDoc |
|
259 | - */ |
|
260 | - public function isShareable() { |
|
261 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @inheritDoc |
|
266 | - */ |
|
267 | - public function getParent() { |
|
268 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * @inheritDoc |
|
273 | - */ |
|
274 | - public function getName() { |
|
275 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @inheritDoc |
|
280 | - */ |
|
281 | - public function getUserFolder($userId) { |
|
282 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * @inheritDoc |
|
287 | - */ |
|
288 | - public function getMimetype() { |
|
289 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * @inheritDoc |
|
294 | - */ |
|
295 | - public function getMimePart() { |
|
296 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @inheritDoc |
|
301 | - */ |
|
302 | - public function isEncrypted() { |
|
303 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * @inheritDoc |
|
308 | - */ |
|
309 | - public function getType() { |
|
310 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * @inheritDoc |
|
315 | - */ |
|
316 | - public function isShared() { |
|
317 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * @inheritDoc |
|
322 | - */ |
|
323 | - public function isMounted() { |
|
324 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * @inheritDoc |
|
329 | - */ |
|
330 | - public function getMountPoint() { |
|
331 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @inheritDoc |
|
336 | - */ |
|
337 | - public function getOwner() { |
|
338 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * @inheritDoc |
|
343 | - */ |
|
344 | - public function getChecksum() { |
|
345 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * @inheritDoc |
|
350 | - */ |
|
351 | - public function getFullPath($path) { |
|
352 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * @inheritDoc |
|
357 | - */ |
|
358 | - public function getRelativePath($path) { |
|
359 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * @inheritDoc |
|
364 | - */ |
|
365 | - public function isSubNode($node) { |
|
366 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * @inheritDoc |
|
371 | - */ |
|
372 | - public function getDirectoryListing() { |
|
373 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * @inheritDoc |
|
378 | - */ |
|
379 | - public function nodeExists($path) { |
|
380 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * @inheritDoc |
|
385 | - */ |
|
386 | - public function newFolder($path) { |
|
387 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * @inheritDoc |
|
392 | - */ |
|
393 | - public function newFile($path) { |
|
394 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * @inheritDoc |
|
399 | - */ |
|
400 | - public function search($query) { |
|
401 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * @inheritDoc |
|
406 | - */ |
|
407 | - public function searchByMime($mimetype) { |
|
408 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * @inheritDoc |
|
413 | - */ |
|
414 | - public function searchByTag($tag, $userId) { |
|
415 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * @inheritDoc |
|
420 | - */ |
|
421 | - public function getById($id) { |
|
422 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * @inheritDoc |
|
427 | - */ |
|
428 | - public function getFreeSpace() { |
|
429 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * @inheritDoc |
|
434 | - */ |
|
435 | - public function isCreatable() { |
|
436 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * @inheritDoc |
|
441 | - */ |
|
442 | - public function getNonExistingName($name) { |
|
443 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * @inheritDoc |
|
448 | - */ |
|
449 | - public function move($targetPath) { |
|
450 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * @inheritDoc |
|
455 | - */ |
|
456 | - public function lock($type) { |
|
457 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * @inheritDoc |
|
462 | - */ |
|
463 | - public function changeLock($targetType) { |
|
464 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * @inheritDoc |
|
469 | - */ |
|
470 | - public function unlock($type) { |
|
471 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
472 | - } |
|
473 | - |
|
474 | - /** |
|
475 | - * @inheritDoc |
|
476 | - */ |
|
477 | - public function getRecent($limit, $offset = 0) { |
|
478 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
479 | - } |
|
37 | + /** @var \Closure */ |
|
38 | + private $rootFolderClosure; |
|
39 | + |
|
40 | + /** @var IRootFolder */ |
|
41 | + private $rootFolder; |
|
42 | + |
|
43 | + /** |
|
44 | + * LazyRoot constructor. |
|
45 | + * |
|
46 | + * @param \Closure $rootFolderClosure |
|
47 | + */ |
|
48 | + public function __construct(\Closure $rootFolderClosure) { |
|
49 | + $this->rootFolderClosure = $rootFolderClosure; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Magic method to first get the real rootFolder and then |
|
54 | + * call $method with $args on it |
|
55 | + * |
|
56 | + * @param $method |
|
57 | + * @param $args |
|
58 | + * @return mixed |
|
59 | + */ |
|
60 | + public function __call($method, $args) { |
|
61 | + if ($this->rootFolder === null) { |
|
62 | + $this->rootFolder = call_user_func($this->rootFolderClosure); |
|
63 | + } |
|
64 | + |
|
65 | + return call_user_func_array([$this->rootFolder, $method], $args); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @inheritDoc |
|
70 | + */ |
|
71 | + public function getUser() { |
|
72 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @inheritDoc |
|
77 | + */ |
|
78 | + public function listen($scope, $method, callable $callback) { |
|
79 | + $this->__call(__FUNCTION__, func_get_args()); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @inheritDoc |
|
84 | + */ |
|
85 | + public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
86 | + $this->__call(__FUNCTION__, func_get_args()); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @inheritDoc |
|
91 | + */ |
|
92 | + public function emit($scope, $method, $arguments = array()) { |
|
93 | + $this->__call(__FUNCTION__, func_get_args()); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @inheritDoc |
|
98 | + */ |
|
99 | + public function mount($storage, $mountPoint, $arguments = array()) { |
|
100 | + $this->__call(__FUNCTION__, func_get_args()); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @inheritDoc |
|
105 | + */ |
|
106 | + public function getMount($mountPoint) { |
|
107 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @inheritDoc |
|
112 | + */ |
|
113 | + public function getMountsIn($mountPoint) { |
|
114 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @inheritDoc |
|
119 | + */ |
|
120 | + public function getMountByStorageId($storageId) { |
|
121 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @inheritDoc |
|
126 | + */ |
|
127 | + public function getMountByNumericStorageId($numericId) { |
|
128 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @inheritDoc |
|
133 | + */ |
|
134 | + public function unMount($mount) { |
|
135 | + $this->__call(__FUNCTION__, func_get_args()); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @inheritDoc |
|
140 | + */ |
|
141 | + public function get($path) { |
|
142 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @inheritDoc |
|
147 | + */ |
|
148 | + public function rename($targetPath) { |
|
149 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @inheritDoc |
|
154 | + */ |
|
155 | + public function delete() { |
|
156 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @inheritDoc |
|
161 | + */ |
|
162 | + public function copy($targetPath) { |
|
163 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @inheritDoc |
|
168 | + */ |
|
169 | + public function touch($mtime = null) { |
|
170 | + $this->__call(__FUNCTION__, func_get_args()); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @inheritDoc |
|
175 | + */ |
|
176 | + public function getStorage() { |
|
177 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @inheritDoc |
|
182 | + */ |
|
183 | + public function getPath() { |
|
184 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * @inheritDoc |
|
189 | + */ |
|
190 | + public function getInternalPath() { |
|
191 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @inheritDoc |
|
196 | + */ |
|
197 | + public function getId() { |
|
198 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * @inheritDoc |
|
203 | + */ |
|
204 | + public function stat() { |
|
205 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * @inheritDoc |
|
210 | + */ |
|
211 | + public function getMTime() { |
|
212 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * @inheritDoc |
|
217 | + */ |
|
218 | + public function getSize() { |
|
219 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @inheritDoc |
|
224 | + */ |
|
225 | + public function getEtag() { |
|
226 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * @inheritDoc |
|
231 | + */ |
|
232 | + public function getPermissions() { |
|
233 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * @inheritDoc |
|
238 | + */ |
|
239 | + public function isReadable() { |
|
240 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @inheritDoc |
|
245 | + */ |
|
246 | + public function isUpdateable() { |
|
247 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * @inheritDoc |
|
252 | + */ |
|
253 | + public function isDeletable() { |
|
254 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * @inheritDoc |
|
259 | + */ |
|
260 | + public function isShareable() { |
|
261 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @inheritDoc |
|
266 | + */ |
|
267 | + public function getParent() { |
|
268 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * @inheritDoc |
|
273 | + */ |
|
274 | + public function getName() { |
|
275 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @inheritDoc |
|
280 | + */ |
|
281 | + public function getUserFolder($userId) { |
|
282 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * @inheritDoc |
|
287 | + */ |
|
288 | + public function getMimetype() { |
|
289 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * @inheritDoc |
|
294 | + */ |
|
295 | + public function getMimePart() { |
|
296 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @inheritDoc |
|
301 | + */ |
|
302 | + public function isEncrypted() { |
|
303 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * @inheritDoc |
|
308 | + */ |
|
309 | + public function getType() { |
|
310 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * @inheritDoc |
|
315 | + */ |
|
316 | + public function isShared() { |
|
317 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * @inheritDoc |
|
322 | + */ |
|
323 | + public function isMounted() { |
|
324 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * @inheritDoc |
|
329 | + */ |
|
330 | + public function getMountPoint() { |
|
331 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @inheritDoc |
|
336 | + */ |
|
337 | + public function getOwner() { |
|
338 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * @inheritDoc |
|
343 | + */ |
|
344 | + public function getChecksum() { |
|
345 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * @inheritDoc |
|
350 | + */ |
|
351 | + public function getFullPath($path) { |
|
352 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * @inheritDoc |
|
357 | + */ |
|
358 | + public function getRelativePath($path) { |
|
359 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * @inheritDoc |
|
364 | + */ |
|
365 | + public function isSubNode($node) { |
|
366 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * @inheritDoc |
|
371 | + */ |
|
372 | + public function getDirectoryListing() { |
|
373 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * @inheritDoc |
|
378 | + */ |
|
379 | + public function nodeExists($path) { |
|
380 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * @inheritDoc |
|
385 | + */ |
|
386 | + public function newFolder($path) { |
|
387 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * @inheritDoc |
|
392 | + */ |
|
393 | + public function newFile($path) { |
|
394 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * @inheritDoc |
|
399 | + */ |
|
400 | + public function search($query) { |
|
401 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * @inheritDoc |
|
406 | + */ |
|
407 | + public function searchByMime($mimetype) { |
|
408 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * @inheritDoc |
|
413 | + */ |
|
414 | + public function searchByTag($tag, $userId) { |
|
415 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * @inheritDoc |
|
420 | + */ |
|
421 | + public function getById($id) { |
|
422 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * @inheritDoc |
|
427 | + */ |
|
428 | + public function getFreeSpace() { |
|
429 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * @inheritDoc |
|
434 | + */ |
|
435 | + public function isCreatable() { |
|
436 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * @inheritDoc |
|
441 | + */ |
|
442 | + public function getNonExistingName($name) { |
|
443 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * @inheritDoc |
|
448 | + */ |
|
449 | + public function move($targetPath) { |
|
450 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * @inheritDoc |
|
455 | + */ |
|
456 | + public function lock($type) { |
|
457 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * @inheritDoc |
|
462 | + */ |
|
463 | + public function changeLock($targetType) { |
|
464 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * @inheritDoc |
|
469 | + */ |
|
470 | + public function unlock($type) { |
|
471 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
472 | + } |
|
473 | + |
|
474 | + /** |
|
475 | + * @inheritDoc |
|
476 | + */ |
|
477 | + public function getRecent($limit, $offset = 0) { |
|
478 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
479 | + } |
|
480 | 480 | } |
@@ -54,6 +54,9 @@ |
||
54 | 54 | $this->flysystem->addPlugin(new GetWithMetadata()); |
55 | 55 | } |
56 | 56 | |
57 | + /** |
|
58 | + * @param string $path |
|
59 | + */ |
|
57 | 60 | protected function buildPath($path) { |
58 | 61 | $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path); |
59 | 62 | return ltrim($fullPath, '/'); |
@@ -35,223 +35,223 @@ |
||
35 | 35 | * To use: subclass and call $this->buildFlysystem with the flysystem adapter of choice |
36 | 36 | */ |
37 | 37 | abstract class Flysystem extends Common { |
38 | - /** |
|
39 | - * @var Filesystem |
|
40 | - */ |
|
41 | - protected $flysystem; |
|
38 | + /** |
|
39 | + * @var Filesystem |
|
40 | + */ |
|
41 | + protected $flysystem; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $root = ''; |
|
43 | + /** |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $root = ''; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Initialize the storage backend with a flyssytem adapter |
|
50 | - * |
|
51 | - * @param \League\Flysystem\AdapterInterface $adapter |
|
52 | - */ |
|
53 | - protected function buildFlySystem(AdapterInterface $adapter) { |
|
54 | - $this->flysystem = new Filesystem($adapter); |
|
55 | - $this->flysystem->addPlugin(new GetWithMetadata()); |
|
56 | - } |
|
48 | + /** |
|
49 | + * Initialize the storage backend with a flyssytem adapter |
|
50 | + * |
|
51 | + * @param \League\Flysystem\AdapterInterface $adapter |
|
52 | + */ |
|
53 | + protected function buildFlySystem(AdapterInterface $adapter) { |
|
54 | + $this->flysystem = new Filesystem($adapter); |
|
55 | + $this->flysystem->addPlugin(new GetWithMetadata()); |
|
56 | + } |
|
57 | 57 | |
58 | - protected function buildPath($path) { |
|
59 | - $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path); |
|
60 | - return ltrim($fullPath, '/'); |
|
61 | - } |
|
58 | + protected function buildPath($path) { |
|
59 | + $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path); |
|
60 | + return ltrim($fullPath, '/'); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - public function file_get_contents($path) { |
|
67 | - return $this->flysystem->read($this->buildPath($path)); |
|
68 | - } |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + public function file_get_contents($path) { |
|
67 | + return $this->flysystem->read($this->buildPath($path)); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * {@inheritdoc} |
|
72 | - */ |
|
73 | - public function file_put_contents($path, $data) { |
|
74 | - return $this->flysystem->put($this->buildPath($path), $data); |
|
75 | - } |
|
70 | + /** |
|
71 | + * {@inheritdoc} |
|
72 | + */ |
|
73 | + public function file_put_contents($path, $data) { |
|
74 | + return $this->flysystem->put($this->buildPath($path), $data); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * {@inheritdoc} |
|
79 | - */ |
|
80 | - public function file_exists($path) { |
|
81 | - return $this->flysystem->has($this->buildPath($path)); |
|
82 | - } |
|
77 | + /** |
|
78 | + * {@inheritdoc} |
|
79 | + */ |
|
80 | + public function file_exists($path) { |
|
81 | + return $this->flysystem->has($this->buildPath($path)); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * {@inheritdoc} |
|
86 | - */ |
|
87 | - public function unlink($path) { |
|
88 | - if ($this->is_dir($path)) { |
|
89 | - return $this->rmdir($path); |
|
90 | - } |
|
91 | - try { |
|
92 | - return $this->flysystem->delete($this->buildPath($path)); |
|
93 | - } catch (FileNotFoundException $e) { |
|
94 | - return false; |
|
95 | - } |
|
96 | - } |
|
84 | + /** |
|
85 | + * {@inheritdoc} |
|
86 | + */ |
|
87 | + public function unlink($path) { |
|
88 | + if ($this->is_dir($path)) { |
|
89 | + return $this->rmdir($path); |
|
90 | + } |
|
91 | + try { |
|
92 | + return $this->flysystem->delete($this->buildPath($path)); |
|
93 | + } catch (FileNotFoundException $e) { |
|
94 | + return false; |
|
95 | + } |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * {@inheritdoc} |
|
100 | - */ |
|
101 | - public function rename($source, $target) { |
|
102 | - if ($this->file_exists($target)) { |
|
103 | - $this->unlink($target); |
|
104 | - } |
|
105 | - return $this->flysystem->rename($this->buildPath($source), $this->buildPath($target)); |
|
106 | - } |
|
98 | + /** |
|
99 | + * {@inheritdoc} |
|
100 | + */ |
|
101 | + public function rename($source, $target) { |
|
102 | + if ($this->file_exists($target)) { |
|
103 | + $this->unlink($target); |
|
104 | + } |
|
105 | + return $this->flysystem->rename($this->buildPath($source), $this->buildPath($target)); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * {@inheritdoc} |
|
110 | - */ |
|
111 | - public function copy($source, $target) { |
|
112 | - if ($this->file_exists($target)) { |
|
113 | - $this->unlink($target); |
|
114 | - } |
|
115 | - return $this->flysystem->copy($this->buildPath($source), $this->buildPath($target)); |
|
116 | - } |
|
108 | + /** |
|
109 | + * {@inheritdoc} |
|
110 | + */ |
|
111 | + public function copy($source, $target) { |
|
112 | + if ($this->file_exists($target)) { |
|
113 | + $this->unlink($target); |
|
114 | + } |
|
115 | + return $this->flysystem->copy($this->buildPath($source), $this->buildPath($target)); |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * {@inheritdoc} |
|
120 | - */ |
|
121 | - public function filesize($path) { |
|
122 | - if ($this->is_dir($path)) { |
|
123 | - return 0; |
|
124 | - } else { |
|
125 | - return $this->flysystem->getSize($this->buildPath($path)); |
|
126 | - } |
|
127 | - } |
|
118 | + /** |
|
119 | + * {@inheritdoc} |
|
120 | + */ |
|
121 | + public function filesize($path) { |
|
122 | + if ($this->is_dir($path)) { |
|
123 | + return 0; |
|
124 | + } else { |
|
125 | + return $this->flysystem->getSize($this->buildPath($path)); |
|
126 | + } |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * {@inheritdoc} |
|
131 | - */ |
|
132 | - public function mkdir($path) { |
|
133 | - if ($this->file_exists($path)) { |
|
134 | - return false; |
|
135 | - } |
|
136 | - return $this->flysystem->createDir($this->buildPath($path)); |
|
137 | - } |
|
129 | + /** |
|
130 | + * {@inheritdoc} |
|
131 | + */ |
|
132 | + public function mkdir($path) { |
|
133 | + if ($this->file_exists($path)) { |
|
134 | + return false; |
|
135 | + } |
|
136 | + return $this->flysystem->createDir($this->buildPath($path)); |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * {@inheritdoc} |
|
141 | - */ |
|
142 | - public function filemtime($path) { |
|
143 | - return $this->flysystem->getTimestamp($this->buildPath($path)); |
|
144 | - } |
|
139 | + /** |
|
140 | + * {@inheritdoc} |
|
141 | + */ |
|
142 | + public function filemtime($path) { |
|
143 | + return $this->flysystem->getTimestamp($this->buildPath($path)); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * {@inheritdoc} |
|
148 | - */ |
|
149 | - public function rmdir($path) { |
|
150 | - try { |
|
151 | - return @$this->flysystem->deleteDir($this->buildPath($path)); |
|
152 | - } catch (FileNotFoundException $e) { |
|
153 | - return false; |
|
154 | - } |
|
155 | - } |
|
146 | + /** |
|
147 | + * {@inheritdoc} |
|
148 | + */ |
|
149 | + public function rmdir($path) { |
|
150 | + try { |
|
151 | + return @$this->flysystem->deleteDir($this->buildPath($path)); |
|
152 | + } catch (FileNotFoundException $e) { |
|
153 | + return false; |
|
154 | + } |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * {@inheritdoc} |
|
159 | - */ |
|
160 | - public function opendir($path) { |
|
161 | - try { |
|
162 | - $content = $this->flysystem->listContents($this->buildPath($path)); |
|
163 | - } catch (FileNotFoundException $e) { |
|
164 | - return false; |
|
165 | - } |
|
166 | - $names = array_map(function ($object) { |
|
167 | - return $object['basename']; |
|
168 | - }, $content); |
|
169 | - return IteratorDirectory::wrap($names); |
|
170 | - } |
|
157 | + /** |
|
158 | + * {@inheritdoc} |
|
159 | + */ |
|
160 | + public function opendir($path) { |
|
161 | + try { |
|
162 | + $content = $this->flysystem->listContents($this->buildPath($path)); |
|
163 | + } catch (FileNotFoundException $e) { |
|
164 | + return false; |
|
165 | + } |
|
166 | + $names = array_map(function ($object) { |
|
167 | + return $object['basename']; |
|
168 | + }, $content); |
|
169 | + return IteratorDirectory::wrap($names); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * {@inheritdoc} |
|
174 | - */ |
|
175 | - public function fopen($path, $mode) { |
|
176 | - $fullPath = $this->buildPath($path); |
|
177 | - $useExisting = true; |
|
178 | - switch ($mode) { |
|
179 | - case 'r': |
|
180 | - case 'rb': |
|
181 | - try { |
|
182 | - return $this->flysystem->readStream($fullPath); |
|
183 | - } catch (FileNotFoundException $e) { |
|
184 | - return false; |
|
185 | - } |
|
186 | - case 'w': |
|
187 | - case 'w+': |
|
188 | - case 'wb': |
|
189 | - case 'wb+': |
|
190 | - $useExisting = false; |
|
191 | - case 'a': |
|
192 | - case 'ab': |
|
193 | - case 'r+': |
|
194 | - case 'a+': |
|
195 | - case 'x': |
|
196 | - case 'x+': |
|
197 | - case 'c': |
|
198 | - case 'c+': |
|
199 | - //emulate these |
|
200 | - if ($useExisting and $this->file_exists($path)) { |
|
201 | - if (!$this->isUpdatable($path)) { |
|
202 | - return false; |
|
203 | - } |
|
204 | - $tmpFile = $this->getCachedFile($path); |
|
205 | - } else { |
|
206 | - if (!$this->isCreatable(dirname($path))) { |
|
207 | - return false; |
|
208 | - } |
|
209 | - $tmpFile = \OCP\Files::tmpFile(); |
|
210 | - } |
|
211 | - $source = fopen($tmpFile, $mode); |
|
212 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) { |
|
213 | - $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r')); |
|
214 | - unlink($tmpFile); |
|
215 | - }); |
|
216 | - } |
|
217 | - return false; |
|
218 | - } |
|
172 | + /** |
|
173 | + * {@inheritdoc} |
|
174 | + */ |
|
175 | + public function fopen($path, $mode) { |
|
176 | + $fullPath = $this->buildPath($path); |
|
177 | + $useExisting = true; |
|
178 | + switch ($mode) { |
|
179 | + case 'r': |
|
180 | + case 'rb': |
|
181 | + try { |
|
182 | + return $this->flysystem->readStream($fullPath); |
|
183 | + } catch (FileNotFoundException $e) { |
|
184 | + return false; |
|
185 | + } |
|
186 | + case 'w': |
|
187 | + case 'w+': |
|
188 | + case 'wb': |
|
189 | + case 'wb+': |
|
190 | + $useExisting = false; |
|
191 | + case 'a': |
|
192 | + case 'ab': |
|
193 | + case 'r+': |
|
194 | + case 'a+': |
|
195 | + case 'x': |
|
196 | + case 'x+': |
|
197 | + case 'c': |
|
198 | + case 'c+': |
|
199 | + //emulate these |
|
200 | + if ($useExisting and $this->file_exists($path)) { |
|
201 | + if (!$this->isUpdatable($path)) { |
|
202 | + return false; |
|
203 | + } |
|
204 | + $tmpFile = $this->getCachedFile($path); |
|
205 | + } else { |
|
206 | + if (!$this->isCreatable(dirname($path))) { |
|
207 | + return false; |
|
208 | + } |
|
209 | + $tmpFile = \OCP\Files::tmpFile(); |
|
210 | + } |
|
211 | + $source = fopen($tmpFile, $mode); |
|
212 | + return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) { |
|
213 | + $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r')); |
|
214 | + unlink($tmpFile); |
|
215 | + }); |
|
216 | + } |
|
217 | + return false; |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * {@inheritdoc} |
|
222 | - */ |
|
223 | - public function touch($path, $mtime = null) { |
|
224 | - if ($this->file_exists($path)) { |
|
225 | - return false; |
|
226 | - } else { |
|
227 | - $this->file_put_contents($path, ''); |
|
228 | - return true; |
|
229 | - } |
|
230 | - } |
|
220 | + /** |
|
221 | + * {@inheritdoc} |
|
222 | + */ |
|
223 | + public function touch($path, $mtime = null) { |
|
224 | + if ($this->file_exists($path)) { |
|
225 | + return false; |
|
226 | + } else { |
|
227 | + $this->file_put_contents($path, ''); |
|
228 | + return true; |
|
229 | + } |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * {@inheritdoc} |
|
234 | - */ |
|
235 | - public function stat($path) { |
|
236 | - $info = $this->flysystem->getWithMetadata($this->buildPath($path), ['timestamp', 'size']); |
|
237 | - return [ |
|
238 | - 'mtime' => $info['timestamp'], |
|
239 | - 'size' => $info['size'] |
|
240 | - ]; |
|
241 | - } |
|
232 | + /** |
|
233 | + * {@inheritdoc} |
|
234 | + */ |
|
235 | + public function stat($path) { |
|
236 | + $info = $this->flysystem->getWithMetadata($this->buildPath($path), ['timestamp', 'size']); |
|
237 | + return [ |
|
238 | + 'mtime' => $info['timestamp'], |
|
239 | + 'size' => $info['size'] |
|
240 | + ]; |
|
241 | + } |
|
242 | 242 | |
243 | - /** |
|
244 | - * {@inheritdoc} |
|
245 | - */ |
|
246 | - public function filetype($path) { |
|
247 | - if ($path === '' or $path === '/' or $path === '.') { |
|
248 | - return 'dir'; |
|
249 | - } |
|
250 | - try { |
|
251 | - $info = $this->flysystem->getMetadata($this->buildPath($path)); |
|
252 | - } catch (FileNotFoundException $e) { |
|
253 | - return false; |
|
254 | - } |
|
255 | - return $info['type']; |
|
256 | - } |
|
243 | + /** |
|
244 | + * {@inheritdoc} |
|
245 | + */ |
|
246 | + public function filetype($path) { |
|
247 | + if ($path === '' or $path === '/' or $path === '.') { |
|
248 | + return 'dir'; |
|
249 | + } |
|
250 | + try { |
|
251 | + $info = $this->flysystem->getMetadata($this->buildPath($path)); |
|
252 | + } catch (FileNotFoundException $e) { |
|
253 | + return false; |
|
254 | + } |
|
255 | + return $info['type']; |
|
256 | + } |
|
257 | 257 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | } |
57 | 57 | |
58 | 58 | protected function buildPath($path) { |
59 | - $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path); |
|
59 | + $fullPath = \OC\Files\Filesystem::normalizePath($this->root.'/'.$path); |
|
60 | 60 | return ltrim($fullPath, '/'); |
61 | 61 | } |
62 | 62 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | } catch (FileNotFoundException $e) { |
164 | 164 | return false; |
165 | 165 | } |
166 | - $names = array_map(function ($object) { |
|
166 | + $names = array_map(function($object) { |
|
167 | 167 | return $object['basename']; |
168 | 168 | }, $content); |
169 | 169 | return IteratorDirectory::wrap($names); |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | $tmpFile = \OCP\Files::tmpFile(); |
210 | 210 | } |
211 | 211 | $source = fopen($tmpFile, $mode); |
212 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) { |
|
212 | + return CallbackWrapper::wrap($source, null, null, function() use ($tmpFile, $fullPath) { |
|
213 | 213 | $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r')); |
214 | 214 | unlink($tmpFile); |
215 | 215 | }); |
@@ -159,7 +159,7 @@ |
||
159 | 159 | * Checks whether the given path is a part file |
160 | 160 | * |
161 | 161 | * @param string $path Path that may identify a .part file |
162 | - * @return string File path without .part extension |
|
162 | + * @return boolean File path without .part extension |
|
163 | 163 | * @note this is needed for reusing keys |
164 | 164 | */ |
165 | 165 | private function isPartFile($path) { |
@@ -30,172 +30,172 @@ |
||
30 | 30 | |
31 | 31 | class Quota extends Wrapper { |
32 | 32 | |
33 | - /** |
|
34 | - * @var int $quota |
|
35 | - */ |
|
36 | - protected $quota; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var string $sizeRoot |
|
40 | - */ |
|
41 | - protected $sizeRoot; |
|
42 | - |
|
43 | - /** |
|
44 | - * @param array $parameters |
|
45 | - */ |
|
46 | - public function __construct($parameters) { |
|
47 | - $this->storage = $parameters['storage']; |
|
48 | - $this->quota = $parameters['quota']; |
|
49 | - $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return int quota value |
|
54 | - */ |
|
55 | - public function getQuota() { |
|
56 | - return $this->quota; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param string $path |
|
61 | - * @param \OC\Files\Storage\Storage $storage |
|
62 | - */ |
|
63 | - protected function getSize($path, $storage = null) { |
|
64 | - if (is_null($storage)) { |
|
65 | - $cache = $this->getCache(); |
|
66 | - } else { |
|
67 | - $cache = $storage->getCache(); |
|
68 | - } |
|
69 | - $data = $cache->get($path); |
|
70 | - if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
71 | - return $data['size']; |
|
72 | - } else { |
|
73 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Get free space as limited by the quota |
|
79 | - * |
|
80 | - * @param string $path |
|
81 | - * @return int |
|
82 | - */ |
|
83 | - public function free_space($path) { |
|
84 | - if ($this->quota < 0) { |
|
85 | - return $this->storage->free_space($path); |
|
86 | - } else { |
|
87 | - $used = $this->getSize($this->sizeRoot); |
|
88 | - if ($used < 0) { |
|
89 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
90 | - } else { |
|
91 | - $free = $this->storage->free_space($path); |
|
92 | - $quotaFree = max($this->quota - $used, 0); |
|
93 | - // if free space is known |
|
94 | - if ($free >= 0) { |
|
95 | - $free = min($free, $quotaFree); |
|
96 | - } else { |
|
97 | - $free = $quotaFree; |
|
98 | - } |
|
99 | - return $free; |
|
100 | - } |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * see http://php.net/manual/en/function.file_put_contents.php |
|
106 | - * |
|
107 | - * @param string $path |
|
108 | - * @param string $data |
|
109 | - * @return bool |
|
110 | - */ |
|
111 | - public function file_put_contents($path, $data) { |
|
112 | - $free = $this->free_space(''); |
|
113 | - if ($free < 0 or strlen($data) < $free) { |
|
114 | - return $this->storage->file_put_contents($path, $data); |
|
115 | - } else { |
|
116 | - return false; |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * see http://php.net/manual/en/function.copy.php |
|
122 | - * |
|
123 | - * @param string $source |
|
124 | - * @param string $target |
|
125 | - * @return bool |
|
126 | - */ |
|
127 | - public function copy($source, $target) { |
|
128 | - $free = $this->free_space(''); |
|
129 | - if ($free < 0 or $this->getSize($source) < $free) { |
|
130 | - return $this->storage->copy($source, $target); |
|
131 | - } else { |
|
132 | - return false; |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * see http://php.net/manual/en/function.fopen.php |
|
138 | - * |
|
139 | - * @param string $path |
|
140 | - * @param string $mode |
|
141 | - * @return resource |
|
142 | - */ |
|
143 | - public function fopen($path, $mode) { |
|
144 | - $source = $this->storage->fopen($path, $mode); |
|
145 | - |
|
146 | - // don't apply quota for part files |
|
147 | - if (!$this->isPartFile($path)) { |
|
148 | - $free = $this->free_space(''); |
|
149 | - if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
150 | - // only apply quota for files, not metadata, trash or others |
|
151 | - if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
152 | - return \OC\Files\Stream\Quota::wrap($source, $free); |
|
153 | - } |
|
154 | - } |
|
155 | - } |
|
156 | - return $source; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Checks whether the given path is a part file |
|
161 | - * |
|
162 | - * @param string $path Path that may identify a .part file |
|
163 | - * @return string File path without .part extension |
|
164 | - * @note this is needed for reusing keys |
|
165 | - */ |
|
166 | - private function isPartFile($path) { |
|
167 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
168 | - |
|
169 | - return ($extension === 'part'); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @param \OCP\Files\Storage $sourceStorage |
|
174 | - * @param string $sourceInternalPath |
|
175 | - * @param string $targetInternalPath |
|
176 | - * @return bool |
|
177 | - */ |
|
178 | - public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
179 | - $free = $this->free_space(''); |
|
180 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
181 | - return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
182 | - } else { |
|
183 | - return false; |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * @param \OCP\Files\Storage $sourceStorage |
|
189 | - * @param string $sourceInternalPath |
|
190 | - * @param string $targetInternalPath |
|
191 | - * @return bool |
|
192 | - */ |
|
193 | - public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
194 | - $free = $this->free_space(''); |
|
195 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
196 | - return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
197 | - } else { |
|
198 | - return false; |
|
199 | - } |
|
200 | - } |
|
33 | + /** |
|
34 | + * @var int $quota |
|
35 | + */ |
|
36 | + protected $quota; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var string $sizeRoot |
|
40 | + */ |
|
41 | + protected $sizeRoot; |
|
42 | + |
|
43 | + /** |
|
44 | + * @param array $parameters |
|
45 | + */ |
|
46 | + public function __construct($parameters) { |
|
47 | + $this->storage = $parameters['storage']; |
|
48 | + $this->quota = $parameters['quota']; |
|
49 | + $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return int quota value |
|
54 | + */ |
|
55 | + public function getQuota() { |
|
56 | + return $this->quota; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param string $path |
|
61 | + * @param \OC\Files\Storage\Storage $storage |
|
62 | + */ |
|
63 | + protected function getSize($path, $storage = null) { |
|
64 | + if (is_null($storage)) { |
|
65 | + $cache = $this->getCache(); |
|
66 | + } else { |
|
67 | + $cache = $storage->getCache(); |
|
68 | + } |
|
69 | + $data = $cache->get($path); |
|
70 | + if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
71 | + return $data['size']; |
|
72 | + } else { |
|
73 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Get free space as limited by the quota |
|
79 | + * |
|
80 | + * @param string $path |
|
81 | + * @return int |
|
82 | + */ |
|
83 | + public function free_space($path) { |
|
84 | + if ($this->quota < 0) { |
|
85 | + return $this->storage->free_space($path); |
|
86 | + } else { |
|
87 | + $used = $this->getSize($this->sizeRoot); |
|
88 | + if ($used < 0) { |
|
89 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
90 | + } else { |
|
91 | + $free = $this->storage->free_space($path); |
|
92 | + $quotaFree = max($this->quota - $used, 0); |
|
93 | + // if free space is known |
|
94 | + if ($free >= 0) { |
|
95 | + $free = min($free, $quotaFree); |
|
96 | + } else { |
|
97 | + $free = $quotaFree; |
|
98 | + } |
|
99 | + return $free; |
|
100 | + } |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * see http://php.net/manual/en/function.file_put_contents.php |
|
106 | + * |
|
107 | + * @param string $path |
|
108 | + * @param string $data |
|
109 | + * @return bool |
|
110 | + */ |
|
111 | + public function file_put_contents($path, $data) { |
|
112 | + $free = $this->free_space(''); |
|
113 | + if ($free < 0 or strlen($data) < $free) { |
|
114 | + return $this->storage->file_put_contents($path, $data); |
|
115 | + } else { |
|
116 | + return false; |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * see http://php.net/manual/en/function.copy.php |
|
122 | + * |
|
123 | + * @param string $source |
|
124 | + * @param string $target |
|
125 | + * @return bool |
|
126 | + */ |
|
127 | + public function copy($source, $target) { |
|
128 | + $free = $this->free_space(''); |
|
129 | + if ($free < 0 or $this->getSize($source) < $free) { |
|
130 | + return $this->storage->copy($source, $target); |
|
131 | + } else { |
|
132 | + return false; |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * see http://php.net/manual/en/function.fopen.php |
|
138 | + * |
|
139 | + * @param string $path |
|
140 | + * @param string $mode |
|
141 | + * @return resource |
|
142 | + */ |
|
143 | + public function fopen($path, $mode) { |
|
144 | + $source = $this->storage->fopen($path, $mode); |
|
145 | + |
|
146 | + // don't apply quota for part files |
|
147 | + if (!$this->isPartFile($path)) { |
|
148 | + $free = $this->free_space(''); |
|
149 | + if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
150 | + // only apply quota for files, not metadata, trash or others |
|
151 | + if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
152 | + return \OC\Files\Stream\Quota::wrap($source, $free); |
|
153 | + } |
|
154 | + } |
|
155 | + } |
|
156 | + return $source; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Checks whether the given path is a part file |
|
161 | + * |
|
162 | + * @param string $path Path that may identify a .part file |
|
163 | + * @return string File path without .part extension |
|
164 | + * @note this is needed for reusing keys |
|
165 | + */ |
|
166 | + private function isPartFile($path) { |
|
167 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
168 | + |
|
169 | + return ($extension === 'part'); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @param \OCP\Files\Storage $sourceStorage |
|
174 | + * @param string $sourceInternalPath |
|
175 | + * @param string $targetInternalPath |
|
176 | + * @return bool |
|
177 | + */ |
|
178 | + public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
179 | + $free = $this->free_space(''); |
|
180 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
181 | + return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
182 | + } else { |
|
183 | + return false; |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * @param \OCP\Files\Storage $sourceStorage |
|
189 | + * @param string $sourceInternalPath |
|
190 | + * @param string $targetInternalPath |
|
191 | + * @return bool |
|
192 | + */ |
|
193 | + public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
194 | + $free = $this->free_space(''); |
|
195 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
196 | + return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
197 | + } else { |
|
198 | + return false; |
|
199 | + } |
|
200 | + } |
|
201 | 201 | } |
@@ -176,7 +176,7 @@ |
||
176 | 176 | * Returns an associative array with all translations |
177 | 177 | * |
178 | 178 | * Called by \OC_L10N_String |
179 | - * @return array |
|
179 | + * @return string[] |
|
180 | 180 | */ |
181 | 181 | public function getTranslations() { |
182 | 182 | return $this->translations; |
@@ -28,190 +28,190 @@ |
||
28 | 28 | |
29 | 29 | class L10N implements IL10N { |
30 | 30 | |
31 | - /** @var IFactory */ |
|
32 | - protected $factory; |
|
33 | - |
|
34 | - /** @var string App of this object */ |
|
35 | - protected $app; |
|
36 | - |
|
37 | - /** @var string Language of this object */ |
|
38 | - protected $lang; |
|
39 | - |
|
40 | - /** @var string Plural forms (string) */ |
|
41 | - private $pluralFormString = 'nplurals=2; plural=(n != 1);'; |
|
42 | - |
|
43 | - /** @var string Plural forms (function) */ |
|
44 | - private $pluralFormFunction = null; |
|
45 | - |
|
46 | - /** @var string[] */ |
|
47 | - private $translations = []; |
|
48 | - |
|
49 | - /** |
|
50 | - * @param IFactory $factory |
|
51 | - * @param string $app |
|
52 | - * @param string $lang |
|
53 | - * @param array $files |
|
54 | - */ |
|
55 | - public function __construct(IFactory $factory, $app, $lang, array $files) { |
|
56 | - $this->factory = $factory; |
|
57 | - $this->app = $app; |
|
58 | - $this->lang = $lang; |
|
59 | - |
|
60 | - $this->translations = []; |
|
61 | - foreach ($files as $languageFile) { |
|
62 | - $this->load($languageFile); |
|
63 | - } |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * The code (en, de, ...) of the language that is used for this instance |
|
68 | - * |
|
69 | - * @return string language |
|
70 | - */ |
|
71 | - public function getLanguageCode() { |
|
72 | - return $this->lang; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Translating |
|
77 | - * @param string $text The text we need a translation for |
|
78 | - * @param array $parameters default:array() Parameters for sprintf |
|
79 | - * @return string Translation or the same text |
|
80 | - * |
|
81 | - * Returns the translation. If no translation is found, $text will be |
|
82 | - * returned. |
|
83 | - */ |
|
84 | - public function t($text, $parameters = array()) { |
|
85 | - return (string) new \OC_L10N_String($this, $text, $parameters); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Translating |
|
90 | - * @param string $text_singular the string to translate for exactly one object |
|
91 | - * @param string $text_plural the string to translate for n objects |
|
92 | - * @param integer $count Number of objects |
|
93 | - * @param array $parameters default:array() Parameters for sprintf |
|
94 | - * @return string Translation or the same text |
|
95 | - * |
|
96 | - * Returns the translation. If no translation is found, $text will be |
|
97 | - * returned. %n will be replaced with the number of objects. |
|
98 | - * |
|
99 | - * The correct plural is determined by the plural_forms-function |
|
100 | - * provided by the po file. |
|
101 | - * |
|
102 | - */ |
|
103 | - public function n($text_singular, $text_plural, $count, $parameters = array()) { |
|
104 | - $identifier = "_${text_singular}_::_${text_plural}_"; |
|
105 | - if (isset($this->translations[$identifier])) { |
|
106 | - return (string) new \OC_L10N_String($this, $identifier, $parameters, $count); |
|
107 | - } else { |
|
108 | - if ($count === 1) { |
|
109 | - return (string) new \OC_L10N_String($this, $text_singular, $parameters, $count); |
|
110 | - } else { |
|
111 | - return (string) new \OC_L10N_String($this, $text_plural, $parameters, $count); |
|
112 | - } |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Localization |
|
118 | - * @param string $type Type of localization |
|
119 | - * @param \DateTime|int|string $data parameters for this localization |
|
120 | - * @param array $options |
|
121 | - * @return string|int|false |
|
122 | - * |
|
123 | - * Returns the localized data. |
|
124 | - * |
|
125 | - * Implemented types: |
|
126 | - * - date |
|
127 | - * - Creates a date |
|
128 | - * - params: timestamp (int/string) |
|
129 | - * - datetime |
|
130 | - * - Creates date and time |
|
131 | - * - params: timestamp (int/string) |
|
132 | - * - time |
|
133 | - * - Creates a time |
|
134 | - * - params: timestamp (int/string) |
|
135 | - * - firstday: Returns the first day of the week (0 sunday - 6 saturday) |
|
136 | - * - jsdate: Returns the short JS date format |
|
137 | - */ |
|
138 | - public function l($type, $data = null, $options = array()) { |
|
139 | - // Use the language of the instance |
|
140 | - $locale = $this->getLanguageCode(); |
|
141 | - if ($locale === 'sr@latin') { |
|
142 | - $locale = 'sr_latn'; |
|
143 | - } |
|
144 | - |
|
145 | - if ($type === 'firstday') { |
|
146 | - return (int) Calendar::getFirstWeekday($locale); |
|
147 | - } |
|
148 | - if ($type === 'jsdate') { |
|
149 | - return (string) Calendar::getDateFormat('short', $locale); |
|
150 | - } |
|
151 | - |
|
152 | - $value = new \DateTime(); |
|
153 | - if ($data instanceof \DateTime) { |
|
154 | - $value = $data; |
|
155 | - } else if (is_string($data) && !is_numeric($data)) { |
|
156 | - $data = strtotime($data); |
|
157 | - $value->setTimestamp($data); |
|
158 | - } else if ($data !== null) { |
|
159 | - $value->setTimestamp($data); |
|
160 | - } |
|
161 | - |
|
162 | - $options = array_merge(array('width' => 'long'), $options); |
|
163 | - $width = $options['width']; |
|
164 | - switch ($type) { |
|
165 | - case 'date': |
|
166 | - return (string) Calendar::formatDate($value, $width, $locale); |
|
167 | - case 'datetime': |
|
168 | - return (string) Calendar::formatDatetime($value, $width, $locale); |
|
169 | - case 'time': |
|
170 | - return (string) Calendar::formatTime($value, $width, $locale); |
|
171 | - default: |
|
172 | - return false; |
|
173 | - } |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Returns an associative array with all translations |
|
178 | - * |
|
179 | - * Called by \OC_L10N_String |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - public function getTranslations() { |
|
183 | - return $this->translations; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Returnsed function accepts the argument $n |
|
188 | - * |
|
189 | - * Called by \OC_L10N_String |
|
190 | - * @return string the plural form function |
|
191 | - */ |
|
192 | - public function getPluralFormFunction() { |
|
193 | - if (is_null($this->pluralFormFunction)) { |
|
194 | - $this->pluralFormFunction = $this->factory->createPluralFunction($this->pluralFormString); |
|
195 | - } |
|
196 | - return $this->pluralFormFunction; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @param $translationFile |
|
201 | - * @return bool |
|
202 | - */ |
|
203 | - protected function load($translationFile) { |
|
204 | - $json = json_decode(file_get_contents($translationFile), true); |
|
205 | - if (!is_array($json)) { |
|
206 | - $jsonError = json_last_error(); |
|
207 | - \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']); |
|
208 | - return false; |
|
209 | - } |
|
210 | - |
|
211 | - if (!empty($json['pluralForm'])) { |
|
212 | - $this->pluralFormString = $json['pluralForm']; |
|
213 | - } |
|
214 | - $this->translations = array_merge($this->translations, $json['translations']); |
|
215 | - return true; |
|
216 | - } |
|
31 | + /** @var IFactory */ |
|
32 | + protected $factory; |
|
33 | + |
|
34 | + /** @var string App of this object */ |
|
35 | + protected $app; |
|
36 | + |
|
37 | + /** @var string Language of this object */ |
|
38 | + protected $lang; |
|
39 | + |
|
40 | + /** @var string Plural forms (string) */ |
|
41 | + private $pluralFormString = 'nplurals=2; plural=(n != 1);'; |
|
42 | + |
|
43 | + /** @var string Plural forms (function) */ |
|
44 | + private $pluralFormFunction = null; |
|
45 | + |
|
46 | + /** @var string[] */ |
|
47 | + private $translations = []; |
|
48 | + |
|
49 | + /** |
|
50 | + * @param IFactory $factory |
|
51 | + * @param string $app |
|
52 | + * @param string $lang |
|
53 | + * @param array $files |
|
54 | + */ |
|
55 | + public function __construct(IFactory $factory, $app, $lang, array $files) { |
|
56 | + $this->factory = $factory; |
|
57 | + $this->app = $app; |
|
58 | + $this->lang = $lang; |
|
59 | + |
|
60 | + $this->translations = []; |
|
61 | + foreach ($files as $languageFile) { |
|
62 | + $this->load($languageFile); |
|
63 | + } |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * The code (en, de, ...) of the language that is used for this instance |
|
68 | + * |
|
69 | + * @return string language |
|
70 | + */ |
|
71 | + public function getLanguageCode() { |
|
72 | + return $this->lang; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Translating |
|
77 | + * @param string $text The text we need a translation for |
|
78 | + * @param array $parameters default:array() Parameters for sprintf |
|
79 | + * @return string Translation or the same text |
|
80 | + * |
|
81 | + * Returns the translation. If no translation is found, $text will be |
|
82 | + * returned. |
|
83 | + */ |
|
84 | + public function t($text, $parameters = array()) { |
|
85 | + return (string) new \OC_L10N_String($this, $text, $parameters); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Translating |
|
90 | + * @param string $text_singular the string to translate for exactly one object |
|
91 | + * @param string $text_plural the string to translate for n objects |
|
92 | + * @param integer $count Number of objects |
|
93 | + * @param array $parameters default:array() Parameters for sprintf |
|
94 | + * @return string Translation or the same text |
|
95 | + * |
|
96 | + * Returns the translation. If no translation is found, $text will be |
|
97 | + * returned. %n will be replaced with the number of objects. |
|
98 | + * |
|
99 | + * The correct plural is determined by the plural_forms-function |
|
100 | + * provided by the po file. |
|
101 | + * |
|
102 | + */ |
|
103 | + public function n($text_singular, $text_plural, $count, $parameters = array()) { |
|
104 | + $identifier = "_${text_singular}_::_${text_plural}_"; |
|
105 | + if (isset($this->translations[$identifier])) { |
|
106 | + return (string) new \OC_L10N_String($this, $identifier, $parameters, $count); |
|
107 | + } else { |
|
108 | + if ($count === 1) { |
|
109 | + return (string) new \OC_L10N_String($this, $text_singular, $parameters, $count); |
|
110 | + } else { |
|
111 | + return (string) new \OC_L10N_String($this, $text_plural, $parameters, $count); |
|
112 | + } |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Localization |
|
118 | + * @param string $type Type of localization |
|
119 | + * @param \DateTime|int|string $data parameters for this localization |
|
120 | + * @param array $options |
|
121 | + * @return string|int|false |
|
122 | + * |
|
123 | + * Returns the localized data. |
|
124 | + * |
|
125 | + * Implemented types: |
|
126 | + * - date |
|
127 | + * - Creates a date |
|
128 | + * - params: timestamp (int/string) |
|
129 | + * - datetime |
|
130 | + * - Creates date and time |
|
131 | + * - params: timestamp (int/string) |
|
132 | + * - time |
|
133 | + * - Creates a time |
|
134 | + * - params: timestamp (int/string) |
|
135 | + * - firstday: Returns the first day of the week (0 sunday - 6 saturday) |
|
136 | + * - jsdate: Returns the short JS date format |
|
137 | + */ |
|
138 | + public function l($type, $data = null, $options = array()) { |
|
139 | + // Use the language of the instance |
|
140 | + $locale = $this->getLanguageCode(); |
|
141 | + if ($locale === 'sr@latin') { |
|
142 | + $locale = 'sr_latn'; |
|
143 | + } |
|
144 | + |
|
145 | + if ($type === 'firstday') { |
|
146 | + return (int) Calendar::getFirstWeekday($locale); |
|
147 | + } |
|
148 | + if ($type === 'jsdate') { |
|
149 | + return (string) Calendar::getDateFormat('short', $locale); |
|
150 | + } |
|
151 | + |
|
152 | + $value = new \DateTime(); |
|
153 | + if ($data instanceof \DateTime) { |
|
154 | + $value = $data; |
|
155 | + } else if (is_string($data) && !is_numeric($data)) { |
|
156 | + $data = strtotime($data); |
|
157 | + $value->setTimestamp($data); |
|
158 | + } else if ($data !== null) { |
|
159 | + $value->setTimestamp($data); |
|
160 | + } |
|
161 | + |
|
162 | + $options = array_merge(array('width' => 'long'), $options); |
|
163 | + $width = $options['width']; |
|
164 | + switch ($type) { |
|
165 | + case 'date': |
|
166 | + return (string) Calendar::formatDate($value, $width, $locale); |
|
167 | + case 'datetime': |
|
168 | + return (string) Calendar::formatDatetime($value, $width, $locale); |
|
169 | + case 'time': |
|
170 | + return (string) Calendar::formatTime($value, $width, $locale); |
|
171 | + default: |
|
172 | + return false; |
|
173 | + } |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Returns an associative array with all translations |
|
178 | + * |
|
179 | + * Called by \OC_L10N_String |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + public function getTranslations() { |
|
183 | + return $this->translations; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Returnsed function accepts the argument $n |
|
188 | + * |
|
189 | + * Called by \OC_L10N_String |
|
190 | + * @return string the plural form function |
|
191 | + */ |
|
192 | + public function getPluralFormFunction() { |
|
193 | + if (is_null($this->pluralFormFunction)) { |
|
194 | + $this->pluralFormFunction = $this->factory->createPluralFunction($this->pluralFormString); |
|
195 | + } |
|
196 | + return $this->pluralFormFunction; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @param $translationFile |
|
201 | + * @return bool |
|
202 | + */ |
|
203 | + protected function load($translationFile) { |
|
204 | + $json = json_decode(file_get_contents($translationFile), true); |
|
205 | + if (!is_array($json)) { |
|
206 | + $jsonError = json_last_error(); |
|
207 | + \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']); |
|
208 | + return false; |
|
209 | + } |
|
210 | + |
|
211 | + if (!empty($json['pluralForm'])) { |
|
212 | + $this->pluralFormString = $json['pluralForm']; |
|
213 | + } |
|
214 | + $this->translations = array_merge($this->translations, $json['translations']); |
|
215 | + return true; |
|
216 | + } |
|
217 | 217 | } |
@@ -331,7 +331,7 @@ |
||
331 | 331 | |
332 | 332 | /** |
333 | 333 | * http basic auth |
334 | - * @return string|false (username, or false on failure) |
|
334 | + * @return string (username, or false on failure) |
|
335 | 335 | */ |
336 | 336 | private static function loginUser() { |
337 | 337 | if(self::$isLoggedIn === true) { |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | $requirements = array()) { |
93 | 93 | $name = strtolower($method).$url; |
94 | 94 | $name = str_replace(array('/', '{', '}'), '_', $name); |
95 | - if(!isset(self::$actions[$name])) { |
|
95 | + if (!isset(self::$actions[$name])) { |
|
96 | 96 | $oldCollection = OC::$server->getRouter()->getCurrentCollection(); |
97 | 97 | OC::$server->getRouter()->useCollection('ocs'); |
98 | 98 | OC::$server->getRouter()->create($name, $url) |
@@ -115,17 +115,17 @@ discard block |
||
115 | 115 | $method = $request->getMethod(); |
116 | 116 | |
117 | 117 | // Prepare the request variables |
118 | - if($method === 'PUT') { |
|
118 | + if ($method === 'PUT') { |
|
119 | 119 | $parameters['_put'] = $request->getParams(); |
120 | - } else if($method === 'DELETE') { |
|
120 | + } else if ($method === 'DELETE') { |
|
121 | 121 | $parameters['_delete'] = $request->getParams(); |
122 | 122 | } |
123 | 123 | $name = $parameters['_route']; |
124 | 124 | // Foreach registered action |
125 | 125 | $responses = array(); |
126 | - foreach(self::$actions[$name] as $action) { |
|
126 | + foreach (self::$actions[$name] as $action) { |
|
127 | 127 | // Check authentication and availability |
128 | - if(!self::isAuthorised($action)) { |
|
128 | + if (!self::isAuthorised($action)) { |
|
129 | 129 | $responses[] = array( |
130 | 130 | 'app' => $action['app'], |
131 | 131 | 'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'), |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | ); |
134 | 134 | continue; |
135 | 135 | } |
136 | - if(!is_callable($action['action'])) { |
|
136 | + if (!is_callable($action['action'])) { |
|
137 | 137 | $responses[] = array( |
138 | 138 | 'app' => $action['app'], |
139 | 139 | 'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'), |
@@ -173,15 +173,15 @@ discard block |
||
173 | 173 | 'failed' => array(), |
174 | 174 | ); |
175 | 175 | |
176 | - foreach($responses as $response) { |
|
177 | - if($response['shipped'] || ($response['app'] === 'core')) { |
|
178 | - if($response['response']->succeeded()) { |
|
176 | + foreach ($responses as $response) { |
|
177 | + if ($response['shipped'] || ($response['app'] === 'core')) { |
|
178 | + if ($response['response']->succeeded()) { |
|
179 | 179 | $shipped['succeeded'][$response['app']] = $response; |
180 | 180 | } else { |
181 | 181 | $shipped['failed'][$response['app']] = $response; |
182 | 182 | } |
183 | 183 | } else { |
184 | - if($response['response']->succeeded()) { |
|
184 | + if ($response['response']->succeeded()) { |
|
185 | 185 | $thirdparty['succeeded'][$response['app']] = $response; |
186 | 186 | } else { |
187 | 187 | $thirdparty['failed'][$response['app']] = $response; |
@@ -190,14 +190,14 @@ discard block |
||
190 | 190 | } |
191 | 191 | |
192 | 192 | // Remove any error responses if there is one shipped response that succeeded |
193 | - if(!empty($shipped['failed'])) { |
|
193 | + if (!empty($shipped['failed'])) { |
|
194 | 194 | // Which shipped response do we use if they all failed? |
195 | 195 | // They may have failed for different reasons (different status codes) |
196 | 196 | // Which response code should we return? |
197 | 197 | // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR |
198 | 198 | // Merge failed responses if more than one |
199 | 199 | $data = array(); |
200 | - foreach($shipped['failed'] as $failure) { |
|
200 | + foreach ($shipped['failed'] as $failure) { |
|
201 | 201 | $data = array_merge_recursive($data, $failure['response']->getData()); |
202 | 202 | } |
203 | 203 | $picked = reset($shipped['failed']); |
@@ -206,12 +206,12 @@ discard block |
||
206 | 206 | $headers = $picked['response']->getHeaders(); |
207 | 207 | $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
208 | 208 | return $response; |
209 | - } elseif(!empty($shipped['succeeded'])) { |
|
209 | + } elseif (!empty($shipped['succeeded'])) { |
|
210 | 210 | $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); |
211 | - } elseif(!empty($thirdparty['failed'])) { |
|
211 | + } elseif (!empty($thirdparty['failed'])) { |
|
212 | 212 | // Merge failed responses if more than one |
213 | 213 | $data = array(); |
214 | - foreach($thirdparty['failed'] as $failure) { |
|
214 | + foreach ($thirdparty['failed'] as $failure) { |
|
215 | 215 | $data = array_merge_recursive($data, $failure['response']->getData()); |
216 | 216 | } |
217 | 217 | $picked = reset($thirdparty['failed']); |
@@ -228,8 +228,8 @@ discard block |
||
228 | 228 | $codes = []; |
229 | 229 | $header = []; |
230 | 230 | |
231 | - foreach($responses as $response) { |
|
232 | - if($response['shipped']) { |
|
231 | + foreach ($responses as $response) { |
|
232 | + if ($response['shipped']) { |
|
233 | 233 | $data = array_merge_recursive($response['response']->getData(), $data); |
234 | 234 | } else { |
235 | 235 | $data = array_merge_recursive($data, $response['response']->getData()); |
@@ -242,8 +242,8 @@ discard block |
||
242 | 242 | // Use any non 100 status codes |
243 | 243 | $statusCode = 100; |
244 | 244 | $statusMessage = null; |
245 | - foreach($codes as $code) { |
|
246 | - if($code['code'] != 100) { |
|
245 | + foreach ($codes as $code) { |
|
246 | + if ($code['code'] != 100) { |
|
247 | 247 | $statusCode = $code['code']; |
248 | 248 | $statusMessage = $code['meta']['message']; |
249 | 249 | break; |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | */ |
261 | 261 | private static function isAuthorised($action) { |
262 | 262 | $level = $action['authlevel']; |
263 | - switch($level) { |
|
263 | + switch ($level) { |
|
264 | 264 | case API::GUEST_AUTH: |
265 | 265 | // Anyone can access |
266 | 266 | return true; |
@@ -270,16 +270,16 @@ discard block |
||
270 | 270 | case API::SUBADMIN_AUTH: |
271 | 271 | // Check for subadmin |
272 | 272 | $user = self::loginUser(); |
273 | - if(!$user) { |
|
273 | + if (!$user) { |
|
274 | 274 | return false; |
275 | 275 | } else { |
276 | 276 | $userObject = \OC::$server->getUserSession()->getUser(); |
277 | - if($userObject === null) { |
|
277 | + if ($userObject === null) { |
|
278 | 278 | return false; |
279 | 279 | } |
280 | 280 | $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
281 | 281 | $admin = OC_User::isAdminUser($user); |
282 | - if($isSubAdmin || $admin) { |
|
282 | + if ($isSubAdmin || $admin) { |
|
283 | 283 | return true; |
284 | 284 | } else { |
285 | 285 | return false; |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | case API::ADMIN_AUTH: |
289 | 289 | // Check for admin |
290 | 290 | $user = self::loginUser(); |
291 | - if(!$user) { |
|
291 | + if (!$user) { |
|
292 | 292 | return false; |
293 | 293 | } else { |
294 | 294 | return OC_User::isAdminUser($user); |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * @return string|false (username, or false on failure) |
305 | 305 | */ |
306 | 306 | private static function loginUser() { |
307 | - if(self::$isLoggedIn === true) { |
|
307 | + if (self::$isLoggedIn === true) { |
|
308 | 308 | return \OC_User::getUser(); |
309 | 309 | } |
310 | 310 | |
@@ -358,13 +358,13 @@ discard block |
||
358 | 358 | * @param OC_OCS_Result $result |
359 | 359 | * @param string $format the format xml|json |
360 | 360 | */ |
361 | - public static function respond($result, $format='xml') { |
|
361 | + public static function respond($result, $format = 'xml') { |
|
362 | 362 | $request = \OC::$server->getRequest(); |
363 | 363 | |
364 | 364 | // Send 401 headers if unauthorised |
365 | - if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
365 | + if ($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
366 | 366 | // If request comes from JS return dummy auth request |
367 | - if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
367 | + if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
368 | 368 | header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
369 | 369 | } else { |
370 | 370 | header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
@@ -372,8 +372,8 @@ discard block |
||
372 | 372 | header('HTTP/1.0 401 Unauthorized'); |
373 | 373 | } |
374 | 374 | |
375 | - foreach($result->getHeaders() as $name => $value) { |
|
376 | - header($name . ': ' . $value); |
|
375 | + foreach ($result->getHeaders() as $name => $value) { |
|
376 | + header($name.': '.$value); |
|
377 | 377 | } |
378 | 378 | |
379 | 379 | $meta = $result->getMeta(); |
@@ -395,14 +395,14 @@ discard block |
||
395 | 395 | * @param XMLWriter $writer |
396 | 396 | */ |
397 | 397 | private static function toXML($array, $writer) { |
398 | - foreach($array as $k => $v) { |
|
398 | + foreach ($array as $k => $v) { |
|
399 | 399 | if ($k[0] === '@') { |
400 | 400 | $writer->writeAttribute(substr($k, 1), $v); |
401 | 401 | continue; |
402 | 402 | } else if (is_numeric($k)) { |
403 | 403 | $k = 'element'; |
404 | 404 | } |
405 | - if(is_array($v)) { |
|
405 | + if (is_array($v)) { |
|
406 | 406 | $writer->startElement($k); |
407 | 407 | self::toXML($v, $writer); |
408 | 408 | $writer->endElement(); |
@@ -37,462 +37,462 @@ |
||
37 | 37 | |
38 | 38 | class OC_API { |
39 | 39 | |
40 | - /** |
|
41 | - * API authentication levels |
|
42 | - */ |
|
43 | - |
|
44 | - /** @deprecated Use \OCP\API::GUEST_AUTH instead */ |
|
45 | - const GUEST_AUTH = 0; |
|
46 | - |
|
47 | - /** @deprecated Use \OCP\API::USER_AUTH instead */ |
|
48 | - const USER_AUTH = 1; |
|
49 | - |
|
50 | - /** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */ |
|
51 | - const SUBADMIN_AUTH = 2; |
|
52 | - |
|
53 | - /** @deprecated Use \OCP\API::ADMIN_AUTH instead */ |
|
54 | - const ADMIN_AUTH = 3; |
|
55 | - |
|
56 | - /** |
|
57 | - * API Response Codes |
|
58 | - */ |
|
59 | - |
|
60 | - /** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */ |
|
61 | - const RESPOND_UNAUTHORISED = 997; |
|
62 | - |
|
63 | - /** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */ |
|
64 | - const RESPOND_SERVER_ERROR = 996; |
|
65 | - |
|
66 | - /** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */ |
|
67 | - const RESPOND_NOT_FOUND = 998; |
|
68 | - |
|
69 | - /** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */ |
|
70 | - const RESPOND_UNKNOWN_ERROR = 999; |
|
71 | - |
|
72 | - /** |
|
73 | - * api actions |
|
74 | - */ |
|
75 | - protected static $actions = array(); |
|
76 | - private static $logoutRequired = false; |
|
77 | - private static $isLoggedIn = false; |
|
78 | - |
|
79 | - /** |
|
80 | - * registers an api call |
|
81 | - * @param string $method the http method |
|
82 | - * @param string $url the url to match |
|
83 | - * @param callable $action the function to run |
|
84 | - * @param string $app the id of the app registering the call |
|
85 | - * @param int $authLevel the level of authentication required for the call |
|
86 | - * @param array $defaults |
|
87 | - * @param array $requirements |
|
88 | - */ |
|
89 | - public static function register($method, $url, $action, $app, |
|
90 | - $authLevel = API::USER_AUTH, |
|
91 | - $defaults = array(), |
|
92 | - $requirements = array()) { |
|
93 | - $name = strtolower($method).$url; |
|
94 | - $name = str_replace(array('/', '{', '}'), '_', $name); |
|
95 | - if(!isset(self::$actions[$name])) { |
|
96 | - $oldCollection = OC::$server->getRouter()->getCurrentCollection(); |
|
97 | - OC::$server->getRouter()->useCollection('ocs'); |
|
98 | - OC::$server->getRouter()->create($name, $url) |
|
99 | - ->method($method) |
|
100 | - ->defaults($defaults) |
|
101 | - ->requirements($requirements) |
|
102 | - ->action('OC_API', 'call'); |
|
103 | - self::$actions[$name] = array(); |
|
104 | - OC::$server->getRouter()->useCollection($oldCollection); |
|
105 | - } |
|
106 | - self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * handles an api call |
|
111 | - * @param array $parameters |
|
112 | - */ |
|
113 | - public static function call($parameters) { |
|
114 | - $request = \OC::$server->getRequest(); |
|
115 | - $method = $request->getMethod(); |
|
116 | - |
|
117 | - // Prepare the request variables |
|
118 | - if($method === 'PUT') { |
|
119 | - $parameters['_put'] = $request->getParams(); |
|
120 | - } else if($method === 'DELETE') { |
|
121 | - $parameters['_delete'] = $request->getParams(); |
|
122 | - } |
|
123 | - $name = $parameters['_route']; |
|
124 | - // Foreach registered action |
|
125 | - $responses = array(); |
|
126 | - foreach(self::$actions[$name] as $action) { |
|
127 | - // Check authentication and availability |
|
128 | - if(!self::isAuthorised($action)) { |
|
129 | - $responses[] = array( |
|
130 | - 'app' => $action['app'], |
|
131 | - 'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'), |
|
132 | - 'shipped' => OC_App::isShipped($action['app']), |
|
133 | - ); |
|
134 | - continue; |
|
135 | - } |
|
136 | - if(!is_callable($action['action'])) { |
|
137 | - $responses[] = array( |
|
138 | - 'app' => $action['app'], |
|
139 | - 'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'), |
|
140 | - 'shipped' => OC_App::isShipped($action['app']), |
|
141 | - ); |
|
142 | - continue; |
|
143 | - } |
|
144 | - // Run the action |
|
145 | - $responses[] = array( |
|
146 | - 'app' => $action['app'], |
|
147 | - 'response' => call_user_func($action['action'], $parameters), |
|
148 | - 'shipped' => OC_App::isShipped($action['app']), |
|
149 | - ); |
|
150 | - } |
|
151 | - $response = self::mergeResponses($responses); |
|
152 | - $format = self::requestedFormat(); |
|
153 | - if (self::$logoutRequired) { |
|
154 | - \OC::$server->getUserSession()->logout(); |
|
155 | - } |
|
156 | - |
|
157 | - self::respond($response, $format); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * merge the returned result objects into one response |
|
162 | - * @param array $responses |
|
163 | - * @return OC_OCS_Result |
|
164 | - */ |
|
165 | - public static function mergeResponses($responses) { |
|
166 | - // Sort into shipped and third-party |
|
167 | - $shipped = array( |
|
168 | - 'succeeded' => array(), |
|
169 | - 'failed' => array(), |
|
170 | - ); |
|
171 | - $thirdparty = array( |
|
172 | - 'succeeded' => array(), |
|
173 | - 'failed' => array(), |
|
174 | - ); |
|
175 | - |
|
176 | - foreach($responses as $response) { |
|
177 | - if($response['shipped'] || ($response['app'] === 'core')) { |
|
178 | - if($response['response']->succeeded()) { |
|
179 | - $shipped['succeeded'][$response['app']] = $response; |
|
180 | - } else { |
|
181 | - $shipped['failed'][$response['app']] = $response; |
|
182 | - } |
|
183 | - } else { |
|
184 | - if($response['response']->succeeded()) { |
|
185 | - $thirdparty['succeeded'][$response['app']] = $response; |
|
186 | - } else { |
|
187 | - $thirdparty['failed'][$response['app']] = $response; |
|
188 | - } |
|
189 | - } |
|
190 | - } |
|
191 | - |
|
192 | - // Remove any error responses if there is one shipped response that succeeded |
|
193 | - if(!empty($shipped['failed'])) { |
|
194 | - // Which shipped response do we use if they all failed? |
|
195 | - // They may have failed for different reasons (different status codes) |
|
196 | - // Which response code should we return? |
|
197 | - // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR |
|
198 | - // Merge failed responses if more than one |
|
199 | - $data = array(); |
|
200 | - foreach($shipped['failed'] as $failure) { |
|
201 | - $data = array_merge_recursive($data, $failure['response']->getData()); |
|
202 | - } |
|
203 | - $picked = reset($shipped['failed']); |
|
204 | - $code = $picked['response']->getStatusCode(); |
|
205 | - $meta = $picked['response']->getMeta(); |
|
206 | - $headers = $picked['response']->getHeaders(); |
|
207 | - $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
|
208 | - return $response; |
|
209 | - } elseif(!empty($shipped['succeeded'])) { |
|
210 | - $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); |
|
211 | - } elseif(!empty($thirdparty['failed'])) { |
|
212 | - // Merge failed responses if more than one |
|
213 | - $data = array(); |
|
214 | - foreach($thirdparty['failed'] as $failure) { |
|
215 | - $data = array_merge_recursive($data, $failure['response']->getData()); |
|
216 | - } |
|
217 | - $picked = reset($thirdparty['failed']); |
|
218 | - $code = $picked['response']->getStatusCode(); |
|
219 | - $meta = $picked['response']->getMeta(); |
|
220 | - $headers = $picked['response']->getHeaders(); |
|
221 | - $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
|
222 | - return $response; |
|
223 | - } else { |
|
224 | - $responses = $thirdparty['succeeded']; |
|
225 | - } |
|
226 | - // Merge the successful responses |
|
227 | - $data = []; |
|
228 | - $codes = []; |
|
229 | - $header = []; |
|
230 | - |
|
231 | - foreach($responses as $response) { |
|
232 | - if($response['shipped']) { |
|
233 | - $data = array_merge_recursive($response['response']->getData(), $data); |
|
234 | - } else { |
|
235 | - $data = array_merge_recursive($data, $response['response']->getData()); |
|
236 | - } |
|
237 | - $header = array_merge_recursive($header, $response['response']->getHeaders()); |
|
238 | - $codes[] = ['code' => $response['response']->getStatusCode(), |
|
239 | - 'meta' => $response['response']->getMeta()]; |
|
240 | - } |
|
241 | - |
|
242 | - // Use any non 100 status codes |
|
243 | - $statusCode = 100; |
|
244 | - $statusMessage = null; |
|
245 | - foreach($codes as $code) { |
|
246 | - if($code['code'] != 100) { |
|
247 | - $statusCode = $code['code']; |
|
248 | - $statusMessage = $code['meta']['message']; |
|
249 | - break; |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - return new OC_OCS_Result($data, $statusCode, $statusMessage, $header); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * authenticate the api call |
|
258 | - * @param array $action the action details as supplied to OC_API::register() |
|
259 | - * @return bool |
|
260 | - */ |
|
261 | - private static function isAuthorised($action) { |
|
262 | - $level = $action['authlevel']; |
|
263 | - switch($level) { |
|
264 | - case API::GUEST_AUTH: |
|
265 | - // Anyone can access |
|
266 | - return true; |
|
267 | - case API::USER_AUTH: |
|
268 | - // User required |
|
269 | - return self::loginUser(); |
|
270 | - case API::SUBADMIN_AUTH: |
|
271 | - // Check for subadmin |
|
272 | - $user = self::loginUser(); |
|
273 | - if(!$user) { |
|
274 | - return false; |
|
275 | - } else { |
|
276 | - $userObject = \OC::$server->getUserSession()->getUser(); |
|
277 | - if($userObject === null) { |
|
278 | - return false; |
|
279 | - } |
|
280 | - $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
281 | - $admin = OC_User::isAdminUser($user); |
|
282 | - if($isSubAdmin || $admin) { |
|
283 | - return true; |
|
284 | - } else { |
|
285 | - return false; |
|
286 | - } |
|
287 | - } |
|
288 | - case API::ADMIN_AUTH: |
|
289 | - // Check for admin |
|
290 | - $user = self::loginUser(); |
|
291 | - if(!$user) { |
|
292 | - return false; |
|
293 | - } else { |
|
294 | - return OC_User::isAdminUser($user); |
|
295 | - } |
|
296 | - default: |
|
297 | - // oops looks like invalid level supplied |
|
298 | - return false; |
|
299 | - } |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * http basic auth |
|
304 | - * @return string|false (username, or false on failure) |
|
305 | - */ |
|
306 | - private static function loginUser() { |
|
307 | - if(self::$isLoggedIn === true) { |
|
308 | - return \OC_User::getUser(); |
|
309 | - } |
|
310 | - |
|
311 | - // reuse existing login |
|
312 | - $loggedIn = \OC::$server->getUserSession()->isLoggedIn(); |
|
313 | - if ($loggedIn === true) { |
|
314 | - if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
315 | - // Do not allow access to OCS until the 2FA challenge was solved successfully |
|
316 | - return false; |
|
317 | - } |
|
318 | - $ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false; |
|
319 | - if ($ocsApiRequest) { |
|
320 | - |
|
321 | - // initialize the user's filesystem |
|
322 | - \OC_Util::setupFS(\OC_User::getUser()); |
|
323 | - self::$isLoggedIn = true; |
|
324 | - |
|
325 | - return OC_User::getUser(); |
|
326 | - } |
|
327 | - return false; |
|
328 | - } |
|
329 | - |
|
330 | - // basic auth - because OC_User::login will create a new session we shall only try to login |
|
331 | - // if user and pass are set |
|
332 | - $userSession = \OC::$server->getUserSession(); |
|
333 | - $request = \OC::$server->getRequest(); |
|
334 | - try { |
|
335 | - if ($userSession->tryTokenLogin($request) |
|
336 | - || $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) { |
|
337 | - self::$logoutRequired = true; |
|
338 | - } else { |
|
339 | - return false; |
|
340 | - } |
|
341 | - // initialize the user's filesystem |
|
342 | - \OC_Util::setupFS(\OC_User::getUser()); |
|
343 | - self::$isLoggedIn = true; |
|
344 | - |
|
345 | - return \OC_User::getUser(); |
|
346 | - } catch (\OC\User\LoginException $e) { |
|
347 | - return false; |
|
348 | - } |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * respond to a call |
|
353 | - * @param OC_OCS_Result $result |
|
354 | - * @param string $format the format xml|json |
|
355 | - */ |
|
356 | - public static function respond($result, $format='xml') { |
|
357 | - $request = \OC::$server->getRequest(); |
|
358 | - |
|
359 | - // Send 401 headers if unauthorised |
|
360 | - if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
361 | - // If request comes from JS return dummy auth request |
|
362 | - if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
363 | - header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
|
364 | - } else { |
|
365 | - header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
|
366 | - } |
|
367 | - header('HTTP/1.0 401 Unauthorized'); |
|
368 | - } |
|
369 | - |
|
370 | - foreach($result->getHeaders() as $name => $value) { |
|
371 | - header($name . ': ' . $value); |
|
372 | - } |
|
373 | - |
|
374 | - $meta = $result->getMeta(); |
|
375 | - $data = $result->getData(); |
|
376 | - if (self::isV2($request)) { |
|
377 | - $statusCode = self::mapStatusCodes($result->getStatusCode()); |
|
378 | - if (!is_null($statusCode)) { |
|
379 | - $meta['statuscode'] = $statusCode; |
|
380 | - OC_Response::setStatus($statusCode); |
|
381 | - } |
|
382 | - } |
|
383 | - |
|
384 | - self::setContentType($format); |
|
385 | - $body = self::renderResult($format, $meta, $data); |
|
386 | - echo $body; |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * @param XMLWriter $writer |
|
391 | - */ |
|
392 | - private static function toXML($array, $writer) { |
|
393 | - foreach($array as $k => $v) { |
|
394 | - if ($k[0] === '@') { |
|
395 | - $writer->writeAttribute(substr($k, 1), $v); |
|
396 | - continue; |
|
397 | - } else if (is_numeric($k)) { |
|
398 | - $k = 'element'; |
|
399 | - } |
|
400 | - if(is_array($v)) { |
|
401 | - $writer->startElement($k); |
|
402 | - self::toXML($v, $writer); |
|
403 | - $writer->endElement(); |
|
404 | - } else { |
|
405 | - $writer->writeElement($k, $v); |
|
406 | - } |
|
407 | - } |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * @return string |
|
412 | - */ |
|
413 | - public static function requestedFormat() { |
|
414 | - $formats = array('json', 'xml'); |
|
415 | - |
|
416 | - $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; |
|
417 | - return $format; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Based on the requested format the response content type is set |
|
422 | - * @param string $format |
|
423 | - */ |
|
424 | - public static function setContentType($format = null) { |
|
425 | - $format = is_null($format) ? self::requestedFormat() : $format; |
|
426 | - if ($format === 'xml') { |
|
427 | - header('Content-type: text/xml; charset=UTF-8'); |
|
428 | - return; |
|
429 | - } |
|
430 | - |
|
431 | - if ($format === 'json') { |
|
432 | - header('Content-Type: application/json; charset=utf-8'); |
|
433 | - return; |
|
434 | - } |
|
435 | - |
|
436 | - header('Content-Type: application/octet-stream; charset=utf-8'); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * @param \OCP\IRequest $request |
|
441 | - * @return bool |
|
442 | - */ |
|
443 | - protected static function isV2(\OCP\IRequest $request) { |
|
444 | - $script = $request->getScriptName(); |
|
445 | - |
|
446 | - return substr($script, -11) === '/ocs/v2.php'; |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * @param integer $sc |
|
451 | - * @return int |
|
452 | - */ |
|
453 | - public static function mapStatusCodes($sc) { |
|
454 | - switch ($sc) { |
|
455 | - case API::RESPOND_NOT_FOUND: |
|
456 | - return Http::STATUS_NOT_FOUND; |
|
457 | - case API::RESPOND_SERVER_ERROR: |
|
458 | - return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
459 | - case API::RESPOND_UNKNOWN_ERROR: |
|
460 | - return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
461 | - case API::RESPOND_UNAUTHORISED: |
|
462 | - // already handled for v1 |
|
463 | - return null; |
|
464 | - case 100: |
|
465 | - return Http::STATUS_OK; |
|
466 | - } |
|
467 | - // any 2xx, 4xx and 5xx will be used as is |
|
468 | - if ($sc >= 200 && $sc < 600) { |
|
469 | - return $sc; |
|
470 | - } |
|
471 | - |
|
472 | - return Http::STATUS_BAD_REQUEST; |
|
473 | - } |
|
474 | - |
|
475 | - /** |
|
476 | - * @param string $format |
|
477 | - * @return string |
|
478 | - */ |
|
479 | - public static function renderResult($format, $meta, $data) { |
|
480 | - $response = array( |
|
481 | - 'ocs' => array( |
|
482 | - 'meta' => $meta, |
|
483 | - 'data' => $data, |
|
484 | - ), |
|
485 | - ); |
|
486 | - if ($format == 'json') { |
|
487 | - return OC_JSON::encode($response); |
|
488 | - } |
|
489 | - |
|
490 | - $writer = new XMLWriter(); |
|
491 | - $writer->openMemory(); |
|
492 | - $writer->setIndent(true); |
|
493 | - $writer->startDocument(); |
|
494 | - self::toXML($response, $writer); |
|
495 | - $writer->endDocument(); |
|
496 | - return $writer->outputMemory(true); |
|
497 | - } |
|
40 | + /** |
|
41 | + * API authentication levels |
|
42 | + */ |
|
43 | + |
|
44 | + /** @deprecated Use \OCP\API::GUEST_AUTH instead */ |
|
45 | + const GUEST_AUTH = 0; |
|
46 | + |
|
47 | + /** @deprecated Use \OCP\API::USER_AUTH instead */ |
|
48 | + const USER_AUTH = 1; |
|
49 | + |
|
50 | + /** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */ |
|
51 | + const SUBADMIN_AUTH = 2; |
|
52 | + |
|
53 | + /** @deprecated Use \OCP\API::ADMIN_AUTH instead */ |
|
54 | + const ADMIN_AUTH = 3; |
|
55 | + |
|
56 | + /** |
|
57 | + * API Response Codes |
|
58 | + */ |
|
59 | + |
|
60 | + /** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */ |
|
61 | + const RESPOND_UNAUTHORISED = 997; |
|
62 | + |
|
63 | + /** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */ |
|
64 | + const RESPOND_SERVER_ERROR = 996; |
|
65 | + |
|
66 | + /** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */ |
|
67 | + const RESPOND_NOT_FOUND = 998; |
|
68 | + |
|
69 | + /** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */ |
|
70 | + const RESPOND_UNKNOWN_ERROR = 999; |
|
71 | + |
|
72 | + /** |
|
73 | + * api actions |
|
74 | + */ |
|
75 | + protected static $actions = array(); |
|
76 | + private static $logoutRequired = false; |
|
77 | + private static $isLoggedIn = false; |
|
78 | + |
|
79 | + /** |
|
80 | + * registers an api call |
|
81 | + * @param string $method the http method |
|
82 | + * @param string $url the url to match |
|
83 | + * @param callable $action the function to run |
|
84 | + * @param string $app the id of the app registering the call |
|
85 | + * @param int $authLevel the level of authentication required for the call |
|
86 | + * @param array $defaults |
|
87 | + * @param array $requirements |
|
88 | + */ |
|
89 | + public static function register($method, $url, $action, $app, |
|
90 | + $authLevel = API::USER_AUTH, |
|
91 | + $defaults = array(), |
|
92 | + $requirements = array()) { |
|
93 | + $name = strtolower($method).$url; |
|
94 | + $name = str_replace(array('/', '{', '}'), '_', $name); |
|
95 | + if(!isset(self::$actions[$name])) { |
|
96 | + $oldCollection = OC::$server->getRouter()->getCurrentCollection(); |
|
97 | + OC::$server->getRouter()->useCollection('ocs'); |
|
98 | + OC::$server->getRouter()->create($name, $url) |
|
99 | + ->method($method) |
|
100 | + ->defaults($defaults) |
|
101 | + ->requirements($requirements) |
|
102 | + ->action('OC_API', 'call'); |
|
103 | + self::$actions[$name] = array(); |
|
104 | + OC::$server->getRouter()->useCollection($oldCollection); |
|
105 | + } |
|
106 | + self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * handles an api call |
|
111 | + * @param array $parameters |
|
112 | + */ |
|
113 | + public static function call($parameters) { |
|
114 | + $request = \OC::$server->getRequest(); |
|
115 | + $method = $request->getMethod(); |
|
116 | + |
|
117 | + // Prepare the request variables |
|
118 | + if($method === 'PUT') { |
|
119 | + $parameters['_put'] = $request->getParams(); |
|
120 | + } else if($method === 'DELETE') { |
|
121 | + $parameters['_delete'] = $request->getParams(); |
|
122 | + } |
|
123 | + $name = $parameters['_route']; |
|
124 | + // Foreach registered action |
|
125 | + $responses = array(); |
|
126 | + foreach(self::$actions[$name] as $action) { |
|
127 | + // Check authentication and availability |
|
128 | + if(!self::isAuthorised($action)) { |
|
129 | + $responses[] = array( |
|
130 | + 'app' => $action['app'], |
|
131 | + 'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'), |
|
132 | + 'shipped' => OC_App::isShipped($action['app']), |
|
133 | + ); |
|
134 | + continue; |
|
135 | + } |
|
136 | + if(!is_callable($action['action'])) { |
|
137 | + $responses[] = array( |
|
138 | + 'app' => $action['app'], |
|
139 | + 'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'), |
|
140 | + 'shipped' => OC_App::isShipped($action['app']), |
|
141 | + ); |
|
142 | + continue; |
|
143 | + } |
|
144 | + // Run the action |
|
145 | + $responses[] = array( |
|
146 | + 'app' => $action['app'], |
|
147 | + 'response' => call_user_func($action['action'], $parameters), |
|
148 | + 'shipped' => OC_App::isShipped($action['app']), |
|
149 | + ); |
|
150 | + } |
|
151 | + $response = self::mergeResponses($responses); |
|
152 | + $format = self::requestedFormat(); |
|
153 | + if (self::$logoutRequired) { |
|
154 | + \OC::$server->getUserSession()->logout(); |
|
155 | + } |
|
156 | + |
|
157 | + self::respond($response, $format); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * merge the returned result objects into one response |
|
162 | + * @param array $responses |
|
163 | + * @return OC_OCS_Result |
|
164 | + */ |
|
165 | + public static function mergeResponses($responses) { |
|
166 | + // Sort into shipped and third-party |
|
167 | + $shipped = array( |
|
168 | + 'succeeded' => array(), |
|
169 | + 'failed' => array(), |
|
170 | + ); |
|
171 | + $thirdparty = array( |
|
172 | + 'succeeded' => array(), |
|
173 | + 'failed' => array(), |
|
174 | + ); |
|
175 | + |
|
176 | + foreach($responses as $response) { |
|
177 | + if($response['shipped'] || ($response['app'] === 'core')) { |
|
178 | + if($response['response']->succeeded()) { |
|
179 | + $shipped['succeeded'][$response['app']] = $response; |
|
180 | + } else { |
|
181 | + $shipped['failed'][$response['app']] = $response; |
|
182 | + } |
|
183 | + } else { |
|
184 | + if($response['response']->succeeded()) { |
|
185 | + $thirdparty['succeeded'][$response['app']] = $response; |
|
186 | + } else { |
|
187 | + $thirdparty['failed'][$response['app']] = $response; |
|
188 | + } |
|
189 | + } |
|
190 | + } |
|
191 | + |
|
192 | + // Remove any error responses if there is one shipped response that succeeded |
|
193 | + if(!empty($shipped['failed'])) { |
|
194 | + // Which shipped response do we use if they all failed? |
|
195 | + // They may have failed for different reasons (different status codes) |
|
196 | + // Which response code should we return? |
|
197 | + // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR |
|
198 | + // Merge failed responses if more than one |
|
199 | + $data = array(); |
|
200 | + foreach($shipped['failed'] as $failure) { |
|
201 | + $data = array_merge_recursive($data, $failure['response']->getData()); |
|
202 | + } |
|
203 | + $picked = reset($shipped['failed']); |
|
204 | + $code = $picked['response']->getStatusCode(); |
|
205 | + $meta = $picked['response']->getMeta(); |
|
206 | + $headers = $picked['response']->getHeaders(); |
|
207 | + $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
|
208 | + return $response; |
|
209 | + } elseif(!empty($shipped['succeeded'])) { |
|
210 | + $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); |
|
211 | + } elseif(!empty($thirdparty['failed'])) { |
|
212 | + // Merge failed responses if more than one |
|
213 | + $data = array(); |
|
214 | + foreach($thirdparty['failed'] as $failure) { |
|
215 | + $data = array_merge_recursive($data, $failure['response']->getData()); |
|
216 | + } |
|
217 | + $picked = reset($thirdparty['failed']); |
|
218 | + $code = $picked['response']->getStatusCode(); |
|
219 | + $meta = $picked['response']->getMeta(); |
|
220 | + $headers = $picked['response']->getHeaders(); |
|
221 | + $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
|
222 | + return $response; |
|
223 | + } else { |
|
224 | + $responses = $thirdparty['succeeded']; |
|
225 | + } |
|
226 | + // Merge the successful responses |
|
227 | + $data = []; |
|
228 | + $codes = []; |
|
229 | + $header = []; |
|
230 | + |
|
231 | + foreach($responses as $response) { |
|
232 | + if($response['shipped']) { |
|
233 | + $data = array_merge_recursive($response['response']->getData(), $data); |
|
234 | + } else { |
|
235 | + $data = array_merge_recursive($data, $response['response']->getData()); |
|
236 | + } |
|
237 | + $header = array_merge_recursive($header, $response['response']->getHeaders()); |
|
238 | + $codes[] = ['code' => $response['response']->getStatusCode(), |
|
239 | + 'meta' => $response['response']->getMeta()]; |
|
240 | + } |
|
241 | + |
|
242 | + // Use any non 100 status codes |
|
243 | + $statusCode = 100; |
|
244 | + $statusMessage = null; |
|
245 | + foreach($codes as $code) { |
|
246 | + if($code['code'] != 100) { |
|
247 | + $statusCode = $code['code']; |
|
248 | + $statusMessage = $code['meta']['message']; |
|
249 | + break; |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + return new OC_OCS_Result($data, $statusCode, $statusMessage, $header); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * authenticate the api call |
|
258 | + * @param array $action the action details as supplied to OC_API::register() |
|
259 | + * @return bool |
|
260 | + */ |
|
261 | + private static function isAuthorised($action) { |
|
262 | + $level = $action['authlevel']; |
|
263 | + switch($level) { |
|
264 | + case API::GUEST_AUTH: |
|
265 | + // Anyone can access |
|
266 | + return true; |
|
267 | + case API::USER_AUTH: |
|
268 | + // User required |
|
269 | + return self::loginUser(); |
|
270 | + case API::SUBADMIN_AUTH: |
|
271 | + // Check for subadmin |
|
272 | + $user = self::loginUser(); |
|
273 | + if(!$user) { |
|
274 | + return false; |
|
275 | + } else { |
|
276 | + $userObject = \OC::$server->getUserSession()->getUser(); |
|
277 | + if($userObject === null) { |
|
278 | + return false; |
|
279 | + } |
|
280 | + $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
281 | + $admin = OC_User::isAdminUser($user); |
|
282 | + if($isSubAdmin || $admin) { |
|
283 | + return true; |
|
284 | + } else { |
|
285 | + return false; |
|
286 | + } |
|
287 | + } |
|
288 | + case API::ADMIN_AUTH: |
|
289 | + // Check for admin |
|
290 | + $user = self::loginUser(); |
|
291 | + if(!$user) { |
|
292 | + return false; |
|
293 | + } else { |
|
294 | + return OC_User::isAdminUser($user); |
|
295 | + } |
|
296 | + default: |
|
297 | + // oops looks like invalid level supplied |
|
298 | + return false; |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * http basic auth |
|
304 | + * @return string|false (username, or false on failure) |
|
305 | + */ |
|
306 | + private static function loginUser() { |
|
307 | + if(self::$isLoggedIn === true) { |
|
308 | + return \OC_User::getUser(); |
|
309 | + } |
|
310 | + |
|
311 | + // reuse existing login |
|
312 | + $loggedIn = \OC::$server->getUserSession()->isLoggedIn(); |
|
313 | + if ($loggedIn === true) { |
|
314 | + if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
315 | + // Do not allow access to OCS until the 2FA challenge was solved successfully |
|
316 | + return false; |
|
317 | + } |
|
318 | + $ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false; |
|
319 | + if ($ocsApiRequest) { |
|
320 | + |
|
321 | + // initialize the user's filesystem |
|
322 | + \OC_Util::setupFS(\OC_User::getUser()); |
|
323 | + self::$isLoggedIn = true; |
|
324 | + |
|
325 | + return OC_User::getUser(); |
|
326 | + } |
|
327 | + return false; |
|
328 | + } |
|
329 | + |
|
330 | + // basic auth - because OC_User::login will create a new session we shall only try to login |
|
331 | + // if user and pass are set |
|
332 | + $userSession = \OC::$server->getUserSession(); |
|
333 | + $request = \OC::$server->getRequest(); |
|
334 | + try { |
|
335 | + if ($userSession->tryTokenLogin($request) |
|
336 | + || $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) { |
|
337 | + self::$logoutRequired = true; |
|
338 | + } else { |
|
339 | + return false; |
|
340 | + } |
|
341 | + // initialize the user's filesystem |
|
342 | + \OC_Util::setupFS(\OC_User::getUser()); |
|
343 | + self::$isLoggedIn = true; |
|
344 | + |
|
345 | + return \OC_User::getUser(); |
|
346 | + } catch (\OC\User\LoginException $e) { |
|
347 | + return false; |
|
348 | + } |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * respond to a call |
|
353 | + * @param OC_OCS_Result $result |
|
354 | + * @param string $format the format xml|json |
|
355 | + */ |
|
356 | + public static function respond($result, $format='xml') { |
|
357 | + $request = \OC::$server->getRequest(); |
|
358 | + |
|
359 | + // Send 401 headers if unauthorised |
|
360 | + if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
361 | + // If request comes from JS return dummy auth request |
|
362 | + if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
363 | + header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
|
364 | + } else { |
|
365 | + header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
|
366 | + } |
|
367 | + header('HTTP/1.0 401 Unauthorized'); |
|
368 | + } |
|
369 | + |
|
370 | + foreach($result->getHeaders() as $name => $value) { |
|
371 | + header($name . ': ' . $value); |
|
372 | + } |
|
373 | + |
|
374 | + $meta = $result->getMeta(); |
|
375 | + $data = $result->getData(); |
|
376 | + if (self::isV2($request)) { |
|
377 | + $statusCode = self::mapStatusCodes($result->getStatusCode()); |
|
378 | + if (!is_null($statusCode)) { |
|
379 | + $meta['statuscode'] = $statusCode; |
|
380 | + OC_Response::setStatus($statusCode); |
|
381 | + } |
|
382 | + } |
|
383 | + |
|
384 | + self::setContentType($format); |
|
385 | + $body = self::renderResult($format, $meta, $data); |
|
386 | + echo $body; |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * @param XMLWriter $writer |
|
391 | + */ |
|
392 | + private static function toXML($array, $writer) { |
|
393 | + foreach($array as $k => $v) { |
|
394 | + if ($k[0] === '@') { |
|
395 | + $writer->writeAttribute(substr($k, 1), $v); |
|
396 | + continue; |
|
397 | + } else if (is_numeric($k)) { |
|
398 | + $k = 'element'; |
|
399 | + } |
|
400 | + if(is_array($v)) { |
|
401 | + $writer->startElement($k); |
|
402 | + self::toXML($v, $writer); |
|
403 | + $writer->endElement(); |
|
404 | + } else { |
|
405 | + $writer->writeElement($k, $v); |
|
406 | + } |
|
407 | + } |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * @return string |
|
412 | + */ |
|
413 | + public static function requestedFormat() { |
|
414 | + $formats = array('json', 'xml'); |
|
415 | + |
|
416 | + $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; |
|
417 | + return $format; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Based on the requested format the response content type is set |
|
422 | + * @param string $format |
|
423 | + */ |
|
424 | + public static function setContentType($format = null) { |
|
425 | + $format = is_null($format) ? self::requestedFormat() : $format; |
|
426 | + if ($format === 'xml') { |
|
427 | + header('Content-type: text/xml; charset=UTF-8'); |
|
428 | + return; |
|
429 | + } |
|
430 | + |
|
431 | + if ($format === 'json') { |
|
432 | + header('Content-Type: application/json; charset=utf-8'); |
|
433 | + return; |
|
434 | + } |
|
435 | + |
|
436 | + header('Content-Type: application/octet-stream; charset=utf-8'); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * @param \OCP\IRequest $request |
|
441 | + * @return bool |
|
442 | + */ |
|
443 | + protected static function isV2(\OCP\IRequest $request) { |
|
444 | + $script = $request->getScriptName(); |
|
445 | + |
|
446 | + return substr($script, -11) === '/ocs/v2.php'; |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * @param integer $sc |
|
451 | + * @return int |
|
452 | + */ |
|
453 | + public static function mapStatusCodes($sc) { |
|
454 | + switch ($sc) { |
|
455 | + case API::RESPOND_NOT_FOUND: |
|
456 | + return Http::STATUS_NOT_FOUND; |
|
457 | + case API::RESPOND_SERVER_ERROR: |
|
458 | + return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
459 | + case API::RESPOND_UNKNOWN_ERROR: |
|
460 | + return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
461 | + case API::RESPOND_UNAUTHORISED: |
|
462 | + // already handled for v1 |
|
463 | + return null; |
|
464 | + case 100: |
|
465 | + return Http::STATUS_OK; |
|
466 | + } |
|
467 | + // any 2xx, 4xx and 5xx will be used as is |
|
468 | + if ($sc >= 200 && $sc < 600) { |
|
469 | + return $sc; |
|
470 | + } |
|
471 | + |
|
472 | + return Http::STATUS_BAD_REQUEST; |
|
473 | + } |
|
474 | + |
|
475 | + /** |
|
476 | + * @param string $format |
|
477 | + * @return string |
|
478 | + */ |
|
479 | + public static function renderResult($format, $meta, $data) { |
|
480 | + $response = array( |
|
481 | + 'ocs' => array( |
|
482 | + 'meta' => $meta, |
|
483 | + 'data' => $data, |
|
484 | + ), |
|
485 | + ); |
|
486 | + if ($format == 'json') { |
|
487 | + return OC_JSON::encode($response); |
|
488 | + } |
|
489 | + |
|
490 | + $writer = new XMLWriter(); |
|
491 | + $writer->openMemory(); |
|
492 | + $writer->setIndent(true); |
|
493 | + $writer->startDocument(); |
|
494 | + self::toXML($response, $writer); |
|
495 | + $writer->endDocument(); |
|
496 | + return $writer->outputMemory(true); |
|
497 | + } |
|
498 | 498 | } |
@@ -88,7 +88,7 @@ |
||
88 | 88 | * send a message to the client |
89 | 89 | * |
90 | 90 | * @param string $type |
91 | - * @param mixed $data |
|
91 | + * @param string $data |
|
92 | 92 | * |
93 | 93 | * @throws \BadMethodCallException |
94 | 94 | * if only one parameter is given, a typeless message will be send with that parameter as data |
@@ -33,98 +33,98 @@ |
||
33 | 33 | * use server side events with caution, to many open requests can hang the server |
34 | 34 | */ |
35 | 35 | class OC_EventSource implements \OCP\IEventSource { |
36 | - /** |
|
37 | - * @var bool |
|
38 | - */ |
|
39 | - private $fallback; |
|
36 | + /** |
|
37 | + * @var bool |
|
38 | + */ |
|
39 | + private $fallback; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @var int |
|
43 | - */ |
|
44 | - private $fallBackId = 0; |
|
41 | + /** |
|
42 | + * @var int |
|
43 | + */ |
|
44 | + private $fallBackId = 0; |
|
45 | 45 | |
46 | - /** |
|
47 | - * @var bool |
|
48 | - */ |
|
49 | - private $started = false; |
|
46 | + /** |
|
47 | + * @var bool |
|
48 | + */ |
|
49 | + private $started = false; |
|
50 | 50 | |
51 | - protected function init() { |
|
52 | - if ($this->started) { |
|
53 | - return; |
|
54 | - } |
|
55 | - $this->started = true; |
|
51 | + protected function init() { |
|
52 | + if ($this->started) { |
|
53 | + return; |
|
54 | + } |
|
55 | + $this->started = true; |
|
56 | 56 | |
57 | - // prevent php output buffering, caching and nginx buffering |
|
58 | - OC_Util::obEnd(); |
|
59 | - header('Cache-Control: no-cache'); |
|
60 | - header('X-Accel-Buffering: no'); |
|
61 | - $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
|
62 | - if ($this->fallback) { |
|
63 | - $this->fallBackId = (int)$_GET['fallback_id']; |
|
64 | - /** |
|
65 | - * FIXME: The default content-security-policy of ownCloud forbids inline |
|
66 | - * JavaScript for security reasons. IE starting on Windows 10 will |
|
67 | - * however also obey the CSP which will break the event source fallback. |
|
68 | - * |
|
69 | - * As a workaround thus we set a custom policy which allows the execution |
|
70 | - * of inline JavaScript. |
|
71 | - * |
|
72 | - * @link https://github.com/owncloud/core/issues/14286 |
|
73 | - */ |
|
74 | - header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
|
75 | - header("Content-Type: text/html"); |
|
76 | - echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
77 | - } else { |
|
78 | - header("Content-Type: text/event-stream"); |
|
79 | - } |
|
80 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
81 | - header('Location: '.\OC::$WEBROOT); |
|
82 | - exit(); |
|
83 | - } |
|
84 | - if (!(\OC::$server->getRequest()->passesCSRFCheck())) { |
|
85 | - $this->send('error', 'Possible CSRF attack. Connection will be closed.'); |
|
86 | - $this->close(); |
|
87 | - exit(); |
|
88 | - } |
|
89 | - flush(); |
|
90 | - } |
|
57 | + // prevent php output buffering, caching and nginx buffering |
|
58 | + OC_Util::obEnd(); |
|
59 | + header('Cache-Control: no-cache'); |
|
60 | + header('X-Accel-Buffering: no'); |
|
61 | + $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
|
62 | + if ($this->fallback) { |
|
63 | + $this->fallBackId = (int)$_GET['fallback_id']; |
|
64 | + /** |
|
65 | + * FIXME: The default content-security-policy of ownCloud forbids inline |
|
66 | + * JavaScript for security reasons. IE starting on Windows 10 will |
|
67 | + * however also obey the CSP which will break the event source fallback. |
|
68 | + * |
|
69 | + * As a workaround thus we set a custom policy which allows the execution |
|
70 | + * of inline JavaScript. |
|
71 | + * |
|
72 | + * @link https://github.com/owncloud/core/issues/14286 |
|
73 | + */ |
|
74 | + header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
|
75 | + header("Content-Type: text/html"); |
|
76 | + echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
77 | + } else { |
|
78 | + header("Content-Type: text/event-stream"); |
|
79 | + } |
|
80 | + if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
81 | + header('Location: '.\OC::$WEBROOT); |
|
82 | + exit(); |
|
83 | + } |
|
84 | + if (!(\OC::$server->getRequest()->passesCSRFCheck())) { |
|
85 | + $this->send('error', 'Possible CSRF attack. Connection will be closed.'); |
|
86 | + $this->close(); |
|
87 | + exit(); |
|
88 | + } |
|
89 | + flush(); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * send a message to the client |
|
94 | - * |
|
95 | - * @param string $type |
|
96 | - * @param mixed $data |
|
97 | - * |
|
98 | - * @throws \BadMethodCallException |
|
99 | - * if only one parameter is given, a typeless message will be send with that parameter as data |
|
100 | - */ |
|
101 | - public function send($type, $data = null) { |
|
102 | - if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
|
103 | - throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
104 | - } |
|
105 | - $this->init(); |
|
106 | - if (is_null($data)) { |
|
107 | - $data = $type; |
|
108 | - $type = null; |
|
109 | - } |
|
110 | - if ($this->fallback) { |
|
111 | - $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
|
112 | - . $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL; |
|
113 | - echo $response; |
|
114 | - } else { |
|
115 | - if ($type) { |
|
116 | - echo 'event: ' . $type . PHP_EOL; |
|
117 | - } |
|
118 | - echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL; |
|
119 | - } |
|
120 | - echo PHP_EOL; |
|
121 | - flush(); |
|
122 | - } |
|
92 | + /** |
|
93 | + * send a message to the client |
|
94 | + * |
|
95 | + * @param string $type |
|
96 | + * @param mixed $data |
|
97 | + * |
|
98 | + * @throws \BadMethodCallException |
|
99 | + * if only one parameter is given, a typeless message will be send with that parameter as data |
|
100 | + */ |
|
101 | + public function send($type, $data = null) { |
|
102 | + if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
|
103 | + throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
104 | + } |
|
105 | + $this->init(); |
|
106 | + if (is_null($data)) { |
|
107 | + $data = $type; |
|
108 | + $type = null; |
|
109 | + } |
|
110 | + if ($this->fallback) { |
|
111 | + $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
|
112 | + . $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL; |
|
113 | + echo $response; |
|
114 | + } else { |
|
115 | + if ($type) { |
|
116 | + echo 'event: ' . $type . PHP_EOL; |
|
117 | + } |
|
118 | + echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL; |
|
119 | + } |
|
120 | + echo PHP_EOL; |
|
121 | + flush(); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * close the connection of the event source |
|
126 | - */ |
|
127 | - public function close() { |
|
128 | - $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it |
|
129 | - } |
|
124 | + /** |
|
125 | + * close the connection of the event source |
|
126 | + */ |
|
127 | + public function close() { |
|
128 | + $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it |
|
129 | + } |
|
130 | 130 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | header('X-Accel-Buffering: no'); |
61 | 61 | $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
62 | 62 | if ($this->fallback) { |
63 | - $this->fallBackId = (int)$_GET['fallback_id']; |
|
63 | + $this->fallBackId = (int) $_GET['fallback_id']; |
|
64 | 64 | /** |
65 | 65 | * FIXME: The default content-security-policy of ownCloud forbids inline |
66 | 66 | * JavaScript for security reasons. IE starting on Windows 10 will |
@@ -73,11 +73,11 @@ discard block |
||
73 | 73 | */ |
74 | 74 | header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
75 | 75 | header("Content-Type: text/html"); |
76 | - echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
76 | + echo str_repeat('<span></span>'.PHP_EOL, 10); //dummy data to keep IE happy |
|
77 | 77 | } else { |
78 | 78 | header("Content-Type: text/event-stream"); |
79 | 79 | } |
80 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
80 | + if (!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
81 | 81 | header('Location: '.\OC::$WEBROOT); |
82 | 82 | exit(); |
83 | 83 | } |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | */ |
101 | 101 | public function send($type, $data = null) { |
102 | 102 | if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
103 | - throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
103 | + throw new BadMethodCallException('Type needs to be alphanumeric ('.$type.')'); |
|
104 | 104 | } |
105 | 105 | $this->init(); |
106 | 106 | if (is_null($data)) { |
@@ -109,13 +109,13 @@ discard block |
||
109 | 109 | } |
110 | 110 | if ($this->fallback) { |
111 | 111 | $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
112 | - . $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL; |
|
112 | + . $this->fallBackId.',"'.$type.'",'.OCP\JSON::encode($data).')</script>'.PHP_EOL; |
|
113 | 113 | echo $response; |
114 | 114 | } else { |
115 | 115 | if ($type) { |
116 | - echo 'event: ' . $type . PHP_EOL; |
|
116 | + echo 'event: '.$type.PHP_EOL; |
|
117 | 117 | } |
118 | - echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL; |
|
118 | + echo 'data: '.OCP\JSON::encode($data).PHP_EOL; |
|
119 | 119 | } |
120 | 120 | echo PHP_EOL; |
121 | 121 | flush(); |