Completed
Pull Request — master (#5854)
by Morris
36:47 queued 20:14
created
lib/private/legacy/user.php 1 patch
Indentation   +509 added lines, -510 removed lines patch added patch discarded remove patch
@@ -57,514 +57,513 @@
 block discarded – undo
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 $_usedBackends = array();
68
-
69
-	private static $_setupedBackends = array();
70
-
71
-	// bool, stores if a user want to access a resource anonymously, e.g if they open a public link
72
-	private static $incognitoMode = false;
73
-
74
-	/**
75
-	 * Adds the backend to the list of used backends
76
-	 *
77
-	 * @param string|\OCP\UserInterface $backend default: database The backend to use for user management
78
-	 * @return bool
79
-	 *
80
-	 * Set the User Authentication Module
81
-	 */
82
-	public static function useBackend($backend = 'database') {
83
-		if ($backend instanceof \OCP\UserInterface) {
84
-			self::$_usedBackends[get_class($backend)] = $backend;
85
-			\OC::$server->getUserManager()->registerBackend($backend);
86
-		} else {
87
-			// You'll never know what happens
88
-			if (null === $backend OR !is_string($backend)) {
89
-				$backend = 'database';
90
-			}
91
-
92
-			// Load backend
93
-			switch ($backend) {
94
-				case 'database':
95
-				case 'mysql':
96
-				case 'sqlite':
97
-					\OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG);
98
-					self::$_usedBackends[$backend] = new \OC\User\Database();
99
-					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
100
-					break;
101
-				case 'dummy':
102
-					self::$_usedBackends[$backend] = new \Test\Util\User\Dummy();
103
-					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
104
-					break;
105
-				default:
106
-					\OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG);
107
-					$className = 'OC_USER_' . strtoupper($backend);
108
-					self::$_usedBackends[$backend] = new $className();
109
-					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
110
-					break;
111
-			}
112
-		}
113
-		return true;
114
-	}
115
-
116
-	/**
117
-	 * remove all used backends
118
-	 */
119
-	public static function clearBackends() {
120
-		self::$_usedBackends = array();
121
-		\OC::$server->getUserManager()->clearBackends();
122
-	}
123
-
124
-	/**
125
-	 * setup the configured backends in config.php
126
-	 */
127
-	public static function setupBackends() {
128
-		OC_App::loadApps(['prelogin']);
129
-		$backends = \OC::$server->getSystemConfig()->getValue('user_backends', []);
130
-		if (isset($backends['default']) && !$backends['default']) {
131
-			// clear default backends
132
-			self::clearBackends();
133
-		}
134
-		foreach ($backends as $i => $config) {
135
-			if (!is_array($config)) {
136
-				continue;
137
-			}
138
-			$class = $config['class'];
139
-			$arguments = $config['arguments'];
140
-			if (class_exists($class)) {
141
-				if (array_search($i, self::$_setupedBackends) === false) {
142
-					// make a reflection object
143
-					$reflectionObj = new ReflectionClass($class);
144
-
145
-					// use Reflection to create a new instance, using the $args
146
-					$backend = $reflectionObj->newInstanceArgs($arguments);
147
-					self::useBackend($backend);
148
-					self::$_setupedBackends[] = $i;
149
-				} else {
150
-					\OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG);
151
-				}
152
-			} else {
153
-				\OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR);
154
-			}
155
-		}
156
-	}
157
-
158
-	/**
159
-
160
-	 * Try to login a user using the magic cookie (remember login)
161
-	 *
162
-	 * @deprecated use \OCP\IUserSession::loginWithCookie()
163
-	 * @param string $uid The username of the user to log in
164
-	 * @param string $token
165
-	 * @param string $oldSessionId
166
-	 * @return bool
167
-	 */
168
-	public static function loginWithCookie($uid, $token, $oldSessionId) {
169
-		return self::getUserSession()->loginWithCookie($uid, $token, $oldSessionId);
170
-	}
171
-
172
-	/**
173
-	 * Try to login a user, assuming authentication
174
-	 * has already happened (e.g. via Single Sign On).
175
-	 *
176
-	 * Log in a user and regenerate a new session.
177
-	 *
178
-	 * @param \OCP\Authentication\IApacheBackend $backend
179
-	 * @return bool
180
-	 */
181
-	public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) {
182
-
183
-		$uid = $backend->getCurrentUserId();
184
-		$run = true;
185
-		OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid));
186
-
187
-		if ($uid) {
188
-			if (self::getUser() !== $uid) {
189
-				self::setUserId($uid);
190
-				$setUidAsDisplayName = true;
191
-				if($backend instanceof \OCP\UserInterface
192
-					&& $backend->implementsActions(OC_User_Backend::GET_DISPLAYNAME)) {
193
-
194
-					$backendDisplayName = $backend->getDisplayName($uid);
195
-					if(is_string($backendDisplayName) && trim($backendDisplayName) !== '') {
196
-						$setUidAsDisplayName = false;
197
-					}
198
-				}
199
-				if($setUidAsDisplayName) {
200
-					self::setDisplayName($uid);
201
-				}
202
-				$userSession = self::getUserSession();
203
-				$userSession->setLoginName($uid);
204
-				$request = OC::$server->getRequest();
205
-				$userSession->createSessionToken($request, $uid, $uid);
206
-				// setup the filesystem
207
-				OC_Util::setupFS($uid);
208
-				// first call the post_login hooks, the login-process needs to be
209
-				// completed before we can safely create the users folder.
210
-				// For example encryption needs to initialize the users keys first
211
-				// before we can create the user folder with the skeleton files
212
-				OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => ''));
213
-				//trigger creation of user home and /files folder
214
-				\OC::$server->getUserFolder($uid);
215
-			}
216
-			return true;
217
-		}
218
-		return false;
219
-	}
220
-
221
-	/**
222
-	 * Verify with Apache whether user is authenticated.
223
-	 *
224
-	 * @return boolean|null
225
-	 *          true: authenticated
226
-	 *          false: not authenticated
227
-	 *          null: not handled / no backend available
228
-	 */
229
-	public static function handleApacheAuth() {
230
-		$backend = self::findFirstActiveUsedBackend();
231
-		if ($backend) {
232
-			OC_App::loadApps();
233
-
234
-			//setup extra user backends
235
-			self::setupBackends();
236
-			self::unsetMagicInCookie();
237
-
238
-			return self::loginWithApache($backend);
239
-		}
240
-
241
-		return null;
242
-	}
243
-
244
-
245
-	/**
246
-	 * Sets user id for session and triggers emit
247
-	 *
248
-	 * @param string $uid
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
-	 * Check if the user is logged in, considers also the HTTP basic credentials
281
-	 *
282
-	 * @deprecated use \OC::$server->getUserSession()->isLoggedIn()
283
-	 * @return bool
284
-	 */
285
-	public static function isLoggedIn() {
286
-		return \OC::$server->getUserSession()->isLoggedIn();
287
-	}
288
-
289
-	/**
290
-	 * set incognito mode, e.g. if a user wants to open a public link
291
-	 *
292
-	 * @param bool $status
293
-	 */
294
-	public static function setIncognitoMode($status) {
295
-		self::$incognitoMode = $status;
296
-	}
297
-
298
-	/**
299
-	 * get incognito mode status
300
-	 *
301
-	 * @return bool
302
-	 */
303
-	public static function isIncognitoMode() {
304
-		return self::$incognitoMode;
305
-	}
306
-
307
-	/**
308
-	 * Supplies an attribute to the logout hyperlink. The default behaviour
309
-	 * is to return an href with '?logout=true' appended. However, it can
310
-	 * supply any attribute(s) which are valid for <a>.
311
-	 *
312
-	 * @return string with one or more HTML attributes.
313
-	 */
314
-	public static function getLogoutAttribute() {
315
-		$backend = self::findFirstActiveUsedBackend();
316
-		if ($backend) {
317
-			return $backend->getLogoutAttribute();
318
-		}
319
-
320
-		$logoutUrl = \OC::$server->getURLGenerator()->linkToRouteAbsolute(
321
-			'core.login.logout',
322
-			[
323
-				'requesttoken' => \OCP\Util::callRegister(),
324
-			]
325
-		);
326
-
327
-		return 'href="'.$logoutUrl.'"';
328
-	}
329
-
330
-	/**
331
-	 * Check if the user is an admin user
332
-	 *
333
-	 * @param string $uid uid of the admin
334
-	 * @return bool
335
-	 */
336
-	public static function isAdminUser($uid) {
337
-		$group = \OC::$server->getGroupManager()->get('admin');
338
-		$user = \OC::$server->getUserManager()->get($uid);
339
-		if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
340
-			return true;
341
-		}
342
-		return false;
343
-	}
344
-
345
-
346
-	/**
347
-	 * get the user id of the user currently logged in.
348
-	 *
349
-	 * @return string|bool uid or false
350
-	 */
351
-	public static function getUser() {
352
-		$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
353
-		if (!is_null($uid) && self::$incognitoMode === false) {
354
-			return $uid;
355
-		} else {
356
-			return false;
357
-		}
358
-	}
359
-
360
-	/**
361
-	 * get the display name of the user currently logged in.
362
-	 *
363
-	 * @param string $uid
364
-	 * @return string uid or false
365
-	 */
366
-	public static function getDisplayName($uid = null) {
367
-		if ($uid) {
368
-			$user = \OC::$server->getUserManager()->get($uid);
369
-			if ($user) {
370
-				return $user->getDisplayName();
371
-			} else {
372
-				return $uid;
373
-			}
374
-		} else {
375
-			$user = self::getUserSession()->getUser();
376
-			if ($user) {
377
-				return $user->getDisplayName();
378
-			} else {
379
-				return false;
380
-			}
381
-		}
382
-	}
383
-
384
-	/**
385
-	 * Autogenerate a password
386
-	 *
387
-	 * @return string
388
-	 *
389
-	 * generates a password
390
-	 */
391
-	public static function generatePassword() {
392
-		return \OC::$server->getSecureRandom()->generate(30);
393
-	}
394
-
395
-	/**
396
-	 * Set password
397
-	 *
398
-	 * @param string $uid The username
399
-	 * @param string $password The new password
400
-	 * @param string $recoveryPassword for the encryption app to reset encryption keys
401
-	 * @return bool
402
-	 *
403
-	 * Change the password of a user
404
-	 */
405
-	public static function setPassword($uid, $password, $recoveryPassword = null) {
406
-		$user = \OC::$server->getUserManager()->get($uid);
407
-		if ($user) {
408
-			return $user->setPassword($password, $recoveryPassword);
409
-		} else {
410
-			return false;
411
-		}
412
-	}
413
-
414
-	/**
415
-	 * Check if the password is correct
416
-	 *
417
-	 * @param string $uid The username
418
-	 * @param string $password The password
419
-	 * @return string|false user id a string on success, false otherwise
420
-	 *
421
-	 * Check if the password is correct without logging in the user
422
-	 * returns the user id or false
423
-	 */
424
-	public static function checkPassword($uid, $password) {
425
-		$manager = \OC::$server->getUserManager();
426
-		$username = $manager->checkPassword($uid, $password);
427
-		if ($username !== false) {
428
-			return $username->getUID();
429
-		}
430
-		return false;
431
-	}
432
-
433
-	/**
434
-	 * @param string $uid The username
435
-	 * @return string
436
-	 *
437
-	 * returns the path to the users home directory
438
-	 * @deprecated Use \OC::$server->getUserManager->getHome()
439
-	 */
440
-	public static function getHome($uid) {
441
-		$user = \OC::$server->getUserManager()->get($uid);
442
-		if ($user) {
443
-			return $user->getHome();
444
-		} else {
445
-			return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
446
-		}
447
-	}
448
-
449
-	/**
450
-	 * Get a list of all users
451
-	 *
452
-	 * @return array an array of all uids
453
-	 *
454
-	 * Get a list of all users.
455
-	 * @param string $search
456
-	 * @param integer $limit
457
-	 * @param integer $offset
458
-	 */
459
-	public static function getUsers($search = '', $limit = null, $offset = null) {
460
-		$users = \OC::$server->getUserManager()->search($search, $limit, $offset);
461
-		$uids = array();
462
-		foreach ($users as $user) {
463
-			$uids[] = $user->getUID();
464
-		}
465
-		return $uids;
466
-	}
467
-
468
-	/**
469
-	 * Get a list of all users display name
470
-	 *
471
-	 * @param string $search
472
-	 * @param int $limit
473
-	 * @param int $offset
474
-	 * @return array associative array with all display names (value) and corresponding uids (key)
475
-	 *
476
-	 * Get a list of all display names and user ids.
477
-	 * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
478
-	 */
479
-	public static function getDisplayNames($search = '', $limit = null, $offset = null) {
480
-		$displayNames = array();
481
-		$users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
482
-		foreach ($users as $user) {
483
-			$displayNames[$user->getUID()] = $user->getDisplayName();
484
-		}
485
-		return $displayNames;
486
-	}
487
-
488
-	/**
489
-	 * check if a user exists
490
-	 *
491
-	 * @param string $uid the username
492
-	 * @return boolean
493
-	 */
494
-	public static function userExists($uid) {
495
-		return \OC::$server->getUserManager()->userExists($uid);
496
-	}
497
-
498
-	/**
499
-	 * disables a user
500
-	 *
501
-	 * @param string $uid the user to disable
502
-	 */
503
-	public static function disableUser($uid) {
504
-		$user = \OC::$server->getUserManager()->get($uid);
505
-		if ($user) {
506
-			$user->setEnabled(false);
507
-		}
508
-	}
509
-
510
-	/**
511
-	 * enable a user
512
-	 *
513
-	 * @param string $uid
514
-	 */
515
-	public static function enableUser($uid) {
516
-		$user = \OC::$server->getUserManager()->get($uid);
517
-		if ($user) {
518
-			$user->setEnabled(true);
519
-		}
520
-	}
521
-
522
-	/**
523
-	 * checks if a user is enabled
524
-	 *
525
-	 * @param string $uid
526
-	 * @return bool
527
-	 */
528
-	public static function isEnabled($uid) {
529
-		$user = \OC::$server->getUserManager()->get($uid);
530
-		if ($user) {
531
-			return $user->isEnabled();
532
-		} else {
533
-			return false;
534
-		}
535
-	}
536
-
537
-	/**
538
-	 * Set cookie value to use in next page load
539
-	 *
540
-	 * @param string $username username to be set
541
-	 * @param string $token
542
-	 */
543
-	public static function setMagicInCookie($username, $token) {
544
-		self::getUserSession()->setMagicInCookie($username, $token);
545
-	}
546
-
547
-	/**
548
-	 * Remove cookie for "remember username"
549
-	 */
550
-	public static function unsetMagicInCookie() {
551
-		self::getUserSession()->unsetMagicInCookie();
552
-	}
553
-
554
-	/**
555
-	 * Returns the first active backend from self::$_usedBackends.
556
-	 *
557
-	 * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
558
-	 */
559
-	private static function findFirstActiveUsedBackend() {
560
-		foreach (self::$_usedBackends as $backend) {
561
-			if ($backend instanceof OCP\Authentication\IApacheBackend) {
562
-				if ($backend->isSessionActive()) {
563
-					return $backend;
564
-				}
565
-			}
566
-		}
567
-
568
-		return null;
569
-	}
60
+    /**
61
+     * @return \OC\User\Session
62
+     */
63
+    public static function getUserSession() {
64
+        return OC::$server->getUserSession();
65
+    }
66
+
67
+    private static $_usedBackends = array();
68
+
69
+    private static $_setupedBackends = array();
70
+
71
+    // bool, stores if a user want to access a resource anonymously, e.g if they open a public link
72
+    private static $incognitoMode = false;
73
+
74
+    /**
75
+     * Adds the backend to the list of used backends
76
+     *
77
+     * @param string|\OCP\UserInterface $backend default: database The backend to use for user management
78
+     * @return bool
79
+     *
80
+     * Set the User Authentication Module
81
+     */
82
+    public static function useBackend($backend = 'database') {
83
+        if ($backend instanceof \OCP\UserInterface) {
84
+            self::$_usedBackends[get_class($backend)] = $backend;
85
+            \OC::$server->getUserManager()->registerBackend($backend);
86
+        } else {
87
+            // You'll never know what happens
88
+            if (null === $backend OR !is_string($backend)) {
89
+                $backend = 'database';
90
+            }
91
+
92
+            // Load backend
93
+            switch ($backend) {
94
+                case 'database':
95
+                case 'mysql':
96
+                case 'sqlite':
97
+                    \OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG);
98
+                    self::$_usedBackends[$backend] = new \OC\User\Database();
99
+                    \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
100
+                    break;
101
+                case 'dummy':
102
+                    self::$_usedBackends[$backend] = new \Test\Util\User\Dummy();
103
+                    \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
104
+                    break;
105
+                default:
106
+                    \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG);
107
+                    $className = 'OC_USER_' . strtoupper($backend);
108
+                    self::$_usedBackends[$backend] = new $className();
109
+                    \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
110
+                    break;
111
+            }
112
+        }
113
+        return true;
114
+    }
115
+
116
+    /**
117
+     * remove all used backends
118
+     */
119
+    public static function clearBackends() {
120
+        self::$_usedBackends = array();
121
+        \OC::$server->getUserManager()->clearBackends();
122
+    }
123
+
124
+    /**
125
+     * setup the configured backends in config.php
126
+     */
127
+    public static function setupBackends() {
128
+        OC_App::loadApps(['prelogin']);
129
+        $backends = \OC::$server->getSystemConfig()->getValue('user_backends', []);
130
+        if (isset($backends['default']) && !$backends['default']) {
131
+            // clear default backends
132
+            self::clearBackends();
133
+        }
134
+        foreach ($backends as $i => $config) {
135
+            if (!is_array($config)) {
136
+                continue;
137
+            }
138
+            $class = $config['class'];
139
+            $arguments = $config['arguments'];
140
+            if (class_exists($class)) {
141
+                if (array_search($i, self::$_setupedBackends) === false) {
142
+                    // make a reflection object
143
+                    $reflectionObj = new ReflectionClass($class);
144
+
145
+                    // use Reflection to create a new instance, using the $args
146
+                    $backend = $reflectionObj->newInstanceArgs($arguments);
147
+                    self::useBackend($backend);
148
+                    self::$_setupedBackends[] = $i;
149
+                } else {
150
+                    \OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG);
151
+                }
152
+            } else {
153
+                \OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR);
154
+            }
155
+        }
156
+    }
157
+
158
+    /**
159
+     * Try to login a user using the magic cookie (remember login)
160
+     *
161
+     * @deprecated use \OCP\IUserSession::loginWithCookie()
162
+     * @param string $uid The username of the user to log in
163
+     * @param string $token
164
+     * @param string $oldSessionId
165
+     * @return bool
166
+     */
167
+    public static function loginWithCookie($uid, $token, $oldSessionId) {
168
+        return self::getUserSession()->loginWithCookie($uid, $token, $oldSessionId);
169
+    }
170
+
171
+    /**
172
+     * Try to login a user, assuming authentication
173
+     * has already happened (e.g. via Single Sign On).
174
+     *
175
+     * Log in a user and regenerate a new session.
176
+     *
177
+     * @param \OCP\Authentication\IApacheBackend $backend
178
+     * @return bool
179
+     */
180
+    public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) {
181
+
182
+        $uid = $backend->getCurrentUserId();
183
+        $run = true;
184
+        OC_Hook::emit("OC_User", "pre_login", array("run" => &$run, "uid" => $uid));
185
+
186
+        if ($uid) {
187
+            if (self::getUser() !== $uid) {
188
+                self::setUserId($uid);
189
+                $setUidAsDisplayName = true;
190
+                if($backend instanceof \OCP\UserInterface
191
+                    && $backend->implementsActions(OC_User_Backend::GET_DISPLAYNAME)) {
192
+
193
+                    $backendDisplayName = $backend->getDisplayName($uid);
194
+                    if(is_string($backendDisplayName) && trim($backendDisplayName) !== '') {
195
+                        $setUidAsDisplayName = false;
196
+                    }
197
+                }
198
+                if($setUidAsDisplayName) {
199
+                    self::setDisplayName($uid);
200
+                }
201
+                $userSession = self::getUserSession();
202
+                $userSession->setLoginName($uid);
203
+                $request = OC::$server->getRequest();
204
+                $userSession->createSessionToken($request, $uid, $uid);
205
+                // setup the filesystem
206
+                OC_Util::setupFS($uid);
207
+                // first call the post_login hooks, the login-process needs to be
208
+                // completed before we can safely create the users folder.
209
+                // For example encryption needs to initialize the users keys first
210
+                // before we can create the user folder with the skeleton files
211
+                OC_Hook::emit("OC_User", "post_login", array("uid" => $uid, 'password' => ''));
212
+                //trigger creation of user home and /files folder
213
+                \OC::$server->getUserFolder($uid);
214
+            }
215
+            return true;
216
+        }
217
+        return false;
218
+    }
219
+
220
+    /**
221
+     * Verify with Apache whether user is authenticated.
222
+     *
223
+     * @return boolean|null
224
+     *          true: authenticated
225
+     *          false: not authenticated
226
+     *          null: not handled / no backend available
227
+     */
228
+    public static function handleApacheAuth() {
229
+        $backend = self::findFirstActiveUsedBackend();
230
+        if ($backend) {
231
+            OC_App::loadApps();
232
+
233
+            //setup extra user backends
234
+            self::setupBackends();
235
+            self::unsetMagicInCookie();
236
+
237
+            return self::loginWithApache($backend);
238
+        }
239
+
240
+        return null;
241
+    }
242
+
243
+
244
+    /**
245
+     * Sets user id for session and triggers emit
246
+     *
247
+     * @param string $uid
248
+     */
249
+    public static function setUserId($uid) {
250
+        $userSession = \OC::$server->getUserSession();
251
+        $userManager = \OC::$server->getUserManager();
252
+        if ($user = $userManager->get($uid)) {
253
+            $userSession->setUser($user);
254
+        } else {
255
+            \OC::$server->getSession()->set('user_id', $uid);
256
+        }
257
+    }
258
+
259
+    /**
260
+     * Sets user display name for session
261
+     *
262
+     * @param string $uid
263
+     * @param string $displayName
264
+     * @return bool Whether the display name could get set
265
+     */
266
+    public static function setDisplayName($uid, $displayName = null) {
267
+        if (is_null($displayName)) {
268
+            $displayName = $uid;
269
+        }
270
+        $user = \OC::$server->getUserManager()->get($uid);
271
+        if ($user) {
272
+            return $user->setDisplayName($displayName);
273
+        } else {
274
+            return false;
275
+        }
276
+    }
277
+
278
+    /**
279
+     * Check if the user is logged in, considers also the HTTP basic credentials
280
+     *
281
+     * @deprecated use \OC::$server->getUserSession()->isLoggedIn()
282
+     * @return bool
283
+     */
284
+    public static function isLoggedIn() {
285
+        return \OC::$server->getUserSession()->isLoggedIn();
286
+    }
287
+
288
+    /**
289
+     * set incognito mode, e.g. if a user wants to open a public link
290
+     *
291
+     * @param bool $status
292
+     */
293
+    public static function setIncognitoMode($status) {
294
+        self::$incognitoMode = $status;
295
+    }
296
+
297
+    /**
298
+     * get incognito mode status
299
+     *
300
+     * @return bool
301
+     */
302
+    public static function isIncognitoMode() {
303
+        return self::$incognitoMode;
304
+    }
305
+
306
+    /**
307
+     * Supplies an attribute to the logout hyperlink. The default behaviour
308
+     * is to return an href with '?logout=true' appended. However, it can
309
+     * supply any attribute(s) which are valid for <a>.
310
+     *
311
+     * @return string with one or more HTML attributes.
312
+     */
313
+    public static function getLogoutAttribute() {
314
+        $backend = self::findFirstActiveUsedBackend();
315
+        if ($backend) {
316
+            return $backend->getLogoutAttribute();
317
+        }
318
+
319
+        $logoutUrl = \OC::$server->getURLGenerator()->linkToRouteAbsolute(
320
+            'core.login.logout',
321
+            [
322
+                'requesttoken' => \OCP\Util::callRegister(),
323
+            ]
324
+        );
325
+
326
+        return 'href="'.$logoutUrl.'"';
327
+    }
328
+
329
+    /**
330
+     * Check if the user is an admin user
331
+     *
332
+     * @param string $uid uid of the admin
333
+     * @return bool
334
+     */
335
+    public static function isAdminUser($uid) {
336
+        $group = \OC::$server->getGroupManager()->get('admin');
337
+        $user = \OC::$server->getUserManager()->get($uid);
338
+        if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
339
+            return true;
340
+        }
341
+        return false;
342
+    }
343
+
344
+
345
+    /**
346
+     * get the user id of the user currently logged in.
347
+     *
348
+     * @return string|bool uid or false
349
+     */
350
+    public static function getUser() {
351
+        $uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
352
+        if (!is_null($uid) && self::$incognitoMode === false) {
353
+            return $uid;
354
+        } else {
355
+            return false;
356
+        }
357
+    }
358
+
359
+    /**
360
+     * get the display name of the user currently logged in.
361
+     *
362
+     * @param string $uid
363
+     * @return string uid or false
364
+     */
365
+    public static function getDisplayName($uid = null) {
366
+        if ($uid) {
367
+            $user = \OC::$server->getUserManager()->get($uid);
368
+            if ($user) {
369
+                return $user->getDisplayName();
370
+            } else {
371
+                return $uid;
372
+            }
373
+        } else {
374
+            $user = self::getUserSession()->getUser();
375
+            if ($user) {
376
+                return $user->getDisplayName();
377
+            } else {
378
+                return false;
379
+            }
380
+        }
381
+    }
382
+
383
+    /**
384
+     * Autogenerate a password
385
+     *
386
+     * @return string
387
+     *
388
+     * generates a password
389
+     */
390
+    public static function generatePassword() {
391
+        return \OC::$server->getSecureRandom()->generate(30);
392
+    }
393
+
394
+    /**
395
+     * Set password
396
+     *
397
+     * @param string $uid The username
398
+     * @param string $password The new password
399
+     * @param string $recoveryPassword for the encryption app to reset encryption keys
400
+     * @return bool
401
+     *
402
+     * Change the password of a user
403
+     */
404
+    public static function setPassword($uid, $password, $recoveryPassword = null) {
405
+        $user = \OC::$server->getUserManager()->get($uid);
406
+        if ($user) {
407
+            return $user->setPassword($password, $recoveryPassword);
408
+        } else {
409
+            return false;
410
+        }
411
+    }
412
+
413
+    /**
414
+     * Check if the password is correct
415
+     *
416
+     * @param string $uid The username
417
+     * @param string $password The password
418
+     * @return string|false user id a string on success, false otherwise
419
+     *
420
+     * Check if the password is correct without logging in the user
421
+     * returns the user id or false
422
+     */
423
+    public static function checkPassword($uid, $password) {
424
+        $manager = \OC::$server->getUserManager();
425
+        $username = $manager->checkPassword($uid, $password);
426
+        if ($username !== false) {
427
+            return $username->getUID();
428
+        }
429
+        return false;
430
+    }
431
+
432
+    /**
433
+     * @param string $uid The username
434
+     * @return string
435
+     *
436
+     * returns the path to the users home directory
437
+     * @deprecated Use \OC::$server->getUserManager->getHome()
438
+     */
439
+    public static function getHome($uid) {
440
+        $user = \OC::$server->getUserManager()->get($uid);
441
+        if ($user) {
442
+            return $user->getHome();
443
+        } else {
444
+            return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
445
+        }
446
+    }
447
+
448
+    /**
449
+     * Get a list of all users
450
+     *
451
+     * @return array an array of all uids
452
+     *
453
+     * Get a list of all users.
454
+     * @param string $search
455
+     * @param integer $limit
456
+     * @param integer $offset
457
+     */
458
+    public static function getUsers($search = '', $limit = null, $offset = null) {
459
+        $users = \OC::$server->getUserManager()->search($search, $limit, $offset);
460
+        $uids = array();
461
+        foreach ($users as $user) {
462
+            $uids[] = $user->getUID();
463
+        }
464
+        return $uids;
465
+    }
466
+
467
+    /**
468
+     * Get a list of all users display name
469
+     *
470
+     * @param string $search
471
+     * @param int $limit
472
+     * @param int $offset
473
+     * @return array associative array with all display names (value) and corresponding uids (key)
474
+     *
475
+     * Get a list of all display names and user ids.
476
+     * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
477
+     */
478
+    public static function getDisplayNames($search = '', $limit = null, $offset = null) {
479
+        $displayNames = array();
480
+        $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
481
+        foreach ($users as $user) {
482
+            $displayNames[$user->getUID()] = $user->getDisplayName();
483
+        }
484
+        return $displayNames;
485
+    }
486
+
487
+    /**
488
+     * check if a user exists
489
+     *
490
+     * @param string $uid the username
491
+     * @return boolean
492
+     */
493
+    public static function userExists($uid) {
494
+        return \OC::$server->getUserManager()->userExists($uid);
495
+    }
496
+
497
+    /**
498
+     * disables a user
499
+     *
500
+     * @param string $uid the user to disable
501
+     */
502
+    public static function disableUser($uid) {
503
+        $user = \OC::$server->getUserManager()->get($uid);
504
+        if ($user) {
505
+            $user->setEnabled(false);
506
+        }
507
+    }
508
+
509
+    /**
510
+     * enable a user
511
+     *
512
+     * @param string $uid
513
+     */
514
+    public static function enableUser($uid) {
515
+        $user = \OC::$server->getUserManager()->get($uid);
516
+        if ($user) {
517
+            $user->setEnabled(true);
518
+        }
519
+    }
520
+
521
+    /**
522
+     * checks if a user is enabled
523
+     *
524
+     * @param string $uid
525
+     * @return bool
526
+     */
527
+    public static function isEnabled($uid) {
528
+        $user = \OC::$server->getUserManager()->get($uid);
529
+        if ($user) {
530
+            return $user->isEnabled();
531
+        } else {
532
+            return false;
533
+        }
534
+    }
535
+
536
+    /**
537
+     * Set cookie value to use in next page load
538
+     *
539
+     * @param string $username username to be set
540
+     * @param string $token
541
+     */
542
+    public static function setMagicInCookie($username, $token) {
543
+        self::getUserSession()->setMagicInCookie($username, $token);
544
+    }
545
+
546
+    /**
547
+     * Remove cookie for "remember username"
548
+     */
549
+    public static function unsetMagicInCookie() {
550
+        self::getUserSession()->unsetMagicInCookie();
551
+    }
552
+
553
+    /**
554
+     * Returns the first active backend from self::$_usedBackends.
555
+     *
556
+     * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
557
+     */
558
+    private static function findFirstActiveUsedBackend() {
559
+        foreach (self::$_usedBackends as $backend) {
560
+            if ($backend instanceof OCP\Authentication\IApacheBackend) {
561
+                if ($backend->isSessionActive()) {
562
+                    return $backend;
563
+                }
564
+            }
565
+        }
566
+
567
+        return null;
568
+    }
570 569
 }
