@@ -70,354 +70,354 @@ |
||
| 70 | 70 | |
| 71 | 71 | class DIContainer extends SimpleContainer implements IAppContainer { |
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - private $middleWares = []; |
|
| 77 | - |
|
| 78 | - /** @var ServerContainer */ |
|
| 79 | - private $server; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Put your class dependencies in here |
|
| 83 | - * @param string $appName the name of the app |
|
| 84 | - * @param array $urlParams |
|
| 85 | - * @param ServerContainer|null $server |
|
| 86 | - */ |
|
| 87 | - public function __construct($appName, $urlParams = [], ServerContainer $server = null) { |
|
| 88 | - parent::__construct(); |
|
| 89 | - $this['AppName'] = $appName; |
|
| 90 | - $this['urlParams'] = $urlParams; |
|
| 91 | - |
|
| 92 | - $this->registerAlias('Request', IRequest::class); |
|
| 93 | - |
|
| 94 | - /** @var \OC\ServerContainer $server */ |
|
| 95 | - if ($server === null) { |
|
| 96 | - $server = \OC::$server; |
|
| 97 | - } |
|
| 98 | - $this->server = $server; |
|
| 99 | - $this->server->registerAppContainer($appName, $this); |
|
| 100 | - |
|
| 101 | - // aliases |
|
| 102 | - $this->registerAlias('appName', 'AppName'); |
|
| 103 | - $this->registerAlias('webRoot', 'WebRoot'); |
|
| 104 | - $this->registerAlias('userId', 'UserId'); |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Core services |
|
| 108 | - */ |
|
| 109 | - $this->registerService(IOutput::class, function () { |
|
| 110 | - return new Output($this->getServer()->getWebRoot()); |
|
| 111 | - }); |
|
| 112 | - |
|
| 113 | - $this->registerService(Folder::class, function () { |
|
| 114 | - return $this->getServer()->getUserFolder(); |
|
| 115 | - }); |
|
| 116 | - |
|
| 117 | - $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
| 118 | - return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
| 119 | - }); |
|
| 120 | - |
|
| 121 | - $this->registerService(IL10N::class, function ($c) { |
|
| 122 | - return $this->getServer()->getL10N($c->query('AppName')); |
|
| 123 | - }); |
|
| 124 | - |
|
| 125 | - // Log wrapper |
|
| 126 | - $this->registerService(ILogger::class, function ($c) { |
|
| 127 | - return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
| 128 | - }); |
|
| 129 | - |
|
| 130 | - $this->registerService(IServerContainer::class, function () { |
|
| 131 | - return $this->getServer(); |
|
| 132 | - }); |
|
| 133 | - $this->registerAlias('ServerContainer', IServerContainer::class); |
|
| 134 | - |
|
| 135 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
| 136 | - return $c->query(Manager::class); |
|
| 137 | - }); |
|
| 138 | - |
|
| 139 | - $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
| 140 | - return $c; |
|
| 141 | - }); |
|
| 142 | - |
|
| 143 | - // commonly used attributes |
|
| 144 | - $this->registerService('UserId', function ($c) { |
|
| 145 | - return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
| 146 | - }); |
|
| 147 | - |
|
| 148 | - $this->registerService('WebRoot', function ($c) { |
|
| 149 | - return $c->query('ServerContainer')->getWebRoot(); |
|
| 150 | - }); |
|
| 151 | - |
|
| 152 | - $this->registerService('OC_Defaults', function ($c) { |
|
| 153 | - return $c->getServer()->getThemingDefaults(); |
|
| 154 | - }); |
|
| 155 | - |
|
| 156 | - $this->registerService('Protocol', function ($c) { |
|
| 157 | - /** @var \OC\Server $server */ |
|
| 158 | - $server = $c->query('ServerContainer'); |
|
| 159 | - $protocol = $server->getRequest()->getHttpProtocol(); |
|
| 160 | - return new Http($_SERVER, $protocol); |
|
| 161 | - }); |
|
| 162 | - |
|
| 163 | - $this->registerService('Dispatcher', function ($c) { |
|
| 164 | - return new Dispatcher( |
|
| 165 | - $c['Protocol'], |
|
| 166 | - $c['MiddlewareDispatcher'], |
|
| 167 | - $c->query(IControllerMethodReflector::class), |
|
| 168 | - $c['Request'] |
|
| 169 | - ); |
|
| 170 | - }); |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * App Framework default arguments |
|
| 174 | - */ |
|
| 175 | - $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
| 176 | - $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
| 177 | - $this->registerParameter('corsMaxAge', 1728000); |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Middleware |
|
| 181 | - */ |
|
| 182 | - $this->registerService('MiddlewareDispatcher', function (SimpleContainer $c) { |
|
| 183 | - $server = $this->getServer(); |
|
| 184 | - |
|
| 185 | - $dispatcher = new MiddlewareDispatcher(); |
|
| 186 | - |
|
| 187 | - $dispatcher->registerMiddleware( |
|
| 188 | - $c->query(OC\AppFramework\Middleware\CompressionMiddleware::class) |
|
| 189 | - ); |
|
| 190 | - |
|
| 191 | - $dispatcher->registerMiddleware( |
|
| 192 | - $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
| 193 | - ); |
|
| 194 | - |
|
| 195 | - $dispatcher->registerMiddleware( |
|
| 196 | - new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
| 197 | - $c->query(IRequest::class), |
|
| 198 | - $c->query(IControllerMethodReflector::class) |
|
| 199 | - ) |
|
| 200 | - ); |
|
| 201 | - $dispatcher->registerMiddleware( |
|
| 202 | - new CORSMiddleware( |
|
| 203 | - $c->query(IRequest::class), |
|
| 204 | - $c->query(IControllerMethodReflector::class), |
|
| 205 | - $c->query(IUserSession::class), |
|
| 206 | - $c->query(OC\Security\Bruteforce\Throttler::class) |
|
| 207 | - ) |
|
| 208 | - ); |
|
| 209 | - $dispatcher->registerMiddleware( |
|
| 210 | - new OCSMiddleware( |
|
| 211 | - $c->query(IRequest::class) |
|
| 212 | - ) |
|
| 213 | - ); |
|
| 214 | - |
|
| 215 | - $securityMiddleware = new SecurityMiddleware( |
|
| 216 | - $c->query(IRequest::class), |
|
| 217 | - $c->query(IControllerMethodReflector::class), |
|
| 218 | - $c->query(INavigationManager::class), |
|
| 219 | - $c->query(IURLGenerator::class), |
|
| 220 | - $server->getLogger(), |
|
| 221 | - $c['AppName'], |
|
| 222 | - $server->getUserSession()->isLoggedIn(), |
|
| 223 | - $server->getGroupManager()->isAdmin($this->getUserId()), |
|
| 224 | - $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
| 225 | - $server->getAppManager(), |
|
| 226 | - $server->getL10N('lib') |
|
| 227 | - ); |
|
| 228 | - $dispatcher->registerMiddleware($securityMiddleware); |
|
| 229 | - $dispatcher->registerMiddleware( |
|
| 230 | - new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
| 231 | - $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
| 232 | - $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
| 233 | - $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
| 234 | - ) |
|
| 235 | - ); |
|
| 236 | - $dispatcher->registerMiddleware( |
|
| 237 | - $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) |
|
| 238 | - ); |
|
| 239 | - $dispatcher->registerMiddleware( |
|
| 240 | - new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
| 241 | - $c->query(IControllerMethodReflector::class), |
|
| 242 | - $c->query(ISession::class), |
|
| 243 | - $c->query(IUserSession::class), |
|
| 244 | - $c->query(ITimeFactory::class) |
|
| 245 | - ) |
|
| 246 | - ); |
|
| 247 | - $dispatcher->registerMiddleware( |
|
| 248 | - new TwoFactorMiddleware( |
|
| 249 | - $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
| 250 | - $c->query(IUserSession::class), |
|
| 251 | - $c->query(ISession::class), |
|
| 252 | - $c->query(IURLGenerator::class), |
|
| 253 | - $c->query(IControllerMethodReflector::class), |
|
| 254 | - $c->query(IRequest::class) |
|
| 255 | - ) |
|
| 256 | - ); |
|
| 257 | - $dispatcher->registerMiddleware( |
|
| 258 | - new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
| 259 | - $c->query(IControllerMethodReflector::class), |
|
| 260 | - $c->query(OC\Security\Bruteforce\Throttler::class), |
|
| 261 | - $c->query(IRequest::class) |
|
| 262 | - ) |
|
| 263 | - ); |
|
| 264 | - $dispatcher->registerMiddleware( |
|
| 265 | - new RateLimitingMiddleware( |
|
| 266 | - $c->query(IRequest::class), |
|
| 267 | - $c->query(IUserSession::class), |
|
| 268 | - $c->query(IControllerMethodReflector::class), |
|
| 269 | - $c->query(OC\Security\RateLimiting\Limiter::class) |
|
| 270 | - ) |
|
| 271 | - ); |
|
| 272 | - $dispatcher->registerMiddleware( |
|
| 273 | - new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
| 274 | - $c->query(IRequest::class), |
|
| 275 | - $c->query(ISession::class), |
|
| 276 | - $c->query(\OCP\IConfig::class) |
|
| 277 | - ) |
|
| 278 | - ); |
|
| 279 | - $dispatcher->registerMiddleware( |
|
| 280 | - $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
| 281 | - ); |
|
| 282 | - |
|
| 283 | - foreach ($this->middleWares as $middleWare) { |
|
| 284 | - $dispatcher->registerMiddleware($c->query($middleWare)); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - $dispatcher->registerMiddleware( |
|
| 288 | - new SessionMiddleware( |
|
| 289 | - $c->query(IControllerMethodReflector::class), |
|
| 290 | - $c->query(ISession::class) |
|
| 291 | - ) |
|
| 292 | - ); |
|
| 293 | - return $dispatcher; |
|
| 294 | - }); |
|
| 295 | - |
|
| 296 | - $this->registerAlias(IAppConfig::class, OC\AppFramework\Services\AppConfig::class); |
|
| 297 | - $this->registerAlias(IInitialState::class, OC\AppFramework\Services\InitialState::class); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * @return \OCP\IServerContainer |
|
| 302 | - */ |
|
| 303 | - public function getServer() { |
|
| 304 | - return $this->server; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * @param string $middleWare |
|
| 309 | - * @return boolean|null |
|
| 310 | - */ |
|
| 311 | - public function registerMiddleWare($middleWare) { |
|
| 312 | - if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
| 313 | - return false; |
|
| 314 | - } |
|
| 315 | - $this->middleWares[] = $middleWare; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * used to return the appname of the set application |
|
| 320 | - * @return string the name of your application |
|
| 321 | - */ |
|
| 322 | - public function getAppName() { |
|
| 323 | - return $this->query('AppName'); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * @deprecated use IUserSession->isLoggedIn() |
|
| 328 | - * @return boolean |
|
| 329 | - */ |
|
| 330 | - public function isLoggedIn() { |
|
| 331 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * @deprecated use IGroupManager->isAdmin($userId) |
|
| 336 | - * @return boolean |
|
| 337 | - */ |
|
| 338 | - public function isAdminUser() { |
|
| 339 | - $uid = $this->getUserId(); |
|
| 340 | - return \OC_User::isAdminUser($uid); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - private function getUserId() { |
|
| 344 | - return $this->getServer()->getSession()->get('user_id'); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * @deprecated use the ILogger instead |
|
| 349 | - * @param string $message |
|
| 350 | - * @param string $level |
|
| 351 | - * @return mixed |
|
| 352 | - */ |
|
| 353 | - public function log($message, $level) { |
|
| 354 | - switch ($level) { |
|
| 355 | - case 'debug': |
|
| 356 | - $level = ILogger::DEBUG; |
|
| 357 | - break; |
|
| 358 | - case 'info': |
|
| 359 | - $level = ILogger::INFO; |
|
| 360 | - break; |
|
| 361 | - case 'warn': |
|
| 362 | - $level = ILogger::WARN; |
|
| 363 | - break; |
|
| 364 | - case 'fatal': |
|
| 365 | - $level = ILogger::FATAL; |
|
| 366 | - break; |
|
| 367 | - default: |
|
| 368 | - $level = ILogger::ERROR; |
|
| 369 | - break; |
|
| 370 | - } |
|
| 371 | - \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Register a capability |
|
| 376 | - * |
|
| 377 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
| 378 | - */ |
|
| 379 | - public function registerCapability($serviceName) { |
|
| 380 | - $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
| 381 | - return $this->query($serviceName); |
|
| 382 | - }); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - public function query(string $name, bool $autoload = true) { |
|
| 386 | - try { |
|
| 387 | - return $this->queryNoFallback($name); |
|
| 388 | - } catch (QueryException $firstException) { |
|
| 389 | - try { |
|
| 390 | - return $this->getServer()->query($name, $autoload); |
|
| 391 | - } catch (QueryException $secondException) { |
|
| 392 | - if ($firstException->getCode() === 1) { |
|
| 393 | - throw $secondException; |
|
| 394 | - } |
|
| 395 | - throw $firstException; |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * @param string $name |
|
| 402 | - * @return mixed |
|
| 403 | - * @throws QueryException if the query could not be resolved |
|
| 404 | - */ |
|
| 405 | - public function queryNoFallback($name) { |
|
| 406 | - $name = $this->sanitizeName($name); |
|
| 407 | - |
|
| 408 | - if ($this->offsetExists($name)) { |
|
| 409 | - return parent::query($name); |
|
| 410 | - } else { |
|
| 411 | - if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
| 412 | - return parent::query($name); |
|
| 413 | - } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
| 414 | - return parent::query($name); |
|
| 415 | - } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
| 416 | - return parent::query($name); |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
| 421 | - ' Class can not be instantiated', 1); |
|
| 422 | - } |
|
| 73 | + /** |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + private $middleWares = []; |
|
| 77 | + |
|
| 78 | + /** @var ServerContainer */ |
|
| 79 | + private $server; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Put your class dependencies in here |
|
| 83 | + * @param string $appName the name of the app |
|
| 84 | + * @param array $urlParams |
|
| 85 | + * @param ServerContainer|null $server |
|
| 86 | + */ |
|
| 87 | + public function __construct($appName, $urlParams = [], ServerContainer $server = null) { |
|
| 88 | + parent::__construct(); |
|
| 89 | + $this['AppName'] = $appName; |
|
| 90 | + $this['urlParams'] = $urlParams; |
|
| 91 | + |
|
| 92 | + $this->registerAlias('Request', IRequest::class); |
|
| 93 | + |
|
| 94 | + /** @var \OC\ServerContainer $server */ |
|
| 95 | + if ($server === null) { |
|
| 96 | + $server = \OC::$server; |
|
| 97 | + } |
|
| 98 | + $this->server = $server; |
|
| 99 | + $this->server->registerAppContainer($appName, $this); |
|
| 100 | + |
|
| 101 | + // aliases |
|
| 102 | + $this->registerAlias('appName', 'AppName'); |
|
| 103 | + $this->registerAlias('webRoot', 'WebRoot'); |
|
| 104 | + $this->registerAlias('userId', 'UserId'); |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Core services |
|
| 108 | + */ |
|
| 109 | + $this->registerService(IOutput::class, function () { |
|
| 110 | + return new Output($this->getServer()->getWebRoot()); |
|
| 111 | + }); |
|
| 112 | + |
|
| 113 | + $this->registerService(Folder::class, function () { |
|
| 114 | + return $this->getServer()->getUserFolder(); |
|
| 115 | + }); |
|
| 116 | + |
|
| 117 | + $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
| 118 | + return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
| 119 | + }); |
|
| 120 | + |
|
| 121 | + $this->registerService(IL10N::class, function ($c) { |
|
| 122 | + return $this->getServer()->getL10N($c->query('AppName')); |
|
| 123 | + }); |
|
| 124 | + |
|
| 125 | + // Log wrapper |
|
| 126 | + $this->registerService(ILogger::class, function ($c) { |
|
| 127 | + return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
| 128 | + }); |
|
| 129 | + |
|
| 130 | + $this->registerService(IServerContainer::class, function () { |
|
| 131 | + return $this->getServer(); |
|
| 132 | + }); |
|
| 133 | + $this->registerAlias('ServerContainer', IServerContainer::class); |
|
| 134 | + |
|
| 135 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
| 136 | + return $c->query(Manager::class); |
|
| 137 | + }); |
|
| 138 | + |
|
| 139 | + $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
| 140 | + return $c; |
|
| 141 | + }); |
|
| 142 | + |
|
| 143 | + // commonly used attributes |
|
| 144 | + $this->registerService('UserId', function ($c) { |
|
| 145 | + return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
| 146 | + }); |
|
| 147 | + |
|
| 148 | + $this->registerService('WebRoot', function ($c) { |
|
| 149 | + return $c->query('ServerContainer')->getWebRoot(); |
|
| 150 | + }); |
|
| 151 | + |
|
| 152 | + $this->registerService('OC_Defaults', function ($c) { |
|
| 153 | + return $c->getServer()->getThemingDefaults(); |
|
| 154 | + }); |
|
| 155 | + |
|
| 156 | + $this->registerService('Protocol', function ($c) { |
|
| 157 | + /** @var \OC\Server $server */ |
|
| 158 | + $server = $c->query('ServerContainer'); |
|
| 159 | + $protocol = $server->getRequest()->getHttpProtocol(); |
|
| 160 | + return new Http($_SERVER, $protocol); |
|
| 161 | + }); |
|
| 162 | + |
|
| 163 | + $this->registerService('Dispatcher', function ($c) { |
|
| 164 | + return new Dispatcher( |
|
| 165 | + $c['Protocol'], |
|
| 166 | + $c['MiddlewareDispatcher'], |
|
| 167 | + $c->query(IControllerMethodReflector::class), |
|
| 168 | + $c['Request'] |
|
| 169 | + ); |
|
| 170 | + }); |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * App Framework default arguments |
|
| 174 | + */ |
|
| 175 | + $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
| 176 | + $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
| 177 | + $this->registerParameter('corsMaxAge', 1728000); |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Middleware |
|
| 181 | + */ |
|
| 182 | + $this->registerService('MiddlewareDispatcher', function (SimpleContainer $c) { |
|
| 183 | + $server = $this->getServer(); |
|
| 184 | + |
|
| 185 | + $dispatcher = new MiddlewareDispatcher(); |
|
| 186 | + |
|
| 187 | + $dispatcher->registerMiddleware( |
|
| 188 | + $c->query(OC\AppFramework\Middleware\CompressionMiddleware::class) |
|
| 189 | + ); |
|
| 190 | + |
|
| 191 | + $dispatcher->registerMiddleware( |
|
| 192 | + $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
| 193 | + ); |
|
| 194 | + |
|
| 195 | + $dispatcher->registerMiddleware( |
|
| 196 | + new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
| 197 | + $c->query(IRequest::class), |
|
| 198 | + $c->query(IControllerMethodReflector::class) |
|
| 199 | + ) |
|
| 200 | + ); |
|
| 201 | + $dispatcher->registerMiddleware( |
|
| 202 | + new CORSMiddleware( |
|
| 203 | + $c->query(IRequest::class), |
|
| 204 | + $c->query(IControllerMethodReflector::class), |
|
| 205 | + $c->query(IUserSession::class), |
|
| 206 | + $c->query(OC\Security\Bruteforce\Throttler::class) |
|
| 207 | + ) |
|
| 208 | + ); |
|
| 209 | + $dispatcher->registerMiddleware( |
|
| 210 | + new OCSMiddleware( |
|
| 211 | + $c->query(IRequest::class) |
|
| 212 | + ) |
|
| 213 | + ); |
|
| 214 | + |
|
| 215 | + $securityMiddleware = new SecurityMiddleware( |
|
| 216 | + $c->query(IRequest::class), |
|
| 217 | + $c->query(IControllerMethodReflector::class), |
|
| 218 | + $c->query(INavigationManager::class), |
|
| 219 | + $c->query(IURLGenerator::class), |
|
| 220 | + $server->getLogger(), |
|
| 221 | + $c['AppName'], |
|
| 222 | + $server->getUserSession()->isLoggedIn(), |
|
| 223 | + $server->getGroupManager()->isAdmin($this->getUserId()), |
|
| 224 | + $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
| 225 | + $server->getAppManager(), |
|
| 226 | + $server->getL10N('lib') |
|
| 227 | + ); |
|
| 228 | + $dispatcher->registerMiddleware($securityMiddleware); |
|
| 229 | + $dispatcher->registerMiddleware( |
|
| 230 | + new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
| 231 | + $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
| 232 | + $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
| 233 | + $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
| 234 | + ) |
|
| 235 | + ); |
|
| 236 | + $dispatcher->registerMiddleware( |
|
| 237 | + $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) |
|
| 238 | + ); |
|
| 239 | + $dispatcher->registerMiddleware( |
|
| 240 | + new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
| 241 | + $c->query(IControllerMethodReflector::class), |
|
| 242 | + $c->query(ISession::class), |
|
| 243 | + $c->query(IUserSession::class), |
|
| 244 | + $c->query(ITimeFactory::class) |
|
| 245 | + ) |
|
| 246 | + ); |
|
| 247 | + $dispatcher->registerMiddleware( |
|
| 248 | + new TwoFactorMiddleware( |
|
| 249 | + $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
| 250 | + $c->query(IUserSession::class), |
|
| 251 | + $c->query(ISession::class), |
|
| 252 | + $c->query(IURLGenerator::class), |
|
| 253 | + $c->query(IControllerMethodReflector::class), |
|
| 254 | + $c->query(IRequest::class) |
|
| 255 | + ) |
|
| 256 | + ); |
|
| 257 | + $dispatcher->registerMiddleware( |
|
| 258 | + new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
| 259 | + $c->query(IControllerMethodReflector::class), |
|
| 260 | + $c->query(OC\Security\Bruteforce\Throttler::class), |
|
| 261 | + $c->query(IRequest::class) |
|
| 262 | + ) |
|
| 263 | + ); |
|
| 264 | + $dispatcher->registerMiddleware( |
|
| 265 | + new RateLimitingMiddleware( |
|
| 266 | + $c->query(IRequest::class), |
|
| 267 | + $c->query(IUserSession::class), |
|
| 268 | + $c->query(IControllerMethodReflector::class), |
|
| 269 | + $c->query(OC\Security\RateLimiting\Limiter::class) |
|
| 270 | + ) |
|
| 271 | + ); |
|
| 272 | + $dispatcher->registerMiddleware( |
|
| 273 | + new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
| 274 | + $c->query(IRequest::class), |
|
| 275 | + $c->query(ISession::class), |
|
| 276 | + $c->query(\OCP\IConfig::class) |
|
| 277 | + ) |
|
| 278 | + ); |
|
| 279 | + $dispatcher->registerMiddleware( |
|
| 280 | + $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
| 281 | + ); |
|
| 282 | + |
|
| 283 | + foreach ($this->middleWares as $middleWare) { |
|
| 284 | + $dispatcher->registerMiddleware($c->query($middleWare)); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + $dispatcher->registerMiddleware( |
|
| 288 | + new SessionMiddleware( |
|
| 289 | + $c->query(IControllerMethodReflector::class), |
|
| 290 | + $c->query(ISession::class) |
|
| 291 | + ) |
|
| 292 | + ); |
|
| 293 | + return $dispatcher; |
|
| 294 | + }); |
|
| 295 | + |
|
| 296 | + $this->registerAlias(IAppConfig::class, OC\AppFramework\Services\AppConfig::class); |
|
| 297 | + $this->registerAlias(IInitialState::class, OC\AppFramework\Services\InitialState::class); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * @return \OCP\IServerContainer |
|
| 302 | + */ |
|
| 303 | + public function getServer() { |
|
| 304 | + return $this->server; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * @param string $middleWare |
|
| 309 | + * @return boolean|null |
|
| 310 | + */ |
|
| 311 | + public function registerMiddleWare($middleWare) { |
|
| 312 | + if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
| 313 | + return false; |
|
| 314 | + } |
|
| 315 | + $this->middleWares[] = $middleWare; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * used to return the appname of the set application |
|
| 320 | + * @return string the name of your application |
|
| 321 | + */ |
|
| 322 | + public function getAppName() { |
|
| 323 | + return $this->query('AppName'); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * @deprecated use IUserSession->isLoggedIn() |
|
| 328 | + * @return boolean |
|
| 329 | + */ |
|
| 330 | + public function isLoggedIn() { |
|
| 331 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * @deprecated use IGroupManager->isAdmin($userId) |
|
| 336 | + * @return boolean |
|
| 337 | + */ |
|
| 338 | + public function isAdminUser() { |
|
| 339 | + $uid = $this->getUserId(); |
|
| 340 | + return \OC_User::isAdminUser($uid); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + private function getUserId() { |
|
| 344 | + return $this->getServer()->getSession()->get('user_id'); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * @deprecated use the ILogger instead |
|
| 349 | + * @param string $message |
|
| 350 | + * @param string $level |
|
| 351 | + * @return mixed |
|
| 352 | + */ |
|
| 353 | + public function log($message, $level) { |
|
| 354 | + switch ($level) { |
|
| 355 | + case 'debug': |
|
| 356 | + $level = ILogger::DEBUG; |
|
| 357 | + break; |
|
| 358 | + case 'info': |
|
| 359 | + $level = ILogger::INFO; |
|
| 360 | + break; |
|
| 361 | + case 'warn': |
|
| 362 | + $level = ILogger::WARN; |
|
| 363 | + break; |
|
| 364 | + case 'fatal': |
|
| 365 | + $level = ILogger::FATAL; |
|
| 366 | + break; |
|
| 367 | + default: |
|
| 368 | + $level = ILogger::ERROR; |
|
| 369 | + break; |
|
| 370 | + } |
|
| 371 | + \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Register a capability |
|
| 376 | + * |
|
| 377 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
| 378 | + */ |
|
| 379 | + public function registerCapability($serviceName) { |
|
| 380 | + $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
| 381 | + return $this->query($serviceName); |
|
| 382 | + }); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + public function query(string $name, bool $autoload = true) { |
|
| 386 | + try { |
|
| 387 | + return $this->queryNoFallback($name); |
|
| 388 | + } catch (QueryException $firstException) { |
|
| 389 | + try { |
|
| 390 | + return $this->getServer()->query($name, $autoload); |
|
| 391 | + } catch (QueryException $secondException) { |
|
| 392 | + if ($firstException->getCode() === 1) { |
|
| 393 | + throw $secondException; |
|
| 394 | + } |
|
| 395 | + throw $firstException; |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * @param string $name |
|
| 402 | + * @return mixed |
|
| 403 | + * @throws QueryException if the query could not be resolved |
|
| 404 | + */ |
|
| 405 | + public function queryNoFallback($name) { |
|
| 406 | + $name = $this->sanitizeName($name); |
|
| 407 | + |
|
| 408 | + if ($this->offsetExists($name)) { |
|
| 409 | + return parent::query($name); |
|
| 410 | + } else { |
|
| 411 | + if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
| 412 | + return parent::query($name); |
|
| 413 | + } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
| 414 | + return parent::query($name); |
|
| 415 | + } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
| 416 | + return parent::query($name); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + throw new QueryException('Could not resolve ' . $name . '!' . |
|
| 421 | + ' Class can not be instantiated', 1); |
|
| 422 | + } |
|
| 423 | 423 | } |
@@ -106,61 +106,61 @@ discard block |
||
| 106 | 106 | /** |
| 107 | 107 | * Core services |
| 108 | 108 | */ |
| 109 | - $this->registerService(IOutput::class, function () { |
|
| 109 | + $this->registerService(IOutput::class, function() { |
|
| 110 | 110 | return new Output($this->getServer()->getWebRoot()); |
| 111 | 111 | }); |
| 112 | 112 | |
| 113 | - $this->registerService(Folder::class, function () { |
|
| 113 | + $this->registerService(Folder::class, function() { |
|
| 114 | 114 | return $this->getServer()->getUserFolder(); |
| 115 | 115 | }); |
| 116 | 116 | |
| 117 | - $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
| 117 | + $this->registerService(IAppData::class, function(SimpleContainer $c) { |
|
| 118 | 118 | return $this->getServer()->getAppDataDir($c->query('AppName')); |
| 119 | 119 | }); |
| 120 | 120 | |
| 121 | - $this->registerService(IL10N::class, function ($c) { |
|
| 121 | + $this->registerService(IL10N::class, function($c) { |
|
| 122 | 122 | return $this->getServer()->getL10N($c->query('AppName')); |
| 123 | 123 | }); |
| 124 | 124 | |
| 125 | 125 | // Log wrapper |
| 126 | - $this->registerService(ILogger::class, function ($c) { |
|
| 126 | + $this->registerService(ILogger::class, function($c) { |
|
| 127 | 127 | return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
| 128 | 128 | }); |
| 129 | 129 | |
| 130 | - $this->registerService(IServerContainer::class, function () { |
|
| 130 | + $this->registerService(IServerContainer::class, function() { |
|
| 131 | 131 | return $this->getServer(); |
| 132 | 132 | }); |
| 133 | 133 | $this->registerAlias('ServerContainer', IServerContainer::class); |
| 134 | 134 | |
| 135 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
| 135 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function($c) { |
|
| 136 | 136 | return $c->query(Manager::class); |
| 137 | 137 | }); |
| 138 | 138 | |
| 139 | - $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
| 139 | + $this->registerService(\OCP\AppFramework\IAppContainer::class, function($c) { |
|
| 140 | 140 | return $c; |
| 141 | 141 | }); |
| 142 | 142 | |
| 143 | 143 | // commonly used attributes |
| 144 | - $this->registerService('UserId', function ($c) { |
|
| 144 | + $this->registerService('UserId', function($c) { |
|
| 145 | 145 | return $c->query(IUserSession::class)->getSession()->get('user_id'); |
| 146 | 146 | }); |
| 147 | 147 | |
| 148 | - $this->registerService('WebRoot', function ($c) { |
|
| 148 | + $this->registerService('WebRoot', function($c) { |
|
| 149 | 149 | return $c->query('ServerContainer')->getWebRoot(); |
| 150 | 150 | }); |
| 151 | 151 | |
| 152 | - $this->registerService('OC_Defaults', function ($c) { |
|
| 152 | + $this->registerService('OC_Defaults', function($c) { |
|
| 153 | 153 | return $c->getServer()->getThemingDefaults(); |
| 154 | 154 | }); |
| 155 | 155 | |
| 156 | - $this->registerService('Protocol', function ($c) { |
|
| 156 | + $this->registerService('Protocol', function($c) { |
|
| 157 | 157 | /** @var \OC\Server $server */ |
| 158 | 158 | $server = $c->query('ServerContainer'); |
| 159 | 159 | $protocol = $server->getRequest()->getHttpProtocol(); |
| 160 | 160 | return new Http($_SERVER, $protocol); |
| 161 | 161 | }); |
| 162 | 162 | |
| 163 | - $this->registerService('Dispatcher', function ($c) { |
|
| 163 | + $this->registerService('Dispatcher', function($c) { |
|
| 164 | 164 | return new Dispatcher( |
| 165 | 165 | $c['Protocol'], |
| 166 | 166 | $c['MiddlewareDispatcher'], |
@@ -179,8 +179,8 @@ discard block |
||
| 179 | 179 | /** |
| 180 | 180 | * Middleware |
| 181 | 181 | */ |
| 182 | - $this->registerService('MiddlewareDispatcher', function (SimpleContainer $c) { |
|
| 183 | - $server = $this->getServer(); |
|
| 182 | + $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) { |
|
| 183 | + $server = $this->getServer(); |
|
| 184 | 184 | |
| 185 | 185 | $dispatcher = new MiddlewareDispatcher(); |
| 186 | 186 | |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
| 378 | 378 | */ |
| 379 | 379 | public function registerCapability($serviceName) { |
| 380 | - $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
| 380 | + $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
| 381 | 381 | return $this->query($serviceName); |
| 382 | 382 | }); |
| 383 | 383 | } |
@@ -412,12 +412,12 @@ discard block |
||
| 412 | 412 | return parent::query($name); |
| 413 | 413 | } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
| 414 | 414 | return parent::query($name); |
| 415 | - } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
| 415 | + } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']).'\\') === 0) { |
|
| 416 | 416 | return parent::query($name); |
| 417 | 417 | } |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
| 420 | + throw new QueryException('Could not resolve '.$name.'!'. |
|
| 421 | 421 | ' Class can not be instantiated', 1); |
| 422 | 422 | } |
| 423 | 423 | } |
@@ -34,53 +34,53 @@ |
||
| 34 | 34 | |
| 35 | 35 | class CompressionMiddleware extends Middleware { |
| 36 | 36 | |
| 37 | - /** @var bool */ |
|
| 38 | - private $useGZip; |
|
| 37 | + /** @var bool */ |
|
| 38 | + private $useGZip; |
|
| 39 | 39 | |
| 40 | - /** @var IRequest */ |
|
| 41 | - private $request; |
|
| 40 | + /** @var IRequest */ |
|
| 41 | + private $request; |
|
| 42 | 42 | |
| 43 | - public function __construct(IRequest $request) { |
|
| 44 | - $this->request = $request; |
|
| 45 | - $this->useGZip = false; |
|
| 46 | - } |
|
| 43 | + public function __construct(IRequest $request) { |
|
| 44 | + $this->request = $request; |
|
| 45 | + $this->useGZip = false; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public function afterController($controller, $methodName, Response $response) { |
|
| 49 | - // By default we do not gzip |
|
| 50 | - $allowGzip = false; |
|
| 48 | + public function afterController($controller, $methodName, Response $response) { |
|
| 49 | + // By default we do not gzip |
|
| 50 | + $allowGzip = false; |
|
| 51 | 51 | |
| 52 | - // Only return gzipped content for 200 responses |
|
| 53 | - if ($response->getStatus() !== Http::STATUS_OK) { |
|
| 54 | - return $response; |
|
| 55 | - } |
|
| 52 | + // Only return gzipped content for 200 responses |
|
| 53 | + if ($response->getStatus() !== Http::STATUS_OK) { |
|
| 54 | + return $response; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - // Check if we are even asked for gzip |
|
| 58 | - $header = $this->request->getHeader('Accept-Encoding'); |
|
| 59 | - if (strpos($header, 'gzip') === false) { |
|
| 60 | - return $response; |
|
| 61 | - } |
|
| 57 | + // Check if we are even asked for gzip |
|
| 58 | + $header = $this->request->getHeader('Accept-Encoding'); |
|
| 59 | + if (strpos($header, 'gzip') === false) { |
|
| 60 | + return $response; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - // We only allow gzip in some cases |
|
| 64 | - if ($response instanceof BaseResponse) { |
|
| 65 | - $allowGzip = true; |
|
| 66 | - } |
|
| 67 | - if ($response instanceof JSONResponse) { |
|
| 68 | - $allowGzip = true; |
|
| 69 | - } |
|
| 63 | + // We only allow gzip in some cases |
|
| 64 | + if ($response instanceof BaseResponse) { |
|
| 65 | + $allowGzip = true; |
|
| 66 | + } |
|
| 67 | + if ($response instanceof JSONResponse) { |
|
| 68 | + $allowGzip = true; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - if ($allowGzip) { |
|
| 72 | - $this->useGZip = true; |
|
| 73 | - $response->addHeader('Content-Encoding', 'gzip'); |
|
| 74 | - } |
|
| 71 | + if ($allowGzip) { |
|
| 72 | + $this->useGZip = true; |
|
| 73 | + $response->addHeader('Content-Encoding', 'gzip'); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - return $response; |
|
| 77 | - } |
|
| 76 | + return $response; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - public function beforeOutput($controller, $methodName, $output) { |
|
| 80 | - if (!$this->useGZip) { |
|
| 81 | - return $output; |
|
| 82 | - } |
|
| 79 | + public function beforeOutput($controller, $methodName, $output) { |
|
| 80 | + if (!$this->useGZip) { |
|
| 81 | + return $output; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - return gzencode($output); |
|
| 85 | - } |
|
| 84 | + return gzencode($output); |
|
| 85 | + } |
|
| 86 | 86 | } |