Completed
Pull Request — master (#5870)
by Björn
23:21 queued 06:38
created
lib/private/AppFramework/DependencyInjection/DIContainer.php 2 patches
Indentation   +377 added lines, -377 removed lines patch added patch discarded remove patch
@@ -63,381 +63,381 @@
 block discarded – undo
63 63
 
64 64
 class DIContainer extends SimpleContainer implements IAppContainer {
65 65
 
66
-	/**
67
-	 * @var array
68
-	 */
69
-	private $middleWares = array();
70
-
71
-	/** @var ServerContainer */
72
-	private $server;
73
-
74
-	/**
75
-	 * Put your class dependencies in here
76
-	 * @param string $appName the name of the app
77
-	 * @param array $urlParams
78
-	 * @param ServerContainer $server
79
-	 */
80
-	public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
81
-		parent::__construct();
82
-		$this['AppName'] = $appName;
83
-		$this['urlParams'] = $urlParams;
84
-
85
-		/** @var \OC\ServerContainer $server */
86
-		if ($server === null) {
87
-			$server = \OC::$server;
88
-		}
89
-		$this->server = $server;
90
-		$this->server->registerAppContainer($appName, $this);
91
-
92
-		// aliases
93
-		$this->registerAlias('appName', 'AppName');
94
-		$this->registerAlias('webRoot', 'WebRoot');
95
-		$this->registerAlias('userId', 'UserId');
96
-
97
-		/**
98
-		 * Core services
99
-		 */
100
-		$this->registerService(IOutput::class, function($c){
101
-			return new Output($this->getServer()->getWebRoot());
102
-		});
103
-
104
-		$this->registerService(Folder::class, function() {
105
-			return $this->getServer()->getUserFolder();
106
-		});
107
-
108
-		$this->registerService(IAppData::class, function (SimpleContainer $c) {
109
-			return $this->getServer()->getAppDataDir($c->query('AppName'));
110
-		});
111
-
112
-		$this->registerService(IL10N::class, function($c) {
113
-			return $this->getServer()->getL10N($c->query('AppName'));
114
-		});
115
-
116
-		$this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
117
-		$this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
118
-
119
-		$this->registerService(IRequest::class, function() {
120
-			return $this->getServer()->query(IRequest::class);
121
-		});
122
-		$this->registerAlias('Request', IRequest::class);
123
-
124
-		$this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
125
-		$this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
126
-
127
-		$this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
128
-
129
-		$this->registerService(IServerContainer::class, function ($c) {
130
-			return $this->getServer();
131
-		});
132
-		$this->registerAlias('ServerContainer', IServerContainer::class);
133
-
134
-		$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
135
-			return $c->query('OCA\WorkflowEngine\Manager');
136
-		});
137
-
138
-		$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
139
-			return $c;
140
-		});
141
-
142
-		// commonly used attributes
143
-		$this->registerService('UserId', function ($c) {
144
-			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
145
-		});
146
-
147
-		$this->registerService('WebRoot', function ($c) {
148
-			return $c->query('ServerContainer')->getWebRoot();
149
-		});
150
-
151
-		$this->registerService('fromMailAddress', function() {
152
-			return Util::getDefaultEmailAddress('no-reply');
153
-		});
154
-
155
-		$this->registerService('OC_Defaults', function ($c) {
156
-			return $c->getServer()->getThemingDefaults();
157
-		});
158
-
159
-		$this->registerService('OCP\Encryption\IManager', function ($c) {
160
-			return $this->getServer()->getEncryptionManager();
161
-		});
162
-
163
-		$this->registerService(IConfig::class, function ($c) {
164
-			return $c->query(OC\GlobalScale\Config::class);
165
-		});
166
-
167
-		$this->registerService(IValidator::class, function($c) {
168
-			return $c->query(Validator::class);
169
-		});
170
-
171
-		$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
172
-			return new \OC\Security\IdentityProof\Manager(
173
-				$this->getServer()->query(\OC\Files\AppData\Factory::class),
174
-				$this->getServer()->getCrypto(),
175
-				$this->getServer()->getConfig()
176
-			);
177
-		});
178
-
179
-		/**
180
-		 * App Framework APIs
181
-		 */
182
-		$this->registerService('API', function($c){
183
-			$c->query('OCP\\ILogger')->debug(
184
-				'Accessing the API class is deprecated! Use the appropriate ' .
185
-				'services instead!'
186
-			);
187
-			return new API($c['AppName']);
188
-		});
189
-
190
-		$this->registerService('Protocol', function($c){
191
-			/** @var \OC\Server $server */
192
-			$server = $c->query('ServerContainer');
193
-			$protocol = $server->getRequest()->getHttpProtocol();
194
-			return new Http($_SERVER, $protocol);
195
-		});
196
-
197
-		$this->registerService('Dispatcher', function($c) {
198
-			return new Dispatcher(
199
-				$c['Protocol'],
200
-				$c['MiddlewareDispatcher'],
201
-				$c['ControllerMethodReflector'],
202
-				$c['Request']
203
-			);
204
-		});
205
-
206
-		/**
207
-		 * App Framework default arguments
208
-		 */
209
-		$this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
210
-		$this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
211
-		$this->registerParameter('corsMaxAge', 1728000);
212
-
213
-		/**
214
-		 * Middleware
215
-		 */
216
-		$app = $this;
217
-		$this->registerService('SecurityMiddleware', function($c) use ($app){
218
-			/** @var \OC\Server $server */
219
-			$server = $app->getServer();
220
-
221
-			return new SecurityMiddleware(
222
-				$c['Request'],
223
-				$c['ControllerMethodReflector'],
224
-				$server->getNavigationManager(),
225
-				$server->getURLGenerator(),
226
-				$server->getLogger(),
227
-				$server->getSession(),
228
-				$c['AppName'],
229
-				$app->isLoggedIn(),
230
-				$app->isAdminUser(),
231
-				$server->getContentSecurityPolicyManager(),
232
-				$server->getCsrfTokenManager(),
233
-				$server->getContentSecurityPolicyNonceManager()
234
-			);
235
-
236
-		});
237
-
238
-		$this->registerService('BruteForceMiddleware', function($c) use ($app) {
239
-			/** @var \OC\Server $server */
240
-			$server = $app->getServer();
241
-
242
-			return new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
243
-				$c['ControllerMethodReflector'],
244
-				$server->getBruteForceThrottler(),
245
-				$server->getRequest()
246
-			);
247
-		});
248
-
249
-		$this->registerService('RateLimitingMiddleware', function($c) use ($app) {
250
-			/** @var \OC\Server $server */
251
-			$server = $app->getServer();
252
-
253
-			return new RateLimitingMiddleware(
254
-				$server->getRequest(),
255
-				$server->getUserSession(),
256
-				$c['ControllerMethodReflector'],
257
-				$c->query(OC\Security\RateLimiting\Limiter::class)
258
-			);
259
-		});
260
-
261
-		$this->registerService('CORSMiddleware', function($c) {
262
-			return new CORSMiddleware(
263
-				$c['Request'],
264
-				$c['ControllerMethodReflector'],
265
-				$c->query(IUserSession::class),
266
-				$c->getServer()->getBruteForceThrottler()
267
-			);
268
-		});
269
-
270
-		$this->registerService('SessionMiddleware', function($c) use ($app) {
271
-			return new SessionMiddleware(
272
-				$c['Request'],
273
-				$c['ControllerMethodReflector'],
274
-				$app->getServer()->getSession()
275
-			);
276
-		});
277
-
278
-		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
279
-			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
280
-			$userSession = $app->getServer()->getUserSession();
281
-			$session = $app->getServer()->getSession();
282
-			$urlGenerator = $app->getServer()->getURLGenerator();
283
-			$reflector = $c['ControllerMethodReflector'];
284
-			$request = $app->getServer()->getRequest();
285
-			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
286
-		});
287
-
288
-		$this->registerService('OCSMiddleware', function (SimpleContainer $c) {
289
-			return new OCSMiddleware(
290
-				$c['Request']
291
-			);
292
-		});
293
-
294
-		$middleWares = &$this->middleWares;
295
-		$this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
296
-			$dispatcher = new MiddlewareDispatcher();
297
-			$dispatcher->registerMiddleware($c['CORSMiddleware']);
298
-			$dispatcher->registerMiddleware($c['OCSMiddleware']);
299
-			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
300
-			$dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
301
-			$dispatcher->registerMiddleware($c['BruteForceMiddleware']);
302
-			$dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
303
-
304
-			foreach($middleWares as $middleWare) {
305
-				$dispatcher->registerMiddleware($c[$middleWare]);
306
-			}
307
-
308
-			$dispatcher->registerMiddleware($c['SessionMiddleware']);
309
-			return $dispatcher;
310
-		});
311
-
312
-	}
313
-
314
-
315
-	/**
316
-	 * @deprecated implements only deprecated methods
317
-	 * @return IApi
318
-	 */
319
-	public function getCoreApi()
320
-	{
321
-		return $this->query('API');
322
-	}
323
-
324
-	/**
325
-	 * @return \OCP\IServerContainer
326
-	 */
327
-	public function getServer()
328
-	{
329
-		return $this->server;
330
-	}
331
-
332
-	/**
333
-	 * @param string $middleWare
334
-	 * @return boolean|null
335
-	 */
336
-	public function registerMiddleWare($middleWare) {
337
-		array_push($this->middleWares, $middleWare);
338
-	}
339
-
340
-	/**
341
-	 * used to return the appname of the set application
342
-	 * @return string the name of your application
343
-	 */
344
-	public function getAppName() {
345
-		return $this->query('AppName');
346
-	}
347
-
348
-	/**
349
-	 * @deprecated use IUserSession->isLoggedIn()
350
-	 * @return boolean
351
-	 */
352
-	public function isLoggedIn() {
353
-		return \OC::$server->getUserSession()->isLoggedIn();
354
-	}
355
-
356
-	/**
357
-	 * @deprecated use IGroupManager->isAdmin($userId)
358
-	 * @return boolean
359
-	 */
360
-	public function isAdminUser() {
361
-		$uid = $this->getUserId();
362
-		return \OC_User::isAdminUser($uid);
363
-	}
364
-
365
-	private function getUserId() {
366
-		return $this->getServer()->getSession()->get('user_id');
367
-	}
368
-
369
-	/**
370
-	 * @deprecated use the ILogger instead
371
-	 * @param string $message
372
-	 * @param string $level
373
-	 * @return mixed
374
-	 */
375
-	public function log($message, $level) {
376
-		switch($level){
377
-			case 'debug':
378
-				$level = \OCP\Util::DEBUG;
379
-				break;
380
-			case 'info':
381
-				$level = \OCP\Util::INFO;
382
-				break;
383
-			case 'warn':
384
-				$level = \OCP\Util::WARN;
385
-				break;
386
-			case 'fatal':
387
-				$level = \OCP\Util::FATAL;
388
-				break;
389
-			default:
390
-				$level = \OCP\Util::ERROR;
391
-				break;
392
-		}
393
-		\OCP\Util::writeLog($this->getAppName(), $message, $level);
394
-	}
395
-
396
-	/**
397
-	 * Register a capability
398
-	 *
399
-	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
400
-	 */
401
-	public function registerCapability($serviceName) {
402
-		$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
403
-			return $this->query($serviceName);
404
-		});
405
-	}
406
-
407
-	/**
408
-	 * @param string $name
409
-	 * @return mixed
410
-	 * @throws QueryException if the query could not be resolved
411
-	 */
412
-	public function query($name) {
413
-		try {
414
-			return $this->queryNoFallback($name);
415
-		} catch (QueryException $e) {
416
-			return $this->getServer()->query($name);
417
-		}
418
-	}
419
-
420
-	/**
421
-	 * @param string $name
422
-	 * @return mixed
423
-	 * @throws QueryException if the query could not be resolved
424
-	 */
425
-	public function queryNoFallback($name) {
426
-		$name = $this->sanitizeName($name);
427
-
428
-		if ($this->offsetExists($name)) {
429
-			return parent::query($name);
430
-		} else {
431
-			if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
432
-				return parent::query($name);
433
-			} else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
434
-				return parent::query($name);
435
-			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
436
-				return parent::query($name);
437
-			}
438
-		}
439
-
440
-		throw new QueryException('Could not resolve ' . $name . '!' .
441
-			' Class can not be instantiated');
442
-	}
66
+    /**
67
+     * @var array
68
+     */
69
+    private $middleWares = array();
70
+
71
+    /** @var ServerContainer */
72
+    private $server;
73
+
74
+    /**
75
+     * Put your class dependencies in here
76
+     * @param string $appName the name of the app
77
+     * @param array $urlParams
78
+     * @param ServerContainer $server
79
+     */
80
+    public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
81
+        parent::__construct();
82
+        $this['AppName'] = $appName;
83
+        $this['urlParams'] = $urlParams;
84
+
85
+        /** @var \OC\ServerContainer $server */
86
+        if ($server === null) {
87
+            $server = \OC::$server;
88
+        }
89
+        $this->server = $server;
90
+        $this->server->registerAppContainer($appName, $this);
91
+
92
+        // aliases
93
+        $this->registerAlias('appName', 'AppName');
94
+        $this->registerAlias('webRoot', 'WebRoot');
95
+        $this->registerAlias('userId', 'UserId');
96
+
97
+        /**
98
+         * Core services
99
+         */
100
+        $this->registerService(IOutput::class, function($c){
101
+            return new Output($this->getServer()->getWebRoot());
102
+        });
103
+
104
+        $this->registerService(Folder::class, function() {
105
+            return $this->getServer()->getUserFolder();
106
+        });
107
+
108
+        $this->registerService(IAppData::class, function (SimpleContainer $c) {
109
+            return $this->getServer()->getAppDataDir($c->query('AppName'));
110
+        });
111
+
112
+        $this->registerService(IL10N::class, function($c) {
113
+            return $this->getServer()->getL10N($c->query('AppName'));
114
+        });
115
+
116
+        $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
117
+        $this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
118
+
119
+        $this->registerService(IRequest::class, function() {
120
+            return $this->getServer()->query(IRequest::class);
121
+        });
122
+        $this->registerAlias('Request', IRequest::class);
123
+
124
+        $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
125
+        $this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
126
+
127
+        $this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
128
+
129
+        $this->registerService(IServerContainer::class, function ($c) {
130
+            return $this->getServer();
131
+        });
132
+        $this->registerAlias('ServerContainer', IServerContainer::class);
133
+
134
+        $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
135
+            return $c->query('OCA\WorkflowEngine\Manager');
136
+        });
137
+
138
+        $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
139
+            return $c;
140
+        });
141
+
142
+        // commonly used attributes
143
+        $this->registerService('UserId', function ($c) {
144
+            return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
145
+        });
146
+
147
+        $this->registerService('WebRoot', function ($c) {
148
+            return $c->query('ServerContainer')->getWebRoot();
149
+        });
150
+
151
+        $this->registerService('fromMailAddress', function() {
152
+            return Util::getDefaultEmailAddress('no-reply');
153
+        });
154
+
155
+        $this->registerService('OC_Defaults', function ($c) {
156
+            return $c->getServer()->getThemingDefaults();
157
+        });
158
+
159
+        $this->registerService('OCP\Encryption\IManager', function ($c) {
160
+            return $this->getServer()->getEncryptionManager();
161
+        });
162
+
163
+        $this->registerService(IConfig::class, function ($c) {
164
+            return $c->query(OC\GlobalScale\Config::class);
165
+        });
166
+
167
+        $this->registerService(IValidator::class, function($c) {
168
+            return $c->query(Validator::class);
169
+        });
170
+
171
+        $this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
172
+            return new \OC\Security\IdentityProof\Manager(
173
+                $this->getServer()->query(\OC\Files\AppData\Factory::class),
174
+                $this->getServer()->getCrypto(),
175
+                $this->getServer()->getConfig()
176
+            );
177
+        });
178
+
179
+        /**
180
+         * App Framework APIs
181
+         */
182
+        $this->registerService('API', function($c){
183
+            $c->query('OCP\\ILogger')->debug(
184
+                'Accessing the API class is deprecated! Use the appropriate ' .
185
+                'services instead!'
186
+            );
187
+            return new API($c['AppName']);
188
+        });
189
+
190
+        $this->registerService('Protocol', function($c){
191
+            /** @var \OC\Server $server */
192
+            $server = $c->query('ServerContainer');
193
+            $protocol = $server->getRequest()->getHttpProtocol();
194
+            return new Http($_SERVER, $protocol);
195
+        });
196
+
197
+        $this->registerService('Dispatcher', function($c) {
198
+            return new Dispatcher(
199
+                $c['Protocol'],
200
+                $c['MiddlewareDispatcher'],
201
+                $c['ControllerMethodReflector'],
202
+                $c['Request']
203
+            );
204
+        });
205
+
206
+        /**
207
+         * App Framework default arguments
208
+         */
209
+        $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
210
+        $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
211
+        $this->registerParameter('corsMaxAge', 1728000);
212
+
213
+        /**
214
+         * Middleware
215
+         */
216
+        $app = $this;
217
+        $this->registerService('SecurityMiddleware', function($c) use ($app){
218
+            /** @var \OC\Server $server */
219
+            $server = $app->getServer();
220
+
221
+            return new SecurityMiddleware(
222
+                $c['Request'],
223
+                $c['ControllerMethodReflector'],
224
+                $server->getNavigationManager(),
225
+                $server->getURLGenerator(),
226
+                $server->getLogger(),
227
+                $server->getSession(),
228
+                $c['AppName'],
229
+                $app->isLoggedIn(),
230
+                $app->isAdminUser(),
231
+                $server->getContentSecurityPolicyManager(),
232
+                $server->getCsrfTokenManager(),
233
+                $server->getContentSecurityPolicyNonceManager()
234
+            );
235
+
236
+        });
237
+
238
+        $this->registerService('BruteForceMiddleware', function($c) use ($app) {
239
+            /** @var \OC\Server $server */
240
+            $server = $app->getServer();
241
+
242
+            return new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
243
+                $c['ControllerMethodReflector'],
244
+                $server->getBruteForceThrottler(),
245
+                $server->getRequest()
246
+            );
247
+        });
248
+
249
+        $this->registerService('RateLimitingMiddleware', function($c) use ($app) {
250
+            /** @var \OC\Server $server */
251
+            $server = $app->getServer();
252
+
253
+            return new RateLimitingMiddleware(
254
+                $server->getRequest(),
255
+                $server->getUserSession(),
256
+                $c['ControllerMethodReflector'],
257
+                $c->query(OC\Security\RateLimiting\Limiter::class)
258
+            );
259
+        });
260
+
261
+        $this->registerService('CORSMiddleware', function($c) {
262
+            return new CORSMiddleware(
263
+                $c['Request'],
264
+                $c['ControllerMethodReflector'],
265
+                $c->query(IUserSession::class),
266
+                $c->getServer()->getBruteForceThrottler()
267
+            );
268
+        });
269
+
270
+        $this->registerService('SessionMiddleware', function($c) use ($app) {
271
+            return new SessionMiddleware(
272
+                $c['Request'],
273
+                $c['ControllerMethodReflector'],
274
+                $app->getServer()->getSession()
275
+            );
276
+        });
277
+
278
+        $this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
279
+            $twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
280
+            $userSession = $app->getServer()->getUserSession();
281
+            $session = $app->getServer()->getSession();
282
+            $urlGenerator = $app->getServer()->getURLGenerator();
283
+            $reflector = $c['ControllerMethodReflector'];
284
+            $request = $app->getServer()->getRequest();
285
+            return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
286
+        });
287
+
288
+        $this->registerService('OCSMiddleware', function (SimpleContainer $c) {
289
+            return new OCSMiddleware(
290
+                $c['Request']
291
+            );
292
+        });
293
+
294
+        $middleWares = &$this->middleWares;
295
+        $this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
296
+            $dispatcher = new MiddlewareDispatcher();
297
+            $dispatcher->registerMiddleware($c['CORSMiddleware']);
298
+            $dispatcher->registerMiddleware($c['OCSMiddleware']);
299
+            $dispatcher->registerMiddleware($c['SecurityMiddleware']);
300
+            $dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
301
+            $dispatcher->registerMiddleware($c['BruteForceMiddleware']);
302
+            $dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
303
+
304
+            foreach($middleWares as $middleWare) {
305
+                $dispatcher->registerMiddleware($c[$middleWare]);
306
+            }
307
+
308
+            $dispatcher->registerMiddleware($c['SessionMiddleware']);
309
+            return $dispatcher;
310
+        });
311
+
312
+    }
313
+
314
+
315
+    /**
316
+     * @deprecated implements only deprecated methods
317
+     * @return IApi
318
+     */
319
+    public function getCoreApi()
320
+    {
321
+        return $this->query('API');
322
+    }
323
+
324
+    /**
325
+     * @return \OCP\IServerContainer
326
+     */
327
+    public function getServer()
328
+    {
329
+        return $this->server;
330
+    }
331
+
332
+    /**
333
+     * @param string $middleWare
334
+     * @return boolean|null
335
+     */
336
+    public function registerMiddleWare($middleWare) {
337
+        array_push($this->middleWares, $middleWare);
338
+    }
339
+
340
+    /**
341
+     * used to return the appname of the set application
342
+     * @return string the name of your application
343
+     */
344
+    public function getAppName() {
345
+        return $this->query('AppName');
346
+    }
347
+
348
+    /**
349
+     * @deprecated use IUserSession->isLoggedIn()
350
+     * @return boolean
351
+     */
352
+    public function isLoggedIn() {
353
+        return \OC::$server->getUserSession()->isLoggedIn();
354
+    }
355
+
356
+    /**
357
+     * @deprecated use IGroupManager->isAdmin($userId)
358
+     * @return boolean
359
+     */
360
+    public function isAdminUser() {
361
+        $uid = $this->getUserId();
362
+        return \OC_User::isAdminUser($uid);
363
+    }
364
+
365
+    private function getUserId() {
366
+        return $this->getServer()->getSession()->get('user_id');
367
+    }
368
+
369
+    /**
370
+     * @deprecated use the ILogger instead
371
+     * @param string $message
372
+     * @param string $level
373
+     * @return mixed
374
+     */
375
+    public function log($message, $level) {
376
+        switch($level){
377
+            case 'debug':
378
+                $level = \OCP\Util::DEBUG;
379
+                break;
380
+            case 'info':
381
+                $level = \OCP\Util::INFO;
382
+                break;
383
+            case 'warn':
384
+                $level = \OCP\Util::WARN;
385
+                break;
386
+            case 'fatal':
387
+                $level = \OCP\Util::FATAL;
388
+                break;
389
+            default:
390
+                $level = \OCP\Util::ERROR;
391
+                break;
392
+        }
393
+        \OCP\Util::writeLog($this->getAppName(), $message, $level);
394
+    }
395
+
396
+    /**
397
+     * Register a capability
398
+     *
399
+     * @param string $serviceName e.g. 'OCA\Files\Capabilities'
400
+     */
401
+    public function registerCapability($serviceName) {
402
+        $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
403
+            return $this->query($serviceName);
404
+        });
405
+    }
406
+
407
+    /**
408
+     * @param string $name
409
+     * @return mixed
410
+     * @throws QueryException if the query could not be resolved
411
+     */
412
+    public function query($name) {
413
+        try {
414
+            return $this->queryNoFallback($name);
415
+        } catch (QueryException $e) {
416
+            return $this->getServer()->query($name);
417
+        }
418
+    }
419
+
420
+    /**
421
+     * @param string $name
422
+     * @return mixed
423
+     * @throws QueryException if the query could not be resolved
424
+     */
425
+    public function queryNoFallback($name) {
426
+        $name = $this->sanitizeName($name);
427
+
428
+        if ($this->offsetExists($name)) {
429
+            return parent::query($name);
430
+        } else {
431
+            if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
432
+                return parent::query($name);
433
+            } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
434
+                return parent::query($name);
435
+            } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
436
+                return parent::query($name);
437
+            }
438
+        }
439
+
440
+        throw new QueryException('Could not resolve ' . $name . '!' .
441
+            ' Class can not be instantiated');
442
+    }
443 443
 }