Please login to merge, or discard this patch.
lib/private/Settings/Personal/PersonalInfo.php 1 patch
Indentation   +216 added lines, -216 removed lines patch added patch discarded remove patch
@@ -39,246 +39,246 @@
 block discarded – undo
39 39
 use OCP\Settings\ISettings;
40 40
 
41 41
 class PersonalInfo implements ISettings {
42
-	/** @var IConfig */
43
-	private $config;
44
-	/** @var IUserManager */
45
-	private $userManager;
46
-	/** @var AccountManager */
47
-	private $accountManager;
48
-	/** @var IGroupManager */
49
-	private $groupManager;
50
-	/** @var IAppManager */
51
-	private $appManager;
52
-	/** @var IFactory */
53
-	private $l10nFactory;
42
+    /** @var IConfig */
43
+    private $config;
44
+    /** @var IUserManager */
45
+    private $userManager;
46
+    /** @var AccountManager */
47
+    private $accountManager;
48
+    /** @var IGroupManager */
49
+    private $groupManager;
50
+    /** @var IAppManager */
51
+    private $appManager;
52
+    /** @var IFactory */
53
+    private $l10nFactory;
54 54
 
55
-	const COMMON_LANGUAGE_CODES = [
56
-		'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it',
57
-		'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
58
-	];
55
+    const COMMON_LANGUAGE_CODES = [
56
+        'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it',
57
+        'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
58
+    ];
59 59
 
60
-	/** @var IL10N */
61
-	private $l;
60
+    /** @var IL10N */
61
+    private $l;
62 62
 
63
-	/**
64
-	 * @param IConfig $config
65
-	 * @param IUserManager $userManager
66
-	 * @param IGroupManager $groupManager
67
-	 * @param AccountManager $accountManager
68
-	 * @param IFactory $l10nFactory
69
-	 * @param IL10N $l
70
-	 */
71
-	public function __construct(
72
-		IConfig $config,
73
-		IUserManager $userManager,
74
-		IGroupManager $groupManager,
75
-		AccountManager $accountManager,
76
-		IAppManager $appManager,
77
-		IFactory $l10nFactory,
78
-		IL10N $l
79
-	) {
80
-		$this->config = $config;
81
-		$this->userManager = $userManager;
82
-		$this->accountManager = $accountManager;
83
-		$this->groupManager = $groupManager;
84
-		$this->appManager = $appManager;
85
-		$this->l10nFactory = $l10nFactory;
86
-		$this->l = $l;
87
-	}
63
+    /**
64
+     * @param IConfig $config
65
+     * @param IUserManager $userManager
66
+     * @param IGroupManager $groupManager
67
+     * @param AccountManager $accountManager
68
+     * @param IFactory $l10nFactory
69
+     * @param IL10N $l
70
+     */
71
+    public function __construct(
72
+        IConfig $config,
73
+        IUserManager $userManager,
74
+        IGroupManager $groupManager,
75
+        AccountManager $accountManager,
76
+        IAppManager $appManager,
77
+        IFactory $l10nFactory,
78
+        IL10N $l
79
+    ) {
80
+        $this->config = $config;
81
+        $this->userManager = $userManager;
82
+        $this->accountManager = $accountManager;
83
+        $this->groupManager = $groupManager;
84
+        $this->appManager = $appManager;
85
+        $this->l10nFactory = $l10nFactory;
86
+        $this->l = $l;
87
+    }
88 88
 
89
-	/**
90
-	 * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
91
-	 * @since 9.1
92
-	 */
93
-	public function getForm() {
94
-		$federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
95
-		$lookupServerUploadEnabled = false;
96
-		if($federatedFileSharingEnabled) {
97
-			$federatedFileSharing = new Application();
98
-			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
99
-			$lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
100
-		}
89
+    /**
90
+     * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
91
+     * @since 9.1
92
+     */
93
+    public function getForm() {
94
+        $federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
95
+        $lookupServerUploadEnabled = false;
96
+        if($federatedFileSharingEnabled) {
97
+            $federatedFileSharing = new Application();
98
+            $shareProvider = $federatedFileSharing->getFederatedShareProvider();
99
+            $lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
100
+        }
101 101
 
102
-		$uid = \OC_User::getUser();
103
-		$user = $this->userManager->get($uid);
104
-		$userData = $this->accountManager->getUser($user);
102
+        $uid = \OC_User::getUser();
103
+        $user = $this->userManager->get($uid);
104
+        $userData = $this->accountManager->getUser($user);
105 105
 
106
-		$storageInfo = \OC_Helper::getStorageInfo('/');
107
-		if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
108
-			$totalSpace = $this->l->t('Unlimited');
109
-		} else {
110
-			$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
111
-		}
106
+        $storageInfo = \OC_Helper::getStorageInfo('/');
107
+        if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
108
+            $totalSpace = $this->l->t('Unlimited');
109
+        } else {
110
+            $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
111
+        }
112 112
 
113
-		$languageParameters = $this->getLanguages($user);
114
-		$messageParameters = $this->getMessageParameters($userData);
113
+        $languageParameters = $this->getLanguages($user);
114
+        $messageParameters = $this->getMessageParameters($userData);
115 115
 
116
-		$parameters = [
117
-			'total_space' => $totalSpace,
118
-			'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
119
-			'usage_relative' => $storageInfo['relative'],
120
-			'quota' => $storageInfo['quota'],
121
-			'avatarChangeSupported' => $user->canChangeAvatar(),
122
-			'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
123
-			'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'],
124
-			'displayNameChangeSupported' => $user->canChangeDisplayName(),
125
-			'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'],
126
-			'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
127
-			'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'],
128
-			'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'],
129
-			'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'],
130
-			'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'],
131
-			'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'],
132
-			'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'],
133
-			'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'],
134
-			'website' =>  $userData[AccountManager::PROPERTY_WEBSITE]['value'],
135
-			'websiteScope' =>  $userData[AccountManager::PROPERTY_WEBSITE]['scope'],
136
-			'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'],
137
-			'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'],
138
-			'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'],
139
-			'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'],
140
-			'groups' => $this->getGroups($user),
141
-			'passwordChangeSupported' => $user->canChangePassword(),
142
-		] + $messageParameters + $languageParameters;
116
+        $parameters = [
117
+            'total_space' => $totalSpace,
118
+            'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
119
+            'usage_relative' => $storageInfo['relative'],
120
+            'quota' => $storageInfo['quota'],
121
+            'avatarChangeSupported' => $user->canChangeAvatar(),
122
+            'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
123
+            'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'],
124
+            'displayNameChangeSupported' => $user->canChangeDisplayName(),
125
+            'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'],
126
+            'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
127
+            'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'],
128
+            'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'],
129
+            'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'],
130
+            'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'],
131
+            'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'],
132
+            'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'],
133
+            'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'],
134
+            'website' =>  $userData[AccountManager::PROPERTY_WEBSITE]['value'],
135
+            'websiteScope' =>  $userData[AccountManager::PROPERTY_WEBSITE]['scope'],
136
+            'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'],
137
+            'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'],
138
+            'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'],
139
+            'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'],
140
+            'groups' => $this->getGroups($user),
141
+            'passwordChangeSupported' => $user->canChangePassword(),
142
+        ] + $messageParameters + $languageParameters;
143 143
 
