@@ -80,395 +80,395 @@ |
||
| 80 | 80 | */ |
| 81 | 81 | class DIContainer extends SimpleContainer implements IAppContainer { |
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * @var array |
|
| 85 | - */ |
|
| 86 | - private $middleWares = []; |
|
| 87 | - |
|
| 88 | - /** @var ServerContainer */ |
|
| 89 | - private $server; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Put your class dependencies in here |
|
| 93 | - * @param string $appName the name of the app |
|
| 94 | - * @param array $urlParams |
|
| 95 | - * @param ServerContainer|null $server |
|
| 96 | - */ |
|
| 97 | - public function __construct($appName, $urlParams = [], ServerContainer $server = null) { |
|
| 98 | - parent::__construct(); |
|
| 99 | - $this['appName'] = $appName; |
|
| 100 | - $this['urlParams'] = $urlParams; |
|
| 101 | - |
|
| 102 | - $this->registerAlias('Request', IRequest::class); |
|
| 103 | - |
|
| 104 | - /** @var \OC\ServerContainer $server */ |
|
| 105 | - if ($server === null) { |
|
| 106 | - $server = \OC::$server; |
|
| 107 | - } |
|
| 108 | - $this->server = $server; |
|
| 109 | - $this->server->registerAppContainer($appName, $this); |
|
| 110 | - |
|
| 111 | - // aliases |
|
| 112 | - /** @deprecated inject $appName */ |
|
| 113 | - $this->registerAlias('AppName', 'appName'); |
|
| 114 | - /** @deprecated inject $webRoot*/ |
|
| 115 | - $this->registerAlias('WebRoot', 'webRoot'); |
|
| 116 | - /** @deprecated inject $userId */ |
|
| 117 | - $this->registerAlias('UserId', 'userId'); |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Core services |
|
| 121 | - */ |
|
| 122 | - $this->registerService(IOutput::class, function () { |
|
| 123 | - return new Output($this->getServer()->getWebRoot()); |
|
| 124 | - }); |
|
| 125 | - |
|
| 126 | - $this->registerService(Folder::class, function () { |
|
| 127 | - return $this->getServer()->getUserFolder(); |
|
| 128 | - }); |
|
| 129 | - |
|
| 130 | - $this->registerService(IAppData::class, function (ContainerInterface $c) { |
|
| 131 | - return $this->getServer()->getAppDataDir($c->get('AppName')); |
|
| 132 | - }); |
|
| 133 | - |
|
| 134 | - $this->registerService(IL10N::class, function (ContainerInterface $c) { |
|
| 135 | - return $this->getServer()->getL10N($c->get('AppName')); |
|
| 136 | - }); |
|
| 137 | - |
|
| 138 | - // Log wrappers |
|
| 139 | - $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { |
|
| 140 | - return new ScopedPsrLogger( |
|
| 141 | - $c->get(PsrLoggerAdapter::class), |
|
| 142 | - $c->get('AppName') |
|
| 143 | - ); |
|
| 144 | - }); |
|
| 145 | - $this->registerService(ILogger::class, function (ContainerInterface $c) { |
|
| 146 | - return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->get('AppName')); |
|
| 147 | - }); |
|
| 148 | - |
|
| 149 | - $this->registerService(IServerContainer::class, function () { |
|
| 150 | - return $this->getServer(); |
|
| 151 | - }); |
|
| 152 | - $this->registerAlias('ServerContainer', IServerContainer::class); |
|
| 153 | - |
|
| 154 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) { |
|
| 155 | - return $c->get(Manager::class); |
|
| 156 | - }); |
|
| 157 | - |
|
| 158 | - $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { |
|
| 159 | - return $c; |
|
| 160 | - }); |
|
| 161 | - $this->registerAlias(IAppContainer::class, ContainerInterface::class); |
|
| 162 | - |
|
| 163 | - // commonly used attributes |
|
| 164 | - $this->registerService('userId', function (ContainerInterface $c) { |
|
| 165 | - return $c->get(IUserSession::class)->getSession()->get('user_id'); |
|
| 166 | - }); |
|
| 167 | - |
|
| 168 | - $this->registerService('webRoot', function (ContainerInterface $c) { |
|
| 169 | - return $c->get(IServerContainer::class)->getWebRoot(); |
|
| 170 | - }); |
|
| 171 | - |
|
| 172 | - $this->registerService('OC_Defaults', function (ContainerInterface $c) { |
|
| 173 | - return $c->get(IServerContainer::class)->getThemingDefaults(); |
|
| 174 | - }); |
|
| 175 | - |
|
| 176 | - $this->registerService('Protocol', function (ContainerInterface $c) { |
|
| 177 | - /** @var \OC\Server $server */ |
|
| 178 | - $server = $c->get(IServerContainer::class); |
|
| 179 | - $protocol = $server->getRequest()->getHttpProtocol(); |
|
| 180 | - return new Http($_SERVER, $protocol); |
|
| 181 | - }); |
|
| 182 | - |
|
| 183 | - $this->registerService('Dispatcher', function (ContainerInterface $c) { |
|
| 184 | - return new Dispatcher( |
|
| 185 | - $c->get('Protocol'), |
|
| 186 | - $c->get(MiddlewareDispatcher::class), |
|
| 187 | - $c->get(IControllerMethodReflector::class), |
|
| 188 | - $c->get(IRequest::class), |
|
| 189 | - $c->get(IConfig::class), |
|
| 190 | - $c->get(IDBConnection::class), |
|
| 191 | - $c->get(LoggerInterface::class), |
|
| 192 | - $c->get(EventLogger::class) |
|
| 193 | - ); |
|
| 194 | - }); |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * App Framework default arguments |
|
| 198 | - */ |
|
| 199 | - $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
| 200 | - $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
| 201 | - $this->registerParameter('corsMaxAge', 1728000); |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Middleware |
|
| 205 | - */ |
|
| 206 | - $this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); |
|
| 207 | - $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) { |
|
| 208 | - $server = $this->getServer(); |
|
| 209 | - |
|
| 210 | - $dispatcher = new MiddlewareDispatcher(); |
|
| 211 | - |
|
| 212 | - $dispatcher->registerMiddleware( |
|
| 213 | - $c->get(OC\AppFramework\Middleware\CompressionMiddleware::class) |
|
| 214 | - ); |
|
| 215 | - |
|
| 216 | - $dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class)); |
|
| 217 | - |
|
| 218 | - $dispatcher->registerMiddleware( |
|
| 219 | - $c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
| 220 | - ); |
|
| 221 | - |
|
| 222 | - $dispatcher->registerMiddleware( |
|
| 223 | - new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
| 224 | - $c->get(IRequest::class), |
|
| 225 | - $c->get(IControllerMethodReflector::class) |
|
| 226 | - ) |
|
| 227 | - ); |
|
| 228 | - $dispatcher->registerMiddleware( |
|
| 229 | - new CORSMiddleware( |
|
| 230 | - $c->get(IRequest::class), |
|
| 231 | - $c->get(IControllerMethodReflector::class), |
|
| 232 | - $c->get(IUserSession::class), |
|
| 233 | - $c->get(OC\Security\Bruteforce\Throttler::class) |
|
| 234 | - ) |
|
| 235 | - ); |
|
| 236 | - $dispatcher->registerMiddleware( |
|
| 237 | - new OCSMiddleware( |
|
| 238 | - $c->get(IRequest::class) |
|
| 239 | - ) |
|
| 240 | - ); |
|
| 241 | - |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - $securityMiddleware = new SecurityMiddleware( |
|
| 245 | - $c->get(IRequest::class), |
|
| 246 | - $c->get(IControllerMethodReflector::class), |
|
| 247 | - $c->get(INavigationManager::class), |
|
| 248 | - $c->get(IURLGenerator::class), |
|
| 249 | - $server->get(LoggerInterface::class), |
|
| 250 | - $c->get('AppName'), |
|
| 251 | - $server->getUserSession()->isLoggedIn(), |
|
| 252 | - $this->getUserId() !== null && $server->getGroupManager()->isAdmin($this->getUserId()), |
|
| 253 | - $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
| 254 | - $server->getAppManager(), |
|
| 255 | - $server->getL10N('lib'), |
|
| 256 | - $c->get(AuthorizedGroupMapper::class), |
|
| 257 | - $server->get(IUserSession::class) |
|
| 258 | - ); |
|
| 259 | - $dispatcher->registerMiddleware($securityMiddleware); |
|
| 260 | - $dispatcher->registerMiddleware( |
|
| 261 | - new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
| 262 | - $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
| 263 | - $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
| 264 | - $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
| 265 | - ) |
|
| 266 | - ); |
|
| 267 | - $dispatcher->registerMiddleware( |
|
| 268 | - $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) |
|
| 269 | - ); |
|
| 270 | - $dispatcher->registerMiddleware( |
|
| 271 | - new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
| 272 | - $c->get(IControllerMethodReflector::class), |
|
| 273 | - $c->get(ISession::class), |
|
| 274 | - $c->get(IUserSession::class), |
|
| 275 | - $c->get(ITimeFactory::class) |
|
| 276 | - ) |
|
| 277 | - ); |
|
| 278 | - $dispatcher->registerMiddleware( |
|
| 279 | - new TwoFactorMiddleware( |
|
| 280 | - $c->get(OC\Authentication\TwoFactorAuth\Manager::class), |
|
| 281 | - $c->get(IUserSession::class), |
|
| 282 | - $c->get(ISession::class), |
|
| 283 | - $c->get(IURLGenerator::class), |
|
| 284 | - $c->get(IControllerMethodReflector::class), |
|
| 285 | - $c->get(IRequest::class) |
|
| 286 | - ) |
|
| 287 | - ); |
|
| 288 | - $dispatcher->registerMiddleware( |
|
| 289 | - new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
| 290 | - $c->get(IControllerMethodReflector::class), |
|
| 291 | - $c->get(OC\Security\Bruteforce\Throttler::class), |
|
| 292 | - $c->get(IRequest::class) |
|
| 293 | - ) |
|
| 294 | - ); |
|
| 295 | - $dispatcher->registerMiddleware( |
|
| 296 | - new RateLimitingMiddleware( |
|
| 297 | - $c->get(IRequest::class), |
|
| 298 | - $c->get(IUserSession::class), |
|
| 299 | - $c->get(IControllerMethodReflector::class), |
|
| 300 | - $c->get(OC\Security\RateLimiting\Limiter::class) |
|
| 301 | - ) |
|
| 302 | - ); |
|
| 303 | - $dispatcher->registerMiddleware( |
|
| 304 | - new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
| 305 | - $c->get(IRequest::class), |
|
| 306 | - $c->get(ISession::class), |
|
| 307 | - $c->get(\OCP\IConfig::class) |
|
| 308 | - ) |
|
| 309 | - ); |
|
| 310 | - $dispatcher->registerMiddleware( |
|
| 311 | - $c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
| 312 | - ); |
|
| 313 | - |
|
| 314 | - foreach ($this->middleWares as $middleWare) { |
|
| 315 | - $dispatcher->registerMiddleware($c->get($middleWare)); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - $dispatcher->registerMiddleware( |
|
| 319 | - new SessionMiddleware( |
|
| 320 | - $c->get(IControllerMethodReflector::class), |
|
| 321 | - $c->get(ISession::class) |
|
| 322 | - ) |
|
| 323 | - ); |
|
| 324 | - return $dispatcher; |
|
| 325 | - }); |
|
| 326 | - |
|
| 327 | - $this->registerService(IAppConfig::class, function (ContainerInterface $c) { |
|
| 328 | - return new OC\AppFramework\Services\AppConfig( |
|
| 329 | - $c->get(IConfig::class), |
|
| 330 | - $c->get('AppName') |
|
| 331 | - ); |
|
| 332 | - }); |
|
| 333 | - $this->registerService(IInitialState::class, function (ContainerInterface $c) { |
|
| 334 | - return new OC\AppFramework\Services\InitialState( |
|
| 335 | - $c->get(IInitialStateService::class), |
|
| 336 | - $c->get('AppName') |
|
| 337 | - ); |
|
| 338 | - }); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * @return \OCP\IServerContainer |
|
| 343 | - */ |
|
| 344 | - public function getServer() { |
|
| 345 | - return $this->server; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * @param string $middleWare |
|
| 350 | - * @return boolean|null |
|
| 351 | - */ |
|
| 352 | - public function registerMiddleWare($middleWare) { |
|
| 353 | - if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
| 354 | - return false; |
|
| 355 | - } |
|
| 356 | - $this->middleWares[] = $middleWare; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * used to return the appname of the set application |
|
| 361 | - * @return string the name of your application |
|
| 362 | - */ |
|
| 363 | - public function getAppName() { |
|
| 364 | - return $this->query('AppName'); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * @deprecated use IUserSession->isLoggedIn() |
|
| 369 | - * @return boolean |
|
| 370 | - */ |
|
| 371 | - public function isLoggedIn() { |
|
| 372 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * @deprecated use IGroupManager->isAdmin($userId) |
|
| 377 | - * @return boolean |
|
| 378 | - */ |
|
| 379 | - public function isAdminUser() { |
|
| 380 | - $uid = $this->getUserId(); |
|
| 381 | - return \OC_User::isAdminUser($uid); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - private function getUserId() { |
|
| 385 | - return $this->getServer()->getSession()->get('user_id'); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * @deprecated use the ILogger instead |
|
| 390 | - * @param string $message |
|
| 391 | - * @param string $level |
|
| 392 | - * @return mixed |
|
| 393 | - */ |
|
| 394 | - public function log($message, $level) { |
|
| 395 | - switch ($level) { |
|
| 396 | - case 'debug': |
|
| 397 | - $level = ILogger::DEBUG; |
|
| 398 | - break; |
|
| 399 | - case 'info': |
|
| 400 | - $level = ILogger::INFO; |
|
| 401 | - break; |
|
| 402 | - case 'warn': |
|
| 403 | - $level = ILogger::WARN; |
|
| 404 | - break; |
|
| 405 | - case 'fatal': |
|
| 406 | - $level = ILogger::FATAL; |
|
| 407 | - break; |
|
| 408 | - default: |
|
| 409 | - $level = ILogger::ERROR; |
|
| 410 | - break; |
|
| 411 | - } |
|
| 412 | - \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * Register a capability |
|
| 417 | - * |
|
| 418 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
| 419 | - */ |
|
| 420 | - public function registerCapability($serviceName) { |
|
| 421 | - $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
| 422 | - return $this->query($serviceName); |
|
| 423 | - }); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - public function has($id): bool { |
|
| 427 | - if (parent::has($id)) { |
|
| 428 | - return true; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - if ($this->server->has($id, true)) { |
|
| 432 | - return true; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - return false; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - public function query(string $name, bool $autoload = true) { |
|
| 439 | - try { |
|
| 440 | - return $this->queryNoFallback($name); |
|
| 441 | - } catch (QueryException $firstException) { |
|
| 442 | - try { |
|
| 443 | - return $this->getServer()->query($name, $autoload); |
|
| 444 | - } catch (QueryException $secondException) { |
|
| 445 | - if ($firstException->getCode() === 1) { |
|
| 446 | - throw $secondException; |
|
| 447 | - } |
|
| 448 | - throw $firstException; |
|
| 449 | - } |
|
| 450 | - } |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * @param string $name |
|
| 455 | - * @return mixed |
|
| 456 | - * @throws QueryException if the query could not be resolved |
|
| 457 | - */ |
|
| 458 | - public function queryNoFallback($name) { |
|
| 459 | - $name = $this->sanitizeName($name); |
|
| 460 | - |
|
| 461 | - if ($this->offsetExists($name)) { |
|
| 462 | - return parent::query($name); |
|
| 463 | - } elseif ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
| 464 | - return parent::query($name); |
|
| 465 | - } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
| 466 | - return parent::query($name); |
|
| 467 | - } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
| 468 | - return parent::query($name); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
| 472 | - ' Class can not be instantiated', 1); |
|
| 473 | - } |
|
| 83 | + /** |
|
| 84 | + * @var array |
|
| 85 | + */ |
|
| 86 | + private $middleWares = []; |
|
| 87 | + |
|
| 88 | + /** @var ServerContainer */ |
|
| 89 | + private $server; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Put your class dependencies in here |
|
| 93 | + * @param string $appName the name of the app |
|
| 94 | + * @param array $urlParams |
|
| 95 | + * @param ServerContainer|null $server |
|
| 96 | + */ |
|
| 97 | + public function __construct($appName, $urlParams = [], ServerContainer $server = null) { |
|
| 98 | + parent::__construct(); |
|
| 99 | + $this['appName'] = $appName; |
|
| 100 | + $this['urlParams'] = $urlParams; |
|
| 101 | + |
|
| 102 | + $this->registerAlias('Request', IRequest::class); |
|
| 103 | + |
|
| 104 | + /** @var \OC\ServerContainer $server */ |
|
| 105 | + if ($server === null) { |
|
| 106 | + $server = \OC::$server; |
|
| 107 | + } |
|
| 108 | + $this->server = $server; |
|
| 109 | + $this->server->registerAppContainer($appName, $this); |
|
| 110 | + |
|
| 111 | + // aliases |
|
| 112 | + /** @deprecated inject $appName */ |
|
| 113 | + $this->registerAlias('AppName', 'appName'); |
|
| 114 | + /** @deprecated inject $webRoot*/ |
|
| 115 | + $this->registerAlias('WebRoot', 'webRoot'); |
|
| 116 | + /** @deprecated inject $userId */ |
|
| 117 | + $this->registerAlias('UserId', 'userId'); |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Core services |
|
| 121 | + */ |
|
| 122 | + $this->registerService(IOutput::class, function () { |
|
| 123 | + return new Output($this->getServer()->getWebRoot()); |
|
| 124 | + }); |
|
| 125 | + |
|
| 126 | + $this->registerService(Folder::class, function () { |
|
| 127 | + return $this->getServer()->getUserFolder(); |
|
| 128 | + }); |
|
| 129 | + |
|
| 130 | + $this->registerService(IAppData::class, function (ContainerInterface $c) { |
|
| 131 | + return $this->getServer()->getAppDataDir($c->get('AppName')); |
|
| 132 | + }); |
|
| 133 | + |
|
| 134 | + $this->registerService(IL10N::class, function (ContainerInterface $c) { |
|
| 135 | + return $this->getServer()->getL10N($c->get('AppName')); |
|
| 136 | + }); |
|
| 137 | + |
|
| 138 | + // Log wrappers |
|
| 139 | + $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { |
|
| 140 | + return new ScopedPsrLogger( |
|
| 141 | + $c->get(PsrLoggerAdapter::class), |
|
| 142 | + $c->get('AppName') |
|
| 143 | + ); |
|
| 144 | + }); |
|
| 145 | + $this->registerService(ILogger::class, function (ContainerInterface $c) { |
|
| 146 | + return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->get('AppName')); |
|
| 147 | + }); |
|
| 148 | + |
|
| 149 | + $this->registerService(IServerContainer::class, function () { |
|
| 150 | + return $this->getServer(); |
|
| 151 | + }); |
|
| 152 | + $this->registerAlias('ServerContainer', IServerContainer::class); |
|
| 153 | + |
|
| 154 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) { |
|
| 155 | + return $c->get(Manager::class); |
|
| 156 | + }); |
|
| 157 | + |
|
| 158 | + $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { |
|
| 159 | + return $c; |
|
| 160 | + }); |
|
| 161 | + $this->registerAlias(IAppContainer::class, ContainerInterface::class); |
|
| 162 | + |
|
| 163 | + // commonly used attributes |
|
| 164 | + $this->registerService('userId', function (ContainerInterface $c) { |
|
| 165 | + return $c->get(IUserSession::class)->getSession()->get('user_id'); |
|
| 166 | + }); |
|
| 167 | + |
|
| 168 | + $this->registerService('webRoot', function (ContainerInterface $c) { |
|
| 169 | + return $c->get(IServerContainer::class)->getWebRoot(); |
|
| 170 | + }); |
|
| 171 | + |
|
| 172 | + $this->registerService('OC_Defaults', function (ContainerInterface $c) { |
|
| 173 | + return $c->get(IServerContainer::class)->getThemingDefaults(); |
|
| 174 | + }); |
|
| 175 | + |
|
| 176 | + $this->registerService('Protocol', function (ContainerInterface $c) { |
|
| 177 | + /** @var \OC\Server $server */ |
|
| 178 | + $server = $c->get(IServerContainer::class); |
|
| 179 | + $protocol = $server->getRequest()->getHttpProtocol(); |
|
| 180 | + return new Http($_SERVER, $protocol); |
|
| 181 | + }); |
|
| 182 | + |
|
| 183 | + $this->registerService('Dispatcher', function (ContainerInterface $c) { |
|
| 184 | + return new Dispatcher( |
|
| 185 | + $c->get('Protocol'), |
|
| 186 | + $c->get(MiddlewareDispatcher::class), |
|
| 187 | + $c->get(IControllerMethodReflector::class), |
|
| 188 | + $c->get(IRequest::class), |
|
| 189 | + $c->get(IConfig::class), |
|
| 190 | + $c->get(IDBConnection::class), |
|
| 191 | + $c->get(LoggerInterface::class), |
|
| 192 | + $c->get(EventLogger::class) |
|
| 193 | + ); |
|
| 194 | + }); |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * App Framework default arguments |
|
| 198 | + */ |
|
| 199 | + $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
| 200 | + $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
| 201 | + $this->registerParameter('corsMaxAge', 1728000); |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Middleware |
|
| 205 | + */ |
|
| 206 | + $this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); |
|
| 207 | + $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) { |
|
| 208 | + $server = $this->getServer(); |
|
| 209 | + |
|
| 210 | + $dispatcher = new MiddlewareDispatcher(); |
|
| 211 | + |
|
| 212 | + $dispatcher->registerMiddleware( |
|
| 213 | + $c->get(OC\AppFramework\Middleware\CompressionMiddleware::class) |
|
| 214 | + ); |
|
| 215 | + |
|
| 216 | + $dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class)); |
|
| 217 | + |
|
| 218 | + $dispatcher->registerMiddleware( |
|
| 219 | + $c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
| 220 | + ); |
|
| 221 | + |
|
| 222 | + $dispatcher->registerMiddleware( |
|
| 223 | + new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
| 224 | + $c->get(IRequest::class), |
|
| 225 | + $c->get(IControllerMethodReflector::class) |
|
| 226 | + ) |
|
| 227 | + ); |
|
| 228 | + $dispatcher->registerMiddleware( |
|
| 229 | + new CORSMiddleware( |
|
| 230 | + $c->get(IRequest::class), |
|
| 231 | + $c->get(IControllerMethodReflector::class), |
|
| 232 | + $c->get(IUserSession::class), |
|
| 233 | + $c->get(OC\Security\Bruteforce\Throttler::class) |
|
| 234 | + ) |
|
| 235 | + ); |
|
| 236 | + $dispatcher->registerMiddleware( |
|
| 237 | + new OCSMiddleware( |
|
| 238 | + $c->get(IRequest::class) |
|
| 239 | + ) |
|
| 240 | + ); |
|
| 241 | + |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + $securityMiddleware = new SecurityMiddleware( |
|
| 245 | + $c->get(IRequest::class), |
|
| 246 | + $c->get(IControllerMethodReflector::class), |
|
| 247 | + $c->get(INavigationManager::class), |
|
| 248 | + $c->get(IURLGenerator::class), |
|
| 249 | + $server->get(LoggerInterface::class), |
|
| 250 | + $c->get('AppName'), |
|
| 251 | + $server->getUserSession()->isLoggedIn(), |
|
| 252 | + $this->getUserId() !== null && $server->getGroupManager()->isAdmin($this->getUserId()), |
|
| 253 | + $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
| 254 | + $server->getAppManager(), |
|
| 255 | + $server->getL10N('lib'), |
|
| 256 | + $c->get(AuthorizedGroupMapper::class), |
|
| 257 | + $server->get(IUserSession::class) |
|
| 258 | + ); |
|
| 259 | + $dispatcher->registerMiddleware($securityMiddleware); |
|
| 260 | + $dispatcher->registerMiddleware( |
|
| 261 | + new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
| 262 | + $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
| 263 | + $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
| 264 | + $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
| 265 | + ) |
|
| 266 | + ); |
|
| 267 | + $dispatcher->registerMiddleware( |
|
| 268 | + $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) |
|
| 269 | + ); |
|
| 270 | + $dispatcher->registerMiddleware( |
|
| 271 | + new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
| 272 | + $c->get(IControllerMethodReflector::class), |
|
| 273 | + $c->get(ISession::class), |
|
| 274 | + $c->get(IUserSession::class), |
|
| 275 | + $c->get(ITimeFactory::class) |
|
| 276 | + ) |
|
| 277 | + ); |
|
| 278 | + $dispatcher->registerMiddleware( |
|
| 279 | + new TwoFactorMiddleware( |
|
| 280 | + $c->get(OC\Authentication\TwoFactorAuth\Manager::class), |
|
| 281 | + $c->get(IUserSession::class), |
|
| 282 | + $c->get(ISession::class), |
|
| 283 | + $c->get(IURLGenerator::class), |
|
| 284 | + $c->get(IControllerMethodReflector::class), |
|
| 285 | + $c->get(IRequest::class) |
|
| 286 | + ) |
|
| 287 | + ); |
|
| 288 | + $dispatcher->registerMiddleware( |
|
| 289 | + new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
| 290 | + $c->get(IControllerMethodReflector::class), |
|
| 291 | + $c->get(OC\Security\Bruteforce\Throttler::class), |
|
| 292 | + $c->get(IRequest::class) |
|
| 293 | + ) |
|
| 294 | + ); |
|
| 295 | + $dispatcher->registerMiddleware( |
|
| 296 | + new RateLimitingMiddleware( |
|
| 297 | + $c->get(IRequest::class), |
|
| 298 | + $c->get(IUserSession::class), |
|
| 299 | + $c->get(IControllerMethodReflector::class), |
|
| 300 | + $c->get(OC\Security\RateLimiting\Limiter::class) |
|
| 301 | + ) |
|
| 302 | + ); |
|
| 303 | + $dispatcher->registerMiddleware( |
|
| 304 | + new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
| 305 | + $c->get(IRequest::class), |
|
| 306 | + $c->get(ISession::class), |
|
| 307 | + $c->get(\OCP\IConfig::class) |
|
| 308 | + ) |
|
| 309 | + ); |
|
| 310 | + $dispatcher->registerMiddleware( |
|
| 311 | + $c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
| 312 | + ); |
|
| 313 | + |
|
| 314 | + foreach ($this->middleWares as $middleWare) { |
|
| 315 | + $dispatcher->registerMiddleware($c->get($middleWare)); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + $dispatcher->registerMiddleware( |
|
| 319 | + new SessionMiddleware( |
|
| 320 | + $c->get(IControllerMethodReflector::class), |
|
| 321 | + $c->get(ISession::class) |
|
| 322 | + ) |
|
| 323 | + ); |
|
| 324 | + return $dispatcher; |
|
| 325 | + }); |
|
| 326 | + |
|
| 327 | + $this->registerService(IAppConfig::class, function (ContainerInterface $c) { |
|
| 328 | + return new OC\AppFramework\Services\AppConfig( |
|
| 329 | + $c->get(IConfig::class), |
|
| 330 | + $c->get('AppName') |
|
| 331 | + ); |
|
| 332 | + }); |
|
| 333 | + $this->registerService(IInitialState::class, function (ContainerInterface $c) { |
|
| 334 | + return new OC\AppFramework\Services\InitialState( |
|
| 335 | + $c->get(IInitialStateService::class), |
|
| 336 | + $c->get('AppName') |
|
| 337 | + ); |
|
| 338 | + }); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * @return \OCP\IServerContainer |
|
| 343 | + */ |
|
| 344 | + public function getServer() { |
|
| 345 | + return $this->server; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * @param string $middleWare |
|
| 350 | + * @return boolean|null |
|
| 351 | + */ |
|
| 352 | + public function registerMiddleWare($middleWare) { |
|
| 353 | + if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
| 354 | + return false; |
|
| 355 | + } |
|
| 356 | + $this->middleWares[] = $middleWare; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * used to return the appname of the set application |
|
| 361 | + * @return string the name of your application |
|
| 362 | + */ |
|
| 363 | + public function getAppName() { |
|
| 364 | + return $this->query('AppName'); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * @deprecated use IUserSession->isLoggedIn() |
|
| 369 | + * @return boolean |
|
| 370 | + */ |
|
| 371 | + public function isLoggedIn() { |
|
| 372 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * @deprecated use IGroupManager->isAdmin($userId) |
|
| 377 | + * @return boolean |
|
| 378 | + */ |
|
| 379 | + public function isAdminUser() { |
|
| 380 | + $uid = $this->getUserId(); |
|
| 381 | + return \OC_User::isAdminUser($uid); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + private function getUserId() { |
|
| 385 | + return $this->getServer()->getSession()->get('user_id'); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * @deprecated use the ILogger instead |
|
| 390 | + * @param string $message |
|
| 391 | + * @param string $level |
|
| 392 | + * @return mixed |
|
| 393 | + */ |
|
| 394 | + public function log($message, $level) { |
|
| 395 | + switch ($level) { |
|
| 396 | + case 'debug': |
|
| 397 | + $level = ILogger::DEBUG; |
|
| 398 | + break; |
|
| 399 | + case 'info': |
|
| 400 | + $level = ILogger::INFO; |
|
| 401 | + break; |
|
| 402 | + case 'warn': |
|
| 403 | + $level = ILogger::WARN; |
|
| 404 | + break; |
|
| 405 | + case 'fatal': |
|
| 406 | + $level = ILogger::FATAL; |
|
| 407 | + break; |
|
| 408 | + default: |
|
| 409 | + $level = ILogger::ERROR; |
|
| 410 | + break; |
|
| 411 | + } |
|
| 412 | + \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * Register a capability |
|
| 417 | + * |
|
| 418 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
| 419 | + */ |
|
| 420 | + public function registerCapability($serviceName) { |
|
| 421 | + $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
| 422 | + return $this->query($serviceName); |
|
| 423 | + }); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + public function has($id): bool { |
|
| 427 | + if (parent::has($id)) { |
|
| 428 | + return true; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + if ($this->server->has($id, true)) { |
|
| 432 | + return true; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + return false; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + public function query(string $name, bool $autoload = true) { |
|
| 439 | + try { |
|
| 440 | + return $this->queryNoFallback($name); |
|
| 441 | + } catch (QueryException $firstException) { |
|
| 442 | + try { |
|
| 443 | + return $this->getServer()->query($name, $autoload); |
|
| 444 | + } catch (QueryException $secondException) { |
|
| 445 | + if ($firstException->getCode() === 1) { |
|
| 446 | + throw $secondException; |
|
| 447 | + } |
|
| 448 | + throw $firstException; |
|
| 449 | + } |
|
| 450 | + } |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * @param string $name |
|
| 455 | + * @return mixed |
|
| 456 | + * @throws QueryException if the query could not be resolved |
|
| 457 | + */ |
|
| 458 | + public function queryNoFallback($name) { |
|
| 459 | + $name = $this->sanitizeName($name); |
|
| 460 | + |
|
| 461 | + if ($this->offsetExists($name)) { |
|
| 462 | + return parent::query($name); |
|
| 463 | + } elseif ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
| 464 | + return parent::query($name); |
|
| 465 | + } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
| 466 | + return parent::query($name); |
|
| 467 | + } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
| 468 | + return parent::query($name); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + throw new QueryException('Could not resolve ' . $name . '!' . |
|
| 472 | + ' Class can not be instantiated', 1); |
|
| 473 | + } |
|
| 474 | 474 | } |
@@ -119,68 +119,68 @@ discard block |
||
| 119 | 119 | /** |
| 120 | 120 | * Core services |
| 121 | 121 | */ |
| 122 | - $this->registerService(IOutput::class, function () { |
|
| 122 | + $this->registerService(IOutput::class, function() { |
|
| 123 | 123 | return new Output($this->getServer()->getWebRoot()); |
| 124 | 124 | }); |
| 125 | 125 | |
| 126 | - $this->registerService(Folder::class, function () { |
|
| 126 | + $this->registerService(Folder::class, function() { |
|
| 127 | 127 | return $this->getServer()->getUserFolder(); |
| 128 | 128 | }); |
| 129 | 129 | |
| 130 | - $this->registerService(IAppData::class, function (ContainerInterface $c) { |
|
| 130 | + $this->registerService(IAppData::class, function(ContainerInterface $c) { |
|
| 131 | 131 | return $this->getServer()->getAppDataDir($c->get('AppName')); |
| 132 | 132 | }); |
| 133 | 133 | |
| 134 | - $this->registerService(IL10N::class, function (ContainerInterface $c) { |
|
| 134 | + $this->registerService(IL10N::class, function(ContainerInterface $c) { |
|
| 135 | 135 | return $this->getServer()->getL10N($c->get('AppName')); |
| 136 | 136 | }); |
| 137 | 137 | |
| 138 | 138 | // Log wrappers |
| 139 | - $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { |
|
| 139 | + $this->registerService(LoggerInterface::class, function(ContainerInterface $c) { |
|
| 140 | 140 | return new ScopedPsrLogger( |
| 141 | 141 | $c->get(PsrLoggerAdapter::class), |
| 142 | 142 | $c->get('AppName') |
| 143 | 143 | ); |
| 144 | 144 | }); |
| 145 | - $this->registerService(ILogger::class, function (ContainerInterface $c) { |
|
| 145 | + $this->registerService(ILogger::class, function(ContainerInterface $c) { |
|
| 146 | 146 | return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->get('AppName')); |
| 147 | 147 | }); |
| 148 | 148 | |
| 149 | - $this->registerService(IServerContainer::class, function () { |
|
| 149 | + $this->registerService(IServerContainer::class, function() { |
|
| 150 | 150 | return $this->getServer(); |
| 151 | 151 | }); |
| 152 | 152 | $this->registerAlias('ServerContainer', IServerContainer::class); |
| 153 | 153 | |
| 154 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) { |
|
| 154 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function(ContainerInterface $c) { |
|
| 155 | 155 | return $c->get(Manager::class); |
| 156 | 156 | }); |
| 157 | 157 | |
| 158 | - $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { |
|
| 158 | + $this->registerService(ContainerInterface::class, function(ContainerInterface $c) { |
|
| 159 | 159 | return $c; |
| 160 | 160 | }); |
| 161 | 161 | $this->registerAlias(IAppContainer::class, ContainerInterface::class); |
| 162 | 162 | |
| 163 | 163 | // commonly used attributes |
| 164 | - $this->registerService('userId', function (ContainerInterface $c) { |
|
| 164 | + $this->registerService('userId', function(ContainerInterface $c) { |
|
| 165 | 165 | return $c->get(IUserSession::class)->getSession()->get('user_id'); |
| 166 | 166 | }); |
| 167 | 167 | |
| 168 | - $this->registerService('webRoot', function (ContainerInterface $c) { |
|
| 168 | + $this->registerService('webRoot', function(ContainerInterface $c) { |
|
| 169 | 169 | return $c->get(IServerContainer::class)->getWebRoot(); |
| 170 | 170 | }); |
| 171 | 171 | |
| 172 | - $this->registerService('OC_Defaults', function (ContainerInterface $c) { |
|
| 172 | + $this->registerService('OC_Defaults', function(ContainerInterface $c) { |
|
| 173 | 173 | return $c->get(IServerContainer::class)->getThemingDefaults(); |
| 174 | 174 | }); |
| 175 | 175 | |
| 176 | - $this->registerService('Protocol', function (ContainerInterface $c) { |
|
| 176 | + $this->registerService('Protocol', function(ContainerInterface $c) { |
|
| 177 | 177 | /** @var \OC\Server $server */ |
| 178 | 178 | $server = $c->get(IServerContainer::class); |
| 179 | 179 | $protocol = $server->getRequest()->getHttpProtocol(); |
| 180 | 180 | return new Http($_SERVER, $protocol); |
| 181 | 181 | }); |
| 182 | 182 | |
| 183 | - $this->registerService('Dispatcher', function (ContainerInterface $c) { |
|
| 183 | + $this->registerService('Dispatcher', function(ContainerInterface $c) { |
|
| 184 | 184 | return new Dispatcher( |
| 185 | 185 | $c->get('Protocol'), |
| 186 | 186 | $c->get(MiddlewareDispatcher::class), |
@@ -204,7 +204,7 @@ discard block |
||
| 204 | 204 | * Middleware |
| 205 | 205 | */ |
| 206 | 206 | $this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); |
| 207 | - $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) { |
|
| 207 | + $this->registerService(MiddlewareDispatcher::class, function(ContainerInterface $c) { |
|
| 208 | 208 | $server = $this->getServer(); |
| 209 | 209 | |
| 210 | 210 | $dispatcher = new MiddlewareDispatcher(); |
@@ -324,13 +324,13 @@ discard block |
||
| 324 | 324 | return $dispatcher; |
| 325 | 325 | }); |
| 326 | 326 | |
| 327 | - $this->registerService(IAppConfig::class, function (ContainerInterface $c) { |
|
| 327 | + $this->registerService(IAppConfig::class, function(ContainerInterface $c) { |
|
| 328 | 328 | return new OC\AppFramework\Services\AppConfig( |
| 329 | 329 | $c->get(IConfig::class), |
| 330 | 330 | $c->get('AppName') |
| 331 | 331 | ); |
| 332 | 332 | }); |
| 333 | - $this->registerService(IInitialState::class, function (ContainerInterface $c) { |
|
| 333 | + $this->registerService(IInitialState::class, function(ContainerInterface $c) { |
|
| 334 | 334 | return new OC\AppFramework\Services\InitialState( |
| 335 | 335 | $c->get(IInitialStateService::class), |
| 336 | 336 | $c->get('AppName') |
@@ -418,7 +418,7 @@ discard block |
||
| 418 | 418 | * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
| 419 | 419 | */ |
| 420 | 420 | public function registerCapability($serviceName) { |
| 421 | - $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
| 421 | + $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
| 422 | 422 | return $this->query($serviceName); |
| 423 | 423 | }); |
| 424 | 424 | } |
@@ -464,11 +464,11 @@ discard block |
||
| 464 | 464 | return parent::query($name); |
| 465 | 465 | } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
| 466 | 466 | return parent::query($name); |
| 467 | - } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
| 467 | + } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']).'\\') === 0) { |
|
| 468 | 468 | return parent::query($name); |
| 469 | 469 | } |
| 470 | 470 | |
| 471 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
| 471 | + throw new QueryException('Could not resolve '.$name.'!'. |
|
| 472 | 472 | ' Class can not be instantiated', 1); |
| 473 | 473 | } |
| 474 | 474 | } |