@@ -245,6 +245,7 @@ |
||
245 | 245 | |
246 | 246 | /** |
247 | 247 | * Sets user id for session and triggers emit |
248 | + * @param string $uid |
|
248 | 249 | */ |
249 | 250 | public static function setUserId($uid) { |
250 | 251 | $userSession = \OC::$server->getUserSession(); |
@@ -57,590 +57,590 @@ |
||
57 | 57 | */ |
58 | 58 | class OC_User { |
59 | 59 | |
60 | - /** |
|
61 | - * @return \OC\User\Session |
|
62 | - */ |
|
63 | - public static function getUserSession() { |
|
64 | - return OC::$server->getUserSession(); |
|
65 | - } |
|
66 | - |
|
67 | - private static $_backends = array(); |
|
68 | - |
|
69 | - private static $_usedBackends = array(); |
|
70 | - |
|
71 | - private static $_setupedBackends = array(); |
|
72 | - |
|
73 | - // bool, stores if a user want to access a resource anonymously, e.g if he opens a public link |
|
74 | - private static $incognitoMode = false; |
|
75 | - |
|
76 | - /** |
|
77 | - * Adds the backend to the list of used backends |
|
78 | - * |
|
79 | - * @param string|\OCP\UserInterface $backend default: database The backend to use for user management |
|
80 | - * @return bool |
|
81 | - * |
|
82 | - * Set the User Authentication Module |
|
83 | - */ |
|
84 | - public static function useBackend($backend = 'database') { |
|
85 | - if ($backend instanceof \OCP\UserInterface) { |
|
86 | - self::$_usedBackends[get_class($backend)] = $backend; |
|
87 | - \OC::$server->getUserManager()->registerBackend($backend); |
|
88 | - } else { |
|
89 | - // You'll never know what happens |
|
90 | - if (null === $backend OR !is_string($backend)) { |
|
91 | - $backend = 'database'; |
|
92 | - } |
|
93 | - |
|
94 | - // Load backend |
|
95 | - switch ($backend) { |
|
96 | - case 'database': |
|
97 | - case 'mysql': |
|
98 | - case 'sqlite': |
|
99 | - \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
100 | - self::$_usedBackends[$backend] = new OC_User_Database(); |
|
101 | - \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
102 | - break; |
|
103 | - case 'dummy': |
|
104 | - self::$_usedBackends[$backend] = new \Test\Util\User\Dummy(); |
|
105 | - \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
106 | - break; |
|
107 | - default: |
|
108 | - \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
109 | - $className = 'OC_USER_' . strToUpper($backend); |
|
110 | - self::$_usedBackends[$backend] = new $className(); |
|
111 | - \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
112 | - break; |
|
113 | - } |
|
114 | - } |
|
115 | - return true; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * remove all used backends |
|
120 | - */ |
|
121 | - public static function clearBackends() { |
|
122 | - self::$_usedBackends = array(); |
|
123 | - \OC::$server->getUserManager()->clearBackends(); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * setup the configured backends in config.php |
|
128 | - */ |
|
129 | - public static function setupBackends() { |
|
130 | - OC_App::loadApps(array('prelogin')); |
|
131 | - $backends = \OC::$server->getSystemConfig()->getValue('user_backends', array()); |
|
132 | - foreach ($backends as $i => $config) { |
|
133 | - $class = $config['class']; |
|
134 | - $arguments = $config['arguments']; |
|
135 | - if (class_exists($class)) { |
|
136 | - if (array_search($i, self::$_setupedBackends) === false) { |
|
137 | - // make a reflection object |
|
138 | - $reflectionObj = new ReflectionClass($class); |
|
139 | - |
|
140 | - // use Reflection to create a new instance, using the $args |
|
141 | - $backend = $reflectionObj->newInstanceArgs($arguments); |
|
142 | - self::useBackend($backend); |
|
143 | - self::$_setupedBackends[] = $i; |
|
144 | - } else { |
|
145 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG); |
|
146 | - } |
|
147 | - } else { |
|
148 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR); |
|
149 | - } |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Try to login a user |
|
155 | - * |
|
156 | - * @param string $loginname The login name of the user to log in |
|
157 | - * @param string $password The password of the user |
|
158 | - * @return boolean|null |
|
159 | - * |
|
160 | - * Log in a user and regenerate a new session - if the password is ok |
|
161 | - */ |
|
162 | - public static function login($loginname, $password) { |
|
163 | - $result = self::getUserSession()->login($loginname, $password); |
|
164 | - if ($result) { |
|
165 | - // Refresh the token |
|
166 | - \OC::$server->getCsrfTokenManager()->refreshToken(); |
|
167 | - //we need to pass the user name, which may differ from login name |
|
168 | - $user = self::getUserSession()->getUser()->getUID(); |
|
169 | - OC_Util::setupFS($user); |
|
170 | - //trigger creation of user home and /files folder |
|
171 | - \OC::$server->getUserFolder($user); |
|
172 | - } |
|
173 | - return $result; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Try to login a user using the magic cookie (remember login) |
|
178 | - * |
|
179 | - * @param string $uid The username of the user to log in |
|
180 | - * @param string $token |
|
181 | - * @return bool |
|
182 | - */ |
|
183 | - public static function loginWithCookie($uid, $token) { |
|
184 | - return self::getUserSession()->loginWithCookie($uid, $token); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Try to login a user, assuming authentication |
|
189 | - * has already happened (e.g. via Single Sign On). |
|
190 | - * |
|
191 | - * Log in a user and regenerate a new session. |
|
192 | - * |
|
193 | - * @param \OCP\Authentication\IApacheBackend $backend |
|
194 | - * @return bool |
|
195 | - */ |
|
196 | - public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) { |
|
197 | - |
|
198 | - $uid = $backend->getCurrentUserId(); |
|
199 | - $run = true; |
|
200 | - OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid)); |
|
201 | - |
|
202 | - if ($uid) { |
|
203 | - if (self::getUser() !== $uid) { |
|
204 | - self::setUserId($uid); |
|
205 | - self::setDisplayName($uid); |
|
206 | - self::getUserSession()->setLoginName($uid); |
|
207 | - // setup the filesystem |
|
208 | - OC_Util::setupFS($uid); |
|
209 | - // first call the post_login hooks, the login-process needs to be |
|
210 | - // completed before we can safely create the users folder. |
|
211 | - // For example encryption needs to initialize the users keys first |
|
212 | - // before we can create the user folder with the skeleton files |
|
213 | - OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => '')); |
|
214 | - //trigger creation of user home and /files folder |
|
215 | - \OC::$server->getUserFolder($uid); |
|
216 | - |
|
217 | - } |
|
218 | - return true; |
|
219 | - } |
|
220 | - return false; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Verify with Apache whether user is authenticated. |
|
225 | - * |
|
226 | - * @return boolean|null |
|
227 | - * true: authenticated |
|
228 | - * false: not authenticated |
|
229 | - * null: not handled / no backend available |
|
230 | - */ |
|
231 | - public static function handleApacheAuth() { |
|
232 | - $backend = self::findFirstActiveUsedBackend(); |
|
233 | - if ($backend) { |
|
234 | - OC_App::loadApps(); |
|
235 | - |
|
236 | - //setup extra user backends |
|
237 | - self::setupBackends(); |
|
238 | - self::unsetMagicInCookie(); |
|
239 | - |
|
240 | - return self::loginWithApache($backend); |
|
241 | - } |
|
242 | - |
|
243 | - return null; |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - /** |
|
248 | - * Sets user id for session and triggers emit |
|
249 | - */ |
|
250 | - public static function setUserId($uid) { |
|
251 | - $userSession = \OC::$server->getUserSession(); |
|
252 | - $userManager = \OC::$server->getUserManager(); |
|
253 | - if ($user = $userManager->get($uid)) { |
|
254 | - $userSession->setUser($user); |
|
255 | - } else { |
|
256 | - \OC::$server->getSession()->set('user_id', $uid); |
|
257 | - } |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Sets user display name for session |
|
262 | - * |
|
263 | - * @param string $uid |
|
264 | - * @param string $displayName |
|
265 | - * @return bool Whether the display name could get set |
|
266 | - */ |
|
267 | - public static function setDisplayName($uid, $displayName = null) { |
|
268 | - if (is_null($displayName)) { |
|
269 | - $displayName = $uid; |
|
270 | - } |
|
271 | - $user = \OC::$server->getUserManager()->get($uid); |
|
272 | - if ($user) { |
|
273 | - return $user->setDisplayName($displayName); |
|
274 | - } else { |
|
275 | - return false; |
|
276 | - } |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Logs the current user out and kills all the session data |
|
281 | - * |
|
282 | - * Logout, destroys session |
|
283 | - */ |
|
284 | - public static function logout() { |
|
285 | - self::getUserSession()->logout(); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Tries to login the user with HTTP Basic Authentication |
|
290 | - */ |
|
291 | - public static function tryBasicAuthLogin() { |
|
292 | - if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) { |
|
293 | - $result = \OC_User::login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); |
|
294 | - if($result === true) { |
|
295 | - /** |
|
296 | - * Add DAV authenticated. This should in an ideal world not be |
|
297 | - * necessary but the iOS App reads cookies from anywhere instead |
|
298 | - * only the DAV endpoint. |
|
299 | - * This makes sure that the cookies will be valid for the whole scope |
|
300 | - * @see https://github.com/owncloud/core/issues/22893 |
|
301 | - */ |
|
302 | - \OC::$server->getSession()->set( |
|
303 | - \OCA\DAV\Connector\Sabre\Auth::DAV_AUTHENTICATED, |
|
304 | - \OC::$server->getUserSession()->getUser()->getUID() |
|
305 | - ); |
|
306 | - } |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Check if the user is logged in, considers also the HTTP basic credentials |
|
312 | - * |
|
313 | - * @return bool |
|
314 | - */ |
|
315 | - public static function isLoggedIn() { |
|
316 | - if (\OC::$server->getSession()->get('user_id') !== null && self::$incognitoMode === false) { |
|
317 | - return self::userExists(\OC::$server->getSession()->get('user_id')); |
|
318 | - } |
|
319 | - |
|
320 | - return false; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * set incognito mode, e.g. if a user wants to open a public link |
|
325 | - * |
|
326 | - * @param bool $status |
|
327 | - */ |
|
328 | - public static function setIncognitoMode($status) { |
|
329 | - self::$incognitoMode = $status; |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * get incognito mode status |
|
334 | - * |
|
335 | - * @return bool |
|
336 | - */ |
|
337 | - public static function isIncognitoMode() { |
|
338 | - return self::$incognitoMode; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * Supplies an attribute to the logout hyperlink. The default behaviour |
|
343 | - * is to return an href with '?logout=true' appended. However, it can |
|
344 | - * supply any attribute(s) which are valid for <a>. |
|
345 | - * |
|
346 | - * @return string with one or more HTML attributes. |
|
347 | - */ |
|
348 | - public static function getLogoutAttribute() { |
|
349 | - $backend = self::findFirstActiveUsedBackend(); |
|
350 | - if ($backend) { |
|
351 | - return $backend->getLogoutAttribute(); |
|
352 | - } |
|
353 | - |
|
354 | - return 'href="' . link_to('', 'index.php') . '?logout=true&requesttoken=' . urlencode(\OCP\Util::callRegister()) . '"'; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Check if the user is an admin user |
|
359 | - * |
|
360 | - * @param string $uid uid of the admin |
|
361 | - * @return bool |
|
362 | - */ |
|
363 | - public static function isAdminUser($uid) { |
|
364 | - if (OC_Group::inGroup($uid, 'admin') && self::$incognitoMode === false) { |
|
365 | - return true; |
|
366 | - } |
|
367 | - return false; |
|
368 | - } |
|
369 | - |
|
370 | - |
|
371 | - /** |
|
372 | - * get the user id of the user currently logged in. |
|
373 | - * |
|
374 | - * @return string|bool uid or false |
|
375 | - */ |
|
376 | - public static function getUser() { |
|
377 | - $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null; |
|
378 | - if (!is_null($uid) && self::$incognitoMode === false) { |
|
379 | - return $uid; |
|
380 | - } else { |
|
381 | - return false; |
|
382 | - } |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * get the display name of the user currently logged in. |
|
387 | - * |
|
388 | - * @param string $uid |
|
389 | - * @return string uid or false |
|
390 | - */ |
|
391 | - public static function getDisplayName($uid = null) { |
|
392 | - if ($uid) { |
|
393 | - $user = \OC::$server->getUserManager()->get($uid); |
|
394 | - if ($user) { |
|
395 | - return $user->getDisplayName(); |
|
396 | - } else { |
|
397 | - return $uid; |
|
398 | - } |
|
399 | - } else { |
|
400 | - $user = self::getUserSession()->getUser(); |
|
401 | - if ($user) { |
|
402 | - return $user->getDisplayName(); |
|
403 | - } else { |
|
404 | - return false; |
|
405 | - } |
|
406 | - } |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Autogenerate a password |
|
411 | - * |
|
412 | - * @return string |
|
413 | - * |
|
414 | - * generates a password |
|
415 | - */ |
|
416 | - public static function generatePassword() { |
|
417 | - return \OC::$server->getSecureRandom()->generate(30); |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Set password |
|
422 | - * |
|
423 | - * @param string $uid The username |
|
424 | - * @param string $password The new password |
|
425 | - * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
426 | - * @return bool |
|
427 | - * |
|
428 | - * Change the password of a user |
|
429 | - */ |
|
430 | - public static function setPassword($uid, $password, $recoveryPassword = null) { |
|
431 | - $user = \OC::$server->getUserManager()->get($uid); |
|
432 | - if ($user) { |
|
433 | - return $user->setPassword($password, $recoveryPassword); |
|
434 | - } else { |
|
435 | - return false; |
|
436 | - } |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Check whether user can change his avatar |
|
441 | - * |
|
442 | - * @param string $uid The username |
|
443 | - * @return bool |
|
444 | - * |
|
445 | - * Check whether a specified user can change his avatar |
|
446 | - */ |
|
447 | - public static function canUserChangeAvatar($uid) { |
|
448 | - $user = \OC::$server->getUserManager()->get($uid); |
|
449 | - if ($user) { |
|
450 | - return $user->canChangeAvatar(); |
|
451 | - } else { |
|
452 | - return false; |
|
453 | - } |
|
454 | - } |
|
455 | - |
|
456 | - /** |
|
457 | - * Check whether user can change his password |
|
458 | - * |
|
459 | - * @param string $uid The username |
|
460 | - * @return bool |
|
461 | - * |
|
462 | - * Check whether a specified user can change his password |
|
463 | - */ |
|
464 | - public static function canUserChangePassword($uid) { |
|
465 | - $user = \OC::$server->getUserManager()->get($uid); |
|
466 | - if ($user) { |
|
467 | - return $user->canChangePassword(); |
|
468 | - } else { |
|
469 | - return false; |
|
470 | - } |
|
471 | - } |
|
472 | - |
|
473 | - /** |
|
474 | - * Check whether user can change his display name |
|
475 | - * |
|
476 | - * @param string $uid The username |
|
477 | - * @return bool |
|
478 | - * |
|
479 | - * Check whether a specified user can change his display name |
|
480 | - */ |
|
481 | - public static function canUserChangeDisplayName($uid) { |
|
482 | - $user = \OC::$server->getUserManager()->get($uid); |
|
483 | - if ($user) { |
|
484 | - return $user->canChangeDisplayName(); |
|
485 | - } else { |
|
486 | - return false; |
|
487 | - } |
|
488 | - } |
|
489 | - |
|
490 | - /** |
|
491 | - * Check if the password is correct |
|
492 | - * |
|
493 | - * @param string $uid The username |
|
494 | - * @param string $password The password |
|
495 | - * @return string|false user id a string on success, false otherwise |
|
496 | - * |
|
497 | - * Check if the password is correct without logging in the user |
|
498 | - * returns the user id or false |
|
499 | - */ |
|
500 | - public static function checkPassword($uid, $password) { |
|
501 | - $manager = \OC::$server->getUserManager(); |
|
502 | - $username = $manager->checkPassword($uid, $password); |
|
503 | - if ($username !== false) { |
|
504 | - return $username->getUID(); |
|
505 | - } |
|
506 | - return false; |
|
507 | - } |
|
508 | - |
|
509 | - /** |
|
510 | - * @param string $uid The username |
|
511 | - * @return string |
|
512 | - * |
|
513 | - * returns the path to the users home directory |
|
514 | - * @deprecated Use \OC::$server->getUserManager->getHome() |
|
515 | - */ |
|
516 | - public static function getHome($uid) { |
|
517 | - $user = \OC::$server->getUserManager()->get($uid); |
|
518 | - if ($user) { |
|
519 | - return $user->getHome(); |
|
520 | - } else { |
|
521 | - return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid; |
|
522 | - } |
|
523 | - } |
|
524 | - |
|
525 | - /** |
|
526 | - * Get a list of all users |
|
527 | - * |
|
528 | - * @return array an array of all uids |
|
529 | - * |
|
530 | - * Get a list of all users. |
|
531 | - * @param string $search |
|
532 | - * @param integer $limit |
|
533 | - * @param integer $offset |
|
534 | - */ |
|
535 | - public static function getUsers($search = '', $limit = null, $offset = null) { |
|
536 | - $users = \OC::$server->getUserManager()->search($search, $limit, $offset); |
|
537 | - $uids = array(); |
|
538 | - foreach ($users as $user) { |
|
539 | - $uids[] = $user->getUID(); |
|
540 | - } |
|
541 | - return $uids; |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Get a list of all users display name |
|
546 | - * |
|
547 | - * @param string $search |
|
548 | - * @param int $limit |
|
549 | - * @param int $offset |
|
550 | - * @return array associative array with all display names (value) and corresponding uids (key) |
|
551 | - * |
|
552 | - * Get a list of all display names and user ids. |
|
553 | - * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
|
554 | - */ |
|
555 | - public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
556 | - $displayNames = array(); |
|
557 | - $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset); |
|
558 | - foreach ($users as $user) { |
|
559 | - $displayNames[$user->getUID()] = $user->getDisplayName(); |
|
560 | - } |
|
561 | - return $displayNames; |
|
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * check if a user exists |
|
566 | - * |
|
567 | - * @param string $uid the username |
|
568 | - * @return boolean |
|
569 | - */ |
|
570 | - public static function userExists($uid) { |
|
571 | - return \OC::$server->getUserManager()->userExists($uid); |
|
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * disables a user |
|
576 | - * |
|
577 | - * @param string $uid the user to disable |
|
578 | - */ |
|
579 | - public static function disableUser($uid) { |
|
580 | - $user = \OC::$server->getUserManager()->get($uid); |
|
581 | - if ($user) { |
|
582 | - $user->setEnabled(false); |
|
583 | - } |
|
584 | - } |
|
585 | - |
|
586 | - /** |
|
587 | - * enable a user |
|
588 | - * |
|
589 | - * @param string $uid |
|
590 | - */ |
|
591 | - public static function enableUser($uid) { |
|
592 | - $user = \OC::$server->getUserManager()->get($uid); |
|
593 | - if ($user) { |
|
594 | - $user->setEnabled(true); |
|
595 | - } |
|
596 | - } |
|
597 | - |
|
598 | - /** |
|
599 | - * checks if a user is enabled |
|
600 | - * |
|
601 | - * @param string $uid |
|
602 | - * @return bool |
|
603 | - */ |
|
604 | - public static function isEnabled($uid) { |
|
605 | - $user = \OC::$server->getUserManager()->get($uid); |
|
606 | - if ($user) { |
|
607 | - return $user->isEnabled(); |
|
608 | - } else { |
|
609 | - return false; |
|
610 | - } |
|
611 | - } |
|
612 | - |
|
613 | - /** |
|
614 | - * Set cookie value to use in next page load |
|
615 | - * |
|
616 | - * @param string $username username to be set |
|
617 | - * @param string $token |
|
618 | - */ |
|
619 | - public static function setMagicInCookie($username, $token) { |
|
620 | - self::getUserSession()->setMagicInCookie($username, $token); |
|
621 | - } |
|
622 | - |
|
623 | - /** |
|
624 | - * Remove cookie for "remember username" |
|
625 | - */ |
|
626 | - public static function unsetMagicInCookie() { |
|
627 | - self::getUserSession()->unsetMagicInCookie(); |
|
628 | - } |
|
629 | - |
|
630 | - /** |
|
631 | - * Returns the first active backend from self::$_usedBackends. |
|
632 | - * |
|
633 | - * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
|
634 | - */ |
|
635 | - private static function findFirstActiveUsedBackend() { |
|
636 | - foreach (self::$_usedBackends as $backend) { |
|
637 | - if ($backend instanceof OCP\Authentication\IApacheBackend) { |
|
638 | - if ($backend->isSessionActive()) { |
|
639 | - return $backend; |
|
640 | - } |
|
641 | - } |
|
642 | - } |
|
643 | - |
|
644 | - return null; |
|
645 | - } |
|
60 | + /** |
|
61 | + * @return \OC\User\Session |
|
62 | + */ |
|
63 | + public static function getUserSession() { |
|
64 | + return OC::$server->getUserSession(); |
|
65 | + } |
|
66 | + |
|
67 | + private static $_backends = array(); |
|
68 | + |
|
69 | + private static $_usedBackends = array(); |
|
70 | + |
|
71 | + private static $_setupedBackends = array(); |
|
72 | + |
|
73 | + // bool, stores if a user want to access a resource anonymously, e.g if he opens a public link |
|
74 | + private static $incognitoMode = false; |
|
75 | + |
|
76 | + /** |
|
77 | + * Adds the backend to the list of used backends |
|
78 | + * |
|
79 | + * @param string|\OCP\UserInterface $backend default: database The backend to use for user management |
|
80 | + * @return bool |
|
81 | + * |
|
82 | + * Set the User Authentication Module |
|
83 | + */ |
|
84 | + public static function useBackend($backend = 'database') { |
|
85 | + if ($backend instanceof \OCP\UserInterface) { |
|
86 | + self::$_usedBackends[get_class($backend)] = $backend; |
|
87 | + \OC::$server->getUserManager()->registerBackend($backend); |
|
88 | + } else { |
|
89 | + // You'll never know what happens |
|
90 | + if (null === $backend OR !is_string($backend)) { |
|
91 | + $backend = 'database'; |
|
92 | + } |
|
93 | + |
|
94 | + // Load backend |
|
95 | + switch ($backend) { |
|
96 | + case 'database': |
|
97 | + case 'mysql': |
|
98 | + case 'sqlite': |
|
99 | + \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
100 | + self::$_usedBackends[$backend] = new OC_User_Database(); |
|
101 | + \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
102 | + break; |
|
103 | + case 'dummy': |
|
104 | + self::$_usedBackends[$backend] = new \Test\Util\User\Dummy(); |
|
105 | + \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
106 | + break; |
|
107 | + default: |
|
108 | + \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
109 | + $className = 'OC_USER_' . strToUpper($backend); |
|
110 | + self::$_usedBackends[$backend] = new $className(); |
|
111 | + \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
|
112 | + break; |
|
113 | + } |
|
114 | + } |
|
115 | + return true; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * remove all used backends |
|
120 | + */ |
|
121 | + public static function clearBackends() { |
|
122 | + self::$_usedBackends = array(); |
|
123 | + \OC::$server->getUserManager()->clearBackends(); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * setup the configured backends in config.php |
|
128 | + */ |
|
129 | + public static function setupBackends() { |
|
130 | + OC_App::loadApps(array('prelogin')); |
|
131 | + $backends = \OC::$server->getSystemConfig()->getValue('user_backends', array()); |
|
132 | + foreach ($backends as $i => $config) { |
|
133 | + $class = $config['class']; |
|
134 | + $arguments = $config['arguments']; |
|
135 | + if (class_exists($class)) { |
|
136 | + if (array_search($i, self::$_setupedBackends) === false) { |
|
137 | + // make a reflection object |
|
138 | + $reflectionObj = new ReflectionClass($class); |
|
139 | + |
|
140 | + // use Reflection to create a new instance, using the $args |
|
141 | + $backend = $reflectionObj->newInstanceArgs($arguments); |
|
142 | + self::useBackend($backend); |
|
143 | + self::$_setupedBackends[] = $i; |
|
144 | + } else { |
|
145 | + \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG); |
|
146 | + } |
|
147 | + } else { |
|
148 | + \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR); |
|
149 | + } |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Try to login a user |
|
155 | + * |
|
156 | + * @param string $loginname The login name of the user to log in |
|
157 | + * @param string $password The password of the user |
|
158 | + * @return boolean|null |
|
159 | + * |
|
160 | + * Log in a user and regenerate a new session - if the password is ok |
|
161 | + */ |
|
162 | + public static function login($loginname, $password) { |
|
163 | + $result = self::getUserSession()->login($loginname, $password); |
|
164 | + if ($result) { |
|
165 | + // Refresh the token |
|
166 | + \OC::$server->getCsrfTokenManager()->refreshToken(); |
|
167 | + //we need to pass the user name, which may differ from login name |
|
168 | + $user = self::getUserSession()->getUser()->getUID(); |
|
169 | + OC_Util::setupFS($user); |
|
170 | + //trigger creation of user home and /files folder |
|
171 | + \OC::$server->getUserFolder($user); |
|
172 | + } |
|
173 | + return $result; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Try to login a user using the magic cookie (remember login) |
|
178 | + * |
|
179 | + * @param string $uid The username of the user to log in |
|
180 | + * @param string $token |
|
181 | + * @return bool |
|
182 | + */ |
|
183 | + public static function loginWithCookie($uid, $token) { |
|
184 | + return self::getUserSession()->loginWithCookie($uid, $token); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Try to login a user, assuming authentication |
|
189 | + * has already happened (e.g. via Single Sign On). |
|
190 | + * |
|
191 | + * Log in a user and regenerate a new session. |
|
192 | + * |
|
193 | + * @param \OCP\Authentication\IApacheBackend $backend |
|
194 | + * @return bool |
|
195 | + */ |
|
196 | + public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) { |
|
197 | + |
|
198 | + $uid = $backend->getCurrentUserId(); |
|
199 | + $run = true; |
|
200 | + OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid)); |
|
201 | + |
|
202 | + if ($uid) { |
|
203 | + if (self::getUser() !== $uid) { |
|
204 | + self::setUserId($uid); |
|
205 | + self::setDisplayName($uid); |
|
206 | + self::getUserSession()->setLoginName($uid); |
|
207 | + // setup the filesystem |
|
208 | + OC_Util::setupFS($uid); |
|
209 | + // first call the post_login hooks, the login-process needs to be |
|
210 | + // completed before we can safely create the users folder. |
|
211 | + // For example encryption needs to initialize the users keys first |
|
212 | + // before we can create the user folder with the skeleton files |
|
213 | + OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => '')); |
|
214 | + //trigger creation of user home and /files folder |
|
215 | + \OC::$server->getUserFolder($uid); |
|
216 | + |
|
217 | + } |
|
218 | + return true; |
|
219 | + } |
|
220 | + return false; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Verify with Apache whether user is authenticated. |
|
225 | + * |
|
226 | + * @return boolean|null |
|
227 | + * true: authenticated |
|
228 | + * false: not authenticated |
|
229 | + * null: not handled / no backend available |
|
230 | + */ |
|
231 | + public static function handleApacheAuth() { |
|
232 | + $backend = self::findFirstActiveUsedBackend(); |
|
233 | + if ($backend) { |
|
234 | + OC_App::loadApps(); |
|
235 | + |
|
236 | + //setup extra user backends |
|
237 | + self::setupBackends(); |
|
238 | + self::unsetMagicInCookie(); |
|
239 | + |
|
240 | + return self::loginWithApache($backend); |
|
241 | + } |
|
242 | + |
|
243 | + return null; |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + /** |
|
248 | + * Sets user id for session and triggers emit |
|
249 | + */ |
|
250 | + public static function setUserId($uid) { |
|
251 | + $userSession = \OC::$server->getUserSession(); |
|
252 | + $userManager = \OC::$server->getUserManager(); |
|
253 | + if ($user = $userManager->get($uid)) { |
|
254 | + $userSession->setUser($user); |
|
255 | + } else { |
|
256 | + \OC::$server->getSession()->set('user_id', $uid); |
|
257 | + } |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Sets user display name for session |
|
262 | + * |
|
263 | + * @param string $uid |
|
264 | + * @param string $displayName |
|
265 | + * @return bool Whether the display name could get set |
|
266 | + */ |
|
267 | + public static function setDisplayName($uid, $displayName = null) { |
|
268 | + if (is_null($displayName)) { |
|
269 | + $displayName = $uid; |
|
270 | + } |
|
271 | + $user = \OC::$server->getUserManager()->get($uid); |
|
272 | + if ($user) { |
|
273 | + return $user->setDisplayName($displayName); |
|
274 | + } else { |
|
275 | + return false; |
|
276 | + } |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Logs the current user out and kills all the session data |
|
281 | + * |
|
282 | + * Logout, destroys session |
|
283 | + */ |
|
284 | + public static function logout() { |
|
285 | + self::getUserSession()->logout(); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Tries to login the user with HTTP Basic Authentication |
|
290 | + */ |
|
291 | + public static function tryBasicAuthLogin() { |
|
292 | + if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) { |
|
293 | + $result = \OC_User::login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); |
|
294 | + if($result === true) { |
|
295 | + /** |
|
296 | + * Add DAV authenticated. This should in an ideal world not be |
|
297 | + * necessary but the iOS App reads cookies from anywhere instead |
|
298 | + * only the DAV endpoint. |
|
299 | + * This makes sure that the cookies will be valid for the whole scope |
|
300 | + * @see https://github.com/owncloud/core/issues/22893 |
|
301 | + */ |
|
302 | + \OC::$server->getSession()->set( |
|
303 | + \OCA\DAV\Connector\Sabre\Auth::DAV_AUTHENTICATED, |
|
304 | + \OC::$server->getUserSession()->getUser()->getUID() |
|
305 | + ); |
|
306 | + } |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Check if the user is logged in, considers also the HTTP basic credentials |
|
312 | + * |
|
313 | + * @return bool |
|
314 | + */ |
|
315 | + public static function isLoggedIn() { |
|
316 | + if (\OC::$server->getSession()->get('user_id') !== null && self::$incognitoMode === false) { |
|
317 | + return self::userExists(\OC::$server->getSession()->get('user_id')); |
|
318 | + } |
|
319 | + |
|
320 | + return false; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * set incognito mode, e.g. if a user wants to open a public link |
|
325 | + * |
|
326 | + * @param bool $status |
|
327 | + */ |
|
328 | + public static function setIncognitoMode($status) { |
|
329 | + self::$incognitoMode = $status; |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * get incognito mode status |
|
334 | + * |
|
335 | + * @return bool |
|
336 | + */ |
|
337 | + public static function isIncognitoMode() { |
|
338 | + return self::$incognitoMode; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Supplies an attribute to the logout hyperlink. The default behaviour |
|
343 | + * is to return an href with '?logout=true' appended. However, it can |
|
344 | + * supply any attribute(s) which are valid for <a>. |
|
345 | + * |
|
346 | + * @return string with one or more HTML attributes. |
|
347 | + */ |
|
348 | + public static function getLogoutAttribute() { |
|
349 | + $backend = self::findFirstActiveUsedBackend(); |
|
350 | + if ($backend) { |
|
351 | + return $backend->getLogoutAttribute(); |
|
352 | + } |
|
353 | + |
|
354 | + return 'href="' . link_to('', 'index.php') . '?logout=true&requesttoken=' . urlencode(\OCP\Util::callRegister()) . '"'; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Check if the user is an admin user |
|
359 | + * |
|
360 | + * @param string $uid uid of the admin |
|
361 | + * @return bool |
|
362 | + */ |
|
363 | + public static function isAdminUser($uid) { |
|
364 | + if (OC_Group::inGroup($uid, 'admin') && self::$incognitoMode === false) { |
|
365 | + return true; |
|
366 | + } |
|
367 | + return false; |
|
368 | + } |
|
369 | + |
|
370 | + |
|
371 | + /** |
|
372 | + * get the user id of the user currently logged in. |
|
373 | + * |
|
374 | + * @return string|bool uid or false |
|
375 | + */ |
|
376 | + public static function getUser() { |
|
377 | + $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null; |
|
378 | + if (!is_null($uid) && self::$incognitoMode === false) { |
|
379 | + return $uid; |
|
380 | + } else { |
|
381 | + return false; |
|
382 | + } |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * get the display name of the user currently logged in. |
|
387 | + * |
|
388 | + * @param string $uid |
|
389 | + * @return string uid or false |
|
390 | + */ |
|
391 | + public static function getDisplayName($uid = null) { |
|
392 | + if ($uid) { |
|
393 | + $user = \OC::$server->getUserManager()->get($uid); |
|
394 | + if ($user) { |
|
395 | + return $user->getDisplayName(); |
|
396 | + } else { |
|
397 | + return $uid; |
|
398 | + } |
|
399 | + } else { |
|
400 | + $user = self::getUserSession()->getUser(); |
|
401 | + if ($user) { |
|
402 | + return $user->getDisplayName(); |
|
403 | + } else { |
|
404 | + return false; |
|
405 | + } |
|
406 | + } |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Autogenerate a password |
|
411 | + * |
|
412 | + * @return string |
|
413 | + * |
|
414 | + * generates a password |
|
415 | + */ |
|
416 | + public static function generatePassword() { |
|
417 | + return \OC::$server->getSecureRandom()->generate(30); |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Set password |
|
422 | + * |
|
423 | + * @param string $uid The username |
|
424 | + * @param string $password The new password |
|
425 | + * @param string $recoveryPassword for the encryption app to reset encryption keys |
|
426 | + * @return bool |
|
427 | + * |
|
428 | + * Change the password of a user |
|
429 | + */ |
|
430 | + public static function setPassword($uid, $password, $recoveryPassword = null) { |
|
431 | + $user = \OC::$server->getUserManager()->get($uid); |
|
432 | + if ($user) { |
|
433 | + return $user->setPassword($password, $recoveryPassword); |
|
434 | + } else { |
|
435 | + return false; |
|
436 | + } |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Check whether user can change his avatar |
|
441 | + * |
|
442 | + * @param string $uid The username |
|
443 | + * @return bool |
|
444 | + * |
|
445 | + * Check whether a specified user can change his avatar |
|
446 | + */ |
|
447 | + public static function canUserChangeAvatar($uid) { |
|
448 | + $user = \OC::$server->getUserManager()->get($uid); |
|
449 | + if ($user) { |
|
450 | + return $user->canChangeAvatar(); |
|
451 | + } else { |
|
452 | + return false; |
|
453 | + } |
|
454 | + } |
|
455 | + |
|
456 | + /** |
|
457 | + * Check whether user can change his password |
|
458 | + * |
|
459 | + * @param string $uid The username |
|
460 | + * @return bool |
|
461 | + * |
|
462 | + * Check whether a specified user can change his password |
|
463 | + */ |
|
464 | + public static function canUserChangePassword($uid) { |
|
465 | + $user = \OC::$server->getUserManager()->get($uid); |
|
466 | + if ($user) { |
|
467 | + return $user->canChangePassword(); |
|
468 | + } else { |
|
469 | + return false; |
|
470 | + } |
|
471 | + } |
|
472 | + |
|
473 | + /** |
|
474 | + * Check whether user can change his display name |
|
475 | + * |
|
476 | + * @param string $uid The username |
|
477 | + * @return bool |
|
478 | + * |
|
479 | + * Check whether a specified user can change his display name |
|
480 | + */ |
|
481 | + public static function canUserChangeDisplayName($uid) { |
|
482 | + $user = \OC::$server->getUserManager()->get($uid); |
|
483 | + if ($user) { |
|
484 | + return $user->canChangeDisplayName(); |
|
485 | + } else { |
|
486 | + return false; |
|
487 | + } |
|
488 | + } |
|
489 | + |
|
490 | + /** |
|
491 | + * Check if the password is correct |
|
492 | + * |
|
493 | + * @param string $uid The username |
|
494 | + * @param string $password The password |
|
495 | + * @return string|false user id a string on success, false otherwise |
|
496 | + * |
|
497 | + * Check if the password is correct without logging in the user |
|
498 | + * returns the user id or false |
|
499 | + */ |
|
500 | + public static function checkPassword($uid, $password) { |
|
501 | + $manager = \OC::$server->getUserManager(); |
|
502 | + $username = $manager->checkPassword($uid, $password); |
|
503 | + if ($username !== false) { |
|
504 | + return $username->getUID(); |
|
505 | + } |
|
506 | + return false; |
|
507 | + } |
|
508 | + |
|
509 | + /** |
|
510 | + * @param string $uid The username |
|
511 | + * @return string |
|
512 | + * |
|
513 | + * returns the path to the users home directory |
|
514 | + * @deprecated Use \OC::$server->getUserManager->getHome() |
|
515 | + */ |
|
516 | + public static function getHome($uid) { |
|
517 | + $user = \OC::$server->getUserManager()->get($uid); |
|
518 | + if ($user) { |
|
519 | + return $user->getHome(); |
|
520 | + } else { |
|
521 | + return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid; |
|
522 | + } |
|
523 | + } |
|
524 | + |
|
525 | + /** |
|
526 | + * Get a list of all users |
|
527 | + * |
|
528 | + * @return array an array of all uids |
|
529 | + * |
|
530 | + * Get a list of all users. |
|
531 | + * @param string $search |
|
532 | + * @param integer $limit |
|
533 | + * @param integer $offset |
|
534 | + */ |
|
535 | + public static function getUsers($search = '', $limit = null, $offset = null) { |
|
536 | + $users = \OC::$server->getUserManager()->search($search, $limit, $offset); |
|
537 | + $uids = array(); |
|
538 | + foreach ($users as $user) { |
|
539 | + $uids[] = $user->getUID(); |
|
540 | + } |
|
541 | + return $uids; |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Get a list of all users display name |
|
546 | + * |
|
547 | + * @param string $search |
|
548 | + * @param int $limit |
|
549 | + * @param int $offset |
|
550 | + * @return array associative array with all display names (value) and corresponding uids (key) |
|
551 | + * |
|
552 | + * Get a list of all display names and user ids. |
|
553 | + * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
|
554 | + */ |
|
555 | + public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
556 | + $displayNames = array(); |
|
557 | + $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset); |
|
558 | + foreach ($users as $user) { |
|
559 | + $displayNames[$user->getUID()] = $user->getDisplayName(); |
|
560 | + } |
|
561 | + return $displayNames; |
|
562 | + } |
|
563 | + |
|
564 | + /** |
|
565 | + * check if a user exists |
|
566 | + * |
|
567 | + * @param string $uid the username |
|
568 | + * @return boolean |
|
569 | + */ |
|
570 | + public static function userExists($uid) { |
|
571 | + return \OC::$server->getUserManager()->userExists($uid); |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * disables a user |
|
576 | + * |
|
577 | + * @param string $uid the user to disable |
|
578 | + */ |
|
579 | + public static function disableUser($uid) { |
|
580 | + $user = \OC::$server->getUserManager()->get($uid); |
|
581 | + if ($user) { |
|
582 | + $user->setEnabled(false); |
|
583 | + } |
|
584 | + } |
|
585 | + |
|
586 | + /** |
|
587 | + * enable a user |
|
588 | + * |
|
589 | + * @param string $uid |
|
590 | + */ |
|
591 | + public static function enableUser($uid) { |
|
592 | + $user = \OC::$server->getUserManager()->get($uid); |
|
593 | + if ($user) { |
|
594 | + $user->setEnabled(true); |
|
595 | + } |
|
596 | + } |
|
597 | + |
|
598 | + /** |
|
599 | + * checks if a user is enabled |
|
600 | + * |
|
601 | + * @param string $uid |
|
602 | + * @return bool |
|
603 | + */ |
|
604 | + public static function isEnabled($uid) { |
|
605 | + $user = \OC::$server->getUserManager()->get($uid); |
|
606 | + if ($user) { |
|
607 | + return $user->isEnabled(); |
|
608 | + } else { |
|
609 | + return false; |
|
610 | + } |
|
611 | + } |
|
612 | + |
|
613 | + /** |
|
614 | + * Set cookie value to use in next page load |
|
615 | + * |
|
616 | + * @param string $username username to be set |
|
617 | + * @param string $token |
|
618 | + */ |
|
619 | + public static function setMagicInCookie($username, $token) { |
|
620 | + self::getUserSession()->setMagicInCookie($username, $token); |
|
621 | + } |
|
622 | + |
|
623 | + /** |
|
624 | + * Remove cookie for "remember username" |
|
625 | + */ |
|
626 | + public static function unsetMagicInCookie() { |
|
627 | + self::getUserSession()->unsetMagicInCookie(); |
|
628 | + } |
|
629 | + |
|
630 | + /** |
|
631 | + * Returns the first active backend from self::$_usedBackends. |
|
632 | + * |
|
633 | + * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
|
634 | + */ |
|
635 | + private static function findFirstActiveUsedBackend() { |
|
636 | + foreach (self::$_usedBackends as $backend) { |
|
637 | + if ($backend instanceof OCP\Authentication\IApacheBackend) { |
|
638 | + if ($backend->isSessionActive()) { |
|
639 | + return $backend; |
|
640 | + } |
|
641 | + } |
|
642 | + } |
|
643 | + |
|
644 | + return null; |
|
645 | + } |
|
646 | 646 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | case 'database': |
97 | 97 | case 'mysql': |
98 | 98 | case 'sqlite': |
99 | - \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
99 | + \OCP\Util::writeLog('core', 'Adding user backend '.$backend.'.', \OCP\Util::DEBUG); |
|
100 | 100 | self::$_usedBackends[$backend] = new OC_User_Database(); |
101 | 101 | \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
102 | 102 | break; |
@@ -105,8 +105,8 @@ discard block |
||
105 | 105 | \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
106 | 106 | break; |
107 | 107 | default: |
108 | - \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG); |
|
109 | - $className = 'OC_USER_' . strToUpper($backend); |
|
108 | + \OCP\Util::writeLog('core', 'Adding default user backend '.$backend.'.', \OCP\Util::DEBUG); |
|
109 | + $className = 'OC_USER_'.strToUpper($backend); |
|
110 | 110 | self::$_usedBackends[$backend] = new $className(); |
111 | 111 | \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); |
112 | 112 | break; |
@@ -142,10 +142,10 @@ discard block |
||
142 | 142 | self::useBackend($backend); |
143 | 143 | self::$_setupedBackends[] = $i; |
144 | 144 | } else { |
145 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG); |
|
145 | + \OCP\Util::writeLog('core', 'User backend '.$class.' already initialized.', \OCP\Util::DEBUG); |
|
146 | 146 | } |
147 | 147 | } else { |
148 | - \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR); |
|
148 | + \OCP\Util::writeLog('core', 'User backend '.$class.' not found.', \OCP\Util::ERROR); |
|
149 | 149 | } |
150 | 150 | } |
151 | 151 | } |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | public static function tryBasicAuthLogin() { |
292 | 292 | if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) { |
293 | 293 | $result = \OC_User::login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); |
294 | - if($result === true) { |
|
294 | + if ($result === true) { |
|
295 | 295 | /** |
296 | 296 | * Add DAV authenticated. This should in an ideal world not be |
297 | 297 | * necessary but the iOS App reads cookies from anywhere instead |
@@ -351,7 +351,7 @@ discard block |
||
351 | 351 | return $backend->getLogoutAttribute(); |
352 | 352 | } |
353 | 353 | |
354 | - return 'href="' . link_to('', 'index.php') . '?logout=true&requesttoken=' . urlencode(\OCP\Util::callRegister()) . '"'; |
|
354 | + return 'href="'.link_to('', 'index.php').'?logout=true&requesttoken='.urlencode(\OCP\Util::callRegister()).'"'; |
|
355 | 355 | } |
356 | 356 | |
357 | 357 | /** |
@@ -518,7 +518,7 @@ discard block |
||
518 | 518 | if ($user) { |
519 | 519 | return $user->getHome(); |
520 | 520 | } else { |
521 | - return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid; |
|
521 | + return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT.'/data').'/'.$uid; |
|
522 | 522 | } |
523 | 523 | } |
524 | 524 |
@@ -143,7 +143,7 @@ |
||
143 | 143 | /** |
144 | 144 | * get the current active user |
145 | 145 | * |
146 | - * @return \OCP\IUser|null Current user, otherwise null |
|
146 | + * @return null|User Current user, otherwise null |
|
147 | 147 | */ |
148 | 148 | public function getUser() { |
149 | 149 | // FIXME: This is a quick'n dirty work-around for the incognito mode as |
@@ -54,266 +54,266 @@ |
||
54 | 54 | * @package OC\User |
55 | 55 | */ |
56 | 56 | class Session implements IUserSession, Emitter { |
57 | - /** |
|
58 | - * @var \OC\User\Manager $manager |
|
59 | - */ |
|
60 | - private $manager; |
|
57 | + /** |
|
58 | + * @var \OC\User\Manager $manager |
|
59 | + */ |
|
60 | + private $manager; |
|
61 | 61 | |
62 | - /** |
|
63 | - * @var \OC\Session\Session $session |
|
64 | - */ |
|
65 | - private $session; |
|
62 | + /** |
|
63 | + * @var \OC\Session\Session $session |
|
64 | + */ |
|
65 | + private $session; |
|
66 | 66 | |
67 | - /** |
|
68 | - * @var \OC\User\User $activeUser |
|
69 | - */ |
|
70 | - protected $activeUser; |
|
67 | + /** |
|
68 | + * @var \OC\User\User $activeUser |
|
69 | + */ |
|
70 | + protected $activeUser; |
|
71 | 71 | |
72 | - /** |
|
73 | - * @param \OCP\IUserManager $manager |
|
74 | - * @param \OCP\ISession $session |
|
75 | - */ |
|
76 | - public function __construct(\OCP\IUserManager $manager, \OCP\ISession $session) { |
|
77 | - $this->manager = $manager; |
|
78 | - $this->session = $session; |
|
79 | - } |
|
72 | + /** |
|
73 | + * @param \OCP\IUserManager $manager |
|
74 | + * @param \OCP\ISession $session |
|
75 | + */ |
|
76 | + public function __construct(\OCP\IUserManager $manager, \OCP\ISession $session) { |
|
77 | + $this->manager = $manager; |
|
78 | + $this->session = $session; |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param string $scope |
|
83 | - * @param string $method |
|
84 | - * @param callable $callback |
|
85 | - */ |
|
86 | - public function listen($scope, $method, callable $callback) { |
|
87 | - $this->manager->listen($scope, $method, $callback); |
|
88 | - } |
|
81 | + /** |
|
82 | + * @param string $scope |
|
83 | + * @param string $method |
|
84 | + * @param callable $callback |
|
85 | + */ |
|
86 | + public function listen($scope, $method, callable $callback) { |
|
87 | + $this->manager->listen($scope, $method, $callback); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @param string $scope optional |
|
92 | - * @param string $method optional |
|
93 | - * @param callable $callback optional |
|
94 | - */ |
|
95 | - public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
96 | - $this->manager->removeListener($scope, $method, $callback); |
|
97 | - } |
|
90 | + /** |
|
91 | + * @param string $scope optional |
|
92 | + * @param string $method optional |
|
93 | + * @param callable $callback optional |
|
94 | + */ |
|
95 | + public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
96 | + $this->manager->removeListener($scope, $method, $callback); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * get the manager object |
|
101 | - * |
|
102 | - * @return \OC\User\Manager |
|
103 | - */ |
|
104 | - public function getManager() { |
|
105 | - return $this->manager; |
|
106 | - } |
|
99 | + /** |
|
100 | + * get the manager object |
|
101 | + * |
|
102 | + * @return \OC\User\Manager |
|
103 | + */ |
|
104 | + public function getManager() { |
|
105 | + return $this->manager; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * get the session object |
|
110 | - * |
|
111 | - * @return \OCP\ISession |
|
112 | - */ |
|
113 | - public function getSession() { |
|
114 | - return $this->session; |
|
115 | - } |
|
108 | + /** |
|
109 | + * get the session object |
|
110 | + * |
|
111 | + * @return \OCP\ISession |
|
112 | + */ |
|
113 | + public function getSession() { |
|
114 | + return $this->session; |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * set the session object |
|
119 | - * |
|
120 | - * @param \OCP\ISession $session |
|
121 | - */ |
|
122 | - public function setSession(\OCP\ISession $session) { |
|
123 | - if ($this->session instanceof \OCP\ISession) { |
|
124 | - $this->session->close(); |
|
125 | - } |
|
126 | - $this->session = $session; |
|
127 | - $this->activeUser = null; |
|
128 | - } |
|
117 | + /** |
|
118 | + * set the session object |
|
119 | + * |
|
120 | + * @param \OCP\ISession $session |
|
121 | + */ |
|
122 | + public function setSession(\OCP\ISession $session) { |
|
123 | + if ($this->session instanceof \OCP\ISession) { |
|
124 | + $this->session->close(); |
|
125 | + } |
|
126 | + $this->session = $session; |
|
127 | + $this->activeUser = null; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * set the currently active user |
|
132 | - * |
|
133 | - * @param \OC\User\User|null $user |
|
134 | - */ |
|
135 | - public function setUser($user) { |
|
136 | - if (is_null($user)) { |
|
137 | - $this->session->remove('user_id'); |
|
138 | - } else { |
|
139 | - $this->session->set('user_id', $user->getUID()); |
|
140 | - } |
|
141 | - $this->activeUser = $user; |
|
142 | - } |
|
130 | + /** |
|
131 | + * set the currently active user |
|
132 | + * |
|
133 | + * @param \OC\User\User|null $user |
|
134 | + */ |
|
135 | + public function setUser($user) { |
|
136 | + if (is_null($user)) { |
|
137 | + $this->session->remove('user_id'); |
|
138 | + } else { |
|
139 | + $this->session->set('user_id', $user->getUID()); |
|
140 | + } |
|
141 | + $this->activeUser = $user; |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * get the current active user |
|
146 | - * |
|
147 | - * @return \OCP\IUser|null Current user, otherwise null |
|
148 | - */ |
|
149 | - public function getUser() { |
|
150 | - // FIXME: This is a quick'n dirty work-around for the incognito mode as |
|
151 | - // described at https://github.com/owncloud/core/pull/12912#issuecomment-67391155 |
|
152 | - if (\OC_User::isIncognitoMode()) { |
|
153 | - return null; |
|
154 | - } |
|
155 | - if ($this->activeUser) { |
|
156 | - return $this->activeUser; |
|
157 | - } else { |
|
158 | - $uid = $this->session->get('user_id'); |
|
159 | - if ($uid !== null) { |
|
160 | - $this->activeUser = $this->manager->get($uid); |
|
161 | - return $this->activeUser; |
|
162 | - } else { |
|
163 | - return null; |
|
164 | - } |
|
165 | - } |
|
166 | - } |
|
144 | + /** |
|
145 | + * get the current active user |
|
146 | + * |
|
147 | + * @return \OCP\IUser|null Current user, otherwise null |
|
148 | + */ |
|
149 | + public function getUser() { |
|
150 | + // FIXME: This is a quick'n dirty work-around for the incognito mode as |
|
151 | + // described at https://github.com/owncloud/core/pull/12912#issuecomment-67391155 |
|
152 | + if (\OC_User::isIncognitoMode()) { |
|
153 | + return null; |
|
154 | + } |
|
155 | + if ($this->activeUser) { |
|
156 | + return $this->activeUser; |
|
157 | + } else { |
|
158 | + $uid = $this->session->get('user_id'); |
|
159 | + if ($uid !== null) { |
|
160 | + $this->activeUser = $this->manager->get($uid); |
|
161 | + return $this->activeUser; |
|
162 | + } else { |
|
163 | + return null; |
|
164 | + } |
|
165 | + } |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Checks whether the user is logged in |
|
170 | - * |
|
171 | - * @return bool if logged in |
|
172 | - */ |
|
173 | - public function isLoggedIn() { |
|
174 | - return $this->getUser() !== null; |
|
175 | - } |
|
168 | + /** |
|
169 | + * Checks whether the user is logged in |
|
170 | + * |
|
171 | + * @return bool if logged in |
|
172 | + */ |
|
173 | + public function isLoggedIn() { |
|
174 | + return $this->getUser() !== null; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * set the login name |
|
179 | - * |
|
180 | - * @param string|null $loginName for the logged in user |
|
181 | - */ |
|
182 | - public function setLoginName($loginName) { |
|
183 | - if (is_null($loginName)) { |
|
184 | - $this->session->remove('loginname'); |
|
185 | - } else { |
|
186 | - $this->session->set('loginname', $loginName); |
|
187 | - } |
|
188 | - } |
|
177 | + /** |
|
178 | + * set the login name |
|
179 | + * |
|
180 | + * @param string|null $loginName for the logged in user |
|
181 | + */ |
|
182 | + public function setLoginName($loginName) { |
|
183 | + if (is_null($loginName)) { |
|
184 | + $this->session->remove('loginname'); |
|
185 | + } else { |
|
186 | + $this->session->set('loginname', $loginName); |
|
187 | + } |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * get the login name of the current user |
|
192 | - * |
|
193 | - * @return string |
|
194 | - */ |
|
195 | - public function getLoginName() { |
|
196 | - if ($this->activeUser) { |
|
197 | - return $this->session->get('loginname'); |
|
198 | - } else { |
|
199 | - $uid = $this->session->get('user_id'); |
|
200 | - if ($uid) { |
|
201 | - $this->activeUser = $this->manager->get($uid); |
|
202 | - return $this->session->get('loginname'); |
|
203 | - } else { |
|
204 | - return null; |
|
205 | - } |
|
206 | - } |
|
207 | - } |
|
190 | + /** |
|
191 | + * get the login name of the current user |
|
192 | + * |
|
193 | + * @return string |
|
194 | + */ |
|
195 | + public function getLoginName() { |
|
196 | + if ($this->activeUser) { |
|
197 | + return $this->session->get('loginname'); |
|
198 | + } else { |
|
199 | + $uid = $this->session->get('user_id'); |
|
200 | + if ($uid) { |
|
201 | + $this->activeUser = $this->manager->get($uid); |
|
202 | + return $this->session->get('loginname'); |
|
203 | + } else { |
|
204 | + return null; |
|
205 | + } |
|
206 | + } |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * try to login with the provided credentials |
|
211 | - * |
|
212 | - * @param string $uid |
|
213 | - * @param string $password |
|
214 | - * @return boolean|null |
|
215 | - * @throws LoginException |
|
216 | - */ |
|
217 | - public function login($uid, $password) { |
|
218 | - $this->session->regenerateId(); |
|
219 | - $this->manager->emit('\OC\User', 'preLogin', array($uid, $password)); |
|
220 | - $user = $this->manager->checkPassword($uid, $password); |
|
221 | - if ($user !== false) { |
|
222 | - if (!is_null($user)) { |
|
223 | - if ($user->isEnabled()) { |
|
224 | - $this->setUser($user); |
|
225 | - $this->setLoginName($uid); |
|
226 | - $this->manager->emit('\OC\User', 'postLogin', array($user, $password)); |
|
227 | - if ($this->isLoggedIn()) { |
|
228 | - return true; |
|
229 | - } else { |
|
230 | - throw new LoginException('Login canceled by app'); |
|
231 | - } |
|
232 | - } else { |
|
233 | - return false; |
|
234 | - } |
|
235 | - } |
|
236 | - } else { |
|
237 | - return false; |
|
238 | - } |
|
239 | - } |
|
209 | + /** |
|
210 | + * try to login with the provided credentials |
|
211 | + * |
|
212 | + * @param string $uid |
|
213 | + * @param string $password |
|
214 | + * @return boolean|null |
|
215 | + * @throws LoginException |
|
216 | + */ |
|
217 | + public function login($uid, $password) { |
|
218 | + $this->session->regenerateId(); |
|
219 | + $this->manager->emit('\OC\User', 'preLogin', array($uid, $password)); |
|
220 | + $user = $this->manager->checkPassword($uid, $password); |
|
221 | + if ($user !== false) { |
|
222 | + if (!is_null($user)) { |
|
223 | + if ($user->isEnabled()) { |
|
224 | + $this->setUser($user); |
|
225 | + $this->setLoginName($uid); |
|
226 | + $this->manager->emit('\OC\User', 'postLogin', array($user, $password)); |
|
227 | + if ($this->isLoggedIn()) { |
|
228 | + return true; |
|
229 | + } else { |
|
230 | + throw new LoginException('Login canceled by app'); |
|
231 | + } |
|
232 | + } else { |
|
233 | + return false; |
|
234 | + } |
|
235 | + } |
|
236 | + } else { |
|
237 | + return false; |
|
238 | + } |
|
239 | + } |
|
240 | 240 | |
241 | - /** |
|
242 | - * perform login using the magic cookie (remember login) |
|
243 | - * |
|
244 | - * @param string $uid the username |
|
245 | - * @param string $currentToken |
|
246 | - * @return bool |
|
247 | - */ |
|
248 | - public function loginWithCookie($uid, $currentToken) { |
|
249 | - $this->session->regenerateId(); |
|
250 | - $this->manager->emit('\OC\User', 'preRememberedLogin', array($uid)); |
|
251 | - $user = $this->manager->get($uid); |
|
252 | - if (is_null($user)) { |
|
253 | - // user does not exist |
|
254 | - return false; |
|
255 | - } |
|
241 | + /** |
|
242 | + * perform login using the magic cookie (remember login) |
|
243 | + * |
|
244 | + * @param string $uid the username |
|
245 | + * @param string $currentToken |
|
246 | + * @return bool |
|
247 | + */ |
|
248 | + public function loginWithCookie($uid, $currentToken) { |
|
249 | + $this->session->regenerateId(); |
|
250 | + $this->manager->emit('\OC\User', 'preRememberedLogin', array($uid)); |
|
251 | + $user = $this->manager->get($uid); |
|
252 | + if (is_null($user)) { |
|
253 | + // user does not exist |
|
254 | + return false; |
|
255 | + } |
|
256 | 256 | |
257 | - // get stored tokens |
|
258 | - $tokens = \OC::$server->getConfig()->getUserKeys($uid, 'login_token'); |
|
259 | - // test cookies token against stored tokens |
|
260 | - if (!in_array($currentToken, $tokens, true)) { |
|
261 | - return false; |
|
262 | - } |
|
263 | - // replace successfully used token with a new one |
|
264 | - \OC::$server->getConfig()->deleteUserValue($uid, 'login_token', $currentToken); |
|
265 | - $newToken = \OC::$server->getSecureRandom()->generate(32); |
|
266 | - \OC::$server->getConfig()->setUserValue($uid, 'login_token', $newToken, time()); |
|
267 | - $this->setMagicInCookie($user->getUID(), $newToken); |
|
257 | + // get stored tokens |
|
258 | + $tokens = \OC::$server->getConfig()->getUserKeys($uid, 'login_token'); |
|
259 | + // test cookies token against stored tokens |
|
260 | + if (!in_array($currentToken, $tokens, true)) { |
|
261 | + return false; |
|
262 | + } |
|
263 | + // replace successfully used token with a new one |
|
264 | + \OC::$server->getConfig()->deleteUserValue($uid, 'login_token', $currentToken); |
|
265 | + $newToken = \OC::$server->getSecureRandom()->generate(32); |
|
266 | + \OC::$server->getConfig()->setUserValue($uid, 'login_token', $newToken, time()); |
|
267 | + $this->setMagicInCookie($user->getUID(), $newToken); |
|
268 | 268 | |
269 | - //login |
|
270 | - $this->setUser($user); |
|
271 | - $this->manager->emit('\OC\User', 'postRememberedLogin', array($user)); |
|
272 | - return true; |
|
273 | - } |
|
269 | + //login |
|
270 | + $this->setUser($user); |
|
271 | + $this->manager->emit('\OC\User', 'postRememberedLogin', array($user)); |
|
272 | + return true; |
|
273 | + } |
|
274 | 274 | |
275 | - /** |
|
276 | - * logout the user from the session |
|
277 | - */ |
|
278 | - public function logout() { |
|
279 | - $this->manager->emit('\OC\User', 'logout'); |
|
280 | - $this->setUser(null); |
|
281 | - $this->setLoginName(null); |
|
282 | - $this->unsetMagicInCookie(); |
|
283 | - $this->session->clear(); |
|
284 | - } |
|
275 | + /** |
|
276 | + * logout the user from the session |
|
277 | + */ |
|
278 | + public function logout() { |
|
279 | + $this->manager->emit('\OC\User', 'logout'); |
|
280 | + $this->setUser(null); |
|
281 | + $this->setLoginName(null); |
|
282 | + $this->unsetMagicInCookie(); |
|
283 | + $this->session->clear(); |
|
284 | + } |
|
285 | 285 | |
286 | - /** |
|
287 | - * Set cookie value to use in next page load |
|
288 | - * |
|
289 | - * @param string $username username to be set |
|
290 | - * @param string $token |
|
291 | - */ |
|
292 | - public function setMagicInCookie($username, $token) { |
|
293 | - $secureCookie = \OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
294 | - $expires = time() + \OC::$server->getConfig()->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
295 | - setcookie("oc_username", $username, $expires, \OC::$WEBROOT, '', $secureCookie, true); |
|
296 | - setcookie("oc_token", $token, $expires, \OC::$WEBROOT, '', $secureCookie, true); |
|
297 | - setcookie("oc_remember_login", "1", $expires, \OC::$WEBROOT, '', $secureCookie, true); |
|
298 | - } |
|
286 | + /** |
|
287 | + * Set cookie value to use in next page load |
|
288 | + * |
|
289 | + * @param string $username username to be set |
|
290 | + * @param string $token |
|
291 | + */ |
|
292 | + public function setMagicInCookie($username, $token) { |
|
293 | + $secureCookie = \OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
294 | + $expires = time() + \OC::$server->getConfig()->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
295 | + setcookie("oc_username", $username, $expires, \OC::$WEBROOT, '', $secureCookie, true); |
|
296 | + setcookie("oc_token", $token, $expires, \OC::$WEBROOT, '', $secureCookie, true); |
|
297 | + setcookie("oc_remember_login", "1", $expires, \OC::$WEBROOT, '', $secureCookie, true); |
|
298 | + } |
|
299 | 299 | |
300 | - /** |
|
301 | - * Remove cookie for "remember username" |
|
302 | - */ |
|
303 | - public function unsetMagicInCookie() { |
|
304 | - //TODO: DI for cookies and IRequest |
|
305 | - $secureCookie = \OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
300 | + /** |
|
301 | + * Remove cookie for "remember username" |
|
302 | + */ |
|
303 | + public function unsetMagicInCookie() { |
|
304 | + //TODO: DI for cookies and IRequest |
|
305 | + $secureCookie = \OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
306 | 306 | |
307 | - unset($_COOKIE["oc_username"]); //TODO: DI |
|
308 | - unset($_COOKIE["oc_token"]); |
|
309 | - unset($_COOKIE["oc_remember_login"]); |
|
310 | - setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT, '',$secureCookie, true); |
|
311 | - setcookie('oc_token', '', time() - 3600, \OC::$WEBROOT, '', $secureCookie, true); |
|
312 | - setcookie('oc_remember_login', '', time() - 3600, \OC::$WEBROOT, '', $secureCookie, true); |
|
313 | - // old cookies might be stored under /webroot/ instead of /webroot |
|
314 | - // and Firefox doesn't like it! |
|
315 | - setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT . '/', '', $secureCookie, true); |
|
316 | - setcookie('oc_token', '', time() - 3600, \OC::$WEBROOT . '/', '', $secureCookie, true); |
|
317 | - setcookie('oc_remember_login', '', time() - 3600, \OC::$WEBROOT . '/', '', $secureCookie, true); |
|
318 | - } |
|
307 | + unset($_COOKIE["oc_username"]); //TODO: DI |
|
308 | + unset($_COOKIE["oc_token"]); |
|
309 | + unset($_COOKIE["oc_remember_login"]); |
|
310 | + setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT, '',$secureCookie, true); |
|
311 | + setcookie('oc_token', '', time() - 3600, \OC::$WEBROOT, '', $secureCookie, true); |
|
312 | + setcookie('oc_remember_login', '', time() - 3600, \OC::$WEBROOT, '', $secureCookie, true); |
|
313 | + // old cookies might be stored under /webroot/ instead of /webroot |
|
314 | + // and Firefox doesn't like it! |
|
315 | + setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT . '/', '', $secureCookie, true); |
|
316 | + setcookie('oc_token', '', time() - 3600, \OC::$WEBROOT . '/', '', $secureCookie, true); |
|
317 | + setcookie('oc_remember_login', '', time() - 3600, \OC::$WEBROOT . '/', '', $secureCookie, true); |
|
318 | + } |
|
319 | 319 | } |
@@ -307,13 +307,13 @@ |
||
307 | 307 | unset($_COOKIE["oc_username"]); //TODO: DI |
308 | 308 | unset($_COOKIE["oc_token"]); |
309 | 309 | unset($_COOKIE["oc_remember_login"]); |
310 | - setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT, '',$secureCookie, true); |
|
310 | + setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT, '', $secureCookie, true); |
|
311 | 311 | setcookie('oc_token', '', time() - 3600, \OC::$WEBROOT, '', $secureCookie, true); |
312 | 312 | setcookie('oc_remember_login', '', time() - 3600, \OC::$WEBROOT, '', $secureCookie, true); |
313 | 313 | // old cookies might be stored under /webroot/ instead of /webroot |
314 | 314 | // and Firefox doesn't like it! |
315 | - setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT . '/', '', $secureCookie, true); |
|
316 | - setcookie('oc_token', '', time() - 3600, \OC::$WEBROOT . '/', '', $secureCookie, true); |
|
317 | - setcookie('oc_remember_login', '', time() - 3600, \OC::$WEBROOT . '/', '', $secureCookie, true); |
|
315 | + setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT.'/', '', $secureCookie, true); |
|
316 | + setcookie('oc_token', '', time() - 3600, \OC::$WEBROOT.'/', '', $secureCookie, true); |
|
317 | + setcookie('oc_remember_login', '', time() - 3600, \OC::$WEBROOT.'/', '', $secureCookie, true); |
|
318 | 318 | } |
319 | 319 | } |
@@ -46,7 +46,7 @@ |
||
46 | 46 | * DispatcherEvent constructor. |
47 | 47 | * |
48 | 48 | * @param string $event |
49 | - * @param $appID |
|
49 | + * @param string $appID |
|
50 | 50 | * @param \OCP\IGroup[] $groups |
51 | 51 | * @since 9.0.0 |
52 | 52 | */ |
@@ -32,56 +32,56 @@ |
||
32 | 32 | */ |
33 | 33 | class ManagerEvent extends Event { |
34 | 34 | |
35 | - const EVENT_APP_ENABLE = 'OCP\App\IAppManager::enableApp'; |
|
36 | - const EVENT_APP_ENABLE_FOR_GROUPS = 'OCP\App\IAppManager::enableAppForGroups'; |
|
37 | - const EVENT_APP_DISABLE = 'OCP\App\IAppManager::disableApp'; |
|
35 | + const EVENT_APP_ENABLE = 'OCP\App\IAppManager::enableApp'; |
|
36 | + const EVENT_APP_ENABLE_FOR_GROUPS = 'OCP\App\IAppManager::enableAppForGroups'; |
|
37 | + const EVENT_APP_DISABLE = 'OCP\App\IAppManager::disableApp'; |
|
38 | 38 | |
39 | - /** @var string */ |
|
40 | - protected $event; |
|
41 | - /** @var string */ |
|
42 | - protected $appID; |
|
43 | - /** @var \OCP\IGroup[] */ |
|
44 | - protected $groups; |
|
39 | + /** @var string */ |
|
40 | + protected $event; |
|
41 | + /** @var string */ |
|
42 | + protected $appID; |
|
43 | + /** @var \OCP\IGroup[] */ |
|
44 | + protected $groups; |
|
45 | 45 | |
46 | - /** |
|
47 | - * DispatcherEvent constructor. |
|
48 | - * |
|
49 | - * @param string $event |
|
50 | - * @param $appID |
|
51 | - * @param \OCP\IGroup[] $groups |
|
52 | - * @since 9.0.0 |
|
53 | - */ |
|
54 | - public function __construct($event, $appID, array $groups = null) { |
|
55 | - $this->event = $event; |
|
56 | - $this->appID = $appID; |
|
57 | - $this->groups = $groups; |
|
58 | - } |
|
46 | + /** |
|
47 | + * DispatcherEvent constructor. |
|
48 | + * |
|
49 | + * @param string $event |
|
50 | + * @param $appID |
|
51 | + * @param \OCP\IGroup[] $groups |
|
52 | + * @since 9.0.0 |
|
53 | + */ |
|
54 | + public function __construct($event, $appID, array $groups = null) { |
|
55 | + $this->event = $event; |
|
56 | + $this->appID = $appID; |
|
57 | + $this->groups = $groups; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @return string |
|
62 | - * @since 9.0.0 |
|
63 | - */ |
|
64 | - public function getEvent() { |
|
65 | - return $this->event; |
|
66 | - } |
|
60 | + /** |
|
61 | + * @return string |
|
62 | + * @since 9.0.0 |
|
63 | + */ |
|
64 | + public function getEvent() { |
|
65 | + return $this->event; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return string |
|
70 | - * @since 9.0.0 |
|
71 | - */ |
|
72 | - public function getAppID() { |
|
73 | - return $this->appID; |
|
74 | - } |
|
68 | + /** |
|
69 | + * @return string |
|
70 | + * @since 9.0.0 |
|
71 | + */ |
|
72 | + public function getAppID() { |
|
73 | + return $this->appID; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * returns the group Ids |
|
78 | - * @return string[] |
|
79 | - * @since 9.0.0 |
|
80 | - */ |
|
81 | - public function getGroups() { |
|
82 | - return array_map(function ($group) { |
|
83 | - /** @var \OCP\IGroup $group */ |
|
84 | - return $group->getGID(); |
|
85 | - }, $this->groups); |
|
86 | - } |
|
76 | + /** |
|
77 | + * returns the group Ids |
|
78 | + * @return string[] |
|
79 | + * @since 9.0.0 |
|
80 | + */ |
|
81 | + public function getGroups() { |
|
82 | + return array_map(function ($group) { |
|
83 | + /** @var \OCP\IGroup $group */ |
|
84 | + return $group->getGID(); |
|
85 | + }, $this->groups); |
|
86 | + } |
|
87 | 87 | } |
@@ -84,7 +84,7 @@ |
||
84 | 84 | * @since 9.0.0 |
85 | 85 | */ |
86 | 86 | public function getGroups() { |
87 | - return array_map(function ($group) { |
|
87 | + return array_map(function($group) { |
|
88 | 88 | /** @var \OCP\IGroup $group */ |
89 | 89 | return $group->getGID(); |
90 | 90 | }, $this->groups); |
@@ -335,7 +335,7 @@ |
||
335 | 335 | * Returns an db result and throws exceptions when there are more or less |
336 | 336 | * results |
337 | 337 | * @param string $sql the sql query |
338 | - * @param array $params the parameters of the sql query |
|
338 | + * @param string[] $params the parameters of the sql query |
|
339 | 339 | * @param int $limit the maximum number of rows |
340 | 340 | * @param int $offset from which row we want to start |
341 | 341 | * @throws DoesNotExistException if the item does not exist |
@@ -38,315 +38,315 @@ |
||
38 | 38 | */ |
39 | 39 | abstract class Mapper { |
40 | 40 | |
41 | - protected $tableName; |
|
42 | - protected $entityClass; |
|
43 | - protected $db; |
|
44 | - |
|
45 | - /** |
|
46 | - * @param IDBConnection $db Instance of the Db abstraction layer |
|
47 | - * @param string $tableName the name of the table. set this to allow entity |
|
48 | - * @param string $entityClass the name of the entity that the sql should be |
|
49 | - * mapped to queries without using sql |
|
50 | - * @since 7.0.0 |
|
51 | - */ |
|
52 | - public function __construct(IDBConnection $db, $tableName, $entityClass=null){ |
|
53 | - $this->db = $db; |
|
54 | - $this->tableName = '*PREFIX*' . $tableName; |
|
55 | - |
|
56 | - // if not given set the entity name to the class without the mapper part |
|
57 | - // cache it here for later use since reflection is slow |
|
58 | - if($entityClass === null) { |
|
59 | - $this->entityClass = str_replace('Mapper', '', get_class($this)); |
|
60 | - } else { |
|
61 | - $this->entityClass = $entityClass; |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * @return string the table name |
|
68 | - * @since 7.0.0 |
|
69 | - */ |
|
70 | - public function getTableName(){ |
|
71 | - return $this->tableName; |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * Deletes an entity from the table |
|
77 | - * @param Entity $entity the entity that should be deleted |
|
78 | - * @return Entity the deleted entity |
|
79 | - * @since 7.0.0 - return value added in 8.1.0 |
|
80 | - */ |
|
81 | - public function delete(Entity $entity){ |
|
82 | - $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
83 | - $stmt = $this->execute($sql, [$entity->getId()]); |
|
84 | - $stmt->closeCursor(); |
|
85 | - return $entity; |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Creates a new entry in the db from an entity |
|
91 | - * @param Entity $entity the entity that should be created |
|
92 | - * @return Entity the saved entity with the set id |
|
93 | - * @since 7.0.0 |
|
94 | - */ |
|
95 | - public function insert(Entity $entity){ |
|
96 | - // get updated fields to save, fields have to be set using a setter to |
|
97 | - // be saved |
|
98 | - $properties = $entity->getUpdatedFields(); |
|
99 | - $values = ''; |
|
100 | - $columns = ''; |
|
101 | - $params = []; |
|
102 | - |
|
103 | - // build the fields |
|
104 | - $i = 0; |
|
105 | - foreach($properties as $property => $updated) { |
|
106 | - $column = $entity->propertyToColumn($property); |
|
107 | - $getter = 'get' . ucfirst($property); |
|
108 | - |
|
109 | - $columns .= '`' . $column . '`'; |
|
110 | - $values .= '?'; |
|
111 | - |
|
112 | - // only append colon if there are more entries |
|
113 | - if($i < count($properties)-1){ |
|
114 | - $columns .= ','; |
|
115 | - $values .= ','; |
|
116 | - } |
|
117 | - |
|
118 | - $params[] = $entity->$getter(); |
|
119 | - $i++; |
|
120 | - |
|
121 | - } |
|
122 | - |
|
123 | - $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
124 | - $columns . ') VALUES(' . $values . ')'; |
|
125 | - |
|
126 | - $stmt = $this->execute($sql, $params); |
|
127 | - |
|
128 | - $entity->setId((int) $this->db->lastInsertId($this->tableName)); |
|
129 | - |
|
130 | - $stmt->closeCursor(); |
|
131 | - |
|
132 | - return $entity; |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * Updates an entry in the db from an entity |
|
139 | - * @throws \InvalidArgumentException if entity has no id |
|
140 | - * @param Entity $entity the entity that should be created |
|
141 | - * @return Entity the saved entity with the set id |
|
142 | - * @since 7.0.0 - return value was added in 8.0.0 |
|
143 | - */ |
|
144 | - public function update(Entity $entity){ |
|
145 | - // if entity wasn't changed it makes no sense to run a db query |
|
146 | - $properties = $entity->getUpdatedFields(); |
|
147 | - if(count($properties) === 0) { |
|
148 | - return $entity; |
|
149 | - } |
|
150 | - |
|
151 | - // entity needs an id |
|
152 | - $id = $entity->getId(); |
|
153 | - if($id === null){ |
|
154 | - throw new \InvalidArgumentException( |
|
155 | - 'Entity which should be updated has no id'); |
|
156 | - } |
|
157 | - |
|
158 | - // get updated fields to save, fields have to be set using a setter to |
|
159 | - // be saved |
|
160 | - // do not update the id field |
|
161 | - unset($properties['id']); |
|
162 | - |
|
163 | - $columns = ''; |
|
164 | - $params = []; |
|
165 | - |
|
166 | - // build the fields |
|
167 | - $i = 0; |
|
168 | - foreach($properties as $property => $updated) { |
|
169 | - |
|
170 | - $column = $entity->propertyToColumn($property); |
|
171 | - $getter = 'get' . ucfirst($property); |
|
172 | - |
|
173 | - $columns .= '`' . $column . '` = ?'; |
|
174 | - |
|
175 | - // only append colon if there are more entries |
|
176 | - if($i < count($properties)-1){ |
|
177 | - $columns .= ','; |
|
178 | - } |
|
179 | - |
|
180 | - $params[] = $entity->$getter(); |
|
181 | - $i++; |
|
182 | - } |
|
183 | - |
|
184 | - $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
185 | - $columns . ' WHERE `id` = ?'; |
|
186 | - $params[] = $id; |
|
187 | - |
|
188 | - $stmt = $this->execute($sql, $params); |
|
189 | - $stmt->closeCursor(); |
|
190 | - |
|
191 | - return $entity; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Checks if an array is associative |
|
196 | - * @param array $array |
|
197 | - * @return bool true if associative |
|
198 | - * @since 8.1.0 |
|
199 | - */ |
|
200 | - private function isAssocArray(array $array) { |
|
201 | - return array_values($array) !== $array; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Returns the correct PDO constant based on the value type |
|
206 | - * @param $value |
|
207 | - * @return int PDO constant |
|
208 | - * @since 8.1.0 |
|
209 | - */ |
|
210 | - private function getPDOType($value) { |
|
211 | - switch (gettype($value)) { |
|
212 | - case 'integer': |
|
213 | - return \PDO::PARAM_INT; |
|
214 | - case 'boolean': |
|
215 | - return \PDO::PARAM_BOOL; |
|
216 | - default: |
|
217 | - return \PDO::PARAM_STR; |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - |
|
222 | - /** |
|
223 | - * Runs an sql query |
|
224 | - * @param string $sql the prepare string |
|
225 | - * @param array $params the params which should replace the ? in the sql query |
|
226 | - * @param int $limit the maximum number of rows |
|
227 | - * @param int $offset from which row we want to start |
|
228 | - * @return \PDOStatement the database query result |
|
229 | - * @since 7.0.0 |
|
230 | - */ |
|
231 | - protected function execute($sql, array $params=[], $limit=null, $offset=null){ |
|
232 | - if ($this->db instanceof IDb) { |
|
233 | - $query = $this->db->prepareQuery($sql, $limit, $offset); |
|
234 | - } else { |
|
235 | - $query = $this->db->prepare($sql, $limit, $offset); |
|
236 | - } |
|
237 | - |
|
238 | - if ($this->isAssocArray($params)) { |
|
239 | - foreach ($params as $key => $param) { |
|
240 | - $pdoConstant = $this->getPDOType($param); |
|
241 | - $query->bindValue($key, $param, $pdoConstant); |
|
242 | - } |
|
243 | - } else { |
|
244 | - $index = 1; // bindParam is 1 indexed |
|
245 | - foreach ($params as $param) { |
|
246 | - $pdoConstant = $this->getPDOType($param); |
|
247 | - $query->bindValue($index, $param, $pdoConstant); |
|
248 | - $index++; |
|
249 | - } |
|
250 | - } |
|
251 | - |
|
252 | - $result = $query->execute(); |
|
253 | - |
|
254 | - // this is only for backwards compatibility reasons and can be removed |
|
255 | - // in owncloud 10. IDb returns a StatementWrapper from execute, PDO, |
|
256 | - // Doctrine and IDbConnection don't so this needs to be done in order |
|
257 | - // to stay backwards compatible for the things that rely on the |
|
258 | - // StatementWrapper being returned |
|
259 | - if ($result instanceof \OC_DB_StatementWrapper) { |
|
260 | - return $result; |
|
261 | - } |
|
262 | - |
|
263 | - return $query; |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * Returns an db result and throws exceptions when there are more or less |
|
269 | - * results |
|
270 | - * @see findEntity |
|
271 | - * @param string $sql the sql query |
|
272 | - * @param array $params the parameters of the sql query |
|
273 | - * @param int $limit the maximum number of rows |
|
274 | - * @param int $offset from which row we want to start |
|
275 | - * @throws DoesNotExistException if the item does not exist |
|
276 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
277 | - * @return array the result as row |
|
278 | - * @since 7.0.0 |
|
279 | - */ |
|
280 | - protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null){ |
|
281 | - $stmt = $this->execute($sql, $params, $limit, $offset); |
|
282 | - $row = $stmt->fetch(); |
|
283 | - |
|
284 | - if($row === false || $row === null){ |
|
285 | - $stmt->closeCursor(); |
|
286 | - throw new DoesNotExistException('No matching entry found'); |
|
287 | - } |
|
288 | - $row2 = $stmt->fetch(); |
|
289 | - $stmt->closeCursor(); |
|
290 | - //MDB2 returns null, PDO and doctrine false when no row is available |
|
291 | - if( ! ($row2 === false || $row2 === null )) { |
|
292 | - throw new MultipleObjectsReturnedException('More than one result'); |
|
293 | - } else { |
|
294 | - return $row; |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * Creates an entity from a row. Automatically determines the entity class |
|
301 | - * from the current mapper name (MyEntityMapper -> MyEntity) |
|
302 | - * @param array $row the row which should be converted to an entity |
|
303 | - * @return Entity the entity |
|
304 | - * @since 7.0.0 |
|
305 | - */ |
|
306 | - protected function mapRowToEntity($row) { |
|
307 | - return call_user_func($this->entityClass .'::fromRow', $row); |
|
308 | - } |
|
309 | - |
|
310 | - |
|
311 | - /** |
|
312 | - * Runs a sql query and returns an array of entities |
|
313 | - * @param string $sql the prepare string |
|
314 | - * @param array $params the params which should replace the ? in the sql query |
|
315 | - * @param int $limit the maximum number of rows |
|
316 | - * @param int $offset from which row we want to start |
|
317 | - * @return array all fetched entities |
|
318 | - * @since 7.0.0 |
|
319 | - */ |
|
320 | - protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
321 | - $stmt = $this->execute($sql, $params, $limit, $offset); |
|
322 | - |
|
323 | - $entities = []; |
|
324 | - |
|
325 | - while($row = $stmt->fetch()){ |
|
326 | - $entities[] = $this->mapRowToEntity($row); |
|
327 | - } |
|
328 | - |
|
329 | - $stmt->closeCursor(); |
|
330 | - |
|
331 | - return $entities; |
|
332 | - } |
|
333 | - |
|
334 | - |
|
335 | - /** |
|
336 | - * Returns an db result and throws exceptions when there are more or less |
|
337 | - * results |
|
338 | - * @param string $sql the sql query |
|
339 | - * @param array $params the parameters of the sql query |
|
340 | - * @param int $limit the maximum number of rows |
|
341 | - * @param int $offset from which row we want to start |
|
342 | - * @throws DoesNotExistException if the item does not exist |
|
343 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
344 | - * @return Entity the entity |
|
345 | - * @since 7.0.0 |
|
346 | - */ |
|
347 | - protected function findEntity($sql, array $params=[], $limit=null, $offset=null){ |
|
348 | - return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
|
349 | - } |
|
41 | + protected $tableName; |
|
42 | + protected $entityClass; |
|
43 | + protected $db; |
|
44 | + |
|
45 | + /** |
|
46 | + * @param IDBConnection $db Instance of the Db abstraction layer |
|
47 | + * @param string $tableName the name of the table. set this to allow entity |
|
48 | + * @param string $entityClass the name of the entity that the sql should be |
|
49 | + * mapped to queries without using sql |
|
50 | + * @since 7.0.0 |
|
51 | + */ |
|
52 | + public function __construct(IDBConnection $db, $tableName, $entityClass=null){ |
|
53 | + $this->db = $db; |
|
54 | + $this->tableName = '*PREFIX*' . $tableName; |
|
55 | + |
|
56 | + // if not given set the entity name to the class without the mapper part |
|
57 | + // cache it here for later use since reflection is slow |
|
58 | + if($entityClass === null) { |
|
59 | + $this->entityClass = str_replace('Mapper', '', get_class($this)); |
|
60 | + } else { |
|
61 | + $this->entityClass = $entityClass; |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * @return string the table name |
|
68 | + * @since 7.0.0 |
|
69 | + */ |
|
70 | + public function getTableName(){ |
|
71 | + return $this->tableName; |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * Deletes an entity from the table |
|
77 | + * @param Entity $entity the entity that should be deleted |
|
78 | + * @return Entity the deleted entity |
|
79 | + * @since 7.0.0 - return value added in 8.1.0 |
|
80 | + */ |
|
81 | + public function delete(Entity $entity){ |
|
82 | + $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
83 | + $stmt = $this->execute($sql, [$entity->getId()]); |
|
84 | + $stmt->closeCursor(); |
|
85 | + return $entity; |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Creates a new entry in the db from an entity |
|
91 | + * @param Entity $entity the entity that should be created |
|
92 | + * @return Entity the saved entity with the set id |
|
93 | + * @since 7.0.0 |
|
94 | + */ |
|
95 | + public function insert(Entity $entity){ |
|
96 | + // get updated fields to save, fields have to be set using a setter to |
|
97 | + // be saved |
|
98 | + $properties = $entity->getUpdatedFields(); |
|
99 | + $values = ''; |
|
100 | + $columns = ''; |
|
101 | + $params = []; |
|
102 | + |
|
103 | + // build the fields |
|
104 | + $i = 0; |
|
105 | + foreach($properties as $property => $updated) { |
|
106 | + $column = $entity->propertyToColumn($property); |
|
107 | + $getter = 'get' . ucfirst($property); |
|
108 | + |
|
109 | + $columns .= '`' . $column . '`'; |
|
110 | + $values .= '?'; |
|
111 | + |
|
112 | + // only append colon if there are more entries |
|
113 | + if($i < count($properties)-1){ |
|
114 | + $columns .= ','; |
|
115 | + $values .= ','; |
|
116 | + } |
|
117 | + |
|
118 | + $params[] = $entity->$getter(); |
|
119 | + $i++; |
|
120 | + |
|
121 | + } |
|
122 | + |
|
123 | + $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
124 | + $columns . ') VALUES(' . $values . ')'; |
|
125 | + |
|
126 | + $stmt = $this->execute($sql, $params); |
|
127 | + |
|
128 | + $entity->setId((int) $this->db->lastInsertId($this->tableName)); |
|
129 | + |
|
130 | + $stmt->closeCursor(); |
|
131 | + |
|
132 | + return $entity; |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * Updates an entry in the db from an entity |
|
139 | + * @throws \InvalidArgumentException if entity has no id |
|
140 | + * @param Entity $entity the entity that should be created |
|
141 | + * @return Entity the saved entity with the set id |
|
142 | + * @since 7.0.0 - return value was added in 8.0.0 |
|
143 | + */ |
|
144 | + public function update(Entity $entity){ |
|
145 | + // if entity wasn't changed it makes no sense to run a db query |
|
146 | + $properties = $entity->getUpdatedFields(); |
|
147 | + if(count($properties) === 0) { |
|
148 | + return $entity; |
|
149 | + } |
|
150 | + |
|
151 | + // entity needs an id |
|
152 | + $id = $entity->getId(); |
|
153 | + if($id === null){ |
|
154 | + throw new \InvalidArgumentException( |
|
155 | + 'Entity which should be updated has no id'); |
|
156 | + } |
|
157 | + |
|
158 | + // get updated fields to save, fields have to be set using a setter to |
|
159 | + // be saved |
|
160 | + // do not update the id field |
|
161 | + unset($properties['id']); |
|
162 | + |
|
163 | + $columns = ''; |
|
164 | + $params = []; |
|
165 | + |
|
166 | + // build the fields |
|
167 | + $i = 0; |
|
168 | + foreach($properties as $property => $updated) { |
|
169 | + |
|
170 | + $column = $entity->propertyToColumn($property); |
|
171 | + $getter = 'get' . ucfirst($property); |
|
172 | + |
|
173 | + $columns .= '`' . $column . '` = ?'; |
|
174 | + |
|
175 | + // only append colon if there are more entries |
|
176 | + if($i < count($properties)-1){ |
|
177 | + $columns .= ','; |
|
178 | + } |
|
179 | + |
|
180 | + $params[] = $entity->$getter(); |
|
181 | + $i++; |
|
182 | + } |
|
183 | + |
|
184 | + $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
185 | + $columns . ' WHERE `id` = ?'; |
|
186 | + $params[] = $id; |
|
187 | + |
|
188 | + $stmt = $this->execute($sql, $params); |
|
189 | + $stmt->closeCursor(); |
|
190 | + |
|
191 | + return $entity; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Checks if an array is associative |
|
196 | + * @param array $array |
|
197 | + * @return bool true if associative |
|
198 | + * @since 8.1.0 |
|
199 | + */ |
|
200 | + private function isAssocArray(array $array) { |
|
201 | + return array_values($array) !== $array; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Returns the correct PDO constant based on the value type |
|
206 | + * @param $value |
|
207 | + * @return int PDO constant |
|
208 | + * @since 8.1.0 |
|
209 | + */ |
|
210 | + private function getPDOType($value) { |
|
211 | + switch (gettype($value)) { |
|
212 | + case 'integer': |
|
213 | + return \PDO::PARAM_INT; |
|
214 | + case 'boolean': |
|
215 | + return \PDO::PARAM_BOOL; |
|
216 | + default: |
|
217 | + return \PDO::PARAM_STR; |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + |
|
222 | + /** |
|
223 | + * Runs an sql query |
|
224 | + * @param string $sql the prepare string |
|
225 | + * @param array $params the params which should replace the ? in the sql query |
|
226 | + * @param int $limit the maximum number of rows |
|
227 | + * @param int $offset from which row we want to start |
|
228 | + * @return \PDOStatement the database query result |
|
229 | + * @since 7.0.0 |
|
230 | + */ |
|
231 | + protected function execute($sql, array $params=[], $limit=null, $offset=null){ |
|
232 | + if ($this->db instanceof IDb) { |
|
233 | + $query = $this->db->prepareQuery($sql, $limit, $offset); |
|
234 | + } else { |
|
235 | + $query = $this->db->prepare($sql, $limit, $offset); |
|
236 | + } |
|
237 | + |
|
238 | + if ($this->isAssocArray($params)) { |
|
239 | + foreach ($params as $key => $param) { |
|
240 | + $pdoConstant = $this->getPDOType($param); |
|
241 | + $query->bindValue($key, $param, $pdoConstant); |
|
242 | + } |
|
243 | + } else { |
|
244 | + $index = 1; // bindParam is 1 indexed |
|
245 | + foreach ($params as $param) { |
|
246 | + $pdoConstant = $this->getPDOType($param); |
|
247 | + $query->bindValue($index, $param, $pdoConstant); |
|
248 | + $index++; |
|
249 | + } |
|
250 | + } |
|
251 | + |
|
252 | + $result = $query->execute(); |
|
253 | + |
|
254 | + // this is only for backwards compatibility reasons and can be removed |
|
255 | + // in owncloud 10. IDb returns a StatementWrapper from execute, PDO, |
|
256 | + // Doctrine and IDbConnection don't so this needs to be done in order |
|
257 | + // to stay backwards compatible for the things that rely on the |
|
258 | + // StatementWrapper being returned |
|
259 | + if ($result instanceof \OC_DB_StatementWrapper) { |
|
260 | + return $result; |
|
261 | + } |
|
262 | + |
|
263 | + return $query; |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * Returns an db result and throws exceptions when there are more or less |
|
269 | + * results |
|
270 | + * @see findEntity |
|
271 | + * @param string $sql the sql query |
|
272 | + * @param array $params the parameters of the sql query |
|
273 | + * @param int $limit the maximum number of rows |
|
274 | + * @param int $offset from which row we want to start |
|
275 | + * @throws DoesNotExistException if the item does not exist |
|
276 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
277 | + * @return array the result as row |
|
278 | + * @since 7.0.0 |
|
279 | + */ |
|
280 | + protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null){ |
|
281 | + $stmt = $this->execute($sql, $params, $limit, $offset); |
|
282 | + $row = $stmt->fetch(); |
|
283 | + |
|
284 | + if($row === false || $row === null){ |
|
285 | + $stmt->closeCursor(); |
|
286 | + throw new DoesNotExistException('No matching entry found'); |
|
287 | + } |
|
288 | + $row2 = $stmt->fetch(); |
|
289 | + $stmt->closeCursor(); |
|
290 | + //MDB2 returns null, PDO and doctrine false when no row is available |
|
291 | + if( ! ($row2 === false || $row2 === null )) { |
|
292 | + throw new MultipleObjectsReturnedException('More than one result'); |
|
293 | + } else { |
|
294 | + return $row; |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * Creates an entity from a row. Automatically determines the entity class |
|
301 | + * from the current mapper name (MyEntityMapper -> MyEntity) |
|
302 | + * @param array $row the row which should be converted to an entity |
|
303 | + * @return Entity the entity |
|
304 | + * @since 7.0.0 |
|
305 | + */ |
|
306 | + protected function mapRowToEntity($row) { |
|
307 | + return call_user_func($this->entityClass .'::fromRow', $row); |
|
308 | + } |
|
309 | + |
|
310 | + |
|
311 | + /** |
|
312 | + * Runs a sql query and returns an array of entities |
|
313 | + * @param string $sql the prepare string |
|
314 | + * @param array $params the params which should replace the ? in the sql query |
|
315 | + * @param int $limit the maximum number of rows |
|
316 | + * @param int $offset from which row we want to start |
|
317 | + * @return array all fetched entities |
|
318 | + * @since 7.0.0 |
|
319 | + */ |
|
320 | + protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
321 | + $stmt = $this->execute($sql, $params, $limit, $offset); |
|
322 | + |
|
323 | + $entities = []; |
|
324 | + |
|
325 | + while($row = $stmt->fetch()){ |
|
326 | + $entities[] = $this->mapRowToEntity($row); |
|
327 | + } |
|
328 | + |
|
329 | + $stmt->closeCursor(); |
|
330 | + |
|
331 | + return $entities; |
|
332 | + } |
|
333 | + |
|
334 | + |
|
335 | + /** |
|
336 | + * Returns an db result and throws exceptions when there are more or less |
|
337 | + * results |
|
338 | + * @param string $sql the sql query |
|
339 | + * @param array $params the parameters of the sql query |
|
340 | + * @param int $limit the maximum number of rows |
|
341 | + * @param int $offset from which row we want to start |
|
342 | + * @throws DoesNotExistException if the item does not exist |
|
343 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
344 | + * @return Entity the entity |
|
345 | + * @since 7.0.0 |
|
346 | + */ |
|
347 | + protected function findEntity($sql, array $params=[], $limit=null, $offset=null){ |
|
348 | + return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
|
349 | + } |
|
350 | 350 | |
351 | 351 | |
352 | 352 | } |
@@ -49,13 +49,13 @@ discard block |
||
49 | 49 | * mapped to queries without using sql |
50 | 50 | * @since 7.0.0 |
51 | 51 | */ |
52 | - public function __construct(IDBConnection $db, $tableName, $entityClass=null){ |
|
52 | + public function __construct(IDBConnection $db, $tableName, $entityClass = null) { |
|
53 | 53 | $this->db = $db; |
54 | - $this->tableName = '*PREFIX*' . $tableName; |
|
54 | + $this->tableName = '*PREFIX*'.$tableName; |
|
55 | 55 | |
56 | 56 | // if not given set the entity name to the class without the mapper part |
57 | 57 | // cache it here for later use since reflection is slow |
58 | - if($entityClass === null) { |
|
58 | + if ($entityClass === null) { |
|
59 | 59 | $this->entityClass = str_replace('Mapper', '', get_class($this)); |
60 | 60 | } else { |
61 | 61 | $this->entityClass = $entityClass; |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | * @return string the table name |
68 | 68 | * @since 7.0.0 |
69 | 69 | */ |
70 | - public function getTableName(){ |
|
70 | + public function getTableName() { |
|
71 | 71 | return $this->tableName; |
72 | 72 | } |
73 | 73 | |
@@ -78,8 +78,8 @@ discard block |
||
78 | 78 | * @return Entity the deleted entity |
79 | 79 | * @since 7.0.0 - return value added in 8.1.0 |
80 | 80 | */ |
81 | - public function delete(Entity $entity){ |
|
82 | - $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
81 | + public function delete(Entity $entity) { |
|
82 | + $sql = 'DELETE FROM `'.$this->tableName.'` WHERE `id` = ?'; |
|
83 | 83 | $stmt = $this->execute($sql, [$entity->getId()]); |
84 | 84 | $stmt->closeCursor(); |
85 | 85 | return $entity; |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | * @return Entity the saved entity with the set id |
93 | 93 | * @since 7.0.0 |
94 | 94 | */ |
95 | - public function insert(Entity $entity){ |
|
95 | + public function insert(Entity $entity) { |
|
96 | 96 | // get updated fields to save, fields have to be set using a setter to |
97 | 97 | // be saved |
98 | 98 | $properties = $entity->getUpdatedFields(); |
@@ -102,15 +102,15 @@ discard block |
||
102 | 102 | |
103 | 103 | // build the fields |
104 | 104 | $i = 0; |
105 | - foreach($properties as $property => $updated) { |
|
105 | + foreach ($properties as $property => $updated) { |
|
106 | 106 | $column = $entity->propertyToColumn($property); |
107 | - $getter = 'get' . ucfirst($property); |
|
107 | + $getter = 'get'.ucfirst($property); |
|
108 | 108 | |
109 | - $columns .= '`' . $column . '`'; |
|
109 | + $columns .= '`'.$column.'`'; |
|
110 | 110 | $values .= '?'; |
111 | 111 | |
112 | 112 | // only append colon if there are more entries |
113 | - if($i < count($properties)-1){ |
|
113 | + if ($i < count($properties) - 1) { |
|
114 | 114 | $columns .= ','; |
115 | 115 | $values .= ','; |
116 | 116 | } |
@@ -120,8 +120,8 @@ discard block |
||
120 | 120 | |
121 | 121 | } |
122 | 122 | |
123 | - $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
124 | - $columns . ') VALUES(' . $values . ')'; |
|
123 | + $sql = 'INSERT INTO `'.$this->tableName.'`('. |
|
124 | + $columns.') VALUES('.$values.')'; |
|
125 | 125 | |
126 | 126 | $stmt = $this->execute($sql, $params); |
127 | 127 | |
@@ -141,16 +141,16 @@ discard block |
||
141 | 141 | * @return Entity the saved entity with the set id |
142 | 142 | * @since 7.0.0 - return value was added in 8.0.0 |
143 | 143 | */ |
144 | - public function update(Entity $entity){ |
|
144 | + public function update(Entity $entity) { |
|
145 | 145 | // if entity wasn't changed it makes no sense to run a db query |
146 | 146 | $properties = $entity->getUpdatedFields(); |
147 | - if(count($properties) === 0) { |
|
147 | + if (count($properties) === 0) { |
|
148 | 148 | return $entity; |
149 | 149 | } |
150 | 150 | |
151 | 151 | // entity needs an id |
152 | 152 | $id = $entity->getId(); |
153 | - if($id === null){ |
|
153 | + if ($id === null) { |
|
154 | 154 | throw new \InvalidArgumentException( |
155 | 155 | 'Entity which should be updated has no id'); |
156 | 156 | } |
@@ -165,15 +165,15 @@ discard block |
||
165 | 165 | |
166 | 166 | // build the fields |
167 | 167 | $i = 0; |
168 | - foreach($properties as $property => $updated) { |
|
168 | + foreach ($properties as $property => $updated) { |
|
169 | 169 | |
170 | 170 | $column = $entity->propertyToColumn($property); |
171 | - $getter = 'get' . ucfirst($property); |
|
171 | + $getter = 'get'.ucfirst($property); |
|
172 | 172 | |
173 | - $columns .= '`' . $column . '` = ?'; |
|
173 | + $columns .= '`'.$column.'` = ?'; |
|
174 | 174 | |
175 | 175 | // only append colon if there are more entries |
176 | - if($i < count($properties)-1){ |
|
176 | + if ($i < count($properties) - 1) { |
|
177 | 177 | $columns .= ','; |
178 | 178 | } |
179 | 179 | |
@@ -181,8 +181,8 @@ discard block |
||
181 | 181 | $i++; |
182 | 182 | } |
183 | 183 | |
184 | - $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
185 | - $columns . ' WHERE `id` = ?'; |
|
184 | + $sql = 'UPDATE `'.$this->tableName.'` SET '. |
|
185 | + $columns.' WHERE `id` = ?'; |
|
186 | 186 | $params[] = $id; |
187 | 187 | |
188 | 188 | $stmt = $this->execute($sql, $params); |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | * @return \PDOStatement the database query result |
229 | 229 | * @since 7.0.0 |
230 | 230 | */ |
231 | - protected function execute($sql, array $params=[], $limit=null, $offset=null){ |
|
231 | + protected function execute($sql, array $params = [], $limit = null, $offset = null) { |
|
232 | 232 | if ($this->db instanceof IDb) { |
233 | 233 | $query = $this->db->prepareQuery($sql, $limit, $offset); |
234 | 234 | } else { |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | $query->bindValue($key, $param, $pdoConstant); |
242 | 242 | } |
243 | 243 | } else { |
244 | - $index = 1; // bindParam is 1 indexed |
|
244 | + $index = 1; // bindParam is 1 indexed |
|
245 | 245 | foreach ($params as $param) { |
246 | 246 | $pdoConstant = $this->getPDOType($param); |
247 | 247 | $query->bindValue($index, $param, $pdoConstant); |
@@ -277,18 +277,18 @@ discard block |
||
277 | 277 | * @return array the result as row |
278 | 278 | * @since 7.0.0 |
279 | 279 | */ |
280 | - protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null){ |
|
280 | + protected function findOneQuery($sql, array $params = [], $limit = null, $offset = null) { |
|
281 | 281 | $stmt = $this->execute($sql, $params, $limit, $offset); |
282 | 282 | $row = $stmt->fetch(); |
283 | 283 | |
284 | - if($row === false || $row === null){ |
|
284 | + if ($row === false || $row === null) { |
|
285 | 285 | $stmt->closeCursor(); |
286 | 286 | throw new DoesNotExistException('No matching entry found'); |
287 | 287 | } |
288 | 288 | $row2 = $stmt->fetch(); |
289 | 289 | $stmt->closeCursor(); |
290 | 290 | //MDB2 returns null, PDO and doctrine false when no row is available |
291 | - if( ! ($row2 === false || $row2 === null )) { |
|
291 | + if (!($row2 === false || $row2 === null)) { |
|
292 | 292 | throw new MultipleObjectsReturnedException('More than one result'); |
293 | 293 | } else { |
294 | 294 | return $row; |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * @since 7.0.0 |
305 | 305 | */ |
306 | 306 | protected function mapRowToEntity($row) { |
307 | - return call_user_func($this->entityClass .'::fromRow', $row); |
|
307 | + return call_user_func($this->entityClass.'::fromRow', $row); |
|
308 | 308 | } |
309 | 309 | |
310 | 310 | |
@@ -317,12 +317,12 @@ discard block |
||
317 | 317 | * @return array all fetched entities |
318 | 318 | * @since 7.0.0 |
319 | 319 | */ |
320 | - protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
320 | + protected function findEntities($sql, array $params = [], $limit = null, $offset = null) { |
|
321 | 321 | $stmt = $this->execute($sql, $params, $limit, $offset); |
322 | 322 | |
323 | 323 | $entities = []; |
324 | 324 | |
325 | - while($row = $stmt->fetch()){ |
|
325 | + while ($row = $stmt->fetch()) { |
|
326 | 326 | $entities[] = $this->mapRowToEntity($row); |
327 | 327 | } |
328 | 328 | |
@@ -344,7 +344,7 @@ discard block |
||
344 | 344 | * @return Entity the entity |
345 | 345 | * @since 7.0.0 |
346 | 346 | */ |
347 | - protected function findEntity($sql, array $params=[], $limit=null, $offset=null){ |
|
347 | + protected function findEntity($sql, array $params = [], $limit = null, $offset = null) { |
|
348 | 348 | return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
349 | 349 | } |
350 | 350 |
@@ -46,6 +46,7 @@ discard block |
||
46 | 46 | class Files { |
47 | 47 | /** |
48 | 48 | * Recusive deletion of folders |
49 | + * @param string $dir |
|
49 | 50 | * @return bool |
50 | 51 | * @since 5.0.0 |
51 | 52 | */ |
@@ -67,7 +68,7 @@ discard block |
||
67 | 68 | /** |
68 | 69 | * Search for files by mimetype |
69 | 70 | * @param string $mimetype |
70 | - * @return array |
|
71 | + * @return \OC\Files\FileInfo[] |
|
71 | 72 | * @since 6.0.0 |
72 | 73 | */ |
73 | 74 | static public function searchByMime( $mimetype ) { |
@@ -44,92 +44,92 @@ |
||
44 | 44 | * @since 5.0.0 |
45 | 45 | */ |
46 | 46 | class Files { |
47 | - /** |
|
48 | - * Recusive deletion of folders |
|
49 | - * @return bool |
|
50 | - * @since 5.0.0 |
|
51 | - */ |
|
52 | - static function rmdirr( $dir ) { |
|
53 | - return \OC_Helper::rmdirr( $dir ); |
|
54 | - } |
|
47 | + /** |
|
48 | + * Recusive deletion of folders |
|
49 | + * @return bool |
|
50 | + * @since 5.0.0 |
|
51 | + */ |
|
52 | + static function rmdirr( $dir ) { |
|
53 | + return \OC_Helper::rmdirr( $dir ); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get the mimetype form a local file |
|
58 | - * @param string $path |
|
59 | - * @return string |
|
60 | - * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead |
|
61 | - * @since 5.0.0 |
|
62 | - */ |
|
63 | - static function getMimeType( $path ) { |
|
64 | - return \OC::$server->getMimeTypeDetector()->detect($path); |
|
65 | - } |
|
56 | + /** |
|
57 | + * Get the mimetype form a local file |
|
58 | + * @param string $path |
|
59 | + * @return string |
|
60 | + * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead |
|
61 | + * @since 5.0.0 |
|
62 | + */ |
|
63 | + static function getMimeType( $path ) { |
|
64 | + return \OC::$server->getMimeTypeDetector()->detect($path); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Search for files by mimetype |
|
69 | - * @param string $mimetype |
|
70 | - * @return array |
|
71 | - * @since 6.0.0 |
|
72 | - */ |
|
73 | - static public function searchByMime( $mimetype ) { |
|
74 | - return(\OC\Files\Filesystem::searchByMime( $mimetype )); |
|
75 | - } |
|
67 | + /** |
|
68 | + * Search for files by mimetype |
|
69 | + * @param string $mimetype |
|
70 | + * @return array |
|
71 | + * @since 6.0.0 |
|
72 | + */ |
|
73 | + static public function searchByMime( $mimetype ) { |
|
74 | + return(\OC\Files\Filesystem::searchByMime( $mimetype )); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Copy the contents of one stream to another |
|
79 | - * @param resource $source |
|
80 | - * @param resource $target |
|
81 | - * @return int the number of bytes copied |
|
82 | - * @since 5.0.0 |
|
83 | - */ |
|
84 | - public static function streamCopy( $source, $target ) { |
|
85 | - list($count, ) = \OC_Helper::streamCopy( $source, $target ); |
|
86 | - return $count; |
|
87 | - } |
|
77 | + /** |
|
78 | + * Copy the contents of one stream to another |
|
79 | + * @param resource $source |
|
80 | + * @param resource $target |
|
81 | + * @return int the number of bytes copied |
|
82 | + * @since 5.0.0 |
|
83 | + */ |
|
84 | + public static function streamCopy( $source, $target ) { |
|
85 | + list($count, ) = \OC_Helper::streamCopy( $source, $target ); |
|
86 | + return $count; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Create a temporary file with an unique filename |
|
91 | - * @param string $postfix |
|
92 | - * @return string |
|
93 | - * |
|
94 | - * temporary files are automatically cleaned up after the script is finished |
|
95 | - * @deprecated 8.1.0 use getTemporaryFile() of \OCP\ITempManager - \OC::$server->getTempManager() |
|
96 | - * @since 5.0.0 |
|
97 | - */ |
|
98 | - public static function tmpFile( $postfix='' ) { |
|
99 | - return \OC::$server->getTempManager()->getTemporaryFile($postfix); |
|
100 | - } |
|
89 | + /** |
|
90 | + * Create a temporary file with an unique filename |
|
91 | + * @param string $postfix |
|
92 | + * @return string |
|
93 | + * |
|
94 | + * temporary files are automatically cleaned up after the script is finished |
|
95 | + * @deprecated 8.1.0 use getTemporaryFile() of \OCP\ITempManager - \OC::$server->getTempManager() |
|
96 | + * @since 5.0.0 |
|
97 | + */ |
|
98 | + public static function tmpFile( $postfix='' ) { |
|
99 | + return \OC::$server->getTempManager()->getTemporaryFile($postfix); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Create a temporary folder with an unique filename |
|
104 | - * @return string |
|
105 | - * |
|
106 | - * temporary files are automatically cleaned up after the script is finished |
|
107 | - * @deprecated 8.1.0 use getTemporaryFolder() of \OCP\ITempManager - \OC::$server->getTempManager() |
|
108 | - * @since 5.0.0 |
|
109 | - */ |
|
110 | - public static function tmpFolder() { |
|
111 | - return \OC::$server->getTempManager()->getTemporaryFolder(); |
|
112 | - } |
|
102 | + /** |
|
103 | + * Create a temporary folder with an unique filename |
|
104 | + * @return string |
|
105 | + * |
|
106 | + * temporary files are automatically cleaned up after the script is finished |
|
107 | + * @deprecated 8.1.0 use getTemporaryFolder() of \OCP\ITempManager - \OC::$server->getTempManager() |
|
108 | + * @since 5.0.0 |
|
109 | + */ |
|
110 | + public static function tmpFolder() { |
|
111 | + return \OC::$server->getTempManager()->getTemporaryFolder(); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Adds a suffix to the name in case the file exists |
|
116 | - * @param string $path |
|
117 | - * @param string $filename |
|
118 | - * @return string |
|
119 | - * @since 5.0.0 |
|
120 | - */ |
|
121 | - public static function buildNotExistingFileName( $path, $filename ) { |
|
122 | - return(\OC_Helper::buildNotExistingFileName( $path, $filename )); |
|
123 | - } |
|
114 | + /** |
|
115 | + * Adds a suffix to the name in case the file exists |
|
116 | + * @param string $path |
|
117 | + * @param string $filename |
|
118 | + * @return string |
|
119 | + * @since 5.0.0 |
|
120 | + */ |
|
121 | + public static function buildNotExistingFileName( $path, $filename ) { |
|
122 | + return(\OC_Helper::buildNotExistingFileName( $path, $filename )); |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * Gets the Storage for an app - creates the needed folder if they are not |
|
127 | - * existant |
|
128 | - * @param string $app |
|
129 | - * @return \OC\Files\View |
|
130 | - * @since 5.0.0 |
|
131 | - */ |
|
132 | - public static function getStorage( $app ) { |
|
133 | - return \OC_App::getStorage( $app ); |
|
134 | - } |
|
125 | + /** |
|
126 | + * Gets the Storage for an app - creates the needed folder if they are not |
|
127 | + * existant |
|
128 | + * @param string $app |
|
129 | + * @return \OC\Files\View |
|
130 | + * @since 5.0.0 |
|
131 | + */ |
|
132 | + public static function getStorage( $app ) { |
|
133 | + return \OC_App::getStorage( $app ); |
|
134 | + } |
|
135 | 135 | } |
@@ -50,8 +50,8 @@ discard block |
||
50 | 50 | * @return bool |
51 | 51 | * @since 5.0.0 |
52 | 52 | */ |
53 | - static function rmdirr( $dir ) { |
|
54 | - return \OC_Helper::rmdirr( $dir ); |
|
53 | + static function rmdirr($dir) { |
|
54 | + return \OC_Helper::rmdirr($dir); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead |
62 | 62 | * @since 5.0.0 |
63 | 63 | */ |
64 | - static function getMimeType( $path ) { |
|
64 | + static function getMimeType($path) { |
|
65 | 65 | return \OC::$server->getMimeTypeDetector()->detect($path); |
66 | 66 | } |
67 | 67 | |
@@ -71,8 +71,8 @@ discard block |
||
71 | 71 | * @return array |
72 | 72 | * @since 6.0.0 |
73 | 73 | */ |
74 | - static public function searchByMime( $mimetype ) { |
|
75 | - return(\OC\Files\Filesystem::searchByMime( $mimetype )); |
|
74 | + static public function searchByMime($mimetype) { |
|
75 | + return(\OC\Files\Filesystem::searchByMime($mimetype)); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
@@ -82,8 +82,8 @@ discard block |
||
82 | 82 | * @return int the number of bytes copied |
83 | 83 | * @since 5.0.0 |
84 | 84 | */ |
85 | - public static function streamCopy( $source, $target ) { |
|
86 | - list($count, ) = \OC_Helper::streamCopy( $source, $target ); |
|
85 | + public static function streamCopy($source, $target) { |
|
86 | + list($count,) = \OC_Helper::streamCopy($source, $target); |
|
87 | 87 | return $count; |
88 | 88 | } |
89 | 89 | |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | * @deprecated 8.1.0 use getTemporaryFile() of \OCP\ITempManager - \OC::$server->getTempManager() |
97 | 97 | * @since 5.0.0 |
98 | 98 | */ |
99 | - public static function tmpFile( $postfix='' ) { |
|
99 | + public static function tmpFile($postfix = '') { |
|
100 | 100 | return \OC::$server->getTempManager()->getTemporaryFile($postfix); |
101 | 101 | } |
102 | 102 | |
@@ -119,8 +119,8 @@ discard block |
||
119 | 119 | * @return string |
120 | 120 | * @since 5.0.0 |
121 | 121 | */ |
122 | - public static function buildNotExistingFileName( $path, $filename ) { |
|
123 | - return(\OC_Helper::buildNotExistingFileName( $path, $filename )); |
|
122 | + public static function buildNotExistingFileName($path, $filename) { |
|
123 | + return(\OC_Helper::buildNotExistingFileName($path, $filename)); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * @return \OC\Files\View |
131 | 131 | * @since 5.0.0 |
132 | 132 | */ |
133 | - public static function getStorage( $app ) { |
|
134 | - return \OC_App::getStorage( $app ); |
|
133 | + public static function getStorage($app) { |
|
134 | + return \OC_App::getStorage($app); |
|
135 | 135 | } |
136 | 136 | } |
@@ -30,7 +30,6 @@ |
||
30 | 30 | * StorageTimeoutException constructor. |
31 | 31 | * |
32 | 32 | * @param string $message |
33 | - * @param int $code |
|
34 | 33 | * @param \Exception $previous |
35 | 34 | * @since 9.0.0 |
36 | 35 | */ |
@@ -27,16 +27,16 @@ |
||
27 | 27 | */ |
28 | 28 | class StorageTimeoutException extends StorageNotAvailableException { |
29 | 29 | |
30 | - /** |
|
31 | - * StorageTimeoutException constructor. |
|
32 | - * |
|
33 | - * @param string $message |
|
34 | - * @param int $code |
|
35 | - * @param \Exception $previous |
|
36 | - * @since 9.0.0 |
|
37 | - */ |
|
38 | - public function __construct($message = '', \Exception $previous = null) { |
|
39 | - $l = \OC::$server->getL10N('core'); |
|
40 | - parent::__construct($l->t('Storage connection timeout. %s', $message), self::STATUS_TIMEOUT, $previous); |
|
41 | - } |
|
30 | + /** |
|
31 | + * StorageTimeoutException constructor. |
|
32 | + * |
|
33 | + * @param string $message |
|
34 | + * @param int $code |
|
35 | + * @param \Exception $previous |
|
36 | + * @since 9.0.0 |
|
37 | + */ |
|
38 | + public function __construct($message = '', \Exception $previous = null) { |
|
39 | + $l = \OC::$server->getL10N('core'); |
|
40 | + parent::__construct($l->t('Storage connection timeout. %s', $message), self::STATUS_TIMEOUT, $previous); |
|
41 | + } |
|
42 | 42 | } |
@@ -100,8 +100,8 @@ |
||
100 | 100 | /** |
101 | 101 | * Return the relative date in relation to today. Returns something like "last hour" or "two month ago" |
102 | 102 | * @param int $timestamp unix timestamp |
103 | - * @param boolean $dateOnly |
|
104 | - * @return \OC_L10N_String human readable interpretation of the timestamp |
|
103 | + * @param integer $dateOnly |
|
104 | + * @return string human readable interpretation of the timestamp |
|
105 | 105 | * |
106 | 106 | * @deprecated 8.0.0 Use \OCP\Template::relative_modified_date() instead |
107 | 107 | */ |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * @deprecated 8.0.0 Use \OCP\Template::image_path() instead |
51 | 51 | */ |
52 | 52 | function image_path( $app, $image ) { |
53 | - return(\image_path( $app, $image )); |
|
53 | + return(\image_path( $app, $image )); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * @deprecated 8.0.0 Use \OCP\Template::mimetype_icon() instead |
62 | 62 | */ |
63 | 63 | function mimetype_icon( $mimetype ) { |
64 | - return(\mimetype_icon( $mimetype )); |
|
64 | + return(\mimetype_icon( $mimetype )); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * @deprecated 8.0.0 Use \OCP\Template::preview_icon() instead |
72 | 72 | */ |
73 | 73 | function preview_icon( $path ) { |
74 | - return(\preview_icon( $path )); |
|
74 | + return(\preview_icon( $path )); |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * @deprecated 8.0.0 Use \OCP\Template::publicPreview_icon() instead |
84 | 84 | */ |
85 | 85 | function publicPreview_icon ( $path, $token ) { |
86 | - return(\publicPreview_icon( $path, $token )); |
|
86 | + return(\publicPreview_icon( $path, $token )); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | * @deprecated 8.0.0 Use \OCP\Template::human_file_size() instead |
95 | 95 | */ |
96 | 96 | function human_file_size( $bytes ) { |
97 | - return(\human_file_size( $bytes )); |
|
97 | + return(\human_file_size( $bytes )); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | * @deprecated 8.0.0 Use \OCP\Template::relative_modified_date() instead |
108 | 108 | */ |
109 | 109 | function relative_modified_date( $timestamp, $dateOnly = false ) { |
110 | - return(\relative_modified_date($timestamp, null, $dateOnly)); |
|
110 | + return(\relative_modified_date($timestamp, null, $dateOnly)); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | * @deprecated 8.0.0 Use \OCP\Template::human_file_size() instead |
119 | 119 | */ |
120 | 120 | function simple_file_size($bytes) { |
121 | - return(\human_file_size($bytes)); |
|
121 | + return(\human_file_size($bytes)); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | * @deprecated 8.0.0 Use \OCP\Template::html_select_options() instead |
132 | 132 | */ |
133 | 133 | function html_select_options($options, $selected, $params=array()) { |
134 | - return(\html_select_options($options, $selected, $params)); |
|
134 | + return(\html_select_options($options, $selected, $params)); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | |
@@ -142,90 +142,90 @@ discard block |
||
142 | 142 | * @since 8.0.0 |
143 | 143 | */ |
144 | 144 | class Template extends \OC_Template { |
145 | - /** |
|
146 | - * Make OC_Helper::imagePath available as a simple function |
|
147 | - * |
|
148 | - * @see \OCP\IURLGenerator::imagePath |
|
149 | - * |
|
150 | - * @param string $app |
|
151 | - * @param string $image |
|
152 | - * @return string to the image |
|
153 | - * @since 8.0.0 |
|
154 | - */ |
|
155 | - public static function image_path($app, $image) { |
|
156 | - return \image_path($app, $image); |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * Make OC_Helper::mimetypeIcon available as a simple function |
|
162 | - * |
|
163 | - * @param string $mimetype |
|
164 | - * @return string to the image of this file type. |
|
165 | - * @since 8.0.0 |
|
166 | - */ |
|
167 | - public static function mimetype_icon($mimetype) { |
|
168 | - return \mimetype_icon($mimetype); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Make preview_icon available as a simple function |
|
173 | - * |
|
174 | - * @param string $path path to file |
|
175 | - * @return string to the preview of the image |
|
176 | - * @since 8.0.0 |
|
177 | - */ |
|
178 | - public static function preview_icon($path) { |
|
179 | - return \preview_icon($path); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Make publicpreview_icon available as a simple function |
|
184 | - * Returns the path to the preview of the image. |
|
185 | - * |
|
186 | - * @param string $path of file |
|
187 | - * @param string $token |
|
188 | - * @return string link to the preview |
|
189 | - * @since 8.0.0 |
|
190 | - */ |
|
191 | - public static function publicPreview_icon($path, $token) { |
|
192 | - return \publicPreview_icon($path, $token); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Make OC_Helper::humanFileSize available as a simple function |
|
197 | - * Example: 2048 to 2 kB. |
|
198 | - * |
|
199 | - * @param int $bytes in bytes |
|
200 | - * @return string size as string |
|
201 | - * @since 8.0.0 |
|
202 | - */ |
|
203 | - public static function human_file_size($bytes) { |
|
204 | - return \human_file_size($bytes); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Return the relative date in relation to today. Returns something like "last hour" or "two month ago" |
|
209 | - * |
|
210 | - * @param int $timestamp unix timestamp |
|
211 | - * @param boolean $dateOnly |
|
212 | - * @return string human readable interpretation of the timestamp |
|
213 | - * @since 8.0.0 |
|
214 | - */ |
|
215 | - public static function relative_modified_date($timestamp, $dateOnly = false) { |
|
216 | - return \relative_modified_date($timestamp, null, $dateOnly); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Generate html code for an options block. |
|
221 | - * |
|
222 | - * @param array $options the options |
|
223 | - * @param mixed $selected which one is selected? |
|
224 | - * @param array $params the parameters |
|
225 | - * @return string html options |
|
226 | - * @since 8.0.0 |
|
227 | - */ |
|
228 | - public static function html_select_options($options, $selected, $params=array()) { |
|
229 | - return \html_select_options($options, $selected, $params); |
|
230 | - } |
|
145 | + /** |
|
146 | + * Make OC_Helper::imagePath available as a simple function |
|
147 | + * |
|
148 | + * @see \OCP\IURLGenerator::imagePath |
|
149 | + * |
|
150 | + * @param string $app |
|
151 | + * @param string $image |
|
152 | + * @return string to the image |
|
153 | + * @since 8.0.0 |
|
154 | + */ |
|
155 | + public static function image_path($app, $image) { |
|
156 | + return \image_path($app, $image); |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * Make OC_Helper::mimetypeIcon available as a simple function |
|
162 | + * |
|
163 | + * @param string $mimetype |
|
164 | + * @return string to the image of this file type. |
|
165 | + * @since 8.0.0 |
|
166 | + */ |
|
167 | + public static function mimetype_icon($mimetype) { |
|
168 | + return \mimetype_icon($mimetype); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Make preview_icon available as a simple function |
|
173 | + * |
|
174 | + * @param string $path path to file |
|
175 | + * @return string to the preview of the image |
|
176 | + * @since 8.0.0 |
|
177 | + */ |
|
178 | + public static function preview_icon($path) { |
|
179 | + return \preview_icon($path); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Make publicpreview_icon available as a simple function |
|
184 | + * Returns the path to the preview of the image. |
|
185 | + * |
|
186 | + * @param string $path of file |
|
187 | + * @param string $token |
|
188 | + * @return string link to the preview |
|
189 | + * @since 8.0.0 |
|
190 | + */ |
|
191 | + public static function publicPreview_icon($path, $token) { |
|
192 | + return \publicPreview_icon($path, $token); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Make OC_Helper::humanFileSize available as a simple function |
|
197 | + * Example: 2048 to 2 kB. |
|
198 | + * |
|
199 | + * @param int $bytes in bytes |
|
200 | + * @return string size as string |
|
201 | + * @since 8.0.0 |
|
202 | + */ |
|
203 | + public static function human_file_size($bytes) { |
|
204 | + return \human_file_size($bytes); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Return the relative date in relation to today. Returns something like "last hour" or "two month ago" |
|
209 | + * |
|
210 | + * @param int $timestamp unix timestamp |
|
211 | + * @param boolean $dateOnly |
|
212 | + * @return string human readable interpretation of the timestamp |
|
213 | + * @since 8.0.0 |
|
214 | + */ |
|
215 | + public static function relative_modified_date($timestamp, $dateOnly = false) { |
|
216 | + return \relative_modified_date($timestamp, null, $dateOnly); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Generate html code for an options block. |
|
221 | + * |
|
222 | + * @param array $options the options |
|
223 | + * @param mixed $selected which one is selected? |
|
224 | + * @param array $params the parameters |
|
225 | + * @return string html options |
|
226 | + * @since 8.0.0 |
|
227 | + */ |
|
228 | + public static function html_select_options($options, $selected, $params=array()) { |
|
229 | + return \html_select_options($options, $selected, $params); |
|
230 | + } |
|
231 | 231 | } |
@@ -49,8 +49,8 @@ discard block |
||
49 | 49 | * @see \OCP\IURLGenerator::imagePath |
50 | 50 | * @deprecated 8.0.0 Use \OCP\Template::image_path() instead |
51 | 51 | */ |
52 | -function image_path( $app, $image ) { |
|
53 | - return(\image_path( $app, $image )); |
|
52 | +function image_path($app, $image) { |
|
53 | + return(\image_path($app, $image)); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | |
@@ -60,8 +60,8 @@ discard block |
||
60 | 60 | * @return string to the image of this file type. |
61 | 61 | * @deprecated 8.0.0 Use \OCP\Template::mimetype_icon() instead |
62 | 62 | */ |
63 | -function mimetype_icon( $mimetype ) { |
|
64 | - return(\mimetype_icon( $mimetype )); |
|
63 | +function mimetype_icon($mimetype) { |
|
64 | + return(\mimetype_icon($mimetype)); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
@@ -70,8 +70,8 @@ discard block |
||
70 | 70 | * @return string to the preview of the image |
71 | 71 | * @deprecated 8.0.0 Use \OCP\Template::preview_icon() instead |
72 | 72 | */ |
73 | -function preview_icon( $path ) { |
|
74 | - return(\preview_icon( $path )); |
|
73 | +function preview_icon($path) { |
|
74 | + return(\preview_icon($path)); |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
@@ -82,8 +82,8 @@ discard block |
||
82 | 82 | * @return string link to the preview |
83 | 83 | * @deprecated 8.0.0 Use \OCP\Template::publicPreview_icon() instead |
84 | 84 | */ |
85 | -function publicPreview_icon ( $path, $token ) { |
|
86 | - return(\publicPreview_icon( $path, $token )); |
|
85 | +function publicPreview_icon($path, $token) { |
|
86 | + return(\publicPreview_icon($path, $token)); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
@@ -93,8 +93,8 @@ discard block |
||
93 | 93 | * @return string size as string |
94 | 94 | * @deprecated 8.0.0 Use \OCP\Template::human_file_size() instead |
95 | 95 | */ |
96 | -function human_file_size( $bytes ) { |
|
97 | - return(\human_file_size( $bytes )); |
|
96 | +function human_file_size($bytes) { |
|
97 | + return(\human_file_size($bytes)); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | * |
107 | 107 | * @deprecated 8.0.0 Use \OCP\Template::relative_modified_date() instead |
108 | 108 | */ |
109 | -function relative_modified_date( $timestamp, $dateOnly = false ) { |
|
109 | +function relative_modified_date($timestamp, $dateOnly = false) { |
|
110 | 110 | return(\relative_modified_date($timestamp, null, $dateOnly)); |
111 | 111 | } |
112 | 112 | |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * @return string html options |
131 | 131 | * @deprecated 8.0.0 Use \OCP\Template::html_select_options() instead |
132 | 132 | */ |
133 | -function html_select_options($options, $selected, $params=array()) { |
|
133 | +function html_select_options($options, $selected, $params = array()) { |
|
134 | 134 | return(\html_select_options($options, $selected, $params)); |
135 | 135 | } |
136 | 136 | |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | * @return string html options |
226 | 226 | * @since 8.0.0 |
227 | 227 | */ |
228 | - public static function html_select_options($options, $selected, $params=array()) { |
|
228 | + public static function html_select_options($options, $selected, $params = array()) { |
|
229 | 229 | return \html_select_options($options, $selected, $params); |
230 | 230 | } |
231 | 231 | } |
@@ -544,7 +544,7 @@ |
||
544 | 544 | * @param array $input The array to work on |
545 | 545 | * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
546 | 546 | * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
547 | - * @return array |
|
547 | + * @return string |
|
548 | 548 | * @since 4.5.0 |
549 | 549 | */ |
550 | 550 | public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
@@ -57,647 +57,647 @@ |
||
57 | 57 | * @since 4.0.0 |
58 | 58 | */ |
59 | 59 | class Util { |
60 | - // consts for Logging |
|
61 | - const DEBUG=0; |
|
62 | - const INFO=1; |
|
63 | - const WARN=2; |
|
64 | - const ERROR=3; |
|
65 | - const FATAL=4; |
|
66 | - |
|
67 | - /** |
|
68 | - * get the current installed version of ownCloud |
|
69 | - * @return array |
|
70 | - * @since 4.0.0 |
|
71 | - */ |
|
72 | - public static function getVersion() { |
|
73 | - return(\OC_Util::getVersion()); |
|
74 | - } |
|
60 | + // consts for Logging |
|
61 | + const DEBUG=0; |
|
62 | + const INFO=1; |
|
63 | + const WARN=2; |
|
64 | + const ERROR=3; |
|
65 | + const FATAL=4; |
|
66 | + |
|
67 | + /** |
|
68 | + * get the current installed version of ownCloud |
|
69 | + * @return array |
|
70 | + * @since 4.0.0 |
|
71 | + */ |
|
72 | + public static function getVersion() { |
|
73 | + return(\OC_Util::getVersion()); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Set current update channel |
|
78 | - * @param string $channel |
|
79 | - * @since 8.1.0 |
|
80 | - */ |
|
81 | - public static function setChannel($channel) { |
|
82 | - //Flush timestamp to reload version.php |
|
83 | - \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel); |
|
84 | - \OC::$server->getSession()->set('OC_Version_Timestamp', 0); |
|
85 | - } |
|
76 | + /** |
|
77 | + * Set current update channel |
|
78 | + * @param string $channel |
|
79 | + * @since 8.1.0 |
|
80 | + */ |
|
81 | + public static function setChannel($channel) { |
|
82 | + //Flush timestamp to reload version.php |
|
83 | + \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel); |
|
84 | + \OC::$server->getSession()->set('OC_Version_Timestamp', 0); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Get current update channel |
|
89 | - * @return string |
|
90 | - * @since 8.1.0 |
|
91 | - */ |
|
92 | - public static function getChannel() { |
|
93 | - return \OC_Util::getChannel(); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * send an email |
|
98 | - * @param string $toaddress |
|
99 | - * @param string $toname |
|
100 | - * @param string $subject |
|
101 | - * @param string $mailtext |
|
102 | - * @param string $fromaddress |
|
103 | - * @param string $fromname |
|
104 | - * @param int $html |
|
105 | - * @param string $altbody |
|
106 | - * @param string $ccaddress |
|
107 | - * @param string $ccname |
|
108 | - * @param string $bcc |
|
109 | - * @deprecated 8.1.0 Use \OCP\Mail\IMailer instead |
|
110 | - * @since 4.0.0 |
|
111 | - */ |
|
112 | - public static function sendMail($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname, |
|
113 | - $html = 0, $altbody = '', $ccaddress = '', $ccname = '', $bcc = '') { |
|
114 | - $mailer = \OC::$server->getMailer(); |
|
115 | - $message = $mailer->createMessage(); |
|
116 | - $message->setTo([$toaddress => $toname]); |
|
117 | - $message->setSubject($subject); |
|
118 | - $message->setPlainBody($mailtext); |
|
119 | - $message->setFrom([$fromaddress => $fromname]); |
|
120 | - if($html === 1) { |
|
121 | - $message->setHTMLBody($altbody); |
|
122 | - } |
|
123 | - |
|
124 | - if($altbody === '') { |
|
125 | - $message->setHTMLBody($mailtext); |
|
126 | - $message->setPlainBody(''); |
|
127 | - } else { |
|
128 | - $message->setHtmlBody($mailtext); |
|
129 | - $message->setPlainBody($altbody); |
|
130 | - } |
|
131 | - |
|
132 | - if(!empty($ccaddress)) { |
|
133 | - if(!empty($ccname)) { |
|
134 | - $message->setCc([$ccaddress => $ccname]); |
|
135 | - } else { |
|
136 | - $message->setCc([$ccaddress]); |
|
137 | - } |
|
138 | - } |
|
139 | - if(!empty($bcc)) { |
|
140 | - $message->setBcc([$bcc]); |
|
141 | - } |
|
142 | - |
|
143 | - $mailer->send($message); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * write a message in the log |
|
148 | - * @param string $app |
|
149 | - * @param string $message |
|
150 | - * @param int $level |
|
151 | - * @since 4.0.0 |
|
152 | - */ |
|
153 | - public static function writeLog( $app, $message, $level ) { |
|
154 | - $context = ['app' => $app]; |
|
155 | - \OC::$server->getLogger()->log($level, $message, $context); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * write exception into the log |
|
160 | - * @param string $app app name |
|
161 | - * @param \Exception $ex exception to log |
|
162 | - * @param int $level log level, defaults to \OCP\Util::FATAL |
|
163 | - * @since ....0.0 - parameter $level was added in 7.0.0 |
|
164 | - * @deprecated 8.2.0 use logException of \OCP\ILogger |
|
165 | - */ |
|
166 | - public static function logException( $app, \Exception $ex, $level = \OCP\Util::FATAL ) { |
|
167 | - \OC::$server->getLogger()->logException($ex, ['app' => $app]); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * check if sharing is disabled for the current user |
|
172 | - * |
|
173 | - * @return boolean |
|
174 | - * @since 7.0.0 |
|
175 | - */ |
|
176 | - public static function isSharingDisabledForUser() { |
|
177 | - return \OC_Util::isSharingDisabledForUser( |
|
178 | - \OC::$server->getConfig(), |
|
179 | - \OC::$server->getGroupManager(), |
|
180 | - \OC::$server->getUserSession()->getUser() |
|
181 | - ); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * get l10n object |
|
186 | - * @param string $application |
|
187 | - * @param string|null $language |
|
188 | - * @return \OC_L10N |
|
189 | - * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
190 | - */ |
|
191 | - public static function getL10N($application, $language = null) { |
|
192 | - return \OC::$server->getL10N($application, $language); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * add a css file |
|
197 | - * @param string $application |
|
198 | - * @param string $file |
|
199 | - * @since 4.0.0 |
|
200 | - */ |
|
201 | - public static function addStyle( $application, $file = null ) { |
|
202 | - \OC_Util::addStyle( $application, $file ); |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * add a javascript file |
|
207 | - * @param string $application |
|
208 | - * @param string $file |
|
209 | - * @since 4.0.0 |
|
210 | - */ |
|
211 | - public static function addScript( $application, $file = null ) { |
|
212 | - \OC_Util::addScript( $application, $file ); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Add a translation JS file |
|
217 | - * @param string $application application id |
|
218 | - * @param string $languageCode language code, defaults to the current locale |
|
219 | - * @since 8.0.0 |
|
220 | - */ |
|
221 | - public static function addTranslations($application, $languageCode = null) { |
|
222 | - \OC_Util::addTranslations($application, $languageCode); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Add a custom element to the header |
|
227 | - * If $text is null then the element will be written as empty element. |
|
228 | - * So use "" to get a closing tag. |
|
229 | - * @param string $tag tag name of the element |
|
230 | - * @param array $attributes array of attributes for the element |
|
231 | - * @param string $text the text content for the element |
|
232 | - * @since 4.0.0 |
|
233 | - */ |
|
234 | - public static function addHeader($tag, $attributes, $text=null) { |
|
235 | - \OC_Util::addHeader($tag, $attributes, $text); |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * formats a timestamp in the "right" way |
|
240 | - * @param int $timestamp $timestamp |
|
241 | - * @param bool $dateOnly option to omit time from the result |
|
242 | - * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to |
|
243 | - * @return string timestamp |
|
244 | - * |
|
245 | - * @deprecated 8.0.0 Use \OC::$server->query('DateTimeFormatter') instead |
|
246 | - * @since 4.0.0 |
|
247 | - */ |
|
248 | - public static function formatDate($timestamp, $dateOnly=false, $timeZone = null) { |
|
249 | - return(\OC_Util::formatDate($timestamp, $dateOnly, $timeZone)); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * check if some encrypted files are stored |
|
254 | - * @return bool |
|
255 | - * |
|
256 | - * @deprecated 8.1.0 No longer required |
|
257 | - * @since 6.0.0 |
|
258 | - */ |
|
259 | - public static function encryptedFiles() { |
|
260 | - return false; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Creates an absolute url to the given app and file. |
|
265 | - * @param string $app app |
|
266 | - * @param string $file file |
|
267 | - * @param array $args array with param=>value, will be appended to the returned url |
|
268 | - * The value of $args will be urlencoded |
|
269 | - * @return string the url |
|
270 | - * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
271 | - */ |
|
272 | - public static function linkToAbsolute( $app, $file, $args = array() ) { |
|
273 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
274 | - return $urlGenerator->getAbsoluteURL( |
|
275 | - $urlGenerator->linkTo($app, $file, $args) |
|
276 | - ); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Creates an absolute url for remote use. |
|
281 | - * @param string $service id |
|
282 | - * @return string the url |
|
283 | - * @since 4.0.0 |
|
284 | - */ |
|
285 | - public static function linkToRemote( $service ) { |
|
286 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
287 | - $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
288 | - return $urlGenerator->getAbsoluteURL( |
|
289 | - $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
290 | - ); |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * Creates an absolute url for public use |
|
295 | - * @param string $service id |
|
296 | - * @return string the url |
|
297 | - * @since 4.5.0 |
|
298 | - */ |
|
299 | - public static function linkToPublic($service) { |
|
300 | - return \OC_Helper::linkToPublic($service); |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Creates an url using a defined route |
|
305 | - * @param string $route |
|
306 | - * @param array $parameters |
|
307 | - * @internal param array $args with param=>value, will be appended to the returned url |
|
308 | - * @return string the url |
|
309 | - * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkToRoute($route, $parameters) |
|
310 | - * @since 5.0.0 |
|
311 | - */ |
|
312 | - public static function linkToRoute( $route, $parameters = array() ) { |
|
313 | - return \OC::$server->getURLGenerator()->linkToRoute($route, $parameters); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Creates an url to the given app and file |
|
318 | - * @param string $app app |
|
319 | - * @param string $file file |
|
320 | - * @param array $args array with param=>value, will be appended to the returned url |
|
321 | - * The value of $args will be urlencoded |
|
322 | - * @return string the url |
|
323 | - * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkTo($app, $file, $args) |
|
324 | - * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
325 | - */ |
|
326 | - public static function linkTo( $app, $file, $args = array() ) { |
|
327 | - return \OC::$server->getURLGenerator()->linkTo($app, $file, $args); |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Returns the server host, even if the website uses one or more reverse proxy |
|
332 | - * @return string the server host |
|
333 | - * @deprecated 8.1.0 Use \OCP\IRequest::getServerHost |
|
334 | - * @since 4.0.0 |
|
335 | - */ |
|
336 | - public static function getServerHost() { |
|
337 | - return \OC::$server->getRequest()->getServerHost(); |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Returns the server host name without an eventual port number |
|
342 | - * @return string the server hostname |
|
343 | - * @since 5.0.0 |
|
344 | - */ |
|
345 | - public static function getServerHostName() { |
|
346 | - $host_name = self::getServerHost(); |
|
347 | - // strip away port number (if existing) |
|
348 | - $colon_pos = strpos($host_name, ':'); |
|
349 | - if ($colon_pos != FALSE) { |
|
350 | - $host_name = substr($host_name, 0, $colon_pos); |
|
351 | - } |
|
352 | - return $host_name; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Returns the default email address |
|
357 | - * @param string $user_part the user part of the address |
|
358 | - * @return string the default email address |
|
359 | - * |
|
360 | - * Assembles a default email address (using the server hostname |
|
361 | - * and the given user part, and returns it |
|
362 | - * Example: when given lostpassword-noreply as $user_part param, |
|
363 | - * and is currently accessed via http(s)://example.com/, |
|
364 | - * it would return '[email protected]' |
|
365 | - * |
|
366 | - * If the configuration value 'mail_from_address' is set in |
|
367 | - * config.php, this value will override the $user_part that |
|
368 | - * is passed to this function |
|
369 | - * @since 5.0.0 |
|
370 | - */ |
|
371 | - public static function getDefaultEmailAddress($user_part) { |
|
372 | - $config = \OC::$server->getConfig(); |
|
373 | - $user_part = $config->getSystemValue('mail_from_address', $user_part); |
|
374 | - $host_name = self::getServerHostName(); |
|
375 | - $host_name = $config->getSystemValue('mail_domain', $host_name); |
|
376 | - $defaultEmailAddress = $user_part.'@'.$host_name; |
|
377 | - |
|
378 | - $mailer = \OC::$server->getMailer(); |
|
379 | - if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
380 | - return $defaultEmailAddress; |
|
381 | - } |
|
382 | - |
|
383 | - // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
384 | - return $user_part.'@localhost.localdomain'; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Returns the server protocol. It respects reverse proxy servers and load balancers |
|
389 | - * @return string the server protocol |
|
390 | - * @deprecated 8.1.0 Use \OCP\IRequest::getServerProtocol |
|
391 | - * @since 4.5.0 |
|
392 | - */ |
|
393 | - public static function getServerProtocol() { |
|
394 | - return \OC::$server->getRequest()->getServerProtocol(); |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * Returns the request uri, even if the website uses one or more reverse proxies |
|
399 | - * @return string the request uri |
|
400 | - * @deprecated 8.1.0 Use \OCP\IRequest::getRequestUri |
|
401 | - * @since 5.0.0 |
|
402 | - */ |
|
403 | - public static function getRequestUri() { |
|
404 | - return \OC::$server->getRequest()->getRequestUri(); |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * Returns the script name, even if the website uses one or more reverse proxies |
|
409 | - * @return string the script name |
|
410 | - * @deprecated 8.1.0 Use \OCP\IRequest::getScriptName |
|
411 | - * @since 5.0.0 |
|
412 | - */ |
|
413 | - public static function getScriptName() { |
|
414 | - return \OC::$server->getRequest()->getScriptName(); |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Creates path to an image |
|
419 | - * @param string $app app |
|
420 | - * @param string $image image name |
|
421 | - * @return string the url |
|
422 | - * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->imagePath($app, $image) |
|
423 | - * @since 4.0.0 |
|
424 | - */ |
|
425 | - public static function imagePath( $app, $image ) { |
|
426 | - return \OC::$server->getURLGenerator()->imagePath($app, $image); |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Make a human file size (2048 to 2 kB) |
|
431 | - * @param int $bytes file size in bytes |
|
432 | - * @return string a human readable file size |
|
433 | - * @since 4.0.0 |
|
434 | - */ |
|
435 | - public static function humanFileSize( $bytes ) { |
|
436 | - return(\OC_Helper::humanFileSize( $bytes )); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Make a computer file size (2 kB to 2048) |
|
441 | - * @param string $str file size in a fancy format |
|
442 | - * @return int a file size in bytes |
|
443 | - * |
|
444 | - * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
445 | - * @since 4.0.0 |
|
446 | - */ |
|
447 | - public static function computerFileSize( $str ) { |
|
448 | - return(\OC_Helper::computerFileSize( $str )); |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * connects a function to a hook |
|
453 | - * |
|
454 | - * @param string $signalClass class name of emitter |
|
455 | - * @param string $signalName name of signal |
|
456 | - * @param string|object $slotClass class name of slot |
|
457 | - * @param string $slotName name of slot |
|
458 | - * @return bool |
|
459 | - * |
|
460 | - * This function makes it very easy to connect to use hooks. |
|
461 | - * |
|
462 | - * TODO: write example |
|
463 | - * @since 4.0.0 |
|
464 | - */ |
|
465 | - static public function connectHook($signalClass, $signalName, $slotClass, $slotName ) { |
|
466 | - return(\OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName )); |
|
467 | - } |
|
468 | - |
|
469 | - /** |
|
470 | - * Emits a signal. To get data from the slot use references! |
|
471 | - * @param string $signalclass class name of emitter |
|
472 | - * @param string $signalname name of signal |
|
473 | - * @param array $params default: array() array with additional data |
|
474 | - * @return bool true if slots exists or false if not |
|
475 | - * |
|
476 | - * TODO: write example |
|
477 | - * @since 4.0.0 |
|
478 | - */ |
|
479 | - static public function emitHook( $signalclass, $signalname, $params = array()) { |
|
480 | - return(\OC_Hook::emit( $signalclass, $signalname, $params )); |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
485 | - * multiple OC_Template elements which invoke `callRegister`. If the value |
|
486 | - * would not be cached these unit-tests would fail. |
|
487 | - * @var string |
|
488 | - */ |
|
489 | - private static $token = ''; |
|
490 | - |
|
491 | - /** |
|
492 | - * Register an get/post call. This is important to prevent CSRF attacks |
|
493 | - * @since 4.5.0 |
|
494 | - */ |
|
495 | - public static function callRegister() { |
|
496 | - if(self::$token === '') { |
|
497 | - self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
|
498 | - } |
|
499 | - return self::$token; |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * Check an ajax get/post call if the request token is valid. exit if not. |
|
504 | - * @since 4.5.0 |
|
505 | - * @deprecated 9.0.0 Use annotations based on the app framework. |
|
506 | - */ |
|
507 | - public static function callCheck() { |
|
508 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
509 | - header('Location: '.\OC::$WEBROOT); |
|
510 | - exit(); |
|
511 | - } |
|
512 | - |
|
513 | - if (!(\OC::$server->getRequest()->passesCSRFCheck())) { |
|
514 | - exit(); |
|
515 | - } |
|
516 | - } |
|
517 | - |
|
518 | - /** |
|
519 | - * Used to sanitize HTML |
|
520 | - * |
|
521 | - * This function is used to sanitize HTML and should be applied on any |
|
522 | - * string or array of strings before displaying it on a web page. |
|
523 | - * |
|
524 | - * @param string|array $value |
|
525 | - * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
526 | - * @since 4.5.0 |
|
527 | - */ |
|
528 | - public static function sanitizeHTML($value) { |
|
529 | - return \OC_Util::sanitizeHTML($value); |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * Public function to encode url parameters |
|
534 | - * |
|
535 | - * This function is used to encode path to file before output. |
|
536 | - * Encoding is done according to RFC 3986 with one exception: |
|
537 | - * Character '/' is preserved as is. |
|
538 | - * |
|
539 | - * @param string $component part of URI to encode |
|
540 | - * @return string |
|
541 | - * @since 6.0.0 |
|
542 | - */ |
|
543 | - public static function encodePath($component) { |
|
544 | - return(\OC_Util::encodePath($component)); |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
549 | - * |
|
550 | - * @param array $input The array to work on |
|
551 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
552 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
553 | - * @return array |
|
554 | - * @since 4.5.0 |
|
555 | - */ |
|
556 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
557 | - return(\OC_Helper::mb_array_change_key_case($input, $case, $encoding)); |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement. |
|
562 | - * |
|
563 | - * @param string $string The input string. Opposite to the PHP build-in function does not accept an array. |
|
564 | - * @param string $replacement The replacement string. |
|
565 | - * @param int $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string. |
|
566 | - * @param int $length Length of the part to be replaced |
|
567 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
568 | - * @return string |
|
569 | - * @since 4.5.0 |
|
570 | - * @deprecated 8.2.0 Use substr_replace() instead. |
|
571 | - */ |
|
572 | - public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') { |
|
573 | - return substr_replace($string, $replacement, $start, $length); |
|
574 | - } |
|
575 | - |
|
576 | - /** |
|
577 | - * Replace all occurrences of the search string with the replacement string |
|
578 | - * |
|
579 | - * @param string $search The value being searched for, otherwise known as the needle. String. |
|
580 | - * @param string $replace The replacement string. |
|
581 | - * @param string $subject The string or array being searched and replaced on, otherwise known as the haystack. |
|
582 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
583 | - * @param int $count If passed, this will be set to the number of replacements performed. |
|
584 | - * @return string |
|
585 | - * @since 4.5.0 |
|
586 | - * @deprecated 8.2.0 Use str_replace() instead. |
|
587 | - */ |
|
588 | - public static function mb_str_replace($search, $replace, $subject, $encoding = 'UTF-8', &$count = null) { |
|
589 | - return str_replace($search, $replace, $subject, $count); |
|
590 | - } |
|
591 | - |
|
592 | - /** |
|
593 | - * performs a search in a nested array |
|
594 | - * |
|
595 | - * @param array $haystack the array to be searched |
|
596 | - * @param string $needle the search string |
|
597 | - * @param int $index optional, only search this key name |
|
598 | - * @return mixed the key of the matching field, otherwise false |
|
599 | - * @since 4.5.0 |
|
600 | - */ |
|
601 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
602 | - return(\OC_Helper::recursiveArraySearch($haystack, $needle, $index)); |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
607 | - * |
|
608 | - * @param string $dir the current folder where the user currently operates |
|
609 | - * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
610 | - * @return int number of bytes representing |
|
611 | - * @since 5.0.0 |
|
612 | - */ |
|
613 | - public static function maxUploadFilesize($dir, $free = null) { |
|
614 | - return \OC_Helper::maxUploadFilesize($dir, $free); |
|
615 | - } |
|
616 | - |
|
617 | - /** |
|
618 | - * Calculate free space left within user quota |
|
619 | - * @param string $dir the current folder where the user currently operates |
|
620 | - * @return int number of bytes representing |
|
621 | - * @since 7.0.0 |
|
622 | - */ |
|
623 | - public static function freeSpace($dir) { |
|
624 | - return \OC_Helper::freeSpace($dir); |
|
625 | - } |
|
626 | - |
|
627 | - /** |
|
628 | - * Calculate PHP upload limit |
|
629 | - * |
|
630 | - * @return int number of bytes representing |
|
631 | - * @since 7.0.0 |
|
632 | - */ |
|
633 | - public static function uploadLimit() { |
|
634 | - return \OC_Helper::uploadLimit(); |
|
635 | - } |
|
636 | - |
|
637 | - /** |
|
638 | - * Returns whether the given file name is valid |
|
639 | - * @param string $file file name to check |
|
640 | - * @return bool true if the file name is valid, false otherwise |
|
641 | - * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
|
642 | - * @since 7.0.0 |
|
643 | - */ |
|
644 | - public static function isValidFileName($file) { |
|
645 | - return \OC_Util::isValidFileName($file); |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * Generates a cryptographic secure pseudo-random string |
|
650 | - * @param int $length of the random string |
|
651 | - * @return string |
|
652 | - * @deprecated 8.0.0 Use \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate($length); instead |
|
653 | - * @since 7.0.0 |
|
654 | - */ |
|
655 | - public static function generateRandomBytes($length = 30) { |
|
656 | - return \OC::$server->getSecureRandom()->generate($length, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
657 | - } |
|
658 | - |
|
659 | - /** |
|
660 | - * Compare two strings to provide a natural sort |
|
661 | - * @param string $a first string to compare |
|
662 | - * @param string $b second string to compare |
|
663 | - * @return -1 if $b comes before $a, 1 if $a comes before $b |
|
664 | - * or 0 if the strings are identical |
|
665 | - * @since 7.0.0 |
|
666 | - */ |
|
667 | - public static function naturalSortCompare($a, $b) { |
|
668 | - return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
669 | - } |
|
670 | - |
|
671 | - /** |
|
672 | - * check if a password is required for each public link |
|
673 | - * @return boolean |
|
674 | - * @since 7.0.0 |
|
675 | - */ |
|
676 | - public static function isPublicLinkPasswordRequired() { |
|
677 | - return \OC_Util::isPublicLinkPasswordRequired(); |
|
678 | - } |
|
679 | - |
|
680 | - /** |
|
681 | - * check if share API enforces a default expire date |
|
682 | - * @return boolean |
|
683 | - * @since 8.0.0 |
|
684 | - */ |
|
685 | - public static function isDefaultExpireDateEnforced() { |
|
686 | - return \OC_Util::isDefaultExpireDateEnforced(); |
|
687 | - } |
|
688 | - |
|
689 | - protected static $needUpgradeCache = null; |
|
690 | - |
|
691 | - /** |
|
692 | - * Checks whether the current version needs upgrade. |
|
693 | - * |
|
694 | - * @return bool true if upgrade is needed, false otherwise |
|
695 | - * @since 7.0.0 |
|
696 | - */ |
|
697 | - public static function needUpgrade() { |
|
698 | - if (!isset(self::$needUpgradeCache)) { |
|
699 | - self::$needUpgradeCache=\OC_Util::needUpgrade(\OC::$server->getConfig()); |
|
700 | - } |
|
701 | - return self::$needUpgradeCache; |
|
702 | - } |
|
87 | + /** |
|
88 | + * Get current update channel |
|
89 | + * @return string |
|
90 | + * @since 8.1.0 |
|
91 | + */ |
|
92 | + public static function getChannel() { |
|
93 | + return \OC_Util::getChannel(); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * send an email |
|
98 | + * @param string $toaddress |
|
99 | + * @param string $toname |
|
100 | + * @param string $subject |
|
101 | + * @param string $mailtext |
|
102 | + * @param string $fromaddress |
|
103 | + * @param string $fromname |
|
104 | + * @param int $html |
|
105 | + * @param string $altbody |
|
106 | + * @param string $ccaddress |
|
107 | + * @param string $ccname |
|
108 | + * @param string $bcc |
|
109 | + * @deprecated 8.1.0 Use \OCP\Mail\IMailer instead |
|
110 | + * @since 4.0.0 |
|
111 | + */ |
|
112 | + public static function sendMail($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname, |
|
113 | + $html = 0, $altbody = '', $ccaddress = '', $ccname = '', $bcc = '') { |
|
114 | + $mailer = \OC::$server->getMailer(); |
|
115 | + $message = $mailer->createMessage(); |
|
116 | + $message->setTo([$toaddress => $toname]); |
|
117 | + $message->setSubject($subject); |
|
118 | + $message->setPlainBody($mailtext); |
|
119 | + $message->setFrom([$fromaddress => $fromname]); |
|
120 | + if($html === 1) { |
|
121 | + $message->setHTMLBody($altbody); |
|
122 | + } |
|
123 | + |
|
124 | + if($altbody === '') { |
|
125 | + $message->setHTMLBody($mailtext); |
|
126 | + $message->setPlainBody(''); |
|
127 | + } else { |
|
128 | + $message->setHtmlBody($mailtext); |
|
129 | + $message->setPlainBody($altbody); |
|
130 | + } |
|
131 | + |
|
132 | + if(!empty($ccaddress)) { |
|
133 | + if(!empty($ccname)) { |
|
134 | + $message->setCc([$ccaddress => $ccname]); |
|
135 | + } else { |
|
136 | + $message->setCc([$ccaddress]); |
|
137 | + } |
|
138 | + } |
|
139 | + if(!empty($bcc)) { |
|
140 | + $message->setBcc([$bcc]); |
|
141 | + } |
|
142 | + |
|
143 | + $mailer->send($message); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * write a message in the log |
|
148 | + * @param string $app |
|
149 | + * @param string $message |
|
150 | + * @param int $level |
|
151 | + * @since 4.0.0 |
|
152 | + */ |
|
153 | + public static function writeLog( $app, $message, $level ) { |
|
154 | + $context = ['app' => $app]; |
|
155 | + \OC::$server->getLogger()->log($level, $message, $context); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * write exception into the log |
|
160 | + * @param string $app app name |
|
161 | + * @param \Exception $ex exception to log |
|
162 | + * @param int $level log level, defaults to \OCP\Util::FATAL |
|
163 | + * @since ....0.0 - parameter $level was added in 7.0.0 |
|
164 | + * @deprecated 8.2.0 use logException of \OCP\ILogger |
|
165 | + */ |
|
166 | + public static function logException( $app, \Exception $ex, $level = \OCP\Util::FATAL ) { |
|
167 | + \OC::$server->getLogger()->logException($ex, ['app' => $app]); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * check if sharing is disabled for the current user |
|
172 | + * |
|
173 | + * @return boolean |
|
174 | + * @since 7.0.0 |
|
175 | + */ |
|
176 | + public static function isSharingDisabledForUser() { |
|
177 | + return \OC_Util::isSharingDisabledForUser( |
|
178 | + \OC::$server->getConfig(), |
|
179 | + \OC::$server->getGroupManager(), |
|
180 | + \OC::$server->getUserSession()->getUser() |
|
181 | + ); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * get l10n object |
|
186 | + * @param string $application |
|
187 | + * @param string|null $language |
|
188 | + * @return \OC_L10N |
|
189 | + * @since 6.0.0 - parameter $language was added in 8.0.0 |
|
190 | + */ |
|
191 | + public static function getL10N($application, $language = null) { |
|
192 | + return \OC::$server->getL10N($application, $language); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * add a css file |
|
197 | + * @param string $application |
|
198 | + * @param string $file |
|
199 | + * @since 4.0.0 |
|
200 | + */ |
|
201 | + public static function addStyle( $application, $file = null ) { |
|
202 | + \OC_Util::addStyle( $application, $file ); |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * add a javascript file |
|
207 | + * @param string $application |
|
208 | + * @param string $file |
|
209 | + * @since 4.0.0 |
|
210 | + */ |
|
211 | + public static function addScript( $application, $file = null ) { |
|
212 | + \OC_Util::addScript( $application, $file ); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Add a translation JS file |
|
217 | + * @param string $application application id |
|
218 | + * @param string $languageCode language code, defaults to the current locale |
|
219 | + * @since 8.0.0 |
|
220 | + */ |
|
221 | + public static function addTranslations($application, $languageCode = null) { |
|
222 | + \OC_Util::addTranslations($application, $languageCode); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Add a custom element to the header |
|
227 | + * If $text is null then the element will be written as empty element. |
|
228 | + * So use "" to get a closing tag. |
|
229 | + * @param string $tag tag name of the element |
|
230 | + * @param array $attributes array of attributes for the element |
|
231 | + * @param string $text the text content for the element |
|
232 | + * @since 4.0.0 |
|
233 | + */ |
|
234 | + public static function addHeader($tag, $attributes, $text=null) { |
|
235 | + \OC_Util::addHeader($tag, $attributes, $text); |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * formats a timestamp in the "right" way |
|
240 | + * @param int $timestamp $timestamp |
|
241 | + * @param bool $dateOnly option to omit time from the result |
|
242 | + * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to |
|
243 | + * @return string timestamp |
|
244 | + * |
|
245 | + * @deprecated 8.0.0 Use \OC::$server->query('DateTimeFormatter') instead |
|
246 | + * @since 4.0.0 |
|
247 | + */ |
|
248 | + public static function formatDate($timestamp, $dateOnly=false, $timeZone = null) { |
|
249 | + return(\OC_Util::formatDate($timestamp, $dateOnly, $timeZone)); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * check if some encrypted files are stored |
|
254 | + * @return bool |
|
255 | + * |
|
256 | + * @deprecated 8.1.0 No longer required |
|
257 | + * @since 6.0.0 |
|
258 | + */ |
|
259 | + public static function encryptedFiles() { |
|
260 | + return false; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Creates an absolute url to the given app and file. |
|
265 | + * @param string $app app |
|
266 | + * @param string $file file |
|
267 | + * @param array $args array with param=>value, will be appended to the returned url |
|
268 | + * The value of $args will be urlencoded |
|
269 | + * @return string the url |
|
270 | + * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
271 | + */ |
|
272 | + public static function linkToAbsolute( $app, $file, $args = array() ) { |
|
273 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
274 | + return $urlGenerator->getAbsoluteURL( |
|
275 | + $urlGenerator->linkTo($app, $file, $args) |
|
276 | + ); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Creates an absolute url for remote use. |
|
281 | + * @param string $service id |
|
282 | + * @return string the url |
|
283 | + * @since 4.0.0 |
|
284 | + */ |
|
285 | + public static function linkToRemote( $service ) { |
|
286 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
287 | + $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
288 | + return $urlGenerator->getAbsoluteURL( |
|
289 | + $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
290 | + ); |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * Creates an absolute url for public use |
|
295 | + * @param string $service id |
|
296 | + * @return string the url |
|
297 | + * @since 4.5.0 |
|
298 | + */ |
|
299 | + public static function linkToPublic($service) { |
|
300 | + return \OC_Helper::linkToPublic($service); |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Creates an url using a defined route |
|
305 | + * @param string $route |
|
306 | + * @param array $parameters |
|
307 | + * @internal param array $args with param=>value, will be appended to the returned url |
|
308 | + * @return string the url |
|
309 | + * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkToRoute($route, $parameters) |
|
310 | + * @since 5.0.0 |
|
311 | + */ |
|
312 | + public static function linkToRoute( $route, $parameters = array() ) { |
|
313 | + return \OC::$server->getURLGenerator()->linkToRoute($route, $parameters); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Creates an url to the given app and file |
|
318 | + * @param string $app app |
|
319 | + * @param string $file file |
|
320 | + * @param array $args array with param=>value, will be appended to the returned url |
|
321 | + * The value of $args will be urlencoded |
|
322 | + * @return string the url |
|
323 | + * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkTo($app, $file, $args) |
|
324 | + * @since 4.0.0 - parameter $args was added in 4.5.0 |
|
325 | + */ |
|
326 | + public static function linkTo( $app, $file, $args = array() ) { |
|
327 | + return \OC::$server->getURLGenerator()->linkTo($app, $file, $args); |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Returns the server host, even if the website uses one or more reverse proxy |
|
332 | + * @return string the server host |
|
333 | + * @deprecated 8.1.0 Use \OCP\IRequest::getServerHost |
|
334 | + * @since 4.0.0 |
|
335 | + */ |
|
336 | + public static function getServerHost() { |
|
337 | + return \OC::$server->getRequest()->getServerHost(); |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Returns the server host name without an eventual port number |
|
342 | + * @return string the server hostname |
|
343 | + * @since 5.0.0 |
|
344 | + */ |
|
345 | + public static function getServerHostName() { |
|
346 | + $host_name = self::getServerHost(); |
|
347 | + // strip away port number (if existing) |
|
348 | + $colon_pos = strpos($host_name, ':'); |
|
349 | + if ($colon_pos != FALSE) { |
|
350 | + $host_name = substr($host_name, 0, $colon_pos); |
|
351 | + } |
|
352 | + return $host_name; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Returns the default email address |
|
357 | + * @param string $user_part the user part of the address |
|
358 | + * @return string the default email address |
|
359 | + * |
|
360 | + * Assembles a default email address (using the server hostname |
|
361 | + * and the given user part, and returns it |
|
362 | + * Example: when given lostpassword-noreply as $user_part param, |
|
363 | + * and is currently accessed via http(s)://example.com/, |
|
364 | + * it would return '[email protected]' |
|
365 | + * |
|
366 | + * If the configuration value 'mail_from_address' is set in |
|
367 | + * config.php, this value will override the $user_part that |
|
368 | + * is passed to this function |
|
369 | + * @since 5.0.0 |
|
370 | + */ |
|
371 | + public static function getDefaultEmailAddress($user_part) { |
|
372 | + $config = \OC::$server->getConfig(); |
|
373 | + $user_part = $config->getSystemValue('mail_from_address', $user_part); |
|
374 | + $host_name = self::getServerHostName(); |
|
375 | + $host_name = $config->getSystemValue('mail_domain', $host_name); |
|
376 | + $defaultEmailAddress = $user_part.'@'.$host_name; |
|
377 | + |
|
378 | + $mailer = \OC::$server->getMailer(); |
|
379 | + if ($mailer->validateMailAddress($defaultEmailAddress)) { |
|
380 | + return $defaultEmailAddress; |
|
381 | + } |
|
382 | + |
|
383 | + // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
|
384 | + return $user_part.'@localhost.localdomain'; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Returns the server protocol. It respects reverse proxy servers and load balancers |
|
389 | + * @return string the server protocol |
|
390 | + * @deprecated 8.1.0 Use \OCP\IRequest::getServerProtocol |
|
391 | + * @since 4.5.0 |
|
392 | + */ |
|
393 | + public static function getServerProtocol() { |
|
394 | + return \OC::$server->getRequest()->getServerProtocol(); |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * Returns the request uri, even if the website uses one or more reverse proxies |
|
399 | + * @return string the request uri |
|
400 | + * @deprecated 8.1.0 Use \OCP\IRequest::getRequestUri |
|
401 | + * @since 5.0.0 |
|
402 | + */ |
|
403 | + public static function getRequestUri() { |
|
404 | + return \OC::$server->getRequest()->getRequestUri(); |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * Returns the script name, even if the website uses one or more reverse proxies |
|
409 | + * @return string the script name |
|
410 | + * @deprecated 8.1.0 Use \OCP\IRequest::getScriptName |
|
411 | + * @since 5.0.0 |
|
412 | + */ |
|
413 | + public static function getScriptName() { |
|
414 | + return \OC::$server->getRequest()->getScriptName(); |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Creates path to an image |
|
419 | + * @param string $app app |
|
420 | + * @param string $image image name |
|
421 | + * @return string the url |
|
422 | + * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->imagePath($app, $image) |
|
423 | + * @since 4.0.0 |
|
424 | + */ |
|
425 | + public static function imagePath( $app, $image ) { |
|
426 | + return \OC::$server->getURLGenerator()->imagePath($app, $image); |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Make a human file size (2048 to 2 kB) |
|
431 | + * @param int $bytes file size in bytes |
|
432 | + * @return string a human readable file size |
|
433 | + * @since 4.0.0 |
|
434 | + */ |
|
435 | + public static function humanFileSize( $bytes ) { |
|
436 | + return(\OC_Helper::humanFileSize( $bytes )); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Make a computer file size (2 kB to 2048) |
|
441 | + * @param string $str file size in a fancy format |
|
442 | + * @return int a file size in bytes |
|
443 | + * |
|
444 | + * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
445 | + * @since 4.0.0 |
|
446 | + */ |
|
447 | + public static function computerFileSize( $str ) { |
|
448 | + return(\OC_Helper::computerFileSize( $str )); |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * connects a function to a hook |
|
453 | + * |
|
454 | + * @param string $signalClass class name of emitter |
|
455 | + * @param string $signalName name of signal |
|
456 | + * @param string|object $slotClass class name of slot |
|
457 | + * @param string $slotName name of slot |
|
458 | + * @return bool |
|
459 | + * |
|
460 | + * This function makes it very easy to connect to use hooks. |
|
461 | + * |
|
462 | + * TODO: write example |
|
463 | + * @since 4.0.0 |
|
464 | + */ |
|
465 | + static public function connectHook($signalClass, $signalName, $slotClass, $slotName ) { |
|
466 | + return(\OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName )); |
|
467 | + } |
|
468 | + |
|
469 | + /** |
|
470 | + * Emits a signal. To get data from the slot use references! |
|
471 | + * @param string $signalclass class name of emitter |
|
472 | + * @param string $signalname name of signal |
|
473 | + * @param array $params default: array() array with additional data |
|
474 | + * @return bool true if slots exists or false if not |
|
475 | + * |
|
476 | + * TODO: write example |
|
477 | + * @since 4.0.0 |
|
478 | + */ |
|
479 | + static public function emitHook( $signalclass, $signalname, $params = array()) { |
|
480 | + return(\OC_Hook::emit( $signalclass, $signalname, $params )); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
|
485 | + * multiple OC_Template elements which invoke `callRegister`. If the value |
|
486 | + * would not be cached these unit-tests would fail. |
|
487 | + * @var string |
|
488 | + */ |
|
489 | + private static $token = ''; |
|
490 | + |
|
491 | + /** |
|
492 | + * Register an get/post call. This is important to prevent CSRF attacks |
|
493 | + * @since 4.5.0 |
|
494 | + */ |
|
495 | + public static function callRegister() { |
|
496 | + if(self::$token === '') { |
|
497 | + self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
|
498 | + } |
|
499 | + return self::$token; |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * Check an ajax get/post call if the request token is valid. exit if not. |
|
504 | + * @since 4.5.0 |
|
505 | + * @deprecated 9.0.0 Use annotations based on the app framework. |
|
506 | + */ |
|
507 | + public static function callCheck() { |
|
508 | + if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
509 | + header('Location: '.\OC::$WEBROOT); |
|
510 | + exit(); |
|
511 | + } |
|
512 | + |
|
513 | + if (!(\OC::$server->getRequest()->passesCSRFCheck())) { |
|
514 | + exit(); |
|
515 | + } |
|
516 | + } |
|
517 | + |
|
518 | + /** |
|
519 | + * Used to sanitize HTML |
|
520 | + * |
|
521 | + * This function is used to sanitize HTML and should be applied on any |
|
522 | + * string or array of strings before displaying it on a web page. |
|
523 | + * |
|
524 | + * @param string|array $value |
|
525 | + * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
|
526 | + * @since 4.5.0 |
|
527 | + */ |
|
528 | + public static function sanitizeHTML($value) { |
|
529 | + return \OC_Util::sanitizeHTML($value); |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * Public function to encode url parameters |
|
534 | + * |
|
535 | + * This function is used to encode path to file before output. |
|
536 | + * Encoding is done according to RFC 3986 with one exception: |
|
537 | + * Character '/' is preserved as is. |
|
538 | + * |
|
539 | + * @param string $component part of URI to encode |
|
540 | + * @return string |
|
541 | + * @since 6.0.0 |
|
542 | + */ |
|
543 | + public static function encodePath($component) { |
|
544 | + return(\OC_Util::encodePath($component)); |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
549 | + * |
|
550 | + * @param array $input The array to work on |
|
551 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
552 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
553 | + * @return array |
|
554 | + * @since 4.5.0 |
|
555 | + */ |
|
556 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
557 | + return(\OC_Helper::mb_array_change_key_case($input, $case, $encoding)); |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement. |
|
562 | + * |
|
563 | + * @param string $string The input string. Opposite to the PHP build-in function does not accept an array. |
|
564 | + * @param string $replacement The replacement string. |
|
565 | + * @param int $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string. |
|
566 | + * @param int $length Length of the part to be replaced |
|
567 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
568 | + * @return string |
|
569 | + * @since 4.5.0 |
|
570 | + * @deprecated 8.2.0 Use substr_replace() instead. |
|
571 | + */ |
|
572 | + public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') { |
|
573 | + return substr_replace($string, $replacement, $start, $length); |
|
574 | + } |
|
575 | + |
|
576 | + /** |
|
577 | + * Replace all occurrences of the search string with the replacement string |
|
578 | + * |
|
579 | + * @param string $search The value being searched for, otherwise known as the needle. String. |
|
580 | + * @param string $replace The replacement string. |
|
581 | + * @param string $subject The string or array being searched and replaced on, otherwise known as the haystack. |
|
582 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
583 | + * @param int $count If passed, this will be set to the number of replacements performed. |
|
584 | + * @return string |
|
585 | + * @since 4.5.0 |
|
586 | + * @deprecated 8.2.0 Use str_replace() instead. |
|
587 | + */ |
|
588 | + public static function mb_str_replace($search, $replace, $subject, $encoding = 'UTF-8', &$count = null) { |
|
589 | + return str_replace($search, $replace, $subject, $count); |
|
590 | + } |
|
591 | + |
|
592 | + /** |
|
593 | + * performs a search in a nested array |
|
594 | + * |
|
595 | + * @param array $haystack the array to be searched |
|
596 | + * @param string $needle the search string |
|
597 | + * @param int $index optional, only search this key name |
|
598 | + * @return mixed the key of the matching field, otherwise false |
|
599 | + * @since 4.5.0 |
|
600 | + */ |
|
601 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
602 | + return(\OC_Helper::recursiveArraySearch($haystack, $needle, $index)); |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
607 | + * |
|
608 | + * @param string $dir the current folder where the user currently operates |
|
609 | + * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
610 | + * @return int number of bytes representing |
|
611 | + * @since 5.0.0 |
|
612 | + */ |
|
613 | + public static function maxUploadFilesize($dir, $free = null) { |
|
614 | + return \OC_Helper::maxUploadFilesize($dir, $free); |
|
615 | + } |
|
616 | + |
|
617 | + /** |
|
618 | + * Calculate free space left within user quota |
|
619 | + * @param string $dir the current folder where the user currently operates |
|
620 | + * @return int number of bytes representing |
|
621 | + * @since 7.0.0 |
|
622 | + */ |
|
623 | + public static function freeSpace($dir) { |
|
624 | + return \OC_Helper::freeSpace($dir); |
|
625 | + } |
|
626 | + |
|
627 | + /** |
|
628 | + * Calculate PHP upload limit |
|
629 | + * |
|
630 | + * @return int number of bytes representing |
|
631 | + * @since 7.0.0 |
|
632 | + */ |
|
633 | + public static function uploadLimit() { |
|
634 | + return \OC_Helper::uploadLimit(); |
|
635 | + } |
|
636 | + |
|
637 | + /** |
|
638 | + * Returns whether the given file name is valid |
|
639 | + * @param string $file file name to check |
|
640 | + * @return bool true if the file name is valid, false otherwise |
|
641 | + * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
|
642 | + * @since 7.0.0 |
|
643 | + */ |
|
644 | + public static function isValidFileName($file) { |
|
645 | + return \OC_Util::isValidFileName($file); |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * Generates a cryptographic secure pseudo-random string |
|
650 | + * @param int $length of the random string |
|
651 | + * @return string |
|
652 | + * @deprecated 8.0.0 Use \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate($length); instead |
|
653 | + * @since 7.0.0 |
|
654 | + */ |
|
655 | + public static function generateRandomBytes($length = 30) { |
|
656 | + return \OC::$server->getSecureRandom()->generate($length, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
657 | + } |
|
658 | + |
|
659 | + /** |
|
660 | + * Compare two strings to provide a natural sort |
|
661 | + * @param string $a first string to compare |
|
662 | + * @param string $b second string to compare |
|
663 | + * @return -1 if $b comes before $a, 1 if $a comes before $b |
|
664 | + * or 0 if the strings are identical |
|
665 | + * @since 7.0.0 |
|
666 | + */ |
|
667 | + public static function naturalSortCompare($a, $b) { |
|
668 | + return \OC\NaturalSort::getInstance()->compare($a, $b); |
|
669 | + } |
|
670 | + |
|
671 | + /** |
|
672 | + * check if a password is required for each public link |
|
673 | + * @return boolean |
|
674 | + * @since 7.0.0 |
|
675 | + */ |
|
676 | + public static function isPublicLinkPasswordRequired() { |
|
677 | + return \OC_Util::isPublicLinkPasswordRequired(); |
|
678 | + } |
|
679 | + |
|
680 | + /** |
|
681 | + * check if share API enforces a default expire date |
|
682 | + * @return boolean |
|
683 | + * @since 8.0.0 |
|
684 | + */ |
|
685 | + public static function isDefaultExpireDateEnforced() { |
|
686 | + return \OC_Util::isDefaultExpireDateEnforced(); |
|
687 | + } |
|
688 | + |
|
689 | + protected static $needUpgradeCache = null; |
|
690 | + |
|
691 | + /** |
|
692 | + * Checks whether the current version needs upgrade. |
|
693 | + * |
|
694 | + * @return bool true if upgrade is needed, false otherwise |
|
695 | + * @since 7.0.0 |
|
696 | + */ |
|
697 | + public static function needUpgrade() { |
|
698 | + if (!isset(self::$needUpgradeCache)) { |
|
699 | + self::$needUpgradeCache=\OC_Util::needUpgrade(\OC::$server->getConfig()); |
|
700 | + } |
|
701 | + return self::$needUpgradeCache; |
|
702 | + } |
|
703 | 703 | } |
@@ -58,11 +58,11 @@ discard block |
||
58 | 58 | */ |
59 | 59 | class Util { |
60 | 60 | // consts for Logging |
61 | - const DEBUG=0; |
|
62 | - const INFO=1; |
|
63 | - const WARN=2; |
|
64 | - const ERROR=3; |
|
65 | - const FATAL=4; |
|
61 | + const DEBUG = 0; |
|
62 | + const INFO = 1; |
|
63 | + const WARN = 2; |
|
64 | + const ERROR = 3; |
|
65 | + const FATAL = 4; |
|
66 | 66 | |
67 | 67 | /** |
68 | 68 | * get the current installed version of ownCloud |
@@ -117,11 +117,11 @@ discard block |
||
117 | 117 | $message->setSubject($subject); |
118 | 118 | $message->setPlainBody($mailtext); |
119 | 119 | $message->setFrom([$fromaddress => $fromname]); |
120 | - if($html === 1) { |
|
120 | + if ($html === 1) { |
|
121 | 121 | $message->setHTMLBody($altbody); |
122 | 122 | } |
123 | 123 | |
124 | - if($altbody === '') { |
|
124 | + if ($altbody === '') { |
|
125 | 125 | $message->setHTMLBody($mailtext); |
126 | 126 | $message->setPlainBody(''); |
127 | 127 | } else { |
@@ -129,14 +129,14 @@ discard block |
||
129 | 129 | $message->setPlainBody($altbody); |
130 | 130 | } |
131 | 131 | |
132 | - if(!empty($ccaddress)) { |
|
133 | - if(!empty($ccname)) { |
|
132 | + if (!empty($ccaddress)) { |
|
133 | + if (!empty($ccname)) { |
|
134 | 134 | $message->setCc([$ccaddress => $ccname]); |
135 | 135 | } else { |
136 | 136 | $message->setCc([$ccaddress]); |
137 | 137 | } |
138 | 138 | } |
139 | - if(!empty($bcc)) { |
|
139 | + if (!empty($bcc)) { |
|
140 | 140 | $message->setBcc([$bcc]); |
141 | 141 | } |
142 | 142 | |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | * @param int $level |
151 | 151 | * @since 4.0.0 |
152 | 152 | */ |
153 | - public static function writeLog( $app, $message, $level ) { |
|
153 | + public static function writeLog($app, $message, $level) { |
|
154 | 154 | $context = ['app' => $app]; |
155 | 155 | \OC::$server->getLogger()->log($level, $message, $context); |
156 | 156 | } |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | * @since ....0.0 - parameter $level was added in 7.0.0 |
164 | 164 | * @deprecated 8.2.0 use logException of \OCP\ILogger |
165 | 165 | */ |
166 | - public static function logException( $app, \Exception $ex, $level = \OCP\Util::FATAL ) { |
|
166 | + public static function logException($app, \Exception $ex, $level = \OCP\Util::FATAL) { |
|
167 | 167 | \OC::$server->getLogger()->logException($ex, ['app' => $app]); |
168 | 168 | } |
169 | 169 | |
@@ -198,8 +198,8 @@ discard block |
||
198 | 198 | * @param string $file |
199 | 199 | * @since 4.0.0 |
200 | 200 | */ |
201 | - public static function addStyle( $application, $file = null ) { |
|
202 | - \OC_Util::addStyle( $application, $file ); |
|
201 | + public static function addStyle($application, $file = null) { |
|
202 | + \OC_Util::addStyle($application, $file); |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | /** |
@@ -208,8 +208,8 @@ discard block |
||
208 | 208 | * @param string $file |
209 | 209 | * @since 4.0.0 |
210 | 210 | */ |
211 | - public static function addScript( $application, $file = null ) { |
|
212 | - \OC_Util::addScript( $application, $file ); |
|
211 | + public static function addScript($application, $file = null) { |
|
212 | + \OC_Util::addScript($application, $file); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | /** |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | * @param string $text the text content for the element |
232 | 232 | * @since 4.0.0 |
233 | 233 | */ |
234 | - public static function addHeader($tag, $attributes, $text=null) { |
|
234 | + public static function addHeader($tag, $attributes, $text = null) { |
|
235 | 235 | \OC_Util::addHeader($tag, $attributes, $text); |
236 | 236 | } |
237 | 237 | |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | * @deprecated 8.0.0 Use \OC::$server->query('DateTimeFormatter') instead |
246 | 246 | * @since 4.0.0 |
247 | 247 | */ |
248 | - public static function formatDate($timestamp, $dateOnly=false, $timeZone = null) { |
|
248 | + public static function formatDate($timestamp, $dateOnly = false, $timeZone = null) { |
|
249 | 249 | return(\OC_Util::formatDate($timestamp, $dateOnly, $timeZone)); |
250 | 250 | } |
251 | 251 | |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | * @return string the url |
270 | 270 | * @since 4.0.0 - parameter $args was added in 4.5.0 |
271 | 271 | */ |
272 | - public static function linkToAbsolute( $app, $file, $args = array() ) { |
|
272 | + public static function linkToAbsolute($app, $file, $args = array()) { |
|
273 | 273 | $urlGenerator = \OC::$server->getURLGenerator(); |
274 | 274 | return $urlGenerator->getAbsoluteURL( |
275 | 275 | $urlGenerator->linkTo($app, $file, $args) |
@@ -282,11 +282,11 @@ discard block |
||
282 | 282 | * @return string the url |
283 | 283 | * @since 4.0.0 |
284 | 284 | */ |
285 | - public static function linkToRemote( $service ) { |
|
285 | + public static function linkToRemote($service) { |
|
286 | 286 | $urlGenerator = \OC::$server->getURLGenerator(); |
287 | - $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; |
|
287 | + $remoteBase = $urlGenerator->linkTo('', 'remote.php').'/'.$service; |
|
288 | 288 | return $urlGenerator->getAbsoluteURL( |
289 | - $remoteBase . (($service[strlen($service) - 1] != '/') ? '/' : '') |
|
289 | + $remoteBase.(($service[strlen($service) - 1] != '/') ? '/' : '') |
|
290 | 290 | ); |
291 | 291 | } |
292 | 292 | |
@@ -309,7 +309,7 @@ discard block |
||
309 | 309 | * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkToRoute($route, $parameters) |
310 | 310 | * @since 5.0.0 |
311 | 311 | */ |
312 | - public static function linkToRoute( $route, $parameters = array() ) { |
|
312 | + public static function linkToRoute($route, $parameters = array()) { |
|
313 | 313 | return \OC::$server->getURLGenerator()->linkToRoute($route, $parameters); |
314 | 314 | } |
315 | 315 | |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->linkTo($app, $file, $args) |
324 | 324 | * @since 4.0.0 - parameter $args was added in 4.5.0 |
325 | 325 | */ |
326 | - public static function linkTo( $app, $file, $args = array() ) { |
|
326 | + public static function linkTo($app, $file, $args = array()) { |
|
327 | 327 | return \OC::$server->getURLGenerator()->linkTo($app, $file, $args); |
328 | 328 | } |
329 | 329 | |
@@ -422,7 +422,7 @@ discard block |
||
422 | 422 | * @deprecated 8.1.0 Use \OC::$server->getURLGenerator()->imagePath($app, $image) |
423 | 423 | * @since 4.0.0 |
424 | 424 | */ |
425 | - public static function imagePath( $app, $image ) { |
|
425 | + public static function imagePath($app, $image) { |
|
426 | 426 | return \OC::$server->getURLGenerator()->imagePath($app, $image); |
427 | 427 | } |
428 | 428 | |
@@ -432,8 +432,8 @@ discard block |
||
432 | 432 | * @return string a human readable file size |
433 | 433 | * @since 4.0.0 |
434 | 434 | */ |
435 | - public static function humanFileSize( $bytes ) { |
|
436 | - return(\OC_Helper::humanFileSize( $bytes )); |
|
435 | + public static function humanFileSize($bytes) { |
|
436 | + return(\OC_Helper::humanFileSize($bytes)); |
|
437 | 437 | } |
438 | 438 | |
439 | 439 | /** |
@@ -444,8 +444,8 @@ discard block |
||
444 | 444 | * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
445 | 445 | * @since 4.0.0 |
446 | 446 | */ |
447 | - public static function computerFileSize( $str ) { |
|
448 | - return(\OC_Helper::computerFileSize( $str )); |
|
447 | + public static function computerFileSize($str) { |
|
448 | + return(\OC_Helper::computerFileSize($str)); |
|
449 | 449 | } |
450 | 450 | |
451 | 451 | /** |
@@ -462,8 +462,8 @@ discard block |
||
462 | 462 | * TODO: write example |
463 | 463 | * @since 4.0.0 |
464 | 464 | */ |
465 | - static public function connectHook($signalClass, $signalName, $slotClass, $slotName ) { |
|
466 | - return(\OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName )); |
|
465 | + static public function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
|
466 | + return(\OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName)); |
|
467 | 467 | } |
468 | 468 | |
469 | 469 | /** |
@@ -476,8 +476,8 @@ discard block |
||
476 | 476 | * TODO: write example |
477 | 477 | * @since 4.0.0 |
478 | 478 | */ |
479 | - static public function emitHook( $signalclass, $signalname, $params = array()) { |
|
480 | - return(\OC_Hook::emit( $signalclass, $signalname, $params )); |
|
479 | + static public function emitHook($signalclass, $signalname, $params = array()) { |
|
480 | + return(\OC_Hook::emit($signalclass, $signalname, $params)); |
|
481 | 481 | } |
482 | 482 | |
483 | 483 | /** |
@@ -493,7 +493,7 @@ discard block |
||
493 | 493 | * @since 4.5.0 |
494 | 494 | */ |
495 | 495 | public static function callRegister() { |
496 | - if(self::$token === '') { |
|
496 | + if (self::$token === '') { |
|
497 | 497 | self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
498 | 498 | } |
499 | 499 | return self::$token; |
@@ -505,7 +505,7 @@ discard block |
||
505 | 505 | * @deprecated 9.0.0 Use annotations based on the app framework. |
506 | 506 | */ |
507 | 507 | public static function callCheck() { |
508 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
508 | + if (!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
509 | 509 | header('Location: '.\OC::$WEBROOT); |
510 | 510 | exit(); |
511 | 511 | } |
@@ -696,7 +696,7 @@ discard block |
||
696 | 696 | */ |
697 | 697 | public static function needUpgrade() { |
698 | 698 | if (!isset(self::$needUpgradeCache)) { |
699 | - self::$needUpgradeCache=\OC_Util::needUpgrade(\OC::$server->getConfig()); |
|
699 | + self::$needUpgradeCache = \OC_Util::needUpgrade(\OC::$server->getConfig()); |
|
700 | 700 | } |
701 | 701 | return self::$needUpgradeCache; |
702 | 702 | } |
@@ -369,7 +369,7 @@ |
||
369 | 369 | * @param string $path path to the file which should be updated |
370 | 370 | * @param string $uid of the user who performs the operation |
371 | 371 | * @param array $accessList who has access to the file contains the key 'users' and 'public' |
372 | - * @return boolean |
|
372 | + * @return null|boolean |
|
373 | 373 | */ |
374 | 374 | public function update($path, $uid, array $accessList) { |
375 | 375 |
@@ -43,522 +43,522 @@ |
||
43 | 43 | |
44 | 44 | class Encryption implements IEncryptionModule { |
45 | 45 | |
46 | - const ID = 'OC_DEFAULT_MODULE'; |
|
47 | - const DISPLAY_NAME = 'Default encryption module'; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var Crypt |
|
51 | - */ |
|
52 | - private $crypt; |
|
53 | - |
|
54 | - /** @var string */ |
|
55 | - private $cipher; |
|
56 | - |
|
57 | - /** @var string */ |
|
58 | - private $path; |
|
59 | - |
|
60 | - /** @var string */ |
|
61 | - private $user; |
|
62 | - |
|
63 | - /** @var string */ |
|
64 | - private $fileKey; |
|
65 | - |
|
66 | - /** @var string */ |
|
67 | - private $writeCache; |
|
68 | - |
|
69 | - /** @var KeyManager */ |
|
70 | - private $keyManager; |
|
71 | - |
|
72 | - /** @var array */ |
|
73 | - private $accessList; |
|
74 | - |
|
75 | - /** @var boolean */ |
|
76 | - private $isWriteOperation; |
|
77 | - |
|
78 | - /** @var Util */ |
|
79 | - private $util; |
|
80 | - |
|
81 | - /** @var Session */ |
|
82 | - private $session; |
|
83 | - |
|
84 | - /** @var ILogger */ |
|
85 | - private $logger; |
|
86 | - |
|
87 | - /** @var IL10N */ |
|
88 | - private $l; |
|
89 | - |
|
90 | - /** @var EncryptAll */ |
|
91 | - private $encryptAll; |
|
92 | - |
|
93 | - /** @var bool */ |
|
94 | - private $useMasterPassword; |
|
95 | - |
|
96 | - /** @var DecryptAll */ |
|
97 | - private $decryptAll; |
|
98 | - |
|
99 | - /** @var int unencrypted block size if block contains signature */ |
|
100 | - private $unencryptedBlockSizeSigned = 6072; |
|
101 | - |
|
102 | - /** @var int unencrypted block size */ |
|
103 | - private $unencryptedBlockSize = 6126; |
|
104 | - |
|
105 | - /** @var int Current version of the file */ |
|
106 | - private $version = 0; |
|
107 | - |
|
108 | - /** @var array remember encryption signature version */ |
|
109 | - private static $rememberVersion = []; |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * |
|
114 | - * @param Crypt $crypt |
|
115 | - * @param KeyManager $keyManager |
|
116 | - * @param Util $util |
|
117 | - * @param Session $session |
|
118 | - * @param EncryptAll $encryptAll |
|
119 | - * @param DecryptAll $decryptAll |
|
120 | - * @param ILogger $logger |
|
121 | - * @param IL10N $il10n |
|
122 | - */ |
|
123 | - public function __construct(Crypt $crypt, |
|
124 | - KeyManager $keyManager, |
|
125 | - Util $util, |
|
126 | - Session $session, |
|
127 | - EncryptAll $encryptAll, |
|
128 | - DecryptAll $decryptAll, |
|
129 | - ILogger $logger, |
|
130 | - IL10N $il10n) { |
|
131 | - $this->crypt = $crypt; |
|
132 | - $this->keyManager = $keyManager; |
|
133 | - $this->util = $util; |
|
134 | - $this->session = $session; |
|
135 | - $this->encryptAll = $encryptAll; |
|
136 | - $this->decryptAll = $decryptAll; |
|
137 | - $this->logger = $logger; |
|
138 | - $this->l = $il10n; |
|
139 | - $this->useMasterPassword = $util->isMasterKeyEnabled(); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @return string defining the technical unique id |
|
144 | - */ |
|
145 | - public function getId() { |
|
146 | - return self::ID; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
151 | - * |
|
152 | - * @return string |
|
153 | - */ |
|
154 | - public function getDisplayName() { |
|
155 | - return self::DISPLAY_NAME; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * start receiving chunks from a file. This is the place where you can |
|
160 | - * perform some initial step before starting encrypting/decrypting the |
|
161 | - * chunks |
|
162 | - * |
|
163 | - * @param string $path to the file |
|
164 | - * @param string $user who read/write the file |
|
165 | - * @param string $mode php stream open mode |
|
166 | - * @param array $header contains the header data read from the file |
|
167 | - * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
168 | - * |
|
169 | - * @return array $header contain data as key-value pairs which should be |
|
170 | - * written to the header, in case of a write operation |
|
171 | - * or if no additional data is needed return a empty array |
|
172 | - */ |
|
173 | - public function begin($path, $user, $mode, array $header, array $accessList) { |
|
174 | - $this->path = $this->getPathToRealFile($path); |
|
175 | - $this->accessList = $accessList; |
|
176 | - $this->user = $user; |
|
177 | - $this->isWriteOperation = false; |
|
178 | - $this->writeCache = ''; |
|
179 | - |
|
180 | - if ($this->session->decryptAllModeActivated()) { |
|
181 | - $encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path); |
|
182 | - $shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid()); |
|
183 | - $this->fileKey = $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
184 | - $shareKey, |
|
185 | - $this->session->getDecryptAllKey()); |
|
186 | - } else { |
|
187 | - $this->fileKey = $this->keyManager->getFileKey($this->path, $this->user); |
|
188 | - } |
|
189 | - |
|
190 | - // always use the version from the original file, also part files |
|
191 | - // need to have a correct version number if they get moved over to the |
|
192 | - // final location |
|
193 | - $this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View()); |
|
194 | - |
|
195 | - if ( |
|
196 | - $mode === 'w' |
|
197 | - || $mode === 'w+' |
|
198 | - || $mode === 'wb' |
|
199 | - || $mode === 'wb+' |
|
200 | - ) { |
|
201 | - $this->isWriteOperation = true; |
|
202 | - if (empty($this->fileKey)) { |
|
203 | - $this->fileKey = $this->crypt->generateFileKey(); |
|
204 | - } |
|
205 | - } else { |
|
206 | - // if we read a part file we need to increase the version by 1 |
|
207 | - // because the version number was also increased by writing |
|
208 | - // the part file |
|
209 | - if(Scanner::isPartialFile($path)) { |
|
210 | - $this->version = $this->version + 1; |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - if ($this->isWriteOperation) { |
|
215 | - $this->cipher = $this->crypt->getCipher(); |
|
216 | - } elseif (isset($header['cipher'])) { |
|
217 | - $this->cipher = $header['cipher']; |
|
218 | - } else { |
|
219 | - // if we read a file without a header we fall-back to the legacy cipher |
|
220 | - // which was used in <=oC6 |
|
221 | - $this->cipher = $this->crypt->getLegacyCipher(); |
|
222 | - } |
|
223 | - |
|
224 | - return array('cipher' => $this->cipher, 'signed' => 'true'); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * last chunk received. This is the place where you can perform some final |
|
229 | - * operation and return some remaining data if something is left in your |
|
230 | - * buffer. |
|
231 | - * |
|
232 | - * @param string $path to the file |
|
233 | - * @param int $position |
|
234 | - * @return string remained data which should be written to the file in case |
|
235 | - * of a write operation |
|
236 | - * @throws PublicKeyMissingException |
|
237 | - * @throws \Exception |
|
238 | - * @throws \OCA\Encryption\Exceptions\MultiKeyEncryptException |
|
239 | - */ |
|
240 | - public function end($path, $position = 0) { |
|
241 | - $result = ''; |
|
242 | - if ($this->isWriteOperation) { |
|
243 | - $this->keyManager->setVersion($path, $this->version + 1, new View()); |
|
244 | - // in case of a part file we remember the new signature versions |
|
245 | - // the version will be set later on update. |
|
246 | - // This way we make sure that other apps listening to the pre-hooks |
|
247 | - // still get the old version which should be the correct value for them |
|
248 | - if (Scanner::isPartialFile($path)) { |
|
249 | - self::$rememberVersion[$this->stripPartFileExtension($path)] = $this->version + 1; |
|
250 | - } |
|
251 | - if (!empty($this->writeCache)) { |
|
252 | - $result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey, $this->version + 1, $position); |
|
253 | - $this->writeCache = ''; |
|
254 | - } |
|
255 | - $publicKeys = array(); |
|
256 | - if ($this->useMasterPassword === true) { |
|
257 | - $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey(); |
|
258 | - } else { |
|
259 | - foreach ($this->accessList['users'] as $uid) { |
|
260 | - try { |
|
261 | - $publicKeys[$uid] = $this->keyManager->getPublicKey($uid); |
|
262 | - } catch (PublicKeyMissingException $e) { |
|
263 | - $this->logger->warning( |
|
264 | - 'no public key found for user "{uid}", user will not be able to read the file', |
|
265 | - ['app' => 'encryption', 'uid' => $uid] |
|
266 | - ); |
|
267 | - // if the public key of the owner is missing we should fail |
|
268 | - if ($uid === $this->user) { |
|
269 | - throw $e; |
|
270 | - } |
|
271 | - } |
|
272 | - } |
|
273 | - } |
|
274 | - |
|
275 | - $publicKeys = $this->keyManager->addSystemKeys($this->accessList, $publicKeys, $this->user); |
|
276 | - $encryptedKeyfiles = $this->crypt->multiKeyEncrypt($this->fileKey, $publicKeys); |
|
277 | - $this->keyManager->setAllFileKeys($this->path, $encryptedKeyfiles); |
|
278 | - } |
|
279 | - return $result; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * encrypt data |
|
284 | - * |
|
285 | - * @param string $data you want to encrypt |
|
286 | - * @param int $position |
|
287 | - * @return string encrypted data |
|
288 | - */ |
|
289 | - public function encrypt($data, $position = 0) { |
|
290 | - // If extra data is left over from the last round, make sure it |
|
291 | - // is integrated into the next block |
|
292 | - if ($this->writeCache) { |
|
293 | - |
|
294 | - // Concat writeCache to start of $data |
|
295 | - $data = $this->writeCache . $data; |
|
296 | - |
|
297 | - // Clear the write cache, ready for reuse - it has been |
|
298 | - // flushed and its old contents processed |
|
299 | - $this->writeCache = ''; |
|
300 | - |
|
301 | - } |
|
302 | - |
|
303 | - $encrypted = ''; |
|
304 | - // While there still remains some data to be processed & written |
|
305 | - while (strlen($data) > 0) { |
|
306 | - |
|
307 | - // Remaining length for this iteration, not of the |
|
308 | - // entire file (may be greater than 8192 bytes) |
|
309 | - $remainingLength = strlen($data); |
|
310 | - |
|
311 | - // If data remaining to be written is less than the |
|
312 | - // size of 1 6126 byte block |
|
313 | - if ($remainingLength < $this->unencryptedBlockSizeSigned) { |
|
314 | - |
|
315 | - // Set writeCache to contents of $data |
|
316 | - // The writeCache will be carried over to the |
|
317 | - // next write round, and added to the start of |
|
318 | - // $data to ensure that written blocks are |
|
319 | - // always the correct length. If there is still |
|
320 | - // data in writeCache after the writing round |
|
321 | - // has finished, then the data will be written |
|
322 | - // to disk by $this->flush(). |
|
323 | - $this->writeCache = $data; |
|
324 | - |
|
325 | - // Clear $data ready for next round |
|
326 | - $data = ''; |
|
327 | - |
|
328 | - } else { |
|
329 | - |
|
330 | - // Read the chunk from the start of $data |
|
331 | - $chunk = substr($data, 0, $this->unencryptedBlockSizeSigned); |
|
332 | - |
|
333 | - $encrypted .= $this->crypt->symmetricEncryptFileContent($chunk, $this->fileKey, $this->version + 1, $position); |
|
334 | - |
|
335 | - // Remove the chunk we just processed from |
|
336 | - // $data, leaving only unprocessed data in $data |
|
337 | - // var, for handling on the next round |
|
338 | - $data = substr($data, $this->unencryptedBlockSizeSigned); |
|
339 | - |
|
340 | - } |
|
341 | - |
|
342 | - } |
|
343 | - |
|
344 | - return $encrypted; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * decrypt data |
|
349 | - * |
|
350 | - * @param string $data you want to decrypt |
|
351 | - * @param int $position |
|
352 | - * @return string decrypted data |
|
353 | - * @throws DecryptionFailedException |
|
354 | - */ |
|
355 | - public function decrypt($data, $position = 0) { |
|
356 | - if (empty($this->fileKey)) { |
|
357 | - $msg = 'Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'; |
|
358 | - $hint = $this->l->t('Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
359 | - $this->logger->error($msg); |
|
360 | - |
|
361 | - throw new DecryptionFailedException($msg, $hint); |
|
362 | - } |
|
363 | - |
|
364 | - return $this->crypt->symmetricDecryptFileContent($data, $this->fileKey, $this->cipher, $this->version, $position); |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * update encrypted file, e.g. give additional users access to the file |
|
369 | - * |
|
370 | - * @param string $path path to the file which should be updated |
|
371 | - * @param string $uid of the user who performs the operation |
|
372 | - * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
373 | - * @return boolean |
|
374 | - */ |
|
375 | - public function update($path, $uid, array $accessList) { |
|
376 | - |
|
377 | - if (empty($accessList)) { |
|
378 | - if (isset(self::$rememberVersion[$path])) { |
|
379 | - $this->keyManager->setVersion($path, self::$rememberVersion[$path], new View()); |
|
380 | - unset(self::$rememberVersion[$path]); |
|
381 | - } |
|
382 | - return; |
|
383 | - } |
|
384 | - |
|
385 | - $fileKey = $this->keyManager->getFileKey($path, $uid); |
|
386 | - |
|
387 | - if (!empty($fileKey)) { |
|
388 | - |
|
389 | - $publicKeys = array(); |
|
390 | - if ($this->useMasterPassword === true) { |
|
391 | - $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey(); |
|
392 | - } else { |
|
393 | - foreach ($accessList['users'] as $user) { |
|
394 | - try { |
|
395 | - $publicKeys[$user] = $this->keyManager->getPublicKey($user); |
|
396 | - } catch (PublicKeyMissingException $e) { |
|
397 | - $this->logger->warning('Could not encrypt file for ' . $user . ': ' . $e->getMessage()); |
|
398 | - } |
|
399 | - } |
|
400 | - } |
|
401 | - |
|
402 | - $publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys, $uid); |
|
403 | - |
|
404 | - $encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys); |
|
405 | - |
|
406 | - $this->keyManager->deleteAllFileKeys($path); |
|
407 | - |
|
408 | - $this->keyManager->setAllFileKeys($path, $encryptedFileKey); |
|
409 | - |
|
410 | - } else { |
|
411 | - $this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted', |
|
412 | - array('file' => $path, 'app' => 'encryption')); |
|
413 | - |
|
414 | - return false; |
|
415 | - } |
|
416 | - |
|
417 | - return true; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * should the file be encrypted or not |
|
422 | - * |
|
423 | - * @param string $path |
|
424 | - * @return boolean |
|
425 | - */ |
|
426 | - public function shouldEncrypt($path) { |
|
427 | - if ($this->util->shouldEncryptHomeStorage() === false) { |
|
428 | - $storage = $this->util->getStorage($path); |
|
429 | - if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
430 | - return false; |
|
431 | - } |
|
432 | - } |
|
433 | - $parts = explode('/', $path); |
|
434 | - if (count($parts) < 4) { |
|
435 | - return false; |
|
436 | - } |
|
437 | - |
|
438 | - if ($parts[2] == 'files') { |
|
439 | - return true; |
|
440 | - } |
|
441 | - if ($parts[2] == 'files_versions') { |
|
442 | - return true; |
|
443 | - } |
|
444 | - if ($parts[2] == 'files_trashbin') { |
|
445 | - return true; |
|
446 | - } |
|
447 | - |
|
448 | - return false; |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * get size of the unencrypted payload per block. |
|
453 | - * ownCloud read/write files with a block size of 8192 byte |
|
454 | - * |
|
455 | - * @param bool $signed |
|
456 | - * @return int |
|
457 | - */ |
|
458 | - public function getUnencryptedBlockSize($signed = false) { |
|
459 | - if ($signed === false) { |
|
460 | - return $this->unencryptedBlockSize; |
|
461 | - } |
|
462 | - |
|
463 | - return $this->unencryptedBlockSizeSigned; |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * check if the encryption module is able to read the file, |
|
468 | - * e.g. if all encryption keys exists |
|
469 | - * |
|
470 | - * @param string $path |
|
471 | - * @param string $uid user for whom we want to check if he can read the file |
|
472 | - * @return bool |
|
473 | - * @throws DecryptionFailedException |
|
474 | - */ |
|
475 | - public function isReadable($path, $uid) { |
|
476 | - $fileKey = $this->keyManager->getFileKey($path, $uid); |
|
477 | - if (empty($fileKey)) { |
|
478 | - $owner = $this->util->getOwner($path); |
|
479 | - if ($owner !== $uid) { |
|
480 | - // if it is a shared file we throw a exception with a useful |
|
481 | - // error message because in this case it means that the file was |
|
482 | - // shared with the user at a point where the user didn't had a |
|
483 | - // valid private/public key |
|
484 | - $msg = 'Encryption module "' . $this->getDisplayName() . |
|
485 | - '" is not able to read ' . $path; |
|
486 | - $hint = $this->l->t('Can not read this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
487 | - $this->logger->warning($msg); |
|
488 | - throw new DecryptionFailedException($msg, $hint); |
|
489 | - } |
|
490 | - return false; |
|
491 | - } |
|
492 | - |
|
493 | - return true; |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Initial encryption of all files |
|
498 | - * |
|
499 | - * @param InputInterface $input |
|
500 | - * @param OutputInterface $output write some status information to the terminal during encryption |
|
501 | - */ |
|
502 | - public function encryptAll(InputInterface $input, OutputInterface $output) { |
|
503 | - $this->encryptAll->encryptAll($input, $output); |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * prepare module to perform decrypt all operation |
|
508 | - * |
|
509 | - * @param InputInterface $input |
|
510 | - * @param OutputInterface $output |
|
511 | - * @param string $user |
|
512 | - * @return bool |
|
513 | - */ |
|
514 | - public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = '') { |
|
515 | - return $this->decryptAll->prepare($input, $output, $user); |
|
516 | - } |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * @param string $path |
|
521 | - * @return string |
|
522 | - */ |
|
523 | - protected function getPathToRealFile($path) { |
|
524 | - $realPath = $path; |
|
525 | - $parts = explode('/', $path); |
|
526 | - if ($parts[2] === 'files_versions') { |
|
527 | - $realPath = '/' . $parts[1] . '/files/' . implode('/', array_slice($parts, 3)); |
|
528 | - $length = strrpos($realPath, '.'); |
|
529 | - $realPath = substr($realPath, 0, $length); |
|
530 | - } |
|
531 | - |
|
532 | - return $realPath; |
|
533 | - } |
|
534 | - |
|
535 | - /** |
|
536 | - * remove .part file extension and the ocTransferId from the file to get the |
|
537 | - * original file name |
|
538 | - * |
|
539 | - * @param string $path |
|
540 | - * @return string |
|
541 | - */ |
|
542 | - protected function stripPartFileExtension($path) { |
|
543 | - if (pathinfo($path, PATHINFO_EXTENSION) === 'part') { |
|
544 | - $pos = strrpos($path, '.', -6); |
|
545 | - $path = substr($path, 0, $pos); |
|
546 | - } |
|
547 | - |
|
548 | - return $path; |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * Check if the module is ready to be used by that specific user. |
|
553 | - * In case a module is not ready - because e.g. key pairs have not been generated |
|
554 | - * upon login this method can return false before any operation starts and might |
|
555 | - * cause issues during operations. |
|
556 | - * |
|
557 | - * @param string $user |
|
558 | - * @return boolean |
|
559 | - * @since 9.1.0 |
|
560 | - */ |
|
561 | - public function isReadyForUser($user) { |
|
562 | - return $this->keyManager->userHasKeys($user); |
|
563 | - } |
|
46 | + const ID = 'OC_DEFAULT_MODULE'; |
|
47 | + const DISPLAY_NAME = 'Default encryption module'; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var Crypt |
|
51 | + */ |
|
52 | + private $crypt; |
|
53 | + |
|
54 | + /** @var string */ |
|
55 | + private $cipher; |
|
56 | + |
|
57 | + /** @var string */ |
|
58 | + private $path; |
|
59 | + |
|
60 | + /** @var string */ |
|
61 | + private $user; |
|
62 | + |
|
63 | + /** @var string */ |
|
64 | + private $fileKey; |
|
65 | + |
|
66 | + /** @var string */ |
|
67 | + private $writeCache; |
|
68 | + |
|
69 | + /** @var KeyManager */ |
|
70 | + private $keyManager; |
|
71 | + |
|
72 | + /** @var array */ |
|
73 | + private $accessList; |
|
74 | + |
|
75 | + /** @var boolean */ |
|
76 | + private $isWriteOperation; |
|
77 | + |
|
78 | + /** @var Util */ |
|
79 | + private $util; |
|
80 | + |
|
81 | + /** @var Session */ |
|
82 | + private $session; |
|
83 | + |
|
84 | + /** @var ILogger */ |
|
85 | + private $logger; |
|
86 | + |
|
87 | + /** @var IL10N */ |
|
88 | + private $l; |
|
89 | + |
|
90 | + /** @var EncryptAll */ |
|
91 | + private $encryptAll; |
|
92 | + |
|
93 | + /** @var bool */ |
|
94 | + private $useMasterPassword; |
|
95 | + |
|
96 | + /** @var DecryptAll */ |
|
97 | + private $decryptAll; |
|
98 | + |
|
99 | + /** @var int unencrypted block size if block contains signature */ |
|
100 | + private $unencryptedBlockSizeSigned = 6072; |
|
101 | + |
|
102 | + /** @var int unencrypted block size */ |
|
103 | + private $unencryptedBlockSize = 6126; |
|
104 | + |
|
105 | + /** @var int Current version of the file */ |
|
106 | + private $version = 0; |
|
107 | + |
|
108 | + /** @var array remember encryption signature version */ |
|
109 | + private static $rememberVersion = []; |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * |
|
114 | + * @param Crypt $crypt |
|
115 | + * @param KeyManager $keyManager |
|
116 | + * @param Util $util |
|
117 | + * @param Session $session |
|
118 | + * @param EncryptAll $encryptAll |
|
119 | + * @param DecryptAll $decryptAll |
|
120 | + * @param ILogger $logger |
|
121 | + * @param IL10N $il10n |
|
122 | + */ |
|
123 | + public function __construct(Crypt $crypt, |
|
124 | + KeyManager $keyManager, |
|
125 | + Util $util, |
|
126 | + Session $session, |
|
127 | + EncryptAll $encryptAll, |
|
128 | + DecryptAll $decryptAll, |
|
129 | + ILogger $logger, |
|
130 | + IL10N $il10n) { |
|
131 | + $this->crypt = $crypt; |
|
132 | + $this->keyManager = $keyManager; |
|
133 | + $this->util = $util; |
|
134 | + $this->session = $session; |
|
135 | + $this->encryptAll = $encryptAll; |
|
136 | + $this->decryptAll = $decryptAll; |
|
137 | + $this->logger = $logger; |
|
138 | + $this->l = $il10n; |
|
139 | + $this->useMasterPassword = $util->isMasterKeyEnabled(); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @return string defining the technical unique id |
|
144 | + */ |
|
145 | + public function getId() { |
|
146 | + return self::ID; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * In comparison to getKey() this function returns a human readable (maybe translated) name |
|
151 | + * |
|
152 | + * @return string |
|
153 | + */ |
|
154 | + public function getDisplayName() { |
|
155 | + return self::DISPLAY_NAME; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * start receiving chunks from a file. This is the place where you can |
|
160 | + * perform some initial step before starting encrypting/decrypting the |
|
161 | + * chunks |
|
162 | + * |
|
163 | + * @param string $path to the file |
|
164 | + * @param string $user who read/write the file |
|
165 | + * @param string $mode php stream open mode |
|
166 | + * @param array $header contains the header data read from the file |
|
167 | + * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
168 | + * |
|
169 | + * @return array $header contain data as key-value pairs which should be |
|
170 | + * written to the header, in case of a write operation |
|
171 | + * or if no additional data is needed return a empty array |
|
172 | + */ |
|
173 | + public function begin($path, $user, $mode, array $header, array $accessList) { |
|
174 | + $this->path = $this->getPathToRealFile($path); |
|
175 | + $this->accessList = $accessList; |
|
176 | + $this->user = $user; |
|
177 | + $this->isWriteOperation = false; |
|
178 | + $this->writeCache = ''; |
|
179 | + |
|
180 | + if ($this->session->decryptAllModeActivated()) { |
|
181 | + $encryptedFileKey = $this->keyManager->getEncryptedFileKey($this->path); |
|
182 | + $shareKey = $this->keyManager->getShareKey($this->path, $this->session->getDecryptAllUid()); |
|
183 | + $this->fileKey = $this->crypt->multiKeyDecrypt($encryptedFileKey, |
|
184 | + $shareKey, |
|
185 | + $this->session->getDecryptAllKey()); |
|
186 | + } else { |
|
187 | + $this->fileKey = $this->keyManager->getFileKey($this->path, $this->user); |
|
188 | + } |
|
189 | + |
|
190 | + // always use the version from the original file, also part files |
|
191 | + // need to have a correct version number if they get moved over to the |
|
192 | + // final location |
|
193 | + $this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View()); |
|
194 | + |
|
195 | + if ( |
|
196 | + $mode === 'w' |
|
197 | + || $mode === 'w+' |
|
198 | + || $mode === 'wb' |
|
199 | + || $mode === 'wb+' |
|
200 | + ) { |
|
201 | + $this->isWriteOperation = true; |
|
202 | + if (empty($this->fileKey)) { |
|
203 | + $this->fileKey = $this->crypt->generateFileKey(); |
|
204 | + } |
|
205 | + } else { |
|
206 | + // if we read a part file we need to increase the version by 1 |
|
207 | + // because the version number was also increased by writing |
|
208 | + // the part file |
|
209 | + if(Scanner::isPartialFile($path)) { |
|
210 | + $this->version = $this->version + 1; |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + if ($this->isWriteOperation) { |
|
215 | + $this->cipher = $this->crypt->getCipher(); |
|
216 | + } elseif (isset($header['cipher'])) { |
|
217 | + $this->cipher = $header['cipher']; |
|
218 | + } else { |
|
219 | + // if we read a file without a header we fall-back to the legacy cipher |
|
220 | + // which was used in <=oC6 |
|
221 | + $this->cipher = $this->crypt->getLegacyCipher(); |
|
222 | + } |
|
223 | + |
|
224 | + return array('cipher' => $this->cipher, 'signed' => 'true'); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * last chunk received. This is the place where you can perform some final |
|
229 | + * operation and return some remaining data if something is left in your |
|
230 | + * buffer. |
|
231 | + * |
|
232 | + * @param string $path to the file |
|
233 | + * @param int $position |
|
234 | + * @return string remained data which should be written to the file in case |
|
235 | + * of a write operation |
|
236 | + * @throws PublicKeyMissingException |
|
237 | + * @throws \Exception |
|
238 | + * @throws \OCA\Encryption\Exceptions\MultiKeyEncryptException |
|
239 | + */ |
|
240 | + public function end($path, $position = 0) { |
|
241 | + $result = ''; |
|
242 | + if ($this->isWriteOperation) { |
|
243 | + $this->keyManager->setVersion($path, $this->version + 1, new View()); |
|
244 | + // in case of a part file we remember the new signature versions |
|
245 | + // the version will be set later on update. |
|
246 | + // This way we make sure that other apps listening to the pre-hooks |
|
247 | + // still get the old version which should be the correct value for them |
|
248 | + if (Scanner::isPartialFile($path)) { |
|
249 | + self::$rememberVersion[$this->stripPartFileExtension($path)] = $this->version + 1; |
|
250 | + } |
|
251 | + if (!empty($this->writeCache)) { |
|
252 | + $result = $this->crypt->symmetricEncryptFileContent($this->writeCache, $this->fileKey, $this->version + 1, $position); |
|
253 | + $this->writeCache = ''; |
|
254 | + } |
|
255 | + $publicKeys = array(); |
|
256 | + if ($this->useMasterPassword === true) { |
|
257 | + $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey(); |
|
258 | + } else { |
|
259 | + foreach ($this->accessList['users'] as $uid) { |
|
260 | + try { |
|
261 | + $publicKeys[$uid] = $this->keyManager->getPublicKey($uid); |
|
262 | + } catch (PublicKeyMissingException $e) { |
|
263 | + $this->logger->warning( |
|
264 | + 'no public key found for user "{uid}", user will not be able to read the file', |
|
265 | + ['app' => 'encryption', 'uid' => $uid] |
|
266 | + ); |
|
267 | + // if the public key of the owner is missing we should fail |
|
268 | + if ($uid === $this->user) { |
|
269 | + throw $e; |
|
270 | + } |
|
271 | + } |
|
272 | + } |
|
273 | + } |
|
274 | + |
|
275 | + $publicKeys = $this->keyManager->addSystemKeys($this->accessList, $publicKeys, $this->user); |
|
276 | + $encryptedKeyfiles = $this->crypt->multiKeyEncrypt($this->fileKey, $publicKeys); |
|
277 | + $this->keyManager->setAllFileKeys($this->path, $encryptedKeyfiles); |
|
278 | + } |
|
279 | + return $result; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * encrypt data |
|
284 | + * |
|
285 | + * @param string $data you want to encrypt |
|
286 | + * @param int $position |
|
287 | + * @return string encrypted data |
|
288 | + */ |
|
289 | + public function encrypt($data, $position = 0) { |
|
290 | + // If extra data is left over from the last round, make sure it |
|
291 | + // is integrated into the next block |
|
292 | + if ($this->writeCache) { |
|
293 | + |
|
294 | + // Concat writeCache to start of $data |
|
295 | + $data = $this->writeCache . $data; |
|
296 | + |
|
297 | + // Clear the write cache, ready for reuse - it has been |
|
298 | + // flushed and its old contents processed |
|
299 | + $this->writeCache = ''; |
|
300 | + |
|
301 | + } |
|
302 | + |
|
303 | + $encrypted = ''; |
|
304 | + // While there still remains some data to be processed & written |
|
305 | + while (strlen($data) > 0) { |
|
306 | + |
|
307 | + // Remaining length for this iteration, not of the |
|
308 | + // entire file (may be greater than 8192 bytes) |
|
309 | + $remainingLength = strlen($data); |
|
310 | + |
|
311 | + // If data remaining to be written is less than the |
|
312 | + // size of 1 6126 byte block |
|
313 | + if ($remainingLength < $this->unencryptedBlockSizeSigned) { |
|
314 | + |
|
315 | + // Set writeCache to contents of $data |
|
316 | + // The writeCache will be carried over to the |
|
317 | + // next write round, and added to the start of |
|
318 | + // $data to ensure that written blocks are |
|
319 | + // always the correct length. If there is still |
|
320 | + // data in writeCache after the writing round |
|
321 | + // has finished, then the data will be written |
|
322 | + // to disk by $this->flush(). |
|
323 | + $this->writeCache = $data; |
|
324 | + |
|
325 | + // Clear $data ready for next round |
|
326 | + $data = ''; |
|
327 | + |
|
328 | + } else { |
|
329 | + |
|
330 | + // Read the chunk from the start of $data |
|
331 | + $chunk = substr($data, 0, $this->unencryptedBlockSizeSigned); |
|
332 | + |
|
333 | + $encrypted .= $this->crypt->symmetricEncryptFileContent($chunk, $this->fileKey, $this->version + 1, $position); |
|
334 | + |
|
335 | + // Remove the chunk we just processed from |
|
336 | + // $data, leaving only unprocessed data in $data |
|
337 | + // var, for handling on the next round |
|
338 | + $data = substr($data, $this->unencryptedBlockSizeSigned); |
|
339 | + |
|
340 | + } |
|
341 | + |
|
342 | + } |
|
343 | + |
|
344 | + return $encrypted; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * decrypt data |
|
349 | + * |
|
350 | + * @param string $data you want to decrypt |
|
351 | + * @param int $position |
|
352 | + * @return string decrypted data |
|
353 | + * @throws DecryptionFailedException |
|
354 | + */ |
|
355 | + public function decrypt($data, $position = 0) { |
|
356 | + if (empty($this->fileKey)) { |
|
357 | + $msg = 'Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'; |
|
358 | + $hint = $this->l->t('Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
359 | + $this->logger->error($msg); |
|
360 | + |
|
361 | + throw new DecryptionFailedException($msg, $hint); |
|
362 | + } |
|
363 | + |
|
364 | + return $this->crypt->symmetricDecryptFileContent($data, $this->fileKey, $this->cipher, $this->version, $position); |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * update encrypted file, e.g. give additional users access to the file |
|
369 | + * |
|
370 | + * @param string $path path to the file which should be updated |
|
371 | + * @param string $uid of the user who performs the operation |
|
372 | + * @param array $accessList who has access to the file contains the key 'users' and 'public' |
|
373 | + * @return boolean |
|
374 | + */ |
|
375 | + public function update($path, $uid, array $accessList) { |
|
376 | + |
|
377 | + if (empty($accessList)) { |
|
378 | + if (isset(self::$rememberVersion[$path])) { |
|
379 | + $this->keyManager->setVersion($path, self::$rememberVersion[$path], new View()); |
|
380 | + unset(self::$rememberVersion[$path]); |
|
381 | + } |
|
382 | + return; |
|
383 | + } |
|
384 | + |
|
385 | + $fileKey = $this->keyManager->getFileKey($path, $uid); |
|
386 | + |
|
387 | + if (!empty($fileKey)) { |
|
388 | + |
|
389 | + $publicKeys = array(); |
|
390 | + if ($this->useMasterPassword === true) { |
|
391 | + $publicKeys[$this->keyManager->getMasterKeyId()] = $this->keyManager->getPublicMasterKey(); |
|
392 | + } else { |
|
393 | + foreach ($accessList['users'] as $user) { |
|
394 | + try { |
|
395 | + $publicKeys[$user] = $this->keyManager->getPublicKey($user); |
|
396 | + } catch (PublicKeyMissingException $e) { |
|
397 | + $this->logger->warning('Could not encrypt file for ' . $user . ': ' . $e->getMessage()); |
|
398 | + } |
|
399 | + } |
|
400 | + } |
|
401 | + |
|
402 | + $publicKeys = $this->keyManager->addSystemKeys($accessList, $publicKeys, $uid); |
|
403 | + |
|
404 | + $encryptedFileKey = $this->crypt->multiKeyEncrypt($fileKey, $publicKeys); |
|
405 | + |
|
406 | + $this->keyManager->deleteAllFileKeys($path); |
|
407 | + |
|
408 | + $this->keyManager->setAllFileKeys($path, $encryptedFileKey); |
|
409 | + |
|
410 | + } else { |
|
411 | + $this->logger->debug('no file key found, we assume that the file "{file}" is not encrypted', |
|
412 | + array('file' => $path, 'app' => 'encryption')); |
|
413 | + |
|
414 | + return false; |
|
415 | + } |
|
416 | + |
|
417 | + return true; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * should the file be encrypted or not |
|
422 | + * |
|
423 | + * @param string $path |
|
424 | + * @return boolean |
|
425 | + */ |
|
426 | + public function shouldEncrypt($path) { |
|
427 | + if ($this->util->shouldEncryptHomeStorage() === false) { |
|
428 | + $storage = $this->util->getStorage($path); |
|
429 | + if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
430 | + return false; |
|
431 | + } |
|
432 | + } |
|
433 | + $parts = explode('/', $path); |
|
434 | + if (count($parts) < 4) { |
|
435 | + return false; |
|
436 | + } |
|
437 | + |
|
438 | + if ($parts[2] == 'files') { |
|
439 | + return true; |
|
440 | + } |
|
441 | + if ($parts[2] == 'files_versions') { |
|
442 | + return true; |
|
443 | + } |
|
444 | + if ($parts[2] == 'files_trashbin') { |
|
445 | + return true; |
|
446 | + } |
|
447 | + |
|
448 | + return false; |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * get size of the unencrypted payload per block. |
|
453 | + * ownCloud read/write files with a block size of 8192 byte |
|
454 | + * |
|
455 | + * @param bool $signed |
|
456 | + * @return int |
|
457 | + */ |
|
458 | + public function getUnencryptedBlockSize($signed = false) { |
|
459 | + if ($signed === false) { |
|
460 | + return $this->unencryptedBlockSize; |
|
461 | + } |
|
462 | + |
|
463 | + return $this->unencryptedBlockSizeSigned; |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * check if the encryption module is able to read the file, |
|
468 | + * e.g. if all encryption keys exists |
|
469 | + * |
|
470 | + * @param string $path |
|
471 | + * @param string $uid user for whom we want to check if he can read the file |
|
472 | + * @return bool |
|
473 | + * @throws DecryptionFailedException |
|
474 | + */ |
|
475 | + public function isReadable($path, $uid) { |
|
476 | + $fileKey = $this->keyManager->getFileKey($path, $uid); |
|
477 | + if (empty($fileKey)) { |
|
478 | + $owner = $this->util->getOwner($path); |
|
479 | + if ($owner !== $uid) { |
|
480 | + // if it is a shared file we throw a exception with a useful |
|
481 | + // error message because in this case it means that the file was |
|
482 | + // shared with the user at a point where the user didn't had a |
|
483 | + // valid private/public key |
|
484 | + $msg = 'Encryption module "' . $this->getDisplayName() . |
|
485 | + '" is not able to read ' . $path; |
|
486 | + $hint = $this->l->t('Can not read this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
|
487 | + $this->logger->warning($msg); |
|
488 | + throw new DecryptionFailedException($msg, $hint); |
|
489 | + } |
|
490 | + return false; |
|
491 | + } |
|
492 | + |
|
493 | + return true; |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Initial encryption of all files |
|
498 | + * |
|
499 | + * @param InputInterface $input |
|
500 | + * @param OutputInterface $output write some status information to the terminal during encryption |
|
501 | + */ |
|
502 | + public function encryptAll(InputInterface $input, OutputInterface $output) { |
|
503 | + $this->encryptAll->encryptAll($input, $output); |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * prepare module to perform decrypt all operation |
|
508 | + * |
|
509 | + * @param InputInterface $input |
|
510 | + * @param OutputInterface $output |
|
511 | + * @param string $user |
|
512 | + * @return bool |
|
513 | + */ |
|
514 | + public function prepareDecryptAll(InputInterface $input, OutputInterface $output, $user = '') { |
|
515 | + return $this->decryptAll->prepare($input, $output, $user); |
|
516 | + } |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * @param string $path |
|
521 | + * @return string |
|
522 | + */ |
|
523 | + protected function getPathToRealFile($path) { |
|
524 | + $realPath = $path; |
|
525 | + $parts = explode('/', $path); |
|
526 | + if ($parts[2] === 'files_versions') { |
|
527 | + $realPath = '/' . $parts[1] . '/files/' . implode('/', array_slice($parts, 3)); |
|
528 | + $length = strrpos($realPath, '.'); |
|
529 | + $realPath = substr($realPath, 0, $length); |
|
530 | + } |
|
531 | + |
|
532 | + return $realPath; |
|
533 | + } |
|
534 | + |
|
535 | + /** |
|
536 | + * remove .part file extension and the ocTransferId from the file to get the |
|
537 | + * original file name |
|
538 | + * |
|
539 | + * @param string $path |
|
540 | + * @return string |
|
541 | + */ |
|
542 | + protected function stripPartFileExtension($path) { |
|
543 | + if (pathinfo($path, PATHINFO_EXTENSION) === 'part') { |
|
544 | + $pos = strrpos($path, '.', -6); |
|
545 | + $path = substr($path, 0, $pos); |
|
546 | + } |
|
547 | + |
|
548 | + return $path; |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * Check if the module is ready to be used by that specific user. |
|
553 | + * In case a module is not ready - because e.g. key pairs have not been generated |
|
554 | + * upon login this method can return false before any operation starts and might |
|
555 | + * cause issues during operations. |
|
556 | + * |
|
557 | + * @param string $user |
|
558 | + * @return boolean |
|
559 | + * @since 9.1.0 |
|
560 | + */ |
|
561 | + public function isReadyForUser($user) { |
|
562 | + return $this->keyManager->userHasKeys($user); |
|
563 | + } |
|
564 | 564 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | // always use the version from the original file, also part files |
191 | 191 | // need to have a correct version number if they get moved over to the |
192 | 192 | // final location |
193 | - $this->version = (int)$this->keyManager->getVersion($this->stripPartFileExtension($path), new View()); |
|
193 | + $this->version = (int) $this->keyManager->getVersion($this->stripPartFileExtension($path), new View()); |
|
194 | 194 | |
195 | 195 | if ( |
196 | 196 | $mode === 'w' |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | // if we read a part file we need to increase the version by 1 |
207 | 207 | // because the version number was also increased by writing |
208 | 208 | // the part file |
209 | - if(Scanner::isPartialFile($path)) { |
|
209 | + if (Scanner::isPartialFile($path)) { |
|
210 | 210 | $this->version = $this->version + 1; |
211 | 211 | } |
212 | 212 | } |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | if ($this->writeCache) { |
293 | 293 | |
294 | 294 | // Concat writeCache to start of $data |
295 | - $data = $this->writeCache . $data; |
|
295 | + $data = $this->writeCache.$data; |
|
296 | 296 | |
297 | 297 | // Clear the write cache, ready for reuse - it has been |
298 | 298 | // flushed and its old contents processed |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | try { |
395 | 395 | $publicKeys[$user] = $this->keyManager->getPublicKey($user); |
396 | 396 | } catch (PublicKeyMissingException $e) { |
397 | - $this->logger->warning('Could not encrypt file for ' . $user . ': ' . $e->getMessage()); |
|
397 | + $this->logger->warning('Could not encrypt file for '.$user.': '.$e->getMessage()); |
|
398 | 398 | } |
399 | 399 | } |
400 | 400 | } |
@@ -481,8 +481,8 @@ discard block |
||
481 | 481 | // error message because in this case it means that the file was |
482 | 482 | // shared with the user at a point where the user didn't had a |
483 | 483 | // valid private/public key |
484 | - $msg = 'Encryption module "' . $this->getDisplayName() . |
|
485 | - '" is not able to read ' . $path; |
|
484 | + $msg = 'Encryption module "'.$this->getDisplayName(). |
|
485 | + '" is not able to read '.$path; |
|
486 | 486 | $hint = $this->l->t('Can not read this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); |
487 | 487 | $this->logger->warning($msg); |
488 | 488 | throw new DecryptionFailedException($msg, $hint); |
@@ -524,7 +524,7 @@ discard block |
||
524 | 524 | $realPath = $path; |
525 | 525 | $parts = explode('/', $path); |
526 | 526 | if ($parts[2] === 'files_versions') { |
527 | - $realPath = '/' . $parts[1] . '/files/' . implode('/', array_slice($parts, 3)); |
|
527 | + $realPath = '/'.$parts[1].'/files/'.implode('/', array_slice($parts, 3)); |
|
528 | 528 | $length = strrpos($realPath, '.'); |
529 | 529 | $realPath = substr($realPath, 0, $length); |
530 | 530 | } |