144 144
 
145
-		return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
146
-	}
145
+        return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
146
+    }
147 147
 
148
-	/**
149
-	 * @return string the section ID, e.g. 'sharing'
150
-	 * @since 9.1
151
-	 */
152
-	public function getSection() {
153
-		return 'personal-info';
154
-	}
148
+    /**
149
+     * @return string the section ID, e.g. 'sharing'
150
+     * @since 9.1
151
+     */
152
+    public function getSection() {
153
+        return 'personal-info';
154
+    }
155 155
 
156
-	/**
157
-	 * @return int whether the form should be rather on the top or bottom of
158
-	 * the admin section. The forms are arranged in ascending order of the
159
-	 * priority values. It is required to return a value between 0 and 100.
160
-	 *
161
-	 * E.g.: 70
162
-	 * @since 9.1
163
-	 */
164
-	public function getPriority() {
165
-		return 10;
166
-	}
156
+    /**
157
+     * @return int whether the form should be rather on the top or bottom of
158
+     * the admin section. The forms are arranged in ascending order of the
159
+     * priority values. It is required to return a value between 0 and 100.
160
+     *
161
+     * E.g.: 70
162
+     * @since 9.1
163
+     */
164
+    public function getPriority() {
165
+        return 10;
166
+    }
167 167
 