Please login to merge, or discard this patch.
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
 	 * @param array $urlParams
78 78
 	 * @param ServerContainer $server
79 79
 	 */
80
-	public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
80
+	public function __construct($appName, $urlParams = array(), ServerContainer $server = null) {
81 81
 		parent::__construct();
82 82
 		$this['AppName'] = $appName;
83 83
 		$this['urlParams'] = $urlParams;
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 		/**
98 98
 		 * Core services
99 99
 		 */
100
-		$this->registerService(IOutput::class, function($c){
100
+		$this->registerService(IOutput::class, function($c) {
101 101
 			return new Output($this->getServer()->getWebRoot());
102 102
 		});
103 103
 
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
 			return $this->getServer()->getUserFolder();
106 106
 		});
107 107
 
108
-		$this->registerService(IAppData::class, function (SimpleContainer $c) {
108
+		$this->registerService(IAppData::class, function(SimpleContainer $c) {
109 109
 			return $this->getServer()->getAppDataDir($c->query('AppName'));
110 110
 		});
111 111
 
@@ -126,25 +126,25 @@  discard block
 block discarded – undo
126 126
 
127 127
 		$this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
128 128
 
129
-		$this->registerService(IServerContainer::class, function ($c) {
129
+		$this->registerService(IServerContainer::class, function($c) {
130 130
 			return $this->getServer();
131 131
 		});
132 132
 		$this->registerAlias('ServerContainer', IServerContainer::class);
133 133
 
134
-		$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
134
+		$this->registerService(\OCP\WorkflowEngine\IManager::class, function($c) {
135 135
 			return $c->query('OCA\WorkflowEngine\Manager');
136 136
 		});
137 137
 
138
-		$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
138
+		$this->registerService(\OCP\AppFramework\IAppContainer::class, function($c) {
139 139
 			return $c;
140 140
 		});
141 141
 
142 142
 		// commonly used attributes
143
-		$this->registerService('UserId', function ($c) {
143
+		$this->registerService('UserId', function($c) {
144 144
 			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
145 145
 		});
146 146
 
147
-		$this->registerService('WebRoot', function ($c) {
147
+		$this->registerService('WebRoot', function($c) {
148 148
 			return $c->query('ServerContainer')->getWebRoot();
149 149
 		});
150 150
 
@@ -152,15 +152,15 @@  discard block
 block discarded – undo
152 152
 			return Util::getDefaultEmailAddress('no-reply');
153 153
 		});
154 154
 
155
-		$this->registerService('OC_Defaults', function ($c) {
155
+		$this->registerService('OC_Defaults', function($c) {
156 156
 			return $c->getServer()->getThemingDefaults();
157 157
 		});
158 158
 
159
-		$this->registerService('OCP\Encryption\IManager', function ($c) {
159
+		$this->registerService('OCP\Encryption\IManager', function($c) {
160 160
 			return $this->getServer()->getEncryptionManager();
161 161
 		});
162 162
 
163
-		$this->registerService(IConfig::class, function ($c) {
163
+		$this->registerService(IConfig::class, function($c) {
164 164
 			return $c->query(OC\GlobalScale\Config::class);
165 165
 		});
166 166
 
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
 			return $c->query(Validator::class);
169 169
 		});
170 170
 
171
-		$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
171
+		$this->registerService(\OC\Security\IdentityProof\Manager::class, function($c) {
172 172
 			return new \OC\Security\IdentityProof\Manager(
173 173
 				$this->getServer()->query(\OC\Files\AppData\Factory::class),
174 174
 				$this->getServer()->getCrypto(),
@@ -179,15 +179,15 @@  discard block
 block discarded – undo
179 179
 		/**
180 180
 		 * App Framework APIs
181 181
 		 */
182
-		$this->registerService('API', function($c){
182
+		$this->registerService('API', function($c) {
183 183
 			$c->query('OCP\\ILogger')->debug(
184
-				'Accessing the API class is deprecated! Use the appropriate ' .
184
+				'Accessing the API class is deprecated! Use the appropriate '.
185 185
 				'services instead!'
186 186
 			);
187 187
 			return new API($c['AppName']);
188 188
 		});
189 189
 
190
-		$this->registerService('Protocol', function($c){
190
+		$this->registerService('Protocol', function($c) {
191 191
 			/** @var \OC\Server $server */
192 192
 			$server = $c->query('ServerContainer');
193 193
 			$protocol = $server->getRequest()->getHttpProtocol();
@@ -275,7 +275,7 @@  discard block
 block discarded – undo
275 275
 			);
276 276
 		});
277 277
 
278
-		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
278
+		$this->registerService('TwoFactorMiddleware', function(SimpleContainer $c) use ($app) {
279 279
 			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
280 280
 			$userSession = $app->getServer()->getUserSession();
281 281
 			$session = $app->getServer()->getSession();
@@ -285,7 +285,7 @@  discard block
 block discarded – undo
285 285
 			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
286 286
 		});
287 287
 
288
-		$this->registerService('OCSMiddleware', function (SimpleContainer $c) {
288
+		$this->registerService('OCSMiddleware', function(SimpleContainer $c) {
289 289
 			return new OCSMiddleware(
290 290
 				$c['Request']
291 291
 			);
@@ -301,7 +301,7 @@  discard block
 block discarded – undo
301 301
 			$dispatcher->registerMiddleware($c['BruteForceMiddleware']);
302 302
 			$dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
303 303
 
304
-			foreach($middleWares as $middleWare) {
304
+			foreach ($middleWares as $middleWare) {
305 305
 				$dispatcher->registerMiddleware($c[$middleWare]);
306 306
 			}
307 307
 
@@ -373,7 +373,7 @@  discard block
 block discarded – undo
373 373
 	 * @return mixed
374 374
 	 */
375 375
 	public function log($message, $level) {
376
-		switch($level){
376
+		switch ($level) {
377 377
 			case 'debug':
378 378
 				$level = \OCP\Util::DEBUG;
379 379
 				break;
@@ -432,12 +432,12 @@  discard block
 block discarded – undo
432 432
 				return parent::query($name);
433 433
 			} else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
434 434
 				return parent::query($name);
435
-			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
435
+			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']).'\\') === 0) {
436 436
 				return parent::query($name);
437 437
 			}
438 438
 		}
439 439
 
440
-		throw new QueryException('Could not resolve ' . $name . '!' .
440
+		throw new QueryException('Could not resolve '.$name.'!'.
441 441
 			' Class can not be instantiated');
442 442
 	}
443 443
 }
Please login to merge, or discard this patch.
lib/private/Repair.php 1 patch
Indentation   +176 added lines, -176 removed lines patch added patch discarded remove patch
@@ -55,180 +55,180 @@
 block discarded – undo
55 55
 use Symfony\Component\EventDispatcher\GenericEvent;
56 56
 
57 57
 class Repair implements IOutput{
58
-	/* @var IRepairStep[] */
59
-	private $repairSteps;
60
-	/** @var EventDispatcher */
61
-	private $dispatcher;
62
-	/** @var string */
63
-	private $currentStep;
64
-
65
-	/**
66
-	 * Creates a new repair step runner
67
-	 *
68
-	 * @param IRepairStep[] $repairSteps array of RepairStep instances
69
-	 * @param EventDispatcher $dispatcher
70
-	 */
71
-	public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
72
-		$this->repairSteps = $repairSteps;
73
-		$this->dispatcher = $dispatcher;
74
-	}
75
-
76
-	/**
77
-	 * Run a series of repair steps for common problems
78
-	 */
79
-	public function run() {
80
-		if (count($this->repairSteps) === 0) {
81
-			$this->emit('\OC\Repair', 'info', array('No repair steps available'));
82
-			return;
83
-		}
84
-		// run each repair step
85
-		foreach ($this->repairSteps as $step) {
86
-			$this->currentStep = $step->getName();
87
-			$this->emit('\OC\Repair', 'step', [$this->currentStep]);
88
-			$step->run($this);
89
-		}
90
-	}
91
-
92
-	/**
93
-	 * Add repair step
94
-	 *
95
-	 * @param IRepairStep|string $repairStep repair step
96
-	 * @throws \Exception
97
-	 */
98
-	public function addStep($repairStep) {
99
-		if (is_string($repairStep)) {
100
-			try {
101
-				$s = \OC::$server->query($repairStep);
102
-			} catch (QueryException $e) {
103
-				if (class_exists($repairStep)) {
104
-					$s = new $repairStep();
105
-				} else {
106
-					throw new \Exception("Repair step '$repairStep' is unknown");
107
-				}
108
-			}
109
-
110
-			if ($s instanceof IRepairStep) {
111
-				$this->repairSteps[] = $s;
112
-			} else {
113
-				throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
114
-			}
115
-		} else {
116
-			$this->repairSteps[] = $repairStep;
117
-		}
118
-	}
119
-
120
-	/**
121
-	 * Returns the default repair steps to be run on the
122
-	 * command line or after an upgrade.
123
-	 *
124
-	 * @return IRepairStep[]
125
-	 */
126
-	public static function getRepairSteps() {
127
-		return [
128
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
129
-			new RepairMimeTypes(\OC::$server->getConfig()),
130
-			new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
131
-			new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
132
-			new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
133
-			new MoveUpdaterStepFile(\OC::$server->getConfig()),
134
-			new MoveAvatars(
135
-				\OC::$server->getJobList(),
136
-				\OC::$server->getConfig()
137
-			),
138
-			new CleanPreviews(
139
-				\OC::$server->getJobList(),
140
-				\OC::$server->getUserManager(),
141
-				\OC::$server->getConfig()
142
-			),
143
-			new FixMountStorages(\OC::$server->getDatabaseConnection()),
144
-			new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
145
-			new InstallCoreBundle(
146
-				\OC::$server->query(BundleFetcher::class),
147
-				\OC::$server->getConfig(),
148
-				\OC::$server->query(Installer::class)
149
-			),
150
-			new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
151
-			new RepairIdentityProofKeyFolders(\OC::$server->getConfig(), \OC::$server->query(Factory::class), \OC::$server->getRootFolder()),
152
-		];
153
-	}
154
-
155
-	/**
156
-	 * Returns expensive repair steps to be run on the
157
-	 * command line with a special option.
158
-	 *
159
-	 * @return IRepairStep[]
160
-	 */
161
-	public static function getExpensiveRepairSteps() {
162
-		return [
163
-			new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
164
-		];
165
-	}
166
-
167
-	/**
168
-	 * Returns the repair steps to be run before an
169
-	 * upgrade.
170
-	 *
171
-	 * @return IRepairStep[]
172
-	 */
173
-	public static function getBeforeUpgradeRepairSteps() {
174
-		$connection = \OC::$server->getDatabaseConnection();
175
-		$config = \OC::$server->getConfig();
176
-		$steps = [
177
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
178
-			new SqliteAutoincrement($connection),
179
-			new SaveAccountsTableData($connection, $config),
180
-		];
181
-
182
-		return $steps;
183
-	}
184
-
185
-	/**
186
-	 * @param string $scope
187
-	 * @param string $method
188
-	 * @param array $arguments
189
-	 */
190
-	public function emit($scope, $method, array $arguments = []) {
191
-		if (!is_null($this->dispatcher)) {
192
-			$this->dispatcher->dispatch("$scope::$method",
193
-				new GenericEvent("$scope::$method", $arguments));
194
-		}
195
-	}
196
-
197
-	public function info($string) {
198
-		// for now just emit as we did in the past
199
-		$this->emit('\OC\Repair', 'info', array($string));
200
-	}
201
-
202
-	/**
203
-	 * @param string $message
204
-	 */
205
-	public function warning($message) {
206
-		// for now just emit as we did in the past
207
-		$this->emit('\OC\Repair', 'warning', [$message]);
208
-	}
209
-
210
-	/**
211
-	 * @param int $max
212
-	 */
213
-	public function startProgress($max = 0) {
214
-		// for now just emit as we did in the past
215
-		$this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
216
-	}
217
-
218
-	/**
219
-	 * @param int $step
220
-	 * @param string $description
221
-	 */
222
-	public function advance($step = 1, $description = '') {
223
-		// for now just emit as we did in the past
224
-		$this->emit('\OC\Repair', 'advance', [$step, $description]);
225
-	}
226
-
227
-	/**
228
-	 * @param int $max
229
-	 */
230
-	public function finishProgress() {
231
-		// for now just emit as we did in the past
232
-		$this->emit('\OC\Repair', 'finishProgress', []);
233
-	}
58
+    /* @var IRepairStep[] */
59
+    private $repairSteps;
60
+    /** @var EventDispatcher */
61
+    private $dispatcher;
62
+    /** @var string */
63
+    private $currentStep;
64
+
65
+    /**
66
+     * Creates a new repair step runner
67
+     *
68
+     * @param IRepairStep[] $repairSteps array of RepairStep instances
69
+     * @param EventDispatcher $dispatcher
70
+     */
71
+    public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
72
+        $this->repairSteps = $repairSteps;
73
+        $this->dispatcher = $dispatcher;
74
+    }
75
+
76
+    /**
77
+     * Run a series of repair steps for common problems
78
+     */
79
+    public function run() {
80
+        if (count($this->repairSteps) === 0) {
81
+            $this->emit('\OC\Repair', 'info', array('No repair steps available'));
82
+            return;
83
+        }
84
+        // run each repair step
85
+        foreach ($this->repairSteps as $step) {
86
+            $this->currentStep = $step->getName();
87
+            $this->emit('\OC\Repair', 'step', [$this->currentStep]);
88
+            $step->run($this);
89
+        }
90
+    }
91
+
92
+    /**
93
+     * Add repair step
94
+     *
95
+     * @param IRepairStep|string $repairStep repair step
96
+     * @throws \Exception
97
+     */
98
+    public function addStep($repairStep) {
99
+        if (is_string($repairStep)) {
100
+            try {
101
+                $s = \OC::$server->query($repairStep);
102
+            } catch (QueryException $e) {
103
+                if (class_exists($repairStep)) {
104
+                    $s = new $repairStep();
105
+                } else {
106
+                    throw new \Exception("Repair step '$repairStep' is unknown");
107
+                }
108
+            }
109
+
110
+            if ($s instanceof IRepairStep) {
111
+                $this->repairSteps[] = $s;
112
+            } else {
113
+                throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
114
+            }
115
+        } else {
116
+            $this->repairSteps[] = $repairStep;
117
+        }
118
+    }
119
+
120
+    /**
121
+     * Returns the default repair steps to be run on the
122
+     * command line or after an upgrade.
123
+     *
124
+     * @return IRepairStep[]
125
+     */
126
+    public static function getRepairSteps() {
127
+        return [
128
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
129
+            new RepairMimeTypes(\OC::$server->getConfig()),
130
+            new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
131
+            new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
132
+            new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
133
+            new MoveUpdaterStepFile(\OC::$server->getConfig()),
134
+            new MoveAvatars(
135
+                \OC::$server->getJobList(),
136
+                \OC::$server->getConfig()
137
+            ),
138
+            new CleanPreviews(
139
+                \OC::$server->getJobList(),
140
+                \OC::$server->getUserManager(),
141
+                \OC::$server->getConfig()
142
+            ),
143
+            new FixMountStorages(\OC::$server->getDatabaseConnection()),
144
+            new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
145
+            new InstallCoreBundle(
146
+                \OC::$server->query(BundleFetcher::class),
147
+                \OC::$server->getConfig(),
148
+                \OC::$server->query(Installer::class)
149
+            ),
150
+            new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
151
+            new RepairIdentityProofKeyFolders(\OC::$server->getConfig(), \OC::$server->query(Factory::class), \OC::$server->getRootFolder()),
152
+        ];
153
+    }
154
+
155
+    /**
156
+     * Returns expensive repair steps to be run on the
157
+     * command line with a special option.
158
+     *
159
+     * @return IRepairStep[]
160
+     */
161
+    public static function getExpensiveRepairSteps() {
162
+        return [
163
+            new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
164
+        ];
165
+    }
166
+
167
+    /**
168
+     * Returns the repair steps to be run before an
169
+     * upgrade.
170
+     *
171
+     * @return IRepairStep[]
172
+     */
173
+    public static function getBeforeUpgradeRepairSteps() {
174
+        $connection = \OC::$server->getDatabaseConnection();
175
+        $config = \OC::$server->getConfig();
176
+        $steps = [
177
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
178
+            new SqliteAutoincrement($connection),
179
+            new SaveAccountsTableData($connection, $config),
180
+        ];
181
+
182
+        return $steps;
183
+    }
184
+
185
+    /**
186
+     * @param string $scope
187
+     * @param string $method
188
+     * @param array $arguments
189
+     */
190
+    public function emit($scope, $method, array $arguments = []) {
191
+        if (!is_null($this->dispatcher)) {
192
+            $this->dispatcher->dispatch("$scope::$method",
193
+                new GenericEvent("$scope::$method", $arguments));
194
+        }
195
+    }
196
+
197
+    public function info($string) {
198
+        // for now just emit as we did in the past
199
+        $this->emit('\OC\Repair', 'info', array($string));
200
+    }
201
+
202
+    /**
203
+     * @param string $message
204
+     */
205
+    public function warning($message) {
206
+        // for now just emit as we did in the past
207
+        $this->emit('\OC\Repair', 'warning', [$message]);
208
+    }
209
+
210
+    /**
211
+     * @param int $max
212
+     */
213
+    public function startProgress($max = 0) {
214
+        // for now just emit as we did in the past
215
+        $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
216
+    }
217
+
218
+    /**
219
+     * @param int $step
220
+     * @param string $description
221
+     */
222
+    public function advance($step = 1, $description = '') {
223
+        // for now just emit as we did in the past
224
+        $this->emit('\OC\Repair', 'advance', [$step, $description]);
225
+    }
226
+
227
+    /**
228
+     * @param int $max
229
+     */
230
+    public function finishProgress() {
231
+        // for now just emit as we did in the past
232
+        $this->emit('\OC\Repair', 'finishProgress', []);
233
+    }
234 234
 }
Please login to merge, or discard this patch.
lib/private/Security/IdentityProof/Manager.php 2 patches
Indentation   +110 added lines, -110 removed lines patch added patch discarded remove patch
@@ -28,116 +28,116 @@
 block discarded – undo
28 28
 use OCP\Security\ICrypto;
29 29
 
30 30
 class Manager {
31
-	/** @var IAppData */
32
-	private $appData;
33
-	/** @var ICrypto */
34
-	private $crypto;
35
-	/** @var IConfig */
36
-	private $config;
37
-
38
-	/**
39
-	 * @param Factory $appDataFactory
40
-	 * @param ICrypto $crypto
41
-	 * @param IConfig $config
42
-	 */
43
-	public function __construct(Factory $appDataFactory,
44
-								ICrypto $crypto,
45
-								IConfig $config
46
-	) {
47
-		$this->appData = $appDataFactory->get('identityproof');
48
-		$this->crypto = $crypto;
49
-		$this->config = $config;
50
-	}
51
-
52
-	/**
53
-	 * Calls the openssl functions to generate a public and private key.
54
-	 * In a separate function for unit testing purposes.
55
-	 *
56
-	 * @return array [$publicKey, $privateKey]
57
-	 */
58
-	protected function generateKeyPair() {
59
-		$config = [
60
-			'digest_alg' => 'sha512',
61
-			'private_key_bits' => 2048,
62
-		];
63
-
64
-		// Generate new key
65
-		$res = openssl_pkey_new($config);
66
-		openssl_pkey_export($res, $privateKey);
67
-
68
-		// Extract the public key from $res to $pubKey
69
-		$publicKey = openssl_pkey_get_details($res);
70
-		$publicKey = $publicKey['key'];
71
-
72
-		return [$publicKey, $privateKey];
73
-	}
74
-
75
-	/**
76
-	 * Generate a key for a given ID
77
-	 * Note: If a key already exists it will be overwritten
78
-	 *
79
-	 * @param string $id key id
80
-	 * @return Key
81
-	 */
82
-	protected function generateKey($id) {
83
-		list($publicKey, $privateKey) = $this->generateKeyPair();
84
-
85
-		// Write the private and public key to the disk
86
-		try {
87
-			$this->appData->newFolder($id);
88
-		} catch (\Exception $e) {}
89
-		$folder = $this->appData->getFolder($id);
90
-		$folder->newFile('private')
91
-			->putContent($this->crypto->encrypt($privateKey));
92
-		$folder->newFile('public')
93
-			->putContent($publicKey);
94
-
95
-		return new Key($publicKey, $privateKey);
96
-	}
97
-
98
-	/**
99
-	 * Get key for a specific id
100
-	 *
101
-	 * @param string $id
102
-	 * @return Key
103
-	 */
104
-	protected function retrieveKey($id) {
105
-		try {
106
-			$folder = $this->appData->getFolder($id);
107
-			$privateKey = $this->crypto->decrypt(
108
-				$folder->getFile('private')->getContent()
109
-			);
110
-			$publicKey = $folder->getFile('public')->getContent();
111
-			return new Key($publicKey, $privateKey);
112
-		} catch (\Exception $e) {
113
-			return $this->generateKey($id);
114
-		}
115
-	}
116
-
117
-	/**
118
-	 * Get public and private key for $user
119
-	 *
120
-	 * @param IUser $user
121
-	 * @return Key
122
-	 */
123
-	public function getKey(IUser $user) {
124
-		$uid = $user->getUID();
125
-		return $this->retrieveKey('user-' . $uid);
126
-	}
127
-
128
-	/**
129
-	 * Get instance wide public and private key
130
-	 *
131
-	 * @return Key
132
-	 * @throws \RuntimeException
133
-	 */
134
-	public function getSystemKey() {
135
-		$instanceId = $this->config->getSystemValue('instanceid', null);
136
-		if ($instanceId === null) {
137
-			throw new \RuntimeException('no instance id!');
138
-		}
139
-		return $this->retrieveKey('system-' . $instanceId);
140
-	}
31
+    /** @var IAppData */
32
+    private $appData;
33
+    /** @var ICrypto */
34
+    private $crypto;
35
+    /** @var IConfig */
36
+    private $config;
37
+
38
+    /**
39
+     * @param Factory $appDataFactory
40
+     * @param ICrypto $crypto
41
+     * @param IConfig $config
42
+     */
43
+    public function __construct(Factory $appDataFactory,
44
+                                ICrypto $crypto,
45
+                                IConfig $config
46
+    ) {
47
+        $this->appData = $appDataFactory->get('identityproof');
48
+        $this->crypto = $crypto;
49
+        $this->config = $config;
50
+    }
51
+
52
+    /**
53
+     * Calls the openssl functions to generate a public and private key.
54
+     * In a separate function for unit testing purposes.
55
+     *
56
+     * @return array [$publicKey, $privateKey]
57
+     */
58
+    protected function generateKeyPair() {
59
+        $config = [
60
+            'digest_alg' => 'sha512',
61
+            'private_key_bits' => 2048,
62
+        ];
63
+
64
+        // Generate new key
65
+        $res = openssl_pkey_new($config);
66
+        openssl_pkey_export($res, $privateKey);
67
+
68
+        // Extract the public key from $res to $pubKey
69
+        $publicKey = openssl_pkey_get_details($res);
70
+        $publicKey = $publicKey['key'];
71
+
72
+        return [$publicKey, $privateKey];
73
+    }
74
+
75
+    /**
76
+     * Generate a key for a given ID
77
+     * Note: If a key already exists it will be overwritten
78
+     *
79
+     * @param string $id key id
80
+     * @return Key
81
+     */
82
+    protected function generateKey($id) {
83
+        list($publicKey, $privateKey) = $this->generateKeyPair();
84
+
85
+        // Write the private and public key to the disk
86
+        try {
87
+            $this->appData->newFolder($id);
88
+        } catch (\Exception $e) {}
89
+        $folder = $this->appData->getFolder($id);
90
+        $folder->newFile('private')
91
+            ->putContent($this->crypto->encrypt($privateKey));
92
+        $folder->newFile('public')
93
+            ->putContent($publicKey);
94
+
95
+        return new Key($publicKey, $privateKey);
96
+    }
97
+
98
+    /**
99
+     * Get key for a specific id
100
+     *
101
+     * @param string $id
102
+     * @return Key
103
+     */
104
+    protected function retrieveKey($id) {
105
+        try {
106
+            $folder = $this->appData->getFolder($id);
107
+            $privateKey = $this->crypto->decrypt(
108
+                $folder->getFile('private')->getContent()
109
+            );
110
+            $publicKey = $folder->getFile('public')->getContent();
111
+            return new Key($publicKey, $privateKey);
112
+        } catch (\Exception $e) {
113
+            return $this->generateKey($id);
114
+        }
115
+    }
116
+
117
+    /**
118
+     * Get public and private key for $user
119
+     *
120
+     * @param IUser $user
121
+     * @return Key
122
+     */
123
+    public function getKey(IUser $user) {
124
+        $uid = $user->getUID();
125
+        return $this->retrieveKey('user-' . $uid);
126
+    }
127
+
128
+    /**
129
+     * Get instance wide public and private key
130
+     *
131
+     * @return Key
132
+     * @throws \RuntimeException
133
+     */
134
+    public function getSystemKey() {
135
+        $instanceId = $this->config->getSystemValue('instanceid', null);
136
+        if ($instanceId === null) {
137
+            throw new \RuntimeException('no instance id!');
138
+        }
139
+        return $this->retrieveKey('system-' . $instanceId);
140
+    }
141 141
 
142 142
 
143 143
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
 	 */
123 123
 	public function getKey(IUser $user) {
124 124
 		$uid = $user->getUID();
125
-		return $this->retrieveKey('user-' . $uid);
125
+		return $this->retrieveKey('user-'.$uid);
126 126
 	}
127 127
 
128 128
 	/**
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 		if ($instanceId === null) {
137 137
 			throw new \RuntimeException('no instance id!');
138 138
 		}
139
-		return $this->retrieveKey('system-' . $instanceId);
139
+		return $this->retrieveKey('system-'.$instanceId);
140 140
 	}
141 141
 
142 142
 
Please login to merge, or discard this patch.
lib/private/Repair/NC13/RepairIdentityProofKeyFolders.php 2 patches
Indentation   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -32,79 +32,79 @@
 block discarded – undo
32 32
 
33 33
 class RepairIdentityProofKeyFolders implements IRepairStep {
34 34
 
35
-	/** @var IConfig */
36
-	private $config;
35
+    /** @var IConfig */
36
+    private $config;
37 37
 
38
-	/** @var \OC\Files\AppData\AppData */
39
-	private $appDataIdentityProof;
38
+    /** @var \OC\Files\AppData\AppData */
39
+    private $appDataIdentityProof;
40 40
 
41
-	/** @var IRootFolder */
42
-	private $rootFolder;
41
+    /** @var IRootFolder */
42
+    private $rootFolder;
43 43
 
44
-	/** @var string */
45
-	private $identityProofDir;
44
+    /** @var string */
45
+    private $identityProofDir;
46 46
 
47
-	/**
48
-	 * RepairIdentityProofKeyFolders constructor.
49
-	 *
50
-	 * @param IConfig $config
51
-	 * @param Factory $appDataFactory
52
-	 * @param IRootFolder $rootFolder
53
-	 */
54
-	public function __construct(IConfig $config, Factory $appDataFactory, IRootFolder $rootFolder) {
55
-		$this->config = $config;
56
-		$this->appDataIdentityProof = $appDataFactory->get('identityproof');
57
-		$this->rootFolder = $rootFolder;
47
+    /**
48
+     * RepairIdentityProofKeyFolders constructor.
49
+     *
50
+     * @param IConfig $config
51
+     * @param Factory $appDataFactory
52
+     * @param IRootFolder $rootFolder
53
+     */
54
+    public function __construct(IConfig $config, Factory $appDataFactory, IRootFolder $rootFolder) {
55
+        $this->config = $config;
56
+        $this->appDataIdentityProof = $appDataFactory->get('identityproof');
57
+        $this->rootFolder = $rootFolder;
58 58
 
59
-		$instanceId = $this->config->getSystemValue('instanceid', null);
60
-		if ($instanceId === null) {
61
-			throw new \RuntimeException('no instance id!');
62
-		}
63
-		$this->identityProofDir = 'appdata_' . $instanceId . '/identityproof/';
64
-	}
59
+        $instanceId = $this->config->getSystemValue('instanceid', null);
60
+        if ($instanceId === null) {
61
+            throw new \RuntimeException('no instance id!');
62
+        }
63
+        $this->identityProofDir = 'appdata_' . $instanceId . '/identityproof/';
64
+    }
65 65
 
66
-	/**
67
-	 * Returns the step's name
68
-	 *
69
-	 * @return string
70
-	 * @since 9.1.0
71
-	 */
72
-	public function getName() {
73
-		return "Rename folder with user specific keys";
74
-	}
66
+    /**
67
+     * Returns the step's name
68
+     *
69
+     * @return string
70
+     * @since 9.1.0
71
+     */
72
+    public function getName() {
73
+        return "Rename folder with user specific keys";
74
+    }
75 75
 
76
-	/**
77
-	 * Run repair step.
78
-	 * Must throw exception on error.
79
-	 *
80
-	 * @param IOutput $output
81
-	 * @throws \Exception in case of failure
82
-	 * @since 9.1.0
83
-	 */
84
-	public function run(IOutput $output) {
85
-		$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
86
-		if (version_compare($versionFromBeforeUpdate, '13.0.0.1', '<=')) {
87
-			$count = $this->repair();
88
-			$output->info('Repaired ' . $count . ' folders');
89
-		}
90
-	}
76
+    /**
77
+     * Run repair step.
78
+     * Must throw exception on error.
79
+     *
80
+     * @param IOutput $output
81
+     * @throws \Exception in case of failure
82
+     * @since 9.1.0
83
+     */
84
+    public function run(IOutput $output) {
85
+        $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
86
+        if (version_compare($versionFromBeforeUpdate, '13.0.0.1', '<=')) {
87
+            $count = $this->repair();
88
+            $output->info('Repaired ' . $count . ' folders');
89
+        }
90
+    }
91 91
 
92
-	/**
93
-	 * rename all dirs with user specific keys to 'user-uid'
94
-	 *
95
-	 * @return int
96
-	 */
97
-	private function repair() {
98
-		$count = 0;
99
-		$dirListing = $this->appDataIdentityProof->getDirectoryListing();
100
-		/** @var ISimpleFolder $folder */
101
-		foreach ($dirListing as $folder) {
102
-			$name = $folder->getName();
103
-			$node = $this->rootFolder->get($this->identityProofDir . $name);
104
-			$node->move($this->identityProofDir . 'user-' . $name);
105
-			$count++;
106
-		}
92
+    /**
93
+     * rename all dirs with user specific keys to 'user-uid'
94
+     *
95
+     * @return int
96
+     */
97
+    private function repair() {
98
+        $count = 0;
99
+        $dirListing = $this->appDataIdentityProof->getDirectoryListing();
100
+        /** @var ISimpleFolder $folder */
101
+        foreach ($dirListing as $folder) {
102
+            $name = $folder->getName();
103
+            $node = $this->rootFolder->get($this->identityProofDir . $name);
104
+            $node->move($this->identityProofDir . 'user-' . $name);
105
+            $count++;
106
+        }
107 107
 
108
-		return $count;
109
-	}
108
+        return $count;
109
+    }
110 110
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 		if ($instanceId === null) {
61 61
 			throw new \RuntimeException('no instance id!');
62 62
 		}
63
-		$this->identityProofDir = 'appdata_' . $instanceId . '/identityproof/';
63
+		$this->identityProofDir = 'appdata_'.$instanceId.'/identityproof/';
64 64
 	}
65 65
 
66 66
 	/**
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
 		$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
86 86
 		if (version_compare($versionFromBeforeUpdate, '13.0.0.1', '<=')) {
87 87
 			$count = $this->repair();
88
-			$output->info('Repaired ' . $count . ' folders');
88
+			$output->info('Repaired '.$count.' folders');
89 89
 		}
90 90
 	}
91 91
 
@@ -100,8 +100,8 @@  discard block
 block discarded – undo
100 100
 		/** @var ISimpleFolder $folder */
101 101
 		foreach ($dirListing as $folder) {
102 102
 			$name = $folder->getName();
103
-			$node = $this->rootFolder->get($this->identityProofDir . $name);
104
-			$node->move($this->identityProofDir . 'user-' . $name);
103
+			$node = $this->rootFolder->get($this->identityProofDir.$name);
104
+			$node->move($this->identityProofDir.'user-'.$name);
105 105
 			$count++;
106 106
 		}
107 107
 
Please login to merge, or discard this patch.