@@ -32,25 +32,25 @@ |
||
32 | 32 | */ |
33 | 33 | class ForbiddenException extends \Exception { |
34 | 34 | |
35 | - /** @var bool */ |
|
36 | - private $retry; |
|
35 | + /** @var bool */ |
|
36 | + private $retry; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param string $message |
|
40 | - * @param bool $retry |
|
41 | - * @param \Exception|null $previous previous exception for cascading |
|
42 | - * @since 9.0.0 |
|
43 | - */ |
|
44 | - public function __construct($message, $retry, \Exception $previous = null) { |
|
45 | - parent::__construct($message, 0, $previous); |
|
46 | - $this->retry = $retry; |
|
47 | - } |
|
38 | + /** |
|
39 | + * @param string $message |
|
40 | + * @param bool $retry |
|
41 | + * @param \Exception|null $previous previous exception for cascading |
|
42 | + * @since 9.0.0 |
|
43 | + */ |
|
44 | + public function __construct($message, $retry, \Exception $previous = null) { |
|
45 | + parent::__construct($message, 0, $previous); |
|
46 | + $this->retry = $retry; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @return bool |
|
51 | - * @since 9.0.0 |
|
52 | - */ |
|
53 | - public function getRetry() { |
|
54 | - return (bool) $this->retry; |
|
55 | - } |
|
49 | + /** |
|
50 | + * @return bool |
|
51 | + * @since 9.0.0 |
|
52 | + */ |
|
53 | + public function getRetry() { |
|
54 | + return (bool) $this->retry; |
|
55 | + } |
|
56 | 56 | } |
@@ -27,15 +27,15 @@ |
||
27 | 27 | */ |
28 | 28 | class StorageTimeoutException extends StorageNotAvailableException { |
29 | 29 | |
30 | - /** |
|
31 | - * StorageTimeoutException constructor. |
|
32 | - * |
|
33 | - * @param string $message |
|
34 | - * @param \Exception|null $previous |
|
35 | - * @since 9.0.0 |
|
36 | - */ |
|
37 | - public function __construct($message = '', \Exception $previous = null) { |
|
38 | - $l = \OC::$server->getL10N('core'); |
|
39 | - parent::__construct($l->t('Storage connection timeout. %s', [$message]), self::STATUS_TIMEOUT, $previous); |
|
40 | - } |
|
30 | + /** |
|
31 | + * StorageTimeoutException constructor. |
|
32 | + * |
|
33 | + * @param string $message |
|
34 | + * @param \Exception|null $previous |
|
35 | + * @since 9.0.0 |
|
36 | + */ |
|
37 | + public function __construct($message = '', \Exception $previous = null) { |
|
38 | + $l = \OC::$server->getL10N('core'); |
|
39 | + parent::__construct($l->t('Storage connection timeout. %s', [$message]), self::STATUS_TIMEOUT, $previous); |
|
40 | + } |
|
41 | 41 | } |
@@ -27,15 +27,15 @@ |
||
27 | 27 | */ |
28 | 28 | class StorageConnectionException extends StorageNotAvailableException { |
29 | 29 | |
30 | - /** |
|
31 | - * StorageConnectionException constructor. |
|
32 | - * |
|
33 | - * @param string $message |
|
34 | - * @param \Exception|null $previous |
|
35 | - * @since 9.0.0 |
|
36 | - */ |
|
37 | - public function __construct($message = '', \Exception $previous = null) { |
|
38 | - $l = \OC::$server->getL10N('core'); |
|
39 | - parent::__construct($l->t('Storage connection error. %s', [$message]), self::STATUS_NETWORK_ERROR, $previous); |
|
40 | - } |
|
30 | + /** |
|
31 | + * StorageConnectionException constructor. |
|
32 | + * |
|
33 | + * @param string $message |
|
34 | + * @param \Exception|null $previous |
|
35 | + * @since 9.0.0 |
|
36 | + */ |
|
37 | + public function __construct($message = '', \Exception $previous = null) { |
|
38 | + $l = \OC::$server->getL10N('core'); |
|
39 | + parent::__construct($l->t('Storage connection error. %s', [$message]), self::STATUS_NETWORK_ERROR, $previous); |
|
40 | + } |
|
41 | 41 | } |
@@ -29,15 +29,15 @@ |
||
29 | 29 | * Authentication mechanism or backend has insufficient data |
30 | 30 | */ |
31 | 31 | class InsufficientDataForMeaningfulAnswerException extends StorageNotAvailableException { |
32 | - /** |
|
33 | - * StorageNotAvailableException constructor. |
|
34 | - * |
|
35 | - * @param string $message |
|
36 | - * @param int $code |
|
37 | - * @param \Exception|null $previous |
|
38 | - * @since 6.0.0 |
|
39 | - */ |
|
40 | - public function __construct($message = '', $code = self::STATUS_INDETERMINATE, \Exception $previous = null) { |
|
41 | - parent::__construct($message, $code, $previous); |
|
42 | - } |
|
32 | + /** |
|
33 | + * StorageNotAvailableException constructor. |
|
34 | + * |
|
35 | + * @param string $message |
|
36 | + * @param int $code |
|
37 | + * @param \Exception|null $previous |
|
38 | + * @since 6.0.0 |
|
39 | + */ |
|
40 | + public function __construct($message = '', $code = self::STATUS_INDETERMINATE, \Exception $previous = null) { |
|
41 | + parent::__construct($message, $code, $previous); |
|
42 | + } |
|
43 | 43 | } |
@@ -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 |
@@ -56,555 +56,555 @@ |
||
56 | 56 | * @since 4.0.0 |
57 | 57 | */ |
58 | 58 | class Util { |
59 | - /** @var \OCP\Share\IManager */ |
|
60 | - private static $shareManager; |
|
61 | - |
|
62 | - /** @var array */ |
|
63 | - private static $scripts = []; |
|
64 | - |
|
65 | - /** @var array */ |
|
66 | - private static $scriptDeps = []; |
|
67 | - |
|
68 | - /** @var array */ |
|
69 | - private static $sortedScriptDeps = []; |
|
70 | - |
|
71 | - /** |
|
72 | - * get the current installed version of Nextcloud |
|
73 | - * @return array |
|
74 | - * @since 4.0.0 |
|
75 | - */ |
|
76 | - public static function getVersion() { |
|
77 | - return \OC_Util::getVersion(); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @since 17.0.0 |
|
82 | - */ |
|
83 | - public static function hasExtendedSupport(): bool { |
|
84 | - try { |
|
85 | - /** @var \OCP\Support\Subscription\IRegistry */ |
|
86 | - $subscriptionRegistry = \OC::$server->query(\OCP\Support\Subscription\IRegistry::class); |
|
87 | - return $subscriptionRegistry->delegateHasExtendedSupport(); |
|
88 | - } catch (AppFramework\QueryException $e) { |
|
89 | - } |
|
90 | - return \OC::$server->getConfig()->getSystemValueBool('extendedSupport', false); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Set current update channel |
|
95 | - * @param string $channel |
|
96 | - * @since 8.1.0 |
|
97 | - */ |
|
98 | - public static function setChannel($channel) { |
|
99 | - \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Get current update channel |
|
104 | - * @return string |
|
105 | - * @since 8.1.0 |
|
106 | - */ |
|
107 | - public static function getChannel() { |
|
108 | - return \OC_Util::getChannel(); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * write a message in the log |
|
113 | - * @param string $app |
|
114 | - * @param string $message |
|
115 | - * @param int $level |
|
116 | - * @since 4.0.0 |
|
117 | - * @deprecated 13.0.0 use log of \OCP\ILogger |
|
118 | - */ |
|
119 | - public static function writeLog($app, $message, $level) { |
|
120 | - $context = ['app' => $app]; |
|
121 | - \OC::$server->getLogger()->log($level, $message, $context); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * check if sharing is disabled for the current user |
|
126 | - * |
|
127 | - * @return boolean |
|
128 | - * @since 7.0.0 |
|
129 | - * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser |
|
130 | - */ |
|
131 | - public static function isSharingDisabledForUser() { |
|
132 | - if (self::$shareManager === null) { |
|
133 | - self::$shareManager = \OC::$server->getShareManager(); |
|
134 | - } |
|
135 | - |
|
136 | - $user = \OC::$server->getUserSession()->getUser(); |
|
137 | - if ($user !== null) { |
|
138 | - $user = $user->getUID(); |
|
139 | - } |
|
140 | - |
|
141 | - return self::$shareManager->sharingDisabledForUser($user); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * get l10n object |
|
146 | - * @param string $application |
|
147 | - * @param string|null $language |
|
148 | - * @return \OCP\IL10N |
|
149 | - * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
150 | - */ |
|
151 | - public static function getL10N($application, $language = null) { |
|
152 | - return \OC::$server->getL10N($application, $language); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * add a css file |
|
157 | - * @param string $application |
|
158 | - * @param string $file |
|
159 | - * @since 4.0.0 |
|
160 | - */ |
|
161 | - public static function addStyle($application, $file = null) { |
|
162 | - \OC_Util::addStyle($application, $file); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * add a javascript file |
|
167 | - * |
|
168 | - * @param string $application |
|
169 | - * @param string|null $file |
|
170 | - * @param string $afterAppId |
|
171 | - * @since 4.0.0 |
|
172 | - */ |
|
173 | - public static function addScript(string $application, string $file = null, string $afterAppId = 'core'): void { |
|
174 | - if (!empty($application)) { |
|
175 | - $path = "$application/js/$file"; |
|
176 | - } else { |
|
177 | - $path = "js/$file"; |
|
178 | - } |
|
179 | - |
|
180 | - // Inject js translations if we load a script for |
|
181 | - // a specific app that is not core, as those js files |
|
182 | - // need separate handling |
|
183 | - if ($application !== 'core' |
|
184 | - && $file !== null |
|
185 | - && strpos($file, 'l10n') === false) { |
|
186 | - self::addTranslations($application); |
|
187 | - } |
|
188 | - |
|
189 | - // store app in dependency list |
|
190 | - if (!array_key_exists($application, self::$scriptDeps)) { |
|
191 | - self::$scriptDeps[$application] = new AppScriptDependency($application, [$afterAppId]); |
|
192 | - } else { |
|
193 | - self::$scriptDeps[$application]->addDep($afterAppId); |
|
194 | - } |
|
195 | - |
|
196 | - self::$scripts[$application][] = $path; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Return the list of scripts injected to the page |
|
201 | - * |
|
202 | - * @return array |
|
203 | - * @since 24.0.0 |
|
204 | - */ |
|
205 | - public static function getScripts(): array { |
|
206 | - // Sort scriptDeps into sortedScriptDeps |
|
207 | - $scriptSort = \OC::$server->get(AppScriptSort::class); |
|
208 | - $sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps); |
|
209 | - |
|
210 | - // Flatten array and remove duplicates |
|
211 | - $sortedScripts = $sortedScripts ? array_merge(...array_values(($sortedScripts))) : []; |
|
212 | - |
|
213 | - // Override core-common and core-main order |
|
214 | - array_unshift($sortedScripts, 'core/js/common', 'core/js/main'); |
|
215 | - |
|
216 | - return array_unique($sortedScripts); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Add a translation JS file |
|
221 | - * @param string $application application id |
|
222 | - * @param string $languageCode language code, defaults to the current locale |
|
223 | - * @since 8.0.0 |
|
224 | - */ |
|
225 | - public static function addTranslations($application, $languageCode = null) { |
|
226 | - if (is_null($languageCode)) { |
|
227 | - $languageCode = \OC::$server->getL10NFactory()->findLanguage($application); |
|
228 | - } |
|
229 | - if (!empty($application)) { |
|
230 | - $path = "$application/l10n/$languageCode"; |
|
231 | - } else { |
|
232 | - $path = "l10n/$languageCode"; |
|
233 | - } |
|
234 | - self::$scripts[$application][] = $path; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Add a custom element to the header |
|
239 | - * If $text is null then the element will be written as empty element. |
|
240 | - * So use "" to get a closing tag. |
|
241 | - * @param string $tag tag name of the element |
|
242 | - * @param array $attributes array of attributes for the element |
|
243 | - * @param string $text the text content for the element |
|
244 | - * @since 4.0.0 |
|
245 | - */ |
|
246 | - public static function addHeader($tag, $attributes, $text = null) { |
|
247 | - \OC_Util::addHeader($tag, $attributes, $text); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Creates an absolute url to the given app and file. |
|
252 | - * @param string $app app |
|
253 | - * @param string $file file |
|
254 | - * @param array $args array with param=>value, will be appended to the returned url |
|
255 | - * The value of $args will be urlencoded |
|
256 | - * @return string the url |
|
257 | - * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
258 | - */ |
|
259 | - public static function linkToAbsolute($app, $file, $args = []) { |
|
260 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
261 | - return $urlGenerator->getAbsoluteURL( |
|
262 | - $urlGenerator->linkTo($app, $file, $args) |
|
263 | - ); |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Creates an absolute url for remote use. |
|
268 | - * @param string $service id |
|
269 | - * @return string the url |
|
270 | - * @since 4.0.0 |
|
271 | - */ |
|
272 | - public static function linkToRemote($service) { |
|
273 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
274 | - $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
275 | - return $urlGenerator->getAbsoluteURL( |
|
276 | - $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
277 | - ); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * Creates an absolute url for public use |
|
282 | - * @param string $service id |
|
283 | - * @return string the url |
|
284 | - * @since 4.5.0 |
|
285 | - * @deprecated 15.0.0 - use OCP\IURLGenerator |
|
286 | - */ |
|
287 | - public static function linkToPublic($service) { |
|
288 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
289 | - if ($service === 'files') { |
|
290 | - return $urlGenerator->getAbsoluteURL('/s'); |
|
291 | - } |
|
292 | - return $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'public.php').'?service='.$service); |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Returns the server host name without an eventual port number |
|
297 | - * @return string the server hostname |
|
298 | - * @since 5.0.0 |
|
299 | - */ |
|
300 | - public static function getServerHostName() { |
|
301 | - $host_name = \OC::$server->getRequest()->getServerHost(); |
|
302 | - // strip away port number (if existing) |
|
303 | - $colon_pos = strpos($host_name, ':'); |
|
304 | - if ($colon_pos != false) { |
|
305 | - $host_name = substr($host_name, 0, $colon_pos); |
|
306 | - } |
|
307 | - return $host_name; |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Returns the default email address |
|
312 | - * @param string $user_part the user part of the address |
|
313 | - * @return string the default email address |
|
314 | - * |
|
315 | - * Assembles a default email address (using the server hostname |
|
316 | - * and the given user part, and returns it |
|
317 | - * Example: when given lostpassword-noreply as $user_part param, |
|
318 | - * and is currently accessed via http(s)://example.com/, |
|
319 | - * it would return '[email protected]' |
|
320 | - * |
|
321 | - * If the configuration value 'mail_from_address' is set in |
|
322 | - * config.php, this value will override the $user_part that |
|
323 | - * is passed to this function |
|
324 | - * @since 5.0.0 |
|
325 | - */ |
|
326 | - public static function getDefaultEmailAddress($user_part) { |
|
327 | - $config = \OC::$server->getConfig(); |
|
328 | - $user_part = $config->getSystemValue('mail_from_address', $user_part); |
|
329 | - $host_name = self::getServerHostName(); |
|
330 | - $host_name = $config->getSystemValue('mail_domain', $host_name); |
|
331 | - $defaultEmailAddress = $user_part.'@'.$host_name; |
|
332 | - |
|
333 | - $mailer = \OC::$server->getMailer(); |
|
334 | - if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
335 | - return $defaultEmailAddress; |
|
336 | - } |
|
337 | - |
|
338 | - // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
339 | - return $user_part.'@localhost.localdomain'; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Make a human file size (2048 to 2 kB) |
|
344 | - * @param int $bytes file size in bytes |
|
345 | - * @return string a human readable file size |
|
346 | - * @since 4.0.0 |
|
347 | - */ |
|
348 | - public static function humanFileSize($bytes) { |
|
349 | - return \OC_Helper::humanFileSize($bytes); |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * Make a computer file size (2 kB to 2048) |
|
354 | - * @param string $str file size in a fancy format |
|
355 | - * @return float|false a file size in bytes |
|
356 | - * |
|
357 | - * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 |
|
358 | - * @since 4.0.0 |
|
359 | - */ |
|
360 | - public static function computerFileSize($str) { |
|
361 | - return \OC_Helper::computerFileSize($str); |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * connects a function to a hook |
|
366 | - * |
|
367 | - * @param string $signalClass class name of emitter |
|
368 | - * @param string $signalName name of signal |
|
369 | - * @param string|object $slotClass class name of slot |
|
370 | - * @param string $slotName name of slot |
|
371 | - * @return bool |
|
372 | - * |
|
373 | - * This function makes it very easy to connect to use hooks. |
|
374 | - * |
|
375 | - * TODO: write example |
|
376 | - * @since 4.0.0 |
|
377 | - * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener |
|
378 | - */ |
|
379 | - public static function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
|
380 | - return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Emits a signal. To get data from the slot use references! |
|
385 | - * @param string $signalclass class name of emitter |
|
386 | - * @param string $signalname name of signal |
|
387 | - * @param array $params default: array() array with additional data |
|
388 | - * @return bool true if slots exists or false if not |
|
389 | - * |
|
390 | - * TODO: write example |
|
391 | - * @since 4.0.0 |
|
392 | - * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::dispatchTypedEvent |
|
393 | - */ |
|
394 | - public static function emitHook($signalclass, $signalname, $params = []) { |
|
395 | - return \OC_Hook::emit($signalclass, $signalname, $params); |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
400 | - * multiple OC_Template elements which invoke `callRegister`. If the value |
|
401 | - * would not be cached these unit-tests would fail. |
|
402 | - * @var string |
|
403 | - */ |
|
404 | - private static $token = ''; |
|
405 | - |
|
406 | - /** |
|
407 | - * Register an get/post call. This is important to prevent CSRF attacks |
|
408 | - * @since 4.5.0 |
|
409 | - */ |
|
410 | - public static function callRegister() { |
|
411 | - if (self::$token === '') { |
|
412 | - self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
|
413 | - } |
|
414 | - return self::$token; |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Used to sanitize HTML |
|
419 | - * |
|
420 | - * This function is used to sanitize HTML and should be applied on any |
|
421 | - * string or array of strings before displaying it on a web page. |
|
422 | - * |
|
423 | - * @param string|string[] $value |
|
424 | - * @return string|string[] an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
425 | - * @since 4.5.0 |
|
426 | - */ |
|
427 | - public static function sanitizeHTML($value) { |
|
428 | - return \OC_Util::sanitizeHTML($value); |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Public function to encode url parameters |
|
433 | - * |
|
434 | - * This function is used to encode path to file before output. |
|
435 | - * Encoding is done according to RFC 3986 with one exception: |
|
436 | - * Character '/' is preserved as is. |
|
437 | - * |
|
438 | - * @param string $component part of URI to encode |
|
439 | - * @return string |
|
440 | - * @since 6.0.0 |
|
441 | - */ |
|
442 | - public static function encodePath($component) { |
|
443 | - return \OC_Util::encodePath($component); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
448 | - * |
|
449 | - * @param array $input The array to work on |
|
450 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
451 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
452 | - * @return array |
|
453 | - * @since 4.5.0 |
|
454 | - */ |
|
455 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
456 | - return \OC_Helper::mb_array_change_key_case($input, $case, $encoding); |
|
457 | - } |
|
458 | - |
|
459 | - /** |
|
460 | - * performs a search in a nested array |
|
461 | - * |
|
462 | - * @param array $haystack the array to be searched |
|
463 | - * @param string $needle the search string |
|
464 | - * @param mixed $index optional, only search this key name |
|
465 | - * @return mixed the key of the matching field, otherwise false |
|
466 | - * @since 4.5.0 |
|
467 | - * @deprecated 15.0.0 |
|
468 | - */ |
|
469 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
470 | - return \OC_Helper::recursiveArraySearch($haystack, $needle, $index); |
|
471 | - } |
|
472 | - |
|
473 | - /** |
|
474 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
475 | - * |
|
476 | - * @param string $dir the current folder where the user currently operates |
|
477 | - * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
478 | - * @return int number of bytes representing |
|
479 | - * @since 5.0.0 |
|
480 | - */ |
|
481 | - public static function maxUploadFilesize($dir, $free = null) { |
|
482 | - return \OC_Helper::maxUploadFilesize($dir, $free); |
|
483 | - } |
|
484 | - |
|
485 | - /** |
|
486 | - * Calculate free space left within user quota |
|
487 | - * @param string $dir the current folder where the user currently operates |
|
488 | - * @return int number of bytes representing |
|
489 | - * @since 7.0.0 |
|
490 | - */ |
|
491 | - public static function freeSpace($dir) { |
|
492 | - return \OC_Helper::freeSpace($dir); |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * Calculate PHP upload limit |
|
497 | - * |
|
498 | - * @return int number of bytes representing |
|
499 | - * @since 7.0.0 |
|
500 | - */ |
|
501 | - public static function uploadLimit() { |
|
502 | - return \OC_Helper::uploadLimit(); |
|
503 | - } |
|
504 | - |
|
505 | - /** |
|
506 | - * Returns whether the given file name is valid |
|
507 | - * @param string $file file name to check |
|
508 | - * @return bool true if the file name is valid, false otherwise |
|
509 | - * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
|
510 | - * @since 7.0.0 |
|
511 | - * @suppress PhanDeprecatedFunction |
|
512 | - */ |
|
513 | - public static function isValidFileName($file) { |
|
514 | - return \OC_Util::isValidFileName($file); |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * Compare two strings to provide a natural sort |
|
519 | - * @param string $a first string to compare |
|
520 | - * @param string $b second string to compare |
|
521 | - * @return int -1 if $b comes before $a, 1 if $a comes before $b |
|
522 | - * or 0 if the strings are identical |
|
523 | - * @since 7.0.0 |
|
524 | - */ |
|
525 | - public static function naturalSortCompare($a, $b) { |
|
526 | - return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Check if a password is required for each public link |
|
531 | - * |
|
532 | - * @param bool $checkGroupMembership Check group membership exclusion |
|
533 | - * @return boolean |
|
534 | - * @since 7.0.0 |
|
535 | - */ |
|
536 | - public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = true) { |
|
537 | - return \OC_Util::isPublicLinkPasswordRequired($checkGroupMembership); |
|
538 | - } |
|
539 | - |
|
540 | - /** |
|
541 | - * check if share API enforces a default expire date |
|
542 | - * @return boolean |
|
543 | - * @since 8.0.0 |
|
544 | - */ |
|
545 | - public static function isDefaultExpireDateEnforced() { |
|
546 | - return \OC_Util::isDefaultExpireDateEnforced(); |
|
547 | - } |
|
548 | - |
|
549 | - protected static $needUpgradeCache = null; |
|
550 | - |
|
551 | - /** |
|
552 | - * Checks whether the current version needs upgrade. |
|
553 | - * |
|
554 | - * @return bool true if upgrade is needed, false otherwise |
|
555 | - * @since 7.0.0 |
|
556 | - */ |
|
557 | - public static function needUpgrade() { |
|
558 | - if (!isset(self::$needUpgradeCache)) { |
|
559 | - self::$needUpgradeCache = \OC_Util::needUpgrade(\OC::$server->getSystemConfig()); |
|
560 | - } |
|
561 | - return self::$needUpgradeCache; |
|
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * Sometimes a string has to be shortened to fit within a certain maximum |
|
566 | - * data length in bytes. substr() you may break multibyte characters, |
|
567 | - * because it operates on single byte level. mb_substr() operates on |
|
568 | - * characters, so does not ensure that the shortend string satisfies the |
|
569 | - * max length in bytes. |
|
570 | - * |
|
571 | - * For example, json_encode is messing with multibyte characters a lot, |
|
572 | - * replacing them with something along "\u1234". |
|
573 | - * |
|
574 | - * This function shortens the string with by $accurancy (-5) from |
|
575 | - * $dataLength characters, until it fits within $dataLength bytes. |
|
576 | - * |
|
577 | - * @since 23.0.0 |
|
578 | - */ |
|
579 | - public static function shortenMultibyteString(string $subject, int $dataLength, int $accuracy = 5): string { |
|
580 | - $temp = mb_substr($subject, 0, $dataLength); |
|
581 | - // json encodes encapsulates the string in double quotes, they need to be substracted |
|
582 | - while ((strlen(json_encode($temp)) - 2) > $dataLength) { |
|
583 | - $temp = mb_substr($temp, 0, -$accuracy); |
|
584 | - } |
|
585 | - return $temp; |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Check if a function is enabled in the php configuration |
|
590 | - * |
|
591 | - * @since 25.0.0 |
|
592 | - */ |
|
593 | - public static function isFunctionEnabled(string $functionName): bool { |
|
594 | - if (!function_exists($functionName)) { |
|
595 | - return false; |
|
596 | - } |
|
597 | - $ini = \OCP\Server::get(IniGetWrapper::class); |
|
598 | - $disabled = explode(',', $ini->get('disable_functions') ?: ''); |
|
599 | - $disabled = array_map('trim', $disabled); |
|
600 | - if (in_array($functionName, $disabled)) { |
|
601 | - return false; |
|
602 | - } |
|
603 | - $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist') ?: ''); |
|
604 | - $disabled = array_map('trim', $disabled); |
|
605 | - if (in_array($functionName, $disabled)) { |
|
606 | - return false; |
|
607 | - } |
|
608 | - return true; |
|
609 | - } |
|
59 | + /** @var \OCP\Share\IManager */ |
|
60 | + private static $shareManager; |
|
61 | + |
|
62 | + /** @var array */ |
|
63 | + private static $scripts = []; |
|
64 | + |
|
65 | + /** @var array */ |
|
66 | + private static $scriptDeps = []; |
|
67 | + |
|
68 | + /** @var array */ |
|
69 | + private static $sortedScriptDeps = []; |
|
70 | + |
|
71 | + /** |
|
72 | + * get the current installed version of Nextcloud |
|
73 | + * @return array |
|
74 | + * @since 4.0.0 |
|
75 | + */ |
|
76 | + public static function getVersion() { |
|
77 | + return \OC_Util::getVersion(); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @since 17.0.0 |
|
82 | + */ |
|
83 | + public static function hasExtendedSupport(): bool { |
|
84 | + try { |
|
85 | + /** @var \OCP\Support\Subscription\IRegistry */ |
|
86 | + $subscriptionRegistry = \OC::$server->query(\OCP\Support\Subscription\IRegistry::class); |
|
87 | + return $subscriptionRegistry->delegateHasExtendedSupport(); |
|
88 | + } catch (AppFramework\QueryException $e) { |
|
89 | + } |
|
90 | + return \OC::$server->getConfig()->getSystemValueBool('extendedSupport', false); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Set current update channel |
|
95 | + * @param string $channel |
|
96 | + * @since 8.1.0 |
|
97 | + */ |
|
98 | + public static function setChannel($channel) { |
|
99 | + \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Get current update channel |
|
104 | + * @return string |
|
105 | + * @since 8.1.0 |
|
106 | + */ |
|
107 | + public static function getChannel() { |
|
108 | + return \OC_Util::getChannel(); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * write a message in the log |
|
113 | + * @param string $app |
|
114 | + * @param string $message |
|
115 | + * @param int $level |
|
116 | + * @since 4.0.0 |
|
117 | + * @deprecated 13.0.0 use log of \OCP\ILogger |
|
118 | + */ |
|
119 | + public static function writeLog($app, $message, $level) { |
|
120 | + $context = ['app' => $app]; |
|
121 | + \OC::$server->getLogger()->log($level, $message, $context); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * check if sharing is disabled for the current user |
|
126 | + * |
|
127 | + * @return boolean |
|
128 | + * @since 7.0.0 |
|
129 | + * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser |
|
130 | + */ |
|
131 | + public static function isSharingDisabledForUser() { |
|
132 | + if (self::$shareManager === null) { |
|
133 | + self::$shareManager = \OC::$server->getShareManager(); |
|
134 | + } |
|
135 | + |
|
136 | + $user = \OC::$server->getUserSession()->getUser(); |
|
137 | + if ($user !== null) { |
|
138 | + $user = $user->getUID(); |
|
139 | + } |
|
140 | + |
|
141 | + return self::$shareManager->sharingDisabledForUser($user); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * get l10n object |
|
146 | + * @param string $application |
|
147 | + * @param string|null $language |
|
148 | + * @return \OCP\IL10N |
|
149 | + * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
150 | + */ |
|
151 | + public static function getL10N($application, $language = null) { |
|
152 | + return \OC::$server->getL10N($application, $language); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * add a css file |
|
157 | + * @param string $application |
|
158 | + * @param string $file |
|
159 | + * @since 4.0.0 |
|
160 | + */ |
|
161 | + public static function addStyle($application, $file = null) { |
|
162 | + \OC_Util::addStyle($application, $file); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * add a javascript file |
|
167 | + * |
|
168 | + * @param string $application |
|
169 | + * @param string|null $file |
|
170 | + * @param string $afterAppId |
|
171 | + * @since 4.0.0 |
|
172 | + */ |
|
173 | + public static function addScript(string $application, string $file = null, string $afterAppId = 'core'): void { |
|
174 | + if (!empty($application)) { |
|
175 | + $path = "$application/js/$file"; |
|
176 | + } else { |
|
177 | + $path = "js/$file"; |
|
178 | + } |
|
179 | + |
|
180 | + // Inject js translations if we load a script for |
|
181 | + // a specific app that is not core, as those js files |
|
182 | + // need separate handling |
|
183 | + if ($application !== 'core' |
|
184 | + && $file !== null |
|
185 | + && strpos($file, 'l10n') === false) { |
|
186 | + self::addTranslations($application); |
|
187 | + } |
|
188 | + |
|
189 | + // store app in dependency list |
|
190 | + if (!array_key_exists($application, self::$scriptDeps)) { |
|
191 | + self::$scriptDeps[$application] = new AppScriptDependency($application, [$afterAppId]); |
|
192 | + } else { |
|
193 | + self::$scriptDeps[$application]->addDep($afterAppId); |
|
194 | + } |
|
195 | + |
|
196 | + self::$scripts[$application][] = $path; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Return the list of scripts injected to the page |
|
201 | + * |
|
202 | + * @return array |
|
203 | + * @since 24.0.0 |
|
204 | + */ |
|
205 | + public static function getScripts(): array { |
|
206 | + // Sort scriptDeps into sortedScriptDeps |
|
207 | + $scriptSort = \OC::$server->get(AppScriptSort::class); |
|
208 | + $sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps); |
|
209 | + |
|
210 | + // Flatten array and remove duplicates |
|
211 | + $sortedScripts = $sortedScripts ? array_merge(...array_values(($sortedScripts))) : []; |
|
212 | + |
|
213 | + // Override core-common and core-main order |
|
214 | + array_unshift($sortedScripts, 'core/js/common', 'core/js/main'); |
|
215 | + |
|
216 | + return array_unique($sortedScripts); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Add a translation JS file |
|
221 | + * @param string $application application id |
|
222 | + * @param string $languageCode language code, defaults to the current locale |
|
223 | + * @since 8.0.0 |
|
224 | + */ |
|
225 | + public static function addTranslations($application, $languageCode = null) { |
|
226 | + if (is_null($languageCode)) { |
|
227 | + $languageCode = \OC::$server->getL10NFactory()->findLanguage($application); |
|
228 | + } |
|
229 | + if (!empty($application)) { |
|
230 | + $path = "$application/l10n/$languageCode"; |
|
231 | + } else { |
|
232 | + $path = "l10n/$languageCode"; |
|
233 | + } |
|
234 | + self::$scripts[$application][] = $path; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Add a custom element to the header |
|
239 | + * If $text is null then the element will be written as empty element. |
|
240 | + * So use "" to get a closing tag. |
|
241 | + * @param string $tag tag name of the element |
|
242 | + * @param array $attributes array of attributes for the element |
|
243 | + * @param string $text the text content for the element |
|
244 | + * @since 4.0.0 |
|
245 | + */ |
|
246 | + public static function addHeader($tag, $attributes, $text = null) { |
|
247 | + \OC_Util::addHeader($tag, $attributes, $text); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Creates an absolute url to the given app and file. |
|
252 | + * @param string $app app |
|
253 | + * @param string $file file |
|
254 | + * @param array $args array with param=>value, will be appended to the returned url |
|
255 | + * The value of $args will be urlencoded |
|
256 | + * @return string the url |
|
257 | + * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
258 | + */ |
|
259 | + public static function linkToAbsolute($app, $file, $args = []) { |
|
260 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
261 | + return $urlGenerator->getAbsoluteURL( |
|
262 | + $urlGenerator->linkTo($app, $file, $args) |
|
263 | + ); |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Creates an absolute url for remote use. |
|
268 | + * @param string $service id |
|
269 | + * @return string the url |
|
270 | + * @since 4.0.0 |
|
271 | + */ |
|
272 | + public static function linkToRemote($service) { |
|
273 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
274 | + $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
275 | + return $urlGenerator->getAbsoluteURL( |
|
276 | + $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
277 | + ); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * Creates an absolute url for public use |
|
282 | + * @param string $service id |
|
283 | + * @return string the url |
|
284 | + * @since 4.5.0 |
|
285 | + * @deprecated 15.0.0 - use OCP\IURLGenerator |
|
286 | + */ |
|
287 | + public static function linkToPublic($service) { |
|
288 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
289 | + if ($service === 'files') { |
|
290 | + return $urlGenerator->getAbsoluteURL('/s'); |
|
291 | + } |
|
292 | + return $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'public.php').'?service='.$service); |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Returns the server host name without an eventual port number |
|
297 | + * @return string the server hostname |
|
298 | + * @since 5.0.0 |
|
299 | + */ |
|
300 | + public static function getServerHostName() { |
|
301 | + $host_name = \OC::$server->getRequest()->getServerHost(); |
|
302 | + // strip away port number (if existing) |
|
303 | + $colon_pos = strpos($host_name, ':'); |
|
304 | + if ($colon_pos != false) { |
|
305 | + $host_name = substr($host_name, 0, $colon_pos); |
|
306 | + } |
|
307 | + return $host_name; |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Returns the default email address |
|
312 | + * @param string $user_part the user part of the address |
|
313 | + * @return string the default email address |
|
314 | + * |
|
315 | + * Assembles a default email address (using the server hostname |
|
316 | + * and the given user part, and returns it |
|
317 | + * Example: when given lostpassword-noreply as $user_part param, |
|
318 | + * and is currently accessed via http(s)://example.com/, |
|
319 | + * it would return '[email protected]' |
|
320 | + * |
|
321 | + * If the configuration value 'mail_from_address' is set in |
|
322 | + * config.php, this value will override the $user_part that |
|
323 | + * is passed to this function |
|
324 | + * @since 5.0.0 |
|
325 | + */ |
|
326 | + public static function getDefaultEmailAddress($user_part) { |
|
327 | + $config = \OC::$server->getConfig(); |
|
328 | + $user_part = $config->getSystemValue('mail_from_address', $user_part); |
|
329 | + $host_name = self::getServerHostName(); |
|
330 | + $host_name = $config->getSystemValue('mail_domain', $host_name); |
|
331 | + $defaultEmailAddress = $user_part.'@'.$host_name; |
|
332 | + |
|
333 | + $mailer = \OC::$server->getMailer(); |
|
334 | + if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
335 | + return $defaultEmailAddress; |
|
336 | + } |
|
337 | + |
|
338 | + // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
339 | + return $user_part.'@localhost.localdomain'; |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Make a human file size (2048 to 2 kB) |
|
344 | + * @param int $bytes file size in bytes |
|
345 | + * @return string a human readable file size |
|
346 | + * @since 4.0.0 |
|
347 | + */ |
|
348 | + public static function humanFileSize($bytes) { |
|
349 | + return \OC_Helper::humanFileSize($bytes); |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * Make a computer file size (2 kB to 2048) |
|
354 | + * @param string $str file size in a fancy format |
|
355 | + * @return float|false a file size in bytes |
|
356 | + * |
|
357 | + * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 |
|
358 | + * @since 4.0.0 |
|
359 | + */ |
|
360 | + public static function computerFileSize($str) { |
|
361 | + return \OC_Helper::computerFileSize($str); |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * connects a function to a hook |
|
366 | + * |
|
367 | + * @param string $signalClass class name of emitter |
|
368 | + * @param string $signalName name of signal |
|
369 | + * @param string|object $slotClass class name of slot |
|
370 | + * @param string $slotName name of slot |
|
371 | + * @return bool |
|
372 | + * |
|
373 | + * This function makes it very easy to connect to use hooks. |
|
374 | + * |
|
375 | + * TODO: write example |
|
376 | + * @since 4.0.0 |
|
377 | + * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener |
|
378 | + */ |
|
379 | + public static function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
|
380 | + return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Emits a signal. To get data from the slot use references! |
|
385 | + * @param string $signalclass class name of emitter |
|
386 | + * @param string $signalname name of signal |
|
387 | + * @param array $params default: array() array with additional data |
|
388 | + * @return bool true if slots exists or false if not |
|
389 | + * |
|
390 | + * TODO: write example |
|
391 | + * @since 4.0.0 |
|
392 | + * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::dispatchTypedEvent |
|
393 | + */ |
|
394 | + public static function emitHook($signalclass, $signalname, $params = []) { |
|
395 | + return \OC_Hook::emit($signalclass, $signalname, $params); |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
400 | + * multiple OC_Template elements which invoke `callRegister`. If the value |
|
401 | + * would not be cached these unit-tests would fail. |
|
402 | + * @var string |
|
403 | + */ |
|
404 | + private static $token = ''; |
|
405 | + |
|
406 | + /** |
|
407 | + * Register an get/post call. This is important to prevent CSRF attacks |
|
408 | + * @since 4.5.0 |
|
409 | + */ |
|
410 | + public static function callRegister() { |
|
411 | + if (self::$token === '') { |
|
412 | + self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
|
413 | + } |
|
414 | + return self::$token; |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Used to sanitize HTML |
|
419 | + * |
|
420 | + * This function is used to sanitize HTML and should be applied on any |
|
421 | + * string or array of strings before displaying it on a web page. |
|
422 | + * |
|
423 | + * @param string|string[] $value |
|
424 | + * @return string|string[] an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
425 | + * @since 4.5.0 |
|
426 | + */ |
|
427 | + public static function sanitizeHTML($value) { |
|
428 | + return \OC_Util::sanitizeHTML($value); |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Public function to encode url parameters |
|
433 | + * |
|
434 | + * This function is used to encode path to file before output. |
|
435 | + * Encoding is done according to RFC 3986 with one exception: |
|
436 | + * Character '/' is preserved as is. |
|
437 | + * |
|
438 | + * @param string $component part of URI to encode |
|
439 | + * @return string |
|
440 | + * @since 6.0.0 |
|
441 | + */ |
|
442 | + public static function encodePath($component) { |
|
443 | + return \OC_Util::encodePath($component); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
448 | + * |
|
449 | + * @param array $input The array to work on |
|
450 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
451 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
452 | + * @return array |
|
453 | + * @since 4.5.0 |
|
454 | + */ |
|
455 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
456 | + return \OC_Helper::mb_array_change_key_case($input, $case, $encoding); |
|
457 | + } |
|
458 | + |
|
459 | + /** |
|
460 | + * performs a search in a nested array |
|
461 | + * |
|
462 | + * @param array $haystack the array to be searched |
|
463 | + * @param string $needle the search string |
|
464 | + * @param mixed $index optional, only search this key name |
|
465 | + * @return mixed the key of the matching field, otherwise false |
|
466 | + * @since 4.5.0 |
|
467 | + * @deprecated 15.0.0 |
|
468 | + */ |
|
469 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
470 | + return \OC_Helper::recursiveArraySearch($haystack, $needle, $index); |
|
471 | + } |
|
472 | + |
|
473 | + /** |
|
474 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
475 | + * |
|
476 | + * @param string $dir the current folder where the user currently operates |
|
477 | + * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
478 | + * @return int number of bytes representing |
|
479 | + * @since 5.0.0 |
|
480 | + */ |
|
481 | + public static function maxUploadFilesize($dir, $free = null) { |
|
482 | + return \OC_Helper::maxUploadFilesize($dir, $free); |
|
483 | + } |
|
484 | + |
|
485 | + /** |
|
486 | + * Calculate free space left within user quota |
|
487 | + * @param string $dir the current folder where the user currently operates |
|
488 | + * @return int number of bytes representing |
|
489 | + * @since 7.0.0 |
|
490 | + */ |
|
491 | + public static function freeSpace($dir) { |
|
492 | + return \OC_Helper::freeSpace($dir); |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * Calculate PHP upload limit |
|
497 | + * |
|
498 | + * @return int number of bytes representing |
|
499 | + * @since 7.0.0 |
|
500 | + */ |
|
501 | + public static function uploadLimit() { |
|
502 | + return \OC_Helper::uploadLimit(); |
|
503 | + } |
|
504 | + |
|
505 | + /** |
|
506 | + * Returns whether the given file name is valid |
|
507 | + * @param string $file file name to check |
|
508 | + * @return bool true if the file name is valid, false otherwise |
|
509 | + * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
|
510 | + * @since 7.0.0 |
|
511 | + * @suppress PhanDeprecatedFunction |
|
512 | + */ |
|
513 | + public static function isValidFileName($file) { |
|
514 | + return \OC_Util::isValidFileName($file); |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * Compare two strings to provide a natural sort |
|
519 | + * @param string $a first string to compare |
|
520 | + * @param string $b second string to compare |
|
521 | + * @return int -1 if $b comes before $a, 1 if $a comes before $b |
|
522 | + * or 0 if the strings are identical |
|
523 | + * @since 7.0.0 |
|
524 | + */ |
|
525 | + public static function naturalSortCompare($a, $b) { |
|
526 | + return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Check if a password is required for each public link |
|
531 | + * |
|
532 | + * @param bool $checkGroupMembership Check group membership exclusion |
|
533 | + * @return boolean |
|
534 | + * @since 7.0.0 |
|
535 | + */ |
|
536 | + public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = true) { |
|
537 | + return \OC_Util::isPublicLinkPasswordRequired($checkGroupMembership); |
|
538 | + } |
|
539 | + |
|
540 | + /** |
|
541 | + * check if share API enforces a default expire date |
|
542 | + * @return boolean |
|
543 | + * @since 8.0.0 |
|
544 | + */ |
|
545 | + public static function isDefaultExpireDateEnforced() { |
|
546 | + return \OC_Util::isDefaultExpireDateEnforced(); |
|
547 | + } |
|
548 | + |
|
549 | + protected static $needUpgradeCache = null; |
|
550 | + |
|
551 | + /** |
|
552 | + * Checks whether the current version needs upgrade. |
|
553 | + * |
|
554 | + * @return bool true if upgrade is needed, false otherwise |
|
555 | + * @since 7.0.0 |
|
556 | + */ |
|
557 | + public static function needUpgrade() { |
|
558 | + if (!isset(self::$needUpgradeCache)) { |
|
559 | + self::$needUpgradeCache = \OC_Util::needUpgrade(\OC::$server->getSystemConfig()); |
|
560 | + } |
|
561 | + return self::$needUpgradeCache; |
|
562 | + } |
|
563 | + |
|
564 | + /** |
|
565 | + * Sometimes a string has to be shortened to fit within a certain maximum |
|
566 | + * data length in bytes. substr() you may break multibyte characters, |
|
567 | + * because it operates on single byte level. mb_substr() operates on |
|
568 | + * characters, so does not ensure that the shortend string satisfies the |
|
569 | + * max length in bytes. |
|
570 | + * |
|
571 | + * For example, json_encode is messing with multibyte characters a lot, |
|
572 | + * replacing them with something along "\u1234". |
|
573 | + * |
|
574 | + * This function shortens the string with by $accurancy (-5) from |
|
575 | + * $dataLength characters, until it fits within $dataLength bytes. |
|
576 | + * |
|
577 | + * @since 23.0.0 |
|
578 | + */ |
|
579 | + public static function shortenMultibyteString(string $subject, int $dataLength, int $accuracy = 5): string { |
|
580 | + $temp = mb_substr($subject, 0, $dataLength); |
|
581 | + // json encodes encapsulates the string in double quotes, they need to be substracted |
|
582 | + while ((strlen(json_encode($temp)) - 2) > $dataLength) { |
|
583 | + $temp = mb_substr($temp, 0, -$accuracy); |
|
584 | + } |
|
585 | + return $temp; |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Check if a function is enabled in the php configuration |
|
590 | + * |
|
591 | + * @since 25.0.0 |
|
592 | + */ |
|
593 | + public static function isFunctionEnabled(string $functionName): bool { |
|
594 | + if (!function_exists($functionName)) { |
|
595 | + return false; |
|
596 | + } |
|
597 | + $ini = \OCP\Server::get(IniGetWrapper::class); |
|
598 | + $disabled = explode(',', $ini->get('disable_functions') ?: ''); |
|
599 | + $disabled = array_map('trim', $disabled); |
|
600 | + if (in_array($functionName, $disabled)) { |
|
601 | + return false; |
|
602 | + } |
|
603 | + $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist') ?: ''); |
|
604 | + $disabled = array_map('trim', $disabled); |
|
605 | + if (in_array($functionName, $disabled)) { |
|
606 | + return false; |
|
607 | + } |
|
608 | + return true; |
|
609 | + } |
|
610 | 610 | } |
@@ -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 | } |
@@ -42,67 +42,67 @@ |
||
42 | 42 | abstract class Middleware { |
43 | 43 | |
44 | 44 | |
45 | - /** |
|
46 | - * This is being run in normal order before the controller is being |
|
47 | - * called which allows several modifications and checks |
|
48 | - * |
|
49 | - * @param Controller $controller the controller that is being called |
|
50 | - * @param string $methodName the name of the method that will be called on |
|
51 | - * the controller |
|
52 | - * @since 6.0.0 |
|
53 | - */ |
|
54 | - public function beforeController($controller, $methodName) { |
|
55 | - } |
|
45 | + /** |
|
46 | + * This is being run in normal order before the controller is being |
|
47 | + * called which allows several modifications and checks |
|
48 | + * |
|
49 | + * @param Controller $controller the controller that is being called |
|
50 | + * @param string $methodName the name of the method that will be called on |
|
51 | + * the controller |
|
52 | + * @since 6.0.0 |
|
53 | + */ |
|
54 | + public function beforeController($controller, $methodName) { |
|
55 | + } |
|
56 | 56 | |
57 | 57 | |
58 | - /** |
|
59 | - * This is being run when either the beforeController method or the |
|
60 | - * controller method itself is throwing an exception. The middleware is |
|
61 | - * asked in reverse order to handle the exception and to return a response. |
|
62 | - * If the response is null, it is assumed that the exception could not be |
|
63 | - * handled and the error will be thrown again |
|
64 | - * |
|
65 | - * @param Controller $controller the controller that is being called |
|
66 | - * @param string $methodName the name of the method that will be called on |
|
67 | - * the controller |
|
68 | - * @param \Exception $exception the thrown exception |
|
69 | - * @throws \Exception the passed in exception if it can't handle it |
|
70 | - * @return Response a Response object in case that the exception was handled |
|
71 | - * @since 6.0.0 |
|
72 | - */ |
|
73 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
74 | - throw $exception; |
|
75 | - } |
|
58 | + /** |
|
59 | + * This is being run when either the beforeController method or the |
|
60 | + * controller method itself is throwing an exception. The middleware is |
|
61 | + * asked in reverse order to handle the exception and to return a response. |
|
62 | + * If the response is null, it is assumed that the exception could not be |
|
63 | + * handled and the error will be thrown again |
|
64 | + * |
|
65 | + * @param Controller $controller the controller that is being called |
|
66 | + * @param string $methodName the name of the method that will be called on |
|
67 | + * the controller |
|
68 | + * @param \Exception $exception the thrown exception |
|
69 | + * @throws \Exception the passed in exception if it can't handle it |
|
70 | + * @return Response a Response object in case that the exception was handled |
|
71 | + * @since 6.0.0 |
|
72 | + */ |
|
73 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
74 | + throw $exception; |
|
75 | + } |
|
76 | 76 | |
77 | 77 | |
78 | - /** |
|
79 | - * This is being run after a successful controllermethod call and allows |
|
80 | - * the manipulation of a Response object. The middleware is run in reverse order |
|
81 | - * |
|
82 | - * @param Controller $controller the controller that is being called |
|
83 | - * @param string $methodName the name of the method that will be called on |
|
84 | - * the controller |
|
85 | - * @param Response $response the generated response from the controller |
|
86 | - * @return Response a Response object |
|
87 | - * @since 6.0.0 |
|
88 | - */ |
|
89 | - public function afterController($controller, $methodName, Response $response) { |
|
90 | - return $response; |
|
91 | - } |
|
78 | + /** |
|
79 | + * This is being run after a successful controllermethod call and allows |
|
80 | + * the manipulation of a Response object. The middleware is run in reverse order |
|
81 | + * |
|
82 | + * @param Controller $controller the controller that is being called |
|
83 | + * @param string $methodName the name of the method that will be called on |
|
84 | + * the controller |
|
85 | + * @param Response $response the generated response from the controller |
|
86 | + * @return Response a Response object |
|
87 | + * @since 6.0.0 |
|
88 | + */ |
|
89 | + public function afterController($controller, $methodName, Response $response) { |
|
90 | + return $response; |
|
91 | + } |
|
92 | 92 | |
93 | 93 | |
94 | - /** |
|
95 | - * This is being run after the response object has been rendered and |
|
96 | - * allows the manipulation of the output. The middleware is run in reverse order |
|
97 | - * |
|
98 | - * @param Controller $controller the controller that is being called |
|
99 | - * @param string $methodName the name of the method that will be called on |
|
100 | - * the controller |
|
101 | - * @param string $output the generated output from a response |
|
102 | - * @return string the output that should be printed |
|
103 | - * @since 6.0.0 |
|
104 | - */ |
|
105 | - public function beforeOutput($controller, $methodName, $output) { |
|
106 | - return $output; |
|
107 | - } |
|
94 | + /** |
|
95 | + * This is being run after the response object has been rendered and |
|
96 | + * allows the manipulation of the output. The middleware is run in reverse order |
|
97 | + * |
|
98 | + * @param Controller $controller the controller that is being called |
|
99 | + * @param string $methodName the name of the method that will be called on |
|
100 | + * the controller |
|
101 | + * @param string $output the generated output from a response |
|
102 | + * @return string the output that should be printed |
|
103 | + * @since 6.0.0 |
|
104 | + */ |
|
105 | + public function beforeOutput($controller, $methodName, $output) { |
|
106 | + return $output; |
|
107 | + } |
|
108 | 108 | } |
@@ -32,14 +32,14 @@ |
||
32 | 32 | * @since 9.1.0 |
33 | 33 | */ |
34 | 34 | class OCSNotFoundException extends OCSException { |
35 | - /** |
|
36 | - * OCSNotFoundException constructor. |
|
37 | - * |
|
38 | - * @param string $message |
|
39 | - * @param Exception|null $previous |
|
40 | - * @since 9.1.0 |
|
41 | - */ |
|
42 | - public function __construct($message = '', Exception $previous = null) { |
|
43 | - parent::__construct($message, Http::STATUS_NOT_FOUND, $previous); |
|
44 | - } |
|
35 | + /** |
|
36 | + * OCSNotFoundException constructor. |
|
37 | + * |
|
38 | + * @param string $message |
|
39 | + * @param Exception|null $previous |
|
40 | + * @since 9.1.0 |
|
41 | + */ |
|
42 | + public function __construct($message = '', Exception $previous = null) { |
|
43 | + parent::__construct($message, Http::STATUS_NOT_FOUND, $previous); |
|
44 | + } |
|
45 | 45 | } |
@@ -32,14 +32,14 @@ |
||
32 | 32 | * @since 9.1.0 |
33 | 33 | */ |
34 | 34 | class OCSForbiddenException extends OCSException { |
35 | - /** |
|
36 | - * OCSForbiddenException constructor. |
|
37 | - * |
|
38 | - * @param string $message |
|
39 | - * @param Exception|null $previous |
|
40 | - * @since 9.1.0 |
|
41 | - */ |
|
42 | - public function __construct($message = '', Exception $previous = null) { |
|
43 | - parent::__construct($message, Http::STATUS_FORBIDDEN, $previous); |
|
44 | - } |
|
35 | + /** |
|
36 | + * OCSForbiddenException constructor. |
|
37 | + * |
|
38 | + * @param string $message |
|
39 | + * @param Exception|null $previous |
|
40 | + * @since 9.1.0 |
|
41 | + */ |
|
42 | + public function __construct($message = '', Exception $previous = null) { |
|
43 | + parent::__construct($message, Http::STATUS_FORBIDDEN, $previous); |
|
44 | + } |
|
45 | 45 | } |