168
-	/**
169
-	 * returns a sorted list of the user's group GIDs
170
-	 *
171
-	 * @param IUser $user
172
-	 * @return array
173
-	 */
174
-	private function getGroups(IUser $user) {
175
-		$groups = array_map(
176
-			function(IGroup $group) {
177
-				return $group->getGID();
178
-			},
179
-			$this->groupManager->getUserGroups($user)
180
-		);
181
-		sort($groups);
168
+    /**
169
+     * returns a sorted list of the user's group GIDs
170
+     *
171
+     * @param IUser $user
172
+     * @return array
173
+     */
174
+    private function getGroups(IUser $user) {
175
+        $groups = array_map(
176
+            function(IGroup $group) {
177
+                return $group->getGID();
178
+            },
179
+            $this->groupManager->getUserGroups($user)
180
+        );
181
+        sort($groups);
182 182
 
183
-		return $groups;
184
-	}
183
+        return $groups;
184
+    }
185 185
 
186
-	/**
187
-	 * returns the user language, common language and other languages in an
188
-	 * associative array
189
-	 *
190
-	 * @param IUser $user
191
-	 * @return array
192
-	 */
193
-	private function getLanguages(IUser $user) {
194
-		$forceLanguage = $this->config->getSystemValue('force_language', false);
195
-		if($forceLanguage !== false) {
196
-			return [];
197
-		}
186
+    /**
187
+     * returns the user language, common language and other languages in an
188
+     * associative array
189
+     *
190
+     * @param IUser $user
191
+     * @return array
192
+     */
193
+    private function getLanguages(IUser $user) {
194
+        $forceLanguage = $this->config->getSystemValue('force_language', false);
195
+        if($forceLanguage !== false) {
196
+            return [];
197
+        }
198 198
 
199
-		$uid = $user->getUID();
199
+        $uid = $user->getUID();
200 200
 
201
-		$userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
202
-		$languageCodes = $this->l10nFactory->findAvailableLanguages();
201
+        $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
202
+        $languageCodes = $this->l10nFactory->findAvailableLanguages();
203 203
 
204
-		$commonLanguages = [];
205
-		$languages = [];
204
+        $commonLanguages = [];
205
+        $languages = [];
206 206
 
207
-		foreach($languageCodes as $lang) {
208
-			$l = \OC::$server->getL10N('settings', $lang);
209
-			// TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
210
-			$potentialName = (string) $l->t('__language_name__');
211
-			if($l->getLanguageCode() === $lang && substr($potentialName, 0, 1) !== '_') {//first check if the language name is in the translation file
212
-				$ln = array('code' => $lang, 'name' => $potentialName);
213
-			} elseif ($lang === 'en') {
214
-				$ln = ['code' => $lang, 'name' => 'English (US)'];
215
-			}else{//fallback to language code
216
-				$ln=array('code'=>$lang, 'name'=>$lang);
217
-			}
207
+        foreach($languageCodes as $lang) {
208
+            $l = \OC::$server->getL10N('settings', $lang);
209
+            // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
210
+            $potentialName = (string) $l->t('__language_name__');
211
+            if($l->getLanguageCode() === $lang && substr($potentialName, 0, 1) !== '_') {//first check if the language name is in the translation file
212
+                $ln = array('code' => $lang, 'name' => $potentialName);
213
+            } elseif ($lang === 'en') {
214
+                $ln = ['code' => $lang, 'name' => 'English (US)'];
215
+            }else{//fallback to language code
216
+                $ln=array('code'=>$lang, 'name'=>$lang);
217
+            }
218 218
 
219
-			// put appropriate languages into appropriate arrays, to print them sorted
220
-			// used language -> common languages -> divider -> other languages
221
-			if ($lang === $userLang) {
222
-				$userLang = $ln;
223
-			} elseif (in_array($lang, self::COMMON_LANGUAGE_CODES)) {
224
-				$commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)]=$ln;
225
-			} else {
226
-				$languages[]=$ln;
227
-			}
228
-		}
219
+            // put appropriate languages into appropriate arrays, to print them sorted
220
+            // used language -> common languages -> divider -> other languages
221
+            if ($lang === $userLang) {
222
+                $userLang = $ln;
223
+            } elseif (in_array($lang, self::COMMON_LANGUAGE_CODES)) {
224
+                $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)]=$ln;
225
+            } else {
226
+                $languages[]=$ln;
227
+            }
228
+        }
229 229
 
