@@ -560,7 +560,7 @@ |
||
560 | 560 | * |
561 | 561 | * @param string $path |
562 | 562 | * @param \OCP\Files\FileInfo $rootInfo (optional) |
563 | - * @return array |
|
563 | + * @return string |
|
564 | 564 | * @throws \OCP\Files\NotFoundException |
565 | 565 | */ |
566 | 566 | public static function getStorageInfo($path, $rootInfo = null) { |
@@ -48,606 +48,606 @@ |
||
48 | 48 | * Collection of useful functions |
49 | 49 | */ |
50 | 50 | class OC_Helper { |
51 | - private static $templateManager; |
|
52 | - |
|
53 | - /** |
|
54 | - * Creates an absolute url for public use |
|
55 | - * @param string $service id |
|
56 | - * @param bool $add_slash |
|
57 | - * @return string the url |
|
58 | - * |
|
59 | - * Returns a absolute url to the given service. |
|
60 | - */ |
|
61 | - public static function linkToPublic($service, $add_slash = false) { |
|
62 | - if ($service === 'files') { |
|
63 | - $url = OC::$server->getURLGenerator()->getAbsoluteURL('/s'); |
|
64 | - } else { |
|
65 | - $url = OC::$server->getURLGenerator()->getAbsoluteURL(OC::$server->getURLGenerator()->linkTo('', 'public.php').'?service='.$service); |
|
66 | - } |
|
67 | - return $url . (($add_slash && $service[strlen($service) - 1] != '/') ? '/' : ''); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Make a human file size |
|
72 | - * @param int $bytes file size in bytes |
|
73 | - * @return string a human readable file size |
|
74 | - * |
|
75 | - * Makes 2048 to 2 kB. |
|
76 | - */ |
|
77 | - public static function humanFileSize($bytes) { |
|
78 | - if ($bytes < 0) { |
|
79 | - return "?"; |
|
80 | - } |
|
81 | - if ($bytes < 1024) { |
|
82 | - return "$bytes B"; |
|
83 | - } |
|
84 | - $bytes = round($bytes / 1024, 0); |
|
85 | - if ($bytes < 1024) { |
|
86 | - return "$bytes KB"; |
|
87 | - } |
|
88 | - $bytes = round($bytes / 1024, 1); |
|
89 | - if ($bytes < 1024) { |
|
90 | - return "$bytes MB"; |
|
91 | - } |
|
92 | - $bytes = round($bytes / 1024, 1); |
|
93 | - if ($bytes < 1024) { |
|
94 | - return "$bytes GB"; |
|
95 | - } |
|
96 | - $bytes = round($bytes / 1024, 1); |
|
97 | - if ($bytes < 1024) { |
|
98 | - return "$bytes TB"; |
|
99 | - } |
|
100 | - |
|
101 | - $bytes = round($bytes / 1024, 1); |
|
102 | - return "$bytes PB"; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Make a php file size |
|
107 | - * @param int $bytes file size in bytes |
|
108 | - * @return string a php parseable file size |
|
109 | - * |
|
110 | - * Makes 2048 to 2k and 2^41 to 2048G |
|
111 | - */ |
|
112 | - public static function phpFileSize($bytes) { |
|
113 | - if ($bytes < 0) { |
|
114 | - return "?"; |
|
115 | - } |
|
116 | - if ($bytes < 1024) { |
|
117 | - return $bytes . "B"; |
|
118 | - } |
|
119 | - $bytes = round($bytes / 1024, 1); |
|
120 | - if ($bytes < 1024) { |
|
121 | - return $bytes . "K"; |
|
122 | - } |
|
123 | - $bytes = round($bytes / 1024, 1); |
|
124 | - if ($bytes < 1024) { |
|
125 | - return $bytes . "M"; |
|
126 | - } |
|
127 | - $bytes = round($bytes / 1024, 1); |
|
128 | - return $bytes . "G"; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Make a computer file size |
|
133 | - * @param string $str file size in human readable format |
|
134 | - * @return float a file size in bytes |
|
135 | - * |
|
136 | - * Makes 2kB to 2048. |
|
137 | - * |
|
138 | - * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
139 | - */ |
|
140 | - public static function computerFileSize($str) { |
|
141 | - $str = strtolower($str); |
|
142 | - if (is_numeric($str)) { |
|
143 | - return floatval($str); |
|
144 | - } |
|
145 | - |
|
146 | - $bytes_array = array( |
|
147 | - 'b' => 1, |
|
148 | - 'k' => 1024, |
|
149 | - 'kb' => 1024, |
|
150 | - 'mb' => 1024 * 1024, |
|
151 | - 'm' => 1024 * 1024, |
|
152 | - 'gb' => 1024 * 1024 * 1024, |
|
153 | - 'g' => 1024 * 1024 * 1024, |
|
154 | - 'tb' => 1024 * 1024 * 1024 * 1024, |
|
155 | - 't' => 1024 * 1024 * 1024 * 1024, |
|
156 | - 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
157 | - 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
158 | - ); |
|
159 | - |
|
160 | - $bytes = floatval($str); |
|
161 | - |
|
162 | - if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) { |
|
163 | - $bytes *= $bytes_array[$matches[1]]; |
|
164 | - } else { |
|
165 | - return false; |
|
166 | - } |
|
167 | - |
|
168 | - $bytes = round($bytes); |
|
169 | - |
|
170 | - return $bytes; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Recursive copying of folders |
|
175 | - * @param string $src source folder |
|
176 | - * @param string $dest target folder |
|
177 | - * |
|
178 | - */ |
|
179 | - static function copyr($src, $dest) { |
|
180 | - if (is_dir($src)) { |
|
181 | - if (!is_dir($dest)) { |
|
182 | - mkdir($dest); |
|
183 | - } |
|
184 | - $files = scandir($src); |
|
185 | - foreach ($files as $file) { |
|
186 | - if ($file != "." && $file != "..") { |
|
187 | - self::copyr("$src/$file", "$dest/$file"); |
|
188 | - } |
|
189 | - } |
|
190 | - } elseif (file_exists($src) && !\OC\Files\Filesystem::isFileBlacklisted($src)) { |
|
191 | - copy($src, $dest); |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Recursive deletion of folders |
|
197 | - * @param string $dir path to the folder |
|
198 | - * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
199 | - * @return bool |
|
200 | - */ |
|
201 | - static function rmdirr($dir, $deleteSelf = true) { |
|
202 | - if (is_dir($dir)) { |
|
203 | - $files = new RecursiveIteratorIterator( |
|
204 | - new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
|
205 | - RecursiveIteratorIterator::CHILD_FIRST |
|
206 | - ); |
|
207 | - |
|
208 | - foreach ($files as $fileInfo) { |
|
209 | - /** @var SplFileInfo $fileInfo */ |
|
210 | - if ($fileInfo->isLink()) { |
|
211 | - unlink($fileInfo->getPathname()); |
|
212 | - } else if ($fileInfo->isDir()) { |
|
213 | - rmdir($fileInfo->getRealPath()); |
|
214 | - } else { |
|
215 | - unlink($fileInfo->getRealPath()); |
|
216 | - } |
|
217 | - } |
|
218 | - if ($deleteSelf) { |
|
219 | - rmdir($dir); |
|
220 | - } |
|
221 | - } elseif (file_exists($dir)) { |
|
222 | - if ($deleteSelf) { |
|
223 | - unlink($dir); |
|
224 | - } |
|
225 | - } |
|
226 | - if (!$deleteSelf) { |
|
227 | - return true; |
|
228 | - } |
|
229 | - |
|
230 | - return !file_exists($dir); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * @return \OC\Files\Type\TemplateManager |
|
235 | - */ |
|
236 | - static public function getFileTemplateManager() { |
|
237 | - if (!self::$templateManager) { |
|
238 | - self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
239 | - } |
|
240 | - return self::$templateManager; |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * detect if a given program is found in the search PATH |
|
245 | - * |
|
246 | - * @param string $name |
|
247 | - * @param bool $path |
|
248 | - * @internal param string $program name |
|
249 | - * @internal param string $optional search path, defaults to $PATH |
|
250 | - * @return bool true if executable program found in path |
|
251 | - */ |
|
252 | - public static function canExecute($name, $path = false) { |
|
253 | - // path defaults to PATH from environment if not set |
|
254 | - if ($path === false) { |
|
255 | - $path = getenv("PATH"); |
|
256 | - } |
|
257 | - // we look for an executable file of that name |
|
258 | - $exts = [""]; |
|
259 | - $check_fn = "is_executable"; |
|
260 | - // Default check will be done with $path directories : |
|
261 | - $dirs = explode(PATH_SEPARATOR, $path); |
|
262 | - // WARNING : We have to check if open_basedir is enabled : |
|
263 | - $obd = OC::$server->getIniWrapper()->getString('open_basedir'); |
|
264 | - if ($obd != "none") { |
|
265 | - $obd_values = explode(PATH_SEPARATOR, $obd); |
|
266 | - if (count($obd_values) > 0 and $obd_values[0]) { |
|
267 | - // open_basedir is in effect ! |
|
268 | - // We need to check if the program is in one of these dirs : |
|
269 | - $dirs = $obd_values; |
|
270 | - } |
|
271 | - } |
|
272 | - foreach ($dirs as $dir) { |
|
273 | - foreach ($exts as $ext) { |
|
274 | - if ($check_fn("$dir/$name" . $ext)) |
|
275 | - return true; |
|
276 | - } |
|
277 | - } |
|
278 | - return false; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * copy the contents of one stream to another |
|
283 | - * |
|
284 | - * @param resource $source |
|
285 | - * @param resource $target |
|
286 | - * @return array the number of bytes copied and result |
|
287 | - */ |
|
288 | - public static function streamCopy($source, $target) { |
|
289 | - if (!$source or !$target) { |
|
290 | - return array(0, false); |
|
291 | - } |
|
292 | - $bufSize = 8192; |
|
293 | - $result = true; |
|
294 | - $count = 0; |
|
295 | - while (!feof($source)) { |
|
296 | - $buf = fread($source, $bufSize); |
|
297 | - $bytesWritten = fwrite($target, $buf); |
|
298 | - if ($bytesWritten !== false) { |
|
299 | - $count += $bytesWritten; |
|
300 | - } |
|
301 | - // note: strlen is expensive so only use it when necessary, |
|
302 | - // on the last block |
|
303 | - if ($bytesWritten === false |
|
304 | - || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
305 | - ) { |
|
306 | - // write error, could be disk full ? |
|
307 | - $result = false; |
|
308 | - break; |
|
309 | - } |
|
310 | - } |
|
311 | - return array($count, $result); |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Adds a suffix to the name in case the file exists |
|
316 | - * |
|
317 | - * @param string $path |
|
318 | - * @param string $filename |
|
319 | - * @return string |
|
320 | - */ |
|
321 | - public static function buildNotExistingFileName($path, $filename) { |
|
322 | - $view = \OC\Files\Filesystem::getView(); |
|
323 | - return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Adds a suffix to the name in case the file exists |
|
328 | - * |
|
329 | - * @param string $path |
|
330 | - * @param string $filename |
|
331 | - * @return string |
|
332 | - */ |
|
333 | - public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
334 | - if ($path === '/') { |
|
335 | - $path = ''; |
|
336 | - } |
|
337 | - if ($pos = strrpos($filename, '.')) { |
|
338 | - $name = substr($filename, 0, $pos); |
|
339 | - $ext = substr($filename, $pos); |
|
340 | - } else { |
|
341 | - $name = $filename; |
|
342 | - $ext = ''; |
|
343 | - } |
|
344 | - |
|
345 | - $newpath = $path . '/' . $filename; |
|
346 | - if ($view->file_exists($newpath)) { |
|
347 | - if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
348 | - //Replace the last "(number)" with "(number+1)" |
|
349 | - $last_match = count($matches[0]) - 1; |
|
350 | - $counter = $matches[1][$last_match][0] + 1; |
|
351 | - $offset = $matches[0][$last_match][1]; |
|
352 | - $match_length = strlen($matches[0][$last_match][0]); |
|
353 | - } else { |
|
354 | - $counter = 2; |
|
355 | - $match_length = 0; |
|
356 | - $offset = false; |
|
357 | - } |
|
358 | - do { |
|
359 | - if ($offset) { |
|
360 | - //Replace the last "(number)" with "(number+1)" |
|
361 | - $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
362 | - } else { |
|
363 | - $newname = $name . ' (' . $counter . ')'; |
|
364 | - } |
|
365 | - $newpath = $path . '/' . $newname . $ext; |
|
366 | - $counter++; |
|
367 | - } while ($view->file_exists($newpath)); |
|
368 | - } |
|
369 | - |
|
370 | - return $newpath; |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
375 | - * |
|
376 | - * @param array $input The array to work on |
|
377 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
378 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
379 | - * @return array |
|
380 | - * |
|
381 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
382 | - * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
383 | - * |
|
384 | - */ |
|
385 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
386 | - $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
387 | - $ret = array(); |
|
388 | - foreach ($input as $k => $v) { |
|
389 | - $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
390 | - } |
|
391 | - return $ret; |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * performs a search in a nested array |
|
396 | - * @param array $haystack the array to be searched |
|
397 | - * @param string $needle the search string |
|
398 | - * @param string $index optional, only search this key name |
|
399 | - * @return mixed the key of the matching field, otherwise false |
|
400 | - * |
|
401 | - * performs a search in a nested array |
|
402 | - * |
|
403 | - * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
|
404 | - */ |
|
405 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
406 | - $aIt = new RecursiveArrayIterator($haystack); |
|
407 | - $it = new RecursiveIteratorIterator($aIt); |
|
408 | - |
|
409 | - while ($it->valid()) { |
|
410 | - if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) { |
|
411 | - return $aIt->key(); |
|
412 | - } |
|
413 | - |
|
414 | - $it->next(); |
|
415 | - } |
|
416 | - |
|
417 | - return false; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
422 | - * |
|
423 | - * @param string $dir the current folder where the user currently operates |
|
424 | - * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
425 | - * @return int number of bytes representing |
|
426 | - */ |
|
427 | - public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
428 | - if (is_null($freeSpace) || $freeSpace < 0){ |
|
429 | - $freeSpace = self::freeSpace($dir); |
|
430 | - } |
|
431 | - return min($freeSpace, self::uploadLimit()); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Calculate free space left within user quota |
|
436 | - * |
|
437 | - * @param string $dir the current folder where the user currently operates |
|
438 | - * @return int number of bytes representing |
|
439 | - */ |
|
440 | - public static function freeSpace($dir) { |
|
441 | - $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
442 | - if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
443 | - $freeSpace = max($freeSpace, 0); |
|
444 | - return $freeSpace; |
|
445 | - } else { |
|
446 | - return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
447 | - } |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Calculate PHP upload limit |
|
452 | - * |
|
453 | - * @return int PHP upload file size limit |
|
454 | - */ |
|
455 | - public static function uploadLimit() { |
|
456 | - $ini = \OC::$server->getIniWrapper(); |
|
457 | - $upload_max_filesize = OCP\Util::computerFileSize($ini->get('upload_max_filesize')); |
|
458 | - $post_max_size = OCP\Util::computerFileSize($ini->get('post_max_size')); |
|
459 | - if ((int)$upload_max_filesize === 0 and (int)$post_max_size === 0) { |
|
460 | - return INF; |
|
461 | - } elseif ((int)$upload_max_filesize === 0 or (int)$post_max_size === 0) { |
|
462 | - return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
463 | - } else { |
|
464 | - return min($upload_max_filesize, $post_max_size); |
|
465 | - } |
|
466 | - } |
|
467 | - |
|
468 | - /** |
|
469 | - * Checks if a function is available |
|
470 | - * |
|
471 | - * @param string $function_name |
|
472 | - * @return bool |
|
473 | - */ |
|
474 | - public static function is_function_enabled($function_name) { |
|
475 | - if (!function_exists($function_name)) { |
|
476 | - return false; |
|
477 | - } |
|
478 | - $ini = \OC::$server->getIniWrapper(); |
|
479 | - $disabled = explode(',', $ini->get('disable_functions')); |
|
480 | - $disabled = array_map('trim', $disabled); |
|
481 | - if (in_array($function_name, $disabled)) { |
|
482 | - return false; |
|
483 | - } |
|
484 | - $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist')); |
|
485 | - $disabled = array_map('trim', $disabled); |
|
486 | - if (in_array($function_name, $disabled)) { |
|
487 | - return false; |
|
488 | - } |
|
489 | - return true; |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Try to find a program |
|
494 | - * |
|
495 | - * @param string $program |
|
496 | - * @return null|string |
|
497 | - */ |
|
498 | - public static function findBinaryPath($program) { |
|
499 | - $memcache = \OC::$server->getMemCacheFactory()->create('findBinaryPath'); |
|
500 | - if ($memcache->hasKey($program)) { |
|
501 | - return $memcache->get($program); |
|
502 | - } |
|
503 | - $result = null; |
|
504 | - if (self::is_function_enabled('exec')) { |
|
505 | - $exeSniffer = new ExecutableFinder(); |
|
506 | - // Returns null if nothing is found |
|
507 | - $result = $exeSniffer->find($program); |
|
508 | - if (empty($result)) { |
|
509 | - $paths = getenv('PATH'); |
|
510 | - if (empty($paths)) { |
|
511 | - $paths = '/usr/local/bin /usr/bin /opt/bin /bin'; |
|
512 | - } else { |
|
513 | - $paths = str_replace(':',' ',getenv('PATH')); |
|
514 | - } |
|
515 | - $command = 'find ' . $paths . ' -name ' . escapeshellarg($program) . ' 2> /dev/null'; |
|
516 | - exec($command, $output, $returnCode); |
|
517 | - if (count($output) > 0) { |
|
518 | - $result = escapeshellcmd($output[0]); |
|
519 | - } |
|
520 | - } |
|
521 | - } |
|
522 | - // store the value for 5 minutes |
|
523 | - $memcache->set($program, $result, 300); |
|
524 | - return $result; |
|
525 | - } |
|
526 | - |
|
527 | - /** |
|
528 | - * Calculate the disc space for the given path |
|
529 | - * |
|
530 | - * @param string $path |
|
531 | - * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
532 | - * @return array |
|
533 | - * @throws \OCP\Files\NotFoundException |
|
534 | - */ |
|
535 | - public static function getStorageInfo($path, $rootInfo = null) { |
|
536 | - // return storage info without adding mount points |
|
537 | - $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
538 | - |
|
539 | - if (!$rootInfo) { |
|
540 | - $rootInfo = \OC\Files\Filesystem::getFileInfo($path, false); |
|
541 | - } |
|
542 | - if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
543 | - throw new \OCP\Files\NotFoundException(); |
|
544 | - } |
|
545 | - $used = $rootInfo->getSize(); |
|
546 | - if ($used < 0) { |
|
547 | - $used = 0; |
|
548 | - } |
|
549 | - $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
550 | - $storage = $rootInfo->getStorage(); |
|
551 | - $sourceStorage = $storage; |
|
552 | - if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
553 | - $includeExtStorage = false; |
|
554 | - $sourceStorage = $storage->getSourceStorage(); |
|
555 | - } |
|
556 | - if ($includeExtStorage) { |
|
557 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
558 | - || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
559 | - ) { |
|
560 | - /** @var \OC\Files\Storage\Home $storage */ |
|
561 | - $user = $storage->getUser(); |
|
562 | - } else { |
|
563 | - $user = \OC::$server->getUserSession()->getUser()->getUID(); |
|
564 | - } |
|
565 | - if ($user) { |
|
566 | - $quota = OC_Util::getUserQuota($user); |
|
567 | - } else { |
|
568 | - $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
569 | - } |
|
570 | - if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
571 | - // always get free space / total space from root + mount points |
|
572 | - return self::getGlobalStorageInfo(); |
|
573 | - } |
|
574 | - } |
|
575 | - |
|
576 | - // TODO: need a better way to get total space from storage |
|
577 | - if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
578 | - /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
579 | - $quota = $sourceStorage->getQuota(); |
|
580 | - } |
|
581 | - $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
582 | - if ($free >= 0) { |
|
583 | - $total = $free + $used; |
|
584 | - } else { |
|
585 | - $total = $free; //either unknown or unlimited |
|
586 | - } |
|
587 | - if ($total > 0) { |
|
588 | - if ($quota > 0 && $total > $quota) { |
|
589 | - $total = $quota; |
|
590 | - } |
|
591 | - // prevent division by zero or error codes (negative values) |
|
592 | - $relative = round(($used / $total) * 10000) / 100; |
|
593 | - } else { |
|
594 | - $relative = 0; |
|
595 | - } |
|
596 | - |
|
597 | - $ownerId = $storage->getOwner($path); |
|
598 | - $ownerDisplayName = ''; |
|
599 | - $owner = \OC::$server->getUserManager()->get($ownerId); |
|
600 | - if($owner) { |
|
601 | - $ownerDisplayName = $owner->getDisplayName(); |
|
602 | - } |
|
603 | - |
|
604 | - return [ |
|
605 | - 'free' => $free, |
|
606 | - 'used' => $used, |
|
607 | - 'quota' => $quota, |
|
608 | - 'total' => $total, |
|
609 | - 'relative' => $relative, |
|
610 | - 'owner' => $ownerId, |
|
611 | - 'ownerDisplayName' => $ownerDisplayName, |
|
612 | - ]; |
|
613 | - } |
|
614 | - |
|
615 | - /** |
|
616 | - * Get storage info including all mount points and quota |
|
617 | - * |
|
618 | - * @return array |
|
619 | - */ |
|
620 | - private static function getGlobalStorageInfo() { |
|
621 | - $quota = OC_Util::getUserQuota(\OCP\User::getUser()); |
|
622 | - |
|
623 | - $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
624 | - $used = $rootInfo['size']; |
|
625 | - if ($used < 0) { |
|
626 | - $used = 0; |
|
627 | - } |
|
628 | - |
|
629 | - $total = $quota; |
|
630 | - $free = $quota - $used; |
|
631 | - |
|
632 | - if ($total > 0) { |
|
633 | - if ($quota > 0 && $total > $quota) { |
|
634 | - $total = $quota; |
|
635 | - } |
|
636 | - // prevent division by zero or error codes (negative values) |
|
637 | - $relative = round(($used / $total) * 10000) / 100; |
|
638 | - } else { |
|
639 | - $relative = 0; |
|
640 | - } |
|
641 | - |
|
642 | - return array('free' => $free, 'used' => $used, 'total' => $total, 'relative' => $relative); |
|
643 | - |
|
644 | - } |
|
645 | - |
|
646 | - /** |
|
647 | - * Returns whether the config file is set manually to read-only |
|
648 | - * @return bool |
|
649 | - */ |
|
650 | - public static function isReadOnlyConfigEnabled() { |
|
651 | - return \OC::$server->getConfig()->getSystemValue('config_is_read_only', false); |
|
652 | - } |
|
51 | + private static $templateManager; |
|
52 | + |
|
53 | + /** |
|
54 | + * Creates an absolute url for public use |
|
55 | + * @param string $service id |
|
56 | + * @param bool $add_slash |
|
57 | + * @return string the url |
|
58 | + * |
|
59 | + * Returns a absolute url to the given service. |
|
60 | + */ |
|
61 | + public static function linkToPublic($service, $add_slash = false) { |
|
62 | + if ($service === 'files') { |
|
63 | + $url = OC::$server->getURLGenerator()->getAbsoluteURL('/s'); |
|
64 | + } else { |
|
65 | + $url = OC::$server->getURLGenerator()->getAbsoluteURL(OC::$server->getURLGenerator()->linkTo('', 'public.php').'?service='.$service); |
|
66 | + } |
|
67 | + return $url . (($add_slash && $service[strlen($service) - 1] != '/') ? '/' : ''); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Make a human file size |
|
72 | + * @param int $bytes file size in bytes |
|
73 | + * @return string a human readable file size |
|
74 | + * |
|
75 | + * Makes 2048 to 2 kB. |
|
76 | + */ |
|
77 | + public static function humanFileSize($bytes) { |
|
78 | + if ($bytes < 0) { |
|
79 | + return "?"; |
|
80 | + } |
|
81 | + if ($bytes < 1024) { |
|
82 | + return "$bytes B"; |
|
83 | + } |
|
84 | + $bytes = round($bytes / 1024, 0); |
|
85 | + if ($bytes < 1024) { |
|
86 | + return "$bytes KB"; |
|
87 | + } |
|
88 | + $bytes = round($bytes / 1024, 1); |
|
89 | + if ($bytes < 1024) { |
|
90 | + return "$bytes MB"; |
|
91 | + } |
|
92 | + $bytes = round($bytes / 1024, 1); |
|
93 | + if ($bytes < 1024) { |
|
94 | + return "$bytes GB"; |
|
95 | + } |
|
96 | + $bytes = round($bytes / 1024, 1); |
|
97 | + if ($bytes < 1024) { |
|
98 | + return "$bytes TB"; |
|
99 | + } |
|
100 | + |
|
101 | + $bytes = round($bytes / 1024, 1); |
|
102 | + return "$bytes PB"; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Make a php file size |
|
107 | + * @param int $bytes file size in bytes |
|
108 | + * @return string a php parseable file size |
|
109 | + * |
|
110 | + * Makes 2048 to 2k and 2^41 to 2048G |
|
111 | + */ |
|
112 | + public static function phpFileSize($bytes) { |
|
113 | + if ($bytes < 0) { |
|
114 | + return "?"; |
|
115 | + } |
|
116 | + if ($bytes < 1024) { |
|
117 | + return $bytes . "B"; |
|
118 | + } |
|
119 | + $bytes = round($bytes / 1024, 1); |
|
120 | + if ($bytes < 1024) { |
|
121 | + return $bytes . "K"; |
|
122 | + } |
|
123 | + $bytes = round($bytes / 1024, 1); |
|
124 | + if ($bytes < 1024) { |
|
125 | + return $bytes . "M"; |
|
126 | + } |
|
127 | + $bytes = round($bytes / 1024, 1); |
|
128 | + return $bytes . "G"; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Make a computer file size |
|
133 | + * @param string $str file size in human readable format |
|
134 | + * @return float a file size in bytes |
|
135 | + * |
|
136 | + * Makes 2kB to 2048. |
|
137 | + * |
|
138 | + * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
139 | + */ |
|
140 | + public static function computerFileSize($str) { |
|
141 | + $str = strtolower($str); |
|
142 | + if (is_numeric($str)) { |
|
143 | + return floatval($str); |
|
144 | + } |
|
145 | + |
|
146 | + $bytes_array = array( |
|
147 | + 'b' => 1, |
|
148 | + 'k' => 1024, |
|
149 | + 'kb' => 1024, |
|
150 | + 'mb' => 1024 * 1024, |
|
151 | + 'm' => 1024 * 1024, |
|
152 | + 'gb' => 1024 * 1024 * 1024, |
|
153 | + 'g' => 1024 * 1024 * 1024, |
|
154 | + 'tb' => 1024 * 1024 * 1024 * 1024, |
|
155 | + 't' => 1024 * 1024 * 1024 * 1024, |
|
156 | + 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
157 | + 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
158 | + ); |
|
159 | + |
|
160 | + $bytes = floatval($str); |
|
161 | + |
|
162 | + if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) { |
|
163 | + $bytes *= $bytes_array[$matches[1]]; |
|
164 | + } else { |
|
165 | + return false; |
|
166 | + } |
|
167 | + |
|
168 | + $bytes = round($bytes); |
|
169 | + |
|
170 | + return $bytes; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Recursive copying of folders |
|
175 | + * @param string $src source folder |
|
176 | + * @param string $dest target folder |
|
177 | + * |
|
178 | + */ |
|
179 | + static function copyr($src, $dest) { |
|
180 | + if (is_dir($src)) { |
|
181 | + if (!is_dir($dest)) { |
|
182 | + mkdir($dest); |
|
183 | + } |
|
184 | + $files = scandir($src); |
|
185 | + foreach ($files as $file) { |
|
186 | + if ($file != "." && $file != "..") { |
|
187 | + self::copyr("$src/$file", "$dest/$file"); |
|
188 | + } |
|
189 | + } |
|
190 | + } elseif (file_exists($src) && !\OC\Files\Filesystem::isFileBlacklisted($src)) { |
|
191 | + copy($src, $dest); |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Recursive deletion of folders |
|
197 | + * @param string $dir path to the folder |
|
198 | + * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
199 | + * @return bool |
|
200 | + */ |
|
201 | + static function rmdirr($dir, $deleteSelf = true) { |
|
202 | + if (is_dir($dir)) { |
|
203 | + $files = new RecursiveIteratorIterator( |
|
204 | + new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
|
205 | + RecursiveIteratorIterator::CHILD_FIRST |
|
206 | + ); |
|
207 | + |
|
208 | + foreach ($files as $fileInfo) { |
|
209 | + /** @var SplFileInfo $fileInfo */ |
|
210 | + if ($fileInfo->isLink()) { |
|
211 | + unlink($fileInfo->getPathname()); |
|
212 | + } else if ($fileInfo->isDir()) { |
|
213 | + rmdir($fileInfo->getRealPath()); |
|
214 | + } else { |
|
215 | + unlink($fileInfo->getRealPath()); |
|
216 | + } |
|
217 | + } |
|
218 | + if ($deleteSelf) { |
|
219 | + rmdir($dir); |
|
220 | + } |
|
221 | + } elseif (file_exists($dir)) { |
|
222 | + if ($deleteSelf) { |
|
223 | + unlink($dir); |
|
224 | + } |
|
225 | + } |
|
226 | + if (!$deleteSelf) { |
|
227 | + return true; |
|
228 | + } |
|
229 | + |
|
230 | + return !file_exists($dir); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * @return \OC\Files\Type\TemplateManager |
|
235 | + */ |
|
236 | + static public function getFileTemplateManager() { |
|
237 | + if (!self::$templateManager) { |
|
238 | + self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
239 | + } |
|
240 | + return self::$templateManager; |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * detect if a given program is found in the search PATH |
|
245 | + * |
|
246 | + * @param string $name |
|
247 | + * @param bool $path |
|
248 | + * @internal param string $program name |
|
249 | + * @internal param string $optional search path, defaults to $PATH |
|
250 | + * @return bool true if executable program found in path |
|
251 | + */ |
|
252 | + public static function canExecute($name, $path = false) { |
|
253 | + // path defaults to PATH from environment if not set |
|
254 | + if ($path === false) { |
|
255 | + $path = getenv("PATH"); |
|
256 | + } |
|
257 | + // we look for an executable file of that name |
|
258 | + $exts = [""]; |
|
259 | + $check_fn = "is_executable"; |
|
260 | + // Default check will be done with $path directories : |
|
261 | + $dirs = explode(PATH_SEPARATOR, $path); |
|
262 | + // WARNING : We have to check if open_basedir is enabled : |
|
263 | + $obd = OC::$server->getIniWrapper()->getString('open_basedir'); |
|
264 | + if ($obd != "none") { |
|
265 | + $obd_values = explode(PATH_SEPARATOR, $obd); |
|
266 | + if (count($obd_values) > 0 and $obd_values[0]) { |
|
267 | + // open_basedir is in effect ! |
|
268 | + // We need to check if the program is in one of these dirs : |
|
269 | + $dirs = $obd_values; |
|
270 | + } |
|
271 | + } |
|
272 | + foreach ($dirs as $dir) { |
|
273 | + foreach ($exts as $ext) { |
|
274 | + if ($check_fn("$dir/$name" . $ext)) |
|
275 | + return true; |
|
276 | + } |
|
277 | + } |
|
278 | + return false; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * copy the contents of one stream to another |
|
283 | + * |
|
284 | + * @param resource $source |
|
285 | + * @param resource $target |
|
286 | + * @return array the number of bytes copied and result |
|
287 | + */ |
|
288 | + public static function streamCopy($source, $target) { |
|
289 | + if (!$source or !$target) { |
|
290 | + return array(0, false); |
|
291 | + } |
|
292 | + $bufSize = 8192; |
|
293 | + $result = true; |
|
294 | + $count = 0; |
|
295 | + while (!feof($source)) { |
|
296 | + $buf = fread($source, $bufSize); |
|
297 | + $bytesWritten = fwrite($target, $buf); |
|
298 | + if ($bytesWritten !== false) { |
|
299 | + $count += $bytesWritten; |
|
300 | + } |
|
301 | + // note: strlen is expensive so only use it when necessary, |
|
302 | + // on the last block |
|
303 | + if ($bytesWritten === false |
|
304 | + || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
305 | + ) { |
|
306 | + // write error, could be disk full ? |
|
307 | + $result = false; |
|
308 | + break; |
|
309 | + } |
|
310 | + } |
|
311 | + return array($count, $result); |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Adds a suffix to the name in case the file exists |
|
316 | + * |
|
317 | + * @param string $path |
|
318 | + * @param string $filename |
|
319 | + * @return string |
|
320 | + */ |
|
321 | + public static function buildNotExistingFileName($path, $filename) { |
|
322 | + $view = \OC\Files\Filesystem::getView(); |
|
323 | + return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Adds a suffix to the name in case the file exists |
|
328 | + * |
|
329 | + * @param string $path |
|
330 | + * @param string $filename |
|
331 | + * @return string |
|
332 | + */ |
|
333 | + public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
334 | + if ($path === '/') { |
|
335 | + $path = ''; |
|
336 | + } |
|
337 | + if ($pos = strrpos($filename, '.')) { |
|
338 | + $name = substr($filename, 0, $pos); |
|
339 | + $ext = substr($filename, $pos); |
|
340 | + } else { |
|
341 | + $name = $filename; |
|
342 | + $ext = ''; |
|
343 | + } |
|
344 | + |
|
345 | + $newpath = $path . '/' . $filename; |
|
346 | + if ($view->file_exists($newpath)) { |
|
347 | + if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
348 | + //Replace the last "(number)" with "(number+1)" |
|
349 | + $last_match = count($matches[0]) - 1; |
|
350 | + $counter = $matches[1][$last_match][0] + 1; |
|
351 | + $offset = $matches[0][$last_match][1]; |
|
352 | + $match_length = strlen($matches[0][$last_match][0]); |
|
353 | + } else { |
|
354 | + $counter = 2; |
|
355 | + $match_length = 0; |
|
356 | + $offset = false; |
|
357 | + } |
|
358 | + do { |
|
359 | + if ($offset) { |
|
360 | + //Replace the last "(number)" with "(number+1)" |
|
361 | + $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
362 | + } else { |
|
363 | + $newname = $name . ' (' . $counter . ')'; |
|
364 | + } |
|
365 | + $newpath = $path . '/' . $newname . $ext; |
|
366 | + $counter++; |
|
367 | + } while ($view->file_exists($newpath)); |
|
368 | + } |
|
369 | + |
|
370 | + return $newpath; |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
375 | + * |
|
376 | + * @param array $input The array to work on |
|
377 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
378 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
379 | + * @return array |
|
380 | + * |
|
381 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
382 | + * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
383 | + * |
|
384 | + */ |
|
385 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
386 | + $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
387 | + $ret = array(); |
|
388 | + foreach ($input as $k => $v) { |
|
389 | + $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
390 | + } |
|
391 | + return $ret; |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * performs a search in a nested array |
|
396 | + * @param array $haystack the array to be searched |
|
397 | + * @param string $needle the search string |
|
398 | + * @param string $index optional, only search this key name |
|
399 | + * @return mixed the key of the matching field, otherwise false |
|
400 | + * |
|
401 | + * performs a search in a nested array |
|
402 | + * |
|
403 | + * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
|
404 | + */ |
|
405 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
406 | + $aIt = new RecursiveArrayIterator($haystack); |
|
407 | + $it = new RecursiveIteratorIterator($aIt); |
|
408 | + |
|
409 | + while ($it->valid()) { |
|
410 | + if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) { |
|
411 | + return $aIt->key(); |
|
412 | + } |
|
413 | + |
|
414 | + $it->next(); |
|
415 | + } |
|
416 | + |
|
417 | + return false; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
422 | + * |
|
423 | + * @param string $dir the current folder where the user currently operates |
|
424 | + * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
425 | + * @return int number of bytes representing |
|
426 | + */ |
|
427 | + public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
428 | + if (is_null($freeSpace) || $freeSpace < 0){ |
|
429 | + $freeSpace = self::freeSpace($dir); |
|
430 | + } |
|
431 | + return min($freeSpace, self::uploadLimit()); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Calculate free space left within user quota |
|
436 | + * |
|
437 | + * @param string $dir the current folder where the user currently operates |
|
438 | + * @return int number of bytes representing |
|
439 | + */ |
|
440 | + public static function freeSpace($dir) { |
|
441 | + $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
442 | + if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
443 | + $freeSpace = max($freeSpace, 0); |
|
444 | + return $freeSpace; |
|
445 | + } else { |
|
446 | + return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
447 | + } |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Calculate PHP upload limit |
|
452 | + * |
|
453 | + * @return int PHP upload file size limit |
|
454 | + */ |
|
455 | + public static function uploadLimit() { |
|
456 | + $ini = \OC::$server->getIniWrapper(); |
|
457 | + $upload_max_filesize = OCP\Util::computerFileSize($ini->get('upload_max_filesize')); |
|
458 | + $post_max_size = OCP\Util::computerFileSize($ini->get('post_max_size')); |
|
459 | + if ((int)$upload_max_filesize === 0 and (int)$post_max_size === 0) { |
|
460 | + return INF; |
|
461 | + } elseif ((int)$upload_max_filesize === 0 or (int)$post_max_size === 0) { |
|
462 | + return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
463 | + } else { |
|
464 | + return min($upload_max_filesize, $post_max_size); |
|
465 | + } |
|
466 | + } |
|
467 | + |
|
468 | + /** |
|
469 | + * Checks if a function is available |
|
470 | + * |
|
471 | + * @param string $function_name |
|
472 | + * @return bool |
|
473 | + */ |
|
474 | + public static function is_function_enabled($function_name) { |
|
475 | + if (!function_exists($function_name)) { |
|
476 | + return false; |
|
477 | + } |
|
478 | + $ini = \OC::$server->getIniWrapper(); |
|
479 | + $disabled = explode(',', $ini->get('disable_functions')); |
|
480 | + $disabled = array_map('trim', $disabled); |
|
481 | + if (in_array($function_name, $disabled)) { |
|
482 | + return false; |
|
483 | + } |
|
484 | + $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist')); |
|
485 | + $disabled = array_map('trim', $disabled); |
|
486 | + if (in_array($function_name, $disabled)) { |
|
487 | + return false; |
|
488 | + } |
|
489 | + return true; |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Try to find a program |
|
494 | + * |
|
495 | + * @param string $program |
|
496 | + * @return null|string |
|
497 | + */ |
|
498 | + public static function findBinaryPath($program) { |
|
499 | + $memcache = \OC::$server->getMemCacheFactory()->create('findBinaryPath'); |
|
500 | + if ($memcache->hasKey($program)) { |
|
501 | + return $memcache->get($program); |
|
502 | + } |
|
503 | + $result = null; |
|
504 | + if (self::is_function_enabled('exec')) { |
|
505 | + $exeSniffer = new ExecutableFinder(); |
|
506 | + // Returns null if nothing is found |
|
507 | + $result = $exeSniffer->find($program); |
|
508 | + if (empty($result)) { |
|
509 | + $paths = getenv('PATH'); |
|
510 | + if (empty($paths)) { |
|
511 | + $paths = '/usr/local/bin /usr/bin /opt/bin /bin'; |
|
512 | + } else { |
|
513 | + $paths = str_replace(':',' ',getenv('PATH')); |
|
514 | + } |
|
515 | + $command = 'find ' . $paths . ' -name ' . escapeshellarg($program) . ' 2> /dev/null'; |
|
516 | + exec($command, $output, $returnCode); |
|
517 | + if (count($output) > 0) { |
|
518 | + $result = escapeshellcmd($output[0]); |
|
519 | + } |
|
520 | + } |
|
521 | + } |
|
522 | + // store the value for 5 minutes |
|
523 | + $memcache->set($program, $result, 300); |
|
524 | + return $result; |
|
525 | + } |
|
526 | + |
|
527 | + /** |
|
528 | + * Calculate the disc space for the given path |
|
529 | + * |
|
530 | + * @param string $path |
|
531 | + * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
532 | + * @return array |
|
533 | + * @throws \OCP\Files\NotFoundException |
|
534 | + */ |
|
535 | + public static function getStorageInfo($path, $rootInfo = null) { |
|
536 | + // return storage info without adding mount points |
|
537 | + $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
538 | + |
|
539 | + if (!$rootInfo) { |
|
540 | + $rootInfo = \OC\Files\Filesystem::getFileInfo($path, false); |
|
541 | + } |
|
542 | + if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
543 | + throw new \OCP\Files\NotFoundException(); |
|
544 | + } |
|
545 | + $used = $rootInfo->getSize(); |
|
546 | + if ($used < 0) { |
|
547 | + $used = 0; |
|
548 | + } |
|
549 | + $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
550 | + $storage = $rootInfo->getStorage(); |
|
551 | + $sourceStorage = $storage; |
|
552 | + if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
553 | + $includeExtStorage = false; |
|
554 | + $sourceStorage = $storage->getSourceStorage(); |
|
555 | + } |
|
556 | + if ($includeExtStorage) { |
|
557 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
558 | + || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
559 | + ) { |
|
560 | + /** @var \OC\Files\Storage\Home $storage */ |
|
561 | + $user = $storage->getUser(); |
|
562 | + } else { |
|
563 | + $user = \OC::$server->getUserSession()->getUser()->getUID(); |
|
564 | + } |
|
565 | + if ($user) { |
|
566 | + $quota = OC_Util::getUserQuota($user); |
|
567 | + } else { |
|
568 | + $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
569 | + } |
|
570 | + if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
571 | + // always get free space / total space from root + mount points |
|
572 | + return self::getGlobalStorageInfo(); |
|
573 | + } |
|
574 | + } |
|
575 | + |
|
576 | + // TODO: need a better way to get total space from storage |
|
577 | + if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
578 | + /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
579 | + $quota = $sourceStorage->getQuota(); |
|
580 | + } |
|
581 | + $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
582 | + if ($free >= 0) { |
|
583 | + $total = $free + $used; |
|
584 | + } else { |
|
585 | + $total = $free; //either unknown or unlimited |
|
586 | + } |
|
587 | + if ($total > 0) { |
|
588 | + if ($quota > 0 && $total > $quota) { |
|
589 | + $total = $quota; |
|
590 | + } |
|
591 | + // prevent division by zero or error codes (negative values) |
|
592 | + $relative = round(($used / $total) * 10000) / 100; |
|
593 | + } else { |
|
594 | + $relative = 0; |
|
595 | + } |
|
596 | + |
|
597 | + $ownerId = $storage->getOwner($path); |
|
598 | + $ownerDisplayName = ''; |
|
599 | + $owner = \OC::$server->getUserManager()->get($ownerId); |
|
600 | + if($owner) { |
|
601 | + $ownerDisplayName = $owner->getDisplayName(); |
|
602 | + } |
|
603 | + |
|
604 | + return [ |
|
605 | + 'free' => $free, |
|
606 | + 'used' => $used, |
|
607 | + 'quota' => $quota, |
|
608 | + 'total' => $total, |
|
609 | + 'relative' => $relative, |
|
610 | + 'owner' => $ownerId, |
|
611 | + 'ownerDisplayName' => $ownerDisplayName, |
|
612 | + ]; |
|
613 | + } |
|
614 | + |
|
615 | + /** |
|
616 | + * Get storage info including all mount points and quota |
|
617 | + * |
|
618 | + * @return array |
|
619 | + */ |
|
620 | + private static function getGlobalStorageInfo() { |
|
621 | + $quota = OC_Util::getUserQuota(\OCP\User::getUser()); |
|
622 | + |
|
623 | + $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
624 | + $used = $rootInfo['size']; |
|
625 | + if ($used < 0) { |
|
626 | + $used = 0; |
|
627 | + } |
|
628 | + |
|
629 | + $total = $quota; |
|
630 | + $free = $quota - $used; |
|
631 | + |
|
632 | + if ($total > 0) { |
|
633 | + if ($quota > 0 && $total > $quota) { |
|
634 | + $total = $quota; |
|
635 | + } |
|
636 | + // prevent division by zero or error codes (negative values) |
|
637 | + $relative = round(($used / $total) * 10000) / 100; |
|
638 | + } else { |
|
639 | + $relative = 0; |
|
640 | + } |
|
641 | + |
|
642 | + return array('free' => $free, 'used' => $used, 'total' => $total, 'relative' => $relative); |
|
643 | + |
|
644 | + } |
|
645 | + |
|
646 | + /** |
|
647 | + * Returns whether the config file is set manually to read-only |
|
648 | + * @return bool |
|
649 | + */ |
|
650 | + public static function isReadOnlyConfigEnabled() { |
|
651 | + return \OC::$server->getConfig()->getSystemValue('config_is_read_only', false); |
|
652 | + } |
|
653 | 653 | } |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | } else { |
65 | 65 | $url = OC::$server->getURLGenerator()->getAbsoluteURL(OC::$server->getURLGenerator()->linkTo('', 'public.php').'?service='.$service); |
66 | 66 | } |
67 | - return $url . (($add_slash && $service[strlen($service) - 1] != '/') ? '/' : ''); |
|
67 | + return $url.(($add_slash && $service[strlen($service) - 1] != '/') ? '/' : ''); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -114,18 +114,18 @@ discard block |
||
114 | 114 | return "?"; |
115 | 115 | } |
116 | 116 | if ($bytes < 1024) { |
117 | - return $bytes . "B"; |
|
117 | + return $bytes."B"; |
|
118 | 118 | } |
119 | 119 | $bytes = round($bytes / 1024, 1); |
120 | 120 | if ($bytes < 1024) { |
121 | - return $bytes . "K"; |
|
121 | + return $bytes."K"; |
|
122 | 122 | } |
123 | 123 | $bytes = round($bytes / 1024, 1); |
124 | 124 | if ($bytes < 1024) { |
125 | - return $bytes . "M"; |
|
125 | + return $bytes."M"; |
|
126 | 126 | } |
127 | 127 | $bytes = round($bytes / 1024, 1); |
128 | - return $bytes . "G"; |
|
128 | + return $bytes."G"; |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | } |
272 | 272 | foreach ($dirs as $dir) { |
273 | 273 | foreach ($exts as $ext) { |
274 | - if ($check_fn("$dir/$name" . $ext)) |
|
274 | + if ($check_fn("$dir/$name".$ext)) |
|
275 | 275 | return true; |
276 | 276 | } |
277 | 277 | } |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | $ext = ''; |
343 | 343 | } |
344 | 344 | |
345 | - $newpath = $path . '/' . $filename; |
|
345 | + $newpath = $path.'/'.$filename; |
|
346 | 346 | if ($view->file_exists($newpath)) { |
347 | 347 | if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
348 | 348 | //Replace the last "(number)" with "(number+1)" |
@@ -358,11 +358,11 @@ discard block |
||
358 | 358 | do { |
359 | 359 | if ($offset) { |
360 | 360 | //Replace the last "(number)" with "(number+1)" |
361 | - $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
361 | + $newname = substr_replace($name, '('.$counter.')', $offset, $match_length); |
|
362 | 362 | } else { |
363 | - $newname = $name . ' (' . $counter . ')'; |
|
363 | + $newname = $name.' ('.$counter.')'; |
|
364 | 364 | } |
365 | - $newpath = $path . '/' . $newname . $ext; |
|
365 | + $newpath = $path.'/'.$newname.$ext; |
|
366 | 366 | $counter++; |
367 | 367 | } while ($view->file_exists($newpath)); |
368 | 368 | } |
@@ -425,7 +425,7 @@ discard block |
||
425 | 425 | * @return int number of bytes representing |
426 | 426 | */ |
427 | 427 | public static function maxUploadFilesize($dir, $freeSpace = null) { |
428 | - if (is_null($freeSpace) || $freeSpace < 0){ |
|
428 | + if (is_null($freeSpace) || $freeSpace < 0) { |
|
429 | 429 | $freeSpace = self::freeSpace($dir); |
430 | 430 | } |
431 | 431 | return min($freeSpace, self::uploadLimit()); |
@@ -443,7 +443,7 @@ discard block |
||
443 | 443 | $freeSpace = max($freeSpace, 0); |
444 | 444 | return $freeSpace; |
445 | 445 | } else { |
446 | - return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
446 | + return (INF > 0) ? INF : PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
447 | 447 | } |
448 | 448 | } |
449 | 449 | |
@@ -456,9 +456,9 @@ discard block |
||
456 | 456 | $ini = \OC::$server->getIniWrapper(); |
457 | 457 | $upload_max_filesize = OCP\Util::computerFileSize($ini->get('upload_max_filesize')); |
458 | 458 | $post_max_size = OCP\Util::computerFileSize($ini->get('post_max_size')); |
459 | - if ((int)$upload_max_filesize === 0 and (int)$post_max_size === 0) { |
|
459 | + if ((int) $upload_max_filesize === 0 and (int) $post_max_size === 0) { |
|
460 | 460 | return INF; |
461 | - } elseif ((int)$upload_max_filesize === 0 or (int)$post_max_size === 0) { |
|
461 | + } elseif ((int) $upload_max_filesize === 0 or (int) $post_max_size === 0) { |
|
462 | 462 | return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
463 | 463 | } else { |
464 | 464 | return min($upload_max_filesize, $post_max_size); |
@@ -510,9 +510,9 @@ discard block |
||
510 | 510 | if (empty($paths)) { |
511 | 511 | $paths = '/usr/local/bin /usr/bin /opt/bin /bin'; |
512 | 512 | } else { |
513 | - $paths = str_replace(':',' ',getenv('PATH')); |
|
513 | + $paths = str_replace(':', ' ', getenv('PATH')); |
|
514 | 514 | } |
515 | - $command = 'find ' . $paths . ' -name ' . escapeshellarg($program) . ' 2> /dev/null'; |
|
515 | + $command = 'find '.$paths.' -name '.escapeshellarg($program).' 2> /dev/null'; |
|
516 | 516 | exec($command, $output, $returnCode); |
517 | 517 | if (count($output) > 0) { |
518 | 518 | $result = escapeshellcmd($output[0]); |
@@ -597,7 +597,7 @@ discard block |
||
597 | 597 | $ownerId = $storage->getOwner($path); |
598 | 598 | $ownerDisplayName = ''; |
599 | 599 | $owner = \OC::$server->getUserManager()->get($ownerId); |
600 | - if($owner) { |
|
600 | + if ($owner) { |
|
601 | 601 | $ownerDisplayName = $owner->getDisplayName(); |
602 | 602 | } |
603 | 603 |
@@ -271,8 +271,9 @@ |
||
271 | 271 | } |
272 | 272 | foreach ($dirs as $dir) { |
273 | 273 | foreach ($exts as $ext) { |
274 | - if ($check_fn("$dir/$name" . $ext)) |
|
275 | - return true; |
|
274 | + if ($check_fn("$dir/$name" . $ext)) { |
|
275 | + return true; |
|
276 | + } |
|
276 | 277 | } |
277 | 278 | } |
278 | 279 | return false; |
@@ -162,7 +162,7 @@ |
||
162 | 162 | /** |
163 | 163 | * Set the BCC recipients of this message. |
164 | 164 | * |
165 | - * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
165 | + * @param string[] $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
166 | 166 | * @return $this |
167 | 167 | */ |
168 | 168 | public function setBcc(array $recipients) { |
@@ -31,223 +31,223 @@ |
||
31 | 31 | * @package OC\Mail |
32 | 32 | */ |
33 | 33 | class Message { |
34 | - /** @var Swift_Message */ |
|
35 | - private $swiftMessage; |
|
36 | - |
|
37 | - /** |
|
38 | - * @param Swift_Message $swiftMessage |
|
39 | - */ |
|
40 | - function __construct(Swift_Message $swiftMessage) { |
|
41 | - $this->swiftMessage = $swiftMessage; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
46 | - * FIXME: Remove this once SwiftMailer supports IDN |
|
47 | - * |
|
48 | - * @param array $addresses Array of mail addresses, key will get converted |
|
49 | - * @return array Converted addresses if `idn_to_ascii` exists |
|
50 | - */ |
|
51 | - protected function convertAddresses($addresses) { |
|
52 | - if (!function_exists('idn_to_ascii')) { |
|
53 | - return $addresses; |
|
54 | - } |
|
55 | - |
|
56 | - $convertedAddresses = array(); |
|
57 | - |
|
58 | - foreach($addresses as $email => $readableName) { |
|
59 | - if(!is_numeric($email)) { |
|
60 | - list($name, $domain) = explode('@', $email, 2); |
|
61 | - $domain = idn_to_ascii($domain); |
|
62 | - $convertedAddresses[$name.'@'.$domain] = $readableName; |
|
63 | - } else { |
|
64 | - list($name, $domain) = explode('@', $readableName, 2); |
|
65 | - $domain = idn_to_ascii($domain); |
|
66 | - $convertedAddresses[$email] = $name.'@'.$domain; |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - return $convertedAddresses; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Set the from address of this message. |
|
75 | - * |
|
76 | - * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php |
|
77 | - * |
|
78 | - * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name') |
|
79 | - * @return $this |
|
80 | - */ |
|
81 | - public function setFrom(array $addresses) { |
|
82 | - $addresses = $this->convertAddresses($addresses); |
|
83 | - |
|
84 | - $this->swiftMessage->setFrom($addresses); |
|
85 | - return $this; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Get the from address of this message. |
|
90 | - * |
|
91 | - * @return array |
|
92 | - */ |
|
93 | - public function getFrom() { |
|
94 | - return $this->swiftMessage->getFrom(); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Set the Reply-To address of this message |
|
99 | - * |
|
100 | - * @param array $addresses |
|
101 | - * @return $this |
|
102 | - */ |
|
103 | - public function setReplyTo(array $addresses) { |
|
104 | - $addresses = $this->convertAddresses($addresses); |
|
105 | - |
|
106 | - $this->swiftMessage->setReplyTo($addresses); |
|
107 | - return $this; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Returns the Reply-To address of this message |
|
112 | - * |
|
113 | - * @return array |
|
114 | - */ |
|
115 | - public function getReplyTo() { |
|
116 | - return $this->swiftMessage->getReplyTo(); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Set the to addresses of this message. |
|
121 | - * |
|
122 | - * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
123 | - * @return $this |
|
124 | - */ |
|
125 | - public function setTo(array $recipients) { |
|
126 | - $recipients = $this->convertAddresses($recipients); |
|
127 | - |
|
128 | - $this->swiftMessage->setTo($recipients); |
|
129 | - return $this; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Get the to address of this message. |
|
134 | - * |
|
135 | - * @return array |
|
136 | - */ |
|
137 | - public function getTo() { |
|
138 | - return $this->swiftMessage->getTo(); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Set the CC recipients of this message. |
|
143 | - * |
|
144 | - * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
145 | - * @return $this |
|
146 | - */ |
|
147 | - public function setCc(array $recipients) { |
|
148 | - $recipients = $this->convertAddresses($recipients); |
|
149 | - |
|
150 | - $this->swiftMessage->setCc($recipients); |
|
151 | - return $this; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Get the cc address of this message. |
|
156 | - * |
|
157 | - * @return array |
|
158 | - */ |
|
159 | - public function getCc() { |
|
160 | - return $this->swiftMessage->getCc(); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Set the BCC recipients of this message. |
|
165 | - * |
|
166 | - * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
167 | - * @return $this |
|
168 | - */ |
|
169 | - public function setBcc(array $recipients) { |
|
170 | - $recipients = $this->convertAddresses($recipients); |
|
171 | - |
|
172 | - $this->swiftMessage->setBcc($recipients); |
|
173 | - return $this; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Get the Bcc address of this message. |
|
178 | - * |
|
179 | - * @return array |
|
180 | - */ |
|
181 | - public function getBcc() { |
|
182 | - return $this->swiftMessage->getBcc(); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Set the subject of this message. |
|
187 | - * |
|
188 | - * @param $subject |
|
189 | - * @return $this |
|
190 | - */ |
|
191 | - public function setSubject($subject) { |
|
192 | - $this->swiftMessage->setSubject($subject); |
|
193 | - return $this; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Get the from subject of this message. |
|
198 | - * |
|
199 | - * @return string |
|
200 | - */ |
|
201 | - public function getSubject() { |
|
202 | - return $this->swiftMessage->getSubject(); |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Set the plain-text body of this message. |
|
207 | - * |
|
208 | - * @param string $body |
|
209 | - * @return $this |
|
210 | - */ |
|
211 | - public function setPlainBody($body) { |
|
212 | - $this->swiftMessage->setBody($body); |
|
213 | - return $this; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Get the plain body of this message. |
|
218 | - * |
|
219 | - * @return string |
|
220 | - */ |
|
221 | - public function getPlainBody() { |
|
222 | - return $this->swiftMessage->getBody(); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one. |
|
227 | - * |
|
228 | - * @param string $body |
|
229 | - * @return $this |
|
230 | - */ |
|
231 | - public function setHtmlBody($body) { |
|
232 | - $this->swiftMessage->addPart($body, 'text/html'); |
|
233 | - return $this; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Get's the underlying SwiftMessage |
|
238 | - * @return Swift_Message |
|
239 | - */ |
|
240 | - public function getSwiftMessage() { |
|
241 | - return $this->swiftMessage; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @param string $body |
|
246 | - * @param string $contentType |
|
247 | - * @return $this |
|
248 | - */ |
|
249 | - public function setBody($body, $contentType) { |
|
250 | - $this->swiftMessage->setBody($body, $contentType); |
|
251 | - return $this; |
|
252 | - } |
|
34 | + /** @var Swift_Message */ |
|
35 | + private $swiftMessage; |
|
36 | + |
|
37 | + /** |
|
38 | + * @param Swift_Message $swiftMessage |
|
39 | + */ |
|
40 | + function __construct(Swift_Message $swiftMessage) { |
|
41 | + $this->swiftMessage = $swiftMessage; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains |
|
46 | + * FIXME: Remove this once SwiftMailer supports IDN |
|
47 | + * |
|
48 | + * @param array $addresses Array of mail addresses, key will get converted |
|
49 | + * @return array Converted addresses if `idn_to_ascii` exists |
|
50 | + */ |
|
51 | + protected function convertAddresses($addresses) { |
|
52 | + if (!function_exists('idn_to_ascii')) { |
|
53 | + return $addresses; |
|
54 | + } |
|
55 | + |
|
56 | + $convertedAddresses = array(); |
|
57 | + |
|
58 | + foreach($addresses as $email => $readableName) { |
|
59 | + if(!is_numeric($email)) { |
|
60 | + list($name, $domain) = explode('@', $email, 2); |
|
61 | + $domain = idn_to_ascii($domain); |
|
62 | + $convertedAddresses[$name.'@'.$domain] = $readableName; |
|
63 | + } else { |
|
64 | + list($name, $domain) = explode('@', $readableName, 2); |
|
65 | + $domain = idn_to_ascii($domain); |
|
66 | + $convertedAddresses[$email] = $name.'@'.$domain; |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + return $convertedAddresses; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Set the from address of this message. |
|
75 | + * |
|
76 | + * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php |
|
77 | + * |
|
78 | + * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name') |
|
79 | + * @return $this |
|
80 | + */ |
|
81 | + public function setFrom(array $addresses) { |
|
82 | + $addresses = $this->convertAddresses($addresses); |
|
83 | + |
|
84 | + $this->swiftMessage->setFrom($addresses); |
|
85 | + return $this; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Get the from address of this message. |
|
90 | + * |
|
91 | + * @return array |
|
92 | + */ |
|
93 | + public function getFrom() { |
|
94 | + return $this->swiftMessage->getFrom(); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Set the Reply-To address of this message |
|
99 | + * |
|
100 | + * @param array $addresses |
|
101 | + * @return $this |
|
102 | + */ |
|
103 | + public function setReplyTo(array $addresses) { |
|
104 | + $addresses = $this->convertAddresses($addresses); |
|
105 | + |
|
106 | + $this->swiftMessage->setReplyTo($addresses); |
|
107 | + return $this; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Returns the Reply-To address of this message |
|
112 | + * |
|
113 | + * @return array |
|
114 | + */ |
|
115 | + public function getReplyTo() { |
|
116 | + return $this->swiftMessage->getReplyTo(); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Set the to addresses of this message. |
|
121 | + * |
|
122 | + * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
123 | + * @return $this |
|
124 | + */ |
|
125 | + public function setTo(array $recipients) { |
|
126 | + $recipients = $this->convertAddresses($recipients); |
|
127 | + |
|
128 | + $this->swiftMessage->setTo($recipients); |
|
129 | + return $this; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Get the to address of this message. |
|
134 | + * |
|
135 | + * @return array |
|
136 | + */ |
|
137 | + public function getTo() { |
|
138 | + return $this->swiftMessage->getTo(); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Set the CC recipients of this message. |
|
143 | + * |
|
144 | + * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
145 | + * @return $this |
|
146 | + */ |
|
147 | + public function setCc(array $recipients) { |
|
148 | + $recipients = $this->convertAddresses($recipients); |
|
149 | + |
|
150 | + $this->swiftMessage->setCc($recipients); |
|
151 | + return $this; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Get the cc address of this message. |
|
156 | + * |
|
157 | + * @return array |
|
158 | + */ |
|
159 | + public function getCc() { |
|
160 | + return $this->swiftMessage->getCc(); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Set the BCC recipients of this message. |
|
165 | + * |
|
166 | + * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name') |
|
167 | + * @return $this |
|
168 | + */ |
|
169 | + public function setBcc(array $recipients) { |
|
170 | + $recipients = $this->convertAddresses($recipients); |
|
171 | + |
|
172 | + $this->swiftMessage->setBcc($recipients); |
|
173 | + return $this; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Get the Bcc address of this message. |
|
178 | + * |
|
179 | + * @return array |
|
180 | + */ |
|
181 | + public function getBcc() { |
|
182 | + return $this->swiftMessage->getBcc(); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Set the subject of this message. |
|
187 | + * |
|
188 | + * @param $subject |
|
189 | + * @return $this |
|
190 | + */ |
|
191 | + public function setSubject($subject) { |
|
192 | + $this->swiftMessage->setSubject($subject); |
|
193 | + return $this; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Get the from subject of this message. |
|
198 | + * |
|
199 | + * @return string |
|
200 | + */ |
|
201 | + public function getSubject() { |
|
202 | + return $this->swiftMessage->getSubject(); |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Set the plain-text body of this message. |
|
207 | + * |
|
208 | + * @param string $body |
|
209 | + * @return $this |
|
210 | + */ |
|
211 | + public function setPlainBody($body) { |
|
212 | + $this->swiftMessage->setBody($body); |
|
213 | + return $this; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Get the plain body of this message. |
|
218 | + * |
|
219 | + * @return string |
|
220 | + */ |
|
221 | + public function getPlainBody() { |
|
222 | + return $this->swiftMessage->getBody(); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one. |
|
227 | + * |
|
228 | + * @param string $body |
|
229 | + * @return $this |
|
230 | + */ |
|
231 | + public function setHtmlBody($body) { |
|
232 | + $this->swiftMessage->addPart($body, 'text/html'); |
|
233 | + return $this; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Get's the underlying SwiftMessage |
|
238 | + * @return Swift_Message |
|
239 | + */ |
|
240 | + public function getSwiftMessage() { |
|
241 | + return $this->swiftMessage; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @param string $body |
|
246 | + * @param string $contentType |
|
247 | + * @return $this |
|
248 | + */ |
|
249 | + public function setBody($body, $contentType) { |
|
250 | + $this->swiftMessage->setBody($body, $contentType); |
|
251 | + return $this; |
|
252 | + } |
|
253 | 253 | } |
@@ -55,8 +55,8 @@ |
||
55 | 55 | |
56 | 56 | $convertedAddresses = array(); |
57 | 57 | |
58 | - foreach($addresses as $email => $readableName) { |
|
59 | - if(!is_numeric($email)) { |
|
58 | + foreach ($addresses as $email => $readableName) { |
|
59 | + if (!is_numeric($email)) { |
|
60 | 60 | list($name, $domain) = explode('@', $email, 2); |
61 | 61 | $domain = idn_to_ascii($domain); |
62 | 62 | $convertedAddresses[$name.'@'.$domain] = $readableName; |
@@ -155,7 +155,7 @@ |
||
155 | 155 | * Set a value in the cache if it's not already stored |
156 | 156 | * |
157 | 157 | * @param string $key |
158 | - * @param mixed $value |
|
158 | + * @param integer $value |
|
159 | 159 | * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
160 | 160 | * @return bool |
161 | 161 | * @throws \Exception |
@@ -33,193 +33,193 @@ |
||
33 | 33 | use OCP\IMemcache; |
34 | 34 | |
35 | 35 | class Memcached extends Cache implements IMemcache { |
36 | - use CASTrait; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var \Memcached $cache |
|
40 | - */ |
|
41 | - private static $cache = null; |
|
42 | - |
|
43 | - use CADTrait; |
|
44 | - |
|
45 | - public function __construct($prefix = '') { |
|
46 | - parent::__construct($prefix); |
|
47 | - if (is_null(self::$cache)) { |
|
48 | - self::$cache = new \Memcached(); |
|
49 | - |
|
50 | - $defaultOptions = [ |
|
51 | - \Memcached::OPT_CONNECT_TIMEOUT => 50, |
|
52 | - \Memcached::OPT_RETRY_TIMEOUT => 50, |
|
53 | - \Memcached::OPT_SEND_TIMEOUT => 50, |
|
54 | - \Memcached::OPT_RECV_TIMEOUT => 50, |
|
55 | - \Memcached::OPT_POLL_TIMEOUT => 50, |
|
56 | - |
|
57 | - // Enable compression |
|
58 | - \Memcached::OPT_COMPRESSION => true, |
|
59 | - |
|
60 | - // Turn on consistent hashing |
|
61 | - \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
|
62 | - |
|
63 | - // Enable Binary Protocol |
|
64 | - //\Memcached::OPT_BINARY_PROTOCOL => true, |
|
65 | - ]; |
|
66 | - // by default enable igbinary serializer if available |
|
67 | - if (\Memcached::HAVE_IGBINARY) { |
|
68 | - $defaultOptions[\Memcached::OPT_SERIALIZER] = |
|
69 | - \Memcached::SERIALIZER_IGBINARY; |
|
70 | - } |
|
71 | - $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); |
|
72 | - if (is_array($options)) { |
|
73 | - $options = $options + $defaultOptions; |
|
74 | - self::$cache->setOptions($options); |
|
75 | - } else { |
|
76 | - throw new HintException("Expected 'memcached_options' config to be an array, got $options"); |
|
77 | - } |
|
78 | - |
|
79 | - $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); |
|
80 | - if (!$servers) { |
|
81 | - $server = \OC::$server->getSystemConfig()->getValue('memcached_server'); |
|
82 | - if ($server) { |
|
83 | - $servers = [$server]; |
|
84 | - } else { |
|
85 | - $servers = [['localhost', 11211]]; |
|
86 | - } |
|
87 | - } |
|
88 | - self::$cache->addServers($servers); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * entries in XCache gets namespaced to prevent collisions between owncloud instances and users |
|
94 | - */ |
|
95 | - protected function getNameSpace() { |
|
96 | - return $this->prefix; |
|
97 | - } |
|
98 | - |
|
99 | - public function get($key) { |
|
100 | - $result = self::$cache->get($this->getNamespace() . $key); |
|
101 | - if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
|
102 | - return null; |
|
103 | - } else { |
|
104 | - return $result; |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - public function set($key, $value, $ttl = 0) { |
|
109 | - if ($ttl > 0) { |
|
110 | - $result = self::$cache->set($this->getNamespace() . $key, $value, $ttl); |
|
111 | - } else { |
|
112 | - $result = self::$cache->set($this->getNamespace() . $key, $value); |
|
113 | - } |
|
114 | - if ($result !== true) { |
|
115 | - $this->verifyReturnCode(); |
|
116 | - } |
|
117 | - return $result; |
|
118 | - } |
|
119 | - |
|
120 | - public function hasKey($key) { |
|
121 | - self::$cache->get($this->getNamespace() . $key); |
|
122 | - return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
|
123 | - } |
|
124 | - |
|
125 | - public function remove($key) { |
|
126 | - $result= self::$cache->delete($this->getNamespace() . $key); |
|
127 | - if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
|
128 | - $this->verifyReturnCode(); |
|
129 | - } |
|
130 | - return $result; |
|
131 | - } |
|
132 | - |
|
133 | - public function clear($prefix = '') { |
|
134 | - $prefix = $this->getNamespace() . $prefix; |
|
135 | - $allKeys = self::$cache->getAllKeys(); |
|
136 | - if ($allKeys === false) { |
|
137 | - // newer Memcached doesn't like getAllKeys(), flush everything |
|
138 | - self::$cache->flush(); |
|
139 | - return true; |
|
140 | - } |
|
141 | - $keys = array(); |
|
142 | - $prefixLength = strlen($prefix); |
|
143 | - foreach ($allKeys as $key) { |
|
144 | - if (substr($key, 0, $prefixLength) === $prefix) { |
|
145 | - $keys[] = $key; |
|
146 | - } |
|
147 | - } |
|
148 | - if (method_exists(self::$cache, 'deleteMulti')) { |
|
149 | - self::$cache->deleteMulti($keys); |
|
150 | - } else { |
|
151 | - foreach ($keys as $key) { |
|
152 | - self::$cache->delete($key); |
|
153 | - } |
|
154 | - } |
|
155 | - return true; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Set a value in the cache if it's not already stored |
|
160 | - * |
|
161 | - * @param string $key |
|
162 | - * @param mixed $value |
|
163 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
164 | - * @return bool |
|
165 | - * @throws \Exception |
|
166 | - */ |
|
167 | - public function add($key, $value, $ttl = 0) { |
|
168 | - $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
169 | - if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
|
170 | - $this->verifyReturnCode(); |
|
171 | - } |
|
172 | - return $result; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Increase a stored number |
|
177 | - * |
|
178 | - * @param string $key |
|
179 | - * @param int $step |
|
180 | - * @return int | bool |
|
181 | - */ |
|
182 | - public function inc($key, $step = 1) { |
|
183 | - $this->add($key, 0); |
|
184 | - $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
185 | - |
|
186 | - if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
187 | - return false; |
|
188 | - } |
|
189 | - |
|
190 | - return $result; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Decrease a stored number |
|
195 | - * |
|
196 | - * @param string $key |
|
197 | - * @param int $step |
|
198 | - * @return int | bool |
|
199 | - */ |
|
200 | - public function dec($key, $step = 1) { |
|
201 | - $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
202 | - |
|
203 | - if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
204 | - return false; |
|
205 | - } |
|
206 | - |
|
207 | - return $result; |
|
208 | - } |
|
209 | - |
|
210 | - static public function isAvailable() { |
|
211 | - return extension_loaded('memcached'); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @throws \Exception |
|
216 | - */ |
|
217 | - private function verifyReturnCode() { |
|
218 | - $code = self::$cache->getResultCode(); |
|
219 | - if ($code === \Memcached::RES_SUCCESS) { |
|
220 | - return; |
|
221 | - } |
|
222 | - $message = self::$cache->getResultMessage(); |
|
223 | - throw new \Exception("Error $code interacting with memcached : $message"); |
|
224 | - } |
|
36 | + use CASTrait; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var \Memcached $cache |
|
40 | + */ |
|
41 | + private static $cache = null; |
|
42 | + |
|
43 | + use CADTrait; |
|
44 | + |
|
45 | + public function __construct($prefix = '') { |
|
46 | + parent::__construct($prefix); |
|
47 | + if (is_null(self::$cache)) { |
|
48 | + self::$cache = new \Memcached(); |
|
49 | + |
|
50 | + $defaultOptions = [ |
|
51 | + \Memcached::OPT_CONNECT_TIMEOUT => 50, |
|
52 | + \Memcached::OPT_RETRY_TIMEOUT => 50, |
|
53 | + \Memcached::OPT_SEND_TIMEOUT => 50, |
|
54 | + \Memcached::OPT_RECV_TIMEOUT => 50, |
|
55 | + \Memcached::OPT_POLL_TIMEOUT => 50, |
|
56 | + |
|
57 | + // Enable compression |
|
58 | + \Memcached::OPT_COMPRESSION => true, |
|
59 | + |
|
60 | + // Turn on consistent hashing |
|
61 | + \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
|
62 | + |
|
63 | + // Enable Binary Protocol |
|
64 | + //\Memcached::OPT_BINARY_PROTOCOL => true, |
|
65 | + ]; |
|
66 | + // by default enable igbinary serializer if available |
|
67 | + if (\Memcached::HAVE_IGBINARY) { |
|
68 | + $defaultOptions[\Memcached::OPT_SERIALIZER] = |
|
69 | + \Memcached::SERIALIZER_IGBINARY; |
|
70 | + } |
|
71 | + $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); |
|
72 | + if (is_array($options)) { |
|
73 | + $options = $options + $defaultOptions; |
|
74 | + self::$cache->setOptions($options); |
|
75 | + } else { |
|
76 | + throw new HintException("Expected 'memcached_options' config to be an array, got $options"); |
|
77 | + } |
|
78 | + |
|
79 | + $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); |
|
80 | + if (!$servers) { |
|
81 | + $server = \OC::$server->getSystemConfig()->getValue('memcached_server'); |
|
82 | + if ($server) { |
|
83 | + $servers = [$server]; |
|
84 | + } else { |
|
85 | + $servers = [['localhost', 11211]]; |
|
86 | + } |
|
87 | + } |
|
88 | + self::$cache->addServers($servers); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * entries in XCache gets namespaced to prevent collisions between owncloud instances and users |
|
94 | + */ |
|
95 | + protected function getNameSpace() { |
|
96 | + return $this->prefix; |
|
97 | + } |
|
98 | + |
|
99 | + public function get($key) { |
|
100 | + $result = self::$cache->get($this->getNamespace() . $key); |
|
101 | + if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
|
102 | + return null; |
|
103 | + } else { |
|
104 | + return $result; |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + public function set($key, $value, $ttl = 0) { |
|
109 | + if ($ttl > 0) { |
|
110 | + $result = self::$cache->set($this->getNamespace() . $key, $value, $ttl); |
|
111 | + } else { |
|
112 | + $result = self::$cache->set($this->getNamespace() . $key, $value); |
|
113 | + } |
|
114 | + if ($result !== true) { |
|
115 | + $this->verifyReturnCode(); |
|
116 | + } |
|
117 | + return $result; |
|
118 | + } |
|
119 | + |
|
120 | + public function hasKey($key) { |
|
121 | + self::$cache->get($this->getNamespace() . $key); |
|
122 | + return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
|
123 | + } |
|
124 | + |
|
125 | + public function remove($key) { |
|
126 | + $result= self::$cache->delete($this->getNamespace() . $key); |
|
127 | + if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
|
128 | + $this->verifyReturnCode(); |
|
129 | + } |
|
130 | + return $result; |
|
131 | + } |
|
132 | + |
|
133 | + public function clear($prefix = '') { |
|
134 | + $prefix = $this->getNamespace() . $prefix; |
|
135 | + $allKeys = self::$cache->getAllKeys(); |
|
136 | + if ($allKeys === false) { |
|
137 | + // newer Memcached doesn't like getAllKeys(), flush everything |
|
138 | + self::$cache->flush(); |
|
139 | + return true; |
|
140 | + } |
|
141 | + $keys = array(); |
|
142 | + $prefixLength = strlen($prefix); |
|
143 | + foreach ($allKeys as $key) { |
|
144 | + if (substr($key, 0, $prefixLength) === $prefix) { |
|
145 | + $keys[] = $key; |
|
146 | + } |
|
147 | + } |
|
148 | + if (method_exists(self::$cache, 'deleteMulti')) { |
|
149 | + self::$cache->deleteMulti($keys); |
|
150 | + } else { |
|
151 | + foreach ($keys as $key) { |
|
152 | + self::$cache->delete($key); |
|
153 | + } |
|
154 | + } |
|
155 | + return true; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Set a value in the cache if it's not already stored |
|
160 | + * |
|
161 | + * @param string $key |
|
162 | + * @param mixed $value |
|
163 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
164 | + * @return bool |
|
165 | + * @throws \Exception |
|
166 | + */ |
|
167 | + public function add($key, $value, $ttl = 0) { |
|
168 | + $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
169 | + if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
|
170 | + $this->verifyReturnCode(); |
|
171 | + } |
|
172 | + return $result; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Increase a stored number |
|
177 | + * |
|
178 | + * @param string $key |
|
179 | + * @param int $step |
|
180 | + * @return int | bool |
|
181 | + */ |
|
182 | + public function inc($key, $step = 1) { |
|
183 | + $this->add($key, 0); |
|
184 | + $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
185 | + |
|
186 | + if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
187 | + return false; |
|
188 | + } |
|
189 | + |
|
190 | + return $result; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Decrease a stored number |
|
195 | + * |
|
196 | + * @param string $key |
|
197 | + * @param int $step |
|
198 | + * @return int | bool |
|
199 | + */ |
|
200 | + public function dec($key, $step = 1) { |
|
201 | + $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
202 | + |
|
203 | + if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
204 | + return false; |
|
205 | + } |
|
206 | + |
|
207 | + return $result; |
|
208 | + } |
|
209 | + |
|
210 | + static public function isAvailable() { |
|
211 | + return extension_loaded('memcached'); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @throws \Exception |
|
216 | + */ |
|
217 | + private function verifyReturnCode() { |
|
218 | + $code = self::$cache->getResultCode(); |
|
219 | + if ($code === \Memcached::RES_SUCCESS) { |
|
220 | + return; |
|
221 | + } |
|
222 | + $message = self::$cache->getResultMessage(); |
|
223 | + throw new \Exception("Error $code interacting with memcached : $message"); |
|
224 | + } |
|
225 | 225 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | } |
98 | 98 | |
99 | 99 | public function get($key) { |
100 | - $result = self::$cache->get($this->getNamespace() . $key); |
|
100 | + $result = self::$cache->get($this->getNamespace().$key); |
|
101 | 101 | if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
102 | 102 | return null; |
103 | 103 | } else { |
@@ -107,9 +107,9 @@ discard block |
||
107 | 107 | |
108 | 108 | public function set($key, $value, $ttl = 0) { |
109 | 109 | if ($ttl > 0) { |
110 | - $result = self::$cache->set($this->getNamespace() . $key, $value, $ttl); |
|
110 | + $result = self::$cache->set($this->getNamespace().$key, $value, $ttl); |
|
111 | 111 | } else { |
112 | - $result = self::$cache->set($this->getNamespace() . $key, $value); |
|
112 | + $result = self::$cache->set($this->getNamespace().$key, $value); |
|
113 | 113 | } |
114 | 114 | if ($result !== true) { |
115 | 115 | $this->verifyReturnCode(); |
@@ -118,12 +118,12 @@ discard block |
||
118 | 118 | } |
119 | 119 | |
120 | 120 | public function hasKey($key) { |
121 | - self::$cache->get($this->getNamespace() . $key); |
|
121 | + self::$cache->get($this->getNamespace().$key); |
|
122 | 122 | return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
123 | 123 | } |
124 | 124 | |
125 | 125 | public function remove($key) { |
126 | - $result= self::$cache->delete($this->getNamespace() . $key); |
|
126 | + $result = self::$cache->delete($this->getNamespace().$key); |
|
127 | 127 | if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
128 | 128 | $this->verifyReturnCode(); |
129 | 129 | } |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | } |
132 | 132 | |
133 | 133 | public function clear($prefix = '') { |
134 | - $prefix = $this->getNamespace() . $prefix; |
|
134 | + $prefix = $this->getNamespace().$prefix; |
|
135 | 135 | $allKeys = self::$cache->getAllKeys(); |
136 | 136 | if ($allKeys === false) { |
137 | 137 | // newer Memcached doesn't like getAllKeys(), flush everything |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | * @throws \Exception |
166 | 166 | */ |
167 | 167 | public function add($key, $value, $ttl = 0) { |
168 | - $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
168 | + $result = self::$cache->add($this->getPrefix().$key, $value, $ttl); |
|
169 | 169 | if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
170 | 170 | $this->verifyReturnCode(); |
171 | 171 | } |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | */ |
182 | 182 | public function inc($key, $step = 1) { |
183 | 183 | $this->add($key, 0); |
184 | - $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
184 | + $result = self::$cache->increment($this->getPrefix().$key, $step); |
|
185 | 185 | |
186 | 186 | if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
187 | 187 | return false; |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | * @return int | bool |
199 | 199 | */ |
200 | 200 | public function dec($key, $step = 1) { |
201 | - $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
201 | + $result = self::$cache->decrement($this->getPrefix().$key, $step); |
|
202 | 202 | |
203 | 203 | if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
204 | 204 | return false; |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | * Returns expensive repair steps to be run on the |
140 | 140 | * command line with a special option. |
141 | 141 | * |
142 | - * @return IRepairStep[] |
|
142 | + * @return OldGroupMembershipShares[] |
|
143 | 143 | */ |
144 | 144 | public static function getExpensiveRepairSteps() { |
145 | 145 | return [ |
@@ -216,7 +216,6 @@ discard block |
||
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
219 | - * @param int $max |
|
220 | 219 | */ |
221 | 220 | public function finishProgress() { |
222 | 221 | // for now just emit as we did in the past |
@@ -59,194 +59,194 @@ |
||
59 | 59 | use Symfony\Component\EventDispatcher\GenericEvent; |
60 | 60 | |
61 | 61 | class Repair implements IOutput{ |
62 | - /* @var IRepairStep[] */ |
|
63 | - private $repairSteps; |
|
64 | - /** @var EventDispatcher */ |
|
65 | - private $dispatcher; |
|
66 | - /** @var string */ |
|
67 | - private $currentStep; |
|
68 | - |
|
69 | - /** |
|
70 | - * Creates a new repair step runner |
|
71 | - * |
|
72 | - * @param IRepairStep[] $repairSteps array of RepairStep instances |
|
73 | - * @param EventDispatcher $dispatcher |
|
74 | - */ |
|
75 | - public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) { |
|
76 | - $this->repairSteps = $repairSteps; |
|
77 | - $this->dispatcher = $dispatcher; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Run a series of repair steps for common problems |
|
82 | - */ |
|
83 | - public function run() { |
|
84 | - if (count($this->repairSteps) === 0) { |
|
85 | - $this->emit('\OC\Repair', 'info', array('No repair steps available')); |
|
86 | - return; |
|
87 | - } |
|
88 | - // run each repair step |
|
89 | - foreach ($this->repairSteps as $step) { |
|
90 | - $this->currentStep = $step->getName(); |
|
91 | - $this->emit('\OC\Repair', 'step', [$this->currentStep]); |
|
92 | - $step->run($this); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Add repair step |
|
98 | - * |
|
99 | - * @param IRepairStep|string $repairStep repair step |
|
100 | - * @throws \Exception |
|
101 | - */ |
|
102 | - public function addStep($repairStep) { |
|
103 | - if (is_string($repairStep)) { |
|
104 | - try { |
|
105 | - $s = \OC::$server->query($repairStep); |
|
106 | - } catch (QueryException $e) { |
|
107 | - if (class_exists($repairStep)) { |
|
108 | - $s = new $repairStep(); |
|
109 | - } else { |
|
110 | - throw new \Exception("Repair step '$repairStep' is unknown"); |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - if ($s instanceof IRepairStep) { |
|
115 | - $this->repairSteps[] = $s; |
|
116 | - } else { |
|
117 | - throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep"); |
|
118 | - } |
|
119 | - } else { |
|
120 | - $this->repairSteps[] = $repairStep; |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Returns the default repair steps to be run on the |
|
126 | - * command line or after an upgrade. |
|
127 | - * |
|
128 | - * @return IRepairStep[] |
|
129 | - */ |
|
130 | - public static function getRepairSteps() { |
|
131 | - return [ |
|
132 | - new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false), |
|
133 | - new RepairMimeTypes(\OC::$server->getConfig()), |
|
134 | - new AssetCache(), |
|
135 | - new FillETags(\OC::$server->getDatabaseConnection()), |
|
136 | - new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()), |
|
137 | - new DropOldTables(\OC::$server->getDatabaseConnection()), |
|
138 | - new DropOldJobs(\OC::$server->getJobList()), |
|
139 | - new RemoveGetETagEntries(\OC::$server->getDatabaseConnection()), |
|
140 | - new UpdateOutdatedOcsIds(\OC::$server->getConfig()), |
|
141 | - new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()), |
|
142 | - new SharePropagation(\OC::$server->getConfig()), |
|
143 | - new RemoveOldShares(\OC::$server->getDatabaseConnection()), |
|
144 | - new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()), |
|
145 | - new RepairUnmergedShares( |
|
146 | - \OC::$server->getConfig(), |
|
147 | - \OC::$server->getDatabaseConnection(), |
|
148 | - \OC::$server->getUserManager(), |
|
149 | - \OC::$server->getGroupManager() |
|
150 | - ), |
|
151 | - new MoveUpdaterStepFile(\OC::$server->getConfig()), |
|
152 | - new MoveAvatars( |
|
153 | - \OC::$server->getJobList(), |
|
154 | - \OC::$server->getConfig() |
|
155 | - ), |
|
156 | - new CleanPreviews( |
|
157 | - \OC::$server->getJobList(), |
|
158 | - \OC::$server->getUserManager(), |
|
159 | - \OC::$server->getConfig() |
|
160 | - ), |
|
161 | - new FixMountStorages(\OC::$server->getDatabaseConnection()), |
|
162 | - ]; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Returns expensive repair steps to be run on the |
|
167 | - * command line with a special option. |
|
168 | - * |
|
169 | - * @return IRepairStep[] |
|
170 | - */ |
|
171 | - public static function getExpensiveRepairSteps() { |
|
172 | - return [ |
|
173 | - new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()), |
|
174 | - ]; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Returns the repair steps to be run before an |
|
179 | - * upgrade. |
|
180 | - * |
|
181 | - * @return IRepairStep[] |
|
182 | - */ |
|
183 | - public static function getBeforeUpgradeRepairSteps() { |
|
184 | - $connection = \OC::$server->getDatabaseConnection(); |
|
185 | - $steps = [ |
|
186 | - new InnoDB(), |
|
187 | - new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true), |
|
188 | - new SqliteAutoincrement($connection), |
|
189 | - new SearchLuceneTables(), |
|
190 | - ]; |
|
191 | - |
|
192 | - //There is no need to delete all previews on every single update |
|
193 | - //only 7.0.0 through 7.0.2 generated broken previews |
|
194 | - $currentVersion = \OC::$server->getConfig()->getSystemValue('version'); |
|
195 | - if (version_compare($currentVersion, '7.0.0.0', '>=') && |
|
196 | - version_compare($currentVersion, '7.0.3.4', '<=')) { |
|
197 | - $steps[] = new \OC\Repair\Preview(); |
|
198 | - } |
|
199 | - |
|
200 | - return $steps; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @param string $scope |
|
205 | - * @param string $method |
|
206 | - * @param array $arguments |
|
207 | - */ |
|
208 | - public function emit($scope, $method, array $arguments = []) { |
|
209 | - if (!is_null($this->dispatcher)) { |
|
210 | - $this->dispatcher->dispatch("$scope::$method", |
|
211 | - new GenericEvent("$scope::$method", $arguments)); |
|
212 | - } |
|
213 | - } |
|
214 | - |
|
215 | - public function info($string) { |
|
216 | - // for now just emit as we did in the past |
|
217 | - $this->emit('\OC\Repair', 'info', array($string)); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @param string $message |
|
222 | - */ |
|
223 | - public function warning($message) { |
|
224 | - // for now just emit as we did in the past |
|
225 | - $this->emit('\OC\Repair', 'warning', [$message]); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @param int $max |
|
230 | - */ |
|
231 | - public function startProgress($max = 0) { |
|
232 | - // for now just emit as we did in the past |
|
233 | - $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * @param int $step |
|
238 | - * @param string $description |
|
239 | - */ |
|
240 | - public function advance($step = 1, $description = '') { |
|
241 | - // for now just emit as we did in the past |
|
242 | - $this->emit('\OC\Repair', 'advance', [$step, $description]); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * @param int $max |
|
247 | - */ |
|
248 | - public function finishProgress() { |
|
249 | - // for now just emit as we did in the past |
|
250 | - $this->emit('\OC\Repair', 'finishProgress', []); |
|
251 | - } |
|
62 | + /* @var IRepairStep[] */ |
|
63 | + private $repairSteps; |
|
64 | + /** @var EventDispatcher */ |
|
65 | + private $dispatcher; |
|
66 | + /** @var string */ |
|
67 | + private $currentStep; |
|
68 | + |
|
69 | + /** |
|
70 | + * Creates a new repair step runner |
|
71 | + * |
|
72 | + * @param IRepairStep[] $repairSteps array of RepairStep instances |
|
73 | + * @param EventDispatcher $dispatcher |
|
74 | + */ |
|
75 | + public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) { |
|
76 | + $this->repairSteps = $repairSteps; |
|
77 | + $this->dispatcher = $dispatcher; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Run a series of repair steps for common problems |
|
82 | + */ |
|
83 | + public function run() { |
|
84 | + if (count($this->repairSteps) === 0) { |
|
85 | + $this->emit('\OC\Repair', 'info', array('No repair steps available')); |
|
86 | + return; |
|
87 | + } |
|
88 | + // run each repair step |
|
89 | + foreach ($this->repairSteps as $step) { |
|
90 | + $this->currentStep = $step->getName(); |
|
91 | + $this->emit('\OC\Repair', 'step', [$this->currentStep]); |
|
92 | + $step->run($this); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Add repair step |
|
98 | + * |
|
99 | + * @param IRepairStep|string $repairStep repair step |
|
100 | + * @throws \Exception |
|
101 | + */ |
|
102 | + public function addStep($repairStep) { |
|
103 | + if (is_string($repairStep)) { |
|
104 | + try { |
|
105 | + $s = \OC::$server->query($repairStep); |
|
106 | + } catch (QueryException $e) { |
|
107 | + if (class_exists($repairStep)) { |
|
108 | + $s = new $repairStep(); |
|
109 | + } else { |
|
110 | + throw new \Exception("Repair step '$repairStep' is unknown"); |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + if ($s instanceof IRepairStep) { |
|
115 | + $this->repairSteps[] = $s; |
|
116 | + } else { |
|
117 | + throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep"); |
|
118 | + } |
|
119 | + } else { |
|
120 | + $this->repairSteps[] = $repairStep; |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Returns the default repair steps to be run on the |
|
126 | + * command line or after an upgrade. |
|
127 | + * |
|
128 | + * @return IRepairStep[] |
|
129 | + */ |
|
130 | + public static function getRepairSteps() { |
|
131 | + return [ |
|
132 | + new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false), |
|
133 | + new RepairMimeTypes(\OC::$server->getConfig()), |
|
134 | + new AssetCache(), |
|
135 | + new FillETags(\OC::$server->getDatabaseConnection()), |
|
136 | + new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()), |
|
137 | + new DropOldTables(\OC::$server->getDatabaseConnection()), |
|
138 | + new DropOldJobs(\OC::$server->getJobList()), |
|
139 | + new RemoveGetETagEntries(\OC::$server->getDatabaseConnection()), |
|
140 | + new UpdateOutdatedOcsIds(\OC::$server->getConfig()), |
|
141 | + new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()), |
|
142 | + new SharePropagation(\OC::$server->getConfig()), |
|
143 | + new RemoveOldShares(\OC::$server->getDatabaseConnection()), |
|
144 | + new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()), |
|
145 | + new RepairUnmergedShares( |
|
146 | + \OC::$server->getConfig(), |
|
147 | + \OC::$server->getDatabaseConnection(), |
|
148 | + \OC::$server->getUserManager(), |
|
149 | + \OC::$server->getGroupManager() |
|
150 | + ), |
|
151 | + new MoveUpdaterStepFile(\OC::$server->getConfig()), |
|
152 | + new MoveAvatars( |
|
153 | + \OC::$server->getJobList(), |
|
154 | + \OC::$server->getConfig() |
|
155 | + ), |
|
156 | + new CleanPreviews( |
|
157 | + \OC::$server->getJobList(), |
|
158 | + \OC::$server->getUserManager(), |
|
159 | + \OC::$server->getConfig() |
|
160 | + ), |
|
161 | + new FixMountStorages(\OC::$server->getDatabaseConnection()), |
|
162 | + ]; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Returns expensive repair steps to be run on the |
|
167 | + * command line with a special option. |
|
168 | + * |
|
169 | + * @return IRepairStep[] |
|
170 | + */ |
|
171 | + public static function getExpensiveRepairSteps() { |
|
172 | + return [ |
|
173 | + new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()), |
|
174 | + ]; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Returns the repair steps to be run before an |
|
179 | + * upgrade. |
|
180 | + * |
|
181 | + * @return IRepairStep[] |
|
182 | + */ |
|
183 | + public static function getBeforeUpgradeRepairSteps() { |
|
184 | + $connection = \OC::$server->getDatabaseConnection(); |
|
185 | + $steps = [ |
|
186 | + new InnoDB(), |
|
187 | + new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true), |
|
188 | + new SqliteAutoincrement($connection), |
|
189 | + new SearchLuceneTables(), |
|
190 | + ]; |
|
191 | + |
|
192 | + //There is no need to delete all previews on every single update |
|
193 | + //only 7.0.0 through 7.0.2 generated broken previews |
|
194 | + $currentVersion = \OC::$server->getConfig()->getSystemValue('version'); |
|
195 | + if (version_compare($currentVersion, '7.0.0.0', '>=') && |
|
196 | + version_compare($currentVersion, '7.0.3.4', '<=')) { |
|
197 | + $steps[] = new \OC\Repair\Preview(); |
|
198 | + } |
|
199 | + |
|
200 | + return $steps; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @param string $scope |
|
205 | + * @param string $method |
|
206 | + * @param array $arguments |
|
207 | + */ |
|
208 | + public function emit($scope, $method, array $arguments = []) { |
|
209 | + if (!is_null($this->dispatcher)) { |
|
210 | + $this->dispatcher->dispatch("$scope::$method", |
|
211 | + new GenericEvent("$scope::$method", $arguments)); |
|
212 | + } |
|
213 | + } |
|
214 | + |
|
215 | + public function info($string) { |
|
216 | + // for now just emit as we did in the past |
|
217 | + $this->emit('\OC\Repair', 'info', array($string)); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @param string $message |
|
222 | + */ |
|
223 | + public function warning($message) { |
|
224 | + // for now just emit as we did in the past |
|
225 | + $this->emit('\OC\Repair', 'warning', [$message]); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @param int $max |
|
230 | + */ |
|
231 | + public function startProgress($max = 0) { |
|
232 | + // for now just emit as we did in the past |
|
233 | + $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * @param int $step |
|
238 | + * @param string $description |
|
239 | + */ |
|
240 | + public function advance($step = 1, $description = '') { |
|
241 | + // for now just emit as we did in the past |
|
242 | + $this->emit('\OC\Repair', 'advance', [$step, $description]); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * @param int $max |
|
247 | + */ |
|
248 | + public function finishProgress() { |
|
249 | + // for now just emit as we did in the past |
|
250 | + $this->emit('\OC\Repair', 'finishProgress', []); |
|
251 | + } |
|
252 | 252 | } |
@@ -58,7 +58,7 @@ |
||
58 | 58 | use Symfony\Component\EventDispatcher\EventDispatcher; |
59 | 59 | use Symfony\Component\EventDispatcher\GenericEvent; |
60 | 60 | |
61 | -class Repair implements IOutput{ |
|
61 | +class Repair implements IOutput { |
|
62 | 62 | /* @var IRepairStep[] */ |
63 | 63 | private $repairSteps; |
64 | 64 | /** @var EventDispatcher */ |
@@ -53,7 +53,7 @@ |
||
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
56 | - * @param \Doctrine\DBAL\Connection $connection |
|
56 | + * @param \OCP\IDBConnection $connection |
|
57 | 57 | * @return string[] |
58 | 58 | */ |
59 | 59 | private function getAllMyIsamTables($connection) { |
@@ -30,41 +30,41 @@ |
||
30 | 30 | |
31 | 31 | class InnoDB implements IRepairStep { |
32 | 32 | |
33 | - public function getName() { |
|
34 | - return 'Repair MySQL database engine'; |
|
35 | - } |
|
33 | + public function getName() { |
|
34 | + return 'Repair MySQL database engine'; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Fix mime types |
|
39 | - */ |
|
40 | - public function run(IOutput $output) { |
|
41 | - $connection = \OC::$server->getDatabaseConnection(); |
|
42 | - if (!$connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
43 | - $output->info('Not a mysql database -> nothing to do'); |
|
44 | - return; |
|
45 | - } |
|
37 | + /** |
|
38 | + * Fix mime types |
|
39 | + */ |
|
40 | + public function run(IOutput $output) { |
|
41 | + $connection = \OC::$server->getDatabaseConnection(); |
|
42 | + if (!$connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
43 | + $output->info('Not a mysql database -> nothing to do'); |
|
44 | + return; |
|
45 | + } |
|
46 | 46 | |
47 | - $tables = $this->getAllMyIsamTables($connection); |
|
48 | - if (is_array($tables)) { |
|
49 | - foreach ($tables as $table) { |
|
50 | - $connection->exec("ALTER TABLE $table ENGINE=InnoDB;"); |
|
51 | - $output->info("Fixed $table"); |
|
52 | - } |
|
53 | - } |
|
54 | - } |
|
47 | + $tables = $this->getAllMyIsamTables($connection); |
|
48 | + if (is_array($tables)) { |
|
49 | + foreach ($tables as $table) { |
|
50 | + $connection->exec("ALTER TABLE $table ENGINE=InnoDB;"); |
|
51 | + $output->info("Fixed $table"); |
|
52 | + } |
|
53 | + } |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param \Doctrine\DBAL\Connection $connection |
|
58 | - * @return string[] |
|
59 | - */ |
|
60 | - private function getAllMyIsamTables($connection) { |
|
61 | - $dbName = \OC::$server->getConfig()->getSystemValue("dbname"); |
|
62 | - $result = $connection->fetchArray( |
|
63 | - "SELECT table_name FROM information_schema.tables WHERE table_schema = ? AND engine = 'MyISAM' AND TABLE_NAME LIKE \"*PREFIX*%\"", |
|
64 | - array($dbName) |
|
65 | - ); |
|
56 | + /** |
|
57 | + * @param \Doctrine\DBAL\Connection $connection |
|
58 | + * @return string[] |
|
59 | + */ |
|
60 | + private function getAllMyIsamTables($connection) { |
|
61 | + $dbName = \OC::$server->getConfig()->getSystemValue("dbname"); |
|
62 | + $result = $connection->fetchArray( |
|
63 | + "SELECT table_name FROM information_schema.tables WHERE table_schema = ? AND engine = 'MyISAM' AND TABLE_NAME LIKE \"*PREFIX*%\"", |
|
64 | + array($dbName) |
|
65 | + ); |
|
66 | 66 | |
67 | - return $result; |
|
68 | - } |
|
67 | + return $result; |
|
68 | + } |
|
69 | 69 | } |
70 | 70 |
@@ -24,7 +24,6 @@ |
||
24 | 24 | use OCP\Security\ICrypto; |
25 | 25 | use OCP\IDBConnection; |
26 | 26 | use OCP\Security\ICredentialsManager; |
27 | -use OCP\IConfig; |
|
28 | 27 | |
29 | 28 | /** |
30 | 29 | * Store and retrieve credentials for external services |
@@ -34,93 +34,93 @@ |
||
34 | 34 | */ |
35 | 35 | class CredentialsManager implements ICredentialsManager { |
36 | 36 | |
37 | - const DB_TABLE = 'credentials'; |
|
37 | + const DB_TABLE = 'credentials'; |
|
38 | 38 | |
39 | - /** @var ICrypto */ |
|
40 | - protected $crypto; |
|
39 | + /** @var ICrypto */ |
|
40 | + protected $crypto; |
|
41 | 41 | |
42 | - /** @var IDBConnection */ |
|
43 | - protected $dbConnection; |
|
42 | + /** @var IDBConnection */ |
|
43 | + protected $dbConnection; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param ICrypto $crypto |
|
47 | - * @param IDBConnection $dbConnection |
|
48 | - */ |
|
49 | - public function __construct(ICrypto $crypto, IDBConnection $dbConnection) { |
|
50 | - $this->crypto = $crypto; |
|
51 | - $this->dbConnection = $dbConnection; |
|
52 | - } |
|
45 | + /** |
|
46 | + * @param ICrypto $crypto |
|
47 | + * @param IDBConnection $dbConnection |
|
48 | + */ |
|
49 | + public function __construct(ICrypto $crypto, IDBConnection $dbConnection) { |
|
50 | + $this->crypto = $crypto; |
|
51 | + $this->dbConnection = $dbConnection; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Store a set of credentials |
|
56 | - * |
|
57 | - * @param string|null $userId Null for system-wide credentials |
|
58 | - * @param string $identifier |
|
59 | - * @param mixed $credentials |
|
60 | - */ |
|
61 | - public function store($userId, $identifier, $credentials) { |
|
62 | - $value = $this->crypto->encrypt(json_encode($credentials)); |
|
54 | + /** |
|
55 | + * Store a set of credentials |
|
56 | + * |
|
57 | + * @param string|null $userId Null for system-wide credentials |
|
58 | + * @param string $identifier |
|
59 | + * @param mixed $credentials |
|
60 | + */ |
|
61 | + public function store($userId, $identifier, $credentials) { |
|
62 | + $value = $this->crypto->encrypt(json_encode($credentials)); |
|
63 | 63 | |
64 | - $this->dbConnection->setValues(self::DB_TABLE, [ |
|
65 | - 'user' => $userId, |
|
66 | - 'identifier' => $identifier, |
|
67 | - ], [ |
|
68 | - 'credentials' => $value, |
|
69 | - ]); |
|
70 | - } |
|
64 | + $this->dbConnection->setValues(self::DB_TABLE, [ |
|
65 | + 'user' => $userId, |
|
66 | + 'identifier' => $identifier, |
|
67 | + ], [ |
|
68 | + 'credentials' => $value, |
|
69 | + ]); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Retrieve a set of credentials |
|
74 | - * |
|
75 | - * @param string|null $userId Null for system-wide credentials |
|
76 | - * @param string $identifier |
|
77 | - * @return mixed |
|
78 | - */ |
|
79 | - public function retrieve($userId, $identifier) { |
|
80 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
81 | - $qb->select('credentials') |
|
82 | - ->from(self::DB_TABLE) |
|
83 | - ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
84 | - ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier))) |
|
85 | - ; |
|
86 | - $result = $qb->execute()->fetch(); |
|
72 | + /** |
|
73 | + * Retrieve a set of credentials |
|
74 | + * |
|
75 | + * @param string|null $userId Null for system-wide credentials |
|
76 | + * @param string $identifier |
|
77 | + * @return mixed |
|
78 | + */ |
|
79 | + public function retrieve($userId, $identifier) { |
|
80 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
81 | + $qb->select('credentials') |
|
82 | + ->from(self::DB_TABLE) |
|
83 | + ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
84 | + ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier))) |
|
85 | + ; |
|
86 | + $result = $qb->execute()->fetch(); |
|
87 | 87 | |
88 | - if (!$result) { |
|
89 | - return null; |
|
90 | - } |
|
91 | - $value = $result['credentials']; |
|
88 | + if (!$result) { |
|
89 | + return null; |
|
90 | + } |
|
91 | + $value = $result['credentials']; |
|
92 | 92 | |
93 | - return json_decode($this->crypto->decrypt($value), true); |
|
94 | - } |
|
93 | + return json_decode($this->crypto->decrypt($value), true); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Delete a set of credentials |
|
98 | - * |
|
99 | - * @param string|null $userId Null for system-wide credentials |
|
100 | - * @param string $identifier |
|
101 | - * @return int rows removed |
|
102 | - */ |
|
103 | - public function delete($userId, $identifier) { |
|
104 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
105 | - $qb->delete(self::DB_TABLE) |
|
106 | - ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
107 | - ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier))) |
|
108 | - ; |
|
109 | - return $qb->execute(); |
|
110 | - } |
|
96 | + /** |
|
97 | + * Delete a set of credentials |
|
98 | + * |
|
99 | + * @param string|null $userId Null for system-wide credentials |
|
100 | + * @param string $identifier |
|
101 | + * @return int rows removed |
|
102 | + */ |
|
103 | + public function delete($userId, $identifier) { |
|
104 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
105 | + $qb->delete(self::DB_TABLE) |
|
106 | + ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
107 | + ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier))) |
|
108 | + ; |
|
109 | + return $qb->execute(); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * Erase all credentials stored for a user |
|
114 | - * |
|
115 | - * @param string $userId |
|
116 | - * @return int rows removed |
|
117 | - */ |
|
118 | - public function erase($userId) { |
|
119 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
120 | - $qb->delete(self::DB_TABLE) |
|
121 | - ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
122 | - ; |
|
123 | - return $qb->execute(); |
|
124 | - } |
|
112 | + /** |
|
113 | + * Erase all credentials stored for a user |
|
114 | + * |
|
115 | + * @param string $userId |
|
116 | + * @return int rows removed |
|
117 | + */ |
|
118 | + public function erase($userId) { |
|
119 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
120 | + $qb->delete(self::DB_TABLE) |
|
121 | + ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
122 | + ; |
|
123 | + return $qb->execute(); |
|
124 | + } |
|
125 | 125 | |
126 | 126 | } |
@@ -176,7 +176,7 @@ |
||
176 | 176 | |
177 | 177 | /** |
178 | 178 | * @param mixed $offset |
179 | - * @return mixed |
|
179 | + * @return string|null |
|
180 | 180 | */ |
181 | 181 | public function offsetGet($offset) { |
182 | 182 | return $this->get($offset); |
@@ -34,167 +34,167 @@ |
||
34 | 34 | * @package OC\Session |
35 | 35 | */ |
36 | 36 | class CryptoSessionData implements \ArrayAccess, ISession { |
37 | - /** @var ISession */ |
|
38 | - protected $session; |
|
39 | - /** @var \OCP\Security\ICrypto */ |
|
40 | - protected $crypto; |
|
41 | - /** @var string */ |
|
42 | - protected $passphrase; |
|
43 | - /** @var array */ |
|
44 | - protected $sessionValues; |
|
45 | - /** @var bool */ |
|
46 | - protected $isModified = false; |
|
47 | - CONST encryptedSessionName = 'encrypted_session_data'; |
|
48 | - |
|
49 | - /** |
|
50 | - * @param ISession $session |
|
51 | - * @param ICrypto $crypto |
|
52 | - * @param string $passphrase |
|
53 | - */ |
|
54 | - public function __construct(ISession $session, |
|
55 | - ICrypto $crypto, |
|
56 | - $passphrase) { |
|
57 | - $this->crypto = $crypto; |
|
58 | - $this->session = $session; |
|
59 | - $this->passphrase = $passphrase; |
|
60 | - $this->initializeSession(); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Close session if class gets destructed |
|
65 | - */ |
|
66 | - public function __destruct() { |
|
67 | - $this->close(); |
|
68 | - } |
|
69 | - |
|
70 | - protected function initializeSession() { |
|
71 | - $encryptedSessionData = $this->session->get(self::encryptedSessionName); |
|
72 | - try { |
|
73 | - $this->sessionValues = json_decode( |
|
74 | - $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
75 | - true |
|
76 | - ); |
|
77 | - } catch (\Exception $e) { |
|
78 | - $this->sessionValues = []; |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Set a value in the session |
|
84 | - * |
|
85 | - * @param string $key |
|
86 | - * @param mixed $value |
|
87 | - */ |
|
88 | - public function set($key, $value) { |
|
89 | - $this->sessionValues[$key] = $value; |
|
90 | - $this->isModified = true; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Get a value from the session |
|
95 | - * |
|
96 | - * @param string $key |
|
97 | - * @return string|null Either the value or null |
|
98 | - */ |
|
99 | - public function get($key) { |
|
100 | - if(isset($this->sessionValues[$key])) { |
|
101 | - return $this->sessionValues[$key]; |
|
102 | - } |
|
103 | - |
|
104 | - return null; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Check if a named key exists in the session |
|
109 | - * |
|
110 | - * @param string $key |
|
111 | - * @return bool |
|
112 | - */ |
|
113 | - public function exists($key) { |
|
114 | - return isset($this->sessionValues[$key]); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Remove a $key/$value pair from the session |
|
119 | - * |
|
120 | - * @param string $key |
|
121 | - */ |
|
122 | - public function remove($key) { |
|
123 | - $this->isModified = true; |
|
124 | - unset($this->sessionValues[$key]); |
|
125 | - $this->session->remove(self::encryptedSessionName); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Reset and recreate the session |
|
130 | - */ |
|
131 | - public function clear() { |
|
132 | - $this->sessionValues = []; |
|
133 | - $this->isModified = true; |
|
134 | - $this->session->clear(); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Wrapper around session_regenerate_id |
|
139 | - * |
|
140 | - * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
141 | - * @return void |
|
142 | - */ |
|
143 | - public function regenerateId($deleteOldSession = true) { |
|
144 | - $this->session->regenerateId($deleteOldSession); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Wrapper around session_id |
|
149 | - * |
|
150 | - * @return string |
|
151 | - * @throws SessionNotAvailableException |
|
152 | - * @since 9.1.0 |
|
153 | - */ |
|
154 | - public function getId() { |
|
155 | - return $this->session->getId(); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Close the session and release the lock, also writes all changed data in batch |
|
160 | - */ |
|
161 | - public function close() { |
|
162 | - if($this->isModified) { |
|
163 | - $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
164 | - $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
165 | - $this->isModified = false; |
|
166 | - } |
|
167 | - $this->session->close(); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @param mixed $offset |
|
172 | - * @return bool |
|
173 | - */ |
|
174 | - public function offsetExists($offset) { |
|
175 | - return $this->exists($offset); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @param mixed $offset |
|
180 | - * @return mixed |
|
181 | - */ |
|
182 | - public function offsetGet($offset) { |
|
183 | - return $this->get($offset); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * @param mixed $offset |
|
188 | - * @param mixed $value |
|
189 | - */ |
|
190 | - public function offsetSet($offset, $value) { |
|
191 | - $this->set($offset, $value); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @param mixed $offset |
|
196 | - */ |
|
197 | - public function offsetUnset($offset) { |
|
198 | - $this->remove($offset); |
|
199 | - } |
|
37 | + /** @var ISession */ |
|
38 | + protected $session; |
|
39 | + /** @var \OCP\Security\ICrypto */ |
|
40 | + protected $crypto; |
|
41 | + /** @var string */ |
|
42 | + protected $passphrase; |
|
43 | + /** @var array */ |
|
44 | + protected $sessionValues; |
|
45 | + /** @var bool */ |
|
46 | + protected $isModified = false; |
|
47 | + CONST encryptedSessionName = 'encrypted_session_data'; |
|
48 | + |
|
49 | + /** |
|
50 | + * @param ISession $session |
|
51 | + * @param ICrypto $crypto |
|
52 | + * @param string $passphrase |
|
53 | + */ |
|
54 | + public function __construct(ISession $session, |
|
55 | + ICrypto $crypto, |
|
56 | + $passphrase) { |
|
57 | + $this->crypto = $crypto; |
|
58 | + $this->session = $session; |
|
59 | + $this->passphrase = $passphrase; |
|
60 | + $this->initializeSession(); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Close session if class gets destructed |
|
65 | + */ |
|
66 | + public function __destruct() { |
|
67 | + $this->close(); |
|
68 | + } |
|
69 | + |
|
70 | + protected function initializeSession() { |
|
71 | + $encryptedSessionData = $this->session->get(self::encryptedSessionName); |
|
72 | + try { |
|
73 | + $this->sessionValues = json_decode( |
|
74 | + $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
75 | + true |
|
76 | + ); |
|
77 | + } catch (\Exception $e) { |
|
78 | + $this->sessionValues = []; |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Set a value in the session |
|
84 | + * |
|
85 | + * @param string $key |
|
86 | + * @param mixed $value |
|
87 | + */ |
|
88 | + public function set($key, $value) { |
|
89 | + $this->sessionValues[$key] = $value; |
|
90 | + $this->isModified = true; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Get a value from the session |
|
95 | + * |
|
96 | + * @param string $key |
|
97 | + * @return string|null Either the value or null |
|
98 | + */ |
|
99 | + public function get($key) { |
|
100 | + if(isset($this->sessionValues[$key])) { |
|
101 | + return $this->sessionValues[$key]; |
|
102 | + } |
|
103 | + |
|
104 | + return null; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Check if a named key exists in the session |
|
109 | + * |
|
110 | + * @param string $key |
|
111 | + * @return bool |
|
112 | + */ |
|
113 | + public function exists($key) { |
|
114 | + return isset($this->sessionValues[$key]); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Remove a $key/$value pair from the session |
|
119 | + * |
|
120 | + * @param string $key |
|
121 | + */ |
|
122 | + public function remove($key) { |
|
123 | + $this->isModified = true; |
|
124 | + unset($this->sessionValues[$key]); |
|
125 | + $this->session->remove(self::encryptedSessionName); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Reset and recreate the session |
|
130 | + */ |
|
131 | + public function clear() { |
|
132 | + $this->sessionValues = []; |
|
133 | + $this->isModified = true; |
|
134 | + $this->session->clear(); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Wrapper around session_regenerate_id |
|
139 | + * |
|
140 | + * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
141 | + * @return void |
|
142 | + */ |
|
143 | + public function regenerateId($deleteOldSession = true) { |
|
144 | + $this->session->regenerateId($deleteOldSession); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Wrapper around session_id |
|
149 | + * |
|
150 | + * @return string |
|
151 | + * @throws SessionNotAvailableException |
|
152 | + * @since 9.1.0 |
|
153 | + */ |
|
154 | + public function getId() { |
|
155 | + return $this->session->getId(); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Close the session and release the lock, also writes all changed data in batch |
|
160 | + */ |
|
161 | + public function close() { |
|
162 | + if($this->isModified) { |
|
163 | + $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
164 | + $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
165 | + $this->isModified = false; |
|
166 | + } |
|
167 | + $this->session->close(); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @param mixed $offset |
|
172 | + * @return bool |
|
173 | + */ |
|
174 | + public function offsetExists($offset) { |
|
175 | + return $this->exists($offset); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @param mixed $offset |
|
180 | + * @return mixed |
|
181 | + */ |
|
182 | + public function offsetGet($offset) { |
|
183 | + return $this->get($offset); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * @param mixed $offset |
|
188 | + * @param mixed $value |
|
189 | + */ |
|
190 | + public function offsetSet($offset, $value) { |
|
191 | + $this->set($offset, $value); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @param mixed $offset |
|
196 | + */ |
|
197 | + public function offsetUnset($offset) { |
|
198 | + $this->remove($offset); |
|
199 | + } |
|
200 | 200 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | * @return string|null Either the value or null |
98 | 98 | */ |
99 | 99 | public function get($key) { |
100 | - if(isset($this->sessionValues[$key])) { |
|
100 | + if (isset($this->sessionValues[$key])) { |
|
101 | 101 | return $this->sessionValues[$key]; |
102 | 102 | } |
103 | 103 | |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | * Close the session and release the lock, also writes all changed data in batch |
160 | 160 | */ |
161 | 161 | public function close() { |
162 | - if($this->isModified) { |
|
162 | + if ($this->isModified) { |
|
163 | 163 | $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
164 | 164 | $this->session->set(self::encryptedSessionName, $encryptedValue); |
165 | 165 | $this->isModified = false; |
@@ -30,7 +30,6 @@ |
||
30 | 30 | use OCP\SystemTag\TagAlreadyExistsException; |
31 | 31 | use OCP\SystemTag\TagNotFoundException; |
32 | 32 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
33 | -use OCP\IUserManager; |
|
34 | 33 | use OCP\IGroupManager; |
35 | 34 | use OCP\SystemTag\ISystemTag; |
36 | 35 | use OCP\IUser; |
@@ -41,394 +41,394 @@ |
||
41 | 41 | */ |
42 | 42 | class SystemTagManager implements ISystemTagManager { |
43 | 43 | |
44 | - const TAG_TABLE = 'systemtag'; |
|
45 | - const TAG_GROUP_TABLE = 'systemtag_group'; |
|
46 | - |
|
47 | - /** @var IDBConnection */ |
|
48 | - protected $connection; |
|
49 | - |
|
50 | - /** @var EventDispatcherInterface */ |
|
51 | - protected $dispatcher; |
|
52 | - |
|
53 | - /** @var IGroupManager */ |
|
54 | - protected $groupManager; |
|
55 | - |
|
56 | - /** |
|
57 | - * Prepared query for selecting tags directly |
|
58 | - * |
|
59 | - * @var \OCP\DB\QueryBuilder\IQueryBuilder |
|
60 | - */ |
|
61 | - private $selectTagQuery; |
|
62 | - |
|
63 | - /** |
|
64 | - * Constructor. |
|
65 | - * |
|
66 | - * @param IDBConnection $connection database connection |
|
67 | - * @param EventDispatcherInterface $dispatcher |
|
68 | - */ |
|
69 | - public function __construct( |
|
70 | - IDBConnection $connection, |
|
71 | - IGroupManager $groupManager, |
|
72 | - EventDispatcherInterface $dispatcher |
|
73 | - ) { |
|
74 | - $this->connection = $connection; |
|
75 | - $this->groupManager = $groupManager; |
|
76 | - $this->dispatcher = $dispatcher; |
|
77 | - |
|
78 | - $query = $this->connection->getQueryBuilder(); |
|
79 | - $this->selectTagQuery = $query->select('*') |
|
80 | - ->from(self::TAG_TABLE) |
|
81 | - ->where($query->expr()->eq('name', $query->createParameter('name'))) |
|
82 | - ->andWhere($query->expr()->eq('visibility', $query->createParameter('visibility'))) |
|
83 | - ->andWhere($query->expr()->eq('editable', $query->createParameter('editable'))); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * {@inheritdoc} |
|
88 | - */ |
|
89 | - public function getTagsByIds($tagIds) { |
|
90 | - if (!is_array($tagIds)) { |
|
91 | - $tagIds = [$tagIds]; |
|
92 | - } |
|
93 | - |
|
94 | - $tags = []; |
|
95 | - |
|
96 | - // note: not all databases will fail if it's a string or starts with a number |
|
97 | - foreach ($tagIds as $tagId) { |
|
98 | - if (!is_numeric($tagId)) { |
|
99 | - throw new \InvalidArgumentException('Tag id must be integer'); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - $query = $this->connection->getQueryBuilder(); |
|
104 | - $query->select('*') |
|
105 | - ->from(self::TAG_TABLE) |
|
106 | - ->where($query->expr()->in('id', $query->createParameter('tagids'))) |
|
107 | - ->addOrderBy('name', 'ASC') |
|
108 | - ->addOrderBy('visibility', 'ASC') |
|
109 | - ->addOrderBy('editable', 'ASC') |
|
110 | - ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY); |
|
111 | - |
|
112 | - $result = $query->execute(); |
|
113 | - while ($row = $result->fetch()) { |
|
114 | - $tags[$row['id']] = $this->createSystemTagFromRow($row); |
|
115 | - } |
|
116 | - |
|
117 | - $result->closeCursor(); |
|
118 | - |
|
119 | - if (count($tags) !== count($tagIds)) { |
|
120 | - throw new TagNotFoundException( |
|
121 | - 'Tag id(s) not found', 0, null, array_diff($tagIds, array_keys($tags)) |
|
122 | - ); |
|
123 | - } |
|
124 | - |
|
125 | - return $tags; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * {@inheritdoc} |
|
130 | - */ |
|
131 | - public function getAllTags($visibilityFilter = null, $nameSearchPattern = null) { |
|
132 | - $tags = []; |
|
133 | - |
|
134 | - $query = $this->connection->getQueryBuilder(); |
|
135 | - $query->select('*') |
|
136 | - ->from(self::TAG_TABLE); |
|
137 | - |
|
138 | - if (!is_null($visibilityFilter)) { |
|
139 | - $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter))); |
|
140 | - } |
|
141 | - |
|
142 | - if (!empty($nameSearchPattern)) { |
|
143 | - $query->andWhere( |
|
144 | - $query->expr()->like( |
|
145 | - 'name', |
|
146 | - $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%') |
|
147 | - ) |
|
148 | - ); |
|
149 | - } |
|
150 | - |
|
151 | - $query |
|
152 | - ->addOrderBy('name', 'ASC') |
|
153 | - ->addOrderBy('visibility', 'ASC') |
|
154 | - ->addOrderBy('editable', 'ASC'); |
|
155 | - |
|
156 | - $result = $query->execute(); |
|
157 | - while ($row = $result->fetch()) { |
|
158 | - $tags[$row['id']] = $this->createSystemTagFromRow($row); |
|
159 | - } |
|
160 | - |
|
161 | - $result->closeCursor(); |
|
162 | - |
|
163 | - return $tags; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * {@inheritdoc} |
|
168 | - */ |
|
169 | - public function getTag($tagName, $userVisible, $userAssignable) { |
|
170 | - $userVisible = (int)$userVisible; |
|
171 | - $userAssignable = (int)$userAssignable; |
|
172 | - |
|
173 | - $result = $this->selectTagQuery |
|
174 | - ->setParameter('name', $tagName) |
|
175 | - ->setParameter('visibility', $userVisible) |
|
176 | - ->setParameter('editable', $userAssignable) |
|
177 | - ->execute(); |
|
178 | - |
|
179 | - $row = $result->fetch(); |
|
180 | - $result->closeCursor(); |
|
181 | - if (!$row) { |
|
182 | - throw new TagNotFoundException( |
|
183 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist' |
|
184 | - ); |
|
185 | - } |
|
186 | - |
|
187 | - return $this->createSystemTagFromRow($row); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * {@inheritdoc} |
|
192 | - */ |
|
193 | - public function createTag($tagName, $userVisible, $userAssignable) { |
|
194 | - $userVisible = (int)$userVisible; |
|
195 | - $userAssignable = (int)$userAssignable; |
|
196 | - |
|
197 | - $query = $this->connection->getQueryBuilder(); |
|
198 | - $query->insert(self::TAG_TABLE) |
|
199 | - ->values([ |
|
200 | - 'name' => $query->createNamedParameter($tagName), |
|
201 | - 'visibility' => $query->createNamedParameter($userVisible), |
|
202 | - 'editable' => $query->createNamedParameter($userAssignable), |
|
203 | - ]); |
|
204 | - |
|
205 | - try { |
|
206 | - $query->execute(); |
|
207 | - } catch (UniqueConstraintViolationException $e) { |
|
208 | - throw new TagAlreadyExistsException( |
|
209 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
210 | - 0, |
|
211 | - $e |
|
212 | - ); |
|
213 | - } |
|
214 | - |
|
215 | - $tagId = $query->getLastInsertId(); |
|
216 | - |
|
217 | - $tag = new SystemTag( |
|
218 | - (int)$tagId, |
|
219 | - $tagName, |
|
220 | - (bool)$userVisible, |
|
221 | - (bool)$userAssignable |
|
222 | - ); |
|
223 | - |
|
224 | - $this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent( |
|
225 | - ManagerEvent::EVENT_CREATE, $tag |
|
226 | - )); |
|
227 | - |
|
228 | - return $tag; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * {@inheritdoc} |
|
233 | - */ |
|
234 | - public function updateTag($tagId, $tagName, $userVisible, $userAssignable) { |
|
235 | - $userVisible = (int)$userVisible; |
|
236 | - $userAssignable = (int)$userAssignable; |
|
237 | - |
|
238 | - try { |
|
239 | - $tags = $this->getTagsByIds($tagId); |
|
240 | - } catch (TagNotFoundException $e) { |
|
241 | - throw new TagNotFoundException( |
|
242 | - 'Tag does not exist', 0, null, [$tagId] |
|
243 | - ); |
|
244 | - } |
|
245 | - |
|
246 | - $beforeUpdate = array_shift($tags); |
|
247 | - $afterUpdate = new SystemTag( |
|
248 | - (int) $tagId, |
|
249 | - $tagName, |
|
250 | - (bool) $userVisible, |
|
251 | - (bool) $userAssignable |
|
252 | - ); |
|
253 | - |
|
254 | - $query = $this->connection->getQueryBuilder(); |
|
255 | - $query->update(self::TAG_TABLE) |
|
256 | - ->set('name', $query->createParameter('name')) |
|
257 | - ->set('visibility', $query->createParameter('visibility')) |
|
258 | - ->set('editable', $query->createParameter('editable')) |
|
259 | - ->where($query->expr()->eq('id', $query->createParameter('tagid'))) |
|
260 | - ->setParameter('name', $tagName) |
|
261 | - ->setParameter('visibility', $userVisible) |
|
262 | - ->setParameter('editable', $userAssignable) |
|
263 | - ->setParameter('tagid', $tagId); |
|
264 | - |
|
265 | - try { |
|
266 | - if ($query->execute() === 0) { |
|
267 | - throw new TagNotFoundException( |
|
268 | - 'Tag does not exist', 0, null, [$tagId] |
|
269 | - ); |
|
270 | - } |
|
271 | - } catch (UniqueConstraintViolationException $e) { |
|
272 | - throw new TagAlreadyExistsException( |
|
273 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
274 | - 0, |
|
275 | - $e |
|
276 | - ); |
|
277 | - } |
|
278 | - |
|
279 | - $this->dispatcher->dispatch(ManagerEvent::EVENT_UPDATE, new ManagerEvent( |
|
280 | - ManagerEvent::EVENT_UPDATE, $afterUpdate, $beforeUpdate |
|
281 | - )); |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * {@inheritdoc} |
|
286 | - */ |
|
287 | - public function deleteTags($tagIds) { |
|
288 | - if (!is_array($tagIds)) { |
|
289 | - $tagIds = [$tagIds]; |
|
290 | - } |
|
291 | - |
|
292 | - $tagNotFoundException = null; |
|
293 | - $tags = []; |
|
294 | - try { |
|
295 | - $tags = $this->getTagsByIds($tagIds); |
|
296 | - } catch (TagNotFoundException $e) { |
|
297 | - $tagNotFoundException = $e; |
|
298 | - |
|
299 | - // Get existing tag objects for the hooks later |
|
300 | - $existingTags = array_diff($tagIds, $tagNotFoundException->getMissingTags()); |
|
301 | - if (!empty($existingTags)) { |
|
302 | - try { |
|
303 | - $tags = $this->getTagsByIds($existingTags); |
|
304 | - } catch (TagNotFoundException $e) { |
|
305 | - // Ignore further errors... |
|
306 | - } |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - // delete relationships first |
|
311 | - $query = $this->connection->getQueryBuilder(); |
|
312 | - $query->delete(SystemTagObjectMapper::RELATION_TABLE) |
|
313 | - ->where($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
314 | - ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
315 | - ->execute(); |
|
316 | - |
|
317 | - $query = $this->connection->getQueryBuilder(); |
|
318 | - $query->delete(self::TAG_TABLE) |
|
319 | - ->where($query->expr()->in('id', $query->createParameter('tagids'))) |
|
320 | - ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
321 | - ->execute(); |
|
322 | - |
|
323 | - foreach ($tags as $tag) { |
|
324 | - $this->dispatcher->dispatch(ManagerEvent::EVENT_DELETE, new ManagerEvent( |
|
325 | - ManagerEvent::EVENT_DELETE, $tag |
|
326 | - )); |
|
327 | - } |
|
328 | - |
|
329 | - if ($tagNotFoundException !== null) { |
|
330 | - throw new TagNotFoundException( |
|
331 | - 'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags() |
|
332 | - ); |
|
333 | - } |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * {@inheritdoc} |
|
338 | - */ |
|
339 | - public function canUserAssignTag(ISystemTag $tag, IUser $user) { |
|
340 | - // early check to avoid unneeded group lookups |
|
341 | - if ($tag->isUserAssignable() && $tag->isUserVisible()) { |
|
342 | - return true; |
|
343 | - } |
|
344 | - |
|
345 | - if ($this->groupManager->isAdmin($user->getUID())) { |
|
346 | - return true; |
|
347 | - } |
|
348 | - |
|
349 | - if (!$tag->isUserVisible()) { |
|
350 | - return false; |
|
351 | - } |
|
352 | - |
|
353 | - $groupIds = $this->groupManager->getUserGroupIds($user); |
|
354 | - if (!empty($groupIds)) { |
|
355 | - $matchingGroups = array_intersect($groupIds, $this->getTagGroups($tag)); |
|
356 | - if (!empty($matchingGroups)) { |
|
357 | - return true; |
|
358 | - } |
|
359 | - } |
|
360 | - |
|
361 | - return false; |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * {@inheritdoc} |
|
366 | - */ |
|
367 | - public function canUserSeeTag(ISystemTag $tag, IUser $user) { |
|
368 | - if ($tag->isUserVisible()) { |
|
369 | - return true; |
|
370 | - } |
|
371 | - |
|
372 | - if ($this->groupManager->isAdmin($user->getUID())) { |
|
373 | - return true; |
|
374 | - } |
|
375 | - |
|
376 | - return false; |
|
377 | - } |
|
378 | - |
|
379 | - private function createSystemTagFromRow($row) { |
|
380 | - return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * {@inheritdoc} |
|
385 | - */ |
|
386 | - public function setTagGroups(ISystemTag $tag, $groupIds) { |
|
387 | - // delete relationships first |
|
388 | - $this->connection->beginTransaction(); |
|
389 | - try { |
|
390 | - $query = $this->connection->getQueryBuilder(); |
|
391 | - $query->delete(self::TAG_GROUP_TABLE) |
|
392 | - ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) |
|
393 | - ->execute(); |
|
394 | - |
|
395 | - // add each group id |
|
396 | - $query = $this->connection->getQueryBuilder(); |
|
397 | - $query->insert(self::TAG_GROUP_TABLE) |
|
398 | - ->values([ |
|
399 | - 'systemtagid' => $query->createNamedParameter($tag->getId()), |
|
400 | - 'gid' => $query->createParameter('gid'), |
|
401 | - ]); |
|
402 | - foreach ($groupIds as $groupId) { |
|
403 | - $query->setParameter('gid', $groupId); |
|
404 | - $query->execute(); |
|
405 | - } |
|
406 | - |
|
407 | - $this->connection->commit(); |
|
408 | - } catch (\Exception $e) { |
|
409 | - $this->connection->rollback(); |
|
410 | - throw $e; |
|
411 | - } |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * {@inheritdoc} |
|
416 | - */ |
|
417 | - public function getTagGroups(ISystemTag $tag) { |
|
418 | - $groupIds = []; |
|
419 | - $query = $this->connection->getQueryBuilder(); |
|
420 | - $query->select('gid') |
|
421 | - ->from(self::TAG_GROUP_TABLE) |
|
422 | - ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) |
|
423 | - ->orderBy('gid'); |
|
424 | - |
|
425 | - $result = $query->execute(); |
|
426 | - while ($row = $result->fetch()) { |
|
427 | - $groupIds[] = $row['gid']; |
|
428 | - } |
|
429 | - |
|
430 | - $result->closeCursor(); |
|
431 | - |
|
432 | - return $groupIds; |
|
433 | - } |
|
44 | + const TAG_TABLE = 'systemtag'; |
|
45 | + const TAG_GROUP_TABLE = 'systemtag_group'; |
|
46 | + |
|
47 | + /** @var IDBConnection */ |
|
48 | + protected $connection; |
|
49 | + |
|
50 | + /** @var EventDispatcherInterface */ |
|
51 | + protected $dispatcher; |
|
52 | + |
|
53 | + /** @var IGroupManager */ |
|
54 | + protected $groupManager; |
|
55 | + |
|
56 | + /** |
|
57 | + * Prepared query for selecting tags directly |
|
58 | + * |
|
59 | + * @var \OCP\DB\QueryBuilder\IQueryBuilder |
|
60 | + */ |
|
61 | + private $selectTagQuery; |
|
62 | + |
|
63 | + /** |
|
64 | + * Constructor. |
|
65 | + * |
|
66 | + * @param IDBConnection $connection database connection |
|
67 | + * @param EventDispatcherInterface $dispatcher |
|
68 | + */ |
|
69 | + public function __construct( |
|
70 | + IDBConnection $connection, |
|
71 | + IGroupManager $groupManager, |
|
72 | + EventDispatcherInterface $dispatcher |
|
73 | + ) { |
|
74 | + $this->connection = $connection; |
|
75 | + $this->groupManager = $groupManager; |
|
76 | + $this->dispatcher = $dispatcher; |
|
77 | + |
|
78 | + $query = $this->connection->getQueryBuilder(); |
|
79 | + $this->selectTagQuery = $query->select('*') |
|
80 | + ->from(self::TAG_TABLE) |
|
81 | + ->where($query->expr()->eq('name', $query->createParameter('name'))) |
|
82 | + ->andWhere($query->expr()->eq('visibility', $query->createParameter('visibility'))) |
|
83 | + ->andWhere($query->expr()->eq('editable', $query->createParameter('editable'))); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * {@inheritdoc} |
|
88 | + */ |
|
89 | + public function getTagsByIds($tagIds) { |
|
90 | + if (!is_array($tagIds)) { |
|
91 | + $tagIds = [$tagIds]; |
|
92 | + } |
|
93 | + |
|
94 | + $tags = []; |
|
95 | + |
|
96 | + // note: not all databases will fail if it's a string or starts with a number |
|
97 | + foreach ($tagIds as $tagId) { |
|
98 | + if (!is_numeric($tagId)) { |
|
99 | + throw new \InvalidArgumentException('Tag id must be integer'); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + $query = $this->connection->getQueryBuilder(); |
|
104 | + $query->select('*') |
|
105 | + ->from(self::TAG_TABLE) |
|
106 | + ->where($query->expr()->in('id', $query->createParameter('tagids'))) |
|
107 | + ->addOrderBy('name', 'ASC') |
|
108 | + ->addOrderBy('visibility', 'ASC') |
|
109 | + ->addOrderBy('editable', 'ASC') |
|
110 | + ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY); |
|
111 | + |
|
112 | + $result = $query->execute(); |
|
113 | + while ($row = $result->fetch()) { |
|
114 | + $tags[$row['id']] = $this->createSystemTagFromRow($row); |
|
115 | + } |
|
116 | + |
|
117 | + $result->closeCursor(); |
|
118 | + |
|
119 | + if (count($tags) !== count($tagIds)) { |
|
120 | + throw new TagNotFoundException( |
|
121 | + 'Tag id(s) not found', 0, null, array_diff($tagIds, array_keys($tags)) |
|
122 | + ); |
|
123 | + } |
|
124 | + |
|
125 | + return $tags; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * {@inheritdoc} |
|
130 | + */ |
|
131 | + public function getAllTags($visibilityFilter = null, $nameSearchPattern = null) { |
|
132 | + $tags = []; |
|
133 | + |
|
134 | + $query = $this->connection->getQueryBuilder(); |
|
135 | + $query->select('*') |
|
136 | + ->from(self::TAG_TABLE); |
|
137 | + |
|
138 | + if (!is_null($visibilityFilter)) { |
|
139 | + $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter))); |
|
140 | + } |
|
141 | + |
|
142 | + if (!empty($nameSearchPattern)) { |
|
143 | + $query->andWhere( |
|
144 | + $query->expr()->like( |
|
145 | + 'name', |
|
146 | + $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%') |
|
147 | + ) |
|
148 | + ); |
|
149 | + } |
|
150 | + |
|
151 | + $query |
|
152 | + ->addOrderBy('name', 'ASC') |
|
153 | + ->addOrderBy('visibility', 'ASC') |
|
154 | + ->addOrderBy('editable', 'ASC'); |
|
155 | + |
|
156 | + $result = $query->execute(); |
|
157 | + while ($row = $result->fetch()) { |
|
158 | + $tags[$row['id']] = $this->createSystemTagFromRow($row); |
|
159 | + } |
|
160 | + |
|
161 | + $result->closeCursor(); |
|
162 | + |
|
163 | + return $tags; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * {@inheritdoc} |
|
168 | + */ |
|
169 | + public function getTag($tagName, $userVisible, $userAssignable) { |
|
170 | + $userVisible = (int)$userVisible; |
|
171 | + $userAssignable = (int)$userAssignable; |
|
172 | + |
|
173 | + $result = $this->selectTagQuery |
|
174 | + ->setParameter('name', $tagName) |
|
175 | + ->setParameter('visibility', $userVisible) |
|
176 | + ->setParameter('editable', $userAssignable) |
|
177 | + ->execute(); |
|
178 | + |
|
179 | + $row = $result->fetch(); |
|
180 | + $result->closeCursor(); |
|
181 | + if (!$row) { |
|
182 | + throw new TagNotFoundException( |
|
183 | + 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist' |
|
184 | + ); |
|
185 | + } |
|
186 | + |
|
187 | + return $this->createSystemTagFromRow($row); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * {@inheritdoc} |
|
192 | + */ |
|
193 | + public function createTag($tagName, $userVisible, $userAssignable) { |
|
194 | + $userVisible = (int)$userVisible; |
|
195 | + $userAssignable = (int)$userAssignable; |
|
196 | + |
|
197 | + $query = $this->connection->getQueryBuilder(); |
|
198 | + $query->insert(self::TAG_TABLE) |
|
199 | + ->values([ |
|
200 | + 'name' => $query->createNamedParameter($tagName), |
|
201 | + 'visibility' => $query->createNamedParameter($userVisible), |
|
202 | + 'editable' => $query->createNamedParameter($userAssignable), |
|
203 | + ]); |
|
204 | + |
|
205 | + try { |
|
206 | + $query->execute(); |
|
207 | + } catch (UniqueConstraintViolationException $e) { |
|
208 | + throw new TagAlreadyExistsException( |
|
209 | + 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
210 | + 0, |
|
211 | + $e |
|
212 | + ); |
|
213 | + } |
|
214 | + |
|
215 | + $tagId = $query->getLastInsertId(); |
|
216 | + |
|
217 | + $tag = new SystemTag( |
|
218 | + (int)$tagId, |
|
219 | + $tagName, |
|
220 | + (bool)$userVisible, |
|
221 | + (bool)$userAssignable |
|
222 | + ); |
|
223 | + |
|
224 | + $this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent( |
|
225 | + ManagerEvent::EVENT_CREATE, $tag |
|
226 | + )); |
|
227 | + |
|
228 | + return $tag; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * {@inheritdoc} |
|
233 | + */ |
|
234 | + public function updateTag($tagId, $tagName, $userVisible, $userAssignable) { |
|
235 | + $userVisible = (int)$userVisible; |
|
236 | + $userAssignable = (int)$userAssignable; |
|
237 | + |
|
238 | + try { |
|
239 | + $tags = $this->getTagsByIds($tagId); |
|
240 | + } catch (TagNotFoundException $e) { |
|
241 | + throw new TagNotFoundException( |
|
242 | + 'Tag does not exist', 0, null, [$tagId] |
|
243 | + ); |
|
244 | + } |
|
245 | + |
|
246 | + $beforeUpdate = array_shift($tags); |
|
247 | + $afterUpdate = new SystemTag( |
|
248 | + (int) $tagId, |
|
249 | + $tagName, |
|
250 | + (bool) $userVisible, |
|
251 | + (bool) $userAssignable |
|
252 | + ); |
|
253 | + |
|
254 | + $query = $this->connection->getQueryBuilder(); |
|
255 | + $query->update(self::TAG_TABLE) |
|
256 | + ->set('name', $query->createParameter('name')) |
|
257 | + ->set('visibility', $query->createParameter('visibility')) |
|
258 | + ->set('editable', $query->createParameter('editable')) |
|
259 | + ->where($query->expr()->eq('id', $query->createParameter('tagid'))) |
|
260 | + ->setParameter('name', $tagName) |
|
261 | + ->setParameter('visibility', $userVisible) |
|
262 | + ->setParameter('editable', $userAssignable) |
|
263 | + ->setParameter('tagid', $tagId); |
|
264 | + |
|
265 | + try { |
|
266 | + if ($query->execute() === 0) { |
|
267 | + throw new TagNotFoundException( |
|
268 | + 'Tag does not exist', 0, null, [$tagId] |
|
269 | + ); |
|
270 | + } |
|
271 | + } catch (UniqueConstraintViolationException $e) { |
|
272 | + throw new TagAlreadyExistsException( |
|
273 | + 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
274 | + 0, |
|
275 | + $e |
|
276 | + ); |
|
277 | + } |
|
278 | + |
|
279 | + $this->dispatcher->dispatch(ManagerEvent::EVENT_UPDATE, new ManagerEvent( |
|
280 | + ManagerEvent::EVENT_UPDATE, $afterUpdate, $beforeUpdate |
|
281 | + )); |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * {@inheritdoc} |
|
286 | + */ |
|
287 | + public function deleteTags($tagIds) { |
|
288 | + if (!is_array($tagIds)) { |
|
289 | + $tagIds = [$tagIds]; |
|
290 | + } |
|
291 | + |
|
292 | + $tagNotFoundException = null; |
|
293 | + $tags = []; |
|
294 | + try { |
|
295 | + $tags = $this->getTagsByIds($tagIds); |
|
296 | + } catch (TagNotFoundException $e) { |
|
297 | + $tagNotFoundException = $e; |
|
298 | + |
|
299 | + // Get existing tag objects for the hooks later |
|
300 | + $existingTags = array_diff($tagIds, $tagNotFoundException->getMissingTags()); |
|
301 | + if (!empty($existingTags)) { |
|
302 | + try { |
|
303 | + $tags = $this->getTagsByIds($existingTags); |
|
304 | + } catch (TagNotFoundException $e) { |
|
305 | + // Ignore further errors... |
|
306 | + } |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + // delete relationships first |
|
311 | + $query = $this->connection->getQueryBuilder(); |
|
312 | + $query->delete(SystemTagObjectMapper::RELATION_TABLE) |
|
313 | + ->where($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
314 | + ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
315 | + ->execute(); |
|
316 | + |
|
317 | + $query = $this->connection->getQueryBuilder(); |
|
318 | + $query->delete(self::TAG_TABLE) |
|
319 | + ->where($query->expr()->in('id', $query->createParameter('tagids'))) |
|
320 | + ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
321 | + ->execute(); |
|
322 | + |
|
323 | + foreach ($tags as $tag) { |
|
324 | + $this->dispatcher->dispatch(ManagerEvent::EVENT_DELETE, new ManagerEvent( |
|
325 | + ManagerEvent::EVENT_DELETE, $tag |
|
326 | + )); |
|
327 | + } |
|
328 | + |
|
329 | + if ($tagNotFoundException !== null) { |
|
330 | + throw new TagNotFoundException( |
|
331 | + 'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags() |
|
332 | + ); |
|
333 | + } |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * {@inheritdoc} |
|
338 | + */ |
|
339 | + public function canUserAssignTag(ISystemTag $tag, IUser $user) { |
|
340 | + // early check to avoid unneeded group lookups |
|
341 | + if ($tag->isUserAssignable() && $tag->isUserVisible()) { |
|
342 | + return true; |
|
343 | + } |
|
344 | + |
|
345 | + if ($this->groupManager->isAdmin($user->getUID())) { |
|
346 | + return true; |
|
347 | + } |
|
348 | + |
|
349 | + if (!$tag->isUserVisible()) { |
|
350 | + return false; |
|
351 | + } |
|
352 | + |
|
353 | + $groupIds = $this->groupManager->getUserGroupIds($user); |
|
354 | + if (!empty($groupIds)) { |
|
355 | + $matchingGroups = array_intersect($groupIds, $this->getTagGroups($tag)); |
|
356 | + if (!empty($matchingGroups)) { |
|
357 | + return true; |
|
358 | + } |
|
359 | + } |
|
360 | + |
|
361 | + return false; |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * {@inheritdoc} |
|
366 | + */ |
|
367 | + public function canUserSeeTag(ISystemTag $tag, IUser $user) { |
|
368 | + if ($tag->isUserVisible()) { |
|
369 | + return true; |
|
370 | + } |
|
371 | + |
|
372 | + if ($this->groupManager->isAdmin($user->getUID())) { |
|
373 | + return true; |
|
374 | + } |
|
375 | + |
|
376 | + return false; |
|
377 | + } |
|
378 | + |
|
379 | + private function createSystemTagFromRow($row) { |
|
380 | + return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * {@inheritdoc} |
|
385 | + */ |
|
386 | + public function setTagGroups(ISystemTag $tag, $groupIds) { |
|
387 | + // delete relationships first |
|
388 | + $this->connection->beginTransaction(); |
|
389 | + try { |
|
390 | + $query = $this->connection->getQueryBuilder(); |
|
391 | + $query->delete(self::TAG_GROUP_TABLE) |
|
392 | + ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) |
|
393 | + ->execute(); |
|
394 | + |
|
395 | + // add each group id |
|
396 | + $query = $this->connection->getQueryBuilder(); |
|
397 | + $query->insert(self::TAG_GROUP_TABLE) |
|
398 | + ->values([ |
|
399 | + 'systemtagid' => $query->createNamedParameter($tag->getId()), |
|
400 | + 'gid' => $query->createParameter('gid'), |
|
401 | + ]); |
|
402 | + foreach ($groupIds as $groupId) { |
|
403 | + $query->setParameter('gid', $groupId); |
|
404 | + $query->execute(); |
|
405 | + } |
|
406 | + |
|
407 | + $this->connection->commit(); |
|
408 | + } catch (\Exception $e) { |
|
409 | + $this->connection->rollback(); |
|
410 | + throw $e; |
|
411 | + } |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * {@inheritdoc} |
|
416 | + */ |
|
417 | + public function getTagGroups(ISystemTag $tag) { |
|
418 | + $groupIds = []; |
|
419 | + $query = $this->connection->getQueryBuilder(); |
|
420 | + $query->select('gid') |
|
421 | + ->from(self::TAG_GROUP_TABLE) |
|
422 | + ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) |
|
423 | + ->orderBy('gid'); |
|
424 | + |
|
425 | + $result = $query->execute(); |
|
426 | + while ($row = $result->fetch()) { |
|
427 | + $groupIds[] = $row['gid']; |
|
428 | + } |
|
429 | + |
|
430 | + $result->closeCursor(); |
|
431 | + |
|
432 | + return $groupIds; |
|
433 | + } |
|
434 | 434 | } |
@@ -136,14 +136,14 @@ discard block |
||
136 | 136 | ->from(self::TAG_TABLE); |
137 | 137 | |
138 | 138 | if (!is_null($visibilityFilter)) { |
139 | - $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter))); |
|
139 | + $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int) $visibilityFilter))); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | if (!empty($nameSearchPattern)) { |
143 | 143 | $query->andWhere( |
144 | 144 | $query->expr()->like( |
145 | 145 | 'name', |
146 | - $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%') |
|
146 | + $query->createNamedParameter('%'.$this->connection->escapeLikeParameter($nameSearchPattern).'%') |
|
147 | 147 | ) |
148 | 148 | ); |
149 | 149 | } |
@@ -167,8 +167,8 @@ discard block |
||
167 | 167 | * {@inheritdoc} |
168 | 168 | */ |
169 | 169 | public function getTag($tagName, $userVisible, $userAssignable) { |
170 | - $userVisible = (int)$userVisible; |
|
171 | - $userAssignable = (int)$userAssignable; |
|
170 | + $userVisible = (int) $userVisible; |
|
171 | + $userAssignable = (int) $userAssignable; |
|
172 | 172 | |
173 | 173 | $result = $this->selectTagQuery |
174 | 174 | ->setParameter('name', $tagName) |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | $result->closeCursor(); |
181 | 181 | if (!$row) { |
182 | 182 | throw new TagNotFoundException( |
183 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist' |
|
183 | + 'Tag ("'.$tagName.'", '.$userVisible.', '.$userAssignable.') does not exist' |
|
184 | 184 | ); |
185 | 185 | } |
186 | 186 | |
@@ -191,8 +191,8 @@ discard block |
||
191 | 191 | * {@inheritdoc} |
192 | 192 | */ |
193 | 193 | public function createTag($tagName, $userVisible, $userAssignable) { |
194 | - $userVisible = (int)$userVisible; |
|
195 | - $userAssignable = (int)$userAssignable; |
|
194 | + $userVisible = (int) $userVisible; |
|
195 | + $userAssignable = (int) $userAssignable; |
|
196 | 196 | |
197 | 197 | $query = $this->connection->getQueryBuilder(); |
198 | 198 | $query->insert(self::TAG_TABLE) |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | $query->execute(); |
207 | 207 | } catch (UniqueConstraintViolationException $e) { |
208 | 208 | throw new TagAlreadyExistsException( |
209 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
209 | + 'Tag ("'.$tagName.'", '.$userVisible.', '.$userAssignable.') already exists', |
|
210 | 210 | 0, |
211 | 211 | $e |
212 | 212 | ); |
@@ -215,10 +215,10 @@ discard block |
||
215 | 215 | $tagId = $query->getLastInsertId(); |
216 | 216 | |
217 | 217 | $tag = new SystemTag( |
218 | - (int)$tagId, |
|
218 | + (int) $tagId, |
|
219 | 219 | $tagName, |
220 | - (bool)$userVisible, |
|
221 | - (bool)$userAssignable |
|
220 | + (bool) $userVisible, |
|
221 | + (bool) $userAssignable |
|
222 | 222 | ); |
223 | 223 | |
224 | 224 | $this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent( |
@@ -232,8 +232,8 @@ discard block |
||
232 | 232 | * {@inheritdoc} |
233 | 233 | */ |
234 | 234 | public function updateTag($tagId, $tagName, $userVisible, $userAssignable) { |
235 | - $userVisible = (int)$userVisible; |
|
236 | - $userAssignable = (int)$userAssignable; |
|
235 | + $userVisible = (int) $userVisible; |
|
236 | + $userAssignable = (int) $userAssignable; |
|
237 | 237 | |
238 | 238 | try { |
239 | 239 | $tags = $this->getTagsByIds($tagId); |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | } |
271 | 271 | } catch (UniqueConstraintViolationException $e) { |
272 | 272 | throw new TagAlreadyExistsException( |
273 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
273 | + 'Tag ("'.$tagName.'", '.$userVisible.', '.$userAssignable.') already exists', |
|
274 | 274 | 0, |
275 | 275 | $e |
276 | 276 | ); |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | } |
378 | 378 | |
379 | 379 | private function createSystemTagFromRow($row) { |
380 | - return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']); |
|
380 | + return new SystemTag((int) $row['id'], $row['name'], (bool) $row['visibility'], (bool) $row['editable']); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | /** |
@@ -742,11 +742,19 @@ discard block |
||
742 | 742 | } |
743 | 743 | |
744 | 744 | // case-insensitive array_search |
745 | + |
|
746 | + /** |
|
747 | + * @param string $needle |
|
748 | + */ |
|
745 | 749 | protected function array_searchi($needle, $haystack, $mem='getName') { |
746 | 750 | if(!is_array($haystack)) { |
747 | 751 | return false; |
748 | 752 | } |
749 | 753 | return array_search(strtolower($needle), array_map( |
754 | + |
|
755 | + /** |
|
756 | + * @param string $tag |
|
757 | + */ |
|
750 | 758 | function($tag) use($mem) { |
751 | 759 | return strtolower(call_user_func(array($tag, $mem))); |
752 | 760 | }, $haystack) |
@@ -771,7 +779,7 @@ discard block |
||
771 | 779 | * Get a tag by its name. |
772 | 780 | * |
773 | 781 | * @param string $name The tag name. |
774 | - * @return integer|bool The tag object's offset within the $this->tags |
|
782 | + * @return \OCP\AppFramework\Db\Entity The tag object's offset within the $this->tags |
|
775 | 783 | * array or false if it doesn't exist. |
776 | 784 | */ |
777 | 785 | private function getTagByName($name) { |
@@ -782,7 +790,7 @@ discard block |
||
782 | 790 | * Get a tag by its ID. |
783 | 791 | * |
784 | 792 | * @param string $id The tag ID to look for. |
785 | - * @return integer|bool The tag object's offset within the $this->tags |
|
793 | + * @return \OCP\AppFramework\Db\Entity The tag object's offset within the $this->tags |
|
786 | 794 | * array or false if it doesn't exist. |
787 | 795 | */ |
788 | 796 | private function getTagById($id) { |
@@ -48,761 +48,761 @@ |
||
48 | 48 | |
49 | 49 | class Tags implements \OCP\ITags { |
50 | 50 | |
51 | - /** |
|
52 | - * Tags |
|
53 | - * |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - private $tags = array(); |
|
57 | - |
|
58 | - /** |
|
59 | - * Used for storing objectid/categoryname pairs while rescanning. |
|
60 | - * |
|
61 | - * @var array |
|
62 | - */ |
|
63 | - private static $relations = array(); |
|
64 | - |
|
65 | - /** |
|
66 | - * Type |
|
67 | - * |
|
68 | - * @var string |
|
69 | - */ |
|
70 | - private $type; |
|
71 | - |
|
72 | - /** |
|
73 | - * User |
|
74 | - * |
|
75 | - * @var string |
|
76 | - */ |
|
77 | - private $user; |
|
78 | - |
|
79 | - /** |
|
80 | - * Are we including tags for shared items? |
|
81 | - * |
|
82 | - * @var bool |
|
83 | - */ |
|
84 | - private $includeShared = false; |
|
85 | - |
|
86 | - /** |
|
87 | - * The current user, plus any owners of the items shared with the current |
|
88 | - * user, if $this->includeShared === true. |
|
89 | - * |
|
90 | - * @var array |
|
91 | - */ |
|
92 | - private $owners = array(); |
|
93 | - |
|
94 | - /** |
|
95 | - * The Mapper we're using to communicate our Tag objects to the database. |
|
96 | - * |
|
97 | - * @var TagMapper |
|
98 | - */ |
|
99 | - private $mapper; |
|
100 | - |
|
101 | - /** |
|
102 | - * The sharing backend for objects of $this->type. Required if |
|
103 | - * $this->includeShared === true to determine ownership of items. |
|
104 | - * |
|
105 | - * @var \OCP\Share_Backend |
|
106 | - */ |
|
107 | - private $backend; |
|
108 | - |
|
109 | - const TAG_TABLE = '*PREFIX*vcategory'; |
|
110 | - const RELATION_TABLE = '*PREFIX*vcategory_to_object'; |
|
111 | - |
|
112 | - const TAG_FAVORITE = '_$!<Favorite>!$_'; |
|
113 | - |
|
114 | - /** |
|
115 | - * Constructor. |
|
116 | - * |
|
117 | - * @param TagMapper $mapper Instance of the TagMapper abstraction layer. |
|
118 | - * @param string $user The user whose data the object will operate on. |
|
119 | - * @param string $type The type of items for which tags will be loaded. |
|
120 | - * @param array $defaultTags Tags that should be created at construction. |
|
121 | - * @param boolean $includeShared Whether to include tags for items shared with this user by others. |
|
122 | - */ |
|
123 | - public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) { |
|
124 | - $this->mapper = $mapper; |
|
125 | - $this->user = $user; |
|
126 | - $this->type = $type; |
|
127 | - $this->includeShared = $includeShared; |
|
128 | - $this->owners = array($this->user); |
|
129 | - if ($this->includeShared) { |
|
130 | - $this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true)); |
|
131 | - $this->backend = \OC\Share\Share::getBackend($this->type); |
|
132 | - } |
|
133 | - $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
134 | - |
|
135 | - if(count($defaultTags) > 0 && count($this->tags) === 0) { |
|
136 | - $this->addMultiple($defaultTags, true); |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Check if any tags are saved for this type and user. |
|
142 | - * |
|
143 | - * @return boolean. |
|
144 | - */ |
|
145 | - public function isEmpty() { |
|
146 | - return count($this->tags) === 0; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Returns an array mapping a given tag's properties to its values: |
|
151 | - * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
152 | - * |
|
153 | - * @param string $id The ID of the tag that is going to be mapped |
|
154 | - * @return array|false |
|
155 | - */ |
|
156 | - public function getTag($id) { |
|
157 | - $key = $this->getTagById($id); |
|
158 | - if ($key !== false) { |
|
159 | - return $this->tagMap($this->tags[$key]); |
|
160 | - } |
|
161 | - return false; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Get the tags for a specific user. |
|
166 | - * |
|
167 | - * This returns an array with maps containing each tag's properties: |
|
168 | - * [ |
|
169 | - * ['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'], |
|
170 | - * ['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'], |
|
171 | - * ] |
|
172 | - * |
|
173 | - * @return array |
|
174 | - */ |
|
175 | - public function getTags() { |
|
176 | - if(!count($this->tags)) { |
|
177 | - return array(); |
|
178 | - } |
|
179 | - |
|
180 | - usort($this->tags, function($a, $b) { |
|
181 | - return strnatcasecmp($a->getName(), $b->getName()); |
|
182 | - }); |
|
183 | - $tagMap = array(); |
|
184 | - |
|
185 | - foreach($this->tags as $tag) { |
|
186 | - if($tag->getName() !== self::TAG_FAVORITE) { |
|
187 | - $tagMap[] = $this->tagMap($tag); |
|
188 | - } |
|
189 | - } |
|
190 | - return $tagMap; |
|
191 | - |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Return only the tags owned by the given user, omitting any tags shared |
|
196 | - * by other users. |
|
197 | - * |
|
198 | - * @param string $user The user whose tags are to be checked. |
|
199 | - * @return array An array of Tag objects. |
|
200 | - */ |
|
201 | - public function getTagsForUser($user) { |
|
202 | - return array_filter($this->tags, |
|
203 | - function($tag) use($user) { |
|
204 | - return $tag->getOwner() === $user; |
|
205 | - } |
|
206 | - ); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get the list of tags for the given ids. |
|
211 | - * |
|
212 | - * @param array $objIds array of object ids |
|
213 | - * @return array|boolean of tags id as key to array of tag names |
|
214 | - * or false if an error occurred |
|
215 | - */ |
|
216 | - public function getTagsForObjects(array $objIds) { |
|
217 | - $entries = array(); |
|
218 | - |
|
219 | - try { |
|
220 | - $conn = \OC::$server->getDatabaseConnection(); |
|
221 | - $chunks = array_chunk($objIds, 900, false); |
|
222 | - foreach ($chunks as $chunk) { |
|
223 | - $result = $conn->executeQuery( |
|
224 | - 'SELECT `category`, `categoryid`, `objid` ' . |
|
225 | - 'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' . |
|
226 | - 'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)', |
|
227 | - array($this->user, $this->type, $chunk), |
|
228 | - array(null, null, IQueryBuilder::PARAM_INT_ARRAY) |
|
229 | - ); |
|
230 | - while ($row = $result->fetch()) { |
|
231 | - $objId = (int)$row['objid']; |
|
232 | - if (!isset($entries[$objId])) { |
|
233 | - $entries[$objId] = array(); |
|
234 | - } |
|
235 | - $entries[$objId][] = $row['category']; |
|
236 | - } |
|
237 | - if (\OCP\DB::isError($result)) { |
|
238 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
239 | - return false; |
|
240 | - } |
|
241 | - } |
|
242 | - } catch(\Exception $e) { |
|
243 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
244 | - \OCP\Util::ERROR); |
|
245 | - return false; |
|
246 | - } |
|
247 | - |
|
248 | - return $entries; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Get the a list if items tagged with $tag. |
|
253 | - * |
|
254 | - * Throws an exception if the tag could not be found. |
|
255 | - * |
|
256 | - * @param string $tag Tag id or name. |
|
257 | - * @return array|false An array of object ids or false on error. |
|
258 | - * @throws \Exception |
|
259 | - */ |
|
260 | - public function getIdsForTag($tag) { |
|
261 | - $result = null; |
|
262 | - $tagId = false; |
|
263 | - if(is_numeric($tag)) { |
|
264 | - $tagId = $tag; |
|
265 | - } elseif(is_string($tag)) { |
|
266 | - $tag = trim($tag); |
|
267 | - if($tag === '') { |
|
268 | - \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG); |
|
269 | - return false; |
|
270 | - } |
|
271 | - $tagId = $this->getTagId($tag); |
|
272 | - } |
|
273 | - |
|
274 | - if($tagId === false) { |
|
275 | - $l10n = \OC::$server->getL10N('core'); |
|
276 | - throw new \Exception( |
|
277 | - $l10n->t('Could not find category "%s"', $tag) |
|
278 | - ); |
|
279 | - } |
|
280 | - |
|
281 | - $ids = array(); |
|
282 | - $sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE |
|
283 | - . '` WHERE `categoryid` = ?'; |
|
284 | - |
|
285 | - try { |
|
286 | - $stmt = \OCP\DB::prepare($sql); |
|
287 | - $result = $stmt->execute(array($tagId)); |
|
288 | - if (\OCP\DB::isError($result)) { |
|
289 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
290 | - return false; |
|
291 | - } |
|
292 | - } catch(\Exception $e) { |
|
293 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
294 | - \OCP\Util::ERROR); |
|
295 | - return false; |
|
296 | - } |
|
297 | - |
|
298 | - if(!is_null($result)) { |
|
299 | - while( $row = $result->fetchRow()) { |
|
300 | - $id = (int)$row['objid']; |
|
301 | - |
|
302 | - if ($this->includeShared) { |
|
303 | - // We have to check if we are really allowed to access the |
|
304 | - // items that are tagged with $tag. To that end, we ask the |
|
305 | - // corresponding sharing backend if the item identified by $id |
|
306 | - // is owned by any of $this->owners. |
|
307 | - foreach ($this->owners as $owner) { |
|
308 | - if ($this->backend->isValidSource($id, $owner)) { |
|
309 | - $ids[] = $id; |
|
310 | - break; |
|
311 | - } |
|
312 | - } |
|
313 | - } else { |
|
314 | - $ids[] = $id; |
|
315 | - } |
|
316 | - } |
|
317 | - } |
|
318 | - |
|
319 | - return $ids; |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Checks whether a tag is saved for the given user, |
|
324 | - * disregarding the ones shared with him or her. |
|
325 | - * |
|
326 | - * @param string $name The tag name to check for. |
|
327 | - * @param string $user The user whose tags are to be checked. |
|
328 | - * @return bool |
|
329 | - */ |
|
330 | - public function userHasTag($name, $user) { |
|
331 | - $key = $this->array_searchi($name, $this->getTagsForUser($user)); |
|
332 | - return ($key !== false) ? $this->tags[$key]->getId() : false; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Checks whether a tag is saved for or shared with the current user. |
|
337 | - * |
|
338 | - * @param string $name The tag name to check for. |
|
339 | - * @return bool |
|
340 | - */ |
|
341 | - public function hasTag($name) { |
|
342 | - return $this->getTagId($name) !== false; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Add a new tag. |
|
347 | - * |
|
348 | - * @param string $name A string with a name of the tag |
|
349 | - * @return false|int the id of the added tag or false on error. |
|
350 | - */ |
|
351 | - public function add($name) { |
|
352 | - $name = trim($name); |
|
353 | - |
|
354 | - if($name === '') { |
|
355 | - \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG); |
|
356 | - return false; |
|
357 | - } |
|
358 | - if($this->userHasTag($name, $this->user)) { |
|
359 | - \OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG); |
|
360 | - return false; |
|
361 | - } |
|
362 | - try { |
|
363 | - $tag = new Tag($this->user, $this->type, $name); |
|
364 | - $tag = $this->mapper->insert($tag); |
|
365 | - $this->tags[] = $tag; |
|
366 | - } catch(\Exception $e) { |
|
367 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
368 | - \OCP\Util::ERROR); |
|
369 | - return false; |
|
370 | - } |
|
371 | - \OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG); |
|
372 | - return $tag->getId(); |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Rename tag. |
|
377 | - * |
|
378 | - * @param string|integer $from The name or ID of the existing tag |
|
379 | - * @param string $to The new name of the tag. |
|
380 | - * @return bool |
|
381 | - */ |
|
382 | - public function rename($from, $to) { |
|
383 | - $from = trim($from); |
|
384 | - $to = trim($to); |
|
385 | - |
|
386 | - if($to === '' || $from === '') { |
|
387 | - \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG); |
|
388 | - return false; |
|
389 | - } |
|
390 | - |
|
391 | - if (is_numeric($from)) { |
|
392 | - $key = $this->getTagById($from); |
|
393 | - } else { |
|
394 | - $key = $this->getTagByName($from); |
|
395 | - } |
|
396 | - if($key === false) { |
|
397 | - \OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG); |
|
398 | - return false; |
|
399 | - } |
|
400 | - $tag = $this->tags[$key]; |
|
401 | - |
|
402 | - if($this->userHasTag($to, $tag->getOwner())) { |
|
403 | - \OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG); |
|
404 | - return false; |
|
405 | - } |
|
406 | - |
|
407 | - try { |
|
408 | - $tag->setName($to); |
|
409 | - $this->tags[$key] = $this->mapper->update($tag); |
|
410 | - } catch(\Exception $e) { |
|
411 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
412 | - \OCP\Util::ERROR); |
|
413 | - return false; |
|
414 | - } |
|
415 | - return true; |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Add a list of new tags. |
|
420 | - * |
|
421 | - * @param string[] $names A string with a name or an array of strings containing |
|
422 | - * the name(s) of the tag(s) to add. |
|
423 | - * @param bool $sync When true, save the tags |
|
424 | - * @param int|null $id int Optional object id to add to this|these tag(s) |
|
425 | - * @return bool Returns false on error. |
|
426 | - */ |
|
427 | - public function addMultiple($names, $sync=false, $id = null) { |
|
428 | - if(!is_array($names)) { |
|
429 | - $names = array($names); |
|
430 | - } |
|
431 | - $names = array_map('trim', $names); |
|
432 | - array_filter($names); |
|
433 | - |
|
434 | - $newones = array(); |
|
435 | - foreach($names as $name) { |
|
436 | - if(!$this->hasTag($name) && $name !== '') { |
|
437 | - $newones[] = new Tag($this->user, $this->type, $name); |
|
438 | - } |
|
439 | - if(!is_null($id) ) { |
|
440 | - // Insert $objectid, $categoryid pairs if not exist. |
|
441 | - self::$relations[] = array('objid' => $id, 'tag' => $name); |
|
442 | - } |
|
443 | - } |
|
444 | - $this->tags = array_merge($this->tags, $newones); |
|
445 | - if($sync === true) { |
|
446 | - $this->save(); |
|
447 | - } |
|
448 | - |
|
449 | - return true; |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Save the list of tags and their object relations |
|
454 | - */ |
|
455 | - protected function save() { |
|
456 | - if(is_array($this->tags)) { |
|
457 | - foreach($this->tags as $tag) { |
|
458 | - try { |
|
459 | - if (!$this->mapper->tagExists($tag)) { |
|
460 | - $this->mapper->insert($tag); |
|
461 | - } |
|
462 | - } catch(\Exception $e) { |
|
463 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
464 | - \OCP\Util::ERROR); |
|
465 | - } |
|
466 | - } |
|
467 | - |
|
468 | - // reload tags to get the proper ids. |
|
469 | - $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
470 | - \OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true), |
|
471 | - \OCP\Util::DEBUG); |
|
472 | - // Loop through temporarily cached objectid/tagname pairs |
|
473 | - // and save relations. |
|
474 | - $tags = $this->tags; |
|
475 | - // For some reason this is needed or array_search(i) will return 0..? |
|
476 | - ksort($tags); |
|
477 | - foreach(self::$relations as $relation) { |
|
478 | - $tagId = $this->getTagId($relation['tag']); |
|
479 | - \OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG); |
|
480 | - if($tagId) { |
|
481 | - try { |
|
482 | - \OCP\DB::insertIfNotExist(self::RELATION_TABLE, |
|
483 | - array( |
|
484 | - 'objid' => $relation['objid'], |
|
485 | - 'categoryid' => $tagId, |
|
486 | - 'type' => $this->type, |
|
487 | - )); |
|
488 | - } catch(\Exception $e) { |
|
489 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
490 | - \OCP\Util::ERROR); |
|
491 | - } |
|
492 | - } |
|
493 | - } |
|
494 | - self::$relations = array(); // reset |
|
495 | - } else { |
|
496 | - \OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! ' |
|
497 | - . print_r($this->tags, true), \OCP\Util::ERROR); |
|
498 | - } |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * Delete tags and tag/object relations for a user. |
|
503 | - * |
|
504 | - * For hooking up on post_deleteUser |
|
505 | - * |
|
506 | - * @param array $arguments |
|
507 | - */ |
|
508 | - public static function post_deleteUser($arguments) { |
|
509 | - // Find all objectid/tagId pairs. |
|
510 | - $result = null; |
|
511 | - try { |
|
512 | - $stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` ' |
|
513 | - . 'WHERE `uid` = ?'); |
|
514 | - $result = $stmt->execute(array($arguments['uid'])); |
|
515 | - if (\OCP\DB::isError($result)) { |
|
516 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
517 | - } |
|
518 | - } catch(\Exception $e) { |
|
519 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
520 | - \OCP\Util::ERROR); |
|
521 | - } |
|
522 | - |
|
523 | - if(!is_null($result)) { |
|
524 | - try { |
|
525 | - $stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
526 | - . 'WHERE `categoryid` = ?'); |
|
527 | - while( $row = $result->fetchRow()) { |
|
528 | - try { |
|
529 | - $stmt->execute(array($row['id'])); |
|
530 | - } catch(\Exception $e) { |
|
531 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
532 | - \OCP\Util::ERROR); |
|
533 | - } |
|
534 | - } |
|
535 | - } catch(\Exception $e) { |
|
536 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
537 | - \OCP\Util::ERROR); |
|
538 | - } |
|
539 | - } |
|
540 | - try { |
|
541 | - $stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` ' |
|
542 | - . 'WHERE `uid` = ?'); |
|
543 | - $result = $stmt->execute(array($arguments['uid'])); |
|
544 | - if (\OCP\DB::isError($result)) { |
|
545 | - \OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
546 | - } |
|
547 | - } catch(\Exception $e) { |
|
548 | - \OCP\Util::writeLog('core', __METHOD__ . ', exception: ' |
|
549 | - . $e->getMessage(), \OCP\Util::ERROR); |
|
550 | - } |
|
551 | - } |
|
552 | - |
|
553 | - /** |
|
554 | - * Delete tag/object relations from the db |
|
555 | - * |
|
556 | - * @param array $ids The ids of the objects |
|
557 | - * @return boolean Returns false on error. |
|
558 | - */ |
|
559 | - public function purgeObjects(array $ids) { |
|
560 | - if(count($ids) === 0) { |
|
561 | - // job done ;) |
|
562 | - return true; |
|
563 | - } |
|
564 | - $updates = $ids; |
|
565 | - try { |
|
566 | - $query = 'DELETE FROM `' . self::RELATION_TABLE . '` '; |
|
567 | - $query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) '; |
|
568 | - $query .= 'AND `type`= ?'; |
|
569 | - $updates[] = $this->type; |
|
570 | - $stmt = \OCP\DB::prepare($query); |
|
571 | - $result = $stmt->execute($updates); |
|
572 | - if (\OCP\DB::isError($result)) { |
|
573 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
574 | - return false; |
|
575 | - } |
|
576 | - } catch(\Exception $e) { |
|
577 | - \OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(), |
|
578 | - \OCP\Util::ERROR); |
|
579 | - return false; |
|
580 | - } |
|
581 | - return true; |
|
582 | - } |
|
583 | - |
|
584 | - /** |
|
585 | - * Get favorites for an object type |
|
586 | - * |
|
587 | - * @return array|false An array of object ids. |
|
588 | - */ |
|
589 | - public function getFavorites() { |
|
590 | - try { |
|
591 | - return $this->getIdsForTag(self::TAG_FAVORITE); |
|
592 | - } catch(\Exception $e) { |
|
593 | - \OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(), |
|
594 | - \OCP\Util::DEBUG); |
|
595 | - return array(); |
|
596 | - } |
|
597 | - } |
|
598 | - |
|
599 | - /** |
|
600 | - * Add an object to favorites |
|
601 | - * |
|
602 | - * @param int $objid The id of the object |
|
603 | - * @return boolean |
|
604 | - */ |
|
605 | - public function addToFavorites($objid) { |
|
606 | - if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) { |
|
607 | - $this->add(self::TAG_FAVORITE); |
|
608 | - } |
|
609 | - return $this->tagAs($objid, self::TAG_FAVORITE); |
|
610 | - } |
|
611 | - |
|
612 | - /** |
|
613 | - * Remove an object from favorites |
|
614 | - * |
|
615 | - * @param int $objid The id of the object |
|
616 | - * @return boolean |
|
617 | - */ |
|
618 | - public function removeFromFavorites($objid) { |
|
619 | - return $this->unTag($objid, self::TAG_FAVORITE); |
|
620 | - } |
|
621 | - |
|
622 | - /** |
|
623 | - * Creates a tag/object relation. |
|
624 | - * |
|
625 | - * @param int $objid The id of the object |
|
626 | - * @param string $tag The id or name of the tag |
|
627 | - * @return boolean Returns false on error. |
|
628 | - */ |
|
629 | - public function tagAs($objid, $tag) { |
|
630 | - if(is_string($tag) && !is_numeric($tag)) { |
|
631 | - $tag = trim($tag); |
|
632 | - if($tag === '') { |
|
633 | - \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG); |
|
634 | - return false; |
|
635 | - } |
|
636 | - if(!$this->hasTag($tag)) { |
|
637 | - $this->add($tag); |
|
638 | - } |
|
639 | - $tagId = $this->getTagId($tag); |
|
640 | - } else { |
|
641 | - $tagId = $tag; |
|
642 | - } |
|
643 | - try { |
|
644 | - \OCP\DB::insertIfNotExist(self::RELATION_TABLE, |
|
645 | - array( |
|
646 | - 'objid' => $objid, |
|
647 | - 'categoryid' => $tagId, |
|
648 | - 'type' => $this->type, |
|
649 | - )); |
|
650 | - } catch(\Exception $e) { |
|
651 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
652 | - \OCP\Util::ERROR); |
|
653 | - return false; |
|
654 | - } |
|
655 | - return true; |
|
656 | - } |
|
657 | - |
|
658 | - /** |
|
659 | - * Delete single tag/object relation from the db |
|
660 | - * |
|
661 | - * @param int $objid The id of the object |
|
662 | - * @param string $tag The id or name of the tag |
|
663 | - * @return boolean |
|
664 | - */ |
|
665 | - public function unTag($objid, $tag) { |
|
666 | - if(is_string($tag) && !is_numeric($tag)) { |
|
667 | - $tag = trim($tag); |
|
668 | - if($tag === '') { |
|
669 | - \OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG); |
|
670 | - return false; |
|
671 | - } |
|
672 | - $tagId = $this->getTagId($tag); |
|
673 | - } else { |
|
674 | - $tagId = $tag; |
|
675 | - } |
|
676 | - |
|
677 | - try { |
|
678 | - $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
679 | - . 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?'; |
|
680 | - $stmt = \OCP\DB::prepare($sql); |
|
681 | - $stmt->execute(array($objid, $tagId, $this->type)); |
|
682 | - } catch(\Exception $e) { |
|
683 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
684 | - \OCP\Util::ERROR); |
|
685 | - return false; |
|
686 | - } |
|
687 | - return true; |
|
688 | - } |
|
689 | - |
|
690 | - /** |
|
691 | - * Delete tags from the database. |
|
692 | - * |
|
693 | - * @param string[]|integer[] $names An array of tags (names or IDs) to delete |
|
694 | - * @return bool Returns false on error |
|
695 | - */ |
|
696 | - public function delete($names) { |
|
697 | - if(!is_array($names)) { |
|
698 | - $names = array($names); |
|
699 | - } |
|
700 | - |
|
701 | - $names = array_map('trim', $names); |
|
702 | - array_filter($names); |
|
703 | - |
|
704 | - \OCP\Util::writeLog('core', __METHOD__ . ', before: ' |
|
705 | - . print_r($this->tags, true), \OCP\Util::DEBUG); |
|
706 | - foreach($names as $name) { |
|
707 | - $id = null; |
|
708 | - |
|
709 | - if (is_numeric($name)) { |
|
710 | - $key = $this->getTagById($name); |
|
711 | - } else { |
|
712 | - $key = $this->getTagByName($name); |
|
713 | - } |
|
714 | - if ($key !== false) { |
|
715 | - $tag = $this->tags[$key]; |
|
716 | - $id = $tag->getId(); |
|
717 | - unset($this->tags[$key]); |
|
718 | - $this->mapper->delete($tag); |
|
719 | - } else { |
|
720 | - \OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name |
|
721 | - . ': not found.', \OCP\Util::ERROR); |
|
722 | - } |
|
723 | - if(!is_null($id) && $id !== false) { |
|
724 | - try { |
|
725 | - $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
726 | - . 'WHERE `categoryid` = ?'; |
|
727 | - $stmt = \OCP\DB::prepare($sql); |
|
728 | - $result = $stmt->execute(array($id)); |
|
729 | - if (\OCP\DB::isError($result)) { |
|
730 | - \OCP\Util::writeLog('core', |
|
731 | - __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), |
|
732 | - \OCP\Util::ERROR); |
|
733 | - return false; |
|
734 | - } |
|
735 | - } catch(\Exception $e) { |
|
736 | - \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
737 | - \OCP\Util::ERROR); |
|
738 | - return false; |
|
739 | - } |
|
740 | - } |
|
741 | - } |
|
742 | - return true; |
|
743 | - } |
|
744 | - |
|
745 | - // case-insensitive array_search |
|
746 | - protected function array_searchi($needle, $haystack, $mem='getName') { |
|
747 | - if(!is_array($haystack)) { |
|
748 | - return false; |
|
749 | - } |
|
750 | - return array_search(strtolower($needle), array_map( |
|
751 | - function($tag) use($mem) { |
|
752 | - return strtolower(call_user_func(array($tag, $mem))); |
|
753 | - }, $haystack) |
|
754 | - ); |
|
755 | - } |
|
756 | - |
|
757 | - /** |
|
758 | - * Get a tag's ID. |
|
759 | - * |
|
760 | - * @param string $name The tag name to look for. |
|
761 | - * @return string|bool The tag's id or false if no matching tag is found. |
|
762 | - */ |
|
763 | - private function getTagId($name) { |
|
764 | - $key = $this->array_searchi($name, $this->tags); |
|
765 | - if ($key !== false) { |
|
766 | - return $this->tags[$key]->getId(); |
|
767 | - } |
|
768 | - return false; |
|
769 | - } |
|
770 | - |
|
771 | - /** |
|
772 | - * Get a tag by its name. |
|
773 | - * |
|
774 | - * @param string $name The tag name. |
|
775 | - * @return integer|bool The tag object's offset within the $this->tags |
|
776 | - * array or false if it doesn't exist. |
|
777 | - */ |
|
778 | - private function getTagByName($name) { |
|
779 | - return $this->array_searchi($name, $this->tags, 'getName'); |
|
780 | - } |
|
781 | - |
|
782 | - /** |
|
783 | - * Get a tag by its ID. |
|
784 | - * |
|
785 | - * @param string $id The tag ID to look for. |
|
786 | - * @return integer|bool The tag object's offset within the $this->tags |
|
787 | - * array or false if it doesn't exist. |
|
788 | - */ |
|
789 | - private function getTagById($id) { |
|
790 | - return $this->array_searchi($id, $this->tags, 'getId'); |
|
791 | - } |
|
792 | - |
|
793 | - /** |
|
794 | - * Returns an array mapping a given tag's properties to its values: |
|
795 | - * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
796 | - * |
|
797 | - * @param Tag $tag The tag that is going to be mapped |
|
798 | - * @return array |
|
799 | - */ |
|
800 | - private function tagMap(Tag $tag) { |
|
801 | - return array( |
|
802 | - 'id' => $tag->getId(), |
|
803 | - 'name' => $tag->getName(), |
|
804 | - 'owner' => $tag->getOwner(), |
|
805 | - 'type' => $tag->getType() |
|
806 | - ); |
|
807 | - } |
|
51 | + /** |
|
52 | + * Tags |
|
53 | + * |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + private $tags = array(); |
|
57 | + |
|
58 | + /** |
|
59 | + * Used for storing objectid/categoryname pairs while rescanning. |
|
60 | + * |
|
61 | + * @var array |
|
62 | + */ |
|
63 | + private static $relations = array(); |
|
64 | + |
|
65 | + /** |
|
66 | + * Type |
|
67 | + * |
|
68 | + * @var string |
|
69 | + */ |
|
70 | + private $type; |
|
71 | + |
|
72 | + /** |
|
73 | + * User |
|
74 | + * |
|
75 | + * @var string |
|
76 | + */ |
|
77 | + private $user; |
|
78 | + |
|
79 | + /** |
|
80 | + * Are we including tags for shared items? |
|
81 | + * |
|
82 | + * @var bool |
|
83 | + */ |
|
84 | + private $includeShared = false; |
|
85 | + |
|
86 | + /** |
|
87 | + * The current user, plus any owners of the items shared with the current |
|
88 | + * user, if $this->includeShared === true. |
|
89 | + * |
|
90 | + * @var array |
|
91 | + */ |
|
92 | + private $owners = array(); |
|
93 | + |
|
94 | + /** |
|
95 | + * The Mapper we're using to communicate our Tag objects to the database. |
|
96 | + * |
|
97 | + * @var TagMapper |
|
98 | + */ |
|
99 | + private $mapper; |
|
100 | + |
|
101 | + /** |
|
102 | + * The sharing backend for objects of $this->type. Required if |
|
103 | + * $this->includeShared === true to determine ownership of items. |
|
104 | + * |
|
105 | + * @var \OCP\Share_Backend |
|
106 | + */ |
|
107 | + private $backend; |
|
108 | + |
|
109 | + const TAG_TABLE = '*PREFIX*vcategory'; |
|
110 | + const RELATION_TABLE = '*PREFIX*vcategory_to_object'; |
|
111 | + |
|
112 | + const TAG_FAVORITE = '_$!<Favorite>!$_'; |
|
113 | + |
|
114 | + /** |
|
115 | + * Constructor. |
|
116 | + * |
|
117 | + * @param TagMapper $mapper Instance of the TagMapper abstraction layer. |
|
118 | + * @param string $user The user whose data the object will operate on. |
|
119 | + * @param string $type The type of items for which tags will be loaded. |
|
120 | + * @param array $defaultTags Tags that should be created at construction. |
|
121 | + * @param boolean $includeShared Whether to include tags for items shared with this user by others. |
|
122 | + */ |
|
123 | + public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) { |
|
124 | + $this->mapper = $mapper; |
|
125 | + $this->user = $user; |
|
126 | + $this->type = $type; |
|
127 | + $this->includeShared = $includeShared; |
|
128 | + $this->owners = array($this->user); |
|
129 | + if ($this->includeShared) { |
|
130 | + $this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true)); |
|
131 | + $this->backend = \OC\Share\Share::getBackend($this->type); |
|
132 | + } |
|
133 | + $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
134 | + |
|
135 | + if(count($defaultTags) > 0 && count($this->tags) === 0) { |
|
136 | + $this->addMultiple($defaultTags, true); |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Check if any tags are saved for this type and user. |
|
142 | + * |
|
143 | + * @return boolean. |
|
144 | + */ |
|
145 | + public function isEmpty() { |
|
146 | + return count($this->tags) === 0; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Returns an array mapping a given tag's properties to its values: |
|
151 | + * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
152 | + * |
|
153 | + * @param string $id The ID of the tag that is going to be mapped |
|
154 | + * @return array|false |
|
155 | + */ |
|
156 | + public function getTag($id) { |
|
157 | + $key = $this->getTagById($id); |
|
158 | + if ($key !== false) { |
|
159 | + return $this->tagMap($this->tags[$key]); |
|
160 | + } |
|
161 | + return false; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Get the tags for a specific user. |
|
166 | + * |
|
167 | + * This returns an array with maps containing each tag's properties: |
|
168 | + * [ |
|
169 | + * ['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'], |
|
170 | + * ['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'], |
|
171 | + * ] |
|
172 | + * |
|
173 | + * @return array |
|
174 | + */ |
|
175 | + public function getTags() { |
|
176 | + if(!count($this->tags)) { |
|
177 | + return array(); |
|
178 | + } |
|
179 | + |
|
180 | + usort($this->tags, function($a, $b) { |
|
181 | + return strnatcasecmp($a->getName(), $b->getName()); |
|
182 | + }); |
|
183 | + $tagMap = array(); |
|
184 | + |
|
185 | + foreach($this->tags as $tag) { |
|
186 | + if($tag->getName() !== self::TAG_FAVORITE) { |
|
187 | + $tagMap[] = $this->tagMap($tag); |
|
188 | + } |
|
189 | + } |
|
190 | + return $tagMap; |
|
191 | + |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Return only the tags owned by the given user, omitting any tags shared |
|
196 | + * by other users. |
|
197 | + * |
|
198 | + * @param string $user The user whose tags are to be checked. |
|
199 | + * @return array An array of Tag objects. |
|
200 | + */ |
|
201 | + public function getTagsForUser($user) { |
|
202 | + return array_filter($this->tags, |
|
203 | + function($tag) use($user) { |
|
204 | + return $tag->getOwner() === $user; |
|
205 | + } |
|
206 | + ); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get the list of tags for the given ids. |
|
211 | + * |
|
212 | + * @param array $objIds array of object ids |
|
213 | + * @return array|boolean of tags id as key to array of tag names |
|
214 | + * or false if an error occurred |
|
215 | + */ |
|
216 | + public function getTagsForObjects(array $objIds) { |
|
217 | + $entries = array(); |
|
218 | + |
|
219 | + try { |
|
220 | + $conn = \OC::$server->getDatabaseConnection(); |
|
221 | + $chunks = array_chunk($objIds, 900, false); |
|
222 | + foreach ($chunks as $chunk) { |
|
223 | + $result = $conn->executeQuery( |
|
224 | + 'SELECT `category`, `categoryid`, `objid` ' . |
|
225 | + 'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' . |
|
226 | + 'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)', |
|
227 | + array($this->user, $this->type, $chunk), |
|
228 | + array(null, null, IQueryBuilder::PARAM_INT_ARRAY) |
|
229 | + ); |
|
230 | + while ($row = $result->fetch()) { |
|
231 | + $objId = (int)$row['objid']; |
|
232 | + if (!isset($entries[$objId])) { |
|
233 | + $entries[$objId] = array(); |
|
234 | + } |
|
235 | + $entries[$objId][] = $row['category']; |
|
236 | + } |
|
237 | + if (\OCP\DB::isError($result)) { |
|
238 | + \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
239 | + return false; |
|
240 | + } |
|
241 | + } |
|
242 | + } catch(\Exception $e) { |
|
243 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
244 | + \OCP\Util::ERROR); |
|
245 | + return false; |
|
246 | + } |
|
247 | + |
|
248 | + return $entries; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Get the a list if items tagged with $tag. |
|
253 | + * |
|
254 | + * Throws an exception if the tag could not be found. |
|
255 | + * |
|
256 | + * @param string $tag Tag id or name. |
|
257 | + * @return array|false An array of object ids or false on error. |
|
258 | + * @throws \Exception |
|
259 | + */ |
|
260 | + public function getIdsForTag($tag) { |
|
261 | + $result = null; |
|
262 | + $tagId = false; |
|
263 | + if(is_numeric($tag)) { |
|
264 | + $tagId = $tag; |
|
265 | + } elseif(is_string($tag)) { |
|
266 | + $tag = trim($tag); |
|
267 | + if($tag === '') { |
|
268 | + \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG); |
|
269 | + return false; |
|
270 | + } |
|
271 | + $tagId = $this->getTagId($tag); |
|
272 | + } |
|
273 | + |
|
274 | + if($tagId === false) { |
|
275 | + $l10n = \OC::$server->getL10N('core'); |
|
276 | + throw new \Exception( |
|
277 | + $l10n->t('Could not find category "%s"', $tag) |
|
278 | + ); |
|
279 | + } |
|
280 | + |
|
281 | + $ids = array(); |
|
282 | + $sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE |
|
283 | + . '` WHERE `categoryid` = ?'; |
|
284 | + |
|
285 | + try { |
|
286 | + $stmt = \OCP\DB::prepare($sql); |
|
287 | + $result = $stmt->execute(array($tagId)); |
|
288 | + if (\OCP\DB::isError($result)) { |
|
289 | + \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
290 | + return false; |
|
291 | + } |
|
292 | + } catch(\Exception $e) { |
|
293 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
294 | + \OCP\Util::ERROR); |
|
295 | + return false; |
|
296 | + } |
|
297 | + |
|
298 | + if(!is_null($result)) { |
|
299 | + while( $row = $result->fetchRow()) { |
|
300 | + $id = (int)$row['objid']; |
|
301 | + |
|
302 | + if ($this->includeShared) { |
|
303 | + // We have to check if we are really allowed to access the |
|
304 | + // items that are tagged with $tag. To that end, we ask the |
|
305 | + // corresponding sharing backend if the item identified by $id |
|
306 | + // is owned by any of $this->owners. |
|
307 | + foreach ($this->owners as $owner) { |
|
308 | + if ($this->backend->isValidSource($id, $owner)) { |
|
309 | + $ids[] = $id; |
|
310 | + break; |
|
311 | + } |
|
312 | + } |
|
313 | + } else { |
|
314 | + $ids[] = $id; |
|
315 | + } |
|
316 | + } |
|
317 | + } |
|
318 | + |
|
319 | + return $ids; |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Checks whether a tag is saved for the given user, |
|
324 | + * disregarding the ones shared with him or her. |
|
325 | + * |
|
326 | + * @param string $name The tag name to check for. |
|
327 | + * @param string $user The user whose tags are to be checked. |
|
328 | + * @return bool |
|
329 | + */ |
|
330 | + public function userHasTag($name, $user) { |
|
331 | + $key = $this->array_searchi($name, $this->getTagsForUser($user)); |
|
332 | + return ($key !== false) ? $this->tags[$key]->getId() : false; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Checks whether a tag is saved for or shared with the current user. |
|
337 | + * |
|
338 | + * @param string $name The tag name to check for. |
|
339 | + * @return bool |
|
340 | + */ |
|
341 | + public function hasTag($name) { |
|
342 | + return $this->getTagId($name) !== false; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Add a new tag. |
|
347 | + * |
|
348 | + * @param string $name A string with a name of the tag |
|
349 | + * @return false|int the id of the added tag or false on error. |
|
350 | + */ |
|
351 | + public function add($name) { |
|
352 | + $name = trim($name); |
|
353 | + |
|
354 | + if($name === '') { |
|
355 | + \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG); |
|
356 | + return false; |
|
357 | + } |
|
358 | + if($this->userHasTag($name, $this->user)) { |
|
359 | + \OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG); |
|
360 | + return false; |
|
361 | + } |
|
362 | + try { |
|
363 | + $tag = new Tag($this->user, $this->type, $name); |
|
364 | + $tag = $this->mapper->insert($tag); |
|
365 | + $this->tags[] = $tag; |
|
366 | + } catch(\Exception $e) { |
|
367 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
368 | + \OCP\Util::ERROR); |
|
369 | + return false; |
|
370 | + } |
|
371 | + \OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG); |
|
372 | + return $tag->getId(); |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Rename tag. |
|
377 | + * |
|
378 | + * @param string|integer $from The name or ID of the existing tag |
|
379 | + * @param string $to The new name of the tag. |
|
380 | + * @return bool |
|
381 | + */ |
|
382 | + public function rename($from, $to) { |
|
383 | + $from = trim($from); |
|
384 | + $to = trim($to); |
|
385 | + |
|
386 | + if($to === '' || $from === '') { |
|
387 | + \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG); |
|
388 | + return false; |
|
389 | + } |
|
390 | + |
|
391 | + if (is_numeric($from)) { |
|
392 | + $key = $this->getTagById($from); |
|
393 | + } else { |
|
394 | + $key = $this->getTagByName($from); |
|
395 | + } |
|
396 | + if($key === false) { |
|
397 | + \OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG); |
|
398 | + return false; |
|
399 | + } |
|
400 | + $tag = $this->tags[$key]; |
|
401 | + |
|
402 | + if($this->userHasTag($to, $tag->getOwner())) { |
|
403 | + \OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG); |
|
404 | + return false; |
|
405 | + } |
|
406 | + |
|
407 | + try { |
|
408 | + $tag->setName($to); |
|
409 | + $this->tags[$key] = $this->mapper->update($tag); |
|
410 | + } catch(\Exception $e) { |
|
411 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
412 | + \OCP\Util::ERROR); |
|
413 | + return false; |
|
414 | + } |
|
415 | + return true; |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Add a list of new tags. |
|
420 | + * |
|
421 | + * @param string[] $names A string with a name or an array of strings containing |
|
422 | + * the name(s) of the tag(s) to add. |
|
423 | + * @param bool $sync When true, save the tags |
|
424 | + * @param int|null $id int Optional object id to add to this|these tag(s) |
|
425 | + * @return bool Returns false on error. |
|
426 | + */ |
|
427 | + public function addMultiple($names, $sync=false, $id = null) { |
|
428 | + if(!is_array($names)) { |
|
429 | + $names = array($names); |
|
430 | + } |
|
431 | + $names = array_map('trim', $names); |
|
432 | + array_filter($names); |
|
433 | + |
|
434 | + $newones = array(); |
|
435 | + foreach($names as $name) { |
|
436 | + if(!$this->hasTag($name) && $name !== '') { |
|
437 | + $newones[] = new Tag($this->user, $this->type, $name); |
|
438 | + } |
|
439 | + if(!is_null($id) ) { |
|
440 | + // Insert $objectid, $categoryid pairs if not exist. |
|
441 | + self::$relations[] = array('objid' => $id, 'tag' => $name); |
|
442 | + } |
|
443 | + } |
|
444 | + $this->tags = array_merge($this->tags, $newones); |
|
445 | + if($sync === true) { |
|
446 | + $this->save(); |
|
447 | + } |
|
448 | + |
|
449 | + return true; |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Save the list of tags and their object relations |
|
454 | + */ |
|
455 | + protected function save() { |
|
456 | + if(is_array($this->tags)) { |
|
457 | + foreach($this->tags as $tag) { |
|
458 | + try { |
|
459 | + if (!$this->mapper->tagExists($tag)) { |
|
460 | + $this->mapper->insert($tag); |
|
461 | + } |
|
462 | + } catch(\Exception $e) { |
|
463 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
464 | + \OCP\Util::ERROR); |
|
465 | + } |
|
466 | + } |
|
467 | + |
|
468 | + // reload tags to get the proper ids. |
|
469 | + $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
470 | + \OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true), |
|
471 | + \OCP\Util::DEBUG); |
|
472 | + // Loop through temporarily cached objectid/tagname pairs |
|
473 | + // and save relations. |
|
474 | + $tags = $this->tags; |
|
475 | + // For some reason this is needed or array_search(i) will return 0..? |
|
476 | + ksort($tags); |
|
477 | + foreach(self::$relations as $relation) { |
|
478 | + $tagId = $this->getTagId($relation['tag']); |
|
479 | + \OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG); |
|
480 | + if($tagId) { |
|
481 | + try { |
|
482 | + \OCP\DB::insertIfNotExist(self::RELATION_TABLE, |
|
483 | + array( |
|
484 | + 'objid' => $relation['objid'], |
|
485 | + 'categoryid' => $tagId, |
|
486 | + 'type' => $this->type, |
|
487 | + )); |
|
488 | + } catch(\Exception $e) { |
|
489 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
490 | + \OCP\Util::ERROR); |
|
491 | + } |
|
492 | + } |
|
493 | + } |
|
494 | + self::$relations = array(); // reset |
|
495 | + } else { |
|
496 | + \OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! ' |
|
497 | + . print_r($this->tags, true), \OCP\Util::ERROR); |
|
498 | + } |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * Delete tags and tag/object relations for a user. |
|
503 | + * |
|
504 | + * For hooking up on post_deleteUser |
|
505 | + * |
|
506 | + * @param array $arguments |
|
507 | + */ |
|
508 | + public static function post_deleteUser($arguments) { |
|
509 | + // Find all objectid/tagId pairs. |
|
510 | + $result = null; |
|
511 | + try { |
|
512 | + $stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` ' |
|
513 | + . 'WHERE `uid` = ?'); |
|
514 | + $result = $stmt->execute(array($arguments['uid'])); |
|
515 | + if (\OCP\DB::isError($result)) { |
|
516 | + \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
517 | + } |
|
518 | + } catch(\Exception $e) { |
|
519 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
520 | + \OCP\Util::ERROR); |
|
521 | + } |
|
522 | + |
|
523 | + if(!is_null($result)) { |
|
524 | + try { |
|
525 | + $stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
526 | + . 'WHERE `categoryid` = ?'); |
|
527 | + while( $row = $result->fetchRow()) { |
|
528 | + try { |
|
529 | + $stmt->execute(array($row['id'])); |
|
530 | + } catch(\Exception $e) { |
|
531 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
532 | + \OCP\Util::ERROR); |
|
533 | + } |
|
534 | + } |
|
535 | + } catch(\Exception $e) { |
|
536 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
537 | + \OCP\Util::ERROR); |
|
538 | + } |
|
539 | + } |
|
540 | + try { |
|
541 | + $stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` ' |
|
542 | + . 'WHERE `uid` = ?'); |
|
543 | + $result = $stmt->execute(array($arguments['uid'])); |
|
544 | + if (\OCP\DB::isError($result)) { |
|
545 | + \OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
546 | + } |
|
547 | + } catch(\Exception $e) { |
|
548 | + \OCP\Util::writeLog('core', __METHOD__ . ', exception: ' |
|
549 | + . $e->getMessage(), \OCP\Util::ERROR); |
|
550 | + } |
|
551 | + } |
|
552 | + |
|
553 | + /** |
|
554 | + * Delete tag/object relations from the db |
|
555 | + * |
|
556 | + * @param array $ids The ids of the objects |
|
557 | + * @return boolean Returns false on error. |
|
558 | + */ |
|
559 | + public function purgeObjects(array $ids) { |
|
560 | + if(count($ids) === 0) { |
|
561 | + // job done ;) |
|
562 | + return true; |
|
563 | + } |
|
564 | + $updates = $ids; |
|
565 | + try { |
|
566 | + $query = 'DELETE FROM `' . self::RELATION_TABLE . '` '; |
|
567 | + $query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) '; |
|
568 | + $query .= 'AND `type`= ?'; |
|
569 | + $updates[] = $this->type; |
|
570 | + $stmt = \OCP\DB::prepare($query); |
|
571 | + $result = $stmt->execute($updates); |
|
572 | + if (\OCP\DB::isError($result)) { |
|
573 | + \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
574 | + return false; |
|
575 | + } |
|
576 | + } catch(\Exception $e) { |
|
577 | + \OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(), |
|
578 | + \OCP\Util::ERROR); |
|
579 | + return false; |
|
580 | + } |
|
581 | + return true; |
|
582 | + } |
|
583 | + |
|
584 | + /** |
|
585 | + * Get favorites for an object type |
|
586 | + * |
|
587 | + * @return array|false An array of object ids. |
|
588 | + */ |
|
589 | + public function getFavorites() { |
|
590 | + try { |
|
591 | + return $this->getIdsForTag(self::TAG_FAVORITE); |
|
592 | + } catch(\Exception $e) { |
|
593 | + \OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(), |
|
594 | + \OCP\Util::DEBUG); |
|
595 | + return array(); |
|
596 | + } |
|
597 | + } |
|
598 | + |
|
599 | + /** |
|
600 | + * Add an object to favorites |
|
601 | + * |
|
602 | + * @param int $objid The id of the object |
|
603 | + * @return boolean |
|
604 | + */ |
|
605 | + public function addToFavorites($objid) { |
|
606 | + if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) { |
|
607 | + $this->add(self::TAG_FAVORITE); |
|
608 | + } |
|
609 | + return $this->tagAs($objid, self::TAG_FAVORITE); |
|
610 | + } |
|
611 | + |
|
612 | + /** |
|
613 | + * Remove an object from favorites |
|
614 | + * |
|
615 | + * @param int $objid The id of the object |
|
616 | + * @return boolean |
|
617 | + */ |
|
618 | + public function removeFromFavorites($objid) { |
|
619 | + return $this->unTag($objid, self::TAG_FAVORITE); |
|
620 | + } |
|
621 | + |
|
622 | + /** |
|
623 | + * Creates a tag/object relation. |
|
624 | + * |
|
625 | + * @param int $objid The id of the object |
|
626 | + * @param string $tag The id or name of the tag |
|
627 | + * @return boolean Returns false on error. |
|
628 | + */ |
|
629 | + public function tagAs($objid, $tag) { |
|
630 | + if(is_string($tag) && !is_numeric($tag)) { |
|
631 | + $tag = trim($tag); |
|
632 | + if($tag === '') { |
|
633 | + \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG); |
|
634 | + return false; |
|
635 | + } |
|
636 | + if(!$this->hasTag($tag)) { |
|
637 | + $this->add($tag); |
|
638 | + } |
|
639 | + $tagId = $this->getTagId($tag); |
|
640 | + } else { |
|
641 | + $tagId = $tag; |
|
642 | + } |
|
643 | + try { |
|
644 | + \OCP\DB::insertIfNotExist(self::RELATION_TABLE, |
|
645 | + array( |
|
646 | + 'objid' => $objid, |
|
647 | + 'categoryid' => $tagId, |
|
648 | + 'type' => $this->type, |
|
649 | + )); |
|
650 | + } catch(\Exception $e) { |
|
651 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
652 | + \OCP\Util::ERROR); |
|
653 | + return false; |
|
654 | + } |
|
655 | + return true; |
|
656 | + } |
|
657 | + |
|
658 | + /** |
|
659 | + * Delete single tag/object relation from the db |
|
660 | + * |
|
661 | + * @param int $objid The id of the object |
|
662 | + * @param string $tag The id or name of the tag |
|
663 | + * @return boolean |
|
664 | + */ |
|
665 | + public function unTag($objid, $tag) { |
|
666 | + if(is_string($tag) && !is_numeric($tag)) { |
|
667 | + $tag = trim($tag); |
|
668 | + if($tag === '') { |
|
669 | + \OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG); |
|
670 | + return false; |
|
671 | + } |
|
672 | + $tagId = $this->getTagId($tag); |
|
673 | + } else { |
|
674 | + $tagId = $tag; |
|
675 | + } |
|
676 | + |
|
677 | + try { |
|
678 | + $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
679 | + . 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?'; |
|
680 | + $stmt = \OCP\DB::prepare($sql); |
|
681 | + $stmt->execute(array($objid, $tagId, $this->type)); |
|
682 | + } catch(\Exception $e) { |
|
683 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
684 | + \OCP\Util::ERROR); |
|
685 | + return false; |
|
686 | + } |
|
687 | + return true; |
|
688 | + } |
|
689 | + |
|
690 | + /** |
|
691 | + * Delete tags from the database. |
|
692 | + * |
|
693 | + * @param string[]|integer[] $names An array of tags (names or IDs) to delete |
|
694 | + * @return bool Returns false on error |
|
695 | + */ |
|
696 | + public function delete($names) { |
|
697 | + if(!is_array($names)) { |
|
698 | + $names = array($names); |
|
699 | + } |
|
700 | + |
|
701 | + $names = array_map('trim', $names); |
|
702 | + array_filter($names); |
|
703 | + |
|
704 | + \OCP\Util::writeLog('core', __METHOD__ . ', before: ' |
|
705 | + . print_r($this->tags, true), \OCP\Util::DEBUG); |
|
706 | + foreach($names as $name) { |
|
707 | + $id = null; |
|
708 | + |
|
709 | + if (is_numeric($name)) { |
|
710 | + $key = $this->getTagById($name); |
|
711 | + } else { |
|
712 | + $key = $this->getTagByName($name); |
|
713 | + } |
|
714 | + if ($key !== false) { |
|
715 | + $tag = $this->tags[$key]; |
|
716 | + $id = $tag->getId(); |
|
717 | + unset($this->tags[$key]); |
|
718 | + $this->mapper->delete($tag); |
|
719 | + } else { |
|
720 | + \OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name |
|
721 | + . ': not found.', \OCP\Util::ERROR); |
|
722 | + } |
|
723 | + if(!is_null($id) && $id !== false) { |
|
724 | + try { |
|
725 | + $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
726 | + . 'WHERE `categoryid` = ?'; |
|
727 | + $stmt = \OCP\DB::prepare($sql); |
|
728 | + $result = $stmt->execute(array($id)); |
|
729 | + if (\OCP\DB::isError($result)) { |
|
730 | + \OCP\Util::writeLog('core', |
|
731 | + __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), |
|
732 | + \OCP\Util::ERROR); |
|
733 | + return false; |
|
734 | + } |
|
735 | + } catch(\Exception $e) { |
|
736 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
737 | + \OCP\Util::ERROR); |
|
738 | + return false; |
|
739 | + } |
|
740 | + } |
|
741 | + } |
|
742 | + return true; |
|
743 | + } |
|
744 | + |
|
745 | + // case-insensitive array_search |
|
746 | + protected function array_searchi($needle, $haystack, $mem='getName') { |
|
747 | + if(!is_array($haystack)) { |
|
748 | + return false; |
|
749 | + } |
|
750 | + return array_search(strtolower($needle), array_map( |
|
751 | + function($tag) use($mem) { |
|
752 | + return strtolower(call_user_func(array($tag, $mem))); |
|
753 | + }, $haystack) |
|
754 | + ); |
|
755 | + } |
|
756 | + |
|
757 | + /** |
|
758 | + * Get a tag's ID. |
|
759 | + * |
|
760 | + * @param string $name The tag name to look for. |
|
761 | + * @return string|bool The tag's id or false if no matching tag is found. |
|
762 | + */ |
|
763 | + private function getTagId($name) { |
|
764 | + $key = $this->array_searchi($name, $this->tags); |
|
765 | + if ($key !== false) { |
|
766 | + return $this->tags[$key]->getId(); |
|
767 | + } |
|
768 | + return false; |
|
769 | + } |
|
770 | + |
|
771 | + /** |
|
772 | + * Get a tag by its name. |
|
773 | + * |
|
774 | + * @param string $name The tag name. |
|
775 | + * @return integer|bool The tag object's offset within the $this->tags |
|
776 | + * array or false if it doesn't exist. |
|
777 | + */ |
|
778 | + private function getTagByName($name) { |
|
779 | + return $this->array_searchi($name, $this->tags, 'getName'); |
|
780 | + } |
|
781 | + |
|
782 | + /** |
|
783 | + * Get a tag by its ID. |
|
784 | + * |
|
785 | + * @param string $id The tag ID to look for. |
|
786 | + * @return integer|bool The tag object's offset within the $this->tags |
|
787 | + * array or false if it doesn't exist. |
|
788 | + */ |
|
789 | + private function getTagById($id) { |
|
790 | + return $this->array_searchi($id, $this->tags, 'getId'); |
|
791 | + } |
|
792 | + |
|
793 | + /** |
|
794 | + * Returns an array mapping a given tag's properties to its values: |
|
795 | + * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
796 | + * |
|
797 | + * @param Tag $tag The tag that is going to be mapped |
|
798 | + * @return array |
|
799 | + */ |
|
800 | + private function tagMap(Tag $tag) { |
|
801 | + return array( |
|
802 | + 'id' => $tag->getId(), |
|
803 | + 'name' => $tag->getName(), |
|
804 | + 'owner' => $tag->getOwner(), |
|
805 | + 'type' => $tag->getType() |
|
806 | + ); |
|
807 | + } |
|
808 | 808 | } |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | } |
133 | 133 | $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
134 | 134 | |
135 | - if(count($defaultTags) > 0 && count($this->tags) === 0) { |
|
135 | + if (count($defaultTags) > 0 && count($this->tags) === 0) { |
|
136 | 136 | $this->addMultiple($defaultTags, true); |
137 | 137 | } |
138 | 138 | } |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | * @return array |
174 | 174 | */ |
175 | 175 | public function getTags() { |
176 | - if(!count($this->tags)) { |
|
176 | + if (!count($this->tags)) { |
|
177 | 177 | return array(); |
178 | 178 | } |
179 | 179 | |
@@ -182,8 +182,8 @@ discard block |
||
182 | 182 | }); |
183 | 183 | $tagMap = array(); |
184 | 184 | |
185 | - foreach($this->tags as $tag) { |
|
186 | - if($tag->getName() !== self::TAG_FAVORITE) { |
|
185 | + foreach ($this->tags as $tag) { |
|
186 | + if ($tag->getName() !== self::TAG_FAVORITE) { |
|
187 | 187 | $tagMap[] = $this->tagMap($tag); |
188 | 188 | } |
189 | 189 | } |
@@ -221,25 +221,25 @@ discard block |
||
221 | 221 | $chunks = array_chunk($objIds, 900, false); |
222 | 222 | foreach ($chunks as $chunk) { |
223 | 223 | $result = $conn->executeQuery( |
224 | - 'SELECT `category`, `categoryid`, `objid` ' . |
|
225 | - 'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' . |
|
224 | + 'SELECT `category`, `categoryid`, `objid` '. |
|
225 | + 'FROM `'.self::RELATION_TABLE.'` r, `'.self::TAG_TABLE.'` '. |
|
226 | 226 | 'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)', |
227 | 227 | array($this->user, $this->type, $chunk), |
228 | 228 | array(null, null, IQueryBuilder::PARAM_INT_ARRAY) |
229 | 229 | ); |
230 | 230 | while ($row = $result->fetch()) { |
231 | - $objId = (int)$row['objid']; |
|
231 | + $objId = (int) $row['objid']; |
|
232 | 232 | if (!isset($entries[$objId])) { |
233 | 233 | $entries[$objId] = array(); |
234 | 234 | } |
235 | 235 | $entries[$objId][] = $row['category']; |
236 | 236 | } |
237 | 237 | if (\OCP\DB::isError($result)) { |
238 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
238 | + \OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
239 | 239 | return false; |
240 | 240 | } |
241 | 241 | } |
242 | - } catch(\Exception $e) { |
|
242 | + } catch (\Exception $e) { |
|
243 | 243 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
244 | 244 | \OCP\Util::ERROR); |
245 | 245 | return false; |
@@ -260,18 +260,18 @@ discard block |
||
260 | 260 | public function getIdsForTag($tag) { |
261 | 261 | $result = null; |
262 | 262 | $tagId = false; |
263 | - if(is_numeric($tag)) { |
|
263 | + if (is_numeric($tag)) { |
|
264 | 264 | $tagId = $tag; |
265 | - } elseif(is_string($tag)) { |
|
265 | + } elseif (is_string($tag)) { |
|
266 | 266 | $tag = trim($tag); |
267 | - if($tag === '') { |
|
267 | + if ($tag === '') { |
|
268 | 268 | \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG); |
269 | 269 | return false; |
270 | 270 | } |
271 | 271 | $tagId = $this->getTagId($tag); |
272 | 272 | } |
273 | 273 | |
274 | - if($tagId === false) { |
|
274 | + if ($tagId === false) { |
|
275 | 275 | $l10n = \OC::$server->getL10N('core'); |
276 | 276 | throw new \Exception( |
277 | 277 | $l10n->t('Could not find category "%s"', $tag) |
@@ -279,25 +279,25 @@ discard block |
||
279 | 279 | } |
280 | 280 | |
281 | 281 | $ids = array(); |
282 | - $sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE |
|
282 | + $sql = 'SELECT `objid` FROM `'.self::RELATION_TABLE |
|
283 | 283 | . '` WHERE `categoryid` = ?'; |
284 | 284 | |
285 | 285 | try { |
286 | 286 | $stmt = \OCP\DB::prepare($sql); |
287 | 287 | $result = $stmt->execute(array($tagId)); |
288 | 288 | if (\OCP\DB::isError($result)) { |
289 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
289 | + \OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
290 | 290 | return false; |
291 | 291 | } |
292 | - } catch(\Exception $e) { |
|
292 | + } catch (\Exception $e) { |
|
293 | 293 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
294 | 294 | \OCP\Util::ERROR); |
295 | 295 | return false; |
296 | 296 | } |
297 | 297 | |
298 | - if(!is_null($result)) { |
|
299 | - while( $row = $result->fetchRow()) { |
|
300 | - $id = (int)$row['objid']; |
|
298 | + if (!is_null($result)) { |
|
299 | + while ($row = $result->fetchRow()) { |
|
300 | + $id = (int) $row['objid']; |
|
301 | 301 | |
302 | 302 | if ($this->includeShared) { |
303 | 303 | // We have to check if we are really allowed to access the |
@@ -351,24 +351,24 @@ discard block |
||
351 | 351 | public function add($name) { |
352 | 352 | $name = trim($name); |
353 | 353 | |
354 | - if($name === '') { |
|
354 | + if ($name === '') { |
|
355 | 355 | \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG); |
356 | 356 | return false; |
357 | 357 | } |
358 | - if($this->userHasTag($name, $this->user)) { |
|
359 | - \OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG); |
|
358 | + if ($this->userHasTag($name, $this->user)) { |
|
359 | + \OCP\Util::writeLog('core', __METHOD__.', name: '.$name.' exists already', \OCP\Util::DEBUG); |
|
360 | 360 | return false; |
361 | 361 | } |
362 | 362 | try { |
363 | 363 | $tag = new Tag($this->user, $this->type, $name); |
364 | 364 | $tag = $this->mapper->insert($tag); |
365 | 365 | $this->tags[] = $tag; |
366 | - } catch(\Exception $e) { |
|
366 | + } catch (\Exception $e) { |
|
367 | 367 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
368 | 368 | \OCP\Util::ERROR); |
369 | 369 | return false; |
370 | 370 | } |
371 | - \OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG); |
|
371 | + \OCP\Util::writeLog('core', __METHOD__.', id: '.$tag->getId(), \OCP\Util::DEBUG); |
|
372 | 372 | return $tag->getId(); |
373 | 373 | } |
374 | 374 | |
@@ -383,7 +383,7 @@ discard block |
||
383 | 383 | $from = trim($from); |
384 | 384 | $to = trim($to); |
385 | 385 | |
386 | - if($to === '' || $from === '') { |
|
386 | + if ($to === '' || $from === '') { |
|
387 | 387 | \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG); |
388 | 388 | return false; |
389 | 389 | } |
@@ -393,21 +393,21 @@ discard block |
||
393 | 393 | } else { |
394 | 394 | $key = $this->getTagByName($from); |
395 | 395 | } |
396 | - if($key === false) { |
|
397 | - \OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG); |
|
396 | + if ($key === false) { |
|
397 | + \OCP\Util::writeLog('core', __METHOD__.', tag: '.$from.' does not exist', \OCP\Util::DEBUG); |
|
398 | 398 | return false; |
399 | 399 | } |
400 | 400 | $tag = $this->tags[$key]; |
401 | 401 | |
402 | - if($this->userHasTag($to, $tag->getOwner())) { |
|
403 | - \OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG); |
|
402 | + if ($this->userHasTag($to, $tag->getOwner())) { |
|
403 | + \OCP\Util::writeLog('core', __METHOD__.', A tag named '.$to.' already exists for user '.$tag->getOwner().'.', \OCP\Util::DEBUG); |
|
404 | 404 | return false; |
405 | 405 | } |
406 | 406 | |
407 | 407 | try { |
408 | 408 | $tag->setName($to); |
409 | 409 | $this->tags[$key] = $this->mapper->update($tag); |
410 | - } catch(\Exception $e) { |
|
410 | + } catch (\Exception $e) { |
|
411 | 411 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
412 | 412 | \OCP\Util::ERROR); |
413 | 413 | return false; |
@@ -424,25 +424,25 @@ discard block |
||
424 | 424 | * @param int|null $id int Optional object id to add to this|these tag(s) |
425 | 425 | * @return bool Returns false on error. |
426 | 426 | */ |
427 | - public function addMultiple($names, $sync=false, $id = null) { |
|
428 | - if(!is_array($names)) { |
|
427 | + public function addMultiple($names, $sync = false, $id = null) { |
|
428 | + if (!is_array($names)) { |
|
429 | 429 | $names = array($names); |
430 | 430 | } |
431 | 431 | $names = array_map('trim', $names); |
432 | 432 | array_filter($names); |
433 | 433 | |
434 | 434 | $newones = array(); |
435 | - foreach($names as $name) { |
|
436 | - if(!$this->hasTag($name) && $name !== '') { |
|
435 | + foreach ($names as $name) { |
|
436 | + if (!$this->hasTag($name) && $name !== '') { |
|
437 | 437 | $newones[] = new Tag($this->user, $this->type, $name); |
438 | 438 | } |
439 | - if(!is_null($id) ) { |
|
439 | + if (!is_null($id)) { |
|
440 | 440 | // Insert $objectid, $categoryid pairs if not exist. |
441 | 441 | self::$relations[] = array('objid' => $id, 'tag' => $name); |
442 | 442 | } |
443 | 443 | } |
444 | 444 | $this->tags = array_merge($this->tags, $newones); |
445 | - if($sync === true) { |
|
445 | + if ($sync === true) { |
|
446 | 446 | $this->save(); |
447 | 447 | } |
448 | 448 | |
@@ -453,13 +453,13 @@ discard block |
||
453 | 453 | * Save the list of tags and their object relations |
454 | 454 | */ |
455 | 455 | protected function save() { |
456 | - if(is_array($this->tags)) { |
|
457 | - foreach($this->tags as $tag) { |
|
456 | + if (is_array($this->tags)) { |
|
457 | + foreach ($this->tags as $tag) { |
|
458 | 458 | try { |
459 | 459 | if (!$this->mapper->tagExists($tag)) { |
460 | 460 | $this->mapper->insert($tag); |
461 | 461 | } |
462 | - } catch(\Exception $e) { |
|
462 | + } catch (\Exception $e) { |
|
463 | 463 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
464 | 464 | \OCP\Util::ERROR); |
465 | 465 | } |
@@ -467,17 +467,17 @@ discard block |
||
467 | 467 | |
468 | 468 | // reload tags to get the proper ids. |
469 | 469 | $this->tags = $this->mapper->loadTags($this->owners, $this->type); |
470 | - \OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true), |
|
470 | + \OCP\Util::writeLog('core', __METHOD__.', tags: '.print_r($this->tags, true), |
|
471 | 471 | \OCP\Util::DEBUG); |
472 | 472 | // Loop through temporarily cached objectid/tagname pairs |
473 | 473 | // and save relations. |
474 | 474 | $tags = $this->tags; |
475 | 475 | // For some reason this is needed or array_search(i) will return 0..? |
476 | 476 | ksort($tags); |
477 | - foreach(self::$relations as $relation) { |
|
477 | + foreach (self::$relations as $relation) { |
|
478 | 478 | $tagId = $this->getTagId($relation['tag']); |
479 | - \OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG); |
|
480 | - if($tagId) { |
|
479 | + \OCP\Util::writeLog('core', __METHOD__.'catid, '.$relation['tag'].' '.$tagId, \OCP\Util::DEBUG); |
|
480 | + if ($tagId) { |
|
481 | 481 | try { |
482 | 482 | \OCP\DB::insertIfNotExist(self::RELATION_TABLE, |
483 | 483 | array( |
@@ -485,7 +485,7 @@ discard block |
||
485 | 485 | 'categoryid' => $tagId, |
486 | 486 | 'type' => $this->type, |
487 | 487 | )); |
488 | - } catch(\Exception $e) { |
|
488 | + } catch (\Exception $e) { |
|
489 | 489 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
490 | 490 | \OCP\Util::ERROR); |
491 | 491 | } |
@@ -509,43 +509,43 @@ discard block |
||
509 | 509 | // Find all objectid/tagId pairs. |
510 | 510 | $result = null; |
511 | 511 | try { |
512 | - $stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` ' |
|
512 | + $stmt = \OCP\DB::prepare('SELECT `id` FROM `'.self::TAG_TABLE.'` ' |
|
513 | 513 | . 'WHERE `uid` = ?'); |
514 | 514 | $result = $stmt->execute(array($arguments['uid'])); |
515 | 515 | if (\OCP\DB::isError($result)) { |
516 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
516 | + \OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
517 | 517 | } |
518 | - } catch(\Exception $e) { |
|
518 | + } catch (\Exception $e) { |
|
519 | 519 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
520 | 520 | \OCP\Util::ERROR); |
521 | 521 | } |
522 | 522 | |
523 | - if(!is_null($result)) { |
|
523 | + if (!is_null($result)) { |
|
524 | 524 | try { |
525 | - $stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
525 | + $stmt = \OCP\DB::prepare('DELETE FROM `'.self::RELATION_TABLE.'` ' |
|
526 | 526 | . 'WHERE `categoryid` = ?'); |
527 | - while( $row = $result->fetchRow()) { |
|
527 | + while ($row = $result->fetchRow()) { |
|
528 | 528 | try { |
529 | 529 | $stmt->execute(array($row['id'])); |
530 | - } catch(\Exception $e) { |
|
530 | + } catch (\Exception $e) { |
|
531 | 531 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
532 | 532 | \OCP\Util::ERROR); |
533 | 533 | } |
534 | 534 | } |
535 | - } catch(\Exception $e) { |
|
535 | + } catch (\Exception $e) { |
|
536 | 536 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
537 | 537 | \OCP\Util::ERROR); |
538 | 538 | } |
539 | 539 | } |
540 | 540 | try { |
541 | - $stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` ' |
|
541 | + $stmt = \OCP\DB::prepare('DELETE FROM `'.self::TAG_TABLE.'` ' |
|
542 | 542 | . 'WHERE `uid` = ?'); |
543 | 543 | $result = $stmt->execute(array($arguments['uid'])); |
544 | 544 | if (\OCP\DB::isError($result)) { |
545 | - \OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
545 | + \OCP\Util::writeLog('core', __METHOD__.', DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
546 | 546 | } |
547 | - } catch(\Exception $e) { |
|
548 | - \OCP\Util::writeLog('core', __METHOD__ . ', exception: ' |
|
547 | + } catch (\Exception $e) { |
|
548 | + \OCP\Util::writeLog('core', __METHOD__.', exception: ' |
|
549 | 549 | . $e->getMessage(), \OCP\Util::ERROR); |
550 | 550 | } |
551 | 551 | } |
@@ -557,24 +557,24 @@ discard block |
||
557 | 557 | * @return boolean Returns false on error. |
558 | 558 | */ |
559 | 559 | public function purgeObjects(array $ids) { |
560 | - if(count($ids) === 0) { |
|
560 | + if (count($ids) === 0) { |
|
561 | 561 | // job done ;) |
562 | 562 | return true; |
563 | 563 | } |
564 | 564 | $updates = $ids; |
565 | 565 | try { |
566 | - $query = 'DELETE FROM `' . self::RELATION_TABLE . '` '; |
|
567 | - $query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) '; |
|
566 | + $query = 'DELETE FROM `'.self::RELATION_TABLE.'` '; |
|
567 | + $query .= 'WHERE `objid` IN ('.str_repeat('?,', count($ids) - 1).'?) '; |
|
568 | 568 | $query .= 'AND `type`= ?'; |
569 | 569 | $updates[] = $this->type; |
570 | 570 | $stmt = \OCP\DB::prepare($query); |
571 | 571 | $result = $stmt->execute($updates); |
572 | 572 | if (\OCP\DB::isError($result)) { |
573 | - \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
573 | + \OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR); |
|
574 | 574 | return false; |
575 | 575 | } |
576 | - } catch(\Exception $e) { |
|
577 | - \OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(), |
|
576 | + } catch (\Exception $e) { |
|
577 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
578 | 578 | \OCP\Util::ERROR); |
579 | 579 | return false; |
580 | 580 | } |
@@ -589,8 +589,8 @@ discard block |
||
589 | 589 | public function getFavorites() { |
590 | 590 | try { |
591 | 591 | return $this->getIdsForTag(self::TAG_FAVORITE); |
592 | - } catch(\Exception $e) { |
|
593 | - \OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(), |
|
592 | + } catch (\Exception $e) { |
|
593 | + \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
594 | 594 | \OCP\Util::DEBUG); |
595 | 595 | return array(); |
596 | 596 | } |
@@ -603,7 +603,7 @@ discard block |
||
603 | 603 | * @return boolean |
604 | 604 | */ |
605 | 605 | public function addToFavorites($objid) { |
606 | - if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) { |
|
606 | + if (!$this->userHasTag(self::TAG_FAVORITE, $this->user)) { |
|
607 | 607 | $this->add(self::TAG_FAVORITE); |
608 | 608 | } |
609 | 609 | return $this->tagAs($objid, self::TAG_FAVORITE); |
@@ -627,16 +627,16 @@ discard block |
||
627 | 627 | * @return boolean Returns false on error. |
628 | 628 | */ |
629 | 629 | public function tagAs($objid, $tag) { |
630 | - if(is_string($tag) && !is_numeric($tag)) { |
|
630 | + if (is_string($tag) && !is_numeric($tag)) { |
|
631 | 631 | $tag = trim($tag); |
632 | - if($tag === '') { |
|
632 | + if ($tag === '') { |
|
633 | 633 | \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG); |
634 | 634 | return false; |
635 | 635 | } |
636 | - if(!$this->hasTag($tag)) { |
|
636 | + if (!$this->hasTag($tag)) { |
|
637 | 637 | $this->add($tag); |
638 | 638 | } |
639 | - $tagId = $this->getTagId($tag); |
|
639 | + $tagId = $this->getTagId($tag); |
|
640 | 640 | } else { |
641 | 641 | $tagId = $tag; |
642 | 642 | } |
@@ -647,7 +647,7 @@ discard block |
||
647 | 647 | 'categoryid' => $tagId, |
648 | 648 | 'type' => $this->type, |
649 | 649 | )); |
650 | - } catch(\Exception $e) { |
|
650 | + } catch (\Exception $e) { |
|
651 | 651 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
652 | 652 | \OCP\Util::ERROR); |
653 | 653 | return false; |
@@ -663,23 +663,23 @@ discard block |
||
663 | 663 | * @return boolean |
664 | 664 | */ |
665 | 665 | public function unTag($objid, $tag) { |
666 | - if(is_string($tag) && !is_numeric($tag)) { |
|
666 | + if (is_string($tag) && !is_numeric($tag)) { |
|
667 | 667 | $tag = trim($tag); |
668 | - if($tag === '') { |
|
668 | + if ($tag === '') { |
|
669 | 669 | \OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG); |
670 | 670 | return false; |
671 | 671 | } |
672 | - $tagId = $this->getTagId($tag); |
|
672 | + $tagId = $this->getTagId($tag); |
|
673 | 673 | } else { |
674 | 674 | $tagId = $tag; |
675 | 675 | } |
676 | 676 | |
677 | 677 | try { |
678 | - $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
678 | + $sql = 'DELETE FROM `'.self::RELATION_TABLE.'` ' |
|
679 | 679 | . 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?'; |
680 | 680 | $stmt = \OCP\DB::prepare($sql); |
681 | 681 | $stmt->execute(array($objid, $tagId, $this->type)); |
682 | - } catch(\Exception $e) { |
|
682 | + } catch (\Exception $e) { |
|
683 | 683 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
684 | 684 | \OCP\Util::ERROR); |
685 | 685 | return false; |
@@ -694,16 +694,16 @@ discard block |
||
694 | 694 | * @return bool Returns false on error |
695 | 695 | */ |
696 | 696 | public function delete($names) { |
697 | - if(!is_array($names)) { |
|
697 | + if (!is_array($names)) { |
|
698 | 698 | $names = array($names); |
699 | 699 | } |
700 | 700 | |
701 | 701 | $names = array_map('trim', $names); |
702 | 702 | array_filter($names); |
703 | 703 | |
704 | - \OCP\Util::writeLog('core', __METHOD__ . ', before: ' |
|
704 | + \OCP\Util::writeLog('core', __METHOD__.', before: ' |
|
705 | 705 | . print_r($this->tags, true), \OCP\Util::DEBUG); |
706 | - foreach($names as $name) { |
|
706 | + foreach ($names as $name) { |
|
707 | 707 | $id = null; |
708 | 708 | |
709 | 709 | if (is_numeric($name)) { |
@@ -717,22 +717,22 @@ discard block |
||
717 | 717 | unset($this->tags[$key]); |
718 | 718 | $this->mapper->delete($tag); |
719 | 719 | } else { |
720 | - \OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name |
|
720 | + \OCP\Util::writeLog('core', __METHOD__.'Cannot delete tag '.$name |
|
721 | 721 | . ': not found.', \OCP\Util::ERROR); |
722 | 722 | } |
723 | - if(!is_null($id) && $id !== false) { |
|
723 | + if (!is_null($id) && $id !== false) { |
|
724 | 724 | try { |
725 | - $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` ' |
|
725 | + $sql = 'DELETE FROM `'.self::RELATION_TABLE.'` ' |
|
726 | 726 | . 'WHERE `categoryid` = ?'; |
727 | 727 | $stmt = \OCP\DB::prepare($sql); |
728 | 728 | $result = $stmt->execute(array($id)); |
729 | 729 | if (\OCP\DB::isError($result)) { |
730 | 730 | \OCP\Util::writeLog('core', |
731 | - __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), |
|
731 | + __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), |
|
732 | 732 | \OCP\Util::ERROR); |
733 | 733 | return false; |
734 | 734 | } |
735 | - } catch(\Exception $e) { |
|
735 | + } catch (\Exception $e) { |
|
736 | 736 | \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
737 | 737 | \OCP\Util::ERROR); |
738 | 738 | return false; |
@@ -743,8 +743,8 @@ discard block |
||
743 | 743 | } |
744 | 744 | |
745 | 745 | // case-insensitive array_search |
746 | - protected function array_searchi($needle, $haystack, $mem='getName') { |
|
747 | - if(!is_array($haystack)) { |
|
746 | + protected function array_searchi($needle, $haystack, $mem = 'getName') { |
|
747 | + if (!is_array($haystack)) { |
|
748 | 748 | return false; |
749 | 749 | } |
750 | 750 | return array_search(strtolower($needle), array_map( |