@@ -304,9 +304,9 @@ |
||
304 | 304 | */ |
305 | 305 | public static function linkToRemote($service) { |
306 | 306 | $urlGenerator = \OC::$server->getURLGenerator(); |
307 | - $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
307 | + $remoteBase = $urlGenerator->linkTo('', 'remote.php').'/'.$service; |
|
308 | 308 | return $urlGenerator->getAbsoluteURL( |
309 | - $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
309 | + $remoteBase.(($service[strlen($service) - 1] != '/') ? '/' : '') |
|
310 | 310 | ); |
311 | 311 | } |
312 | 312 |
@@ -57,474 +57,474 @@ |
||
57 | 57 | * @since 4.0.0 |
58 | 58 | */ |
59 | 59 | class Util { |
60 | - /** |
|
61 | - * @deprecated 14.0.0 use \OCP\ILogger::DEBUG |
|
62 | - */ |
|
63 | - public const DEBUG = 0; |
|
64 | - /** |
|
65 | - * @deprecated 14.0.0 use \OCP\ILogger::INFO |
|
66 | - */ |
|
67 | - public const INFO = 1; |
|
68 | - /** |
|
69 | - * @deprecated 14.0.0 use \OCP\ILogger::WARN |
|
70 | - */ |
|
71 | - public const WARN = 2; |
|
72 | - /** |
|
73 | - * @deprecated 14.0.0 use \OCP\ILogger::ERROR |
|
74 | - */ |
|
75 | - public const ERROR = 3; |
|
76 | - /** |
|
77 | - * @deprecated 14.0.0 use \OCP\ILogger::FATAL |
|
78 | - */ |
|
79 | - public const FATAL = 4; |
|
80 | - |
|
81 | - /** \OCP\Share\IManager */ |
|
82 | - private static $shareManager; |
|
83 | - |
|
84 | - /** |
|
85 | - * get the current installed version of Nextcloud |
|
86 | - * @return array |
|
87 | - * @since 4.0.0 |
|
88 | - */ |
|
89 | - public static function getVersion() { |
|
90 | - return \OC_Util::getVersion(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @since 17.0.0 |
|
95 | - */ |
|
96 | - public static function hasExtendedSupport(): bool { |
|
97 | - try { |
|
98 | - /** @var \OCP\Support\Subscription\IRegistry */ |
|
99 | - $subscriptionRegistry = \OC::$server->query(\OCP\Support\Subscription\IRegistry::class); |
|
100 | - return $subscriptionRegistry->delegateHasExtendedSupport(); |
|
101 | - } catch (AppFramework\QueryException $e) { |
|
102 | - } |
|
103 | - return \OC::$server->getConfig()->getSystemValueBool('extendedSupport', false); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Set current update channel |
|
108 | - * @param string $channel |
|
109 | - * @since 8.1.0 |
|
110 | - */ |
|
111 | - public static function setChannel($channel) { |
|
112 | - \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Get current update channel |
|
117 | - * @return string |
|
118 | - * @since 8.1.0 |
|
119 | - */ |
|
120 | - public static function getChannel() { |
|
121 | - return \OC_Util::getChannel(); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * write a message in the log |
|
126 | - * @param string $app |
|
127 | - * @param string $message |
|
128 | - * @param int $level |
|
129 | - * @since 4.0.0 |
|
130 | - * @deprecated 13.0.0 use log of \OCP\ILogger |
|
131 | - */ |
|
132 | - public static function writeLog($app, $message, $level) { |
|
133 | - $context = ['app' => $app]; |
|
134 | - \OC::$server->getLogger()->log($level, $message, $context); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * check if sharing is disabled for the current user |
|
139 | - * |
|
140 | - * @return boolean |
|
141 | - * @since 7.0.0 |
|
142 | - * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser |
|
143 | - */ |
|
144 | - public static function isSharingDisabledForUser() { |
|
145 | - if (self::$shareManager === null) { |
|
146 | - self::$shareManager = \OC::$server->getShareManager(); |
|
147 | - } |
|
148 | - |
|
149 | - $user = \OC::$server->getUserSession()->getUser(); |
|
150 | - if ($user !== null) { |
|
151 | - $user = $user->getUID(); |
|
152 | - } |
|
153 | - |
|
154 | - return self::$shareManager->sharingDisabledForUser($user); |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * get l10n object |
|
159 | - * @param string $application |
|
160 | - * @param string|null $language |
|
161 | - * @return \OCP\IL10N |
|
162 | - * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
163 | - */ |
|
164 | - public static function getL10N($application, $language = null) { |
|
165 | - return \OC::$server->getL10N($application, $language); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * add a css file |
|
170 | - * @param string $application |
|
171 | - * @param string $file |
|
172 | - * @since 4.0.0 |
|
173 | - */ |
|
174 | - public static function addStyle($application, $file = null) { |
|
175 | - \OC_Util::addStyle($application, $file); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * add a javascript file |
|
180 | - * @param string $application |
|
181 | - * @param string $file |
|
182 | - * @since 4.0.0 |
|
183 | - */ |
|
184 | - public static function addScript($application, $file = null) { |
|
185 | - \OC_Util::addScript($application, $file); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Add a translation JS file |
|
190 | - * @param string $application application id |
|
191 | - * @param string $languageCode language code, defaults to the current locale |
|
192 | - * @since 8.0.0 |
|
193 | - */ |
|
194 | - public static function addTranslations($application, $languageCode = null) { |
|
195 | - \OC_Util::addTranslations($application, $languageCode); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Add a custom element to the header |
|
200 | - * If $text is null then the element will be written as empty element. |
|
201 | - * So use "" to get a closing tag. |
|
202 | - * @param string $tag tag name of the element |
|
203 | - * @param array $attributes array of attributes for the element |
|
204 | - * @param string $text the text content for the element |
|
205 | - * @since 4.0.0 |
|
206 | - */ |
|
207 | - public static function addHeader($tag, $attributes, $text = null) { |
|
208 | - \OC_Util::addHeader($tag, $attributes, $text); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Creates an absolute url to the given app and file. |
|
213 | - * @param string $app app |
|
214 | - * @param string $file file |
|
215 | - * @param array $args array with param=>value, will be appended to the returned url |
|
216 | - * The value of $args will be urlencoded |
|
217 | - * @return string the url |
|
218 | - * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
219 | - */ |
|
220 | - public static function linkToAbsolute($app, $file, $args = []) { |
|
221 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
222 | - return $urlGenerator->getAbsoluteURL( |
|
223 | - $urlGenerator->linkTo($app, $file, $args) |
|
224 | - ); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Creates an absolute url for remote use. |
|
229 | - * @param string $service id |
|
230 | - * @return string the url |
|
231 | - * @since 4.0.0 |
|
232 | - */ |
|
233 | - public static function linkToRemote($service) { |
|
234 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
235 | - $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
236 | - return $urlGenerator->getAbsoluteURL( |
|
237 | - $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
238 | - ); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Creates an absolute url for public use |
|
243 | - * @param string $service id |
|
244 | - * @return string the url |
|
245 | - * @since 4.5.0 |
|
246 | - * @deprecated 15.0.0 - use OCP\IURLGenerator |
|
247 | - */ |
|
248 | - public static function linkToPublic($service) { |
|
249 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
250 | - if ($service === 'files') { |
|
251 | - return $urlGenerator->getAbsoluteURL('/s'); |
|
252 | - } |
|
253 | - return $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'public.php').'?service='.$service); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Returns the server host name without an eventual port number |
|
258 | - * @return string the server hostname |
|
259 | - * @since 5.0.0 |
|
260 | - */ |
|
261 | - public static function getServerHostName() { |
|
262 | - $host_name = \OC::$server->getRequest()->getServerHost(); |
|
263 | - // strip away port number (if existing) |
|
264 | - $colon_pos = strpos($host_name, ':'); |
|
265 | - if ($colon_pos != false) { |
|
266 | - $host_name = substr($host_name, 0, $colon_pos); |
|
267 | - } |
|
268 | - return $host_name; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Returns the default email address |
|
273 | - * @param string $user_part the user part of the address |
|
274 | - * @return string the default email address |
|
275 | - * |
|
276 | - * Assembles a default email address (using the server hostname |
|
277 | - * and the given user part, and returns it |
|
278 | - * Example: when given lostpassword-noreply as $user_part param, |
|
279 | - * and is currently accessed via http(s)://example.com/, |
|
280 | - * it would return '[email protected]' |
|
281 | - * |
|
282 | - * If the configuration value 'mail_from_address' is set in |
|
283 | - * config.php, this value will override the $user_part that |
|
284 | - * is passed to this function |
|
285 | - * @since 5.0.0 |
|
286 | - */ |
|
287 | - public static function getDefaultEmailAddress($user_part) { |
|
288 | - $config = \OC::$server->getConfig(); |
|
289 | - $user_part = $config->getSystemValue('mail_from_address', $user_part); |
|
290 | - $host_name = self::getServerHostName(); |
|
291 | - $host_name = $config->getSystemValue('mail_domain', $host_name); |
|
292 | - $defaultEmailAddress = $user_part.'@'.$host_name; |
|
293 | - |
|
294 | - $mailer = \OC::$server->getMailer(); |
|
295 | - if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
296 | - return $defaultEmailAddress; |
|
297 | - } |
|
298 | - |
|
299 | - // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
300 | - return $user_part.'@localhost.localdomain'; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Make a human file size (2048 to 2 kB) |
|
305 | - * @param int $bytes file size in bytes |
|
306 | - * @return string a human readable file size |
|
307 | - * @since 4.0.0 |
|
308 | - */ |
|
309 | - public static function humanFileSize($bytes) { |
|
310 | - return \OC_Helper::humanFileSize($bytes); |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Make a computer file size (2 kB to 2048) |
|
315 | - * @param string $str file size in a fancy format |
|
316 | - * @return float a file size in bytes |
|
317 | - * |
|
318 | - * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
319 | - * @since 4.0.0 |
|
320 | - */ |
|
321 | - public static function computerFileSize($str) { |
|
322 | - return \OC_Helper::computerFileSize($str); |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * connects a function to a hook |
|
327 | - * |
|
328 | - * @param string $signalClass class name of emitter |
|
329 | - * @param string $signalName name of signal |
|
330 | - * @param string|object $slotClass class name of slot |
|
331 | - * @param string $slotName name of slot |
|
332 | - * @return bool |
|
333 | - * |
|
334 | - * This function makes it very easy to connect to use hooks. |
|
335 | - * |
|
336 | - * TODO: write example |
|
337 | - * @since 4.0.0 |
|
338 | - */ |
|
339 | - public static function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
|
340 | - return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Emits a signal. To get data from the slot use references! |
|
345 | - * @param string $signalclass class name of emitter |
|
346 | - * @param string $signalname name of signal |
|
347 | - * @param array $params default: array() array with additional data |
|
348 | - * @return bool true if slots exists or false if not |
|
349 | - * |
|
350 | - * TODO: write example |
|
351 | - * @since 4.0.0 |
|
352 | - */ |
|
353 | - public static function emitHook($signalclass, $signalname, $params = []) { |
|
354 | - return \OC_Hook::emit($signalclass, $signalname, $params); |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
359 | - * multiple OC_Template elements which invoke `callRegister`. If the value |
|
360 | - * would not be cached these unit-tests would fail. |
|
361 | - * @var string |
|
362 | - */ |
|
363 | - private static $token = ''; |
|
364 | - |
|
365 | - /** |
|
366 | - * Register an get/post call. This is important to prevent CSRF attacks |
|
367 | - * @since 4.5.0 |
|
368 | - */ |
|
369 | - public static function callRegister() { |
|
370 | - if (self::$token === '') { |
|
371 | - self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
|
372 | - } |
|
373 | - return self::$token; |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * Used to sanitize HTML |
|
378 | - * |
|
379 | - * This function is used to sanitize HTML and should be applied on any |
|
380 | - * string or array of strings before displaying it on a web page. |
|
381 | - * |
|
382 | - * @param string|array $value |
|
383 | - * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
384 | - * @since 4.5.0 |
|
385 | - */ |
|
386 | - public static function sanitizeHTML($value) { |
|
387 | - return \OC_Util::sanitizeHTML($value); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Public function to encode url parameters |
|
392 | - * |
|
393 | - * This function is used to encode path to file before output. |
|
394 | - * Encoding is done according to RFC 3986 with one exception: |
|
395 | - * Character '/' is preserved as is. |
|
396 | - * |
|
397 | - * @param string $component part of URI to encode |
|
398 | - * @return string |
|
399 | - * @since 6.0.0 |
|
400 | - */ |
|
401 | - public static function encodePath($component) { |
|
402 | - return \OC_Util::encodePath($component); |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
407 | - * |
|
408 | - * @param array $input The array to work on |
|
409 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
410 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
411 | - * @return array |
|
412 | - * @since 4.5.0 |
|
413 | - */ |
|
414 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
415 | - return \OC_Helper::mb_array_change_key_case($input, $case, $encoding); |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * performs a search in a nested array |
|
420 | - * |
|
421 | - * @param array $haystack the array to be searched |
|
422 | - * @param string $needle the search string |
|
423 | - * @param mixed $index optional, only search this key name |
|
424 | - * @return mixed the key of the matching field, otherwise false |
|
425 | - * @since 4.5.0 |
|
426 | - * @deprecated 15.0.0 |
|
427 | - */ |
|
428 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
429 | - return \OC_Helper::recursiveArraySearch($haystack, $needle, $index); |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
434 | - * |
|
435 | - * @param string $dir the current folder where the user currently operates |
|
436 | - * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
437 | - * @return int number of bytes representing |
|
438 | - * @since 5.0.0 |
|
439 | - */ |
|
440 | - public static function maxUploadFilesize($dir, $free = null) { |
|
441 | - return \OC_Helper::maxUploadFilesize($dir, $free); |
|
442 | - } |
|
443 | - |
|
444 | - /** |
|
445 | - * Calculate free space left within user quota |
|
446 | - * @param string $dir the current folder where the user currently operates |
|
447 | - * @return int number of bytes representing |
|
448 | - * @since 7.0.0 |
|
449 | - */ |
|
450 | - public static function freeSpace($dir) { |
|
451 | - return \OC_Helper::freeSpace($dir); |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Calculate PHP upload limit |
|
456 | - * |
|
457 | - * @return int number of bytes representing |
|
458 | - * @since 7.0.0 |
|
459 | - */ |
|
460 | - public static function uploadLimit() { |
|
461 | - return \OC_Helper::uploadLimit(); |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Returns whether the given file name is valid |
|
466 | - * @param string $file file name to check |
|
467 | - * @return bool true if the file name is valid, false otherwise |
|
468 | - * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
|
469 | - * @since 7.0.0 |
|
470 | - * @suppress PhanDeprecatedFunction |
|
471 | - */ |
|
472 | - public static function isValidFileName($file) { |
|
473 | - return \OC_Util::isValidFileName($file); |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Compare two strings to provide a natural sort |
|
478 | - * @param string $a first string to compare |
|
479 | - * @param string $b second string to compare |
|
480 | - * @return int -1 if $b comes before $a, 1 if $a comes before $b |
|
481 | - * or 0 if the strings are identical |
|
482 | - * @since 7.0.0 |
|
483 | - */ |
|
484 | - public static function naturalSortCompare($a, $b) { |
|
485 | - return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * check if a password is required for each public link |
|
490 | - * @return boolean |
|
491 | - * @since 7.0.0 |
|
492 | - */ |
|
493 | - public static function isPublicLinkPasswordRequired() { |
|
494 | - return \OC_Util::isPublicLinkPasswordRequired(); |
|
495 | - } |
|
496 | - |
|
497 | - /** |
|
498 | - * check if share API enforces a default expire date |
|
499 | - * @return boolean |
|
500 | - * @since 8.0.0 |
|
501 | - */ |
|
502 | - public static function isDefaultExpireDateEnforced() { |
|
503 | - return \OC_Util::isDefaultExpireDateEnforced(); |
|
504 | - } |
|
505 | - |
|
506 | - protected static $needUpgradeCache = null; |
|
507 | - |
|
508 | - /** |
|
509 | - * Checks whether the current version needs upgrade. |
|
510 | - * |
|
511 | - * @return bool true if upgrade is needed, false otherwise |
|
512 | - * @since 7.0.0 |
|
513 | - */ |
|
514 | - public static function needUpgrade() { |
|
515 | - if (!isset(self::$needUpgradeCache)) { |
|
516 | - self::$needUpgradeCache = \OC_Util::needUpgrade(\OC::$server->getSystemConfig()); |
|
517 | - } |
|
518 | - return self::$needUpgradeCache; |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * is this Internet explorer ? |
|
523 | - * |
|
524 | - * @return boolean |
|
525 | - * @since 14.0.0 |
|
526 | - */ |
|
527 | - public static function isIe() { |
|
528 | - return \OC_Util::isIe(); |
|
529 | - } |
|
60 | + /** |
|
61 | + * @deprecated 14.0.0 use \OCP\ILogger::DEBUG |
|
62 | + */ |
|
63 | + public const DEBUG = 0; |
|
64 | + /** |
|
65 | + * @deprecated 14.0.0 use \OCP\ILogger::INFO |
|
66 | + */ |
|
67 | + public const INFO = 1; |
|
68 | + /** |
|
69 | + * @deprecated 14.0.0 use \OCP\ILogger::WARN |
|
70 | + */ |
|
71 | + public const WARN = 2; |
|
72 | + /** |
|
73 | + * @deprecated 14.0.0 use \OCP\ILogger::ERROR |
|
74 | + */ |
|
75 | + public const ERROR = 3; |
|
76 | + /** |
|
77 | + * @deprecated 14.0.0 use \OCP\ILogger::FATAL |
|
78 | + */ |
|
79 | + public const FATAL = 4; |
|
80 | + |
|
81 | + /** \OCP\Share\IManager */ |
|
82 | + private static $shareManager; |
|
83 | + |
|
84 | + /** |
|
85 | + * get the current installed version of Nextcloud |
|
86 | + * @return array |
|
87 | + * @since 4.0.0 |
|
88 | + */ |
|
89 | + public static function getVersion() { |
|
90 | + return \OC_Util::getVersion(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @since 17.0.0 |
|
95 | + */ |
|
96 | + public static function hasExtendedSupport(): bool { |
|
97 | + try { |
|
98 | + /** @var \OCP\Support\Subscription\IRegistry */ |
|
99 | + $subscriptionRegistry = \OC::$server->query(\OCP\Support\Subscription\IRegistry::class); |
|
100 | + return $subscriptionRegistry->delegateHasExtendedSupport(); |
|
101 | + } catch (AppFramework\QueryException $e) { |
|
102 | + } |
|
103 | + return \OC::$server->getConfig()->getSystemValueBool('extendedSupport', false); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Set current update channel |
|
108 | + * @param string $channel |
|
109 | + * @since 8.1.0 |
|
110 | + */ |
|
111 | + public static function setChannel($channel) { |
|
112 | + \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Get current update channel |
|
117 | + * @return string |
|
118 | + * @since 8.1.0 |
|
119 | + */ |
|
120 | + public static function getChannel() { |
|
121 | + return \OC_Util::getChannel(); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * write a message in the log |
|
126 | + * @param string $app |
|
127 | + * @param string $message |
|
128 | + * @param int $level |
|
129 | + * @since 4.0.0 |
|
130 | + * @deprecated 13.0.0 use log of \OCP\ILogger |
|
131 | + */ |
|
132 | + public static function writeLog($app, $message, $level) { |
|
133 | + $context = ['app' => $app]; |
|
134 | + \OC::$server->getLogger()->log($level, $message, $context); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * check if sharing is disabled for the current user |
|
139 | + * |
|
140 | + * @return boolean |
|
141 | + * @since 7.0.0 |
|
142 | + * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser |
|
143 | + */ |
|
144 | + public static function isSharingDisabledForUser() { |
|
145 | + if (self::$shareManager === null) { |
|
146 | + self::$shareManager = \OC::$server->getShareManager(); |
|
147 | + } |
|
148 | + |
|
149 | + $user = \OC::$server->getUserSession()->getUser(); |
|
150 | + if ($user !== null) { |
|
151 | + $user = $user->getUID(); |
|
152 | + } |
|
153 | + |
|
154 | + return self::$shareManager->sharingDisabledForUser($user); |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * get l10n object |
|
159 | + * @param string $application |
|
160 | + * @param string|null $language |
|
161 | + * @return \OCP\IL10N |
|
162 | + * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
163 | + */ |
|
164 | + public static function getL10N($application, $language = null) { |
|
165 | + return \OC::$server->getL10N($application, $language); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * add a css file |
|
170 | + * @param string $application |
|
171 | + * @param string $file |
|
172 | + * @since 4.0.0 |
|
173 | + */ |
|
174 | + public static function addStyle($application, $file = null) { |
|
175 | + \OC_Util::addStyle($application, $file); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * add a javascript file |
|
180 | + * @param string $application |
|
181 | + * @param string $file |
|
182 | + * @since 4.0.0 |
|
183 | + */ |
|
184 | + public static function addScript($application, $file = null) { |
|
185 | + \OC_Util::addScript($application, $file); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Add a translation JS file |
|
190 | + * @param string $application application id |
|
191 | + * @param string $languageCode language code, defaults to the current locale |
|
192 | + * @since 8.0.0 |
|
193 | + */ |
|
194 | + public static function addTranslations($application, $languageCode = null) { |
|
195 | + \OC_Util::addTranslations($application, $languageCode); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Add a custom element to the header |
|
200 | + * If $text is null then the element will be written as empty element. |
|
201 | + * So use "" to get a closing tag. |
|
202 | + * @param string $tag tag name of the element |
|
203 | + * @param array $attributes array of attributes for the element |
|
204 | + * @param string $text the text content for the element |
|
205 | + * @since 4.0.0 |
|
206 | + */ |
|
207 | + public static function addHeader($tag, $attributes, $text = null) { |
|
208 | + \OC_Util::addHeader($tag, $attributes, $text); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Creates an absolute url to the given app and file. |
|
213 | + * @param string $app app |
|
214 | + * @param string $file file |
|
215 | + * @param array $args array with param=>value, will be appended to the returned url |
|
216 | + * The value of $args will be urlencoded |
|
217 | + * @return string the url |
|
218 | + * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
219 | + */ |
|
220 | + public static function linkToAbsolute($app, $file, $args = []) { |
|
221 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
222 | + return $urlGenerator->getAbsoluteURL( |
|
223 | + $urlGenerator->linkTo($app, $file, $args) |
|
224 | + ); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Creates an absolute url for remote use. |
|
229 | + * @param string $service id |
|
230 | + * @return string the url |
|
231 | + * @since 4.0.0 |
|
232 | + */ |
|
233 | + public static function linkToRemote($service) { |
|
234 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
235 | + $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
236 | + return $urlGenerator->getAbsoluteURL( |
|
237 | + $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
238 | + ); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Creates an absolute url for public use |
|
243 | + * @param string $service id |
|
244 | + * @return string the url |
|
245 | + * @since 4.5.0 |
|
246 | + * @deprecated 15.0.0 - use OCP\IURLGenerator |
|
247 | + */ |
|
248 | + public static function linkToPublic($service) { |
|
249 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
250 | + if ($service === 'files') { |
|
251 | + return $urlGenerator->getAbsoluteURL('/s'); |
|
252 | + } |
|
253 | + return $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'public.php').'?service='.$service); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Returns the server host name without an eventual port number |
|
258 | + * @return string the server hostname |
|
259 | + * @since 5.0.0 |
|
260 | + */ |
|
261 | + public static function getServerHostName() { |
|
262 | + $host_name = \OC::$server->getRequest()->getServerHost(); |
|
263 | + // strip away port number (if existing) |
|
264 | + $colon_pos = strpos($host_name, ':'); |
|
265 | + if ($colon_pos != false) { |
|
266 | + $host_name = substr($host_name, 0, $colon_pos); |
|
267 | + } |
|
268 | + return $host_name; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Returns the default email address |
|
273 | + * @param string $user_part the user part of the address |
|
274 | + * @return string the default email address |
|
275 | + * |
|
276 | + * Assembles a default email address (using the server hostname |
|
277 | + * and the given user part, and returns it |
|
278 | + * Example: when given lostpassword-noreply as $user_part param, |
|
279 | + * and is currently accessed via http(s)://example.com/, |
|
280 | + * it would return '[email protected]' |
|
281 | + * |
|
282 | + * If the configuration value 'mail_from_address' is set in |
|
283 | + * config.php, this value will override the $user_part that |
|
284 | + * is passed to this function |
|
285 | + * @since 5.0.0 |
|
286 | + */ |
|
287 | + public static function getDefaultEmailAddress($user_part) { |
|
288 | + $config = \OC::$server->getConfig(); |
|
289 | + $user_part = $config->getSystemValue('mail_from_address', $user_part); |
|
290 | + $host_name = self::getServerHostName(); |
|
291 | + $host_name = $config->getSystemValue('mail_domain', $host_name); |
|
292 | + $defaultEmailAddress = $user_part.'@'.$host_name; |
|
293 | + |
|
294 | + $mailer = \OC::$server->getMailer(); |
|
295 | + if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
296 | + return $defaultEmailAddress; |
|
297 | + } |
|
298 | + |
|
299 | + // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
300 | + return $user_part.'@localhost.localdomain'; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Make a human file size (2048 to 2 kB) |
|
305 | + * @param int $bytes file size in bytes |
|
306 | + * @return string a human readable file size |
|
307 | + * @since 4.0.0 |
|
308 | + */ |
|
309 | + public static function humanFileSize($bytes) { |
|
310 | + return \OC_Helper::humanFileSize($bytes); |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Make a computer file size (2 kB to 2048) |
|
315 | + * @param string $str file size in a fancy format |
|
316 | + * @return float a file size in bytes |
|
317 | + * |
|
318 | + * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
319 | + * @since 4.0.0 |
|
320 | + */ |
|
321 | + public static function computerFileSize($str) { |
|
322 | + return \OC_Helper::computerFileSize($str); |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * connects a function to a hook |
|
327 | + * |
|
328 | + * @param string $signalClass class name of emitter |
|
329 | + * @param string $signalName name of signal |
|
330 | + * @param string|object $slotClass class name of slot |
|
331 | + * @param string $slotName name of slot |
|
332 | + * @return bool |
|
333 | + * |
|
334 | + * This function makes it very easy to connect to use hooks. |
|
335 | + * |
|
336 | + * TODO: write example |
|
337 | + * @since 4.0.0 |
|
338 | + */ |
|
339 | + public static function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
|
340 | + return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Emits a signal. To get data from the slot use references! |
|
345 | + * @param string $signalclass class name of emitter |
|
346 | + * @param string $signalname name of signal |
|
347 | + * @param array $params default: array() array with additional data |
|
348 | + * @return bool true if slots exists or false if not |
|
349 | + * |
|
350 | + * TODO: write example |
|
351 | + * @since 4.0.0 |
|
352 | + */ |
|
353 | + public static function emitHook($signalclass, $signalname, $params = []) { |
|
354 | + return \OC_Hook::emit($signalclass, $signalname, $params); |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
359 | + * multiple OC_Template elements which invoke `callRegister`. If the value |
|
360 | + * would not be cached these unit-tests would fail. |
|
361 | + * @var string |
|
362 | + */ |
|
363 | + private static $token = ''; |
|
364 | + |
|
365 | + /** |
|
366 | + * Register an get/post call. This is important to prevent CSRF attacks |
|
367 | + * @since 4.5.0 |
|
368 | + */ |
|
369 | + public static function callRegister() { |
|
370 | + if (self::$token === '') { |
|
371 | + self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
|
372 | + } |
|
373 | + return self::$token; |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * Used to sanitize HTML |
|
378 | + * |
|
379 | + * This function is used to sanitize HTML and should be applied on any |
|
380 | + * string or array of strings before displaying it on a web page. |
|
381 | + * |
|
382 | + * @param string|array $value |
|
383 | + * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
384 | + * @since 4.5.0 |
|
385 | + */ |
|
386 | + public static function sanitizeHTML($value) { |
|
387 | + return \OC_Util::sanitizeHTML($value); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Public function to encode url parameters |
|
392 | + * |
|
393 | + * This function is used to encode path to file before output. |
|
394 | + * Encoding is done according to RFC 3986 with one exception: |
|
395 | + * Character '/' is preserved as is. |
|
396 | + * |
|
397 | + * @param string $component part of URI to encode |
|
398 | + * @return string |
|
399 | + * @since 6.0.0 |
|
400 | + */ |
|
401 | + public static function encodePath($component) { |
|
402 | + return \OC_Util::encodePath($component); |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
407 | + * |
|
408 | + * @param array $input The array to work on |
|
409 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
410 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
411 | + * @return array |
|
412 | + * @since 4.5.0 |
|
413 | + */ |
|
414 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
415 | + return \OC_Helper::mb_array_change_key_case($input, $case, $encoding); |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * performs a search in a nested array |
|
420 | + * |
|
421 | + * @param array $haystack the array to be searched |
|
422 | + * @param string $needle the search string |
|
423 | + * @param mixed $index optional, only search this key name |
|
424 | + * @return mixed the key of the matching field, otherwise false |
|
425 | + * @since 4.5.0 |
|
426 | + * @deprecated 15.0.0 |
|
427 | + */ |
|
428 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
429 | + return \OC_Helper::recursiveArraySearch($haystack, $needle, $index); |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
434 | + * |
|
435 | + * @param string $dir the current folder where the user currently operates |
|
436 | + * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
437 | + * @return int number of bytes representing |
|
438 | + * @since 5.0.0 |
|
439 | + */ |
|
440 | + public static function maxUploadFilesize($dir, $free = null) { |
|
441 | + return \OC_Helper::maxUploadFilesize($dir, $free); |
|
442 | + } |
|
443 | + |
|
444 | + /** |
|
445 | + * Calculate free space left within user quota |
|
446 | + * @param string $dir the current folder where the user currently operates |
|
447 | + * @return int number of bytes representing |
|
448 | + * @since 7.0.0 |
|
449 | + */ |
|
450 | + public static function freeSpace($dir) { |
|
451 | + return \OC_Helper::freeSpace($dir); |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Calculate PHP upload limit |
|
456 | + * |
|
457 | + * @return int number of bytes representing |
|
458 | + * @since 7.0.0 |
|
459 | + */ |
|
460 | + public static function uploadLimit() { |
|
461 | + return \OC_Helper::uploadLimit(); |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Returns whether the given file name is valid |
|
466 | + * @param string $file file name to check |
|
467 | + * @return bool true if the file name is valid, false otherwise |
|
468 | + * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
|
469 | + * @since 7.0.0 |
|
470 | + * @suppress PhanDeprecatedFunction |
|
471 | + */ |
|
472 | + public static function isValidFileName($file) { |
|
473 | + return \OC_Util::isValidFileName($file); |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Compare two strings to provide a natural sort |
|
478 | + * @param string $a first string to compare |
|
479 | + * @param string $b second string to compare |
|
480 | + * @return int -1 if $b comes before $a, 1 if $a comes before $b |
|
481 | + * or 0 if the strings are identical |
|
482 | + * @since 7.0.0 |
|
483 | + */ |
|
484 | + public static function naturalSortCompare($a, $b) { |
|
485 | + return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * check if a password is required for each public link |
|
490 | + * @return boolean |
|
491 | + * @since 7.0.0 |
|
492 | + */ |
|
493 | + public static function isPublicLinkPasswordRequired() { |
|
494 | + return \OC_Util::isPublicLinkPasswordRequired(); |
|
495 | + } |
|
496 | + |
|
497 | + /** |
|
498 | + * check if share API enforces a default expire date |
|
499 | + * @return boolean |
|
500 | + * @since 8.0.0 |
|
501 | + */ |
|
502 | + public static function isDefaultExpireDateEnforced() { |
|
503 | + return \OC_Util::isDefaultExpireDateEnforced(); |
|
504 | + } |
|
505 | + |
|
506 | + protected static $needUpgradeCache = null; |
|
507 | + |
|
508 | + /** |
|
509 | + * Checks whether the current version needs upgrade. |
|
510 | + * |
|
511 | + * @return bool true if upgrade is needed, false otherwise |
|
512 | + * @since 7.0.0 |
|
513 | + */ |
|
514 | + public static function needUpgrade() { |
|
515 | + if (!isset(self::$needUpgradeCache)) { |
|
516 | + self::$needUpgradeCache = \OC_Util::needUpgrade(\OC::$server->getSystemConfig()); |
|
517 | + } |
|
518 | + return self::$needUpgradeCache; |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * is this Internet explorer ? |
|
523 | + * |
|
524 | + * @return boolean |
|
525 | + * @since 14.0.0 |
|
526 | + */ |
|
527 | + public static function isIe() { |
|
528 | + return \OC_Util::isIe(); |
|
529 | + } |
|
530 | 530 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | |
79 | 79 | // default responders |
80 | 80 | $this->responders = [ |
81 | - 'json' => function ($data) { |
|
81 | + 'json' => function($data) { |
|
82 | 82 | if ($data instanceof DataResponse) { |
83 | 83 | $response = new JSONResponse( |
84 | 84 | $data->getData(), |
@@ -153,6 +153,6 @@ discard block |
||
153 | 153 | return $responder($response); |
154 | 154 | } |
155 | 155 | throw new \DomainException('No responder registered for format '. |
156 | - $format . '!'); |
|
156 | + $format.'!'); |
|
157 | 157 | } |
158 | 158 | } |
@@ -45,122 +45,122 @@ |
||
45 | 45 | */ |
46 | 46 | abstract class Controller { |
47 | 47 | |
48 | - /** |
|
49 | - * app name |
|
50 | - * @var string |
|
51 | - * @since 7.0.0 |
|
52 | - */ |
|
53 | - protected $appName; |
|
54 | - |
|
55 | - /** |
|
56 | - * current request |
|
57 | - * @var \OCP\IRequest |
|
58 | - * @since 6.0.0 |
|
59 | - */ |
|
60 | - protected $request; |
|
61 | - |
|
62 | - /** |
|
63 | - * @var array |
|
64 | - * @since 7.0.0 |
|
65 | - */ |
|
66 | - private $responders; |
|
67 | - |
|
68 | - /** |
|
69 | - * constructor of the controller |
|
70 | - * @param string $appName the name of the app |
|
71 | - * @param IRequest $request an instance of the request |
|
72 | - * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
73 | - */ |
|
74 | - public function __construct($appName, |
|
75 | - IRequest $request) { |
|
76 | - $this->appName = $appName; |
|
77 | - $this->request = $request; |
|
78 | - |
|
79 | - // default responders |
|
80 | - $this->responders = [ |
|
81 | - 'json' => function ($data) { |
|
82 | - if ($data instanceof DataResponse) { |
|
83 | - $response = new JSONResponse( |
|
84 | - $data->getData(), |
|
85 | - $data->getStatus() |
|
86 | - ); |
|
87 | - $dataHeaders = $data->getHeaders(); |
|
88 | - $headers = $response->getHeaders(); |
|
89 | - // do not overwrite Content-Type if it already exists |
|
90 | - if (isset($dataHeaders['Content-Type'])) { |
|
91 | - unset($headers['Content-Type']); |
|
92 | - } |
|
93 | - $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
94 | - |
|
95 | - if ($data->getETag() !== null) { |
|
96 | - $response->setETag($data->getETag()); |
|
97 | - } |
|
98 | - if ($data->getLastModified() !== null) { |
|
99 | - $response->setLastModified($data->getLastModified()); |
|
100 | - } |
|
101 | - |
|
102 | - return $response; |
|
103 | - } |
|
104 | - return new JSONResponse($data); |
|
105 | - } |
|
106 | - ]; |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * Parses an HTTP accept header and returns the supported responder type |
|
112 | - * @param string $acceptHeader |
|
113 | - * @param string $default |
|
114 | - * @return string the responder type |
|
115 | - * @since 7.0.0 |
|
116 | - * @since 9.1.0 Added default parameter |
|
117 | - */ |
|
118 | - public function getResponderByHTTPHeader($acceptHeader, $default = 'json') { |
|
119 | - $headers = explode(',', $acceptHeader); |
|
120 | - |
|
121 | - // return the first matching responder |
|
122 | - foreach ($headers as $header) { |
|
123 | - $header = strtolower(trim($header)); |
|
124 | - |
|
125 | - $responder = str_replace('application/', '', $header); |
|
126 | - |
|
127 | - if (array_key_exists($responder, $this->responders)) { |
|
128 | - return $responder; |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - // no matching header return default |
|
133 | - return $default; |
|
134 | - } |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * Registers a formatter for a type |
|
139 | - * @param string $format |
|
140 | - * @param \Closure $responder |
|
141 | - * @since 7.0.0 |
|
142 | - */ |
|
143 | - protected function registerResponder($format, \Closure $responder) { |
|
144 | - $this->responders[$format] = $responder; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * Serializes and formats a response |
|
150 | - * @param mixed $response the value that was returned from a controller and |
|
151 | - * is not a Response instance |
|
152 | - * @param string $format the format for which a formatter has been registered |
|
153 | - * @throws \DomainException if format does not match a registered formatter |
|
154 | - * @return Response |
|
155 | - * @since 7.0.0 |
|
156 | - */ |
|
157 | - public function buildResponse($response, $format = 'json') { |
|
158 | - if (array_key_exists($format, $this->responders)) { |
|
159 | - $responder = $this->responders[$format]; |
|
160 | - |
|
161 | - return $responder($response); |
|
162 | - } |
|
163 | - throw new \DomainException('No responder registered for format '. |
|
164 | - $format . '!'); |
|
165 | - } |
|
48 | + /** |
|
49 | + * app name |
|
50 | + * @var string |
|
51 | + * @since 7.0.0 |
|
52 | + */ |
|
53 | + protected $appName; |
|
54 | + |
|
55 | + /** |
|
56 | + * current request |
|
57 | + * @var \OCP\IRequest |
|
58 | + * @since 6.0.0 |
|
59 | + */ |
|
60 | + protected $request; |
|
61 | + |
|
62 | + /** |
|
63 | + * @var array |
|
64 | + * @since 7.0.0 |
|
65 | + */ |
|
66 | + private $responders; |
|
67 | + |
|
68 | + /** |
|
69 | + * constructor of the controller |
|
70 | + * @param string $appName the name of the app |
|
71 | + * @param IRequest $request an instance of the request |
|
72 | + * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
73 | + */ |
|
74 | + public function __construct($appName, |
|
75 | + IRequest $request) { |
|
76 | + $this->appName = $appName; |
|
77 | + $this->request = $request; |
|
78 | + |
|
79 | + // default responders |
|
80 | + $this->responders = [ |
|
81 | + 'json' => function ($data) { |
|
82 | + if ($data instanceof DataResponse) { |
|
83 | + $response = new JSONResponse( |
|
84 | + $data->getData(), |
|
85 | + $data->getStatus() |
|
86 | + ); |
|
87 | + $dataHeaders = $data->getHeaders(); |
|
88 | + $headers = $response->getHeaders(); |
|
89 | + // do not overwrite Content-Type if it already exists |
|
90 | + if (isset($dataHeaders['Content-Type'])) { |
|
91 | + unset($headers['Content-Type']); |
|
92 | + } |
|
93 | + $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
94 | + |
|
95 | + if ($data->getETag() !== null) { |
|
96 | + $response->setETag($data->getETag()); |
|
97 | + } |
|
98 | + if ($data->getLastModified() !== null) { |
|
99 | + $response->setLastModified($data->getLastModified()); |
|
100 | + } |
|
101 | + |
|
102 | + return $response; |
|
103 | + } |
|
104 | + return new JSONResponse($data); |
|
105 | + } |
|
106 | + ]; |
|
107 | + } |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * Parses an HTTP accept header and returns the supported responder type |
|
112 | + * @param string $acceptHeader |
|
113 | + * @param string $default |
|
114 | + * @return string the responder type |
|
115 | + * @since 7.0.0 |
|
116 | + * @since 9.1.0 Added default parameter |
|
117 | + */ |
|
118 | + public function getResponderByHTTPHeader($acceptHeader, $default = 'json') { |
|
119 | + $headers = explode(',', $acceptHeader); |
|
120 | + |
|
121 | + // return the first matching responder |
|
122 | + foreach ($headers as $header) { |
|
123 | + $header = strtolower(trim($header)); |
|
124 | + |
|
125 | + $responder = str_replace('application/', '', $header); |
|
126 | + |
|
127 | + if (array_key_exists($responder, $this->responders)) { |
|
128 | + return $responder; |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + // no matching header return default |
|
133 | + return $default; |
|
134 | + } |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * Registers a formatter for a type |
|
139 | + * @param string $format |
|
140 | + * @param \Closure $responder |
|
141 | + * @since 7.0.0 |
|
142 | + */ |
|
143 | + protected function registerResponder($format, \Closure $responder) { |
|
144 | + $this->responders[$format] = $responder; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * Serializes and formats a response |
|
150 | + * @param mixed $response the value that was returned from a controller and |
|
151 | + * is not a Response instance |
|
152 | + * @param string $format the format for which a formatter has been registered |
|
153 | + * @throws \DomainException if format does not match a registered formatter |
|
154 | + * @return Response |
|
155 | + * @since 7.0.0 |
|
156 | + */ |
|
157 | + public function buildResponse($response, $format = 'json') { |
|
158 | + if (array_key_exists($format, $this->responders)) { |
|
159 | + $responder = $this->responders[$format]; |
|
160 | + |
|
161 | + return $responder($response); |
|
162 | + } |
|
163 | + throw new \DomainException('No responder registered for format '. |
|
164 | + $format . '!'); |
|
165 | + } |
|
166 | 166 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | $this->file = $file; |
52 | 52 | $this->setStatus($statusCode); |
53 | 53 | $this->setHeaders(array_merge($this->getHeaders(), $headers)); |
54 | - $this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"'); |
|
54 | + $this->addHeader('Content-Disposition', 'inline; filename="'.rawurldecode($file->getName()).'"'); |
|
55 | 55 | |
56 | 56 | $this->setETag($file->getEtag()); |
57 | 57 | $lastModified = new \DateTime(); |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function callback(IOutput $output) { |
67 | 67 | if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) { |
68 | - $output->setHeader('Content-Length: ' . $this->file->getSize()); |
|
68 | + $output->setHeader('Content-Length: '.$this->file->getSize()); |
|
69 | 69 | $output->setOutput($this->file->getContent()); |
70 | 70 | } |
71 | 71 | } |
@@ -32,40 +32,40 @@ |
||
32 | 32 | */ |
33 | 33 | class FileDisplayResponse extends Response implements ICallbackResponse { |
34 | 34 | |
35 | - /** @var \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile */ |
|
36 | - private $file; |
|
35 | + /** @var \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile */ |
|
36 | + private $file; |
|
37 | 37 | |
38 | - /** |
|
39 | - * FileDisplayResponse constructor. |
|
40 | - * |
|
41 | - * @param \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile $file |
|
42 | - * @param int $statusCode |
|
43 | - * @param array $headers |
|
44 | - * @since 11.0.0 |
|
45 | - */ |
|
46 | - public function __construct($file, $statusCode = Http::STATUS_OK, |
|
47 | - $headers = []) { |
|
48 | - parent::__construct(); |
|
38 | + /** |
|
39 | + * FileDisplayResponse constructor. |
|
40 | + * |
|
41 | + * @param \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile $file |
|
42 | + * @param int $statusCode |
|
43 | + * @param array $headers |
|
44 | + * @since 11.0.0 |
|
45 | + */ |
|
46 | + public function __construct($file, $statusCode = Http::STATUS_OK, |
|
47 | + $headers = []) { |
|
48 | + parent::__construct(); |
|
49 | 49 | |
50 | - $this->file = $file; |
|
51 | - $this->setStatus($statusCode); |
|
52 | - $this->setHeaders(array_merge($this->getHeaders(), $headers)); |
|
53 | - $this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"'); |
|
50 | + $this->file = $file; |
|
51 | + $this->setStatus($statusCode); |
|
52 | + $this->setHeaders(array_merge($this->getHeaders(), $headers)); |
|
53 | + $this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"'); |
|
54 | 54 | |
55 | - $this->setETag($file->getEtag()); |
|
56 | - $lastModified = new \DateTime(); |
|
57 | - $lastModified->setTimestamp($file->getMTime()); |
|
58 | - $this->setLastModified($lastModified); |
|
59 | - } |
|
55 | + $this->setETag($file->getEtag()); |
|
56 | + $lastModified = new \DateTime(); |
|
57 | + $lastModified->setTimestamp($file->getMTime()); |
|
58 | + $this->setLastModified($lastModified); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param IOutput $output |
|
63 | - * @since 11.0.0 |
|
64 | - */ |
|
65 | - public function callback(IOutput $output) { |
|
66 | - if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) { |
|
67 | - $output->setHeader('Content-Length: ' . $this->file->getSize()); |
|
68 | - $output->setOutput($this->file->getContent()); |
|
69 | - } |
|
70 | - } |
|
61 | + /** |
|
62 | + * @param IOutput $output |
|
63 | + * @since 11.0.0 |
|
64 | + */ |
|
65 | + public function callback(IOutput $output) { |
|
66 | + if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) { |
|
67 | + $output->setHeader('Content-Length: ' . $this->file->getSize()); |
|
68 | + $output->setOutput($this->file->getContent()); |
|
69 | + } |
|
70 | + } |
|
71 | 71 | } |
@@ -68,7 +68,7 @@ |
||
68 | 68 | public function render() { |
69 | 69 | $response = json_encode($this->data, JSON_HEX_TAG); |
70 | 70 | if ($response === false) { |
71 | - throw new \Exception(sprintf('Could not json_encode due to invalid ' . |
|
71 | + throw new \Exception(sprintf('Could not json_encode due to invalid '. |
|
72 | 72 | 'non UTF-8 characters in the array: %s', var_export($this->data, true))); |
73 | 73 | } |
74 | 74 |
@@ -41,64 +41,64 @@ |
||
41 | 41 | */ |
42 | 42 | class JSONResponse extends Response { |
43 | 43 | |
44 | - /** |
|
45 | - * response data |
|
46 | - * @var array|object |
|
47 | - */ |
|
48 | - protected $data; |
|
44 | + /** |
|
45 | + * response data |
|
46 | + * @var array|object |
|
47 | + */ |
|
48 | + protected $data; |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * constructor of JSONResponse |
|
53 | - * @param array|object $data the object or array that should be transformed |
|
54 | - * @param int $statusCode the Http status code, defaults to 200 |
|
55 | - * @since 6.0.0 |
|
56 | - */ |
|
57 | - public function __construct($data = [], $statusCode = Http::STATUS_OK) { |
|
58 | - parent::__construct(); |
|
51 | + /** |
|
52 | + * constructor of JSONResponse |
|
53 | + * @param array|object $data the object or array that should be transformed |
|
54 | + * @param int $statusCode the Http status code, defaults to 200 |
|
55 | + * @since 6.0.0 |
|
56 | + */ |
|
57 | + public function __construct($data = [], $statusCode = Http::STATUS_OK) { |
|
58 | + parent::__construct(); |
|
59 | 59 | |
60 | - $this->data = $data; |
|
61 | - $this->setStatus($statusCode); |
|
62 | - $this->addHeader('Content-Type', 'application/json; charset=utf-8'); |
|
63 | - } |
|
60 | + $this->data = $data; |
|
61 | + $this->setStatus($statusCode); |
|
62 | + $this->addHeader('Content-Type', 'application/json; charset=utf-8'); |
|
63 | + } |
|
64 | 64 | |
65 | 65 | |
66 | - /** |
|
67 | - * Returns the rendered json |
|
68 | - * @return string the rendered json |
|
69 | - * @since 6.0.0 |
|
70 | - * @throws \Exception If data could not get encoded |
|
71 | - */ |
|
72 | - public function render() { |
|
73 | - $response = json_encode($this->data, JSON_HEX_TAG); |
|
74 | - if ($response === false) { |
|
75 | - throw new \Exception(sprintf('Could not json_encode due to invalid ' . |
|
76 | - 'non UTF-8 characters in the array: %s', var_export($this->data, true))); |
|
77 | - } |
|
66 | + /** |
|
67 | + * Returns the rendered json |
|
68 | + * @return string the rendered json |
|
69 | + * @since 6.0.0 |
|
70 | + * @throws \Exception If data could not get encoded |
|
71 | + */ |
|
72 | + public function render() { |
|
73 | + $response = json_encode($this->data, JSON_HEX_TAG); |
|
74 | + if ($response === false) { |
|
75 | + throw new \Exception(sprintf('Could not json_encode due to invalid ' . |
|
76 | + 'non UTF-8 characters in the array: %s', var_export($this->data, true))); |
|
77 | + } |
|
78 | 78 | |
79 | - return $response; |
|
80 | - } |
|
79 | + return $response; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Sets values in the data json array |
|
84 | - * @param array|object $data an array or object which will be transformed |
|
85 | - * to JSON |
|
86 | - * @return JSONResponse Reference to this object |
|
87 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
88 | - */ |
|
89 | - public function setData($data) { |
|
90 | - $this->data = $data; |
|
82 | + /** |
|
83 | + * Sets values in the data json array |
|
84 | + * @param array|object $data an array or object which will be transformed |
|
85 | + * to JSON |
|
86 | + * @return JSONResponse Reference to this object |
|
87 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
88 | + */ |
|
89 | + public function setData($data) { |
|
90 | + $this->data = $data; |
|
91 | 91 | |
92 | - return $this; |
|
93 | - } |
|
92 | + return $this; |
|
93 | + } |
|
94 | 94 | |
95 | 95 | |
96 | - /** |
|
97 | - * Used to get the set parameters |
|
98 | - * @return array the data |
|
99 | - * @since 6.0.0 |
|
100 | - */ |
|
101 | - public function getData() { |
|
102 | - return $this->data; |
|
103 | - } |
|
96 | + /** |
|
97 | + * Used to get the set parameters |
|
98 | + * @return array the data |
|
99 | + * @since 6.0.0 |
|
100 | + */ |
|
101 | + public function getData() { |
|
102 | + return $this->data; |
|
103 | + } |
|
104 | 104 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | $result = $this->zip->addFromString($path, $source); |
73 | 73 | } |
74 | 74 | if ($result) { |
75 | - $this->zip->close();//close and reopen to save the zip |
|
75 | + $this->zip->close(); //close and reopen to save the zip |
|
76 | 76 | $this->zip->open($this->path); |
77 | 77 | } |
78 | 78 | return $result; |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | public function getFiles() { |
131 | 131 | $fileCount = $this->zip->numFiles; |
132 | 132 | $files = []; |
133 | - for ($i = 0;$i < $fileCount;$i++) { |
|
133 | + for ($i = 0; $i < $fileCount; $i++) { |
|
134 | 134 | $files[] = $this->zip->getNameIndex($i); |
135 | 135 | } |
136 | 136 | return $files; |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | $this->extractFile($path, $tmpFile); |
205 | 205 | } |
206 | 206 | $handle = fopen($tmpFile, $mode); |
207 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
207 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
208 | 208 | $this->writeBack($tmpFile, $path); |
209 | 209 | }); |
210 | 210 | } |
@@ -36,199 +36,199 @@ |
||
36 | 36 | use OCP\ILogger; |
37 | 37 | |
38 | 38 | class ZIP extends Archive { |
39 | - /** |
|
40 | - * @var \ZipArchive zip |
|
41 | - */ |
|
42 | - private $zip = null; |
|
43 | - private $path; |
|
39 | + /** |
|
40 | + * @var \ZipArchive zip |
|
41 | + */ |
|
42 | + private $zip = null; |
|
43 | + private $path; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param string $source |
|
47 | - */ |
|
48 | - public function __construct($source) { |
|
49 | - $this->path = $source; |
|
50 | - $this->zip = new \ZipArchive(); |
|
51 | - if ($this->zip->open($source, \ZipArchive::CREATE)) { |
|
52 | - } else { |
|
53 | - \OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN); |
|
54 | - } |
|
55 | - } |
|
56 | - /** |
|
57 | - * add an empty folder to the archive |
|
58 | - * @param string $path |
|
59 | - * @return bool |
|
60 | - */ |
|
61 | - public function addFolder($path) { |
|
62 | - return $this->zip->addEmptyDir($path); |
|
63 | - } |
|
64 | - /** |
|
65 | - * add a file to the archive |
|
66 | - * @param string $path |
|
67 | - * @param string $source either a local file or string data |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - public function addFile($path, $source = '') { |
|
71 | - if ($source and $source[0] == '/' and file_exists($source)) { |
|
72 | - $result = $this->zip->addFile($source, $path); |
|
73 | - } else { |
|
74 | - $result = $this->zip->addFromString($path, $source); |
|
75 | - } |
|
76 | - if ($result) { |
|
77 | - $this->zip->close();//close and reopen to save the zip |
|
78 | - $this->zip->open($this->path); |
|
79 | - } |
|
80 | - return $result; |
|
81 | - } |
|
82 | - /** |
|
83 | - * rename a file or folder in the archive |
|
84 | - * @param string $source |
|
85 | - * @param string $dest |
|
86 | - * @return boolean|null |
|
87 | - */ |
|
88 | - public function rename($source, $dest) { |
|
89 | - $source = $this->stripPath($source); |
|
90 | - $dest = $this->stripPath($dest); |
|
91 | - $this->zip->renameName($source, $dest); |
|
92 | - } |
|
93 | - /** |
|
94 | - * get the uncompressed size of a file in the archive |
|
95 | - * @param string $path |
|
96 | - * @return int |
|
97 | - */ |
|
98 | - public function filesize($path) { |
|
99 | - $stat = $this->zip->statName($path); |
|
100 | - return $stat['size']; |
|
101 | - } |
|
102 | - /** |
|
103 | - * get the last modified time of a file in the archive |
|
104 | - * @param string $path |
|
105 | - * @return int |
|
106 | - */ |
|
107 | - public function mtime($path) { |
|
108 | - return filemtime($this->path); |
|
109 | - } |
|
110 | - /** |
|
111 | - * get the files in a folder |
|
112 | - * @param string $path |
|
113 | - * @return array |
|
114 | - */ |
|
115 | - public function getFolder($path) { |
|
116 | - $files = $this->getFiles(); |
|
117 | - $folderContent = []; |
|
118 | - $pathLength = strlen($path); |
|
119 | - foreach ($files as $file) { |
|
120 | - if (substr($file, 0, $pathLength) == $path and $file != $path) { |
|
121 | - if (strrpos(substr($file, 0, -1), '/') <= $pathLength) { |
|
122 | - $folderContent[] = substr($file, $pathLength); |
|
123 | - } |
|
124 | - } |
|
125 | - } |
|
126 | - return $folderContent; |
|
127 | - } |
|
128 | - /** |
|
129 | - * get all files in the archive |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - public function getFiles() { |
|
133 | - $fileCount = $this->zip->numFiles; |
|
134 | - $files = []; |
|
135 | - for ($i = 0;$i < $fileCount;$i++) { |
|
136 | - $files[] = $this->zip->getNameIndex($i); |
|
137 | - } |
|
138 | - return $files; |
|
139 | - } |
|
140 | - /** |
|
141 | - * get the content of a file |
|
142 | - * @param string $path |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function getFile($path) { |
|
146 | - return $this->zip->getFromName($path); |
|
147 | - } |
|
148 | - /** |
|
149 | - * extract a single file from the archive |
|
150 | - * @param string $path |
|
151 | - * @param string $dest |
|
152 | - * @return boolean|null |
|
153 | - */ |
|
154 | - public function extractFile($path, $dest) { |
|
155 | - $fp = $this->zip->getStream($path); |
|
156 | - file_put_contents($dest, $fp); |
|
157 | - } |
|
158 | - /** |
|
159 | - * extract the archive |
|
160 | - * @param string $dest |
|
161 | - * @return bool |
|
162 | - */ |
|
163 | - public function extract($dest) { |
|
164 | - return $this->zip->extractTo($dest); |
|
165 | - } |
|
166 | - /** |
|
167 | - * check if a file or folder exists in the archive |
|
168 | - * @param string $path |
|
169 | - * @return bool |
|
170 | - */ |
|
171 | - public function fileExists($path) { |
|
172 | - return ($this->zip->locateName($path) !== false) or ($this->zip->locateName($path.'/') !== false); |
|
173 | - } |
|
174 | - /** |
|
175 | - * remove a file or folder from the archive |
|
176 | - * @param string $path |
|
177 | - * @return bool |
|
178 | - */ |
|
179 | - public function remove($path) { |
|
180 | - if ($this->fileExists($path.'/')) { |
|
181 | - return $this->zip->deleteName($path.'/'); |
|
182 | - } else { |
|
183 | - return $this->zip->deleteName($path); |
|
184 | - } |
|
185 | - } |
|
186 | - /** |
|
187 | - * get a file handler |
|
188 | - * @param string $path |
|
189 | - * @param string $mode |
|
190 | - * @return resource |
|
191 | - */ |
|
192 | - public function getStream($path, $mode) { |
|
193 | - if ($mode == 'r' or $mode == 'rb') { |
|
194 | - return $this->zip->getStream($path); |
|
195 | - } else { |
|
196 | - //since we can't directly get a writable stream, |
|
197 | - //make a temp copy of the file and put it back |
|
198 | - //in the archive when the stream is closed |
|
199 | - if (strrpos($path, '.') !== false) { |
|
200 | - $ext = substr($path, strrpos($path, '.')); |
|
201 | - } else { |
|
202 | - $ext = ''; |
|
203 | - } |
|
204 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
205 | - if ($this->fileExists($path)) { |
|
206 | - $this->extractFile($path, $tmpFile); |
|
207 | - } |
|
208 | - $handle = fopen($tmpFile, $mode); |
|
209 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
210 | - $this->writeBack($tmpFile, $path); |
|
211 | - }); |
|
212 | - } |
|
213 | - } |
|
45 | + /** |
|
46 | + * @param string $source |
|
47 | + */ |
|
48 | + public function __construct($source) { |
|
49 | + $this->path = $source; |
|
50 | + $this->zip = new \ZipArchive(); |
|
51 | + if ($this->zip->open($source, \ZipArchive::CREATE)) { |
|
52 | + } else { |
|
53 | + \OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN); |
|
54 | + } |
|
55 | + } |
|
56 | + /** |
|
57 | + * add an empty folder to the archive |
|
58 | + * @param string $path |
|
59 | + * @return bool |
|
60 | + */ |
|
61 | + public function addFolder($path) { |
|
62 | + return $this->zip->addEmptyDir($path); |
|
63 | + } |
|
64 | + /** |
|
65 | + * add a file to the archive |
|
66 | + * @param string $path |
|
67 | + * @param string $source either a local file or string data |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + public function addFile($path, $source = '') { |
|
71 | + if ($source and $source[0] == '/' and file_exists($source)) { |
|
72 | + $result = $this->zip->addFile($source, $path); |
|
73 | + } else { |
|
74 | + $result = $this->zip->addFromString($path, $source); |
|
75 | + } |
|
76 | + if ($result) { |
|
77 | + $this->zip->close();//close and reopen to save the zip |
|
78 | + $this->zip->open($this->path); |
|
79 | + } |
|
80 | + return $result; |
|
81 | + } |
|
82 | + /** |
|
83 | + * rename a file or folder in the archive |
|
84 | + * @param string $source |
|
85 | + * @param string $dest |
|
86 | + * @return boolean|null |
|
87 | + */ |
|
88 | + public function rename($source, $dest) { |
|
89 | + $source = $this->stripPath($source); |
|
90 | + $dest = $this->stripPath($dest); |
|
91 | + $this->zip->renameName($source, $dest); |
|
92 | + } |
|
93 | + /** |
|
94 | + * get the uncompressed size of a file in the archive |
|
95 | + * @param string $path |
|
96 | + * @return int |
|
97 | + */ |
|
98 | + public function filesize($path) { |
|
99 | + $stat = $this->zip->statName($path); |
|
100 | + return $stat['size']; |
|
101 | + } |
|
102 | + /** |
|
103 | + * get the last modified time of a file in the archive |
|
104 | + * @param string $path |
|
105 | + * @return int |
|
106 | + */ |
|
107 | + public function mtime($path) { |
|
108 | + return filemtime($this->path); |
|
109 | + } |
|
110 | + /** |
|
111 | + * get the files in a folder |
|
112 | + * @param string $path |
|
113 | + * @return array |
|
114 | + */ |
|
115 | + public function getFolder($path) { |
|
116 | + $files = $this->getFiles(); |
|
117 | + $folderContent = []; |
|
118 | + $pathLength = strlen($path); |
|
119 | + foreach ($files as $file) { |
|
120 | + if (substr($file, 0, $pathLength) == $path and $file != $path) { |
|
121 | + if (strrpos(substr($file, 0, -1), '/') <= $pathLength) { |
|
122 | + $folderContent[] = substr($file, $pathLength); |
|
123 | + } |
|
124 | + } |
|
125 | + } |
|
126 | + return $folderContent; |
|
127 | + } |
|
128 | + /** |
|
129 | + * get all files in the archive |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + public function getFiles() { |
|
133 | + $fileCount = $this->zip->numFiles; |
|
134 | + $files = []; |
|
135 | + for ($i = 0;$i < $fileCount;$i++) { |
|
136 | + $files[] = $this->zip->getNameIndex($i); |
|
137 | + } |
|
138 | + return $files; |
|
139 | + } |
|
140 | + /** |
|
141 | + * get the content of a file |
|
142 | + * @param string $path |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function getFile($path) { |
|
146 | + return $this->zip->getFromName($path); |
|
147 | + } |
|
148 | + /** |
|
149 | + * extract a single file from the archive |
|
150 | + * @param string $path |
|
151 | + * @param string $dest |
|
152 | + * @return boolean|null |
|
153 | + */ |
|
154 | + public function extractFile($path, $dest) { |
|
155 | + $fp = $this->zip->getStream($path); |
|
156 | + file_put_contents($dest, $fp); |
|
157 | + } |
|
158 | + /** |
|
159 | + * extract the archive |
|
160 | + * @param string $dest |
|
161 | + * @return bool |
|
162 | + */ |
|
163 | + public function extract($dest) { |
|
164 | + return $this->zip->extractTo($dest); |
|
165 | + } |
|
166 | + /** |
|
167 | + * check if a file or folder exists in the archive |
|
168 | + * @param string $path |
|
169 | + * @return bool |
|
170 | + */ |
|
171 | + public function fileExists($path) { |
|
172 | + return ($this->zip->locateName($path) !== false) or ($this->zip->locateName($path.'/') !== false); |
|
173 | + } |
|
174 | + /** |
|
175 | + * remove a file or folder from the archive |
|
176 | + * @param string $path |
|
177 | + * @return bool |
|
178 | + */ |
|
179 | + public function remove($path) { |
|
180 | + if ($this->fileExists($path.'/')) { |
|
181 | + return $this->zip->deleteName($path.'/'); |
|
182 | + } else { |
|
183 | + return $this->zip->deleteName($path); |
|
184 | + } |
|
185 | + } |
|
186 | + /** |
|
187 | + * get a file handler |
|
188 | + * @param string $path |
|
189 | + * @param string $mode |
|
190 | + * @return resource |
|
191 | + */ |
|
192 | + public function getStream($path, $mode) { |
|
193 | + if ($mode == 'r' or $mode == 'rb') { |
|
194 | + return $this->zip->getStream($path); |
|
195 | + } else { |
|
196 | + //since we can't directly get a writable stream, |
|
197 | + //make a temp copy of the file and put it back |
|
198 | + //in the archive when the stream is closed |
|
199 | + if (strrpos($path, '.') !== false) { |
|
200 | + $ext = substr($path, strrpos($path, '.')); |
|
201 | + } else { |
|
202 | + $ext = ''; |
|
203 | + } |
|
204 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
205 | + if ($this->fileExists($path)) { |
|
206 | + $this->extractFile($path, $tmpFile); |
|
207 | + } |
|
208 | + $handle = fopen($tmpFile, $mode); |
|
209 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
210 | + $this->writeBack($tmpFile, $path); |
|
211 | + }); |
|
212 | + } |
|
213 | + } |
|
214 | 214 | |
215 | - /** |
|
216 | - * write back temporary files |
|
217 | - */ |
|
218 | - public function writeBack($tmpFile, $path) { |
|
219 | - $this->addFile($path, $tmpFile); |
|
220 | - unlink($tmpFile); |
|
221 | - } |
|
215 | + /** |
|
216 | + * write back temporary files |
|
217 | + */ |
|
218 | + public function writeBack($tmpFile, $path) { |
|
219 | + $this->addFile($path, $tmpFile); |
|
220 | + unlink($tmpFile); |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * @param string $path |
|
225 | - * @return string |
|
226 | - */ |
|
227 | - private function stripPath($path) { |
|
228 | - if (!$path || $path[0] == '/') { |
|
229 | - return substr($path, 1); |
|
230 | - } else { |
|
231 | - return $path; |
|
232 | - } |
|
233 | - } |
|
223 | + /** |
|
224 | + * @param string $path |
|
225 | + * @return string |
|
226 | + */ |
|
227 | + private function stripPath($path) { |
|
228 | + if (!$path || $path[0] == '/') { |
|
229 | + return substr($path, 1); |
|
230 | + } else { |
|
231 | + return $path; |
|
232 | + } |
|
233 | + } |
|
234 | 234 | } |
@@ -214,28 +214,28 @@ discard block |
||
214 | 214 | $alias = strtolower($alias); |
215 | 215 | |
216 | 216 | foreach ($this->blackListedClassNames as $blackListedAlias => $blackListedClassName) { |
217 | - if (strpos($blackListedClassName, $name . '\\') === 0) { |
|
217 | + if (strpos($blackListedClassName, $name.'\\') === 0) { |
|
218 | 218 | $aliasedClassName = str_replace($name, $alias, $blackListedClassName); |
219 | 219 | $this->blackListedClassNames[$aliasedClassName] = $blackListedClassName; |
220 | 220 | } |
221 | 221 | } |
222 | 222 | |
223 | 223 | foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) { |
224 | - if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) { |
|
224 | + if (strpos($blackListedConstant, $name.'\\') === 0 || strpos($blackListedConstant, $name.'::') === 0) { |
|
225 | 225 | $aliasedConstantName = str_replace($name, $alias, $blackListedConstant); |
226 | 226 | $this->blackListedConstants[$aliasedConstantName] = $blackListedConstant; |
227 | 227 | } |
228 | 228 | } |
229 | 229 | |
230 | 230 | foreach ($this->blackListedFunctions as $blackListedAlias => $blackListedFunction) { |
231 | - if (strpos($blackListedFunction, $name . '\\') === 0 || strpos($blackListedFunction, $name . '::') === 0) { |
|
231 | + if (strpos($blackListedFunction, $name.'\\') === 0 || strpos($blackListedFunction, $name.'::') === 0) { |
|
232 | 232 | $aliasedFunctionName = str_replace($name, $alias, $blackListedFunction); |
233 | 233 | $this->blackListedFunctions[$aliasedFunctionName] = $blackListedFunction; |
234 | 234 | } |
235 | 235 | } |
236 | 236 | |
237 | 237 | foreach ($this->blackListedMethods as $blackListedAlias => $blackListedMethod) { |
238 | - if (strpos($blackListedMethod, $name . '\\') === 0 || strpos($blackListedMethod, $name . '::') === 0) { |
|
238 | + if (strpos($blackListedMethod, $name.'\\') === 0 || strpos($blackListedMethod, $name.'::') === 0) { |
|
239 | 239 | $aliasedMethodName = str_replace($name, $alias, $blackListedMethod); |
240 | 240 | $this->blackListedMethods[$aliasedMethodName] = $blackListedMethod; |
241 | 241 | } |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | } |
257 | 257 | |
258 | 258 | private function checkBlackListConstant($class, $constantName, Node $node) { |
259 | - $name = $class . '::' . $constantName; |
|
259 | + $name = $class.'::'.$constantName; |
|
260 | 260 | $lowerName = strtolower($name); |
261 | 261 | |
262 | 262 | if (isset($this->blackListedConstants[$lowerName])) { |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | } |
271 | 271 | |
272 | 272 | private function checkBlackListFunction($class, $functionName, Node $node) { |
273 | - $name = $class . '::' . $functionName; |
|
273 | + $name = $class.'::'.$functionName; |
|
274 | 274 | $lowerName = strtolower($name); |
275 | 275 | |
276 | 276 | if (isset($this->blackListedFunctions[$lowerName])) { |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | } |
285 | 285 | |
286 | 286 | private function checkBlackListMethod($class, $functionName, Node $node) { |
287 | - $name = $class . '::' . $functionName; |
|
287 | + $name = $class.'::'.$functionName; |
|
288 | 288 | $lowerName = strtolower($name); |
289 | 289 | |
290 | 290 | if (isset($this->blackListedMethods[$lowerName])) { |
@@ -30,280 +30,280 @@ |
||
30 | 30 | use PhpParser\NodeVisitorAbstract; |
31 | 31 | |
32 | 32 | class NodeVisitor extends NodeVisitorAbstract { |
33 | - /** @var ICheck */ |
|
34 | - protected $list; |
|
35 | - |
|
36 | - /** @var string */ |
|
37 | - protected $blackListDescription; |
|
38 | - /** @var string[] */ |
|
39 | - protected $blackListedClassNames; |
|
40 | - /** @var string[] */ |
|
41 | - protected $blackListedConstants; |
|
42 | - /** @var string[] */ |
|
43 | - protected $blackListedFunctions; |
|
44 | - /** @var string[] */ |
|
45 | - protected $blackListedMethods; |
|
46 | - /** @var bool */ |
|
47 | - protected $checkEqualOperatorUsage; |
|
48 | - /** @var string[] */ |
|
49 | - protected $errorMessages; |
|
50 | - |
|
51 | - /** |
|
52 | - * @param ICheck $list |
|
53 | - */ |
|
54 | - public function __construct(ICheck $list) { |
|
55 | - $this->list = $list; |
|
56 | - |
|
57 | - $this->blackListedClassNames = []; |
|
58 | - foreach ($list->getClasses() as $class => $blackListInfo) { |
|
59 | - if (is_numeric($class) && is_string($blackListInfo)) { |
|
60 | - $class = $blackListInfo; |
|
61 | - $blackListInfo = null; |
|
62 | - } |
|
63 | - |
|
64 | - $class = strtolower($class); |
|
65 | - $this->blackListedClassNames[$class] = $class; |
|
66 | - } |
|
67 | - |
|
68 | - $this->blackListedConstants = []; |
|
69 | - foreach ($list->getConstants() as $constantName => $blackListInfo) { |
|
70 | - $constantName = strtolower($constantName); |
|
71 | - $this->blackListedConstants[$constantName] = $constantName; |
|
72 | - } |
|
73 | - |
|
74 | - $this->blackListedFunctions = []; |
|
75 | - foreach ($list->getFunctions() as $functionName => $blackListInfo) { |
|
76 | - $functionName = strtolower($functionName); |
|
77 | - $this->blackListedFunctions[$functionName] = $functionName; |
|
78 | - } |
|
79 | - |
|
80 | - $this->blackListedMethods = []; |
|
81 | - foreach ($list->getMethods() as $functionName => $blackListInfo) { |
|
82 | - $functionName = strtolower($functionName); |
|
83 | - $this->blackListedMethods[$functionName] = $functionName; |
|
84 | - } |
|
85 | - |
|
86 | - $this->checkEqualOperatorUsage = $list->checkStrongComparisons(); |
|
87 | - |
|
88 | - $this->errorMessages = [ |
|
89 | - CodeChecker::CLASS_EXTENDS_NOT_ALLOWED => "%s class must not be extended", |
|
90 | - CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED => "%s interface must not be implemented", |
|
91 | - CodeChecker::STATIC_CALL_NOT_ALLOWED => "Static method of %s class must not be called", |
|
92 | - CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "Constant of %s class must not not be fetched", |
|
93 | - CodeChecker::CLASS_NEW_NOT_ALLOWED => "%s class must not be instantiated", |
|
94 | - CodeChecker::CLASS_USE_NOT_ALLOWED => "%s class must not be imported with a use statement", |
|
95 | - CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED => "Method of %s class must not be called", |
|
96 | - |
|
97 | - CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged", |
|
98 | - ]; |
|
99 | - } |
|
100 | - |
|
101 | - /** @var array */ |
|
102 | - public $errors = []; |
|
103 | - |
|
104 | - public function enterNode(Node $node) { |
|
105 | - if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\Equal) { |
|
106 | - $this->errors[] = [ |
|
107 | - 'disallowedToken' => '==', |
|
108 | - 'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED, |
|
109 | - 'line' => $node->getLine(), |
|
110 | - 'reason' => $this->buildReason('==', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED) |
|
111 | - ]; |
|
112 | - } |
|
113 | - if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\NotEqual) { |
|
114 | - $this->errors[] = [ |
|
115 | - 'disallowedToken' => '!=', |
|
116 | - 'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED, |
|
117 | - 'line' => $node->getLine(), |
|
118 | - 'reason' => $this->buildReason('!=', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED) |
|
119 | - ]; |
|
120 | - } |
|
121 | - if ($node instanceof Node\Stmt\Class_) { |
|
122 | - if (!is_null($node->extends)) { |
|
123 | - $this->checkBlackList($node->extends->toString(), CodeChecker::CLASS_EXTENDS_NOT_ALLOWED, $node); |
|
124 | - } |
|
125 | - foreach ($node->implements as $implements) { |
|
126 | - $this->checkBlackList($implements->toString(), CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED, $node); |
|
127 | - } |
|
128 | - } |
|
129 | - if ($node instanceof Node\Expr\StaticCall) { |
|
130 | - if (!is_null($node->class)) { |
|
131 | - if ($node->class instanceof Name) { |
|
132 | - $this->checkBlackList($node->class->toString(), CodeChecker::STATIC_CALL_NOT_ALLOWED, $node); |
|
133 | - |
|
134 | - $this->checkBlackListFunction($node->class->toString(), $node->name, $node); |
|
135 | - $this->checkBlackListMethod($node->class->toString(), $node->name, $node); |
|
136 | - } |
|
137 | - |
|
138 | - if ($node->class instanceof Node\Expr\Variable) { |
|
139 | - /** |
|
140 | - * TODO: find a way to detect something like this: |
|
141 | - * $c = "OC_API"; |
|
142 | - * $n = $c::call(); |
|
143 | - */ |
|
144 | - // $this->checkBlackListMethod($node->class->..., $node->name, $node); |
|
145 | - } |
|
146 | - } |
|
147 | - } |
|
148 | - if ($node instanceof Node\Expr\MethodCall) { |
|
149 | - if (!is_null($node->var)) { |
|
150 | - if ($node->var instanceof Node\Expr\Variable) { |
|
151 | - /** |
|
152 | - * TODO: find a way to detect something like this: |
|
153 | - * $c = new OC_API(); |
|
154 | - * $n = $c::call(); |
|
155 | - * $n = $c->call(); |
|
156 | - */ |
|
157 | - // $this->checkBlackListMethod($node->var->..., $node->name, $node); |
|
158 | - } |
|
159 | - } |
|
160 | - } |
|
161 | - if ($node instanceof Node\Expr\ClassConstFetch) { |
|
162 | - if (!is_null($node->class)) { |
|
163 | - if ($node->class instanceof Name) { |
|
164 | - $this->checkBlackList($node->class->toString(), CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, $node); |
|
165 | - } |
|
166 | - if ($node->class instanceof Node\Expr\Variable || $node->class instanceof Node\Expr\PropertyFetch) { |
|
167 | - /** |
|
168 | - * TODO: find a way to detect something like this: |
|
169 | - * $c = "OC_API"; |
|
170 | - * $n = $i::ADMIN_AUTH; |
|
171 | - */ |
|
172 | - } else { |
|
173 | - $this->checkBlackListConstant($node->class->toString(), $node->name, $node); |
|
174 | - } |
|
175 | - } |
|
176 | - } |
|
177 | - if ($node instanceof Node\Expr\New_) { |
|
178 | - if (!is_null($node->class)) { |
|
179 | - if ($node->class instanceof Name) { |
|
180 | - $this->checkBlackList($node->class->toString(), CodeChecker::CLASS_NEW_NOT_ALLOWED, $node); |
|
181 | - } |
|
182 | - if ($node->class instanceof Node\Expr\Variable) { |
|
183 | - /** |
|
184 | - * TODO: find a way to detect something like this: |
|
185 | - * $c = "OC_API"; |
|
186 | - * $n = new $i; |
|
187 | - */ |
|
188 | - } |
|
189 | - } |
|
190 | - } |
|
191 | - if ($node instanceof Node\Stmt\UseUse) { |
|
192 | - $this->checkBlackList($node->name->toString(), CodeChecker::CLASS_USE_NOT_ALLOWED, $node); |
|
193 | - if ($node->alias) { |
|
194 | - $this->addUseNameToBlackList($node->name->toString(), $node->alias); |
|
195 | - } else { |
|
196 | - $this->addUseNameToBlackList($node->name->toString(), $node->name->getLast()); |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Check whether an alias was introduced for a namespace of a blacklisted class |
|
203 | - * |
|
204 | - * Example: |
|
205 | - * - Blacklist entry: OCP\AppFramework\IApi |
|
206 | - * - Name: OCP\AppFramework |
|
207 | - * - Alias: OAF |
|
208 | - * => new blacklist entry: OAF\IApi |
|
209 | - * |
|
210 | - * @param string $name |
|
211 | - * @param string $alias |
|
212 | - */ |
|
213 | - private function addUseNameToBlackList($name, $alias) { |
|
214 | - $name = strtolower($name); |
|
215 | - $alias = strtolower($alias); |
|
216 | - |
|
217 | - foreach ($this->blackListedClassNames as $blackListedAlias => $blackListedClassName) { |
|
218 | - if (strpos($blackListedClassName, $name . '\\') === 0) { |
|
219 | - $aliasedClassName = str_replace($name, $alias, $blackListedClassName); |
|
220 | - $this->blackListedClassNames[$aliasedClassName] = $blackListedClassName; |
|
221 | - } |
|
222 | - } |
|
223 | - |
|
224 | - foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) { |
|
225 | - if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) { |
|
226 | - $aliasedConstantName = str_replace($name, $alias, $blackListedConstant); |
|
227 | - $this->blackListedConstants[$aliasedConstantName] = $blackListedConstant; |
|
228 | - } |
|
229 | - } |
|
230 | - |
|
231 | - foreach ($this->blackListedFunctions as $blackListedAlias => $blackListedFunction) { |
|
232 | - if (strpos($blackListedFunction, $name . '\\') === 0 || strpos($blackListedFunction, $name . '::') === 0) { |
|
233 | - $aliasedFunctionName = str_replace($name, $alias, $blackListedFunction); |
|
234 | - $this->blackListedFunctions[$aliasedFunctionName] = $blackListedFunction; |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - foreach ($this->blackListedMethods as $blackListedAlias => $blackListedMethod) { |
|
239 | - if (strpos($blackListedMethod, $name . '\\') === 0 || strpos($blackListedMethod, $name . '::') === 0) { |
|
240 | - $aliasedMethodName = str_replace($name, $alias, $blackListedMethod); |
|
241 | - $this->blackListedMethods[$aliasedMethodName] = $blackListedMethod; |
|
242 | - } |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - private function checkBlackList($name, $errorCode, Node $node) { |
|
247 | - $lowerName = strtolower($name); |
|
248 | - |
|
249 | - if (isset($this->blackListedClassNames[$lowerName])) { |
|
250 | - $this->errors[] = [ |
|
251 | - 'disallowedToken' => $name, |
|
252 | - 'errorCode' => $errorCode, |
|
253 | - 'line' => $node->getLine(), |
|
254 | - 'reason' => $this->buildReason($this->blackListedClassNames[$lowerName], $errorCode) |
|
255 | - ]; |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - private function checkBlackListConstant($class, $constantName, Node $node) { |
|
260 | - $name = $class . '::' . $constantName; |
|
261 | - $lowerName = strtolower($name); |
|
262 | - |
|
263 | - if (isset($this->blackListedConstants[$lowerName])) { |
|
264 | - $this->errors[] = [ |
|
265 | - 'disallowedToken' => $name, |
|
266 | - 'errorCode' => CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, |
|
267 | - 'line' => $node->getLine(), |
|
268 | - 'reason' => $this->buildReason($this->blackListedConstants[$lowerName], CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED) |
|
269 | - ]; |
|
270 | - } |
|
271 | - } |
|
272 | - |
|
273 | - private function checkBlackListFunction($class, $functionName, Node $node) { |
|
274 | - $name = $class . '::' . $functionName; |
|
275 | - $lowerName = strtolower($name); |
|
276 | - |
|
277 | - if (isset($this->blackListedFunctions[$lowerName])) { |
|
278 | - $this->errors[] = [ |
|
279 | - 'disallowedToken' => $name, |
|
280 | - 'errorCode' => CodeChecker::STATIC_CALL_NOT_ALLOWED, |
|
281 | - 'line' => $node->getLine(), |
|
282 | - 'reason' => $this->buildReason($this->blackListedFunctions[$lowerName], CodeChecker::STATIC_CALL_NOT_ALLOWED) |
|
283 | - ]; |
|
284 | - } |
|
285 | - } |
|
286 | - |
|
287 | - private function checkBlackListMethod($class, $functionName, Node $node) { |
|
288 | - $name = $class . '::' . $functionName; |
|
289 | - $lowerName = strtolower($name); |
|
290 | - |
|
291 | - if (isset($this->blackListedMethods[$lowerName])) { |
|
292 | - $this->errors[] = [ |
|
293 | - 'disallowedToken' => $name, |
|
294 | - 'errorCode' => CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED, |
|
295 | - 'line' => $node->getLine(), |
|
296 | - 'reason' => $this->buildReason($this->blackListedMethods[$lowerName], CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED) |
|
297 | - ]; |
|
298 | - } |
|
299 | - } |
|
300 | - |
|
301 | - private function buildReason($name, $errorCode) { |
|
302 | - if (isset($this->errorMessages[$errorCode])) { |
|
303 | - $desc = $this->list->getDescription($errorCode, $name); |
|
304 | - return sprintf($this->errorMessages[$errorCode], $desc); |
|
305 | - } |
|
306 | - |
|
307 | - return "$name usage not allowed - error: $errorCode"; |
|
308 | - } |
|
33 | + /** @var ICheck */ |
|
34 | + protected $list; |
|
35 | + |
|
36 | + /** @var string */ |
|
37 | + protected $blackListDescription; |
|
38 | + /** @var string[] */ |
|
39 | + protected $blackListedClassNames; |
|
40 | + /** @var string[] */ |
|
41 | + protected $blackListedConstants; |
|
42 | + /** @var string[] */ |
|
43 | + protected $blackListedFunctions; |
|
44 | + /** @var string[] */ |
|
45 | + protected $blackListedMethods; |
|
46 | + /** @var bool */ |
|
47 | + protected $checkEqualOperatorUsage; |
|
48 | + /** @var string[] */ |
|
49 | + protected $errorMessages; |
|
50 | + |
|
51 | + /** |
|
52 | + * @param ICheck $list |
|
53 | + */ |
|
54 | + public function __construct(ICheck $list) { |
|
55 | + $this->list = $list; |
|
56 | + |
|
57 | + $this->blackListedClassNames = []; |
|
58 | + foreach ($list->getClasses() as $class => $blackListInfo) { |
|
59 | + if (is_numeric($class) && is_string($blackListInfo)) { |
|
60 | + $class = $blackListInfo; |
|
61 | + $blackListInfo = null; |
|
62 | + } |
|
63 | + |
|
64 | + $class = strtolower($class); |
|
65 | + $this->blackListedClassNames[$class] = $class; |
|
66 | + } |
|
67 | + |
|
68 | + $this->blackListedConstants = []; |
|
69 | + foreach ($list->getConstants() as $constantName => $blackListInfo) { |
|
70 | + $constantName = strtolower($constantName); |
|
71 | + $this->blackListedConstants[$constantName] = $constantName; |
|
72 | + } |
|
73 | + |
|
74 | + $this->blackListedFunctions = []; |
|
75 | + foreach ($list->getFunctions() as $functionName => $blackListInfo) { |
|
76 | + $functionName = strtolower($functionName); |
|
77 | + $this->blackListedFunctions[$functionName] = $functionName; |
|
78 | + } |
|
79 | + |
|
80 | + $this->blackListedMethods = []; |
|
81 | + foreach ($list->getMethods() as $functionName => $blackListInfo) { |
|
82 | + $functionName = strtolower($functionName); |
|
83 | + $this->blackListedMethods[$functionName] = $functionName; |
|
84 | + } |
|
85 | + |
|
86 | + $this->checkEqualOperatorUsage = $list->checkStrongComparisons(); |
|
87 | + |
|
88 | + $this->errorMessages = [ |
|
89 | + CodeChecker::CLASS_EXTENDS_NOT_ALLOWED => "%s class must not be extended", |
|
90 | + CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED => "%s interface must not be implemented", |
|
91 | + CodeChecker::STATIC_CALL_NOT_ALLOWED => "Static method of %s class must not be called", |
|
92 | + CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "Constant of %s class must not not be fetched", |
|
93 | + CodeChecker::CLASS_NEW_NOT_ALLOWED => "%s class must not be instantiated", |
|
94 | + CodeChecker::CLASS_USE_NOT_ALLOWED => "%s class must not be imported with a use statement", |
|
95 | + CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED => "Method of %s class must not be called", |
|
96 | + |
|
97 | + CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged", |
|
98 | + ]; |
|
99 | + } |
|
100 | + |
|
101 | + /** @var array */ |
|
102 | + public $errors = []; |
|
103 | + |
|
104 | + public function enterNode(Node $node) { |
|
105 | + if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\Equal) { |
|
106 | + $this->errors[] = [ |
|
107 | + 'disallowedToken' => '==', |
|
108 | + 'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED, |
|
109 | + 'line' => $node->getLine(), |
|
110 | + 'reason' => $this->buildReason('==', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED) |
|
111 | + ]; |
|
112 | + } |
|
113 | + if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\NotEqual) { |
|
114 | + $this->errors[] = [ |
|
115 | + 'disallowedToken' => '!=', |
|
116 | + 'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED, |
|
117 | + 'line' => $node->getLine(), |
|
118 | + 'reason' => $this->buildReason('!=', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED) |
|
119 | + ]; |
|
120 | + } |
|
121 | + if ($node instanceof Node\Stmt\Class_) { |
|
122 | + if (!is_null($node->extends)) { |
|
123 | + $this->checkBlackList($node->extends->toString(), CodeChecker::CLASS_EXTENDS_NOT_ALLOWED, $node); |
|
124 | + } |
|
125 | + foreach ($node->implements as $implements) { |
|
126 | + $this->checkBlackList($implements->toString(), CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED, $node); |
|
127 | + } |
|
128 | + } |
|
129 | + if ($node instanceof Node\Expr\StaticCall) { |
|
130 | + if (!is_null($node->class)) { |
|
131 | + if ($node->class instanceof Name) { |
|
132 | + $this->checkBlackList($node->class->toString(), CodeChecker::STATIC_CALL_NOT_ALLOWED, $node); |
|
133 | + |
|
134 | + $this->checkBlackListFunction($node->class->toString(), $node->name, $node); |
|
135 | + $this->checkBlackListMethod($node->class->toString(), $node->name, $node); |
|
136 | + } |
|
137 | + |
|
138 | + if ($node->class instanceof Node\Expr\Variable) { |
|
139 | + /** |
|
140 | + * TODO: find a way to detect something like this: |
|
141 | + * $c = "OC_API"; |
|
142 | + * $n = $c::call(); |
|
143 | + */ |
|
144 | + // $this->checkBlackListMethod($node->class->..., $node->name, $node); |
|
145 | + } |
|
146 | + } |
|
147 | + } |
|
148 | + if ($node instanceof Node\Expr\MethodCall) { |
|
149 | + if (!is_null($node->var)) { |
|
150 | + if ($node->var instanceof Node\Expr\Variable) { |
|
151 | + /** |
|
152 | + * TODO: find a way to detect something like this: |
|
153 | + * $c = new OC_API(); |
|
154 | + * $n = $c::call(); |
|
155 | + * $n = $c->call(); |
|
156 | + */ |
|
157 | + // $this->checkBlackListMethod($node->var->..., $node->name, $node); |
|
158 | + } |
|
159 | + } |
|
160 | + } |
|
161 | + if ($node instanceof Node\Expr\ClassConstFetch) { |
|
162 | + if (!is_null($node->class)) { |
|
163 | + if ($node->class instanceof Name) { |
|
164 | + $this->checkBlackList($node->class->toString(), CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, $node); |
|
165 | + } |
|
166 | + if ($node->class instanceof Node\Expr\Variable || $node->class instanceof Node\Expr\PropertyFetch) { |
|
167 | + /** |
|
168 | + * TODO: find a way to detect something like this: |
|
169 | + * $c = "OC_API"; |
|
170 | + * $n = $i::ADMIN_AUTH; |
|
171 | + */ |
|
172 | + } else { |
|
173 | + $this->checkBlackListConstant($node->class->toString(), $node->name, $node); |
|
174 | + } |
|
175 | + } |
|
176 | + } |
|
177 | + if ($node instanceof Node\Expr\New_) { |
|
178 | + if (!is_null($node->class)) { |
|
179 | + if ($node->class instanceof Name) { |
|
180 | + $this->checkBlackList($node->class->toString(), CodeChecker::CLASS_NEW_NOT_ALLOWED, $node); |
|
181 | + } |
|
182 | + if ($node->class instanceof Node\Expr\Variable) { |
|
183 | + /** |
|
184 | + * TODO: find a way to detect something like this: |
|
185 | + * $c = "OC_API"; |
|
186 | + * $n = new $i; |
|
187 | + */ |
|
188 | + } |
|
189 | + } |
|
190 | + } |
|
191 | + if ($node instanceof Node\Stmt\UseUse) { |
|
192 | + $this->checkBlackList($node->name->toString(), CodeChecker::CLASS_USE_NOT_ALLOWED, $node); |
|
193 | + if ($node->alias) { |
|
194 | + $this->addUseNameToBlackList($node->name->toString(), $node->alias); |
|
195 | + } else { |
|
196 | + $this->addUseNameToBlackList($node->name->toString(), $node->name->getLast()); |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Check whether an alias was introduced for a namespace of a blacklisted class |
|
203 | + * |
|
204 | + * Example: |
|
205 | + * - Blacklist entry: OCP\AppFramework\IApi |
|
206 | + * - Name: OCP\AppFramework |
|
207 | + * - Alias: OAF |
|
208 | + * => new blacklist entry: OAF\IApi |
|
209 | + * |
|
210 | + * @param string $name |
|
211 | + * @param string $alias |
|
212 | + */ |
|
213 | + private function addUseNameToBlackList($name, $alias) { |
|
214 | + $name = strtolower($name); |
|
215 | + $alias = strtolower($alias); |
|
216 | + |
|
217 | + foreach ($this->blackListedClassNames as $blackListedAlias => $blackListedClassName) { |
|
218 | + if (strpos($blackListedClassName, $name . '\\') === 0) { |
|
219 | + $aliasedClassName = str_replace($name, $alias, $blackListedClassName); |
|
220 | + $this->blackListedClassNames[$aliasedClassName] = $blackListedClassName; |
|
221 | + } |
|
222 | + } |
|
223 | + |
|
224 | + foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) { |
|
225 | + if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) { |
|
226 | + $aliasedConstantName = str_replace($name, $alias, $blackListedConstant); |
|
227 | + $this->blackListedConstants[$aliasedConstantName] = $blackListedConstant; |
|
228 | + } |
|
229 | + } |
|
230 | + |
|
231 | + foreach ($this->blackListedFunctions as $blackListedAlias => $blackListedFunction) { |
|
232 | + if (strpos($blackListedFunction, $name . '\\') === 0 || strpos($blackListedFunction, $name . '::') === 0) { |
|
233 | + $aliasedFunctionName = str_replace($name, $alias, $blackListedFunction); |
|
234 | + $this->blackListedFunctions[$aliasedFunctionName] = $blackListedFunction; |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + foreach ($this->blackListedMethods as $blackListedAlias => $blackListedMethod) { |
|
239 | + if (strpos($blackListedMethod, $name . '\\') === 0 || strpos($blackListedMethod, $name . '::') === 0) { |
|
240 | + $aliasedMethodName = str_replace($name, $alias, $blackListedMethod); |
|
241 | + $this->blackListedMethods[$aliasedMethodName] = $blackListedMethod; |
|
242 | + } |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + private function checkBlackList($name, $errorCode, Node $node) { |
|
247 | + $lowerName = strtolower($name); |
|
248 | + |
|
249 | + if (isset($this->blackListedClassNames[$lowerName])) { |
|
250 | + $this->errors[] = [ |
|
251 | + 'disallowedToken' => $name, |
|
252 | + 'errorCode' => $errorCode, |
|
253 | + 'line' => $node->getLine(), |
|
254 | + 'reason' => $this->buildReason($this->blackListedClassNames[$lowerName], $errorCode) |
|
255 | + ]; |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + private function checkBlackListConstant($class, $constantName, Node $node) { |
|
260 | + $name = $class . '::' . $constantName; |
|
261 | + $lowerName = strtolower($name); |
|
262 | + |
|
263 | + if (isset($this->blackListedConstants[$lowerName])) { |
|
264 | + $this->errors[] = [ |
|
265 | + 'disallowedToken' => $name, |
|
266 | + 'errorCode' => CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, |
|
267 | + 'line' => $node->getLine(), |
|
268 | + 'reason' => $this->buildReason($this->blackListedConstants[$lowerName], CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED) |
|
269 | + ]; |
|
270 | + } |
|
271 | + } |
|
272 | + |
|
273 | + private function checkBlackListFunction($class, $functionName, Node $node) { |
|
274 | + $name = $class . '::' . $functionName; |
|
275 | + $lowerName = strtolower($name); |
|
276 | + |
|
277 | + if (isset($this->blackListedFunctions[$lowerName])) { |
|
278 | + $this->errors[] = [ |
|
279 | + 'disallowedToken' => $name, |
|
280 | + 'errorCode' => CodeChecker::STATIC_CALL_NOT_ALLOWED, |
|
281 | + 'line' => $node->getLine(), |
|
282 | + 'reason' => $this->buildReason($this->blackListedFunctions[$lowerName], CodeChecker::STATIC_CALL_NOT_ALLOWED) |
|
283 | + ]; |
|
284 | + } |
|
285 | + } |
|
286 | + |
|
287 | + private function checkBlackListMethod($class, $functionName, Node $node) { |
|
288 | + $name = $class . '::' . $functionName; |
|
289 | + $lowerName = strtolower($name); |
|
290 | + |
|
291 | + if (isset($this->blackListedMethods[$lowerName])) { |
|
292 | + $this->errors[] = [ |
|
293 | + 'disallowedToken' => $name, |
|
294 | + 'errorCode' => CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED, |
|
295 | + 'line' => $node->getLine(), |
|
296 | + 'reason' => $this->buildReason($this->blackListedMethods[$lowerName], CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED) |
|
297 | + ]; |
|
298 | + } |
|
299 | + } |
|
300 | + |
|
301 | + private function buildReason($name, $errorCode) { |
|
302 | + if (isset($this->errorMessages[$errorCode])) { |
|
303 | + $desc = $this->list->getDescription($errorCode, $name); |
|
304 | + return sprintf($this->errorMessages[$errorCode], $desc); |
|
305 | + } |
|
306 | + |
|
307 | + return "$name usage not allowed - error: $errorCode"; |
|
308 | + } |
|
309 | 309 | } |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | * @return int|null |
152 | 152 | */ |
153 | 153 | public function getId() { |
154 | - return isset($this->data['fileid']) ? (int) $this->data['fileid'] : null; |
|
154 | + return isset($this->data['fileid']) ? (int) $this->data['fileid'] : null; |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | public function getEtag() { |
182 | 182 | $this->updateEntryfromSubMounts(); |
183 | 183 | if (count($this->childEtags) > 0) { |
184 | - $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags); |
|
184 | + $combinedEtag = $this->data['etag'].'::'.implode('::', $this->childEtags); |
|
185 | 185 | return md5($combinedEtag); |
186 | 186 | } else { |
187 | 187 | return $this->data['etag']; |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | $relativeEntryPath = substr($entryPath, strlen($this->getPath())); |
378 | 378 | // attach the permissions to propagate etag on permision changes of submounts |
379 | 379 | $permissions = isset($data['permissions']) ? $data['permissions'] : 0; |
380 | - $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions; |
|
380 | + $this->childEtags[] = $relativeEntryPath.'/'.$data['etag'].$permissions; |
|
381 | 381 | } |
382 | 382 | } |
383 | 383 |
@@ -38,380 +38,380 @@ |
||
38 | 38 | use OCP\IUser; |
39 | 39 | |
40 | 40 | class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess { |
41 | - /** |
|
42 | - * @var array $data |
|
43 | - */ |
|
44 | - private $data; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var string $path |
|
48 | - */ |
|
49 | - private $path; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var \OC\Files\Storage\Storage $storage |
|
53 | - */ |
|
54 | - private $storage; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var string $internalPath |
|
58 | - */ |
|
59 | - private $internalPath; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var \OCP\Files\Mount\IMountPoint |
|
63 | - */ |
|
64 | - private $mount; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var IUser |
|
68 | - */ |
|
69 | - private $owner; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var string[] |
|
73 | - */ |
|
74 | - private $childEtags = []; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var IMountPoint[] |
|
78 | - */ |
|
79 | - private $subMounts = []; |
|
80 | - |
|
81 | - private $subMountsUsed = false; |
|
82 | - |
|
83 | - /** |
|
84 | - * The size of the file/folder without any sub mount |
|
85 | - * |
|
86 | - * @var int |
|
87 | - */ |
|
88 | - private $rawSize = 0; |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string|boolean $path |
|
92 | - * @param Storage\Storage $storage |
|
93 | - * @param string $internalPath |
|
94 | - * @param array|ICacheEntry $data |
|
95 | - * @param \OCP\Files\Mount\IMountPoint $mount |
|
96 | - * @param \OCP\IUser|null $owner |
|
97 | - */ |
|
98 | - public function __construct($path, $storage, $internalPath, $data, $mount, $owner = null) { |
|
99 | - $this->path = $path; |
|
100 | - $this->storage = $storage; |
|
101 | - $this->internalPath = $internalPath; |
|
102 | - $this->data = $data; |
|
103 | - $this->mount = $mount; |
|
104 | - $this->owner = $owner; |
|
105 | - $this->rawSize = $this->data['size'] ?? 0; |
|
106 | - } |
|
107 | - |
|
108 | - public function offsetSet($offset, $value) { |
|
109 | - $this->data[$offset] = $value; |
|
110 | - } |
|
111 | - |
|
112 | - public function offsetExists($offset) { |
|
113 | - return isset($this->data[$offset]); |
|
114 | - } |
|
115 | - |
|
116 | - public function offsetUnset($offset) { |
|
117 | - unset($this->data[$offset]); |
|
118 | - } |
|
119 | - |
|
120 | - public function offsetGet($offset) { |
|
121 | - if ($offset === 'type') { |
|
122 | - return $this->getType(); |
|
123 | - } elseif ($offset === 'etag') { |
|
124 | - return $this->getEtag(); |
|
125 | - } elseif ($offset === 'size') { |
|
126 | - return $this->getSize(); |
|
127 | - } elseif ($offset === 'mtime') { |
|
128 | - return $this->getMTime(); |
|
129 | - } elseif ($offset === 'permissions') { |
|
130 | - return $this->getPermissions(); |
|
131 | - } elseif (isset($this->data[$offset])) { |
|
132 | - return $this->data[$offset]; |
|
133 | - } else { |
|
134 | - return null; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @return string |
|
140 | - */ |
|
141 | - public function getPath() { |
|
142 | - return $this->path; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @return \OCP\Files\Storage |
|
147 | - */ |
|
148 | - public function getStorage() { |
|
149 | - return $this->storage; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @return string |
|
154 | - */ |
|
155 | - public function getInternalPath() { |
|
156 | - return $this->internalPath; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Get FileInfo ID or null in case of part file |
|
161 | - * |
|
162 | - * @return int|null |
|
163 | - */ |
|
164 | - public function getId() { |
|
165 | - return isset($this->data['fileid']) ? (int) $this->data['fileid'] : null; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - public function getMimetype() { |
|
172 | - return $this->data['mimetype']; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @return string |
|
177 | - */ |
|
178 | - public function getMimePart() { |
|
179 | - return $this->data['mimepart']; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * @return string |
|
184 | - */ |
|
185 | - public function getName() { |
|
186 | - return isset($this->data['name']) ? $this->data['name'] : basename($this->getPath()); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - public function getEtag() { |
|
193 | - $this->updateEntryfromSubMounts(); |
|
194 | - if (count($this->childEtags) > 0) { |
|
195 | - $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags); |
|
196 | - return md5($combinedEtag); |
|
197 | - } else { |
|
198 | - return $this->data['etag']; |
|
199 | - } |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @return int |
|
204 | - */ |
|
205 | - public function getSize($includeMounts = true) { |
|
206 | - if ($includeMounts) { |
|
207 | - $this->updateEntryfromSubMounts(); |
|
208 | - return isset($this->data['size']) ? 0 + $this->data['size'] : 0; |
|
209 | - } else { |
|
210 | - return $this->rawSize; |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @return int |
|
216 | - */ |
|
217 | - public function getMTime() { |
|
218 | - $this->updateEntryfromSubMounts(); |
|
219 | - return (int) $this->data['mtime']; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function isEncrypted() { |
|
226 | - return $this->data['encrypted']; |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Return the currently version used for the HMAC in the encryption app |
|
231 | - * |
|
232 | - * @return int |
|
233 | - */ |
|
234 | - public function getEncryptedVersion() { |
|
235 | - return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * @return int |
|
240 | - */ |
|
241 | - public function getPermissions() { |
|
242 | - $perms = (int) $this->data['permissions']; |
|
243 | - if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) { |
|
244 | - $perms = $perms & ~\OCP\Constants::PERMISSION_SHARE; |
|
245 | - } |
|
246 | - return (int) $perms; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * @return string \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER |
|
251 | - */ |
|
252 | - public function getType() { |
|
253 | - if (!isset($this->data['type'])) { |
|
254 | - $this->data['type'] = ($this->getMimetype() === 'httpd/unix-directory') ? self::TYPE_FOLDER : self::TYPE_FILE; |
|
255 | - } |
|
256 | - return $this->data['type']; |
|
257 | - } |
|
258 | - |
|
259 | - public function getData() { |
|
260 | - return $this->data; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * @param int $permissions |
|
265 | - * @return bool |
|
266 | - */ |
|
267 | - protected function checkPermissions($permissions) { |
|
268 | - return ($this->getPermissions() & $permissions) === $permissions; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * @return bool |
|
273 | - */ |
|
274 | - public function isReadable() { |
|
275 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_READ); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @return bool |
|
280 | - */ |
|
281 | - public function isUpdateable() { |
|
282 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE); |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * Check whether new files or folders can be created inside this folder |
|
287 | - * |
|
288 | - * @return bool |
|
289 | - */ |
|
290 | - public function isCreatable() { |
|
291 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE); |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * @return bool |
|
296 | - */ |
|
297 | - public function isDeletable() { |
|
298 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * @return bool |
|
303 | - */ |
|
304 | - public function isShareable() { |
|
305 | - return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE); |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Check if a file or folder is shared |
|
310 | - * |
|
311 | - * @return bool |
|
312 | - */ |
|
313 | - public function isShared() { |
|
314 | - $sid = $this->getStorage()->getId(); |
|
315 | - if (!is_null($sid)) { |
|
316 | - $sid = explode(':', $sid); |
|
317 | - return ($sid[0] === 'shared'); |
|
318 | - } |
|
319 | - |
|
320 | - return false; |
|
321 | - } |
|
322 | - |
|
323 | - public function isMounted() { |
|
324 | - $storage = $this->getStorage(); |
|
325 | - if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
326 | - return false; |
|
327 | - } |
|
328 | - $sid = $storage->getId(); |
|
329 | - if (!is_null($sid)) { |
|
330 | - $sid = explode(':', $sid); |
|
331 | - return ($sid[0] !== 'home' and $sid[0] !== 'shared'); |
|
332 | - } |
|
333 | - |
|
334 | - return false; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Get the mountpoint the file belongs to |
|
339 | - * |
|
340 | - * @return \OCP\Files\Mount\IMountPoint |
|
341 | - */ |
|
342 | - public function getMountPoint() { |
|
343 | - return $this->mount; |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Get the owner of the file |
|
348 | - * |
|
349 | - * @return \OCP\IUser |
|
350 | - */ |
|
351 | - public function getOwner() { |
|
352 | - return $this->owner; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * @param IMountPoint[] $mounts |
|
357 | - */ |
|
358 | - public function setSubMounts(array $mounts) { |
|
359 | - $this->subMounts = $mounts; |
|
360 | - } |
|
361 | - |
|
362 | - private function updateEntryfromSubMounts() { |
|
363 | - if ($this->subMountsUsed) { |
|
364 | - return; |
|
365 | - } |
|
366 | - $this->subMountsUsed = true; |
|
367 | - foreach ($this->subMounts as $mount) { |
|
368 | - $subStorage = $mount->getStorage(); |
|
369 | - if ($subStorage) { |
|
370 | - $subCache = $subStorage->getCache(''); |
|
371 | - $rootEntry = $subCache->get(''); |
|
372 | - $this->addSubEntry($rootEntry, $mount->getMountPoint()); |
|
373 | - } |
|
374 | - } |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Add a cache entry which is the child of this folder |
|
379 | - * |
|
380 | - * Sets the size, etag and size to for cross-storage childs |
|
381 | - * |
|
382 | - * @param array|ICacheEntry $data cache entry for the child |
|
383 | - * @param string $entryPath full path of the child entry |
|
384 | - */ |
|
385 | - public function addSubEntry($data, $entryPath) { |
|
386 | - $this->data['size'] += isset($data['size']) ? $data['size'] : 0; |
|
387 | - if (isset($data['mtime'])) { |
|
388 | - $this->data['mtime'] = max($this->data['mtime'], $data['mtime']); |
|
389 | - } |
|
390 | - if (isset($data['etag'])) { |
|
391 | - // prefix the etag with the relative path of the subentry to propagate etag on mount moves |
|
392 | - $relativeEntryPath = substr($entryPath, strlen($this->getPath())); |
|
393 | - // attach the permissions to propagate etag on permision changes of submounts |
|
394 | - $permissions = isset($data['permissions']) ? $data['permissions'] : 0; |
|
395 | - $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions; |
|
396 | - } |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * @inheritdoc |
|
401 | - */ |
|
402 | - public function getChecksum() { |
|
403 | - return $this->data['checksum']; |
|
404 | - } |
|
405 | - |
|
406 | - public function getExtension(): string { |
|
407 | - return pathinfo($this->getName(), PATHINFO_EXTENSION); |
|
408 | - } |
|
409 | - |
|
410 | - public function getCreationTime(): int { |
|
411 | - return (int) $this->data['creation_time']; |
|
412 | - } |
|
413 | - |
|
414 | - public function getUploadTime(): int { |
|
415 | - return (int) $this->data['upload_time']; |
|
416 | - } |
|
41 | + /** |
|
42 | + * @var array $data |
|
43 | + */ |
|
44 | + private $data; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var string $path |
|
48 | + */ |
|
49 | + private $path; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var \OC\Files\Storage\Storage $storage |
|
53 | + */ |
|
54 | + private $storage; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var string $internalPath |
|
58 | + */ |
|
59 | + private $internalPath; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var \OCP\Files\Mount\IMountPoint |
|
63 | + */ |
|
64 | + private $mount; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var IUser |
|
68 | + */ |
|
69 | + private $owner; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var string[] |
|
73 | + */ |
|
74 | + private $childEtags = []; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var IMountPoint[] |
|
78 | + */ |
|
79 | + private $subMounts = []; |
|
80 | + |
|
81 | + private $subMountsUsed = false; |
|
82 | + |
|
83 | + /** |
|
84 | + * The size of the file/folder without any sub mount |
|
85 | + * |
|
86 | + * @var int |
|
87 | + */ |
|
88 | + private $rawSize = 0; |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string|boolean $path |
|
92 | + * @param Storage\Storage $storage |
|
93 | + * @param string $internalPath |
|
94 | + * @param array|ICacheEntry $data |
|
95 | + * @param \OCP\Files\Mount\IMountPoint $mount |
|
96 | + * @param \OCP\IUser|null $owner |
|
97 | + */ |
|
98 | + public function __construct($path, $storage, $internalPath, $data, $mount, $owner = null) { |
|
99 | + $this->path = $path; |
|
100 | + $this->storage = $storage; |
|
101 | + $this->internalPath = $internalPath; |
|
102 | + $this->data = $data; |
|
103 | + $this->mount = $mount; |
|
104 | + $this->owner = $owner; |
|
105 | + $this->rawSize = $this->data['size'] ?? 0; |
|
106 | + } |
|
107 | + |
|
108 | + public function offsetSet($offset, $value) { |
|
109 | + $this->data[$offset] = $value; |
|
110 | + } |
|
111 | + |
|
112 | + public function offsetExists($offset) { |
|
113 | + return isset($this->data[$offset]); |
|
114 | + } |
|
115 | + |
|
116 | + public function offsetUnset($offset) { |
|
117 | + unset($this->data[$offset]); |
|
118 | + } |
|
119 | + |
|
120 | + public function offsetGet($offset) { |
|
121 | + if ($offset === 'type') { |
|
122 | + return $this->getType(); |
|
123 | + } elseif ($offset === 'etag') { |
|
124 | + return $this->getEtag(); |
|
125 | + } elseif ($offset === 'size') { |
|
126 | + return $this->getSize(); |
|
127 | + } elseif ($offset === 'mtime') { |
|
128 | + return $this->getMTime(); |
|
129 | + } elseif ($offset === 'permissions') { |
|
130 | + return $this->getPermissions(); |
|
131 | + } elseif (isset($this->data[$offset])) { |
|
132 | + return $this->data[$offset]; |
|
133 | + } else { |
|
134 | + return null; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @return string |
|
140 | + */ |
|
141 | + public function getPath() { |
|
142 | + return $this->path; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @return \OCP\Files\Storage |
|
147 | + */ |
|
148 | + public function getStorage() { |
|
149 | + return $this->storage; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @return string |
|
154 | + */ |
|
155 | + public function getInternalPath() { |
|
156 | + return $this->internalPath; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Get FileInfo ID or null in case of part file |
|
161 | + * |
|
162 | + * @return int|null |
|
163 | + */ |
|
164 | + public function getId() { |
|
165 | + return isset($this->data['fileid']) ? (int) $this->data['fileid'] : null; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + public function getMimetype() { |
|
172 | + return $this->data['mimetype']; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @return string |
|
177 | + */ |
|
178 | + public function getMimePart() { |
|
179 | + return $this->data['mimepart']; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * @return string |
|
184 | + */ |
|
185 | + public function getName() { |
|
186 | + return isset($this->data['name']) ? $this->data['name'] : basename($this->getPath()); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + public function getEtag() { |
|
193 | + $this->updateEntryfromSubMounts(); |
|
194 | + if (count($this->childEtags) > 0) { |
|
195 | + $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags); |
|
196 | + return md5($combinedEtag); |
|
197 | + } else { |
|
198 | + return $this->data['etag']; |
|
199 | + } |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @return int |
|
204 | + */ |
|
205 | + public function getSize($includeMounts = true) { |
|
206 | + if ($includeMounts) { |
|
207 | + $this->updateEntryfromSubMounts(); |
|
208 | + return isset($this->data['size']) ? 0 + $this->data['size'] : 0; |
|
209 | + } else { |
|
210 | + return $this->rawSize; |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @return int |
|
216 | + */ |
|
217 | + public function getMTime() { |
|
218 | + $this->updateEntryfromSubMounts(); |
|
219 | + return (int) $this->data['mtime']; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function isEncrypted() { |
|
226 | + return $this->data['encrypted']; |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Return the currently version used for the HMAC in the encryption app |
|
231 | + * |
|
232 | + * @return int |
|
233 | + */ |
|
234 | + public function getEncryptedVersion() { |
|
235 | + return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * @return int |
|
240 | + */ |
|
241 | + public function getPermissions() { |
|
242 | + $perms = (int) $this->data['permissions']; |
|
243 | + if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) { |
|
244 | + $perms = $perms & ~\OCP\Constants::PERMISSION_SHARE; |
|
245 | + } |
|
246 | + return (int) $perms; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * @return string \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER |
|
251 | + */ |
|
252 | + public function getType() { |
|
253 | + if (!isset($this->data['type'])) { |
|
254 | + $this->data['type'] = ($this->getMimetype() === 'httpd/unix-directory') ? self::TYPE_FOLDER : self::TYPE_FILE; |
|
255 | + } |
|
256 | + return $this->data['type']; |
|
257 | + } |
|
258 | + |
|
259 | + public function getData() { |
|
260 | + return $this->data; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * @param int $permissions |
|
265 | + * @return bool |
|
266 | + */ |
|
267 | + protected function checkPermissions($permissions) { |
|
268 | + return ($this->getPermissions() & $permissions) === $permissions; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * @return bool |
|
273 | + */ |
|
274 | + public function isReadable() { |
|
275 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_READ); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @return bool |
|
280 | + */ |
|
281 | + public function isUpdateable() { |
|
282 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE); |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * Check whether new files or folders can be created inside this folder |
|
287 | + * |
|
288 | + * @return bool |
|
289 | + */ |
|
290 | + public function isCreatable() { |
|
291 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE); |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * @return bool |
|
296 | + */ |
|
297 | + public function isDeletable() { |
|
298 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * @return bool |
|
303 | + */ |
|
304 | + public function isShareable() { |
|
305 | + return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE); |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Check if a file or folder is shared |
|
310 | + * |
|
311 | + * @return bool |
|
312 | + */ |
|
313 | + public function isShared() { |
|
314 | + $sid = $this->getStorage()->getId(); |
|
315 | + if (!is_null($sid)) { |
|
316 | + $sid = explode(':', $sid); |
|
317 | + return ($sid[0] === 'shared'); |
|
318 | + } |
|
319 | + |
|
320 | + return false; |
|
321 | + } |
|
322 | + |
|
323 | + public function isMounted() { |
|
324 | + $storage = $this->getStorage(); |
|
325 | + if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
326 | + return false; |
|
327 | + } |
|
328 | + $sid = $storage->getId(); |
|
329 | + if (!is_null($sid)) { |
|
330 | + $sid = explode(':', $sid); |
|
331 | + return ($sid[0] !== 'home' and $sid[0] !== 'shared'); |
|
332 | + } |
|
333 | + |
|
334 | + return false; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Get the mountpoint the file belongs to |
|
339 | + * |
|
340 | + * @return \OCP\Files\Mount\IMountPoint |
|
341 | + */ |
|
342 | + public function getMountPoint() { |
|
343 | + return $this->mount; |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Get the owner of the file |
|
348 | + * |
|
349 | + * @return \OCP\IUser |
|
350 | + */ |
|
351 | + public function getOwner() { |
|
352 | + return $this->owner; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * @param IMountPoint[] $mounts |
|
357 | + */ |
|
358 | + public function setSubMounts(array $mounts) { |
|
359 | + $this->subMounts = $mounts; |
|
360 | + } |
|
361 | + |
|
362 | + private function updateEntryfromSubMounts() { |
|
363 | + if ($this->subMountsUsed) { |
|
364 | + return; |
|
365 | + } |
|
366 | + $this->subMountsUsed = true; |
|
367 | + foreach ($this->subMounts as $mount) { |
|
368 | + $subStorage = $mount->getStorage(); |
|
369 | + if ($subStorage) { |
|
370 | + $subCache = $subStorage->getCache(''); |
|
371 | + $rootEntry = $subCache->get(''); |
|
372 | + $this->addSubEntry($rootEntry, $mount->getMountPoint()); |
|
373 | + } |
|
374 | + } |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Add a cache entry which is the child of this folder |
|
379 | + * |
|
380 | + * Sets the size, etag and size to for cross-storage childs |
|
381 | + * |
|
382 | + * @param array|ICacheEntry $data cache entry for the child |
|
383 | + * @param string $entryPath full path of the child entry |
|
384 | + */ |
|
385 | + public function addSubEntry($data, $entryPath) { |
|
386 | + $this->data['size'] += isset($data['size']) ? $data['size'] : 0; |
|
387 | + if (isset($data['mtime'])) { |
|
388 | + $this->data['mtime'] = max($this->data['mtime'], $data['mtime']); |
|
389 | + } |
|
390 | + if (isset($data['etag'])) { |
|
391 | + // prefix the etag with the relative path of the subentry to propagate etag on mount moves |
|
392 | + $relativeEntryPath = substr($entryPath, strlen($this->getPath())); |
|
393 | + // attach the permissions to propagate etag on permision changes of submounts |
|
394 | + $permissions = isset($data['permissions']) ? $data['permissions'] : 0; |
|
395 | + $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions; |
|
396 | + } |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * @inheritdoc |
|
401 | + */ |
|
402 | + public function getChecksum() { |
|
403 | + return $this->data['checksum']; |
|
404 | + } |
|
405 | + |
|
406 | + public function getExtension(): string { |
|
407 | + return pathinfo($this->getName(), PATHINFO_EXTENSION); |
|
408 | + } |
|
409 | + |
|
410 | + public function getCreationTime(): int { |
|
411 | + return (int) $this->data['creation_time']; |
|
412 | + } |
|
413 | + |
|
414 | + public function getUploadTime(): int { |
|
415 | + return (int) $this->data['upload_time']; |
|
416 | + } |
|
417 | 417 | } |
@@ -33,53 +33,53 @@ |
||
33 | 33 | namespace OC\Files\Storage; |
34 | 34 | |
35 | 35 | class CommonTest extends \OC\Files\Storage\Common { |
36 | - /** |
|
37 | - * underlying local storage used for missing functions |
|
38 | - * @var \OC\Files\Storage\Local |
|
39 | - */ |
|
40 | - private $storage; |
|
36 | + /** |
|
37 | + * underlying local storage used for missing functions |
|
38 | + * @var \OC\Files\Storage\Local |
|
39 | + */ |
|
40 | + private $storage; |
|
41 | 41 | |
42 | - public function __construct($params) { |
|
43 | - $this->storage = new \OC\Files\Storage\Local($params); |
|
44 | - } |
|
42 | + public function __construct($params) { |
|
43 | + $this->storage = new \OC\Files\Storage\Local($params); |
|
44 | + } |
|
45 | 45 | |
46 | - public function getId() { |
|
47 | - return 'test::'.$this->storage->getId(); |
|
48 | - } |
|
49 | - public function mkdir($path) { |
|
50 | - return $this->storage->mkdir($path); |
|
51 | - } |
|
52 | - public function rmdir($path) { |
|
53 | - return $this->storage->rmdir($path); |
|
54 | - } |
|
55 | - public function opendir($path) { |
|
56 | - return $this->storage->opendir($path); |
|
57 | - } |
|
58 | - public function stat($path) { |
|
59 | - return $this->storage->stat($path); |
|
60 | - } |
|
61 | - public function filetype($path) { |
|
62 | - return @$this->storage->filetype($path); |
|
63 | - } |
|
64 | - public function isReadable($path) { |
|
65 | - return $this->storage->isReadable($path); |
|
66 | - } |
|
67 | - public function isUpdatable($path) { |
|
68 | - return $this->storage->isUpdatable($path); |
|
69 | - } |
|
70 | - public function file_exists($path) { |
|
71 | - return $this->storage->file_exists($path); |
|
72 | - } |
|
73 | - public function unlink($path) { |
|
74 | - return $this->storage->unlink($path); |
|
75 | - } |
|
76 | - public function fopen($path, $mode) { |
|
77 | - return $this->storage->fopen($path, $mode); |
|
78 | - } |
|
79 | - public function free_space($path) { |
|
80 | - return $this->storage->free_space($path); |
|
81 | - } |
|
82 | - public function touch($path, $mtime = null) { |
|
83 | - return $this->storage->touch($path, $mtime); |
|
84 | - } |
|
46 | + public function getId() { |
|
47 | + return 'test::'.$this->storage->getId(); |
|
48 | + } |
|
49 | + public function mkdir($path) { |
|
50 | + return $this->storage->mkdir($path); |
|
51 | + } |
|
52 | + public function rmdir($path) { |
|
53 | + return $this->storage->rmdir($path); |
|
54 | + } |
|
55 | + public function opendir($path) { |
|
56 | + return $this->storage->opendir($path); |
|
57 | + } |
|
58 | + public function stat($path) { |
|
59 | + return $this->storage->stat($path); |
|
60 | + } |
|
61 | + public function filetype($path) { |
|
62 | + return @$this->storage->filetype($path); |
|
63 | + } |
|
64 | + public function isReadable($path) { |
|
65 | + return $this->storage->isReadable($path); |
|
66 | + } |
|
67 | + public function isUpdatable($path) { |
|
68 | + return $this->storage->isUpdatable($path); |
|
69 | + } |
|
70 | + public function file_exists($path) { |
|
71 | + return $this->storage->file_exists($path); |
|
72 | + } |
|
73 | + public function unlink($path) { |
|
74 | + return $this->storage->unlink($path); |
|
75 | + } |
|
76 | + public function fopen($path, $mode) { |
|
77 | + return $this->storage->fopen($path, $mode); |
|
78 | + } |
|
79 | + public function free_space($path) { |
|
80 | + return $this->storage->free_space($path); |
|
81 | + } |
|
82 | + public function touch($path, $mtime = null) { |
|
83 | + return $this->storage->touch($path, $mtime); |
|
84 | + } |
|
85 | 85 | } |
@@ -64,14 +64,14 @@ |
||
64 | 64 | if (empty($compare)) { |
65 | 65 | $compare = array_keys($input); |
66 | 66 | } |
67 | - $fieldList = '`' . implode('`,`', array_keys($input)) . '`'; |
|
67 | + $fieldList = '`'.implode('`,`', array_keys($input)).'`'; |
|
68 | 68 | $query = "INSERT INTO `$table` ($fieldList) SELECT " |
69 | 69 | . str_repeat('?,', count($input) - 1).'? ' |
70 | 70 | . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE "; |
71 | 71 | |
72 | 72 | $inserts = array_values($input); |
73 | 73 | foreach ($compare as $key) { |
74 | - $query .= '`' . $key . '`'; |
|
74 | + $query .= '`'.$key.'`'; |
|
75 | 75 | if (is_null($input[$key])) { |
76 | 76 | $query .= ' IS NULL AND '; |
77 | 77 | } else { |
@@ -31,70 +31,70 @@ |
||
31 | 31 | |
32 | 32 | class AdapterSqlite extends Adapter { |
33 | 33 | |
34 | - /** |
|
35 | - * @param string $tableName |
|
36 | - */ |
|
37 | - public function lockTable($tableName) { |
|
38 | - $this->conn->executeUpdate('BEGIN EXCLUSIVE TRANSACTION'); |
|
39 | - } |
|
34 | + /** |
|
35 | + * @param string $tableName |
|
36 | + */ |
|
37 | + public function lockTable($tableName) { |
|
38 | + $this->conn->executeUpdate('BEGIN EXCLUSIVE TRANSACTION'); |
|
39 | + } |
|
40 | 40 | |
41 | - public function unlockTable() { |
|
42 | - $this->conn->executeUpdate('COMMIT TRANSACTION'); |
|
43 | - } |
|
41 | + public function unlockTable() { |
|
42 | + $this->conn->executeUpdate('COMMIT TRANSACTION'); |
|
43 | + } |
|
44 | 44 | |
45 | - public function fixupStatement($statement) { |
|
46 | - $statement = preg_replace('/`(\w+)` ILIKE \?/', 'LOWER($1) LIKE LOWER(?)', $statement); |
|
47 | - $statement = str_replace('`', '"', $statement); |
|
48 | - $statement = str_ireplace('NOW()', 'datetime(\'now\')', $statement); |
|
49 | - $statement = str_ireplace('GREATEST(', 'MAX(', $statement); |
|
50 | - $statement = str_ireplace('UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement); |
|
51 | - return $statement; |
|
52 | - } |
|
45 | + public function fixupStatement($statement) { |
|
46 | + $statement = preg_replace('/`(\w+)` ILIKE \?/', 'LOWER($1) LIKE LOWER(?)', $statement); |
|
47 | + $statement = str_replace('`', '"', $statement); |
|
48 | + $statement = str_ireplace('NOW()', 'datetime(\'now\')', $statement); |
|
49 | + $statement = str_ireplace('GREATEST(', 'MAX(', $statement); |
|
50 | + $statement = str_ireplace('UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement); |
|
51 | + return $statement; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
56 | - * it is needed that there is also a unique constraint on the values. Then this method will |
|
57 | - * catch the exception and return 0. |
|
58 | - * |
|
59 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
60 | - * @param array $input data that should be inserted into the table (column name => value) |
|
61 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
62 | - * If this is null or an empty array, all keys of $input will be compared |
|
63 | - * Please note: text fields (clob) must not be used in the compare array |
|
64 | - * @return int number of inserted rows |
|
65 | - * @throws \Doctrine\DBAL\DBALException |
|
66 | - * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
67 | - */ |
|
68 | - public function insertIfNotExist($table, $input, array $compare = null) { |
|
69 | - if (empty($compare)) { |
|
70 | - $compare = array_keys($input); |
|
71 | - } |
|
72 | - $fieldList = '`' . implode('`,`', array_keys($input)) . '`'; |
|
73 | - $query = "INSERT INTO `$table` ($fieldList) SELECT " |
|
74 | - . str_repeat('?,', count($input) - 1).'? ' |
|
75 | - . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE "; |
|
54 | + /** |
|
55 | + * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
56 | + * it is needed that there is also a unique constraint on the values. Then this method will |
|
57 | + * catch the exception and return 0. |
|
58 | + * |
|
59 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
60 | + * @param array $input data that should be inserted into the table (column name => value) |
|
61 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
62 | + * If this is null or an empty array, all keys of $input will be compared |
|
63 | + * Please note: text fields (clob) must not be used in the compare array |
|
64 | + * @return int number of inserted rows |
|
65 | + * @throws \Doctrine\DBAL\DBALException |
|
66 | + * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
67 | + */ |
|
68 | + public function insertIfNotExist($table, $input, array $compare = null) { |
|
69 | + if (empty($compare)) { |
|
70 | + $compare = array_keys($input); |
|
71 | + } |
|
72 | + $fieldList = '`' . implode('`,`', array_keys($input)) . '`'; |
|
73 | + $query = "INSERT INTO `$table` ($fieldList) SELECT " |
|
74 | + . str_repeat('?,', count($input) - 1).'? ' |
|
75 | + . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE "; |
|
76 | 76 | |
77 | - $inserts = array_values($input); |
|
78 | - foreach ($compare as $key) { |
|
79 | - $query .= '`' . $key . '`'; |
|
80 | - if (is_null($input[$key])) { |
|
81 | - $query .= ' IS NULL AND '; |
|
82 | - } else { |
|
83 | - $inserts[] = $input[$key]; |
|
84 | - $query .= ' = ? AND '; |
|
85 | - } |
|
86 | - } |
|
87 | - $query = substr($query, 0, -5); |
|
88 | - $query .= ')'; |
|
77 | + $inserts = array_values($input); |
|
78 | + foreach ($compare as $key) { |
|
79 | + $query .= '`' . $key . '`'; |
|
80 | + if (is_null($input[$key])) { |
|
81 | + $query .= ' IS NULL AND '; |
|
82 | + } else { |
|
83 | + $inserts[] = $input[$key]; |
|
84 | + $query .= ' = ? AND '; |
|
85 | + } |
|
86 | + } |
|
87 | + $query = substr($query, 0, -5); |
|
88 | + $query .= ')'; |
|
89 | 89 | |
90 | - try { |
|
91 | - return $this->conn->executeUpdate($query, $inserts); |
|
92 | - } catch (UniqueConstraintViolationException $e) { |
|
93 | - // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it |
|
94 | - // it's fine to ignore this then |
|
95 | - // |
|
96 | - // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315 |
|
97 | - return 0; |
|
98 | - } |
|
99 | - } |
|
90 | + try { |
|
91 | + return $this->conn->executeUpdate($query, $inserts); |
|
92 | + } catch (UniqueConstraintViolationException $e) { |
|
93 | + // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it |
|
94 | + // it's fine to ignore this then |
|
95 | + // |
|
96 | + // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315 |
|
97 | + return 0; |
|
98 | + } |
|
99 | + } |
|
100 | 100 | } |