230
-		// if user language is not available but set somehow: show the actual code as name
231
-		if (!is_array($userLang)) {
232
-			$userLang = [
233
-				'code' => $userLang,
234
-				'name' => $userLang,
235
-			];
236
-		}
230
+        // if user language is not available but set somehow: show the actual code as name
231
+        if (!is_array($userLang)) {
232
+            $userLang = [
233
+                'code' => $userLang,
234
+                'name' => $userLang,
235
+            ];
236
+        }
237 237
 
238
-		ksort($commonLanguages);
238
+        ksort($commonLanguages);
239 239
 
240
-		// sort now by displayed language not the iso-code
241
-		usort( $languages, function ($a, $b) {
242
-			if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
243
-				// If a doesn't have a name, but b does, list b before a
244
-				return 1;
245
-			}
246
-			if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
247
-				// If a does have a name, but b doesn't, list a before b
248
-				return -1;
249
-			}
250
-			// Otherwise compare the names
251
-			return strcmp($a['name'], $b['name']);
252
-		});
240
+        // sort now by displayed language not the iso-code
241
+        usort( $languages, function ($a, $b) {
242
+            if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
243
+                // If a doesn't have a name, but b does, list b before a
244
+                return 1;
245
+            }
246
+            if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
247
+                // If a does have a name, but b doesn't, list a before b
248
+                return -1;
249
+            }
250
+            // Otherwise compare the names
251
+            return strcmp($a['name'], $b['name']);
252
+        });
253 253
 
