@@ -63,381 +63,381 @@  | 
                                                    ||
| 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 | }  | 
                                                        
@@ -77,7 +77,7 @@ discard block  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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  | 
                                                    ||
| 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 | }  | 
                                                        
@@ -28,115 +28,115 @@  | 
                                                    ||
| 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 | - return $this->retrieveKey($user->getUID());  | 
                                                        |
| 125 | - }  | 
                                                        |
| 126 | -  | 
                                                        |
| 127 | - /**  | 
                                                        |
| 128 | - * Get instance wide public and private key  | 
                                                        |
| 129 | - *  | 
                                                        |
| 130 | - * @return Key  | 
                                                        |
| 131 | - * @throws \RuntimeException  | 
                                                        |
| 132 | - */  | 
                                                        |
| 133 | -	public function getSystemKey() { | 
                                                        |
| 134 | -		$instanceId = $this->config->getSystemValue('instanceid', null); | 
                                                        |
| 135 | -		if ($instanceId === null) { | 
                                                        |
| 136 | -			throw new \RuntimeException('no instance id!'); | 
                                                        |
| 137 | - }  | 
                                                        |
| 138 | - return $this->retrieveKey($instanceId);  | 
                                                        |
| 139 | - }  | 
                                                        |
| 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 | + return $this->retrieveKey($user->getUID());  | 
                                                        |
| 125 | + }  | 
                                                        |
| 126 | +  | 
                                                        |
| 127 | + /**  | 
                                                        |
| 128 | + * Get instance wide public and private key  | 
                                                        |
| 129 | + *  | 
                                                        |
| 130 | + * @return Key  | 
                                                        |
| 131 | + * @throws \RuntimeException  | 
                                                        |
| 132 | + */  | 
                                                        |
| 133 | +    public function getSystemKey() { | 
                                                        |
| 134 | +        $instanceId = $this->config->getSystemValue('instanceid', null); | 
                                                        |
| 135 | +        if ($instanceId === null) { | 
                                                        |
| 136 | +            throw new \RuntimeException('no instance id!'); | 
                                                        |
| 137 | + }  | 
                                                        |
| 138 | + return $this->retrieveKey($instanceId);  | 
                                                        |
| 139 | + }  | 
                                                        |
| 140 | 140 | |
| 141 | 141 | |
| 142 | 142 | }  |