Completed
Push — master ( 98cdd9...2f3484 )
by Morris
46s
created
lib/private/legacy/util.php 1 patch
Indentation   +1430 added lines, -1430 removed lines patch added patch discarded remove patch
@@ -65,1438 +65,1438 @@
 block discarded – undo
65 65
 use OCP\IUser;
66 66
 
67 67
 class OC_Util {
68
-	public static $scripts = array();
69
-	public static $styles = array();
70
-	public static $headers = array();
71
-	private static $rootMounted = false;
72
-	private static $fsSetup = false;
73
-
74
-	/** @var array Local cache of version.php */
75
-	private static $versionCache = null;
76
-
77
-	protected static function getAppManager() {
78
-		return \OC::$server->getAppManager();
79
-	}
80
-
81
-	private static function initLocalStorageRootFS() {
82
-		// mount local file backend as root
83
-		$configDataDirectory = \OC::$server->getSystemConfig()->getValue("datadirectory", OC::$SERVERROOT . "/data");
84
-		//first set up the local "root" storage
85
-		\OC\Files\Filesystem::initMountManager();
86
-		if (!self::$rootMounted) {
87
-			\OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir' => $configDataDirectory), '/');
88
-			self::$rootMounted = true;
89
-		}
90
-	}
91
-
92
-	/**
93
-	 * mounting an object storage as the root fs will in essence remove the
94
-	 * necessity of a data folder being present.
95
-	 * TODO make home storage aware of this and use the object storage instead of local disk access
96
-	 *
97
-	 * @param array $config containing 'class' and optional 'arguments'
98
-	 * @suppress PhanDeprecatedFunction
99
-	 */
100
-	private static function initObjectStoreRootFS($config) {
101
-		// check misconfiguration
102
-		if (empty($config['class'])) {
103
-			\OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
104
-		}
105
-		if (!isset($config['arguments'])) {
106
-			$config['arguments'] = array();
107
-		}
108
-
109
-		// instantiate object store implementation
110
-		$name = $config['class'];
111
-		if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
112
-			$segments = explode('\\', $name);
113
-			OC_App::loadApp(strtolower($segments[1]));
114
-		}
115
-		$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
116
-		// mount with plain / root object store implementation
117
-		$config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage';
118
-
119
-		// mount object storage as root
120
-		\OC\Files\Filesystem::initMountManager();
121
-		if (!self::$rootMounted) {
122
-			\OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/');
123
-			self::$rootMounted = true;
124
-		}
125
-	}
126
-
127
-	/**
128
-	 * mounting an object storage as the root fs will in essence remove the
129
-	 * necessity of a data folder being present.
130
-	 *
131
-	 * @param array $config containing 'class' and optional 'arguments'
132
-	 * @suppress PhanDeprecatedFunction
133
-	 */
134
-	private static function initObjectStoreMultibucketRootFS($config) {
135
-		// check misconfiguration
136
-		if (empty($config['class'])) {
137
-			\OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
138
-		}
139
-		if (!isset($config['arguments'])) {
140
-			$config['arguments'] = array();
141
-		}
142
-
143
-		// instantiate object store implementation
144
-		$name = $config['class'];
145
-		if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
146
-			$segments = explode('\\', $name);
147
-			OC_App::loadApp(strtolower($segments[1]));
148
-		}
149
-
150
-		if (!isset($config['arguments']['bucket'])) {
151
-			$config['arguments']['bucket'] = '';
152
-		}
153
-		// put the root FS always in first bucket for multibucket configuration
154
-		$config['arguments']['bucket'] .= '0';
155
-
156
-		$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
157
-		// mount with plain / root object store implementation
158
-		$config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage';
159
-
160
-		// mount object storage as root
161
-		\OC\Files\Filesystem::initMountManager();
162
-		if (!self::$rootMounted) {
163
-			\OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/');
164
-			self::$rootMounted = true;
165
-		}
166
-	}
167
-
168
-	/**
169
-	 * Can be set up
170
-	 *
171
-	 * @param string $user
172
-	 * @return boolean
173
-	 * @description configure the initial filesystem based on the configuration
174
-	 * @suppress PhanDeprecatedFunction
175
-	 * @suppress PhanAccessMethodInternal
176
-	 */
177
-	public static function setupFS($user = '') {
178
-		//setting up the filesystem twice can only lead to trouble
179
-		if (self::$fsSetup) {
180
-			return false;
181
-		}
182
-
183
-		\OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem');
184
-
185
-		// If we are not forced to load a specific user we load the one that is logged in
186
-		if ($user === null) {
187
-			$user = '';
188
-		} else if ($user == "" && \OC::$server->getUserSession()->isLoggedIn()) {
189
-			$user = OC_User::getUser();
190
-		}
191
-
192
-		// load all filesystem apps before, so no setup-hook gets lost
193
-		OC_App::loadApps(array('filesystem'));
194
-
195
-		// the filesystem will finish when $user is not empty,
196
-		// mark fs setup here to avoid doing the setup from loading
197
-		// OC_Filesystem
198
-		if ($user != '') {
199
-			self::$fsSetup = true;
200
-		}
201
-
202
-		\OC\Files\Filesystem::initMountManager();
203
-
204
-		\OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
205
-		\OC\Files\Filesystem::addStorageWrapper('mount_options', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) {
206
-			if ($storage->instanceOfStorage('\OC\Files\Storage\Common')) {
207
-				/** @var \OC\Files\Storage\Common $storage */
208
-				$storage->setMountOptions($mount->getOptions());
209
-			}
210
-			return $storage;
211
-		});
212
-
213
-		\OC\Files\Filesystem::addStorageWrapper('enable_sharing', function ($mountPoint, \OCP\Files\Storage\IStorage $storage, \OCP\Files\Mount\IMountPoint $mount) {
214
-			if (!$mount->getOption('enable_sharing', true)) {
215
-				return new \OC\Files\Storage\Wrapper\PermissionsMask([
216
-					'storage' => $storage,
217
-					'mask' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_SHARE
218
-				]);
219
-			}
220
-			return $storage;
221
-		});
222
-
223
-		// install storage availability wrapper, before most other wrappers
224
-		\OC\Files\Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, \OCP\Files\Storage\IStorage $storage) {
225
-			if (!$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) {
226
-				return new \OC\Files\Storage\Wrapper\Availability(['storage' => $storage]);
227
-			}
228
-			return $storage;
229
-		});
230
-
231
-		\OC\Files\Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) {
232
-			if ($mount->getOption('encoding_compatibility', false) && !$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) {
233
-				return new \OC\Files\Storage\Wrapper\Encoding(['storage' => $storage]);
234
-			}
235
-			return $storage;
236
-		});
237
-
238
-		\OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) {
239
-			// set up quota for home storages, even for other users
240
-			// which can happen when using sharing
241
-
242
-			/**
243
-			 * @var \OC\Files\Storage\Storage $storage
244
-			 */
245
-			if ($storage->instanceOfStorage('\OC\Files\Storage\Home')
246
-				|| $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')
247
-			) {
248
-				/** @var \OC\Files\Storage\Home $storage */
249
-				if (is_object($storage->getUser())) {
250
-					$user = $storage->getUser()->getUID();
251
-					$quota = OC_Util::getUserQuota($user);
252
-					if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) {
253
-						return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files'));
254
-					}
255
-				}
256
-			}
257
-
258
-			return $storage;
259
-		});
260
-
261
-		OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user));
262
-		\OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(true);
263
-
264
-		//check if we are using an object storage
265
-		$objectStore = \OC::$server->getSystemConfig()->getValue('objectstore', null);
266
-		$objectStoreMultibucket = \OC::$server->getSystemConfig()->getValue('objectstore_multibucket', null);
267
-
268
-		// use the same order as in ObjectHomeMountProvider
269
-		if (isset($objectStoreMultibucket)) {
270
-			self::initObjectStoreMultibucketRootFS($objectStoreMultibucket);
271
-		} elseif (isset($objectStore)) {
272
-			self::initObjectStoreRootFS($objectStore);
273
-		} else {
274
-			self::initLocalStorageRootFS();
275
-		}
276
-
277
-		if ($user != '' && !OCP\User::userExists($user)) {
278
-			\OC::$server->getEventLogger()->end('setup_fs');
279
-			return false;
280
-		}
281
-
282
-		//if we aren't logged in, there is no use to set up the filesystem
283
-		if ($user != "") {
284
-
285
-			$userDir = '/' . $user . '/files';
286
-
287
-			//jail the user into his "home" directory
288
-			\OC\Files\Filesystem::init($user, $userDir);
289
-
290
-			OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir));
291
-		}
292
-		\OC::$server->getEventLogger()->end('setup_fs');
293
-		return true;
294
-	}
295
-
296
-	/**
297
-	 * check if a password is required for each public link
298
-	 *
299
-	 * @return boolean
300
-	 * @suppress PhanDeprecatedFunction
301
-	 */
302
-	public static function isPublicLinkPasswordRequired() {
303
-		$appConfig = \OC::$server->getAppConfig();
304
-		$enforcePassword = $appConfig->getValue('core', 'shareapi_enforce_links_password', 'no');
305
-		return ($enforcePassword === 'yes') ? true : false;
306
-	}
307
-
308
-	/**
309
-	 * check if sharing is disabled for the current user
310
-	 * @param IConfig $config
311
-	 * @param IGroupManager $groupManager
312
-	 * @param IUser|null $user
313
-	 * @return bool
314
-	 */
315
-	public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) {
316
-		if ($config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
317
-			$groupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', '');
318
-			$excludedGroups = json_decode($groupsList);
319
-			if (is_null($excludedGroups)) {
320
-				$excludedGroups = explode(',', $groupsList);
321
-				$newValue = json_encode($excludedGroups);
322
-				$config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
323
-			}
324
-			$usersGroups = $groupManager->getUserGroupIds($user);
325
-			if (!empty($usersGroups)) {
326
-				$remainingGroups = array_diff($usersGroups, $excludedGroups);
327
-				// if the user is only in groups which are disabled for sharing then
328
-				// sharing is also disabled for the user
329
-				if (empty($remainingGroups)) {
330
-					return true;
331
-				}
332
-			}
333
-		}
334
-		return false;
335
-	}
336
-
337
-	/**
338
-	 * check if share API enforces a default expire date
339
-	 *
340
-	 * @return boolean
341
-	 * @suppress PhanDeprecatedFunction
342
-	 */
343
-	public static function isDefaultExpireDateEnforced() {
344
-		$isDefaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no');
345
-		$enforceDefaultExpireDate = false;
346
-		if ($isDefaultExpireDateEnabled === 'yes') {
347
-			$value = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no');
348
-			$enforceDefaultExpireDate = ($value === 'yes') ? true : false;
349
-		}
350
-
351
-		return $enforceDefaultExpireDate;
352
-	}
353
-
354
-	/**
355
-	 * Get the quota of a user
356
-	 *
357
-	 * @param string $userId
358
-	 * @return float Quota bytes
359
-	 */
360
-	public static function getUserQuota($userId) {
361
-		$user = \OC::$server->getUserManager()->get($userId);
362
-		if (is_null($user)) {
363
-			return \OCP\Files\FileInfo::SPACE_UNLIMITED;
364
-		}
365
-		$userQuota = $user->getQuota();
366
-		if($userQuota === 'none') {
367
-			return \OCP\Files\FileInfo::SPACE_UNLIMITED;
368
-		}
369
-		return OC_Helper::computerFileSize($userQuota);
370
-	}
371
-
372
-	/**
373
-	 * copies the skeleton to the users /files
374
-	 *
375
-	 * @param String $userId
376
-	 * @param \OCP\Files\Folder $userDirectory
377
-	 * @throws \RuntimeException
378
-	 * @suppress PhanDeprecatedFunction
379
-	 */
380
-	public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) {
381
-
382
-		$skeletonDirectory = \OC::$server->getConfig()->getSystemValue('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton');
383
-		$instanceId = \OC::$server->getConfig()->getSystemValue('instanceid', '');
384
-
385
-		if ($instanceId === null) {
386
-			throw new \RuntimeException('no instance id!');
387
-		}
388
-		$appdata = 'appdata_' . $instanceId;
389
-		if ($userId === $appdata) {
390
-			throw new \RuntimeException('username is reserved name: ' . $appdata);
391
-		}
392
-
393
-		if (!empty($skeletonDirectory)) {
394
-			\OCP\Util::writeLog(
395
-				'files_skeleton',
396
-				'copying skeleton for '.$userId.' from '.$skeletonDirectory.' to '.$userDirectory->getFullPath('/'),
397
-				\OCP\Util::DEBUG
398
-			);
399
-			self::copyr($skeletonDirectory, $userDirectory);
400
-			// update the file cache
401
-			$userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
402
-		}
403
-	}
404
-
405
-	/**
406
-	 * copies a directory recursively by using streams
407
-	 *
408
-	 * @param string $source
409
-	 * @param \OCP\Files\Folder $target
410
-	 * @return void
411
-	 */
412
-	public static function copyr($source, \OCP\Files\Folder $target) {
413
-		$logger = \OC::$server->getLogger();
414
-
415
-		// Verify if folder exists
416
-		$dir = opendir($source);
417
-		if($dir === false) {
418
-			$logger->error(sprintf('Could not opendir "%s"', $source), ['app' => 'core']);
419
-			return;
420
-		}
421
-
422
-		// Copy the files
423
-		while (false !== ($file = readdir($dir))) {
424
-			if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
425
-				if (is_dir($source . '/' . $file)) {
426
-					$child = $target->newFolder($file);
427
-					self::copyr($source . '/' . $file, $child);
428
-				} else {
429
-					$child = $target->newFile($file);
430
-					$sourceStream = fopen($source . '/' . $file, 'r');
431
-					if($sourceStream === false) {
432
-						$logger->error(sprintf('Could not fopen "%s"', $source . '/' . $file), ['app' => 'core']);
433
-						closedir($dir);
434
-						return;
435
-					}
436
-					stream_copy_to_stream($sourceStream, $child->fopen('w'));
437
-				}
438
-			}
439
-		}
440
-		closedir($dir);
441
-	}
442
-
443
-	/**
444
-	 * @return void
445
-	 * @suppress PhanUndeclaredMethod
446
-	 */
447
-	public static function tearDownFS() {
448
-		\OC\Files\Filesystem::tearDown();
449
-		\OC::$server->getRootFolder()->clearCache();
450
-		self::$fsSetup = false;
451
-		self::$rootMounted = false;
452
-	}
453
-
454
-	/**
455
-	 * get the current installed version of ownCloud
456
-	 *
457
-	 * @return array
458
-	 */
459
-	public static function getVersion() {
460
-		OC_Util::loadVersion();
461
-		return self::$versionCache['OC_Version'];
462
-	}
463
-
464
-	/**
465
-	 * get the current installed version string of ownCloud
466
-	 *
467
-	 * @return string
468
-	 */
469
-	public static function getVersionString() {
470
-		OC_Util::loadVersion();
471
-		return self::$versionCache['OC_VersionString'];
472
-	}
473
-
474
-	/**
475
-	 * @deprecated the value is of no use anymore
476
-	 * @return string
477
-	 */
478
-	public static function getEditionString() {
479
-		return '';
480
-	}
481
-
482
-	/**
483
-	 * @description get the update channel of the current installed of ownCloud.
484
-	 * @return string
485
-	 */
486
-	public static function getChannel() {
487
-		OC_Util::loadVersion();
488
-		return \OC::$server->getConfig()->getSystemValue('updater.release.channel', self::$versionCache['OC_Channel']);
489
-	}
490
-
491
-	/**
492
-	 * @description get the build number of the current installed of ownCloud.
493
-	 * @return string
494
-	 */
495
-	public static function getBuild() {
496
-		OC_Util::loadVersion();
497
-		return self::$versionCache['OC_Build'];
498
-	}
499
-
500
-	/**
501
-	 * @description load the version.php into the session as cache
502
-	 * @suppress PhanUndeclaredVariable
503
-	 */
504
-	private static function loadVersion() {
505
-		if (self::$versionCache !== null) {
506
-			return;
507
-		}
508
-
509
-		$timestamp = filemtime(OC::$SERVERROOT . '/version.php');
510
-		require OC::$SERVERROOT . '/version.php';
511
-		/** @var $timestamp int */
512
-		self::$versionCache['OC_Version_Timestamp'] = $timestamp;
513
-		/** @var $OC_Version string */
514
-		self::$versionCache['OC_Version'] = $OC_Version;
515
-		/** @var $OC_VersionString string */
516
-		self::$versionCache['OC_VersionString'] = $OC_VersionString;
517
-		/** @var $OC_Build string */
518
-		self::$versionCache['OC_Build'] = $OC_Build;
519
-
520
-		/** @var $OC_Channel string */
521
-		self::$versionCache['OC_Channel'] = $OC_Channel;
522
-	}
523
-
524
-	/**
525
-	 * generates a path for JS/CSS files. If no application is provided it will create the path for core.
526
-	 *
527
-	 * @param string $application application to get the files from
528
-	 * @param string $directory directory within this application (css, js, vendor, etc)
529
-	 * @param string $file the file inside of the above folder
530
-	 * @return string the path
531
-	 */
532
-	private static function generatePath($application, $directory, $file) {
533
-		if (is_null($file)) {
534
-			$file = $application;
535
-			$application = "";
536
-		}
537
-		if (!empty($application)) {
538
-			return "$application/$directory/$file";
539
-		} else {
540
-			return "$directory/$file";
541
-		}
542
-	}
543
-
544
-	/**
545
-	 * add a javascript file
546
-	 *
547
-	 * @param string $application application id
548
-	 * @param string|null $file filename
549
-	 * @param bool $prepend prepend the Script to the beginning of the list
550
-	 * @return void
551
-	 */
552
-	public static function addScript($application, $file = null, $prepend = false) {
553
-		$path = OC_Util::generatePath($application, 'js', $file);
554
-
555
-		// core js files need separate handling
556
-		if ($application !== 'core' && $file !== null) {
557
-			self::addTranslations ( $application );
558
-		}
559
-		self::addExternalResource($application, $prepend, $path, "script");
560
-	}
561
-
562
-	/**
563
-	 * add a javascript file from the vendor sub folder
564
-	 *
565
-	 * @param string $application application id
566
-	 * @param string|null $file filename
567
-	 * @param bool $prepend prepend the Script to the beginning of the list
568
-	 * @return void
569
-	 */
570
-	public static function addVendorScript($application, $file = null, $prepend = false) {
571
-		$path = OC_Util::generatePath($application, 'vendor', $file);
572
-		self::addExternalResource($application, $prepend, $path, "script");
573
-	}
574
-
575
-	/**
576
-	 * add a translation JS file
577
-	 *
578
-	 * @param string $application application id
579
-	 * @param string|null $languageCode language code, defaults to the current language
580
-	 * @param bool|null $prepend prepend the Script to the beginning of the list
581
-	 */
582
-	public static function addTranslations($application, $languageCode = null, $prepend = false) {
583
-		if (is_null($languageCode)) {
584
-			$languageCode = \OC::$server->getL10NFactory()->findLanguage($application);
585
-		}
586
-		if (!empty($application)) {
587
-			$path = "$application/l10n/$languageCode";
588
-		} else {
589
-			$path = "l10n/$languageCode";
590
-		}
591
-		self::addExternalResource($application, $prepend, $path, "script");
592
-	}
593
-
594
-	/**
595
-	 * add a css file
596
-	 *
597
-	 * @param string $application application id
598
-	 * @param string|null $file filename
599
-	 * @param bool $prepend prepend the Style to the beginning of the list
600
-	 * @return void
601
-	 */
602
-	public static function addStyle($application, $file = null, $prepend = false) {
603
-		$path = OC_Util::generatePath($application, 'css', $file);
604
-		self::addExternalResource($application, $prepend, $path, "style");
605
-	}
606
-
607
-	/**
608
-	 * add a css file from the vendor sub folder
609
-	 *
610
-	 * @param string $application application id
611
-	 * @param string|null $file filename
612
-	 * @param bool $prepend prepend the Style to the beginning of the list
613
-	 * @return void
614
-	 */
615
-	public static function addVendorStyle($application, $file = null, $prepend = false) {
616
-		$path = OC_Util::generatePath($application, 'vendor', $file);
617
-		self::addExternalResource($application, $prepend, $path, "style");
618
-	}
619
-
620
-	/**
621
-	 * add an external resource css/js file
622
-	 *
623
-	 * @param string $application application id
624
-	 * @param bool $prepend prepend the file to the beginning of the list
625
-	 * @param string $path
626
-	 * @param string $type (script or style)
627
-	 * @return void
628
-	 */
629
-	private static function addExternalResource($application, $prepend, $path, $type = "script") {
630
-
631
-		if ($type === "style") {
632
-			if (!in_array($path, self::$styles)) {
633
-				if ($prepend === true) {
634
-					array_unshift ( self::$styles, $path );
635
-				} else {
636
-					self::$styles[] = $path;
637
-				}
638
-			}
639
-		} elseif ($type === "script") {
640
-			if (!in_array($path, self::$scripts)) {
641
-				if ($prepend === true) {
642
-					array_unshift ( self::$scripts, $path );
643
-				} else {
644
-					self::$scripts [] = $path;
645
-				}
646
-			}
647
-		}
648
-	}
649
-
650
-	/**
651
-	 * Add a custom element to the header
652
-	 * If $text is null then the element will be written as empty element.
653
-	 * So use "" to get a closing tag.
654
-	 * @param string $tag tag name of the element
655
-	 * @param array $attributes array of attributes for the element
656
-	 * @param string $text the text content for the element
657
-	 */
658
-	public static function addHeader($tag, $attributes, $text=null) {
659
-		self::$headers[] = array(
660
-			'tag' => $tag,
661
-			'attributes' => $attributes,
662
-			'text' => $text
663
-		);
664
-	}
665
-
666
-	/**
667
-	 * formats a timestamp in the "right" way
668
-	 *
669
-	 * @param int $timestamp
670
-	 * @param bool $dateOnly option to omit time from the result
671
-	 * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
672
-	 * @return string timestamp
673
-	 *
674
-	 * @deprecated Use \OC::$server->query('DateTimeFormatter') instead
675
-	 */
676
-	public static function formatDate($timestamp, $dateOnly = false, $timeZone = null) {
677
-		if ($timeZone !== null && !$timeZone instanceof \DateTimeZone) {
678
-			$timeZone = new \DateTimeZone($timeZone);
679
-		}
680
-
681
-		/** @var \OC\DateTimeFormatter $formatter */
682
-		$formatter = \OC::$server->query('DateTimeFormatter');
683
-		if ($dateOnly) {
684
-			return $formatter->formatDate($timestamp, 'long', $timeZone);
685
-		}
686
-		return $formatter->formatDateTime($timestamp, 'long', 'long', $timeZone);
687
-	}
688
-
689
-	/**
690
-	 * check if the current server configuration is suitable for ownCloud
691
-	 *
692
-	 * @param \OC\SystemConfig $config
693
-	 * @return array arrays with error messages and hints
694
-	 */
695
-	public static function checkServer(\OC\SystemConfig $config) {
696
-		$l = \OC::$server->getL10N('lib');
697
-		$errors = array();
698
-		$CONFIG_DATADIRECTORY = $config->getValue('datadirectory', OC::$SERVERROOT . '/data');
699
-
700
-		if (!self::needUpgrade($config) && $config->getValue('installed', false)) {
701
-			// this check needs to be done every time
702
-			$errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY);
703
-		}
704
-
705
-		// Assume that if checkServer() succeeded before in this session, then all is fine.
706
-		if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) {
707
-			return $errors;
708
-		}
709
-
710
-		$webServerRestart = false;
711
-		$setup = new \OC\Setup($config, \OC::$server->getIniWrapper(), \OC::$server->getL10N('lib'),
712
-			\OC::$server->query(\OCP\Defaults::class), \OC::$server->getLogger(), \OC::$server->getSecureRandom());
713
-
714
-		$urlGenerator = \OC::$server->getURLGenerator();
715
-
716
-		$availableDatabases = $setup->getSupportedDatabases();
717
-		if (empty($availableDatabases)) {
718
-			$errors[] = array(
719
-				'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'),
720
-				'hint' => '' //TODO: sane hint
721
-			);
722
-			$webServerRestart = true;
723
-		}
724
-
725
-		// Check if config folder is writable.
726
-		if(!OC_Helper::isReadOnlyConfigEnabled()) {
727
-			if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) {
728
-				$errors[] = array(
729
-					'error' => $l->t('Cannot write into "config" directory'),
730
-					'hint' => $l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s',
731
-						[$urlGenerator->linkToDocs('admin-dir_permissions')])
732
-				);
733
-			}
734
-		}
735
-
736
-		// Check if there is a writable install folder.
737
-		if ($config->getValue('appstoreenabled', true)) {
738
-			if (OC_App::getInstallPath() === null
739
-				|| !is_writable(OC_App::getInstallPath())
740
-				|| !is_readable(OC_App::getInstallPath())
741
-			) {
742
-				$errors[] = array(
743
-					'error' => $l->t('Cannot write into "apps" directory'),
744
-					'hint' => $l->t('This can usually be fixed by giving the webserver write access to the apps directory'
745
-						. ' or disabling the appstore in the config file. See %s',
746
-						[$urlGenerator->linkToDocs('admin-dir_permissions')])
747
-				);
748
-			}
749
-		}
750
-		// Create root dir.
751
-		if ($config->getValue('installed', false)) {
752
-			if (!is_dir($CONFIG_DATADIRECTORY)) {
753
-				$success = @mkdir($CONFIG_DATADIRECTORY);
754
-				if ($success) {
755
-					$errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
756
-				} else {
757
-					$errors[] = [
758
-						'error' => $l->t('Cannot create "data" directory'),
759
-						'hint' => $l->t('This can usually be fixed by giving the webserver write access to the root directory. See %s',
760
-							[$urlGenerator->linkToDocs('admin-dir_permissions')])
761
-					];
762
-				}
763
-			} else if (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) {
764
-				//common hint for all file permissions error messages
765
-				$permissionsHint = $l->t('Permissions can usually be fixed by giving the webserver write access to the root directory. See %s.',
766
-					[$urlGenerator->linkToDocs('admin-dir_permissions')]);
767
-				$errors[] = [
768
-					'error' => 'Your data directory is not writable',
769
-					'hint' => $permissionsHint
770
-				];
771
-			} else {
772
-				$errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
773
-			}
774
-		}
775
-
776
-		if (!OC_Util::isSetLocaleWorking()) {
777
-			$errors[] = array(
778
-				'error' => $l->t('Setting locale to %s failed',
779
-					array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/'
780
-						. 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8')),
781
-				'hint' => $l->t('Please install one of these locales on your system and restart your webserver.')
782
-			);
783
-		}
784
-
785
-		// Contains the dependencies that should be checked against
786
-		// classes = class_exists
787
-		// functions = function_exists
788
-		// defined = defined
789
-		// ini = ini_get
790
-		// If the dependency is not found the missing module name is shown to the EndUser
791
-		// When adding new checks always verify that they pass on Travis as well
792
-		// for ini settings, see https://github.com/owncloud/administration/blob/master/travis-ci/custom.ini
793
-		$dependencies = array(
794
-			'classes' => array(
795
-				'ZipArchive' => 'zip',
796
-				'DOMDocument' => 'dom',
797
-				'XMLWriter' => 'XMLWriter',
798
-				'XMLReader' => 'XMLReader',
799
-			),
800
-			'functions' => [
801
-				'xml_parser_create' => 'libxml',
802
-				'mb_strcut' => 'mb multibyte',
803
-				'ctype_digit' => 'ctype',
804
-				'json_encode' => 'JSON',
805
-				'gd_info' => 'GD',
806
-				'gzencode' => 'zlib',
807
-				'iconv' => 'iconv',
808
-				'simplexml_load_string' => 'SimpleXML',
809
-				'hash' => 'HASH Message Digest Framework',
810
-				'curl_init' => 'cURL',
811
-				'openssl_verify' => 'OpenSSL',
812
-			],
813
-			'defined' => array(
814
-				'PDO::ATTR_DRIVER_NAME' => 'PDO'
815
-			),
816
-			'ini' => [
817
-				'default_charset' => 'UTF-8',
818
-			],
819
-		);
820
-		$missingDependencies = array();
821
-		$invalidIniSettings = [];
822
-		$moduleHint = $l->t('Please ask your server administrator to install the module.');
823
-
824
-		/**
825
-		 * FIXME: The dependency check does not work properly on HHVM on the moment
826
-		 *        and prevents installation. Once HHVM is more compatible with our
827
-		 *        approach to check for these values we should re-enable those
828
-		 *        checks.
829
-		 */
830
-		$iniWrapper = \OC::$server->getIniWrapper();
831
-		if (!self::runningOnHhvm()) {
832
-			foreach ($dependencies['classes'] as $class => $module) {
833
-				if (!class_exists($class)) {
834
-					$missingDependencies[] = $module;
835
-				}
836
-			}
837
-			foreach ($dependencies['functions'] as $function => $module) {
838
-				if (!function_exists($function)) {
839
-					$missingDependencies[] = $module;
840
-				}
841
-			}
842
-			foreach ($dependencies['defined'] as $defined => $module) {
843
-				if (!defined($defined)) {
844
-					$missingDependencies[] = $module;
845
-				}
846
-			}
847
-			foreach ($dependencies['ini'] as $setting => $expected) {
848
-				if (is_bool($expected)) {
849
-					if ($iniWrapper->getBool($setting) !== $expected) {
850
-						$invalidIniSettings[] = [$setting, $expected];
851
-					}
852
-				}
853
-				if (is_int($expected)) {
854
-					if ($iniWrapper->getNumeric($setting) !== $expected) {
855
-						$invalidIniSettings[] = [$setting, $expected];
856
-					}
857
-				}
858
-				if (is_string($expected)) {
859
-					if (strtolower($iniWrapper->getString($setting)) !== strtolower($expected)) {
860
-						$invalidIniSettings[] = [$setting, $expected];
861
-					}
862
-				}
863
-			}
864
-		}
865
-
866
-		foreach($missingDependencies as $missingDependency) {
867
-			$errors[] = array(
868
-				'error' => $l->t('PHP module %s not installed.', array($missingDependency)),
869
-				'hint' => $moduleHint
870
-			);
871
-			$webServerRestart = true;
872
-		}
873
-		foreach($invalidIniSettings as $setting) {
874
-			if(is_bool($setting[1])) {
875
-				$setting[1] = ($setting[1]) ? 'on' : 'off';
876
-			}
877
-			$errors[] = [
878
-				'error' => $l->t('PHP setting "%s" is not set to "%s".', [$setting[0], var_export($setting[1], true)]),
879
-				'hint' =>  $l->t('Adjusting this setting in php.ini will make Nextcloud run again')
880
-			];
881
-			$webServerRestart = true;
882
-		}
883
-
884
-		/**
885
-		 * The mbstring.func_overload check can only be performed if the mbstring
886
-		 * module is installed as it will return null if the checking setting is
887
-		 * not available and thus a check on the boolean value fails.
888
-		 *
889
-		 * TODO: Should probably be implemented in the above generic dependency
890
-		 *       check somehow in the long-term.
891
-		 */
892
-		if($iniWrapper->getBool('mbstring.func_overload') !== null &&
893
-			$iniWrapper->getBool('mbstring.func_overload') === true) {
894
-			$errors[] = array(
895
-				'error' => $l->t('mbstring.func_overload is set to "%s" instead of the expected value "0"', [$iniWrapper->getString('mbstring.func_overload')]),
896
-				'hint' => $l->t('To fix this issue set <code>mbstring.func_overload</code> to <code>0</code> in your php.ini')
897
-			);
898
-		}
899
-
900
-		if(function_exists('xml_parser_create') &&
901
-			LIBXML_LOADED_VERSION < 20700 ) {
902
-			$version = LIBXML_LOADED_VERSION;
903
-			$major = floor($version/10000);
904
-			$version -= ($major * 10000);
905
-			$minor = floor($version/100);
906
-			$version -= ($minor * 100);
907
-			$patch = $version;
908
-			$errors[] = array(
909
-				'error' => $l->t('libxml2 2.7.0 is at least required. Currently %s is installed.', [$major . '.' . $minor . '.' . $patch]),
910
-				'hint' => $l->t('To fix this issue update your libxml2 version and restart your web server.')
911
-			);
912
-		}
913
-
914
-		if (!self::isAnnotationsWorking()) {
915
-			$errors[] = array(
916
-				'error' => $l->t('PHP is apparently set up to strip inline doc blocks. This will make several core apps inaccessible.'),
917
-				'hint' => $l->t('This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.')
918
-			);
919
-		}
920
-
921
-		if (!\OC::$CLI && $webServerRestart) {
922
-			$errors[] = array(
923
-				'error' => $l->t('PHP modules have been installed, but they are still listed as missing?'),
924
-				'hint' => $l->t('Please ask your server administrator to restart the web server.')
925
-			);
926
-		}
927
-
928
-		$errors = array_merge($errors, self::checkDatabaseVersion());
929
-
930
-		// Cache the result of this function
931
-		\OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0);
932
-
933
-		return $errors;
934
-	}
935
-
936
-	/**
937
-	 * Check the database version
938
-	 *
939
-	 * @return array errors array
940
-	 */
941
-	public static function checkDatabaseVersion() {
942
-		$l = \OC::$server->getL10N('lib');
943
-		$errors = array();
944
-		$dbType = \OC::$server->getSystemConfig()->getValue('dbtype', 'sqlite');
945
-		if ($dbType === 'pgsql') {
946
-			// check PostgreSQL version
947
-			try {
948
-				$result = \OC_DB::executeAudited('SHOW SERVER_VERSION');
949
-				$data = $result->fetchRow();
950
-				if (isset($data['server_version'])) {
951
-					$version = $data['server_version'];
952
-					if (version_compare($version, '9.0.0', '<')) {
953
-						$errors[] = array(
954
-							'error' => $l->t('PostgreSQL >= 9 required'),
955
-							'hint' => $l->t('Please upgrade your database version')
956
-						);
957
-					}
958
-				}
959
-			} catch (\Doctrine\DBAL\DBALException $e) {
960
-				$logger = \OC::$server->getLogger();
961
-				$logger->warning('Error occurred while checking PostgreSQL version, assuming >= 9');
962
-				$logger->logException($e);
963
-			}
964
-		}
965
-		return $errors;
966
-	}
967
-
968
-	/**
969
-	 * Check for correct file permissions of data directory
970
-	 *
971
-	 * @param string $dataDirectory
972
-	 * @return array arrays with error messages and hints
973
-	 */
974
-	public static function checkDataDirectoryPermissions($dataDirectory) {
975
-		$l = \OC::$server->getL10N('lib');
976
-		$errors = array();
977
-		$permissionsModHint = $l->t('Please change the permissions to 0770 so that the directory'
978
-			. ' cannot be listed by other users.');
979
-		$perms = substr(decoct(@fileperms($dataDirectory)), -3);
980
-		if (substr($perms, -1) !== '0') {
981
-			chmod($dataDirectory, 0770);
982
-			clearstatcache();
983
-			$perms = substr(decoct(@fileperms($dataDirectory)), -3);
984
-			if ($perms[2] !== '0') {
985
-				$errors[] = [
986
-					'error' => $l->t('Your data directory is readable by other users'),
987
-					'hint' => $permissionsModHint
988
-				];
989
-			}
990
-		}
991
-		return $errors;
992
-	}
993
-
994
-	/**
995
-	 * Check that the data directory exists and is valid by
996
-	 * checking the existence of the ".ocdata" file.
997
-	 *
998
-	 * @param string $dataDirectory data directory path
999
-	 * @return array errors found
1000
-	 */
1001
-	public static function checkDataDirectoryValidity($dataDirectory) {
1002
-		$l = \OC::$server->getL10N('lib');
1003
-		$errors = [];
1004
-		if ($dataDirectory[0] !== '/') {
1005
-			$errors[] = [
1006
-				'error' => $l->t('Your data directory must be an absolute path'),
1007
-				'hint' => $l->t('Check the value of "datadirectory" in your configuration')
1008
-			];
1009
-		}
1010
-		if (!file_exists($dataDirectory . '/.ocdata')) {
1011
-			$errors[] = [
1012
-				'error' => $l->t('Your data directory is invalid'),
1013
-				'hint' => $l->t('Ensure there is a file called ".ocdata"' .
1014
-					' in the root of the data directory.')
1015
-			];
1016
-		}
1017
-		return $errors;
1018
-	}
1019
-
1020
-	/**
1021
-	 * Check if the user is logged in, redirects to home if not. With
1022
-	 * redirect URL parameter to the request URI.
1023
-	 *
1024
-	 * @return void
1025
-	 */
1026
-	public static function checkLoggedIn() {
1027
-		// Check if we are a user
1028
-		if (!\OC::$server->getUserSession()->isLoggedIn()) {
1029
-			header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute(
1030
-						'core.login.showLoginForm',
1031
-						[
1032
-							'redirect_url' => \OC::$server->getRequest()->getRequestUri(),
1033
-						]
1034
-					)
1035
-			);
1036
-			exit();
1037
-		}
1038
-		// Redirect to 2FA challenge selection if 2FA challenge was not solved yet
1039
-		if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
1040
-			header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
1041
-			exit();
1042
-		}
1043
-	}
1044
-
1045
-	/**
1046
-	 * Check if the user is a admin, redirects to home if not
1047
-	 *
1048
-	 * @return void
1049
-	 */
1050
-	public static function checkAdminUser() {
1051
-		OC_Util::checkLoggedIn();
1052
-		if (!OC_User::isAdminUser(OC_User::getUser())) {
1053
-			header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php'));
1054
-			exit();
1055
-		}
1056
-	}
1057
-
1058
-	/**
1059
-	 * Check if the user is a subadmin, redirects to home if not
1060
-	 *
1061
-	 * @return null|boolean $groups where the current user is subadmin
1062
-	 */
1063
-	public static function checkSubAdminUser() {
1064
-		OC_Util::checkLoggedIn();
1065
-		$userObject = \OC::$server->getUserSession()->getUser();
1066
-		$isSubAdmin = false;
1067
-		if($userObject !== null) {
1068
-			$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
1069
-		}
1070
-
1071
-		if (!$isSubAdmin) {
1072
-			header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php'));
1073
-			exit();
1074
-		}
1075
-		return true;
1076
-	}
1077
-
1078
-	/**
1079
-	 * Returns the URL of the default page
1080
-	 * based on the system configuration and
1081
-	 * the apps visible for the current user
1082
-	 *
1083
-	 * @return string URL
1084
-	 * @suppress PhanDeprecatedFunction
1085
-	 */
1086
-	public static function getDefaultPageUrl() {
1087
-		$urlGenerator = \OC::$server->getURLGenerator();
1088
-		// Deny the redirect if the URL contains a @
1089
-		// This prevents unvalidated redirects like ?redirect_url=:[email protected]
1090
-		if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) {
1091
-			$location = $urlGenerator->getAbsoluteURL(urldecode($_REQUEST['redirect_url']));
1092
-		} else {
1093
-			$defaultPage = \OC::$server->getAppConfig()->getValue('core', 'defaultpage');
1094
-			if ($defaultPage) {
1095
-				$location = $urlGenerator->getAbsoluteURL($defaultPage);
1096
-			} else {
1097
-				$appId = 'files';
1098
-				$config = \OC::$server->getConfig();
1099
-				$defaultApps = explode(',', $config->getSystemValue('defaultapp', 'files'));
1100
-				// find the first app that is enabled for the current user
1101
-				foreach ($defaultApps as $defaultApp) {
1102
-					$defaultApp = OC_App::cleanAppId(strip_tags($defaultApp));
1103
-					if (static::getAppManager()->isEnabledForUser($defaultApp)) {
1104
-						$appId = $defaultApp;
1105
-						break;
1106
-					}
1107
-				}
1108
-
1109
-				if($config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true') {
1110
-					$location = $urlGenerator->getAbsoluteURL('/apps/' . $appId . '/');
1111
-				} else {
1112
-					$location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $appId . '/');
1113
-				}
1114
-			}
1115
-		}
1116
-		return $location;
1117
-	}
1118
-
1119
-	/**
1120
-	 * Redirect to the user default page
1121
-	 *
1122
-	 * @return void
1123
-	 */
1124
-	public static function redirectToDefaultPage() {
1125
-		$location = self::getDefaultPageUrl();
1126
-		header('Location: ' . $location);
1127
-		exit();
1128
-	}
1129
-
1130
-	/**
1131
-	 * get an id unique for this instance
1132
-	 *
1133
-	 * @return string
1134
-	 */
1135
-	public static function getInstanceId() {
1136
-		$id = \OC::$server->getSystemConfig()->getValue('instanceid', null);
1137
-		if (is_null($id)) {
1138
-			// We need to guarantee at least one letter in instanceid so it can be used as the session_name
1139
-			$id = 'oc' . \OC::$server->getSecureRandom()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
1140
-			\OC::$server->getSystemConfig()->setValue('instanceid', $id);
1141
-		}
1142
-		return $id;
1143
-	}
1144
-
1145
-	/**
1146
-	 * Public function to sanitize HTML
1147
-	 *
1148
-	 * This function is used to sanitize HTML and should be applied on any
1149
-	 * string or array of strings before displaying it on a web page.
1150
-	 *
1151
-	 * @param string|array $value
1152
-	 * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter.
1153
-	 */
1154
-	public static function sanitizeHTML($value) {
1155
-		if (is_array($value)) {
1156
-			$value = array_map(function($value) {
1157
-				return self::sanitizeHTML($value);
1158
-			}, $value);
1159
-		} else {
1160
-			// Specify encoding for PHP<5.4
1161
-			$value = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
1162
-		}
1163
-		return $value;
1164
-	}
1165
-
1166
-	/**
1167
-	 * Public function to encode url parameters
1168
-	 *
1169
-	 * This function is used to encode path to file before output.
1170
-	 * Encoding is done according to RFC 3986 with one exception:
1171
-	 * Character '/' is preserved as is.
1172
-	 *
1173
-	 * @param string $component part of URI to encode
1174
-	 * @return string
1175
-	 */
1176
-	public static function encodePath($component) {
1177
-		$encoded = rawurlencode($component);
1178
-		$encoded = str_replace('%2F', '/', $encoded);
1179
-		return $encoded;
1180
-	}
1181
-
1182
-
1183
-	public function createHtaccessTestFile(\OCP\IConfig $config) {
1184
-		// php dev server does not support htaccess
1185
-		if (php_sapi_name() === 'cli-server') {
1186
-			return false;
1187
-		}
1188
-
1189
-		// testdata
1190
-		$fileName = '/htaccesstest.txt';
1191
-		$testContent = 'This is used for testing whether htaccess is properly enabled to disallow access from the outside. This file can be safely removed.';
1192
-
1193
-		// creating a test file
1194
-		$testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName;
1195
-
1196
-		if (file_exists($testFile)) {// already running this test, possible recursive call
1197
-			return false;
1198
-		}
1199
-
1200
-		$fp = @fopen($testFile, 'w');
1201
-		if (!$fp) {
1202
-			throw new OC\HintException('Can\'t create test file to check for working .htaccess file.',
1203
-				'Make sure it is possible for the webserver to write to ' . $testFile);
1204
-		}
1205
-		fwrite($fp, $testContent);
1206
-		fclose($fp);
1207
-
1208
-		return $testContent;
1209
-	}
1210
-
1211
-	/**
1212
-	 * Check if the .htaccess file is working
1213
-	 * @param \OCP\IConfig $config
1214
-	 * @return bool
1215
-	 * @throws Exception
1216
-	 * @throws \OC\HintException If the test file can't get written.
1217
-	 */
1218
-	public function isHtaccessWorking(\OCP\IConfig $config) {
1219
-
1220
-		if (\OC::$CLI || !$config->getSystemValue('check_for_working_htaccess', true)) {
1221
-			return true;
1222
-		}
1223
-
1224
-		$testContent = $this->createHtaccessTestFile($config);
1225
-		if ($testContent === false) {
1226
-			return false;
1227
-		}
1228
-
1229
-		$fileName = '/htaccesstest.txt';
1230
-		$testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName;
1231
-
1232
-		// accessing the file via http
1233
-		$url = \OC::$server->getURLGenerator()->getAbsoluteURL(OC::$WEBROOT . '/data' . $fileName);
1234
-		try {
1235
-			$content = \OC::$server->getHTTPClientService()->newClient()->get($url)->getBody();
1236
-		} catch (\Exception $e) {
1237
-			$content = false;
1238
-		}
1239
-
1240
-		// cleanup
1241
-		@unlink($testFile);
1242
-
1243
-		/*
68
+    public static $scripts = array();
69
+    public static $styles = array();
70
+    public static $headers = array();
71
+    private static $rootMounted = false;
72
+    private static $fsSetup = false;
73
+
74
+    /** @var array Local cache of version.php */
75
+    private static $versionCache = null;
76
+
77
+    protected static function getAppManager() {
78
+        return \OC::$server->getAppManager();
79
+    }
80
+
81
+    private static function initLocalStorageRootFS() {
82
+        // mount local file backend as root
83
+        $configDataDirectory = \OC::$server->getSystemConfig()->getValue("datadirectory", OC::$SERVERROOT . "/data");
84
+        //first set up the local "root" storage
85
+        \OC\Files\Filesystem::initMountManager();
86
+        if (!self::$rootMounted) {
87
+            \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir' => $configDataDirectory), '/');
88
+            self::$rootMounted = true;
89
+        }
90
+    }
91
+
92
+    /**
93
+     * mounting an object storage as the root fs will in essence remove the
94
+     * necessity of a data folder being present.
95
+     * TODO make home storage aware of this and use the object storage instead of local disk access
96
+     *
97
+     * @param array $config containing 'class' and optional 'arguments'
98
+     * @suppress PhanDeprecatedFunction
99
+     */
100
+    private static function initObjectStoreRootFS($config) {
101
+        // check misconfiguration
102
+        if (empty($config['class'])) {
103
+            \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
104
+        }
105
+        if (!isset($config['arguments'])) {
106
+            $config['arguments'] = array();
107
+        }
108
+
109
+        // instantiate object store implementation
110
+        $name = $config['class'];
111
+        if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
112
+            $segments = explode('\\', $name);
113
+            OC_App::loadApp(strtolower($segments[1]));
114
+        }
115
+        $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
116
+        // mount with plain / root object store implementation
117
+        $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage';
118
+
119
+        // mount object storage as root
120
+        \OC\Files\Filesystem::initMountManager();
121
+        if (!self::$rootMounted) {
122
+            \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/');
123
+            self::$rootMounted = true;
124
+        }
125
+    }
126
+
127
+    /**
128
+     * mounting an object storage as the root fs will in essence remove the
129
+     * necessity of a data folder being present.
130
+     *
131
+     * @param array $config containing 'class' and optional 'arguments'
132
+     * @suppress PhanDeprecatedFunction
133
+     */
134
+    private static function initObjectStoreMultibucketRootFS($config) {
135
+        // check misconfiguration
136
+        if (empty($config['class'])) {
137
+            \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
138
+        }
139
+        if (!isset($config['arguments'])) {
140
+            $config['arguments'] = array();
141
+        }
142
+
143
+        // instantiate object store implementation
144
+        $name = $config['class'];
145
+        if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
146
+            $segments = explode('\\', $name);
147
+            OC_App::loadApp(strtolower($segments[1]));
148
+        }
149
+
150
+        if (!isset($config['arguments']['bucket'])) {
151
+            $config['arguments']['bucket'] = '';
152
+        }
153
+        // put the root FS always in first bucket for multibucket configuration
154
+        $config['arguments']['bucket'] .= '0';
155
+
156
+        $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
157
+        // mount with plain / root object store implementation
158
+        $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage';
159
+
160
+        // mount object storage as root
161
+        \OC\Files\Filesystem::initMountManager();
162
+        if (!self::$rootMounted) {
163
+            \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/');
164
+            self::$rootMounted = true;
165
+        }
166
+    }
167
+
168
+    /**
169
+     * Can be set up
170
+     *
171
+     * @param string $user
172
+     * @return boolean
173
+     * @description configure the initial filesystem based on the configuration
174
+     * @suppress PhanDeprecatedFunction
175
+     * @suppress PhanAccessMethodInternal
176
+     */
177
+    public static function setupFS($user = '') {
178
+        //setting up the filesystem twice can only lead to trouble
179
+        if (self::$fsSetup) {
180
+            return false;
181
+        }
182
+
183
+        \OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem');
184
+
185
+        // If we are not forced to load a specific user we load the one that is logged in
186
+        if ($user === null) {
187
+            $user = '';
188
+        } else if ($user == "" && \OC::$server->getUserSession()->isLoggedIn()) {
189
+            $user = OC_User::getUser();
190
+        }
191
+
192
+        // load all filesystem apps before, so no setup-hook gets lost
193
+        OC_App::loadApps(array('filesystem'));
194
+
195
+        // the filesystem will finish when $user is not empty,
196
+        // mark fs setup here to avoid doing the setup from loading
197
+        // OC_Filesystem
198
+        if ($user != '') {
199
+            self::$fsSetup = true;
200
+        }
201
+
202
+        \OC\Files\Filesystem::initMountManager();
203
+
204
+        \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
205
+        \OC\Files\Filesystem::addStorageWrapper('mount_options', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) {
206
+            if ($storage->instanceOfStorage('\OC\Files\Storage\Common')) {
207
+                /** @var \OC\Files\Storage\Common $storage */
208
+                $storage->setMountOptions($mount->getOptions());
209
+            }
210
+            return $storage;
211
+        });
212
+
213
+        \OC\Files\Filesystem::addStorageWrapper('enable_sharing', function ($mountPoint, \OCP\Files\Storage\IStorage $storage, \OCP\Files\Mount\IMountPoint $mount) {
214
+            if (!$mount->getOption('enable_sharing', true)) {
215
+                return new \OC\Files\Storage\Wrapper\PermissionsMask([
216
+                    'storage' => $storage,
217
+                    'mask' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_SHARE
218
+                ]);
219
+            }
220
+            return $storage;
221
+        });
222
+
223
+        // install storage availability wrapper, before most other wrappers
224
+        \OC\Files\Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, \OCP\Files\Storage\IStorage $storage) {
225
+            if (!$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) {
226
+                return new \OC\Files\Storage\Wrapper\Availability(['storage' => $storage]);
227
+            }
228
+            return $storage;
229
+        });
230
+
231
+        \OC\Files\Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) {
232
+            if ($mount->getOption('encoding_compatibility', false) && !$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) {
233
+                return new \OC\Files\Storage\Wrapper\Encoding(['storage' => $storage]);
234
+            }
235
+            return $storage;
236
+        });
237
+
238
+        \OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) {
239
+            // set up quota for home storages, even for other users
240
+            // which can happen when using sharing
241
+
242
+            /**
243
+             * @var \OC\Files\Storage\Storage $storage
244
+             */
245
+            if ($storage->instanceOfStorage('\OC\Files\Storage\Home')
246
+                || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')
247
+            ) {
248
+                /** @var \OC\Files\Storage\Home $storage */
249
+                if (is_object($storage->getUser())) {
250
+                    $user = $storage->getUser()->getUID();
251
+                    $quota = OC_Util::getUserQuota($user);
252
+                    if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) {
253
+                        return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files'));
254
+                    }
255
+                }
256
+            }
257
+
258
+            return $storage;
259
+        });
260
+
261
+        OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user));
262
+        \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(true);
263
+
264
+        //check if we are using an object storage
265
+        $objectStore = \OC::$server->getSystemConfig()->getValue('objectstore', null);
266
+        $objectStoreMultibucket = \OC::$server->getSystemConfig()->getValue('objectstore_multibucket', null);
267
+
268
+        // use the same order as in ObjectHomeMountProvider
269
+        if (isset($objectStoreMultibucket)) {
270
+            self::initObjectStoreMultibucketRootFS($objectStoreMultibucket);
271
+        } elseif (isset($objectStore)) {
272
+            self::initObjectStoreRootFS($objectStore);
273
+        } else {
274
+            self::initLocalStorageRootFS();
275
+        }
276
+
277
+        if ($user != '' && !OCP\User::userExists($user)) {
278
+            \OC::$server->getEventLogger()->end('setup_fs');
279
+            return false;
280
+        }
281
+
282
+        //if we aren't logged in, there is no use to set up the filesystem
283
+        if ($user != "") {
284
+
285
+            $userDir = '/' . $user . '/files';
286
+
287
+            //jail the user into his "home" directory
288
+            \OC\Files\Filesystem::init($user, $userDir);
289
+
290
+            OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir));
291
+        }
292
+        \OC::$server->getEventLogger()->end('setup_fs');
293
+        return true;
294
+    }
295
+
296
+    /**
297
+     * check if a password is required for each public link
298
+     *
299
+     * @return boolean
300
+     * @suppress PhanDeprecatedFunction
301
+     */
302
+    public static function isPublicLinkPasswordRequired() {
303
+        $appConfig = \OC::$server->getAppConfig();
304
+        $enforcePassword = $appConfig->getValue('core', 'shareapi_enforce_links_password', 'no');
305
+        return ($enforcePassword === 'yes') ? true : false;
306
+    }
307
+
308
+    /**
309
+     * check if sharing is disabled for the current user
310
+     * @param IConfig $config
311
+     * @param IGroupManager $groupManager
312
+     * @param IUser|null $user
313
+     * @return bool
314
+     */
315
+    public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) {
316
+        if ($config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
317
+            $groupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', '');
318
+            $excludedGroups = json_decode($groupsList);
319
+            if (is_null($excludedGroups)) {
320
+                $excludedGroups = explode(',', $groupsList);
321
+                $newValue = json_encode($excludedGroups);
322
+                $config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
323
+            }
324
+            $usersGroups = $groupManager->getUserGroupIds($user);
325
+            if (!empty($usersGroups)) {
326
+                $remainingGroups = array_diff($usersGroups, $excludedGroups);
327
+                // if the user is only in groups which are disabled for sharing then
328
+                // sharing is also disabled for the user
329
+                if (empty($remainingGroups)) {
330
+                    return true;
331
+                }
332
+            }
333
+        }
334
+        return false;
335
+    }
336
+
337
+    /**
338
+     * check if share API enforces a default expire date
339
+     *
340
+     * @return boolean
341
+     * @suppress PhanDeprecatedFunction
342
+     */
343
+    public static function isDefaultExpireDateEnforced() {
344
+        $isDefaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no');
345
+        $enforceDefaultExpireDate = false;
346
+        if ($isDefaultExpireDateEnabled === 'yes') {
347
+            $value = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no');
348
+            $enforceDefaultExpireDate = ($value === 'yes') ? true : false;
349
+        }
350
+
351
+        return $enforceDefaultExpireDate;
352
+    }
353
+
354
+    /**
355
+     * Get the quota of a user
356
+     *
357
+     * @param string $userId
358
+     * @return float Quota bytes
359
+     */
360
+    public static function getUserQuota($userId) {
361
+        $user = \OC::$server->getUserManager()->get($userId);
362
+        if (is_null($user)) {
363
+            return \OCP\Files\FileInfo::SPACE_UNLIMITED;
364
+        }
365
+        $userQuota = $user->getQuota();
366
+        if($userQuota === 'none') {
367
+            return \OCP\Files\FileInfo::SPACE_UNLIMITED;
368
+        }
369
+        return OC_Helper::computerFileSize($userQuota);
370
+    }
371
+
372
+    /**
373
+     * copies the skeleton to the users /files
374
+     *
375
+     * @param String $userId
376
+     * @param \OCP\Files\Folder $userDirectory
377
+     * @throws \RuntimeException
378
+     * @suppress PhanDeprecatedFunction
379
+     */
380
+    public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) {
381
+
382
+        $skeletonDirectory = \OC::$server->getConfig()->getSystemValue('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton');
383
+        $instanceId = \OC::$server->getConfig()->getSystemValue('instanceid', '');
384
+
385
+        if ($instanceId === null) {
386
+            throw new \RuntimeException('no instance id!');
387
+        }
388
+        $appdata = 'appdata_' . $instanceId;
389
+        if ($userId === $appdata) {
390
+            throw new \RuntimeException('username is reserved name: ' . $appdata);
391
+        }
392
+
393
+        if (!empty($skeletonDirectory)) {
394
+            \OCP\Util::writeLog(
395
+                'files_skeleton',
396
+                'copying skeleton for '.$userId.' from '.$skeletonDirectory.' to '.$userDirectory->getFullPath('/'),
397
+                \OCP\Util::DEBUG
398
+            );
399
+            self::copyr($skeletonDirectory, $userDirectory);
400
+            // update the file cache
401
+            $userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
402
+        }
403
+    }
404
+
405
+    /**
406
+     * copies a directory recursively by using streams
407
+     *
408
+     * @param string $source
409
+     * @param \OCP\Files\Folder $target
410
+     * @return void
411
+     */
412
+    public static function copyr($source, \OCP\Files\Folder $target) {
413
+        $logger = \OC::$server->getLogger();
414
+
415
+        // Verify if folder exists
416
+        $dir = opendir($source);
417
+        if($dir === false) {
418
+            $logger->error(sprintf('Could not opendir "%s"', $source), ['app' => 'core']);
419
+            return;
420
+        }
421
+
422
+        // Copy the files
423
+        while (false !== ($file = readdir($dir))) {
424
+            if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
425
+                if (is_dir($source . '/' . $file)) {
426
+                    $child = $target->newFolder($file);
427
+                    self::copyr($source . '/' . $file, $child);
428
+                } else {
429
+                    $child = $target->newFile($file);
430
+                    $sourceStream = fopen($source . '/' . $file, 'r');
431
+                    if($sourceStream === false) {
432
+                        $logger->error(sprintf('Could not fopen "%s"', $source . '/' . $file), ['app' => 'core']);
433
+                        closedir($dir);
434
+                        return;
435
+                    }
436
+                    stream_copy_to_stream($sourceStream, $child->fopen('w'));
437
+                }
438
+            }
439
+        }
440
+        closedir($dir);
441
+    }
442
+
443
+    /**
444
+     * @return void
445
+     * @suppress PhanUndeclaredMethod
446
+     */
447
+    public static function tearDownFS() {
448
+        \OC\Files\Filesystem::tearDown();
449
+        \OC::$server->getRootFolder()->clearCache();
450
+        self::$fsSetup = false;
451
+        self::$rootMounted = false;
452
+    }
453
+
454
+    /**
455
+     * get the current installed version of ownCloud
456
+     *
457
+     * @return array
458
+     */
459
+    public static function getVersion() {
460
+        OC_Util::loadVersion();
461
+        return self::$versionCache['OC_Version'];
462
+    }
463
+
464
+    /**
465
+     * get the current installed version string of ownCloud
466
+     *
467
+     * @return string
468
+     */
469
+    public static function getVersionString() {
470
+        OC_Util::loadVersion();
471
+        return self::$versionCache['OC_VersionString'];
472
+    }
473
+
474
+    /**
475
+     * @deprecated the value is of no use anymore
476
+     * @return string
477
+     */
478
+    public static function getEditionString() {
479
+        return '';
480
+    }
481
+
482
+    /**
483
+     * @description get the update channel of the current installed of ownCloud.
484
+     * @return string
485
+     */
486
+    public static function getChannel() {
487
+        OC_Util::loadVersion();
488
+        return \OC::$server->getConfig()->getSystemValue('updater.release.channel', self::$versionCache['OC_Channel']);
489
+    }
490
+
491
+    /**
492
+     * @description get the build number of the current installed of ownCloud.
493
+     * @return string
494
+     */
495
+    public static function getBuild() {
496
+        OC_Util::loadVersion();
497
+        return self::$versionCache['OC_Build'];
498
+    }
499
+
500
+    /**
501
+     * @description load the version.php into the session as cache
502
+     * @suppress PhanUndeclaredVariable
503
+     */
504
+    private static function loadVersion() {
505
+        if (self::$versionCache !== null) {
506
+            return;
507
+        }
508
+
509
+        $timestamp = filemtime(OC::$SERVERROOT . '/version.php');
510
+        require OC::$SERVERROOT . '/version.php';
511
+        /** @var $timestamp int */
512
+        self::$versionCache['OC_Version_Timestamp'] = $timestamp;
513
+        /** @var $OC_Version string */
514
+        self::$versionCache['OC_Version'] = $OC_Version;
515
+        /** @var $OC_VersionString string */
516
+        self::$versionCache['OC_VersionString'] = $OC_VersionString;
517
+        /** @var $OC_Build string */
518
+        self::$versionCache['OC_Build'] = $OC_Build;
519
+
520
+        /** @var $OC_Channel string */
521
+        self::$versionCache['OC_Channel'] = $OC_Channel;
522
+    }
523
+
524
+    /**
525
+     * generates a path for JS/CSS files. If no application is provided it will create the path for core.
526
+     *
527
+     * @param string $application application to get the files from
528
+     * @param string $directory directory within this application (css, js, vendor, etc)
529
+     * @param string $file the file inside of the above folder
530
+     * @return string the path
531
+     */
532
+    private static function generatePath($application, $directory, $file) {
533
+        if (is_null($file)) {
534
+            $file = $application;
535
+            $application = "";
536
+        }
537
+        if (!empty($application)) {
538
+            return "$application/$directory/$file";
539
+        } else {
540
+            return "$directory/$file";
541
+        }
542
+    }
543
+
544
+    /**
545
+     * add a javascript file
546
+     *
547
+     * @param string $application application id
548
+     * @param string|null $file filename
549
+     * @param bool $prepend prepend the Script to the beginning of the list
550
+     * @return void
551
+     */
552
+    public static function addScript($application, $file = null, $prepend = false) {
553
+        $path = OC_Util::generatePath($application, 'js', $file);
554
+
555
+        // core js files need separate handling
556
+        if ($application !== 'core' && $file !== null) {
557
+            self::addTranslations ( $application );
558
+        }
559
+        self::addExternalResource($application, $prepend, $path, "script");
560
+    }
561
+
562
+    /**
563
+     * add a javascript file from the vendor sub folder
564
+     *
565
+     * @param string $application application id
566
+     * @param string|null $file filename
567
+     * @param bool $prepend prepend the Script to the beginning of the list
568
+     * @return void
569
+     */
570
+    public static function addVendorScript($application, $file = null, $prepend = false) {
571
+        $path = OC_Util::generatePath($application, 'vendor', $file);
572
+        self::addExternalResource($application, $prepend, $path, "script");
573
+    }
574
+
575
+    /**
576
+     * add a translation JS file
577
+     *
578
+     * @param string $application application id
579
+     * @param string|null $languageCode language code, defaults to the current language
580
+     * @param bool|null $prepend prepend the Script to the beginning of the list
581
+     */
582
+    public static function addTranslations($application, $languageCode = null, $prepend = false) {
583
+        if (is_null($languageCode)) {
584
+            $languageCode = \OC::$server->getL10NFactory()->findLanguage($application);
585
+        }
586
+        if (!empty($application)) {
587
+            $path = "$application/l10n/$languageCode";
588
+        } else {
589
+            $path = "l10n/$languageCode";
590
+        }
591
+        self::addExternalResource($application, $prepend, $path, "script");
592
+    }
593
+
594
+    /**
595
+     * add a css file
596
+     *
597
+     * @param string $application application id
598
+     * @param string|null $file filename
599
+     * @param bool $prepend prepend the Style to the beginning of the list
600
+     * @return void
601
+     */
602
+    public static function addStyle($application, $file = null, $prepend = false) {
603
+        $path = OC_Util::generatePath($application, 'css', $file);
604
+        self::addExternalResource($application, $prepend, $path, "style");
605
+    }
606
+
607
+    /**
608
+     * add a css file from the vendor sub folder
609
+     *
610
+     * @param string $application application id
611
+     * @param string|null $file filename
612
+     * @param bool $prepend prepend the Style to the beginning of the list
613
+     * @return void
614
+     */
615
+    public static function addVendorStyle($application, $file = null, $prepend = false) {
616
+        $path = OC_Util::generatePath($application, 'vendor', $file);
617
+        self::addExternalResource($application, $prepend, $path, "style");
618
+    }
619
+
620
+    /**
621
+     * add an external resource css/js file
622
+     *
623
+     * @param string $application application id
624
+     * @param bool $prepend prepend the file to the beginning of the list
625
+     * @param string $path
626
+     * @param string $type (script or style)
627
+     * @return void
628
+     */
629
+    private static function addExternalResource($application, $prepend, $path, $type = "script") {
630
+
631
+        if ($type === "style") {
632
+            if (!in_array($path, self::$styles)) {
633
+                if ($prepend === true) {
634
+                    array_unshift ( self::$styles, $path );
635
+                } else {
636
+                    self::$styles[] = $path;
637
+                }
638
+            }
639
+        } elseif ($type === "script") {
640
+            if (!in_array($path, self::$scripts)) {
641
+                if ($prepend === true) {
642
+                    array_unshift ( self::$scripts, $path );
643
+                } else {
644
+                    self::$scripts [] = $path;
645
+                }
646
+            }
647
+        }
648
+    }
649
+
650
+    /**
651
+     * Add a custom element to the header
652
+     * If $text is null then the element will be written as empty element.
653
+     * So use "" to get a closing tag.
654
+     * @param string $tag tag name of the element
655
+     * @param array $attributes array of attributes for the element
656
+     * @param string $text the text content for the element
657
+     */
658
+    public static function addHeader($tag, $attributes, $text=null) {
659
+        self::$headers[] = array(
660
+            'tag' => $tag,
661
+            'attributes' => $attributes,
662
+            'text' => $text
663
+        );
664
+    }
665
+
666
+    /**
667
+     * formats a timestamp in the "right" way
668
+     *
669
+     * @param int $timestamp
670
+     * @param bool $dateOnly option to omit time from the result
671
+     * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
672
+     * @return string timestamp
673
+     *
674
+     * @deprecated Use \OC::$server->query('DateTimeFormatter') instead
675
+     */
676
+    public static function formatDate($timestamp, $dateOnly = false, $timeZone = null) {
677
+        if ($timeZone !== null && !$timeZone instanceof \DateTimeZone) {
678
+            $timeZone = new \DateTimeZone($timeZone);
679
+        }
680
+
681
+        /** @var \OC\DateTimeFormatter $formatter */
682
+        $formatter = \OC::$server->query('DateTimeFormatter');
683
+        if ($dateOnly) {
684
+            return $formatter->formatDate($timestamp, 'long', $timeZone);
685
+        }
686
+        return $formatter->formatDateTime($timestamp, 'long', 'long', $timeZone);
687
+    }
688
+
689
+    /**
690
+     * check if the current server configuration is suitable for ownCloud
691
+     *
692
+     * @param \OC\SystemConfig $config
693
+     * @return array arrays with error messages and hints
694
+     */
695
+    public static function checkServer(\OC\SystemConfig $config) {
696
+        $l = \OC::$server->getL10N('lib');
697
+        $errors = array();
698
+        $CONFIG_DATADIRECTORY = $config->getValue('datadirectory', OC::$SERVERROOT . '/data');
699
+
700
+        if (!self::needUpgrade($config) && $config->getValue('installed', false)) {
701
+            // this check needs to be done every time
702
+            $errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY);
703
+        }
704
+
705
+        // Assume that if checkServer() succeeded before in this session, then all is fine.
706
+        if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) {
707
+            return $errors;
708
+        }
709
+
710
+        $webServerRestart = false;
711
+        $setup = new \OC\Setup($config, \OC::$server->getIniWrapper(), \OC::$server->getL10N('lib'),
712
+            \OC::$server->query(\OCP\Defaults::class), \OC::$server->getLogger(), \OC::$server->getSecureRandom());
713
+
714
+        $urlGenerator = \OC::$server->getURLGenerator();
715
+
716
+        $availableDatabases = $setup->getSupportedDatabases();
717
+        if (empty($availableDatabases)) {
718
+            $errors[] = array(
719
+                'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'),
720
+                'hint' => '' //TODO: sane hint
721
+            );
722
+            $webServerRestart = true;
723
+        }
724
+
725
+        // Check if config folder is writable.
726
+        if(!OC_Helper::isReadOnlyConfigEnabled()) {
727
+            if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) {
728
+                $errors[] = array(
729
+                    'error' => $l->t('Cannot write into "config" directory'),
730
+                    'hint' => $l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s',
731
+                        [$urlGenerator->linkToDocs('admin-dir_permissions')])
732
+                );
733
+            }
734
+        }
735
+
736
+        // Check if there is a writable install folder.
737
+        if ($config->getValue('appstoreenabled', true)) {
738
+            if (OC_App::getInstallPath() === null
739
+                || !is_writable(OC_App::getInstallPath())
740
+                || !is_readable(OC_App::getInstallPath())
741
+            ) {
742
+                $errors[] = array(
743
+                    'error' => $l->t('Cannot write into "apps" directory'),
744
+                    'hint' => $l->t('This can usually be fixed by giving the webserver write access to the apps directory'
745
+                        . ' or disabling the appstore in the config file. See %s',
746
+                        [$urlGenerator->linkToDocs('admin-dir_permissions')])
747
+                );
748
+            }
749
+        }
750
+        // Create root dir.
751
+        if ($config->getValue('installed', false)) {
752
+            if (!is_dir($CONFIG_DATADIRECTORY)) {
753
+                $success = @mkdir($CONFIG_DATADIRECTORY);
754
+                if ($success) {
755
+                    $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
756
+                } else {
757
+                    $errors[] = [
758
+                        'error' => $l->t('Cannot create "data" directory'),
759
+                        'hint' => $l->t('This can usually be fixed by giving the webserver write access to the root directory. See %s',
760
+                            [$urlGenerator->linkToDocs('admin-dir_permissions')])
761
+                    ];
762
+                }
763
+            } else if (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) {
764
+                //common hint for all file permissions error messages
765
+                $permissionsHint = $l->t('Permissions can usually be fixed by giving the webserver write access to the root directory. See %s.',
766
+                    [$urlGenerator->linkToDocs('admin-dir_permissions')]);
767
+                $errors[] = [
768
+                    'error' => 'Your data directory is not writable',
769
+                    'hint' => $permissionsHint
770
+                ];
771
+            } else {
772
+                $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
773
+            }
774
+        }
775
+
776
+        if (!OC_Util::isSetLocaleWorking()) {
777
+            $errors[] = array(
778
+                'error' => $l->t('Setting locale to %s failed',
779
+                    array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/'
780
+                        . 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8')),
781
+                'hint' => $l->t('Please install one of these locales on your system and restart your webserver.')
782
+            );
783
+        }
784
+
785
+        // Contains the dependencies that should be checked against
786
+        // classes = class_exists
787
+        // functions = function_exists
788
+        // defined = defined
789
+        // ini = ini_get
790
+        // If the dependency is not found the missing module name is shown to the EndUser
791
+        // When adding new checks always verify that they pass on Travis as well
792
+        // for ini settings, see https://github.com/owncloud/administration/blob/master/travis-ci/custom.ini
793
+        $dependencies = array(
794
+            'classes' => array(
795
+                'ZipArchive' => 'zip',
796
+                'DOMDocument' => 'dom',
797
+                'XMLWriter' => 'XMLWriter',
798
+                'XMLReader' => 'XMLReader',
799
+            ),
800
+            'functions' => [
801
+                'xml_parser_create' => 'libxml',
802
+                'mb_strcut' => 'mb multibyte',
803
+                'ctype_digit' => 'ctype',
804
+                'json_encode' => 'JSON',
805
+                'gd_info' => 'GD',
806
+                'gzencode' => 'zlib',
807
+                'iconv' => 'iconv',
808
+                'simplexml_load_string' => 'SimpleXML',
809
+                'hash' => 'HASH Message Digest Framework',
810
+                'curl_init' => 'cURL',
811
+                'openssl_verify' => 'OpenSSL',
812
+            ],
813
+            'defined' => array(
814
+                'PDO::ATTR_DRIVER_NAME' => 'PDO'
815
+            ),
816
+            'ini' => [
817
+                'default_charset' => 'UTF-8',
818
+            ],
819
+        );
820
+        $missingDependencies = array();
821
+        $invalidIniSettings = [];
822
+        $moduleHint = $l->t('Please ask your server administrator to install the module.');
823
+
824
+        /**
825
+         * FIXME: The dependency check does not work properly on HHVM on the moment
826
+         *        and prevents installation. Once HHVM is more compatible with our
827
+         *        approach to check for these values we should re-enable those
828
+         *        checks.
829
+         */
830
+        $iniWrapper = \OC::$server->getIniWrapper();
831
+        if (!self::runningOnHhvm()) {
832
+            foreach ($dependencies['classes'] as $class => $module) {
833
+                if (!class_exists($class)) {
834
+                    $missingDependencies[] = $module;
835
+                }
836
+            }
837
+            foreach ($dependencies['functions'] as $function => $module) {
838
+                if (!function_exists($function)) {
839
+                    $missingDependencies[] = $module;
840
+                }
841
+            }
842
+            foreach ($dependencies['defined'] as $defined => $module) {
843
+                if (!defined($defined)) {
844
+                    $missingDependencies[] = $module;
845
+                }
846
+            }
847
+            foreach ($dependencies['ini'] as $setting => $expected) {
848
+                if (is_bool($expected)) {
849
+                    if ($iniWrapper->getBool($setting) !== $expected) {
850
+                        $invalidIniSettings[] = [$setting, $expected];
851
+                    }
852
+                }
853
+                if (is_int($expected)) {
854
+                    if ($iniWrapper->getNumeric($setting) !== $expected) {
855
+                        $invalidIniSettings[] = [$setting, $expected];
856
+                    }
857
+                }
858
+                if (is_string($expected)) {
859
+                    if (strtolower($iniWrapper->getString($setting)) !== strtolower($expected)) {
860
+                        $invalidIniSettings[] = [$setting, $expected];
861
+                    }
862
+                }
863
+            }
864
+        }
865
+
866
+        foreach($missingDependencies as $missingDependency) {
867
+            $errors[] = array(
868
+                'error' => $l->t('PHP module %s not installed.', array($missingDependency)),
869
+                'hint' => $moduleHint
870
+            );
871
+            $webServerRestart = true;
872
+        }
873
+        foreach($invalidIniSettings as $setting) {
874
+            if(is_bool($setting[1])) {
875
+                $setting[1] = ($setting[1]) ? 'on' : 'off';
876
+            }
877
+            $errors[] = [
878
+                'error' => $l->t('PHP setting "%s" is not set to "%s".', [$setting[0], var_export($setting[1], true)]),
879
+                'hint' =>  $l->t('Adjusting this setting in php.ini will make Nextcloud run again')
880
+            ];
881
+            $webServerRestart = true;
882
+        }
883
+
884
+        /**
885
+         * The mbstring.func_overload check can only be performed if the mbstring
886
+         * module is installed as it will return null if the checking setting is
887
+         * not available and thus a check on the boolean value fails.
888
+         *
889
+         * TODO: Should probably be implemented in the above generic dependency
890
+         *       check somehow in the long-term.
891
+         */
892
+        if($iniWrapper->getBool('mbstring.func_overload') !== null &&
893
+            $iniWrapper->getBool('mbstring.func_overload') === true) {
894
+            $errors[] = array(
895
+                'error' => $l->t('mbstring.func_overload is set to "%s" instead of the expected value "0"', [$iniWrapper->getString('mbstring.func_overload')]),
896
+                'hint' => $l->t('To fix this issue set <code>mbstring.func_overload</code> to <code>0</code> in your php.ini')
897
+            );
898
+        }
899
+
900
+        if(function_exists('xml_parser_create') &&
901
+            LIBXML_LOADED_VERSION < 20700 ) {
902
+            $version = LIBXML_LOADED_VERSION;
903
+            $major = floor($version/10000);
904
+            $version -= ($major * 10000);
905
+            $minor = floor($version/100);
906
+            $version -= ($minor * 100);
907
+            $patch = $version;
908
+            $errors[] = array(
909
+                'error' => $l->t('libxml2 2.7.0 is at least required. Currently %s is installed.', [$major . '.' . $minor . '.' . $patch]),
910
+                'hint' => $l->t('To fix this issue update your libxml2 version and restart your web server.')
911
+            );
912
+        }
913
+
914
+        if (!self::isAnnotationsWorking()) {
915
+            $errors[] = array(
916
+                'error' => $l->t('PHP is apparently set up to strip inline doc blocks. This will make several core apps inaccessible.'),
917
+                'hint' => $l->t('This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.')
918
+            );
919
+        }
920
+
921
+        if (!\OC::$CLI && $webServerRestart) {
922
+            $errors[] = array(
923
+                'error' => $l->t('PHP modules have been installed, but they are still listed as missing?'),
924
+                'hint' => $l->t('Please ask your server administrator to restart the web server.')
925
+            );
926
+        }
927
+
928
+        $errors = array_merge($errors, self::checkDatabaseVersion());
929
+
930
+        // Cache the result of this function
931
+        \OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0);
932
+
933
+        return $errors;
934
+    }
935
+
936
+    /**
937
+     * Check the database version
938
+     *
939
+     * @return array errors array
940
+     */
941
+    public static function checkDatabaseVersion() {
942
+        $l = \OC::$server->getL10N('lib');
943
+        $errors = array();
944
+        $dbType = \OC::$server->getSystemConfig()->getValue('dbtype', 'sqlite');
945
+        if ($dbType === 'pgsql') {
946
+            // check PostgreSQL version
947
+            try {
948
+                $result = \OC_DB::executeAudited('SHOW SERVER_VERSION');
949
+                $data = $result->fetchRow();
950
+                if (isset($data['server_version'])) {
951
+                    $version = $data['server_version'];
952
+                    if (version_compare($version, '9.0.0', '<')) {
953
+                        $errors[] = array(
954
+                            'error' => $l->t('PostgreSQL >= 9 required'),
955
+                            'hint' => $l->t('Please upgrade your database version')
956
+                        );
957
+                    }
958
+                }
959
+            } catch (\Doctrine\DBAL\DBALException $e) {
960
+                $logger = \OC::$server->getLogger();
961
+                $logger->warning('Error occurred while checking PostgreSQL version, assuming >= 9');
962
+                $logger->logException($e);
963
+            }
964
+        }
965
+        return $errors;
966
+    }
967
+
968
+    /**
969
+     * Check for correct file permissions of data directory
970
+     *
971
+     * @param string $dataDirectory
972
+     * @return array arrays with error messages and hints
973
+     */
974
+    public static function checkDataDirectoryPermissions($dataDirectory) {
975
+        $l = \OC::$server->getL10N('lib');
976
+        $errors = array();
977
+        $permissionsModHint = $l->t('Please change the permissions to 0770 so that the directory'
978
+            . ' cannot be listed by other users.');
979
+        $perms = substr(decoct(@fileperms($dataDirectory)), -3);
980
+        if (substr($perms, -1) !== '0') {
981
+            chmod($dataDirectory, 0770);
982
+            clearstatcache();
983
+            $perms = substr(decoct(@fileperms($dataDirectory)), -3);
984
+            if ($perms[2] !== '0') {
985
+                $errors[] = [
986
+                    'error' => $l->t('Your data directory is readable by other users'),
987
+                    'hint' => $permissionsModHint
988
+                ];
989
+            }
990
+        }
991
+        return $errors;
992
+    }
993
+
994
+    /**
995
+     * Check that the data directory exists and is valid by
996
+     * checking the existence of the ".ocdata" file.
997
+     *
998
+     * @param string $dataDirectory data directory path
999
+     * @return array errors found
1000
+     */
1001
+    public static function checkDataDirectoryValidity($dataDirectory) {
1002
+        $l = \OC::$server->getL10N('lib');
1003
+        $errors = [];
1004
+        if ($dataDirectory[0] !== '/') {
1005
+            $errors[] = [
1006
+                'error' => $l->t('Your data directory must be an absolute path'),
1007
+                'hint' => $l->t('Check the value of "datadirectory" in your configuration')
1008
+            ];
1009
+        }
1010
+        if (!file_exists($dataDirectory . '/.ocdata')) {
1011
+            $errors[] = [
1012
+                'error' => $l->t('Your data directory is invalid'),
1013
+                'hint' => $l->t('Ensure there is a file called ".ocdata"' .
1014
+                    ' in the root of the data directory.')
1015
+            ];
1016
+        }
1017
+        return $errors;
1018
+    }
1019
+
1020
+    /**
1021
+     * Check if the user is logged in, redirects to home if not. With
1022
+     * redirect URL parameter to the request URI.
1023
+     *
1024
+     * @return void
1025
+     */
1026
+    public static function checkLoggedIn() {
1027
+        // Check if we are a user
1028
+        if (!\OC::$server->getUserSession()->isLoggedIn()) {
1029
+            header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute(
1030
+                        'core.login.showLoginForm',
1031
+                        [
1032
+                            'redirect_url' => \OC::$server->getRequest()->getRequestUri(),
1033
+                        ]
1034
+                    )
1035
+            );
1036
+            exit();
1037
+        }
1038
+        // Redirect to 2FA challenge selection if 2FA challenge was not solved yet
1039
+        if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
1040
+            header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
1041
+            exit();
1042
+        }
1043
+    }
1044
+
1045
+    /**
1046
+     * Check if the user is a admin, redirects to home if not
1047
+     *
1048
+     * @return void
1049
+     */
1050
+    public static function checkAdminUser() {
1051
+        OC_Util::checkLoggedIn();
1052
+        if (!OC_User::isAdminUser(OC_User::getUser())) {
1053
+            header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php'));
1054
+            exit();
1055
+        }
1056
+    }
1057
+
1058
+    /**
1059
+     * Check if the user is a subadmin, redirects to home if not
1060
+     *
1061
+     * @return null|boolean $groups where the current user is subadmin
1062
+     */
1063
+    public static function checkSubAdminUser() {
1064
+        OC_Util::checkLoggedIn();
1065
+        $userObject = \OC::$server->getUserSession()->getUser();
1066
+        $isSubAdmin = false;
1067
+        if($userObject !== null) {
1068
+            $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
1069
+        }
1070
+
1071
+        if (!$isSubAdmin) {
1072
+            header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php'));
1073
+            exit();
1074
+        }
1075
+        return true;
1076
+    }
1077
+
1078
+    /**
1079
+     * Returns the URL of the default page
1080
+     * based on the system configuration and
1081
+     * the apps visible for the current user
1082
+     *
1083
+     * @return string URL
1084
+     * @suppress PhanDeprecatedFunction
1085
+     */
1086
+    public static function getDefaultPageUrl() {
1087
+        $urlGenerator = \OC::$server->getURLGenerator();
1088
+        // Deny the redirect if the URL contains a @
1089
+        // This prevents unvalidated redirects like ?redirect_url=:[email protected]
1090
+        if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) {
1091
+            $location = $urlGenerator->getAbsoluteURL(urldecode($_REQUEST['redirect_url']));
1092
+        } else {
1093
+            $defaultPage = \OC::$server->getAppConfig()->getValue('core', 'defaultpage');
1094
+            if ($defaultPage) {
1095
+                $location = $urlGenerator->getAbsoluteURL($defaultPage);
1096
+            } else {
1097
+                $appId = 'files';
1098
+                $config = \OC::$server->getConfig();
1099
+                $defaultApps = explode(',', $config->getSystemValue('defaultapp', 'files'));
1100
+                // find the first app that is enabled for the current user
1101
+                foreach ($defaultApps as $defaultApp) {
1102
+                    $defaultApp = OC_App::cleanAppId(strip_tags($defaultApp));
1103
+                    if (static::getAppManager()->isEnabledForUser($defaultApp)) {
1104
+                        $appId = $defaultApp;
1105
+                        break;
1106
+                    }
1107
+                }
1108
+
1109
+                if($config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true') {
1110
+                    $location = $urlGenerator->getAbsoluteURL('/apps/' . $appId . '/');
1111
+                } else {
1112
+                    $location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $appId . '/');
1113
+                }
1114
+            }
1115
+        }
1116
+        return $location;
1117
+    }
1118
+
1119
+    /**
1120
+     * Redirect to the user default page
1121
+     *
1122
+     * @return void
1123
+     */
1124
+    public static function redirectToDefaultPage() {
1125
+        $location = self::getDefaultPageUrl();
1126
+        header('Location: ' . $location);
1127
+        exit();
1128
+    }
1129
+
1130
+    /**
1131
+     * get an id unique for this instance
1132
+     *
1133
+     * @return string
1134
+     */
1135
+    public static function getInstanceId() {
1136
+        $id = \OC::$server->getSystemConfig()->getValue('instanceid', null);
1137
+        if (is_null($id)) {
1138
+            // We need to guarantee at least one letter in instanceid so it can be used as the session_name
1139
+            $id = 'oc' . \OC::$server->getSecureRandom()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
1140
+            \OC::$server->getSystemConfig()->setValue('instanceid', $id);
1141
+        }
1142
+        return $id;
1143
+    }
1144
+
1145
+    /**
1146
+     * Public function to sanitize HTML
1147
+     *
1148
+     * This function is used to sanitize HTML and should be applied on any
1149
+     * string or array of strings before displaying it on a web page.
1150
+     *
1151
+     * @param string|array $value
1152
+     * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter.
1153
+     */
1154
+    public static function sanitizeHTML($value) {
1155
+        if (is_array($value)) {
1156
+            $value = array_map(function($value) {
1157
+                return self::sanitizeHTML($value);
1158
+            }, $value);
1159
+        } else {
1160
+            // Specify encoding for PHP<5.4
1161
+            $value = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
1162
+        }
1163
+        return $value;
1164
+    }
1165
+
1166
+    /**
1167
+     * Public function to encode url parameters
1168
+     *
1169
+     * This function is used to encode path to file before output.
1170
+     * Encoding is done according to RFC 3986 with one exception:
1171
+     * Character '/' is preserved as is.
1172
+     *
1173
+     * @param string $component part of URI to encode
1174
+     * @return string
1175
+     */
1176
+    public static function encodePath($component) {
1177
+        $encoded = rawurlencode($component);
1178
+        $encoded = str_replace('%2F', '/', $encoded);
1179
+        return $encoded;
1180
+    }
1181
+
1182
+
1183
+    public function createHtaccessTestFile(\OCP\IConfig $config) {
1184
+        // php dev server does not support htaccess
1185
+        if (php_sapi_name() === 'cli-server') {
1186
+            return false;
1187
+        }
1188
+
1189
+        // testdata
1190
+        $fileName = '/htaccesstest.txt';
1191
+        $testContent = 'This is used for testing whether htaccess is properly enabled to disallow access from the outside. This file can be safely removed.';
1192
+
1193
+        // creating a test file
1194
+        $testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName;
1195
+
1196
+        if (file_exists($testFile)) {// already running this test, possible recursive call
1197
+            return false;
1198
+        }
1199
+
1200
+        $fp = @fopen($testFile, 'w');
1201
+        if (!$fp) {
1202
+            throw new OC\HintException('Can\'t create test file to check for working .htaccess file.',
1203
+                'Make sure it is possible for the webserver to write to ' . $testFile);
1204
+        }
1205
+        fwrite($fp, $testContent);
1206
+        fclose($fp);
1207
+
1208
+        return $testContent;
1209
+    }
1210
+
1211
+    /**
1212
+     * Check if the .htaccess file is working
1213
+     * @param \OCP\IConfig $config
1214
+     * @return bool
1215
+     * @throws Exception
1216
+     * @throws \OC\HintException If the test file can't get written.
1217
+     */
1218
+    public function isHtaccessWorking(\OCP\IConfig $config) {
1219
+
1220
+        if (\OC::$CLI || !$config->getSystemValue('check_for_working_htaccess', true)) {
1221
+            return true;
1222
+        }
1223
+
1224
+        $testContent = $this->createHtaccessTestFile($config);
1225
+        if ($testContent === false) {
1226
+            return false;
1227
+        }
1228
+
1229
+        $fileName = '/htaccesstest.txt';
1230
+        $testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName;
1231
+
1232
+        // accessing the file via http
1233
+        $url = \OC::$server->getURLGenerator()->getAbsoluteURL(OC::$WEBROOT . '/data' . $fileName);
1234
+        try {
1235
+            $content = \OC::$server->getHTTPClientService()->newClient()->get($url)->getBody();
1236
+        } catch (\Exception $e) {
1237
+            $content = false;
1238
+        }
1239
+
1240
+        // cleanup
1241
+        @unlink($testFile);
1242
+
1243
+        /*
1244 1244
 		 * If the content is not equal to test content our .htaccess
1245 1245
 		 * is working as required
1246 1246
 		 */
1247
-		return $content !== $testContent;
1248
-	}
1249
-
1250
-	/**
1251
-	 * Check if the setlocal call does not work. This can happen if the right
1252
-	 * local packages are not available on the server.
1253
-	 *
1254
-	 * @return bool
1255
-	 */
1256
-	public static function isSetLocaleWorking() {
1257
-		\Patchwork\Utf8\Bootup::initLocale();
1258
-		if ('' === basename('§')) {
1259
-			return false;
1260
-		}
1261
-		return true;
1262
-	}
1263
-
1264
-	/**
1265
-	 * Check if it's possible to get the inline annotations
1266
-	 *
1267
-	 * @return bool
1268
-	 */
1269
-	public static function isAnnotationsWorking() {
1270
-		$reflection = new \ReflectionMethod(__METHOD__);
1271
-		$docs = $reflection->getDocComment();
1272
-
1273
-		return (is_string($docs) && strlen($docs) > 50);
1274
-	}
1275
-
1276
-	/**
1277
-	 * Check if the PHP module fileinfo is loaded.
1278
-	 *
1279
-	 * @return bool
1280
-	 */
1281
-	public static function fileInfoLoaded() {
1282
-		return function_exists('finfo_open');
1283
-	}
1284
-
1285
-	/**
1286
-	 * clear all levels of output buffering
1287
-	 *
1288
-	 * @return void
1289
-	 */
1290
-	public static function obEnd() {
1291
-		while (ob_get_level()) {
1292
-			ob_end_clean();
1293
-		}
1294
-	}
1295
-
1296
-	/**
1297
-	 * Checks whether the server is running on Mac OS X
1298
-	 *
1299
-	 * @return bool true if running on Mac OS X, false otherwise
1300
-	 */
1301
-	public static function runningOnMac() {
1302
-		return (strtoupper(substr(PHP_OS, 0, 6)) === 'DARWIN');
1303
-	}
1304
-
1305
-	/**
1306
-	 * Checks whether server is running on HHVM
1307
-	 *
1308
-	 * @return bool True if running on HHVM, false otherwise
1309
-	 */
1310
-	public static function runningOnHhvm() {
1311
-		return defined('HHVM_VERSION');
1312
-	}
1313
-
1314
-	/**
1315
-	 * Handles the case that there may not be a theme, then check if a "default"
1316
-	 * theme exists and take that one
1317
-	 *
1318
-	 * @return string the theme
1319
-	 */
1320
-	public static function getTheme() {
1321
-		$theme = \OC::$server->getSystemConfig()->getValue("theme", '');
1322
-
1323
-		if ($theme === '') {
1324
-			if (is_dir(OC::$SERVERROOT . '/themes/default')) {
1325
-				$theme = 'default';
1326
-			}
1327
-		}
1328
-
1329
-		return $theme;
1330
-	}
1331
-
1332
-	/**
1333
-	 * Clear a single file from the opcode cache
1334
-	 * This is useful for writing to the config file
1335
-	 * in case the opcode cache does not re-validate files
1336
-	 * Returns true if successful, false if unsuccessful:
1337
-	 * caller should fall back on clearing the entire cache
1338
-	 * with clearOpcodeCache() if unsuccessful
1339
-	 *
1340
-	 * @param string $path the path of the file to clear from the cache
1341
-	 * @return bool true if underlying function returns true, otherwise false
1342
-	 */
1343
-	public static function deleteFromOpcodeCache($path) {
1344
-		$ret = false;
1345
-		if ($path) {
1346
-			// APC >= 3.1.1
1347
-			if (function_exists('apc_delete_file')) {
1348
-				$ret = @apc_delete_file($path);
1349
-			}
1350
-			// Zend OpCache >= 7.0.0, PHP >= 5.5.0
1351
-			if (function_exists('opcache_invalidate')) {
1352
-				$ret = opcache_invalidate($path);
1353
-			}
1354
-		}
1355
-		return $ret;
1356
-	}
1357
-
1358
-	/**
1359
-	 * Clear the opcode cache if one exists
1360
-	 * This is necessary for writing to the config file
1361
-	 * in case the opcode cache does not re-validate files
1362
-	 *
1363
-	 * @return void
1364
-	 * @suppress PhanDeprecatedFunction
1365
-	 * @suppress PhanUndeclaredConstant
1366
-	 */
1367
-	public static function clearOpcodeCache() {
1368
-		// APC
1369
-		if (function_exists('apc_clear_cache')) {
1370
-			apc_clear_cache();
1371
-		}
1372
-		// Zend Opcache
1373
-		if (function_exists('accelerator_reset')) {
1374
-			accelerator_reset();
1375
-		}
1376
-		// XCache
1377
-		if (function_exists('xcache_clear_cache')) {
1378
-			if (\OC::$server->getIniWrapper()->getBool('xcache.admin.enable_auth')) {
1379
-				\OCP\Util::writeLog('core', 'XCache opcode cache will not be cleared because "xcache.admin.enable_auth" is enabled.', \OCP\Util::WARN);
1380
-			} else {
1381
-				@xcache_clear_cache(XC_TYPE_PHP, 0);
1382
-			}
1383
-		}
1384
-		// Opcache (PHP >= 5.5)
1385
-		if (function_exists('opcache_reset')) {
1386
-			opcache_reset();
1387
-		}
1388
-	}
1389
-
1390
-	/**
1391
-	 * Normalize a unicode string
1392
-	 *
1393
-	 * @param string $value a not normalized string
1394
-	 * @return bool|string
1395
-	 */
1396
-	public static function normalizeUnicode($value) {
1397
-		if(Normalizer::isNormalized($value)) {
1398
-			return $value;
1399
-		}
1400
-
1401
-		$normalizedValue = Normalizer::normalize($value);
1402
-		if ($normalizedValue === null || $normalizedValue === false) {
1403
-			\OC::$server->getLogger()->warning('normalizing failed for "' . $value . '"', ['app' => 'core']);
1404
-			return $value;
1405
-		}
1406
-
1407
-		return $normalizedValue;
1408
-	}
1409
-
1410
-	/**
1411
-	 * A human readable string is generated based on version and build number
1412
-	 *
1413
-	 * @return string
1414
-	 */
1415
-	public static function getHumanVersion() {
1416
-		$version = OC_Util::getVersionString();
1417
-		$build = OC_Util::getBuild();
1418
-		if (!empty($build) and OC_Util::getChannel() === 'daily') {
1419
-			$version .= ' Build:' . $build;
1420
-		}
1421
-		return $version;
1422
-	}
1423
-
1424
-	/**
1425
-	 * Returns whether the given file name is valid
1426
-	 *
1427
-	 * @param string $file file name to check
1428
-	 * @return bool true if the file name is valid, false otherwise
1429
-	 * @deprecated use \OC\Files\View::verifyPath()
1430
-	 */
1431
-	public static function isValidFileName($file) {
1432
-		$trimmed = trim($file);
1433
-		if ($trimmed === '') {
1434
-			return false;
1435
-		}
1436
-		if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) {
1437
-			return false;
1438
-		}
1439
-
1440
-		// detect part files
1441
-		if (preg_match('/' . \OCP\Files\FileInfo::BLACKLIST_FILES_REGEX . '/', $trimmed) !== 0) {
1442
-			return false;
1443
-		}
1444
-
1445
-		foreach (str_split($trimmed) as $char) {
1446
-			if (strpos(\OCP\Constants::FILENAME_INVALID_CHARS, $char) !== false) {
1447
-				return false;
1448
-			}
1449
-		}
1450
-		return true;
1451
-	}
1452
-
1453
-	/**
1454
-	 * Check whether the instance needs to perform an upgrade,
1455
-	 * either when the core version is higher or any app requires
1456
-	 * an upgrade.
1457
-	 *
1458
-	 * @param \OC\SystemConfig $config
1459
-	 * @return bool whether the core or any app needs an upgrade
1460
-	 * @throws \OC\HintException When the upgrade from the given version is not allowed
1461
-	 */
1462
-	public static function needUpgrade(\OC\SystemConfig $config) {
1463
-		if ($config->getValue('installed', false)) {
1464
-			$installedVersion = $config->getValue('version', '0.0.0');
1465
-			$currentVersion = implode('.', \OCP\Util::getVersion());
1466
-			$versionDiff = version_compare($currentVersion, $installedVersion);
1467
-			if ($versionDiff > 0) {
1468
-				return true;
1469
-			} else if ($config->getValue('debug', false) && $versionDiff < 0) {
1470
-				// downgrade with debug
1471
-				$installedMajor = explode('.', $installedVersion);
1472
-				$installedMajor = $installedMajor[0] . '.' . $installedMajor[1];
1473
-				$currentMajor = explode('.', $currentVersion);
1474
-				$currentMajor = $currentMajor[0] . '.' . $currentMajor[1];
1475
-				if ($installedMajor === $currentMajor) {
1476
-					// Same major, allow downgrade for developers
1477
-					return true;
1478
-				} else {
1479
-					// downgrade attempt, throw exception
1480
-					throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')');
1481
-				}
1482
-			} else if ($versionDiff < 0) {
1483
-				// downgrade attempt, throw exception
1484
-				throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')');
1485
-			}
1486
-
1487
-			// also check for upgrades for apps (independently from the user)
1488
-			$apps = \OC_App::getEnabledApps(false, true);
1489
-			$shouldUpgrade = false;
1490
-			foreach ($apps as $app) {
1491
-				if (\OC_App::shouldUpgrade($app)) {
1492
-					$shouldUpgrade = true;
1493
-					break;
1494
-				}
1495
-			}
1496
-			return $shouldUpgrade;
1497
-		} else {
1498
-			return false;
1499
-		}
1500
-	}
1247
+        return $content !== $testContent;
1248
+    }
1249
+
1250
+    /**
1251
+     * Check if the setlocal call does not work. This can happen if the right
1252
+     * local packages are not available on the server.
1253
+     *
1254
+     * @return bool
1255
+     */
1256
+    public static function isSetLocaleWorking() {
1257
+        \Patchwork\Utf8\Bootup::initLocale();
1258
+        if ('' === basename('§')) {
1259
+            return false;
1260
+        }
1261
+        return true;
1262
+    }
1263
+
1264
+    /**
1265
+     * Check if it's possible to get the inline annotations
1266
+     *
1267
+     * @return bool
1268
+     */
1269
+    public static function isAnnotationsWorking() {
1270
+        $reflection = new \ReflectionMethod(__METHOD__);
1271
+        $docs = $reflection->getDocComment();
1272
+
1273
+        return (is_string($docs) && strlen($docs) > 50);
1274
+    }
1275
+
1276
+    /**
1277
+     * Check if the PHP module fileinfo is loaded.
1278
+     *
1279
+     * @return bool
1280
+     */
1281
+    public static function fileInfoLoaded() {
1282
+        return function_exists('finfo_open');
1283
+    }
1284
+
1285
+    /**
1286
+     * clear all levels of output buffering
1287
+     *
1288
+     * @return void
1289
+     */
1290
+    public static function obEnd() {
1291
+        while (ob_get_level()) {
1292
+            ob_end_clean();
1293
+        }
1294
+    }
1295
+
1296
+    /**
1297
+     * Checks whether the server is running on Mac OS X
1298
+     *
1299
+     * @return bool true if running on Mac OS X, false otherwise
1300
+     */
1301
+    public static function runningOnMac() {
1302
+        return (strtoupper(substr(PHP_OS, 0, 6)) === 'DARWIN');
1303
+    }
1304
+
1305
+    /**
1306
+     * Checks whether server is running on HHVM
1307
+     *
1308
+     * @return bool True if running on HHVM, false otherwise
1309
+     */
1310
+    public static function runningOnHhvm() {
1311
+        return defined('HHVM_VERSION');
1312
+    }
1313
+
1314
+    /**
1315
+     * Handles the case that there may not be a theme, then check if a "default"
1316
+     * theme exists and take that one
1317
+     *
1318
+     * @return string the theme
1319
+     */
1320
+    public static function getTheme() {
1321
+        $theme = \OC::$server->getSystemConfig()->getValue("theme", '');
1322
+
1323
+        if ($theme === '') {
1324
+            if (is_dir(OC::$SERVERROOT . '/themes/default')) {
1325
+                $theme = 'default';
1326
+            }
1327
+        }
1328
+
1329
+        return $theme;
1330
+    }
1331
+
1332
+    /**
1333
+     * Clear a single file from the opcode cache
1334
+     * This is useful for writing to the config file
1335
+     * in case the opcode cache does not re-validate files
1336
+     * Returns true if successful, false if unsuccessful:
1337
+     * caller should fall back on clearing the entire cache
1338
+     * with clearOpcodeCache() if unsuccessful
1339
+     *
1340
+     * @param string $path the path of the file to clear from the cache
1341
+     * @return bool true if underlying function returns true, otherwise false
1342
+     */
1343
+    public static function deleteFromOpcodeCache($path) {
1344
+        $ret = false;
1345
+        if ($path) {
1346
+            // APC >= 3.1.1
1347
+            if (function_exists('apc_delete_file')) {
1348
+                $ret = @apc_delete_file($path);
1349
+            }
1350
+            // Zend OpCache >= 7.0.0, PHP >= 5.5.0
1351
+            if (function_exists('opcache_invalidate')) {
1352
+                $ret = opcache_invalidate($path);
1353
+            }
1354
+        }
1355
+        return $ret;
1356
+    }
1357
+
1358
+    /**
1359
+     * Clear the opcode cache if one exists
1360
+     * This is necessary for writing to the config file
1361
+     * in case the opcode cache does not re-validate files
1362
+     *
1363
+     * @return void
1364
+     * @suppress PhanDeprecatedFunction
1365
+     * @suppress PhanUndeclaredConstant
1366
+     */
1367
+    public static function clearOpcodeCache() {
1368
+        // APC
1369
+        if (function_exists('apc_clear_cache')) {
1370
+            apc_clear_cache();
1371
+        }
1372
+        // Zend Opcache
1373
+        if (function_exists('accelerator_reset')) {
1374
+            accelerator_reset();
1375
+        }
1376
+        // XCache
1377
+        if (function_exists('xcache_clear_cache')) {
1378
+            if (\OC::$server->getIniWrapper()->getBool('xcache.admin.enable_auth')) {
1379
+                \OCP\Util::writeLog('core', 'XCache opcode cache will not be cleared because "xcache.admin.enable_auth" is enabled.', \OCP\Util::WARN);
1380
+            } else {
1381
+                @xcache_clear_cache(XC_TYPE_PHP, 0);
1382
+            }
1383
+        }
1384
+        // Opcache (PHP >= 5.5)
1385
+        if (function_exists('opcache_reset')) {
1386
+            opcache_reset();
1387
+        }
1388
+    }
1389
+
1390
+    /**
1391
+     * Normalize a unicode string
1392
+     *
1393
+     * @param string $value a not normalized string
1394
+     * @return bool|string
1395
+     */
1396
+    public static function normalizeUnicode($value) {
1397
+        if(Normalizer::isNormalized($value)) {
1398
+            return $value;
1399
+        }
1400
+
1401
+        $normalizedValue = Normalizer::normalize($value);
1402
+        if ($normalizedValue === null || $normalizedValue === false) {
1403
+            \OC::$server->getLogger()->warning('normalizing failed for "' . $value . '"', ['app' => 'core']);
1404
+            return $value;
1405
+        }
1406
+
1407
+        return $normalizedValue;
1408
+    }
1409
+
1410
+    /**
1411
+     * A human readable string is generated based on version and build number
1412
+     *
1413
+     * @return string
1414
+     */
1415
+    public static function getHumanVersion() {
1416
+        $version = OC_Util::getVersionString();
1417
+        $build = OC_Util::getBuild();
1418
+        if (!empty($build) and OC_Util::getChannel() === 'daily') {
1419
+            $version .= ' Build:' . $build;
1420
+        }
1421
+        return $version;
1422
+    }
1423
+
1424
+    /**
1425
+     * Returns whether the given file name is valid
1426
+     *
1427
+     * @param string $file file name to check
1428
+     * @return bool true if the file name is valid, false otherwise
1429
+     * @deprecated use \OC\Files\View::verifyPath()
1430
+     */
1431
+    public static function isValidFileName($file) {
1432
+        $trimmed = trim($file);
1433
+        if ($trimmed === '') {
1434
+            return false;
1435
+        }
1436
+        if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) {
1437
+            return false;
1438
+        }
1439
+
1440
+        // detect part files
1441
+        if (preg_match('/' . \OCP\Files\FileInfo::BLACKLIST_FILES_REGEX . '/', $trimmed) !== 0) {
1442
+            return false;
1443
+        }
1444
+
1445
+        foreach (str_split($trimmed) as $char) {
1446
+            if (strpos(\OCP\Constants::FILENAME_INVALID_CHARS, $char) !== false) {
1447
+                return false;
1448
+            }
1449
+        }
1450
+        return true;
1451
+    }
1452
+
1453
+    /**
1454
+     * Check whether the instance needs to perform an upgrade,
1455
+     * either when the core version is higher or any app requires
1456
+     * an upgrade.
1457
+     *
1458
+     * @param \OC\SystemConfig $config
1459
+     * @return bool whether the core or any app needs an upgrade
1460
+     * @throws \OC\HintException When the upgrade from the given version is not allowed
1461
+     */
1462
+    public static function needUpgrade(\OC\SystemConfig $config) {
1463
+        if ($config->getValue('installed', false)) {
1464
+            $installedVersion = $config->getValue('version', '0.0.0');
1465
+            $currentVersion = implode('.', \OCP\Util::getVersion());
1466
+            $versionDiff = version_compare($currentVersion, $installedVersion);
1467
+            if ($versionDiff > 0) {
1468
+                return true;
1469
+            } else if ($config->getValue('debug', false) && $versionDiff < 0) {
1470
+                // downgrade with debug
1471
+                $installedMajor = explode('.', $installedVersion);
1472
+                $installedMajor = $installedMajor[0] . '.' . $installedMajor[1];
1473
+                $currentMajor = explode('.', $currentVersion);
1474
+                $currentMajor = $currentMajor[0] . '.' . $currentMajor[1];
1475
+                if ($installedMajor === $currentMajor) {
1476
+                    // Same major, allow downgrade for developers
1477
+                    return true;
1478
+                } else {
1479
+                    // downgrade attempt, throw exception
1480
+                    throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')');
1481
+                }
1482
+            } else if ($versionDiff < 0) {
1483
+                // downgrade attempt, throw exception
1484
+                throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')');
1485
+            }
1486
+
1487
+            // also check for upgrades for apps (independently from the user)
1488
+            $apps = \OC_App::getEnabledApps(false, true);
1489
+            $shouldUpgrade = false;
1490
+            foreach ($apps as $app) {
1491
+                if (\OC_App::shouldUpgrade($app)) {
1492
+                    $shouldUpgrade = true;
1493
+                    break;
1494
+                }
1495
+            }
1496
+            return $shouldUpgrade;
1497
+        } else {
1498
+            return false;
1499
+        }
1500
+    }
1501 1501
 