254
-		return [
255
-			'activelanguage' => $userLang,
256
-			'commonlanguages' => $commonLanguages,
257
-			'languages' => $languages
258
-		];
259
-	}
254
+        return [
255
+            'activelanguage' => $userLang,
256
+            'commonlanguages' => $commonLanguages,
257
+            'languages' => $languages
258
+        ];
259
+    }
260 260
 
261
-	/**
262
-	 * @param array $userData
263
-	 * @return array
264
-	 */
265
-	private function getMessageParameters(array $userData) {
266
-		$needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER];
267
-		$messageParameters = [];
268
-		foreach ($needVerifyMessage as $property) {
269
-			switch ($userData[$property]['verified']) {
270
-				case AccountManager::VERIFIED:
271
-					$message = $this->l->t('Verifying');
272
-					break;
273
-				case AccountManager::VERIFICATION_IN_PROGRESS:
274
-					$message = $this->l->t('Verifying …');
275
-					break;
276
-				default:
277
-					$message = $this->l->t('Verify');
278
-			}
279
-			$messageParameters[$property . 'Message'] = $message;
280
-		}
281
-		return $messageParameters;
282
-	}
261
+    /**
262
+     * @param array $userData
263
+     * @return array
264
+     */
265
+    private function getMessageParameters(array $userData) {
266
+        $needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER];
267
+        $messageParameters = [];
268
+        foreach ($needVerifyMessage as $property) {
269
+            switch ($userData[$property]['verified']) {
270
+                case AccountManager::VERIFIED:
271
+                    $message = $this->l->t('Verifying');
272
+                    break;
273
+                case AccountManager::VERIFICATION_IN_PROGRESS:
274
+                    $message = $this->l->t('Verifying …');
275
+                    break;
276
+                default:
277
+                    $message = $this->l->t('Verify');
278
+            }
279
+            $messageParameters[$property . 'Message'] = $message;
280
+        }
281
+        return $messageParameters;
282
+    }
283 283
 
284 284
 }
Please login to merge, or discard this patch.