Completed
Pull Request — master (#4430)
by Morris
16:20 queued 04:45
created
lib/private/legacy/user.php 2 patches
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -94,7 +94,7 @@  discard block
 block discarded – undo
94 94
 				case 'database':
95 95
 				case 'mysql':
96 96
 				case 'sqlite':
97
-					\OCP\Util::writeLog('core', 'Adding user backend ' . $backend . '.', \OCP\Util::DEBUG);
97
+					\OCP\Util::writeLog('core', 'Adding user backend '.$backend.'.', \OCP\Util::DEBUG);
98 98
 					self::$_usedBackends[$backend] = new \OC\User\Database();
99 99
 					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
100 100
 					break;
@@ -103,8 +103,8 @@  discard block
 block discarded – undo
103 103
 					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
104 104
 					break;
105 105
 				default:
106
-					\OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG);
107
-					$className = 'OC_USER_' . strtoupper($backend);
106
+					\OCP\Util::writeLog('core', 'Adding default user backend '.$backend.'.', \OCP\Util::DEBUG);
107
+					$className = 'OC_USER_'.strtoupper($backend);
108 108
 					self::$_usedBackends[$backend] = new $className();
109 109
 					\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
110 110
 					break;
@@ -147,10 +147,10 @@  discard block
 block discarded – undo
147 147
 					self::useBackend($backend);
148 148
 					self::$_setupedBackends[] = $i;
149 149
 				} else {
150
-					\OCP\Util::writeLog('core', 'User backend ' . $class . ' already initialized.', \OCP\Util::DEBUG);
150
+					\OCP\Util::writeLog('core', 'User backend '.$class.' already initialized.', \OCP\Util::DEBUG);
151 151
 				}
152 152
 			} else {
153
-				\OCP\Util::writeLog('core', 'User backend ' . $class . ' not found.', \OCP\Util::ERROR);
153
+				\OCP\Util::writeLog('core', 'User backend '.$class.' not found.', \OCP\Util::ERROR);
154 154
 			}
155 155
 		}
156 156
 	}
@@ -188,15 +188,15 @@  discard block
 block discarded – undo