1502 1502
 }
Please login to merge, or discard this patch.
lib/private/Files/Cache/Cache.php 1 patch
Indentation   +829 added lines, -829 removed lines patch added patch discarded remove patch
@@ -56,843 +56,843 @@
 block discarded – undo
56 56
  * - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater
57 57
  */
58 58
 class Cache implements ICache {
59
-	use MoveFromCacheTrait {
60
-		MoveFromCacheTrait::moveFromCache as moveFromCacheFallback;
61
-	}
62
-
63
-	/**
64
-	 * @var array partial data for the cache
65
-	 */
66
-	protected $partial = array();
67
-
68
-	/**
69
-	 * @var string
70
-	 */
71
-	protected $storageId;
72
-
73
-	/**
74
-	 * @var Storage $storageCache
75
-	 */
76
-	protected $storageCache;
77
-
78
-	/** @var IMimeTypeLoader */
79
-	protected $mimetypeLoader;
80
-
81
-	/**
82
-	 * @var IDBConnection
83
-	 */
84
-	protected $connection;
85
-
86
-	/** @var QuerySearchHelper */
87
-	protected $querySearchHelper;
88
-
89
-	/**
90
-	 * @param \OC\Files\Storage\Storage|string $storage
91
-	 */
92
-	public function __construct($storage) {
93
-		if ($storage instanceof \OC\Files\Storage\Storage) {
94
-			$this->storageId = $storage->getId();
95
-		} else {
96
-			$this->storageId = $storage;
97
-		}
98
-		if (strlen($this->storageId) > 64) {
99
-			$this->storageId = md5($this->storageId);
100
-		}
101
-
102
-		$this->storageCache = new Storage($storage);
103
-		$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
104
-		$this->connection = \OC::$server->getDatabaseConnection();
105
-		$this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader);
106
-	}
107
-
108
-	/**
109
-	 * Get the numeric storage id for this cache's storage
110
-	 *
111
-	 * @return int
112
-	 */
113
-	public function getNumericStorageId() {
114
-		return $this->storageCache->getNumericId();
115
-	}
116
-
117
-	/**
118
-	 * get the stored metadata of a file or folder
119
-	 *
120
-	 * @param string | int $file either the path of a file or folder or the file id for a file or folder
121
-	 * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache
122
-	 */
123
-	public function get($file) {
124
-		if (is_string($file) or $file == '') {
125
-			// normalize file
126
-			$file = $this->normalize($file);
127
-
128
-			$where = 'WHERE `storage` = ? AND `path_hash` = ?';
129
-			$params = array($this->getNumericStorageId(), md5($file));
130
-		} else { //file id
131
-			$where = 'WHERE `fileid` = ?';
132
-			$params = array($file);
133
-		}
134
-		$sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
59
+    use MoveFromCacheTrait {
60
+        MoveFromCacheTrait::moveFromCache as moveFromCacheFallback;
61
+    }
62
+
63
+    /**
64
+     * @var array partial data for the cache
65
+     */
66
+    protected $partial = array();
67
+
68
+    /**
69
+     * @var string
70
+     */
71
+    protected $storageId;
72
+
73
+    /**
74
+     * @var Storage $storageCache
75
+     */
76
+    protected $storageCache;
77
+
78
+    /** @var IMimeTypeLoader */
79
+    protected $mimetypeLoader;
80
+
81
+    /**
82
+     * @var IDBConnection
83
+     */
84
+    protected $connection;
85
+
86
+    /** @var QuerySearchHelper */
87
+    protected $querySearchHelper;
88
+
89
+    /**
90
+     * @param \OC\Files\Storage\Storage|string $storage
91
+     */
92
+    public function __construct($storage) {
93
+        if ($storage instanceof \OC\Files\Storage\Storage) {
94
+            $this->storageId = $storage->getId();
95
+        } else {
96
+            $this->storageId = $storage;
97
+        }
98
+        if (strlen($this->storageId) > 64) {
99
+            $this->storageId = md5($this->storageId);
100
+        }
101
+
102
+        $this->storageCache = new Storage($storage);
103
+        $this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
104
+        $this->connection = \OC::$server->getDatabaseConnection();
105
+        $this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader);
106
+    }
107
+
108
+    /**
109
+     * Get the numeric storage id for this cache's storage
110
+     *
111
+     * @return int
112
+     */
113
+    public function getNumericStorageId() {
114
+        return $this->storageCache->getNumericId();
115
+    }
116
+
117
+    /**
118
+     * get the stored metadata of a file or folder
119
+     *
120
+     * @param string | int $file either the path of a file or folder or the file id for a file or folder
121
+     * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache
122
+     */
123
+    public function get($file) {
124
+        if (is_string($file) or $file == '') {
125
+            // normalize file
126
+            $file = $this->normalize($file);
127
+
128
+            $where = 'WHERE `storage` = ? AND `path_hash` = ?';
129
+            $params = array($this->getNumericStorageId(), md5($file));
130
+        } else { //file id
131
+            $where = 'WHERE `fileid` = ?';
132
+            $params = array($file);
133
+        }
134
+        $sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
135 135
 					   `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum`
136 136
 				FROM `*PREFIX*filecache` ' . $where;
137
-		$result = $this->connection->executeQuery($sql, $params);
138
-		$data = $result->fetch();
139
-
140
-		//FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO
141
-		//PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false
142
-		if ($data === null) {
143
-			$data = false;
144
-		}
145
-
146
-		//merge partial data
147
-		if (!$data and is_string($file)) {
148
-			if (isset($this->partial[$file])) {
149
-				$data = $this->partial[$file];
150
-			}
151
-			return $data;
152
-		} else {
153
-			return self::cacheEntryFromData($data, $this->mimetypeLoader);
154
-		}
155
-	}
156
-
157
-	/**
158
-	 * Create a CacheEntry from database row
159
-	 *
160
-	 * @param array $data
161
-	 * @param IMimeTypeLoader $mimetypeLoader
162
-	 * @return CacheEntry
163
-	 */
164
-	public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) {
165
-		//fix types
166
-		$data['fileid'] = (int)$data['fileid'];
167
-		$data['parent'] = (int)$data['parent'];
168
-		$data['size'] = 0 + $data['size'];
169
-		$data['mtime'] = (int)$data['mtime'];
170
-		$data['storage_mtime'] = (int)$data['storage_mtime'];
171
-		$data['encryptedVersion'] = (int)$data['encrypted'];
172
-		$data['encrypted'] = (bool)$data['encrypted'];
173
-		$data['storage_id'] = $data['storage'];
174
-		$data['storage'] = (int)$data['storage'];
175
-		$data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']);
176
-		$data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']);
177
-		if ($data['storage_mtime'] == 0) {
178
-			$data['storage_mtime'] = $data['mtime'];
179
-		}
180
-		$data['permissions'] = (int)$data['permissions'];
181
-		return new CacheEntry($data);
182
-	}
183
-
184
-	/**
185
-	 * get the metadata of all files stored in $folder
186
-	 *
187
-	 * @param string $folder
188
-	 * @return ICacheEntry[]
189
-	 */
190
-	public function getFolderContents($folder) {
191
-		$fileId = $this->getId($folder);
192
-		return $this->getFolderContentsById($fileId);
193
-	}
194
-
195
-	/**
196
-	 * get the metadata of all files stored in $folder
197
-	 *
198
-	 * @param int $fileId the file id of the folder
199
-	 * @return ICacheEntry[]
200
-	 */
201
-	public function getFolderContentsById($fileId) {
202
-		if ($fileId > -1) {
203
-			$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
137
+        $result = $this->connection->executeQuery($sql, $params);
138
+        $data = $result->fetch();
139
+
140
+        //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO
141
+        //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false
142
+        if ($data === null) {
143
+            $data = false;
144
+        }
145
+
146
+        //merge partial data
147
+        if (!$data and is_string($file)) {
148
+            if (isset($this->partial[$file])) {
149
+                $data = $this->partial[$file];
150
+            }
151
+            return $data;
152
+        } else {
153
+            return self::cacheEntryFromData($data, $this->mimetypeLoader);
154
+        }
155
+    }
156
+
157
+    /**
158
+     * Create a CacheEntry from database row
159
+     *
160
+     * @param array $data
161
+     * @param IMimeTypeLoader $mimetypeLoader
162
+     * @return CacheEntry
163
+     */
164
+    public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) {
165
+        //fix types
166
+        $data['fileid'] = (int)$data['fileid'];
167
+        $data['parent'] = (int)$data['parent'];
168
+        $data['size'] = 0 + $data['size'];
169
+        $data['mtime'] = (int)$data['mtime'];
170
+        $data['storage_mtime'] = (int)$data['storage_mtime'];
171
+        $data['encryptedVersion'] = (int)$data['encrypted'];
172
+        $data['encrypted'] = (bool)$data['encrypted'];
173
+        $data['storage_id'] = $data['storage'];
174
+        $data['storage'] = (int)$data['storage'];
175
+        $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']);
176
+        $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']);
177
+        if ($data['storage_mtime'] == 0) {
178
+            $data['storage_mtime'] = $data['mtime'];
179
+        }
180
+        $data['permissions'] = (int)$data['permissions'];
181
+        return new CacheEntry($data);
182
+    }
183
+
184
+    /**
185
+     * get the metadata of all files stored in $folder
186
+     *
187
+     * @param string $folder
188
+     * @return ICacheEntry[]
189
+     */
190
+    public function getFolderContents($folder) {
191
+        $fileId = $this->getId($folder);
192
+        return $this->getFolderContentsById($fileId);
193
+    }
194
+
195
+    /**
196
+     * get the metadata of all files stored in $folder
197
+     *
198
+     * @param int $fileId the file id of the folder
199
+     * @return ICacheEntry[]
200
+     */
201
+    public function getFolderContentsById($fileId) {
202
+        if ($fileId > -1) {
203
+            $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
204 204
 						   `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum`
205 205
 					FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC';
206
-			$result = $this->connection->executeQuery($sql, [$fileId]);
207
-			$files = $result->fetchAll();
208
-			return array_map(function (array $data) {
209
-				return self::cacheEntryFromData($data, $this->mimetypeLoader);;
210
-			}, $files);
211
-		} else {
212
-			return array();
213
-		}
214
-	}
215
-
216
-	/**
217
-	 * insert or update meta data for a file or folder
218
-	 *
219
-	 * @param string $file
220
-	 * @param array $data
221
-	 *
222
-	 * @return int file id
223
-	 * @throws \RuntimeException
224
-	 */
225
-	public function put($file, array $data) {
226
-		if (($id = $this->getId($file)) > -1) {
227
-			$this->update($id, $data);
228
-			return $id;
229
-		} else {
230
-			return $this->insert($file, $data);
231
-		}
232
-	}
233
-
234
-	/**
235
-	 * insert meta data for a new file or folder
236
-	 *
237
-	 * @param string $file
238
-	 * @param array $data
239
-	 *
240
-	 * @return int file id
241
-	 * @throws \RuntimeException
242
-	 */
243
-	public function insert($file, array $data) {
244
-		// normalize file
245
-		$file = $this->normalize($file);
246
-
247
-		if (isset($this->partial[$file])) { //add any saved partial data
248
-			$data = array_merge($this->partial[$file], $data);
249
-			unset($this->partial[$file]);
250
-		}
251
-
252
-		$requiredFields = array('size', 'mtime', 'mimetype');
253
-		foreach ($requiredFields as $field) {
254
-			if (!isset($data[$field])) { //data not complete save as partial and return
255
-				$this->partial[$file] = $data;
256
-				return -1;
257
-			}
258
-		}
259
-
260
-		$data['path'] = $file;
261
-		$data['parent'] = $this->getParentId($file);
262
-		$data['name'] = basename($file);
263
-
264
-		list($queryParts, $params) = $this->buildParts($data);
265
-		$queryParts[] = '`storage`';
266
-		$params[] = $this->getNumericStorageId();
267
-
268
-		$queryParts = array_map(function ($item) {
269
-			return trim($item, "`");
270
-		}, $queryParts);
271
-		$values = array_combine($queryParts, $params);
272
-		if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
273
-			'storage',
274
-			'path_hash',
275
-		])
276
-		) {
277
-			return (int)$this->connection->lastInsertId('*PREFIX*filecache');
278
-		}
279
-
280
-		// The file was created in the mean time
281
-		if (($id = $this->getId($file)) > -1) {
282
-			$this->update($id, $data);
283
-			return $id;
284
-		} else {
285
-			throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.');
286
-		}
287
-	}
288
-
289
-	/**
290
-	 * update the metadata of an existing file or folder in the cache
291
-	 *
292
-	 * @param int $id the fileid of the existing file or folder
293
-	 * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged
294
-	 */
295
-	public function update($id, array $data) {
296
-
297
-		if (isset($data['path'])) {
298
-			// normalize path
299
-			$data['path'] = $this->normalize($data['path']);
300
-		}
301
-
302
-		if (isset($data['name'])) {
303
-			// normalize path
304
-			$data['name'] = $this->normalize($data['name']);
305
-		}
306
-
307
-		list($queryParts, $params) = $this->buildParts($data);
308
-		// duplicate $params because we need the parts twice in the SQL statement
309
-		// once for the SET part, once in the WHERE clause
310
-		$params = array_merge($params, $params);
311
-		$params[] = $id;
312
-
313
-		// don't update if the data we try to set is the same as the one in the record
314
-		// some databases (Postgres) don't like superfluous updates
315
-		$sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' .
316
-			'WHERE (' .
317
-			implode(' <> ? OR ', $queryParts) . ' <> ? OR ' .
318
-			implode(' IS NULL OR ', $queryParts) . ' IS NULL' .
319
-			') AND `fileid` = ? ';
320
-		$this->connection->executeQuery($sql, $params);
321
-
322
-	}
323
-
324
-	/**
325
-	 * extract query parts and params array from data array
326
-	 *
327
-	 * @param array $data
328
-	 * @return array [$queryParts, $params]
329
-	 *        $queryParts: string[], the (escaped) column names to be set in the query
330
-	 *        $params: mixed[], the new values for the columns, to be passed as params to the query
331
-	 */
332
-	protected function buildParts(array $data) {
333
-		$fields = array(
334
-			'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted',
335
-			'etag', 'permissions', 'checksum', 'storage');
336
-
337
-		$doNotCopyStorageMTime = false;
338
-		if (array_key_exists('mtime', $data) && $data['mtime'] === null) {
339
-			// this horrific magic tells it to not copy storage_mtime to mtime
340
-			unset($data['mtime']);
341
-			$doNotCopyStorageMTime = true;
342
-		}
343
-
344
-		$params = array();
345
-		$queryParts = array();
346
-		foreach ($data as $name => $value) {
347
-			if (array_search($name, $fields) !== false) {
348
-				if ($name === 'path') {
349
-					$params[] = md5($value);
350
-					$queryParts[] = '`path_hash`';
351
-				} elseif ($name === 'mimetype') {
352
-					$params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/')));
353
-					$queryParts[] = '`mimepart`';
354
-					$value = $this->mimetypeLoader->getId($value);
355
-				} elseif ($name === 'storage_mtime') {
356
-					if (!$doNotCopyStorageMTime && !isset($data['mtime'])) {
357
-						$params[] = $value;
358
-						$queryParts[] = '`mtime`';
359
-					}
360
-				} elseif ($name === 'encrypted') {
361
-					if (isset($data['encryptedVersion'])) {
362
-						$value = $data['encryptedVersion'];
363
-					} else {
364
-						// Boolean to integer conversion
365
-						$value = $value ? 1 : 0;
366
-					}
367
-				}
368
-				$params[] = $value;
369
-				$queryParts[] = '`' . $name . '`';
370
-			}
371
-		}
372
-		return array($queryParts, $params);
373
-	}
374
-
375
-	/**
376
-	 * get the file id for a file
377
-	 *
378
-	 * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file
379
-	 *
380
-	 * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing
381
-	 *
382
-	 * @param string $file
383
-	 * @return int
384
-	 */
385
-	public function getId($file) {
386
-		// normalize file
387
-		$file = $this->normalize($file);
388
-
389
-		$pathHash = md5($file);
390
-
391
-		$sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
392
-		$result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
393
-		if ($row = $result->fetch()) {
394
-			return $row['fileid'];
395
-		} else {
396
-			return -1;
397
-		}
398
-	}
399
-
400
-	/**
401
-	 * get the id of the parent folder of a file
402
-	 *
403
-	 * @param string $file
404
-	 * @return int
405
-	 */
406
-	public function getParentId($file) {
407
-		if ($file === '') {
408
-			return -1;
409
-		} else {
410
-			$parent = $this->getParentPath($file);
411
-			return (int)$this->getId($parent);
412
-		}
413
-	}
414
-
415
-	private function getParentPath($path) {
416
-		$parent = dirname($path);
417
-		if ($parent === '.') {
418
-			$parent = '';
419
-		}
420
-		return $parent;
421
-	}
422
-
423
-	/**
424
-	 * check if a file is available in the cache
425
-	 *
426
-	 * @param string $file
427
-	 * @return bool
428
-	 */
429
-	public function inCache($file) {
430
-		return $this->getId($file) != -1;
431
-	}
432
-
433
-	/**
434
-	 * remove a file or folder from the cache
435
-	 *
436
-	 * when removing a folder from the cache all files and folders inside the folder will be removed as well
437
-	 *
438
-	 * @param string $file
439
-	 */
440
-	public function remove($file) {
441
-		$entry = $this->get($file);
442
-		$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
443
-		$this->connection->executeQuery($sql, array($entry['fileid']));
444
-		if ($entry['mimetype'] === 'httpd/unix-directory') {
445
-			$this->removeChildren($entry);
446
-		}
447
-	}
448
-
449
-	/**
450
-	 * Get all sub folders of a folder
451
-	 *
452
-	 * @param array $entry the cache entry of the folder to get the subfolders for
453
-	 * @return array[] the cache entries for the subfolders
454
-	 */
455
-	private function getSubFolders($entry) {
456
-		$children = $this->getFolderContentsById($entry['fileid']);
457
-		return array_filter($children, function ($child) {
458
-			return $child['mimetype'] === 'httpd/unix-directory';
459
-		});
460
-	}
461
-
462
-	/**
463
-	 * Recursively remove all children of a folder
464
-	 *
465
-	 * @param array $entry the cache entry of the folder to remove the children of
466
-	 * @throws \OC\DatabaseException
467
-	 */
468
-	private function removeChildren($entry) {
469
-		$subFolders = $this->getSubFolders($entry);
470
-		foreach ($subFolders as $folder) {
471
-			$this->removeChildren($folder);
472
-		}
473
-		$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?';
474
-		$this->connection->executeQuery($sql, array($entry['fileid']));
475
-	}
476
-
477
-	/**
478
-	 * Move a file or folder in the cache
479
-	 *
480
-	 * @param string $source
481
-	 * @param string $target
482
-	 */
483
-	public function move($source, $target) {
484
-		$this->moveFromCache($this, $source, $target);
485
-	}
486
-
487
-	/**
488
-	 * Get the storage id and path needed for a move
489
-	 *
490
-	 * @param string $path
491
-	 * @return array [$storageId, $internalPath]
492
-	 */
493
-	protected function getMoveInfo($path) {
494
-		return [$this->getNumericStorageId(), $path];
495
-	}
496
-
497
-	/**
498
-	 * Move a file or folder in the cache
499
-	 *
500
-	 * @param \OCP\Files\Cache\ICache $sourceCache
501
-	 * @param string $sourcePath
502
-	 * @param string $targetPath
503
-	 * @throws \OC\DatabaseException
504
-	 * @throws \Exception if the given storages have an invalid id
505
-	 * @suppress SqlInjectionChecker
506
-	 */
507
-	public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
508
-		if ($sourceCache instanceof Cache) {
509
-			// normalize source and target
510
-			$sourcePath = $this->normalize($sourcePath);
511
-			$targetPath = $this->normalize($targetPath);
512
-
513
-			$sourceData = $sourceCache->get($sourcePath);
514
-			$sourceId = $sourceData['fileid'];
515
-			$newParentId = $this->getParentId($targetPath);
516
-
517
-			list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath);
518
-			list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath);
519
-
520
-			if (is_null($sourceStorageId) || $sourceStorageId === false) {
521
-				throw new \Exception('Invalid source storage id: ' . $sourceStorageId);
522
-			}
523
-			if (is_null($targetStorageId) || $targetStorageId === false) {
524
-				throw new \Exception('Invalid target storage id: ' . $targetStorageId);
525
-			}
526
-
527
-			$this->connection->beginTransaction();
528
-			if ($sourceData['mimetype'] === 'httpd/unix-directory') {
529
-				//update all child entries
530
-				$sourceLength = mb_strlen($sourcePath);
531
-				$query = $this->connection->getQueryBuilder();
532
-
533
-				$fun = $query->func();
534
-				$newPathFunction = $fun->concat(
535
-					$query->createNamedParameter($targetPath),
536
-					$fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash
537
-				);
538
-				$query->update('filecache')
539
-					->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT))
540
-					->set('path_hash', $fun->md5($newPathFunction))
541
-					->set('path', $newPathFunction)
542
-					->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT)))
543
-					->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%')));
544
-
545
-				try {
546
-					$query->execute();
547
-				} catch (\OC\DatabaseException $e) {
548
-					$this->connection->rollBack();
549
-					throw $e;
550
-				}
551
-			}
552
-
553
-			$sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?';
554
-			$this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId));
555
-			$this->connection->commit();
556
-		} else {
557
-			$this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
558
-		}
559
-	}
560
-
561
-	/**
562
-	 * remove all entries for files that are stored on the storage from the cache
563
-	 */
564
-	public function clear() {
565
-		$sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
566
-		$this->connection->executeQuery($sql, array($this->getNumericStorageId()));
567
-
568
-		$sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
569
-		$this->connection->executeQuery($sql, array($this->storageId));
570
-	}
571
-
572
-	/**
573
-	 * Get the scan status of a file
574
-	 *
575
-	 * - Cache::NOT_FOUND: File is not in the cache
576
-	 * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known
577
-	 * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned
578
-	 * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned
579
-	 *
580
-	 * @param string $file
581
-	 *
582
-	 * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
583
-	 */
584
-	public function getStatus($file) {
585
-		// normalize file
586
-		$file = $this->normalize($file);
587
-
588
-		$pathHash = md5($file);
589
-		$sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
590
-		$result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
591
-		if ($row = $result->fetch()) {
592
-			if ((int)$row['size'] === -1) {
593
-				return self::SHALLOW;
594
-			} else {
595
-				return self::COMPLETE;
596
-			}
597
-		} else {
598
-			if (isset($this->partial[$file])) {
599
-				return self::PARTIAL;
600
-			} else {
601
-				return self::NOT_FOUND;
602
-			}
603
-		}
604
-	}
605
-
606
-	/**
607
-	 * search for files matching $pattern
608
-	 *
609
-	 * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
610
-	 * @return ICacheEntry[] an array of cache entries where the name matches the search pattern
611
-	 */
612
-	public function search($pattern) {
613
-		// normalize pattern
614
-		$pattern = $this->normalize($pattern);
615
-
616
-		if ($pattern === '%%') {
617
-			return [];
618
-		}
619
-
620
-
621
-		$sql = '
206
+            $result = $this->connection->executeQuery($sql, [$fileId]);
207
+            $files = $result->fetchAll();
208
+            return array_map(function (array $data) {
209
+                return self::cacheEntryFromData($data, $this->mimetypeLoader);;
210
+            }, $files);
211
+        } else {
212
+            return array();
213
+        }
214
+    }
215
+
216
+    /**
217
+     * insert or update meta data for a file or folder
218
+     *
219
+     * @param string $file
220
+     * @param array $data
221
+     *
222
+     * @return int file id
223
+     * @throws \RuntimeException
224
+     */
225
+    public function put($file, array $data) {
226
+        if (($id = $this->getId($file)) > -1) {
227
+            $this->update($id, $data);
228
+            return $id;
229
+        } else {
230
+            return $this->insert($file, $data);
231
+        }
232
+    }
233
+
234
+    /**
235
+     * insert meta data for a new file or folder
236
+     *
237
+     * @param string $file
238
+     * @param array $data
239
+     *
240
+     * @return int file id
241
+     * @throws \RuntimeException
242
+     */
243
+    public function insert($file, array $data) {
244
+        // normalize file
245
+        $file = $this->normalize($file);
246
+
247
+        if (isset($this->partial[$file])) { //add any saved partial data
248
+            $data = array_merge($this->partial[$file], $data);
249
+            unset($this->partial[$file]);
250
+        }
251
+
252
+        $requiredFields = array('size', 'mtime', 'mimetype');
253
+        foreach ($requiredFields as $field) {
254
+            if (!isset($data[$field])) { //data not complete save as partial and return
255
+                $this->partial[$file] = $data;
256
+                return -1;
257
+            }
258
+        }
259
+
260
+        $data['path'] = $file;
261
+        $data['parent'] = $this->getParentId($file);
262
+        $data['name'] = basename($file);
263
+
264
+        list($queryParts, $params) = $this->buildParts($data);
265
+        $queryParts[] = '`storage`';
266
+        $params[] = $this->getNumericStorageId();
267
+
268
+        $queryParts = array_map(function ($item) {
269
+            return trim($item, "`");
270
+        }, $queryParts);
271
+        $values = array_combine($queryParts, $params);
272
+        if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
273
+            'storage',
274
+            'path_hash',
275
+        ])
276
+        ) {
277
+            return (int)$this->connection->lastInsertId('*PREFIX*filecache');
278
+        }
279
+
280
+        // The file was created in the mean time
281
+        if (($id = $this->getId($file)) > -1) {
282
+            $this->update($id, $data);
283
+            return $id;
284
+        } else {
285
+            throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.');
286
+        }
287
+    }
288
+
289
+    /**
290
+     * update the metadata of an existing file or folder in the cache
291
+     *
292
+     * @param int $id the fileid of the existing file or folder
293
+     * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged
294
+     */
295
+    public function update($id, array $data) {
296
+
297
+        if (isset($data['path'])) {
298
+            // normalize path
299
+            $data['path'] = $this->normalize($data['path']);
300
+        }
301
+
302
+        if (isset($data['name'])) {
303
+            // normalize path
304
+            $data['name'] = $this->normalize($data['name']);
305
+        }
306
+
307
+        list($queryParts, $params) = $this->buildParts($data);
308
+        // duplicate $params because we need the parts twice in the SQL statement
309
+        // once for the SET part, once in the WHERE clause
310
+        $params = array_merge($params, $params);
311
+        $params[] = $id;
312
+
313
+        // don't update if the data we try to set is the same as the one in the record
314
+        // some databases (Postgres) don't like superfluous updates
315
+        $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' .
316
+            'WHERE (' .
317
+            implode(' <> ? OR ', $queryParts) . ' <> ? OR ' .
318
+            implode(' IS NULL OR ', $queryParts) . ' IS NULL' .
319
+            ') AND `fileid` = ? ';
320
+        $this->connection->executeQuery($sql, $params);
321
+
322
+    }
323
+
324
+    /**
325
+     * extract query parts and params array from data array
326
+     *
327
+     * @param array $data
328
+     * @return array [$queryParts, $params]
329
+     *        $queryParts: string[], the (escaped) column names to be set in the query
330
+     *        $params: mixed[], the new values for the columns, to be passed as params to the query
331
+     */
332
+    protected function buildParts(array $data) {
333
+        $fields = array(
334
+            'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted',
335
+            'etag', 'permissions', 'checksum', 'storage');
336
+
337
+        $doNotCopyStorageMTime = false;
338
+        if (array_key_exists('mtime', $data) && $data['mtime'] === null) {
339
+            // this horrific magic tells it to not copy storage_mtime to mtime
340
+            unset($data['mtime']);
341
+            $doNotCopyStorageMTime = true;
342
+        }
343
+
344
+        $params = array();
345
+        $queryParts = array();
346
+        foreach ($data as $name => $value) {
347
+            if (array_search($name, $fields) !== false) {
348
+                if ($name === 'path') {
349
+                    $params[] = md5($value);
350
+                    $queryParts[] = '`path_hash`';
351
+                } elseif ($name === 'mimetype') {
352
+                    $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/')));
353
+                    $queryParts[] = '`mimepart`';
354
+                    $value = $this->mimetypeLoader->getId($value);
355
+                } elseif ($name === 'storage_mtime') {
356
+                    if (!$doNotCopyStorageMTime && !isset($data['mtime'])) {
357
+                        $params[] = $value;
358
+                        $queryParts[] = '`mtime`';
359
+                    }
360
+                } elseif ($name === 'encrypted') {
361
+                    if (isset($data['encryptedVersion'])) {
362
+                        $value = $data['encryptedVersion'];
363
+                    } else {
364
+                        // Boolean to integer conversion
365
+                        $value = $value ? 1 : 0;
366
+                    }
367
+                }
368
+                $params[] = $value;
369
+                $queryParts[] = '`' . $name . '`';
370
+            }
371
+        }
372
+        return array($queryParts, $params);
373
+    }
374
+
375
+    /**
376
+     * get the file id for a file
377
+     *
378
+     * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file
379
+     *
380
+     * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing
381
+     *
382
+     * @param string $file
383
+     * @return int
384
+     */
385
+    public function getId($file) {
386
+        // normalize file
387
+        $file = $this->normalize($file);
388
+
389
+        $pathHash = md5($file);
390
+
391
+        $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
392
+        $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
393
+        if ($row = $result->fetch()) {
394
+            return $row['fileid'];
395
+        } else {
396
+            return -1;
397
+        }
398
+    }
399
+
400
+    /**
401
+     * get the id of the parent folder of a file
402
+     *
403
+     * @param string $file
404
+     * @return int
405
+     */
406
+    public function getParentId($file) {
407
+        if ($file === '') {
408
+            return -1;
409
+        } else {
410
+            $parent = $this->getParentPath($file);
411
+            return (int)$this->getId($parent);
412
+        }
413
+    }
414
+
415
+    private function getParentPath($path) {
416
+        $parent = dirname($path);
417
+        if ($parent === '.') {
418
+            $parent = '';
419
+        }
420
+        return $parent;
421
+    }
422
+
423
+    /**
424
+     * check if a file is available in the cache
425
+     *
426
+     * @param string $file
427
+     * @return bool
428
+     */
429
+    public function inCache($file) {
430
+        return $this->getId($file) != -1;
431
+    }
432
+
433
+    /**
434
+     * remove a file or folder from the cache
435
+     *
436
+     * when removing a folder from the cache all files and folders inside the folder will be removed as well
437
+     *
438
+     * @param string $file
439
+     */
440
+    public function remove($file) {
441
+        $entry = $this->get($file);
442
+        $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
443
+        $this->connection->executeQuery($sql, array($entry['fileid']));
444
+        if ($entry['mimetype'] === 'httpd/unix-directory') {
445
+            $this->removeChildren($entry);
446
+        }
447
+    }
448
+
449
+    /**
450
+     * Get all sub folders of a folder
451
+     *
452
+     * @param array $entry the cache entry of the folder to get the subfolders for
453
+     * @return array[] the cache entries for the subfolders
454
+     */
455
+    private function getSubFolders($entry) {
456
+        $children = $this->getFolderContentsById($entry['fileid']);
457
+        return array_filter($children, function ($child) {
458
+            return $child['mimetype'] === 'httpd/unix-directory';
459
+        });
460
+    }
461
+
462
+    /**
463
+     * Recursively remove all children of a folder
464
+     *
465
+     * @param array $entry the cache entry of the folder to remove the children of
466
+     * @throws \OC\DatabaseException
467
+     */
468
+    private function removeChildren($entry) {
469
+        $subFolders = $this->getSubFolders($entry);
470
+        foreach ($subFolders as $folder) {
471
+            $this->removeChildren($folder);
472
+        }
473
+        $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?';
474
+        $this->connection->executeQuery($sql, array($entry['fileid']));
475
+    }
476
+
477
+    /**
478
+     * Move a file or folder in the cache
479
+     *
480
+     * @param string $source
481
+     * @param string $target
482
+     */
483
+    public function move($source, $target) {
484
+        $this->moveFromCache($this, $source, $target);
485
+    }
486
+
487
+    /**
488
+     * Get the storage id and path needed for a move
489
+     *
490
+     * @param string $path
491
+     * @return array [$storageId, $internalPath]
492
+     */
493
+    protected function getMoveInfo($path) {
494
+        return [$this->getNumericStorageId(), $path];
495
+    }
496
+
497
+    /**
498
+     * Move a file or folder in the cache
499
+     *
500
+     * @param \OCP\Files\Cache\ICache $sourceCache
501
+     * @param string $sourcePath
502
+     * @param string $targetPath
503
+     * @throws \OC\DatabaseException
504
+     * @throws \Exception if the given storages have an invalid id
505
+     * @suppress SqlInjectionChecker
506
+     */
507
+    public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
508
+        if ($sourceCache instanceof Cache) {
509
+            // normalize source and target
510
+            $sourcePath = $this->normalize($sourcePath);
511
+            $targetPath = $this->normalize($targetPath);
512
+
513
+            $sourceData = $sourceCache->get($sourcePath);
514
+            $sourceId = $sourceData['fileid'];
515
+            $newParentId = $this->getParentId($targetPath);
516
+
517
+            list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath);
518
+            list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath);
519
+
520
+            if (is_null($sourceStorageId) || $sourceStorageId === false) {
521
+                throw new \Exception('Invalid source storage id: ' . $sourceStorageId);
522
+            }
523
+            if (is_null($targetStorageId) || $targetStorageId === false) {
524
+                throw new \Exception('Invalid target storage id: ' . $targetStorageId);
525
+            }
526
+
527
+            $this->connection->beginTransaction();
528
+            if ($sourceData['mimetype'] === 'httpd/unix-directory') {
529
+                //update all child entries
530
+                $sourceLength = mb_strlen($sourcePath);
531
+                $query = $this->connection->getQueryBuilder();
532
+
533
+                $fun = $query->func();
534
+                $newPathFunction = $fun->concat(
535
+                    $query->createNamedParameter($targetPath),
536
+                    $fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash
537
+                );
538
+                $query->update('filecache')
539
+                    ->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT))
540
+                    ->set('path_hash', $fun->md5($newPathFunction))
541
+                    ->set('path', $newPathFunction)
542
+                    ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT)))
543
+                    ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%')));
544
+
545
+                try {
546
+                    $query->execute();
547
+                } catch (\OC\DatabaseException $e) {
548
+                    $this->connection->rollBack();
549
+                    throw $e;
550
+                }
551
+            }
552
+
553
+            $sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?';
554
+            $this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId));
555
+            $this->connection->commit();
556
+        } else {
557
+            $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
558
+        }
559
+    }
560
+
561
+    /**
562
+     * remove all entries for files that are stored on the storage from the cache
563
+     */
564
+    public function clear() {
565
+        $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
566
+        $this->connection->executeQuery($sql, array($this->getNumericStorageId()));
567
+
568
+        $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
569
+        $this->connection->executeQuery($sql, array($this->storageId));
570
+    }
571
+
572
+    /**
573
+     * Get the scan status of a file
574
+     *
575
+     * - Cache::NOT_FOUND: File is not in the cache
576
+     * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known
577
+     * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned
578
+     * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned
579
+     *
580
+     * @param string $file
581
+     *
582
+     * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
583
+     */
584
+    public function getStatus($file) {
585
+        // normalize file
586
+        $file = $this->normalize($file);
587
+
588
+        $pathHash = md5($file);
589
+        $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?';
590
+        $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash));
591
+        if ($row = $result->fetch()) {
592
+            if ((int)$row['size'] === -1) {
593
+                return self::SHALLOW;
594
+            } else {
595
+                return self::COMPLETE;
596
+            }
597
+        } else {
598
+            if (isset($this->partial[$file])) {
599
+                return self::PARTIAL;
600
+            } else {
601
+                return self::NOT_FOUND;
602
+            }
603
+        }
604
+    }
605
+
606
+    /**
607
+     * search for files matching $pattern
608
+     *
609
+     * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
610
+     * @return ICacheEntry[] an array of cache entries where the name matches the search pattern
611
+     */
612
+    public function search($pattern) {
613
+        // normalize pattern
614
+        $pattern = $this->normalize($pattern);
615
+
616
+        if ($pattern === '%%') {
617
+            return [];
618
+        }
619
+
620
+
621
+        $sql = '
622 622
 			SELECT `fileid`, `storage`, `path`, `parent`, `name`,
623 623
 				`mimetype`, `storage_mtime`, `mimepart`, `size`, `mtime`,
624 624
 				 `encrypted`, `etag`, `permissions`, `checksum`
625 625
 			FROM `*PREFIX*filecache`
626 626
 			WHERE `storage` = ? AND `name` ILIKE ?';
627
-		$result = $this->connection->executeQuery($sql,
628
-			[$this->getNumericStorageId(), $pattern]
629
-		);
630
-
631
-		return $this->searchResultToCacheEntries($result);
632
-	}
633
-
634
-	/**
635
-	 * @param Statement $result
636
-	 * @return CacheEntry[]
637
-	 */
638
-	private function searchResultToCacheEntries(Statement $result) {
639
-		$files = $result->fetchAll();
640
-
641
-		return array_map(function (array $data) {
642
-			return self::cacheEntryFromData($data, $this->mimetypeLoader);
643
-		}, $files);
644
-	}
645
-
646
-	/**
647
-	 * search for files by mimetype
648
-	 *
649
-	 * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
650
-	 *        where it will search for all mimetypes in the group ('image/*')
651
-	 * @return ICacheEntry[] an array of cache entries where the mimetype matches the search
652
-	 */
653
-	public function searchByMime($mimetype) {
654
-		if (strpos($mimetype, '/')) {
655
-			$where = '`mimetype` = ?';
656
-		} else {
657
-			$where = '`mimepart` = ?';
658
-		}
659
-		$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum`
627
+        $result = $this->connection->executeQuery($sql,
628
+            [$this->getNumericStorageId(), $pattern]
629
+        );
630
+
631
+        return $this->searchResultToCacheEntries($result);
632
+    }
633
+
634
+    /**
635
+     * @param Statement $result
636
+     * @return CacheEntry[]
637
+     */
638
+    private function searchResultToCacheEntries(Statement $result) {
639
+        $files = $result->fetchAll();
640
+
641
+        return array_map(function (array $data) {
642
+            return self::cacheEntryFromData($data, $this->mimetypeLoader);
643
+        }, $files);
644
+    }
645
+
646
+    /**
647
+     * search for files by mimetype
648
+     *
649
+     * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
650
+     *        where it will search for all mimetypes in the group ('image/*')
651
+     * @return ICacheEntry[] an array of cache entries where the mimetype matches the search
652
+     */
653
+    public function searchByMime($mimetype) {
654
+        if (strpos($mimetype, '/')) {
655
+            $where = '`mimetype` = ?';
656
+        } else {
657
+            $where = '`mimepart` = ?';
658
+        }
659
+        $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum`
660 660
 				FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?';
661
-		$mimetype = $this->mimetypeLoader->getId($mimetype);
662
-		$result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId()));
663
-
664
-		return $this->searchResultToCacheEntries($result);
665
-	}
666
-
667
-	public function searchQuery(ISearchQuery $searchQuery) {
668
-		$builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
669
-
670
-		$query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum'])
671
-			->from('filecache', 'file');
672
-
673
-		$query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId())));
674
-
675
-		if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) {
676
-			$query
677
-				->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid'))
678
-				->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX(
679
-					$builder->expr()->eq('tagmap.type', 'tag.type'),
680
-					$builder->expr()->eq('tagmap.categoryid', 'tag.id')
681
-				))
682
-				->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files')))
683
-				->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID())));
684
-		}
685
-
686
-		$query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation()));
687
-
688
-		$this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder());
689
-
690
-		if ($searchQuery->getLimit()) {
691
-			$query->setMaxResults($searchQuery->getLimit());
692
-		}
693
-		if ($searchQuery->getOffset()) {
694
-			$query->setFirstResult($searchQuery->getOffset());
695
-		}
696
-
697
-		$result = $query->execute();
698
-		return $this->searchResultToCacheEntries($result);
699
-	}
700
-
701
-	/**
702
-	 * Search for files by tag of a given users.
703
-	 *
704
-	 * Note that every user can tag files differently.
705
-	 *
706
-	 * @param string|int $tag name or tag id
707
-	 * @param string $userId owner of the tags
708
-	 * @return ICacheEntry[] file data
709
-	 */
710
-	public function searchByTag($tag, $userId) {
711
-		$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' .
712
-			'`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' .
713
-			'`encrypted`, `etag`, `permissions`, `checksum` ' .
714
-			'FROM `*PREFIX*filecache` `file`, ' .
715
-			'`*PREFIX*vcategory_to_object` `tagmap`, ' .
716
-			'`*PREFIX*vcategory` `tag` ' .
717
-			// JOIN filecache to vcategory_to_object
718
-			'WHERE `file`.`fileid` = `tagmap`.`objid` ' .
719
-			// JOIN vcategory_to_object to vcategory
720
-			'AND `tagmap`.`type` = `tag`.`type` ' .
721
-			'AND `tagmap`.`categoryid` = `tag`.`id` ' .
722
-			// conditions
723
-			'AND `file`.`storage` = ? ' .
724
-			'AND `tag`.`type` = \'files\' ' .
725
-			'AND `tag`.`uid` = ? ';
726
-		if (is_int($tag)) {
727
-			$sql .= 'AND `tag`.`id` = ? ';
728
-		} else {
729
-			$sql .= 'AND `tag`.`category` = ? ';
730
-		}
731
-		$result = $this->connection->executeQuery(
732
-			$sql,
733
-			[
734
-				$this->getNumericStorageId(),
735
-				$userId,
736
-				$tag
737
-			]
738
-		);
739
-
740
-		$files = $result->fetchAll();
741
-
742
-		return array_map(function (array $data) {
743
-			return self::cacheEntryFromData($data, $this->mimetypeLoader);
744
-		}, $files);
745
-	}
746
-
747
-	/**
748
-	 * Re-calculate the folder size and the size of all parent folders
749
-	 *
750
-	 * @param string|boolean $path
751
-	 * @param array $data (optional) meta data of the folder
752
-	 */
753
-	public function correctFolderSize($path, $data = null) {
754
-		$this->calculateFolderSize($path, $data);
755
-		if ($path !== '') {
756
-			$parent = dirname($path);
757
-			if ($parent === '.' or $parent === '/') {
758
-				$parent = '';
759
-			}
760
-			$this->correctFolderSize($parent);
761
-		}
762
-	}
763
-
764
-	/**
765
-	 * calculate the size of a folder and set it in the cache
766
-	 *
767
-	 * @param string $path
768
-	 * @param array $entry (optional) meta data of the folder
769
-	 * @return int
770
-	 */
771
-	public function calculateFolderSize($path, $entry = null) {
772
-		$totalSize = 0;
773
-		if (is_null($entry) or !isset($entry['fileid'])) {
774
-			$entry = $this->get($path);
775
-		}
776
-		if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') {
777
-			$id = $entry['fileid'];
778
-			$sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' .
779
-				'FROM `*PREFIX*filecache` ' .
780
-				'WHERE `parent` = ? AND `storage` = ?';
781
-			$result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
782
-			if ($row = $result->fetch()) {
783
-				$result->closeCursor();
784
-				list($sum, $min) = array_values($row);
785
-				$sum = 0 + $sum;
786
-				$min = 0 + $min;
787
-				if ($min === -1) {
788
-					$totalSize = $min;
789
-				} else {
790
-					$totalSize = $sum;
791
-				}
792
-				$update = array();
793
-				if ($entry['size'] !== $totalSize) {
794
-					$update['size'] = $totalSize;
795
-				}
796
-				if (count($update) > 0) {
797
-					$this->update($id, $update);
798
-				}
799
-			} else {
800
-				$result->closeCursor();
801
-			}
802
-		}
803
-		return $totalSize;
804
-	}
805
-
806
-	/**
807
-	 * get all file ids on the files on the storage
808
-	 *
809
-	 * @return int[]
810
-	 */
811
-	public function getAll() {
812
-		$sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?';
813
-		$result = $this->connection->executeQuery($sql, array($this->getNumericStorageId()));
814
-		$ids = array();
815
-		while ($row = $result->fetch()) {
816
-			$ids[] = $row['fileid'];
817
-		}
818
-		return $ids;
819
-	}
820
-
821
-	/**
822
-	 * find a folder in the cache which has not been fully scanned
823
-	 *
824
-	 * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
825
-	 * use the one with the highest id gives the best result with the background scanner, since that is most
826
-	 * likely the folder where we stopped scanning previously
827
-	 *
828
-	 * @return string|bool the path of the folder or false when no folder matched
829
-	 */
830
-	public function getIncomplete() {
831
-		$query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`'
832
-			. ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1);
833
-		$query->execute([$this->getNumericStorageId()]);
834
-		if ($row = $query->fetch()) {
835
-			return $row['path'];
836
-		} else {
837
-			return false;
838
-		}
839
-	}
840
-
841
-	/**
842
-	 * get the path of a file on this storage by it's file id
843
-	 *
844
-	 * @param int $id the file id of the file or folder to search
845
-	 * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache
846
-	 */
847
-	public function getPathById($id) {
848
-		$sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?';
849
-		$result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
850
-		if ($row = $result->fetch()) {
851
-			// Oracle stores empty strings as null...
852
-			if ($row['path'] === null) {
853
-				return '';
854
-			}
855
-			return $row['path'];
856
-		} else {
857
-			return null;
858
-		}
859
-	}
860
-
861
-	/**
862
-	 * get the storage id of the storage for a file and the internal path of the file
863
-	 * unlike getPathById this does not limit the search to files on this storage and
864
-	 * instead does a global search in the cache table
865
-	 *
866
-	 * @param int $id
867
-	 * @deprecated use getPathById() instead
868
-	 * @return array first element holding the storage id, second the path
869
-	 */
870
-	static public function getById($id) {
871
-		$connection = \OC::$server->getDatabaseConnection();
872
-		$sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
873
-		$result = $connection->executeQuery($sql, array($id));
874
-		if ($row = $result->fetch()) {
875
-			$numericId = $row['storage'];
876
-			$path = $row['path'];
877
-		} else {
878
-			return null;
879
-		}
880
-
881
-		if ($id = Storage::getStorageId($numericId)) {
882
-			return array($id, $path);
883
-		} else {
884
-			return null;
885
-		}
886
-	}
887
-
888
-	/**
889
-	 * normalize the given path
890
-	 *
891
-	 * @param string $path
892
-	 * @return string
893
-	 */
894
-	public function normalize($path) {
895
-
896
-		return trim(\OC_Util::normalizeUnicode($path), '/');
897
-	}
661
+        $mimetype = $this->mimetypeLoader->getId($mimetype);
662
+        $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId()));
663
+
664
+        return $this->searchResultToCacheEntries($result);
665
+    }
666
+
667
+    public function searchQuery(ISearchQuery $searchQuery) {
668
+        $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
669
+
670
+        $query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum'])
671
+            ->from('filecache', 'file');
672
+
673
+        $query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId())));
674
+
675
+        if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) {
676
+            $query
677
+                ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid'))
678
+                ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX(
679
+                    $builder->expr()->eq('tagmap.type', 'tag.type'),
680
+                    $builder->expr()->eq('tagmap.categoryid', 'tag.id')
681
+                ))
682
+                ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files')))
683
+                ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID())));
684
+        }
685
+
686
+        $query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation()));
687
+
688
+        $this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder());
689
+
690
+        if ($searchQuery->getLimit()) {
691
+            $query->setMaxResults($searchQuery->getLimit());
692
+        }
693
+        if ($searchQuery->getOffset()) {
694
+            $query->setFirstResult($searchQuery->getOffset());
695
+        }
696
+
697
+        $result = $query->execute();
698
+        return $this->searchResultToCacheEntries($result);
699
+    }
700
+
701
+    /**
702
+     * Search for files by tag of a given users.
703
+     *
704
+     * Note that every user can tag files differently.
705
+     *
706
+     * @param string|int $tag name or tag id
707
+     * @param string $userId owner of the tags
708
+     * @return ICacheEntry[] file data
709
+     */
710
+    public function searchByTag($tag, $userId) {
711
+        $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' .
712
+            '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' .
713
+            '`encrypted`, `etag`, `permissions`, `checksum` ' .
714
+            'FROM `*PREFIX*filecache` `file`, ' .
715
+            '`*PREFIX*vcategory_to_object` `tagmap`, ' .
716
+            '`*PREFIX*vcategory` `tag` ' .
717
+            // JOIN filecache to vcategory_to_object
718
+            'WHERE `file`.`fileid` = `tagmap`.`objid` ' .
719
+            // JOIN vcategory_to_object to vcategory
720
+            'AND `tagmap`.`type` = `tag`.`type` ' .
721
+            'AND `tagmap`.`categoryid` = `tag`.`id` ' .
722
+            // conditions
723
+            'AND `file`.`storage` = ? ' .
724
+            'AND `tag`.`type` = \'files\' ' .
725
+            'AND `tag`.`uid` = ? ';
726
+        if (is_int($tag)) {
727
+            $sql .= 'AND `tag`.`id` = ? ';
728
+        } else {
729
+            $sql .= 'AND `tag`.`category` = ? ';
730
+        }
731
+        $result = $this->connection->executeQuery(
732
+            $sql,
733
+            [
734
+                $this->getNumericStorageId(),
735
+                $userId,
736
+                $tag
737
+            ]
738
+        );
739
+
740
+        $files = $result->fetchAll();
741
+
742
+        return array_map(function (array $data) {
743
+            return self::cacheEntryFromData($data, $this->mimetypeLoader);
744
+        }, $files);
745
+    }
746
+
747
+    /**
748
+     * Re-calculate the folder size and the size of all parent folders
749
+     *
750
+     * @param string|boolean $path
751
+     * @param array $data (optional) meta data of the folder
752
+     */
753
+    public function correctFolderSize($path, $data = null) {
754
+        $this->calculateFolderSize($path, $data);
755
+        if ($path !== '') {
756
+            $parent = dirname($path);
757
+            if ($parent === '.' or $parent === '/') {
758
+                $parent = '';
759
+            }
760
+            $this->correctFolderSize($parent);
761
+        }
762
+    }
763
+
764
+    /**
765
+     * calculate the size of a folder and set it in the cache
766
+     *
767
+     * @param string $path
768
+     * @param array $entry (optional) meta data of the folder
769
+     * @return int
770
+     */
771
+    public function calculateFolderSize($path, $entry = null) {
772
+        $totalSize = 0;
773
+        if (is_null($entry) or !isset($entry['fileid'])) {
774
+            $entry = $this->get($path);
775
+        }
776
+        if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') {
777
+            $id = $entry['fileid'];
778
+            $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' .
779
+                'FROM `*PREFIX*filecache` ' .
780
+                'WHERE `parent` = ? AND `storage` = ?';
781
+            $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
782
+            if ($row = $result->fetch()) {
783
+                $result->closeCursor();
784
+                list($sum, $min) = array_values($row);
785
+                $sum = 0 + $sum;
786
+                $min = 0 + $min;
787
+                if ($min === -1) {
788
+                    $totalSize = $min;
789
+                } else {
790
+                    $totalSize = $sum;
791
+                }
792
+                $update = array();
793
+                if ($entry['size'] !== $totalSize) {
794
+                    $update['size'] = $totalSize;
795
+                }
796
+                if (count($update) > 0) {
797
+                    $this->update($id, $update);
798
+                }
799
+            } else {
800
+                $result->closeCursor();
801
+            }
802
+        }
803
+        return $totalSize;
804
+    }
805
+
806
+    /**
807
+     * get all file ids on the files on the storage
808
+     *
809
+     * @return int[]
810
+     */
811
+    public function getAll() {
812
+        $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?';
813
+        $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId()));
814
+        $ids = array();
815
+        while ($row = $result->fetch()) {
816
+            $ids[] = $row['fileid'];
817
+        }
818
+        return $ids;
819
+    }
820
+
821
+    /**
822
+     * find a folder in the cache which has not been fully scanned
823
+     *
824
+     * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
825
+     * use the one with the highest id gives the best result with the background scanner, since that is most
826
+     * likely the folder where we stopped scanning previously
827
+     *
828
+     * @return string|bool the path of the folder or false when no folder matched
829
+     */
830
+    public function getIncomplete() {
831
+        $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`'
832
+            . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1);
833
+        $query->execute([$this->getNumericStorageId()]);
834
+        if ($row = $query->fetch()) {
835
+            return $row['path'];
836
+        } else {
837
+            return false;
838
+        }
839
+    }
840
+
841
+    /**
842
+     * get the path of a file on this storage by it's file id
843
+     *
844
+     * @param int $id the file id of the file or folder to search
845
+     * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache
846
+     */
847
+    public function getPathById($id) {
848
+        $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?';
849
+        $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId()));
850
+        if ($row = $result->fetch()) {
851
+            // Oracle stores empty strings as null...
852
+            if ($row['path'] === null) {
853
+                return '';
854
+            }
855
+            return $row['path'];
856
+        } else {
857
+            return null;
858
+        }
859
+    }
860
+
861
+    /**
862
+     * get the storage id of the storage for a file and the internal path of the file
863
+     * unlike getPathById this does not limit the search to files on this storage and
864
+     * instead does a global search in the cache table
865
+     *
866
+     * @param int $id
867
+     * @deprecated use getPathById() instead
868
+     * @return array first element holding the storage id, second the path
869
+     */
870
+    static public function getById($id) {
871
+        $connection = \OC::$server->getDatabaseConnection();
872
+        $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
873
+        $result = $connection->executeQuery($sql, array($id));
874
+        if ($row = $result->fetch()) {
875
+            $numericId = $row['storage'];
876
+            $path = $row['path'];
877
+        } else {
878
+            return null;
879
+        }
880
+
881
+        if ($id = Storage::getStorageId($numericId)) {
882
+            return array($id, $path);
883
+        } else {
884
+            return null;
885
+        }
886
+    }
887
+
888
+    /**
889
+     * normalize the given path
890
+     *
891
+     * @param string $path
892
+     * @return string
893
+     */
894
+    public function normalize($path) {
895
+
896
+        return trim(\OC_Util::normalizeUnicode($path), '/');
897
+    }
898 898
 }
Please login to merge, or discard this patch.