188 188
 			if (self::getUser() !== $uid) {
189 189
 				self::setUserId($uid);
190 190
 				$setUidAsDisplayName = true;
191
-				if($backend instanceof \OCP\UserInterface
191
+				if ($backend instanceof \OCP\UserInterface
192 192
 					&& $backend->implementsActions(OC_User_Backend::GET_DISPLAYNAME)) {
193 193
 
194 194
 					$backendDisplayName = $backend->getDisplayName($uid);
195
-					if(is_string($backendDisplayName) && trim($backendDisplayName) !== '') {
195
+					if (is_string($backendDisplayName) && trim($backendDisplayName) !== '') {
196 196
 						$setUidAsDisplayName = false;
197 197
 					}
198 198
 				}
199
-				if($setUidAsDisplayName) {
199
+				if ($setUidAsDisplayName) {
200 200
 					self::setDisplayName($uid);
201 201
 				}
202 202
 				$userSession = self::getUserSession();
@@ -494,7 +494,7 @@  discard block
 block discarded – undo
494 494
 		if ($user) {
495 495
 			return $user->getHome();
496 496
 		} else {
497
-			return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
497
+			return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT.'/data').'/'.$uid;
498 498
 		}
499 499
 	}
500 500
 
Please login to merge, or discard this patch.
Indentation   +560 added lines, -561 removed lines patch added patch discarded remove patch
@@ -57,565 +57,564 @@
 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 whether user can change his avatar
416
-	 *
417
-	 * @param string $uid The username
418
-	 * @return bool
419
-	 *
420
-	 * Check whether a specified user can change his avatar
421
-	 */
422
-	public static function canUserChangeAvatar($uid) {
423
-		$user = \OC::$server->getUserManager()->get($uid);
424
-		if ($user) {
425
-			return $user->canChangeAvatar();
426
-		} else {
427
-			return false;
428
-		}
429
-	}
430
-
431
-	/**
432
-	 * Check whether user can change his password
433
-	 *
434
-	 * @param string $uid The username
435
-	 * @return bool
436
-	 *
437
-	 * Check whether a specified user can change his password
438
-	 */
439
-	public static function canUserChangePassword($uid) {
440
-		$user = \OC::$server->getUserManager()->get($uid);
441
-		if ($user) {
442
-			return $user->canChangePassword();
443
-		} else {
444
-			return false;
445
-		}
446
-	}
447
-
448
-	/**
449
-	 * Check whether user can change his display name
450
-	 *
451
-	 * @param string $uid The username
452
-	 * @return bool
453
-	 *
454
-	 * Check whether a specified user can change his display name
455
-	 */
456
-	public static function canUserChangeDisplayName($uid) {
457
-		$user = \OC::$server->getUserManager()->get($uid);
458
-		if ($user) {
459
-			return $user->canChangeDisplayName();
460
-		} else {
461
-			return false;
462
-		}
463
-	}
464
-
465
-	/**
466
-	 * Check if the password is correct
467
-	 *
468
-	 * @param string $uid The username
469
-	 * @param string $password The password
470
-	 * @return string|false user id a string on success, false otherwise
471
-	 *
472
-	 * Check if the password is correct without logging in the user
473
-	 * returns the user id or false
474
-	 */
475
-	public static function checkPassword($uid, $password) {
476
-		$manager = \OC::$server->getUserManager();
477
-		$username = $manager->checkPassword($uid, $password);
478
-		if ($username !== false) {
479
-			return $username->getUID();
480
-		}
481
-		return false;
482
-	}
483
-
484
-	/**
485
-	 * @param string $uid The username
486
-	 * @return string
487
-	 *
488
-	 * returns the path to the users home directory
489
-	 * @deprecated Use \OC::$server->getUserManager->getHome()
490
-	 */
491
-	public static function getHome($uid) {
492
-		$user = \OC::$server->getUserManager()->get($uid);
493
-		if ($user) {
494
-			return $user->getHome();
495
-		} else {
496
-			return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
497
-		}
498
-	}
499
-
500
-	/**
501
-	 * Get a list of all users
502
-	 *
503
-	 * @return array an array of all uids
504
-	 *
505
-	 * Get a list of all users.
506
-	 * @param string $search
507
-	 * @param integer $limit
508
-	 * @param integer $offset
509
-	 */
510
-	public static function getUsers($search = '', $limit = null, $offset = null) {
511
-		$users = \OC::$server->getUserManager()->search($search, $limit, $offset);
512
-		$uids = array();
513
-		foreach ($users as $user) {
514
-			$uids[] = $user->getUID();
515
-		}
516
-		return $uids;
517
-	}
518
-
519
-	/**
520
-	 * Get a list of all users display name
521
-	 *
522
-	 * @param string $search
523
-	 * @param int $limit
524
-	 * @param int $offset
525
-	 * @return array associative array with all display names (value) and corresponding uids (key)
526
-	 *
527
-	 * Get a list of all display names and user ids.
528
-	 * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
529
-	 */
530
-	public static function getDisplayNames($search = '', $limit = null, $offset = null) {
531
-		$displayNames = array();
532
-		$users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
533
-		foreach ($users as $user) {
534
-			$displayNames[$user->getUID()] = $user->getDisplayName();
535
-		}
536
-		return $displayNames;
537
-	}
538
-
539
-	/**
540
-	 * check if a user exists
541
-	 *
542
-	 * @param string $uid the username
543
-	 * @return boolean
544
-	 */
545
-	public static function userExists($uid) {
546
-		return \OC::$server->getUserManager()->userExists($uid);
547
-	}
548
-
549
-	/**
550
-	 * disables a user
551
-	 *
552
-	 * @param string $uid the user to disable
553
-	 */
554
-	public static function disableUser($uid) {
555
-		$user = \OC::$server->getUserManager()->get($uid);
556
-		if ($user) {
557
-			$user->setEnabled(false);
558
-		}
559
-	}
560
-
561
-	/**
562
-	 * enable a user
563
-	 *
564
-	 * @param string $uid
565
-	 */
566
-	public static function enableUser($uid) {
567
-		$user = \OC::$server->getUserManager()->get($uid);
568
-		if ($user) {
569
-			$user->setEnabled(true);
570
-		}
571
-	}
572
-
573
-	/**
574
-	 * checks if a user is enabled
575
-	 *
576
-	 * @param string $uid
577
-	 * @return bool
578
-	 */
579
-	public static function isEnabled($uid) {
580
-		$user = \OC::$server->getUserManager()->get($uid);
581
-		if ($user) {
582
-			return $user->isEnabled();
583
-		} else {
584
-			return false;
585
-		}
586
-	}
587
-
588
-	/**
589
-	 * Set cookie value to use in next page load
590
-	 *
591
-	 * @param string $username username to be set
592
-	 * @param string $token
593
-	 */
594
-	public static function setMagicInCookie($username, $token) {
595
-		self::getUserSession()->setMagicInCookie($username, $token);
596
-	}
597
-
598
-	/**
599
-	 * Remove cookie for "remember username"
600
-	 */
601
-	public static function unsetMagicInCookie() {
602
-		self::getUserSession()->unsetMagicInCookie();
603
-	}
604
-
605
-	/**
606
-	 * Returns the first active backend from self::$_usedBackends.
607
-	 *
608
-	 * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
609
-	 */
610
-	private static function findFirstActiveUsedBackend() {
611
-		foreach (self::$_usedBackends as $backend) {
612
-			if ($backend instanceof OCP\Authentication\IApacheBackend) {
613
-				if ($backend->isSessionActive()) {
614
-					return $backend;
615
-				}
616
-			}
617
-		}
618
-
619
-		return null;
620
-	}
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 whether user can change his avatar
415
+     *
416
+     * @param string $uid The username
417
+     * @return bool
418
+     *
419
+     * Check whether a specified user can change his avatar
420
+     */
421
+    public static function canUserChangeAvatar($uid) {
422
+        $user = \OC::$server->getUserManager()->get($uid);
423
+        if ($user) {
424
+            return $user->canChangeAvatar();
425
+        } else {
426
+            return false;
427
+        }
428
+    }
429
+
430
+    /**
431
+     * Check whether user can change his password
432
+     *
433
+     * @param string $uid The username
434
+     * @return bool
435
+     *
436
+     * Check whether a specified user can change his password
437
+     */
438
+    public static function canUserChangePassword($uid) {
439
+        $user = \OC::$server->getUserManager()->get($uid);
440
+        if ($user) {
441
+            return $user->canChangePassword();
442
+        } else {
443
+            return false;
444
+        }
445
+    }
446
+
447
+    /**
448
+     * Check whether user can change his display name
449
+     *
450
+     * @param string $uid The username
451
+     * @return bool
452
+     *
453
+     * Check whether a specified user can change his display name
454
+     */
455
+    public static function canUserChangeDisplayName($uid) {
456
+        $user = \OC::$server->getUserManager()->get($uid);
457
+        if ($user) {
458
+            return $user->canChangeDisplayName();
459
+        } else {
460
+            return false;
461
+        }
462
+    }
463
+
464
+    /**
465
+     * Check if the password is correct
466
+     *
467
+     * @param string $uid The username
468
+     * @param string $password The password
469
+     * @return string|false user id a string on success, false otherwise
470
+     *
471
+     * Check if the password is correct without logging in the user
472
+     * returns the user id or false
473
+     */
474
+    public static function checkPassword($uid, $password) {
475
+        $manager = \OC::$server->getUserManager();
476
+        $username = $manager->checkPassword($uid, $password);
477
+        if ($username !== false) {
478
+            return $username->getUID();
479
+        }
480
+        return false;
481
+    }
482
+
483
+    /**
484
+     * @param string $uid The username
485
+     * @return string
486
+     *
487
+     * returns the path to the users home directory
488
+     * @deprecated Use \OC::$server->getUserManager->getHome()
489
+     */
490
+    public static function getHome($uid) {
491
+        $user = \OC::$server->getUserManager()->get($uid);
492
+        if ($user) {
493
+            return $user->getHome();
494
+        } else {
495
+            return \OC::$server->getSystemConfig()->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid;
496
+        }
497
+    }
498
+
499
+    /**
500
+     * Get a list of all users
501
+     *
502
+     * @return array an array of all uids
503
+     *
504
+     * Get a list of all users.
505
+     * @param string $search
506
+     * @param integer $limit
507
+     * @param integer $offset
508
+     */
509
+    public static function getUsers($search = '', $limit = null, $offset = null) {
510
+        $users = \OC::$server->getUserManager()->search($search, $limit, $offset);
511
+        $uids = array();
512
+        foreach ($users as $user) {
513
+            $uids[] = $user->getUID();
514
+        }
515
+        return $uids;
516
+    }
517
+
518
+    /**
519
+     * Get a list of all users display name
520
+     *
521
+     * @param string $search
522
+     * @param int $limit
523
+     * @param int $offset
524
+     * @return array associative array with all display names (value) and corresponding uids (key)
525
+     *
526
+     * Get a list of all display names and user ids.
527
+     * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead.
528
+     */
529
+    public static function getDisplayNames($search = '', $limit = null, $offset = null) {
530
+        $displayNames = array();
531
+        $users = \OC::$server->getUserManager()->searchDisplayName($search, $limit, $offset);
532
+        foreach ($users as $user) {
533
+            $displayNames[$user->getUID()] = $user->getDisplayName();
534
+        }
535
+        return $displayNames;
536
+    }
537
+
538
+    /**
539
+     * check if a user exists
540
+     *
541
+     * @param string $uid the username
542
+     * @return boolean
543
+     */
544
+    public static function userExists($uid) {
545
+        return \OC::$server->getUserManager()->userExists($uid);
546
+    }
547
+
548
+    /**
549
+     * disables a user
550
+     *
551
+     * @param string $uid the user to disable
552
+     */
553
+    public static function disableUser($uid) {
554
+        $user = \OC::$server->getUserManager()->get($uid);
555
+        if ($user) {
556
+            $user->setEnabled(false);
557
+        }
558
+    }
559
+
560
+    /**
561
+     * enable a user
562
+     *
563
+     * @param string $uid
564
+     */
565
+    public static function enableUser($uid) {
566
+        $user = \OC::$server->getUserManager()->get($uid);
567
+        if ($user) {
568
+            $user->setEnabled(true);
569
+        }
570
+    }
571
+
572
+    /**
573
+     * checks if a user is enabled
574
+     *
575
+     * @param string $uid
576
+     * @return bool
577
+     */
578
+    public static function isEnabled($uid) {
579
+        $user = \OC::$server->getUserManager()->get($uid);
580
+        if ($user) {
581
+            return $user->isEnabled();
582
+        } else {
583
+            return false;
584
+        }
585
+    }
586
+
587
+    /**
588
+     * Set cookie value to use in next page load
589
+     *
590
+     * @param string $username username to be set
591
+     * @param string $token
592
+     */
593
+    public static function setMagicInCookie($username, $token) {
594
+        self::getUserSession()->setMagicInCookie($username, $token);
595
+    }
596
+
597
+    /**
598
+     * Remove cookie for "remember username"
599
+     */
600
+    public static function unsetMagicInCookie() {
601
+        self::getUserSession()->unsetMagicInCookie();
602
+    }
603
+
604
+    /**
605
+     * Returns the first active backend from self::$_usedBackends.
606
+     *
607
+     * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend
608
+     */
609
+    private static function findFirstActiveUsedBackend() {
610
+        foreach (self::$_usedBackends as $backend) {
611
+            if ($backend instanceof OCP\Authentication\IApacheBackend) {
612
+                if ($backend->isSessionActive()) {
613
+                    return $backend;
614
+                }
615
+            }
616
+        }
617
+
618
+        return null;
619
+    }
621 620
 }
Please login to merge, or discard this patch.
lib/private/legacy/api.php 1 patch
Indentation   +458 added lines, -458 removed lines patch added patch discarded remove patch
@@ -37,462 +37,462 @@
 block discarded – undo
37 37
 
38 38
 class OC_API {
39 39
 
40
-	/**
41
-	 * API authentication levels
42
-	 */
43
-
44
-	/** @deprecated Use \OCP\API::GUEST_AUTH instead */
45
-	const GUEST_AUTH = 0;
46
-
47
-	/** @deprecated Use \OCP\API::USER_AUTH instead */
48
-	const USER_AUTH = 1;
49
-
50
-	/** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */
51
-	const SUBADMIN_AUTH = 2;
52
-
53
-	/** @deprecated Use \OCP\API::ADMIN_AUTH instead */
54
-	const ADMIN_AUTH = 3;
55
-
56
-	/**
57
-	 * API Response Codes
58
-	 */
59
-
60
-	/** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */
61
-	const RESPOND_UNAUTHORISED = 997;
62
-
63
-	/** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */
64
-	const RESPOND_SERVER_ERROR = 996;
65
-
66
-	/** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */
67
-	const RESPOND_NOT_FOUND = 998;
68
-
69
-	/** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */
70
-	const RESPOND_UNKNOWN_ERROR = 999;
71
-
72
-	/**
73
-	 * api actions
74
-	 */
75
-	protected static $actions = array();
76
-	private static $logoutRequired = false;
77
-	private static $isLoggedIn = false;
78
-
79
-	/**
80
-	 * registers an api call
81
-	 * @param string $method the http method
82
-	 * @param string $url the url to match
83
-	 * @param callable $action the function to run
84
-	 * @param string $app the id of the app registering the call
85
-	 * @param int $authLevel the level of authentication required for the call
86
-	 * @param array $defaults
87
-	 * @param array $requirements
88
-	 */
89
-	public static function register($method, $url, $action, $app,
90
-				$authLevel = API::USER_AUTH,
91
-				$defaults = array(),
92
-				$requirements = array()) {
93
-		$name = strtolower($method).$url;
94
-		$name = str_replace(array('/', '{', '}'), '_', $name);
95
-		if(!isset(self::$actions[$name])) {
96
-			$oldCollection = OC::$server->getRouter()->getCurrentCollection();
97
-			OC::$server->getRouter()->useCollection('ocs');
98
-			OC::$server->getRouter()->create($name, $url)
99
-				->method($method)
100
-				->defaults($defaults)
101
-				->requirements($requirements)
102
-				->action('OC_API', 'call');
103
-			self::$actions[$name] = array();
104
-			OC::$server->getRouter()->useCollection($oldCollection);
105
-		}
106
-		self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel);
107
-	}
108
-
109
-	/**
110
-	 * handles an api call
111
-	 * @param array $parameters
112
-	 */
113
-	public static function call($parameters) {
114
-		$request = \OC::$server->getRequest();
115
-		$method = $request->getMethod();
116
-
117
-		// Prepare the request variables
118
-		if($method === 'PUT') {
119
-			$parameters['_put'] = $request->getParams();
120
-		} else if($method === 'DELETE') {
121
-			$parameters['_delete'] = $request->getParams();
122
-		}
123
-		$name = $parameters['_route'];
124
-		// Foreach registered action
125
-		$responses = array();
126
-		foreach(self::$actions[$name] as $action) {
127
-			// Check authentication and availability
128
-			if(!self::isAuthorised($action)) {
129
-				$responses[] = array(
130
-					'app' => $action['app'],
131
-					'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'),
132
-					'shipped' => OC_App::isShipped($action['app']),
133
-					);
134
-				continue;
135
-			}
136
-			if(!is_callable($action['action'])) {
137
-				$responses[] = array(
138
-					'app' => $action['app'],
139
-					'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'),
140
-					'shipped' => OC_App::isShipped($action['app']),
141
-					);
142
-				continue;
143
-			}
144
-			// Run the action
145
-			$responses[] = array(
146
-				'app' => $action['app'],
147
-				'response' => call_user_func($action['action'], $parameters),
148
-				'shipped' => OC_App::isShipped($action['app']),
149
-				);
150
-		}
151
-		$response = self::mergeResponses($responses);
152
-		$format = self::requestedFormat();
153
-		if (self::$logoutRequired) {
154
-			\OC::$server->getUserSession()->logout();
155
-		}
156
-
157
-		self::respond($response, $format);
158
-	}
159
-
160
-	/**
161
-	 * merge the returned result objects into one response
162
-	 * @param array $responses
163
-	 * @return OC_OCS_Result
164
-	 */
165
-	public static function mergeResponses($responses) {
166
-		// Sort into shipped and third-party
167
-		$shipped = array(
168
-			'succeeded' => array(),
169
-			'failed' => array(),
170
-			);
171
-		$thirdparty = array(
172
-			'succeeded' => array(),
173
-			'failed' => array(),
174
-			);
175
-
176
-		foreach($responses as $response) {
177
-			if($response['shipped'] || ($response['app'] === 'core')) {
178
-				if($response['response']->succeeded()) {
179
-					$shipped['succeeded'][$response['app']] = $response;
180
-				} else {
181
-					$shipped['failed'][$response['app']] = $response;
182
-				}
183
-			} else {
184
-				if($response['response']->succeeded()) {
185
-					$thirdparty['succeeded'][$response['app']] = $response;
186
-				} else {
187
-					$thirdparty['failed'][$response['app']] = $response;
188
-				}
189
-			}
190
-		}
191
-
192
-		// Remove any error responses if there is one shipped response that succeeded
193
-		if(!empty($shipped['failed'])) {
194
-			// Which shipped response do we use if they all failed?
195
-			// They may have failed for different reasons (different status codes)
196
-			// Which response code should we return?
197
-			// Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR
198
-			// Merge failed responses if more than one
199
-			$data = array();
200
-			foreach($shipped['failed'] as $failure) {
201
-				$data = array_merge_recursive($data, $failure['response']->getData());
202
-			}
203
-			$picked = reset($shipped['failed']);
204
-			$code = $picked['response']->getStatusCode();
205
-			$meta = $picked['response']->getMeta();
206
-			$headers = $picked['response']->getHeaders();
207
-			$response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
208
-			return $response;
209
-		} elseif(!empty($shipped['succeeded'])) {
210
-			$responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
211
-		} elseif(!empty($thirdparty['failed'])) {
212
-			// Merge failed responses if more than one
213
-			$data = array();
214
-			foreach($thirdparty['failed'] as $failure) {
215
-				$data = array_merge_recursive($data, $failure['response']->getData());
216
-			}
217
-			$picked = reset($thirdparty['failed']);
218
-			$code = $picked['response']->getStatusCode();
219
-			$meta = $picked['response']->getMeta();
220
-			$headers = $picked['response']->getHeaders();
221
-			$response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
222
-			return $response;
223
-		} else {
224
-			$responses = $thirdparty['succeeded'];
225
-		}
226
-		// Merge the successful responses
227
-		$data = [];
228
-		$codes = [];
229
-		$header = [];
230
-
231
-		foreach($responses as $response) {
232
-			if($response['shipped']) {
233
-				$data = array_merge_recursive($response['response']->getData(), $data);
234
-			} else {
235
-				$data = array_merge_recursive($data, $response['response']->getData());
236
-			}
237
-			$header = array_merge_recursive($header, $response['response']->getHeaders());
238
-			$codes[] = ['code' => $response['response']->getStatusCode(),
239
-				'meta' => $response['response']->getMeta()];
240
-		}
241
-
242
-		// Use any non 100 status codes
243
-		$statusCode = 100;
244
-		$statusMessage = null;
245
-		foreach($codes as $code) {
246
-			if($code['code'] != 100) {
247
-				$statusCode = $code['code'];
248
-				$statusMessage = $code['meta']['message'];
249
-				break;
250
-			}
251
-		}
252
-
253
-		return new OC_OCS_Result($data, $statusCode, $statusMessage, $header);
254
-	}
255
-
256
-	/**
257
-	 * authenticate the api call
258
-	 * @param array $action the action details as supplied to OC_API::register()
259
-	 * @return bool
260
-	 */
261
-	private static function isAuthorised($action) {
262
-		$level = $action['authlevel'];
263
-		switch($level) {
264
-			case API::GUEST_AUTH:
265
-				// Anyone can access
266
-				return true;
267
-			case API::USER_AUTH:
268
-				// User required
269
-				return self::loginUser();
270
-			case API::SUBADMIN_AUTH:
271
-				// Check for subadmin
272
-				$user = self::loginUser();
273
-				if(!$user) {
274
-					return false;
275
-				} else {
276
-					$userObject = \OC::$server->getUserSession()->getUser();
277
-					if($userObject === null) {
278
-						return false;
279
-					}
280
-					$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
281
-					$admin = OC_User::isAdminUser($user);
282
-					if($isSubAdmin || $admin) {
283
-						return true;
284
-					} else {
285
-						return false;
286
-					}
287
-				}
288
-			case API::ADMIN_AUTH:
289
-				// Check for admin
290
-				$user = self::loginUser();
291
-				if(!$user) {
292
-					return false;
293
-				} else {
294
-					return OC_User::isAdminUser($user);
295
-				}
296
-			default:
297
-				// oops looks like invalid level supplied
298
-				return false;
299
-		}
300
-	}
301
-
302
-	/**
303
-	 * http basic auth
304
-	 * @return string|false (username, or false on failure)
305
-	 */
306
-	private static function loginUser() {
307
-		if(self::$isLoggedIn === true) {
308
-			return \OC_User::getUser();
309
-		}
310
-
311
-		// reuse existing login
312
-		$loggedIn = \OC::$server->getUserSession()->isLoggedIn();
313
-		if ($loggedIn === true) {
314
-			if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
315
-				// Do not allow access to OCS until the 2FA challenge was solved successfully
316
-				return false;
317
-			}
318
-			$ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false;
319
-			if ($ocsApiRequest) {
320
-
321
-				// initialize the user's filesystem
322
-				\OC_Util::setupFS(\OC_User::getUser());
323
-				self::$isLoggedIn = true;
324
-
325
-				return OC_User::getUser();
326
-			}
327
-			return false;
328
-		}
329
-
330
-		// basic auth - because OC_User::login will create a new session we shall only try to login
331
-		// if user and pass are set
332
-		$userSession = \OC::$server->getUserSession();
333
-		$request = \OC::$server->getRequest();
334
-		try {
335
-			if ($userSession->tryTokenLogin($request)
336
-				|| $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) {
337
-				self::$logoutRequired = true;
338
-			} else {
339
-				return false;
340
-			}
341
-			// initialize the user's filesystem
342
-			\OC_Util::setupFS(\OC_User::getUser());
343
-			self::$isLoggedIn = true;
344
-
345
-			return \OC_User::getUser();
346
-		} catch (\OC\User\LoginException $e) {
347
-			return false;
348
-		}
349
-	}
350
-
351
-	/**
352
-	 * respond to a call
353
-	 * @param OC_OCS_Result $result
354
-	 * @param string $format the format xml|json
355
-	 */
356
-	public static function respond($result, $format='xml') {
357
-		$request = \OC::$server->getRequest();
358
-
359
-		// Send 401 headers if unauthorised
360
-		if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
361
-			// If request comes from JS return dummy auth request
362
-			if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
363
-				header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
364
-			} else {
365
-				header('WWW-Authenticate: Basic realm="Authorisation Required"');
366
-			}
367
-			header('HTTP/1.0 401 Unauthorized');
368
-		}
369
-
370
-		foreach($result->getHeaders() as $name => $value) {
371
-			header($name . ': ' . $value);
372
-		}
373
-
374
-		$meta = $result->getMeta();
375
-		$data = $result->getData();
376
-		if (self::isV2($request)) {
377
-			$statusCode = self::mapStatusCodes($result->getStatusCode());
378
-			if (!is_null($statusCode)) {
379
-				$meta['statuscode'] = $statusCode;
380
-				OC_Response::setStatus($statusCode);
381
-			}
382
-		}
383
-
384
-		self::setContentType($format);
385
-		$body = self::renderResult($format, $meta, $data);
386
-		echo $body;
387
-	}
388
-
389
-	/**
390
-	 * @param XMLWriter $writer
391
-	 */
392
-	private static function toXML($array, $writer) {
393
-		foreach($array as $k => $v) {
394
-			if ($k[0] === '@') {
395
-				$writer->writeAttribute(substr($k, 1), $v);
396
-				continue;
397
-			} else if (is_numeric($k)) {
398
-				$k = 'element';
399
-			}
400
-			if(is_array($v)) {
401
-				$writer->startElement($k);
402
-				self::toXML($v, $writer);
403
-				$writer->endElement();
404
-			} else {
405
-				$writer->writeElement($k, $v);
406
-			}
407
-		}
408
-	}
409
-
410
-	/**
411
-	 * @return string
412
-	 */
413
-	public static function requestedFormat() {
414
-		$formats = array('json', 'xml');
415
-
416
-		$format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
417
-		return $format;
418
-	}
419
-
420
-	/**
421
-	 * Based on the requested format the response content type is set
422
-	 * @param string $format
423
-	 */
424
-	public static function setContentType($format = null) {
425
-		$format = is_null($format) ? self::requestedFormat() : $format;
426
-		if ($format === 'xml') {
427
-			header('Content-type: text/xml; charset=UTF-8');
428
-			return;
429
-		}
430
-
431
-		if ($format === 'json') {
432
-			header('Content-Type: application/json; charset=utf-8');
433
-			return;
434
-		}
435
-
436
-		header('Content-Type: application/octet-stream; charset=utf-8');
437
-	}
438
-
439
-	/**
440
-	 * @param \OCP\IRequest $request
441
-	 * @return bool
442
-	 */
443
-	protected static function isV2(\OCP\IRequest $request) {
444
-		$script = $request->getScriptName();
445
-
446
-		return substr($script, -11) === '/ocs/v2.php';
447
-	}
448
-
449
-	/**
450
-	 * @param integer $sc
451
-	 * @return int
452
-	 */
453
-	public static function mapStatusCodes($sc) {
454
-		switch ($sc) {
455
-			case API::RESPOND_NOT_FOUND:
456
-				return Http::STATUS_NOT_FOUND;
457
-			case API::RESPOND_SERVER_ERROR:
458
-				return Http::STATUS_INTERNAL_SERVER_ERROR;
459
-			case API::RESPOND_UNKNOWN_ERROR:
460
-				return Http::STATUS_INTERNAL_SERVER_ERROR;
461
-			case API::RESPOND_UNAUTHORISED:
462
-				// already handled for v1
463
-				return null;
464
-			case 100:
465
-				return Http::STATUS_OK;
466
-		}
467
-		// any 2xx, 4xx and 5xx will be used as is
468
-		if ($sc >= 200 && $sc < 600) {
469
-			return $sc;
470
-		}
471
-
472
-		return Http::STATUS_BAD_REQUEST;
473
-	}
474
-
475
-	/**
476
-	 * @param string $format
477
-	 * @return string
478
-	 */
479
-	public static function renderResult($format, $meta, $data) {
480
-		$response = array(
481
-			'ocs' => array(
482
-				'meta' => $meta,
483
-				'data' => $data,
484
-			),
485
-		);
486
-		if ($format == 'json') {
487
-			return OC_JSON::encode($response);
488
-		}
489
-
490
-		$writer = new XMLWriter();
491
-		$writer->openMemory();
492
-		$writer->setIndent(true);
493
-		$writer->startDocument();
494
-		self::toXML($response, $writer);
495
-		$writer->endDocument();
496
-		return $writer->outputMemory(true);
497
-	}
40
+    /**
41
+     * API authentication levels
42
+     */
43
+
44
+    /** @deprecated Use \OCP\API::GUEST_AUTH instead */
45
+    const GUEST_AUTH = 0;
46
+
47
+    /** @deprecated Use \OCP\API::USER_AUTH instead */
48
+    const USER_AUTH = 1;
49
+
50
+    /** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */
51
+    const SUBADMIN_AUTH = 2;
52
+
53
+    /** @deprecated Use \OCP\API::ADMIN_AUTH instead */
54
+    const ADMIN_AUTH = 3;
55
+
56
+    /**
57
+     * API Response Codes
58
+     */
59
+
60
+    /** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */
61
+    const RESPOND_UNAUTHORISED = 997;
62
+
63
+    /** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */
64
+    const RESPOND_SERVER_ERROR = 996;
65
+
66
+    /** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */
67
+    const RESPOND_NOT_FOUND = 998;
68
+
69
+    /** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */
70
+    const RESPOND_UNKNOWN_ERROR = 999;
71
+
72
+    /**
73
+     * api actions
74
+     */
75
+    protected static $actions = array();
76
+    private static $logoutRequired = false;
77
+    private static $isLoggedIn = false;
78
+
79
+    /**
80
+     * registers an api call
81
+     * @param string $method the http method
82
+     * @param string $url the url to match
83
+     * @param callable $action the function to run
84
+     * @param string $app the id of the app registering the call
85
+     * @param int $authLevel the level of authentication required for the call
86
+     * @param array $defaults
87
+     * @param array $requirements
88
+     */
89
+    public static function register($method, $url, $action, $app,
90
+                $authLevel = API::USER_AUTH,
91
+                $defaults = array(),
92
+                $requirements = array()) {
93
+        $name = strtolower($method).$url;
94
+        $name = str_replace(array('/', '{', '}'), '_', $name);
95
+        if(!isset(self::$actions[$name])) {
96
+            $oldCollection = OC::$server->getRouter()->getCurrentCollection();
97
+            OC::$server->getRouter()->useCollection('ocs');
98
+            OC::$server->getRouter()->create($name, $url)
99
+                ->method($method)
100
+                ->defaults($defaults)
101
+                ->requirements($requirements)
102
+                ->action('OC_API', 'call');
103
+            self::$actions[$name] = array();
104
+            OC::$server->getRouter()->useCollection($oldCollection);
105
+        }
106
+        self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel);
107
+    }
108
+
109
+    /**
110
+     * handles an api call
111
+     * @param array $parameters
112
+     */
113
+    public static function call($parameters) {
114
+        $request = \OC::$server->getRequest();
115
+        $method = $request->getMethod();
116
+
117
+        // Prepare the request variables
118
+        if($method === 'PUT') {
119
+            $parameters['_put'] = $request->getParams();
120
+        } else if($method === 'DELETE') {
121
+            $parameters['_delete'] = $request->getParams();
122
+        }
123
+        $name = $parameters['_route'];
124
+        // Foreach registered action
125
+        $responses = array();
126
+        foreach(self::$actions[$name] as $action) {
127
+            // Check authentication and availability
128
+            if(!self::isAuthorised($action)) {
129
+                $responses[] = array(
130
+                    'app' => $action['app'],
131
+                    'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'),
132
+                    'shipped' => OC_App::isShipped($action['app']),
133
+                    );
134
+                continue;
135
+            }
136
+            if(!is_callable($action['action'])) {
137
+                $responses[] = array(
138
+                    'app' => $action['app'],
139
+                    'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'),
140
+                    'shipped' => OC_App::isShipped($action['app']),
141
+                    );
142
+                continue;
143
+            }
144
+            // Run the action
145
+            $responses[] = array(
146
+                'app' => $action['app'],
147
+                'response' => call_user_func($action['action'], $parameters),
148
+                'shipped' => OC_App::isShipped($action['app']),
149
+                );
150
+        }
151
+        $response = self::mergeResponses($responses);
152
+        $format = self::requestedFormat();
153
+        if (self::$logoutRequired) {
154
+            \OC::$server->getUserSession()->logout();
155
+        }
156
+
157
+        self::respond($response, $format);
158
+    }
159
+
160
+    /**
161
+     * merge the returned result objects into one response
162
+     * @param array $responses
163
+     * @return OC_OCS_Result
164
+     */
165
+    public static function mergeResponses($responses) {
166
+        // Sort into shipped and third-party
167
+        $shipped = array(
168
+            'succeeded' => array(),
169
+            'failed' => array(),
170
+            );
171
+        $thirdparty = array(
172
+            'succeeded' => array(),
173
+            'failed' => array(),
174
+            );
175
+
176
+        foreach($responses as $response) {
177
+            if($response['shipped'] || ($response['app'] === 'core')) {
178
+                if($response['response']->succeeded()) {
179
+                    $shipped['succeeded'][$response['app']] = $response;
180
+                } else {
181
+                    $shipped['failed'][$response['app']] = $response;
182
+                }
183
+            } else {
184
+                if($response['response']->succeeded()) {
185
+                    $thirdparty['succeeded'][$response['app']] = $response;
186
+                } else {
187
+                    $thirdparty['failed'][$response['app']] = $response;
188
+                }
189
+            }
190
+        }
191
+
192
+        // Remove any error responses if there is one shipped response that succeeded
193
+        if(!empty($shipped['failed'])) {
194
+            // Which shipped response do we use if they all failed?
195
+            // They may have failed for different reasons (different status codes)
196
+            // Which response code should we return?
197
+            // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR
198
+            // Merge failed responses if more than one
199
+            $data = array();
200
+            foreach($shipped['failed'] as $failure) {
201
+                $data = array_merge_recursive($data, $failure['response']->getData());
202
+            }
203
+            $picked = reset($shipped['failed']);
204
+            $code = $picked['response']->getStatusCode();
205
+            $meta = $picked['response']->getMeta();
206
+            $headers = $picked['response']->getHeaders();
207
+            $response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
208
+            return $response;
209
+        } elseif(!empty($shipped['succeeded'])) {
210
+            $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
211
+        } elseif(!empty($thirdparty['failed'])) {
212
+            // Merge failed responses if more than one
213
+            $data = array();
214
+            foreach($thirdparty['failed'] as $failure) {
215
+                $data = array_merge_recursive($data, $failure['response']->getData());
216
+            }
217
+            $picked = reset($thirdparty['failed']);
218
+            $code = $picked['response']->getStatusCode();
219
+            $meta = $picked['response']->getMeta();
220
+            $headers = $picked['response']->getHeaders();
221
+            $response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
222
+            return $response;
223
+        } else {
224
+            $responses = $thirdparty['succeeded'];
225
+        }
226
+        // Merge the successful responses
227
+        $data = [];
228
+        $codes = [];
229
+        $header = [];
230
+
231
+        foreach($responses as $response) {
232
+            if($response['shipped']) {
233
+                $data = array_merge_recursive($response['response']->getData(), $data);
234
+            } else {
235
+                $data = array_merge_recursive($data, $response['response']->getData());
236
+            }
237
+            $header = array_merge_recursive($header, $response['response']->getHeaders());
238
+            $codes[] = ['code' => $response['response']->getStatusCode(),
239
+                'meta' => $response['response']->getMeta()];
240
+        }
241
+
242
+        // Use any non 100 status codes
243
+        $statusCode = 100;
244
+        $statusMessage = null;
245
+        foreach($codes as $code) {
246
+            if($code['code'] != 100) {
247
+                $statusCode = $code['code'];
248
+                $statusMessage = $code['meta']['message'];
249
+                break;
250
+            }
251
+        }
252
+
253
+        return new OC_OCS_Result($data, $statusCode, $statusMessage, $header);
254
+    }
255
+
256
+    /**
257
+     * authenticate the api call
258
+     * @param array $action the action details as supplied to OC_API::register()
259
+     * @return bool
260
+     */
261
+    private static function isAuthorised($action) {
262
+        $level = $action['authlevel'];
263
+        switch($level) {
264
+            case API::GUEST_AUTH:
265
+                // Anyone can access
266
+                return true;
267
+            case API::USER_AUTH:
268
+                // User required
269
+                return self::loginUser();
270
+            case API::SUBADMIN_AUTH:
271
+                // Check for subadmin
272
+                $user = self::loginUser();
273
+                if(!$user) {
274
+                    return false;
275
+                } else {
276
+                    $userObject = \OC::$server->getUserSession()->getUser();
277
+                    if($userObject === null) {
278
+                        return false;
279
+                    }
280
+                    $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
281
+                    $admin = OC_User::isAdminUser($user);
282
+                    if($isSubAdmin || $admin) {
283
+                        return true;
284
+                    } else {
285
+                        return false;
286
+                    }
287
+                }
288
+            case API::ADMIN_AUTH:
289
+                // Check for admin
290
+                $user = self::loginUser();
291
+                if(!$user) {
292
+                    return false;
293
+                } else {
294
+                    return OC_User::isAdminUser($user);
295
+                }
296
+            default:
297
+                // oops looks like invalid level supplied
298
+                return false;
299
+        }
300
+    }
301
+
302
+    /**
303
+     * http basic auth
304
+     * @return string|false (username, or false on failure)
305
+     */
306
+    private static function loginUser() {
307
+        if(self::$isLoggedIn === true) {
308
+            return \OC_User::getUser();
309
+        }
310
+
311
+        // reuse existing login
312
+        $loggedIn = \OC::$server->getUserSession()->isLoggedIn();
313
+        if ($loggedIn === true) {
314
+            if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
315
+                // Do not allow access to OCS until the 2FA challenge was solved successfully
316
+                return false;
317
+            }
318
+            $ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false;
319
+            if ($ocsApiRequest) {
320
+
321
+                // initialize the user's filesystem
322
+                \OC_Util::setupFS(\OC_User::getUser());
323
+                self::$isLoggedIn = true;
324
+
325
+                return OC_User::getUser();
326
+            }
327
+            return false;
328
+        }
329
+
330
+        // basic auth - because OC_User::login will create a new session we shall only try to login
331
+        // if user and pass are set
332
+        $userSession = \OC::$server->getUserSession();
333
+        $request = \OC::$server->getRequest();
334
+        try {
335
+            if ($userSession->tryTokenLogin($request)
336
+                || $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) {
337
+                self::$logoutRequired = true;
338
+            } else {
339
+                return false;
340
+            }
341
+            // initialize the user's filesystem
342
+            \OC_Util::setupFS(\OC_User::getUser());
343
+            self::$isLoggedIn = true;
344
+
345
+            return \OC_User::getUser();
346
+        } catch (\OC\User\LoginException $e) {
347
+            return false;
348
+        }
349
+    }
350
+
351
+    /**
352
+     * respond to a call
353
+     * @param OC_OCS_Result $result
354
+     * @param string $format the format xml|json
355
+     */
356
+    public static function respond($result, $format='xml') {
357
+        $request = \OC::$server->getRequest();
358
+
359
+        // Send 401 headers if unauthorised
360
+        if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
361
+            // If request comes from JS return dummy auth request
362
+            if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
363
+                header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
364
+            } else {
365
+                header('WWW-Authenticate: Basic realm="Authorisation Required"');
366
+            }
367
+            header('HTTP/1.0 401 Unauthorized');
368
+        }
369
+
370
+        foreach($result->getHeaders() as $name => $value) {
371
+            header($name . ': ' . $value);
372
+        }
373
+
374
+        $meta = $result->getMeta();
375
+        $data = $result->getData();
376
+        if (self::isV2($request)) {
377
+            $statusCode = self::mapStatusCodes($result->getStatusCode());
378
+            if (!is_null($statusCode)) {
379
+                $meta['statuscode'] = $statusCode;
380
+                OC_Response::setStatus($statusCode);
381
+            }
382
+        }
383
+
384
+        self::setContentType($format);
385
+        $body = self::renderResult($format, $meta, $data);
386
+        echo $body;
387
+    }
388
+
389
+    /**
390
+     * @param XMLWriter $writer
391
+     */
392
+    private static function toXML($array, $writer) {
393
+        foreach($array as $k => $v) {
394
+            if ($k[0] === '@') {
395
+                $writer->writeAttribute(substr($k, 1), $v);
396
+                continue;
397
+            } else if (is_numeric($k)) {
398
+                $k = 'element';
399
+            }
400
+            if(is_array($v)) {
401
+                $writer->startElement($k);
402
+                self::toXML($v, $writer);
403
+                $writer->endElement();
404
+            } else {
405
+                $writer->writeElement($k, $v);
406
+            }
407
+        }
408
+    }
409
+
410
+    /**
411
+     * @return string
412
+     */
413
+    public static function requestedFormat() {
414
+        $formats = array('json', 'xml');
415
+
416
+        $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
417
+        return $format;
418
+    }
419
+
420
+    /**
421
+     * Based on the requested format the response content type is set
422
+     * @param string $format
423
+     */
424
+    public static function setContentType($format = null) {
425
+        $format = is_null($format) ? self::requestedFormat() : $format;
426
+        if ($format === 'xml') {
427
+            header('Content-type: text/xml; charset=UTF-8');
428
+            return;
429
+        }
430
+
431
+        if ($format === 'json') {
432
+            header('Content-Type: application/json; charset=utf-8');
433
+            return;
434
+        }
435
+
436
+        header('Content-Type: application/octet-stream; charset=utf-8');
437
+    }
438
+
439
+    /**
440
+     * @param \OCP\IRequest $request
441
+     * @return bool
442
+     */
443
+    protected static function isV2(\OCP\IRequest $request) {
444
+        $script = $request->getScriptName();
445
+
446
+        return substr($script, -11) === '/ocs/v2.php';
447
+    }
448
+
449
+    /**
450
+     * @param integer $sc
451
+     * @return int
452
+     */
453
+    public static function mapStatusCodes($sc) {
454
+        switch ($sc) {
455
+            case API::RESPOND_NOT_FOUND:
456
+                return Http::STATUS_NOT_FOUND;
457
+            case API::RESPOND_SERVER_ERROR:
458
+                return Http::STATUS_INTERNAL_SERVER_ERROR;
459
+            case API::RESPOND_UNKNOWN_ERROR:
460
+                return Http::STATUS_INTERNAL_SERVER_ERROR;
461
+            case API::RESPOND_UNAUTHORISED:
462
+                // already handled for v1
463
+                return null;
464
+            case 100:
465
+                return Http::STATUS_OK;
466
+        }
467
+        // any 2xx, 4xx and 5xx will be used as is
468
+        if ($sc >= 200 && $sc < 600) {
469
+            return $sc;
470
+        }
471
+
472
+        return Http::STATUS_BAD_REQUEST;
473
+    }
474
+
475
+    /**
476
+     * @param string $format
477
+     * @return string
478
+     */
479
+    public static function renderResult($format, $meta, $data) {
480
+        $response = array(
481
+            'ocs' => array(
482
+                'meta' => $meta,
483
+                'data' => $data,
484
+            ),
485
+        );
486
+        if ($format == 'json') {
487
+            return OC_JSON::encode($response);
488
+        }
489
+
490
+        $writer = new XMLWriter();
491
+        $writer->openMemory();
492
+        $writer->setIndent(true);
493
+        $writer->startDocument();
494
+        self::toXML($response, $writer);
495
+        $writer->endDocument();
496
+        return $writer->outputMemory(true);
497
+    }
498 498
 }
Please login to merge, or discard this patch.