@@ -31,26 +31,26 @@ |
||
| 31 | 31 | use OCP\IRequest; |
| 32 | 32 | |
| 33 | 33 | class NotModifiedMiddleware extends Middleware { |
| 34 | - /** @var IRequest */ |
|
| 35 | - private $request; |
|
| 36 | - |
|
| 37 | - public function __construct(IRequest $request) { |
|
| 38 | - $this->request = $request; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public function afterController($controller, $methodName, Response $response) { |
|
| 42 | - $etagHeader = $this->request->getHeader('IF_NONE_MATCH'); |
|
| 43 | - if ($etagHeader !== '' && $response->getETag() !== null && trim($etagHeader) === '"' . $response->getETag() . '"') { |
|
| 44 | - $response->setStatus(Http::STATUS_NOT_MODIFIED); |
|
| 45 | - return $response; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $modifiedSinceHeader = $this->request->getHeader('IF_MODIFIED_SINCE'); |
|
| 49 | - if ($modifiedSinceHeader !== '' && $response->getLastModified() !== null && trim($modifiedSinceHeader) === $response->getLastModified()->format(\DateTime::RFC2822)) { |
|
| 50 | - $response->setStatus(Http::STATUS_NOT_MODIFIED); |
|
| 51 | - return $response; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - return $response; |
|
| 55 | - } |
|
| 34 | + /** @var IRequest */ |
|
| 35 | + private $request; |
|
| 36 | + |
|
| 37 | + public function __construct(IRequest $request) { |
|
| 38 | + $this->request = $request; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public function afterController($controller, $methodName, Response $response) { |
|
| 42 | + $etagHeader = $this->request->getHeader('IF_NONE_MATCH'); |
|
| 43 | + if ($etagHeader !== '' && $response->getETag() !== null && trim($etagHeader) === '"' . $response->getETag() . '"') { |
|
| 44 | + $response->setStatus(Http::STATUS_NOT_MODIFIED); |
|
| 45 | + return $response; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $modifiedSinceHeader = $this->request->getHeader('IF_MODIFIED_SINCE'); |
|
| 49 | + if ($modifiedSinceHeader !== '' && $response->getLastModified() !== null && trim($modifiedSinceHeader) === $response->getLastModified()->format(\DateTime::RFC2822)) { |
|
| 50 | + $response->setStatus(Http::STATUS_NOT_MODIFIED); |
|
| 51 | + return $response; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + return $response; |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | |
| 41 | 41 | public function afterController($controller, $methodName, Response $response) { |
| 42 | 42 | $etagHeader = $this->request->getHeader('IF_NONE_MATCH'); |
| 43 | - if ($etagHeader !== '' && $response->getETag() !== null && trim($etagHeader) === '"' . $response->getETag() . '"') { |
|
| 43 | + if ($etagHeader !== '' && $response->getETag() !== null && trim($etagHeader) === '"'.$response->getETag().'"') { |
|
| 44 | 44 | $response->setStatus(Http::STATUS_NOT_MODIFIED); |
| 45 | 45 | return $response; |
| 46 | 46 | } |
@@ -70,356 +70,356 @@ |
||
| 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($c->query(OC\AppFramework\Middleware\NotModifiedMiddleware::class)); |
|
| 192 | - |
|
| 193 | - $dispatcher->registerMiddleware( |
|
| 194 | - $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
| 195 | - ); |
|
| 196 | - |
|
| 197 | - $dispatcher->registerMiddleware( |
|
| 198 | - new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
| 199 | - $c->query(IRequest::class), |
|
| 200 | - $c->query(IControllerMethodReflector::class) |
|
| 201 | - ) |
|
| 202 | - ); |
|
| 203 | - $dispatcher->registerMiddleware( |
|
| 204 | - new CORSMiddleware( |
|
| 205 | - $c->query(IRequest::class), |
|
| 206 | - $c->query(IControllerMethodReflector::class), |
|
| 207 | - $c->query(IUserSession::class), |
|
| 208 | - $c->query(OC\Security\Bruteforce\Throttler::class) |
|
| 209 | - ) |
|
| 210 | - ); |
|
| 211 | - $dispatcher->registerMiddleware( |
|
| 212 | - new OCSMiddleware( |
|
| 213 | - $c->query(IRequest::class) |
|
| 214 | - ) |
|
| 215 | - ); |
|
| 216 | - |
|
| 217 | - $securityMiddleware = new SecurityMiddleware( |
|
| 218 | - $c->query(IRequest::class), |
|
| 219 | - $c->query(IControllerMethodReflector::class), |
|
| 220 | - $c->query(INavigationManager::class), |
|
| 221 | - $c->query(IURLGenerator::class), |
|
| 222 | - $server->getLogger(), |
|
| 223 | - $c['AppName'], |
|
| 224 | - $server->getUserSession()->isLoggedIn(), |
|
| 225 | - $server->getGroupManager()->isAdmin($this->getUserId()), |
|
| 226 | - $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
| 227 | - $server->getAppManager(), |
|
| 228 | - $server->getL10N('lib') |
|
| 229 | - ); |
|
| 230 | - $dispatcher->registerMiddleware($securityMiddleware); |
|
| 231 | - $dispatcher->registerMiddleware( |
|
| 232 | - new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
| 233 | - $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
| 234 | - $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
| 235 | - $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
| 236 | - ) |
|
| 237 | - ); |
|
| 238 | - $dispatcher->registerMiddleware( |
|
| 239 | - $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) |
|
| 240 | - ); |
|
| 241 | - $dispatcher->registerMiddleware( |
|
| 242 | - new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
| 243 | - $c->query(IControllerMethodReflector::class), |
|
| 244 | - $c->query(ISession::class), |
|
| 245 | - $c->query(IUserSession::class), |
|
| 246 | - $c->query(ITimeFactory::class) |
|
| 247 | - ) |
|
| 248 | - ); |
|
| 249 | - $dispatcher->registerMiddleware( |
|
| 250 | - new TwoFactorMiddleware( |
|
| 251 | - $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
| 252 | - $c->query(IUserSession::class), |
|
| 253 | - $c->query(ISession::class), |
|
| 254 | - $c->query(IURLGenerator::class), |
|
| 255 | - $c->query(IControllerMethodReflector::class), |
|
| 256 | - $c->query(IRequest::class) |
|
| 257 | - ) |
|
| 258 | - ); |
|
| 259 | - $dispatcher->registerMiddleware( |
|
| 260 | - new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
| 261 | - $c->query(IControllerMethodReflector::class), |
|
| 262 | - $c->query(OC\Security\Bruteforce\Throttler::class), |
|
| 263 | - $c->query(IRequest::class) |
|
| 264 | - ) |
|
| 265 | - ); |
|
| 266 | - $dispatcher->registerMiddleware( |
|
| 267 | - new RateLimitingMiddleware( |
|
| 268 | - $c->query(IRequest::class), |
|
| 269 | - $c->query(IUserSession::class), |
|
| 270 | - $c->query(IControllerMethodReflector::class), |
|
| 271 | - $c->query(OC\Security\RateLimiting\Limiter::class) |
|
| 272 | - ) |
|
| 273 | - ); |
|
| 274 | - $dispatcher->registerMiddleware( |
|
| 275 | - new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
| 276 | - $c->query(IRequest::class), |
|
| 277 | - $c->query(ISession::class), |
|
| 278 | - $c->query(\OCP\IConfig::class) |
|
| 279 | - ) |
|
| 280 | - ); |
|
| 281 | - $dispatcher->registerMiddleware( |
|
| 282 | - $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
| 283 | - ); |
|
| 284 | - |
|
| 285 | - foreach ($this->middleWares as $middleWare) { |
|
| 286 | - $dispatcher->registerMiddleware($c->query($middleWare)); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - $dispatcher->registerMiddleware( |
|
| 290 | - new SessionMiddleware( |
|
| 291 | - $c->query(IControllerMethodReflector::class), |
|
| 292 | - $c->query(ISession::class) |
|
| 293 | - ) |
|
| 294 | - ); |
|
| 295 | - return $dispatcher; |
|
| 296 | - }); |
|
| 297 | - |
|
| 298 | - $this->registerAlias(IAppConfig::class, OC\AppFramework\Services\AppConfig::class); |
|
| 299 | - $this->registerAlias(IInitialState::class, OC\AppFramework\Services\InitialState::class); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @return \OCP\IServerContainer |
|
| 304 | - */ |
|
| 305 | - public function getServer() { |
|
| 306 | - return $this->server; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * @param string $middleWare |
|
| 311 | - * @return boolean|null |
|
| 312 | - */ |
|
| 313 | - public function registerMiddleWare($middleWare) { |
|
| 314 | - if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
| 315 | - return false; |
|
| 316 | - } |
|
| 317 | - $this->middleWares[] = $middleWare; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * used to return the appname of the set application |
|
| 322 | - * @return string the name of your application |
|
| 323 | - */ |
|
| 324 | - public function getAppName() { |
|
| 325 | - return $this->query('AppName'); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * @deprecated use IUserSession->isLoggedIn() |
|
| 330 | - * @return boolean |
|
| 331 | - */ |
|
| 332 | - public function isLoggedIn() { |
|
| 333 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * @deprecated use IGroupManager->isAdmin($userId) |
|
| 338 | - * @return boolean |
|
| 339 | - */ |
|
| 340 | - public function isAdminUser() { |
|
| 341 | - $uid = $this->getUserId(); |
|
| 342 | - return \OC_User::isAdminUser($uid); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - private function getUserId() { |
|
| 346 | - return $this->getServer()->getSession()->get('user_id'); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * @deprecated use the ILogger instead |
|
| 351 | - * @param string $message |
|
| 352 | - * @param string $level |
|
| 353 | - * @return mixed |
|
| 354 | - */ |
|
| 355 | - public function log($message, $level) { |
|
| 356 | - switch ($level) { |
|
| 357 | - case 'debug': |
|
| 358 | - $level = ILogger::DEBUG; |
|
| 359 | - break; |
|
| 360 | - case 'info': |
|
| 361 | - $level = ILogger::INFO; |
|
| 362 | - break; |
|
| 363 | - case 'warn': |
|
| 364 | - $level = ILogger::WARN; |
|
| 365 | - break; |
|
| 366 | - case 'fatal': |
|
| 367 | - $level = ILogger::FATAL; |
|
| 368 | - break; |
|
| 369 | - default: |
|
| 370 | - $level = ILogger::ERROR; |
|
| 371 | - break; |
|
| 372 | - } |
|
| 373 | - \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Register a capability |
|
| 378 | - * |
|
| 379 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
| 380 | - */ |
|
| 381 | - public function registerCapability($serviceName) { |
|
| 382 | - $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
| 383 | - return $this->query($serviceName); |
|
| 384 | - }); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - public function query(string $name, bool $autoload = true) { |
|
| 388 | - try { |
|
| 389 | - return $this->queryNoFallback($name); |
|
| 390 | - } catch (QueryException $firstException) { |
|
| 391 | - try { |
|
| 392 | - return $this->getServer()->query($name, $autoload); |
|
| 393 | - } catch (QueryException $secondException) { |
|
| 394 | - if ($firstException->getCode() === 1) { |
|
| 395 | - throw $secondException; |
|
| 396 | - } |
|
| 397 | - throw $firstException; |
|
| 398 | - } |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * @param string $name |
|
| 404 | - * @return mixed |
|
| 405 | - * @throws QueryException if the query could not be resolved |
|
| 406 | - */ |
|
| 407 | - public function queryNoFallback($name) { |
|
| 408 | - $name = $this->sanitizeName($name); |
|
| 409 | - |
|
| 410 | - if ($this->offsetExists($name)) { |
|
| 411 | - return parent::query($name); |
|
| 412 | - } else { |
|
| 413 | - if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
| 414 | - return parent::query($name); |
|
| 415 | - } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
| 416 | - return parent::query($name); |
|
| 417 | - } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
| 418 | - return parent::query($name); |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
| 423 | - ' Class can not be instantiated', 1); |
|
| 424 | - } |
|
| 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($c->query(OC\AppFramework\Middleware\NotModifiedMiddleware::class)); |
|
| 192 | + |
|
| 193 | + $dispatcher->registerMiddleware( |
|
| 194 | + $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
| 195 | + ); |
|
| 196 | + |
|
| 197 | + $dispatcher->registerMiddleware( |
|
| 198 | + new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
| 199 | + $c->query(IRequest::class), |
|
| 200 | + $c->query(IControllerMethodReflector::class) |
|
| 201 | + ) |
|
| 202 | + ); |
|
| 203 | + $dispatcher->registerMiddleware( |
|
| 204 | + new CORSMiddleware( |
|
| 205 | + $c->query(IRequest::class), |
|
| 206 | + $c->query(IControllerMethodReflector::class), |
|
| 207 | + $c->query(IUserSession::class), |
|
| 208 | + $c->query(OC\Security\Bruteforce\Throttler::class) |
|
| 209 | + ) |
|
| 210 | + ); |
|
| 211 | + $dispatcher->registerMiddleware( |
|
| 212 | + new OCSMiddleware( |
|
| 213 | + $c->query(IRequest::class) |
|
| 214 | + ) |
|
| 215 | + ); |
|
| 216 | + |
|
| 217 | + $securityMiddleware = new SecurityMiddleware( |
|
| 218 | + $c->query(IRequest::class), |
|
| 219 | + $c->query(IControllerMethodReflector::class), |
|
| 220 | + $c->query(INavigationManager::class), |
|
| 221 | + $c->query(IURLGenerator::class), |
|
| 222 | + $server->getLogger(), |
|
| 223 | + $c['AppName'], |
|
| 224 | + $server->getUserSession()->isLoggedIn(), |
|
| 225 | + $server->getGroupManager()->isAdmin($this->getUserId()), |
|
| 226 | + $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
| 227 | + $server->getAppManager(), |
|
| 228 | + $server->getL10N('lib') |
|
| 229 | + ); |
|
| 230 | + $dispatcher->registerMiddleware($securityMiddleware); |
|
| 231 | + $dispatcher->registerMiddleware( |
|
| 232 | + new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
| 233 | + $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
| 234 | + $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
| 235 | + $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
| 236 | + ) |
|
| 237 | + ); |
|
| 238 | + $dispatcher->registerMiddleware( |
|
| 239 | + $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) |
|
| 240 | + ); |
|
| 241 | + $dispatcher->registerMiddleware( |
|
| 242 | + new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
| 243 | + $c->query(IControllerMethodReflector::class), |
|
| 244 | + $c->query(ISession::class), |
|
| 245 | + $c->query(IUserSession::class), |
|
| 246 | + $c->query(ITimeFactory::class) |
|
| 247 | + ) |
|
| 248 | + ); |
|
| 249 | + $dispatcher->registerMiddleware( |
|
| 250 | + new TwoFactorMiddleware( |
|
| 251 | + $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
| 252 | + $c->query(IUserSession::class), |
|
| 253 | + $c->query(ISession::class), |
|
| 254 | + $c->query(IURLGenerator::class), |
|
| 255 | + $c->query(IControllerMethodReflector::class), |
|
| 256 | + $c->query(IRequest::class) |
|
| 257 | + ) |
|
| 258 | + ); |
|
| 259 | + $dispatcher->registerMiddleware( |
|
| 260 | + new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
| 261 | + $c->query(IControllerMethodReflector::class), |
|
| 262 | + $c->query(OC\Security\Bruteforce\Throttler::class), |
|
| 263 | + $c->query(IRequest::class) |
|
| 264 | + ) |
|
| 265 | + ); |
|
| 266 | + $dispatcher->registerMiddleware( |
|
| 267 | + new RateLimitingMiddleware( |
|
| 268 | + $c->query(IRequest::class), |
|
| 269 | + $c->query(IUserSession::class), |
|
| 270 | + $c->query(IControllerMethodReflector::class), |
|
| 271 | + $c->query(OC\Security\RateLimiting\Limiter::class) |
|
| 272 | + ) |
|
| 273 | + ); |
|
| 274 | + $dispatcher->registerMiddleware( |
|
| 275 | + new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
| 276 | + $c->query(IRequest::class), |
|
| 277 | + $c->query(ISession::class), |
|
| 278 | + $c->query(\OCP\IConfig::class) |
|
| 279 | + ) |
|
| 280 | + ); |
|
| 281 | + $dispatcher->registerMiddleware( |
|
| 282 | + $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
| 283 | + ); |
|
| 284 | + |
|
| 285 | + foreach ($this->middleWares as $middleWare) { |
|
| 286 | + $dispatcher->registerMiddleware($c->query($middleWare)); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + $dispatcher->registerMiddleware( |
|
| 290 | + new SessionMiddleware( |
|
| 291 | + $c->query(IControllerMethodReflector::class), |
|
| 292 | + $c->query(ISession::class) |
|
| 293 | + ) |
|
| 294 | + ); |
|
| 295 | + return $dispatcher; |
|
| 296 | + }); |
|
| 297 | + |
|
| 298 | + $this->registerAlias(IAppConfig::class, OC\AppFramework\Services\AppConfig::class); |
|
| 299 | + $this->registerAlias(IInitialState::class, OC\AppFramework\Services\InitialState::class); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @return \OCP\IServerContainer |
|
| 304 | + */ |
|
| 305 | + public function getServer() { |
|
| 306 | + return $this->server; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * @param string $middleWare |
|
| 311 | + * @return boolean|null |
|
| 312 | + */ |
|
| 313 | + public function registerMiddleWare($middleWare) { |
|
| 314 | + if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
| 315 | + return false; |
|
| 316 | + } |
|
| 317 | + $this->middleWares[] = $middleWare; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * used to return the appname of the set application |
|
| 322 | + * @return string the name of your application |
|
| 323 | + */ |
|
| 324 | + public function getAppName() { |
|
| 325 | + return $this->query('AppName'); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * @deprecated use IUserSession->isLoggedIn() |
|
| 330 | + * @return boolean |
|
| 331 | + */ |
|
| 332 | + public function isLoggedIn() { |
|
| 333 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * @deprecated use IGroupManager->isAdmin($userId) |
|
| 338 | + * @return boolean |
|
| 339 | + */ |
|
| 340 | + public function isAdminUser() { |
|
| 341 | + $uid = $this->getUserId(); |
|
| 342 | + return \OC_User::isAdminUser($uid); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + private function getUserId() { |
|
| 346 | + return $this->getServer()->getSession()->get('user_id'); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * @deprecated use the ILogger instead |
|
| 351 | + * @param string $message |
|
| 352 | + * @param string $level |
|
| 353 | + * @return mixed |
|
| 354 | + */ |
|
| 355 | + public function log($message, $level) { |
|
| 356 | + switch ($level) { |
|
| 357 | + case 'debug': |
|
| 358 | + $level = ILogger::DEBUG; |
|
| 359 | + break; |
|
| 360 | + case 'info': |
|
| 361 | + $level = ILogger::INFO; |
|
| 362 | + break; |
|
| 363 | + case 'warn': |
|
| 364 | + $level = ILogger::WARN; |
|
| 365 | + break; |
|
| 366 | + case 'fatal': |
|
| 367 | + $level = ILogger::FATAL; |
|
| 368 | + break; |
|
| 369 | + default: |
|
| 370 | + $level = ILogger::ERROR; |
|
| 371 | + break; |
|
| 372 | + } |
|
| 373 | + \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Register a capability |
|
| 378 | + * |
|
| 379 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
| 380 | + */ |
|
| 381 | + public function registerCapability($serviceName) { |
|
| 382 | + $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
| 383 | + return $this->query($serviceName); |
|
| 384 | + }); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + public function query(string $name, bool $autoload = true) { |
|
| 388 | + try { |
|
| 389 | + return $this->queryNoFallback($name); |
|
| 390 | + } catch (QueryException $firstException) { |
|
| 391 | + try { |
|
| 392 | + return $this->getServer()->query($name, $autoload); |
|
| 393 | + } catch (QueryException $secondException) { |
|
| 394 | + if ($firstException->getCode() === 1) { |
|
| 395 | + throw $secondException; |
|
| 396 | + } |
|
| 397 | + throw $firstException; |
|
| 398 | + } |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * @param string $name |
|
| 404 | + * @return mixed |
|
| 405 | + * @throws QueryException if the query could not be resolved |
|
| 406 | + */ |
|
| 407 | + public function queryNoFallback($name) { |
|
| 408 | + $name = $this->sanitizeName($name); |
|
| 409 | + |
|
| 410 | + if ($this->offsetExists($name)) { |
|
| 411 | + return parent::query($name); |
|
| 412 | + } else { |
|
| 413 | + if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
| 414 | + return parent::query($name); |
|
| 415 | + } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
| 416 | + return parent::query($name); |
|
| 417 | + } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
| 418 | + return parent::query($name); |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + throw new QueryException('Could not resolve ' . $name . '!' . |
|
| 423 | + ' Class can not be instantiated', 1); |
|
| 424 | + } |
|
| 425 | 425 | } |
@@ -33,99 +33,99 @@ |
||
| 33 | 33 | use OCP\AppFramework\Http as BaseHttp; |
| 34 | 34 | |
| 35 | 35 | class Http extends BaseHttp { |
| 36 | - private $server; |
|
| 37 | - private $protocolVersion; |
|
| 38 | - protected $headers; |
|
| 36 | + private $server; |
|
| 37 | + private $protocolVersion; |
|
| 38 | + protected $headers; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param array $server $_SERVER |
|
| 42 | - * @param string $protocolVersion the http version to use defaults to HTTP/1.1 |
|
| 43 | - */ |
|
| 44 | - public function __construct($server, $protocolVersion='HTTP/1.1') { |
|
| 45 | - $this->server = $server; |
|
| 46 | - $this->protocolVersion = $protocolVersion; |
|
| 40 | + /** |
|
| 41 | + * @param array $server $_SERVER |
|
| 42 | + * @param string $protocolVersion the http version to use defaults to HTTP/1.1 |
|
| 43 | + */ |
|
| 44 | + public function __construct($server, $protocolVersion='HTTP/1.1') { |
|
| 45 | + $this->server = $server; |
|
| 46 | + $this->protocolVersion = $protocolVersion; |
|
| 47 | 47 | |
| 48 | - $this->headers = [ |
|
| 49 | - self::STATUS_CONTINUE => 'Continue', |
|
| 50 | - self::STATUS_SWITCHING_PROTOCOLS => 'Switching Protocols', |
|
| 51 | - self::STATUS_PROCESSING => 'Processing', |
|
| 52 | - self::STATUS_OK => 'OK', |
|
| 53 | - self::STATUS_CREATED => 'Created', |
|
| 54 | - self::STATUS_ACCEPTED => 'Accepted', |
|
| 55 | - self::STATUS_NON_AUTHORATIVE_INFORMATION => 'Non-Authorative Information', |
|
| 56 | - self::STATUS_NO_CONTENT => 'No Content', |
|
| 57 | - self::STATUS_RESET_CONTENT => 'Reset Content', |
|
| 58 | - self::STATUS_PARTIAL_CONTENT => 'Partial Content', |
|
| 59 | - self::STATUS_MULTI_STATUS => 'Multi-Status', // RFC 4918 |
|
| 60 | - self::STATUS_ALREADY_REPORTED => 'Already Reported', // RFC 5842 |
|
| 61 | - self::STATUS_IM_USED => 'IM Used', // RFC 3229 |
|
| 62 | - self::STATUS_MULTIPLE_CHOICES => 'Multiple Choices', |
|
| 63 | - self::STATUS_MOVED_PERMANENTLY => 'Moved Permanently', |
|
| 64 | - self::STATUS_FOUND => 'Found', |
|
| 65 | - self::STATUS_SEE_OTHER => 'See Other', |
|
| 66 | - self::STATUS_NOT_MODIFIED => 'Not Modified', |
|
| 67 | - self::STATUS_USE_PROXY => 'Use Proxy', |
|
| 68 | - self::STATUS_RESERVED => 'Reserved', |
|
| 69 | - self::STATUS_TEMPORARY_REDIRECT => 'Temporary Redirect', |
|
| 70 | - self::STATUS_BAD_REQUEST => 'Bad request', |
|
| 71 | - self::STATUS_UNAUTHORIZED => 'Unauthorized', |
|
| 72 | - self::STATUS_PAYMENT_REQUIRED => 'Payment Required', |
|
| 73 | - self::STATUS_FORBIDDEN => 'Forbidden', |
|
| 74 | - self::STATUS_NOT_FOUND => 'Not Found', |
|
| 75 | - self::STATUS_METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
| 76 | - self::STATUS_NOT_ACCEPTABLE => 'Not Acceptable', |
|
| 77 | - self::STATUS_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
| 78 | - self::STATUS_REQUEST_TIMEOUT => 'Request Timeout', |
|
| 79 | - self::STATUS_CONFLICT => 'Conflict', |
|
| 80 | - self::STATUS_GONE => 'Gone', |
|
| 81 | - self::STATUS_LENGTH_REQUIRED => 'Length Required', |
|
| 82 | - self::STATUS_PRECONDITION_FAILED => 'Precondition failed', |
|
| 83 | - self::STATUS_REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
| 84 | - self::STATUS_REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
| 85 | - self::STATUS_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
| 86 | - self::STATUS_REQUEST_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
| 87 | - self::STATUS_EXPECTATION_FAILED => 'Expectation Failed', |
|
| 88 | - self::STATUS_IM_A_TEAPOT => 'I\'m a teapot', // RFC 2324 |
|
| 89 | - self::STATUS_UNPROCESSABLE_ENTITY => 'Unprocessable Entity', // RFC 4918 |
|
| 90 | - self::STATUS_LOCKED => 'Locked', // RFC 4918 |
|
| 91 | - self::STATUS_FAILED_DEPENDENCY => 'Failed Dependency', // RFC 4918 |
|
| 92 | - self::STATUS_UPGRADE_REQUIRED => 'Upgrade required', |
|
| 93 | - self::STATUS_PRECONDITION_REQUIRED => 'Precondition required', // draft-nottingham-http-new-status |
|
| 94 | - self::STATUS_TOO_MANY_REQUESTS => 'Too Many Requests', // draft-nottingham-http-new-status |
|
| 95 | - self::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large', // draft-nottingham-http-new-status |
|
| 96 | - self::STATUS_INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
| 97 | - self::STATUS_NOT_IMPLEMENTED => 'Not Implemented', |
|
| 98 | - self::STATUS_BAD_GATEWAY => 'Bad Gateway', |
|
| 99 | - self::STATUS_SERVICE_UNAVAILABLE => 'Service Unavailable', |
|
| 100 | - self::STATUS_GATEWAY_TIMEOUT => 'Gateway Timeout', |
|
| 101 | - self::STATUS_HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version not supported', |
|
| 102 | - self::STATUS_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates', |
|
| 103 | - self::STATUS_INSUFFICIENT_STORAGE => 'Insufficient Storage', // RFC 4918 |
|
| 104 | - self::STATUS_LOOP_DETECTED => 'Loop Detected', // RFC 5842 |
|
| 105 | - self::STATUS_BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded', // non-standard |
|
| 106 | - self::STATUS_NOT_EXTENDED => 'Not extended', |
|
| 107 | - self::STATUS_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required', // draft-nottingham-http-new-status |
|
| 108 | - ]; |
|
| 109 | - } |
|
| 48 | + $this->headers = [ |
|
| 49 | + self::STATUS_CONTINUE => 'Continue', |
|
| 50 | + self::STATUS_SWITCHING_PROTOCOLS => 'Switching Protocols', |
|
| 51 | + self::STATUS_PROCESSING => 'Processing', |
|
| 52 | + self::STATUS_OK => 'OK', |
|
| 53 | + self::STATUS_CREATED => 'Created', |
|
| 54 | + self::STATUS_ACCEPTED => 'Accepted', |
|
| 55 | + self::STATUS_NON_AUTHORATIVE_INFORMATION => 'Non-Authorative Information', |
|
| 56 | + self::STATUS_NO_CONTENT => 'No Content', |
|
| 57 | + self::STATUS_RESET_CONTENT => 'Reset Content', |
|
| 58 | + self::STATUS_PARTIAL_CONTENT => 'Partial Content', |
|
| 59 | + self::STATUS_MULTI_STATUS => 'Multi-Status', // RFC 4918 |
|
| 60 | + self::STATUS_ALREADY_REPORTED => 'Already Reported', // RFC 5842 |
|
| 61 | + self::STATUS_IM_USED => 'IM Used', // RFC 3229 |
|
| 62 | + self::STATUS_MULTIPLE_CHOICES => 'Multiple Choices', |
|
| 63 | + self::STATUS_MOVED_PERMANENTLY => 'Moved Permanently', |
|
| 64 | + self::STATUS_FOUND => 'Found', |
|
| 65 | + self::STATUS_SEE_OTHER => 'See Other', |
|
| 66 | + self::STATUS_NOT_MODIFIED => 'Not Modified', |
|
| 67 | + self::STATUS_USE_PROXY => 'Use Proxy', |
|
| 68 | + self::STATUS_RESERVED => 'Reserved', |
|
| 69 | + self::STATUS_TEMPORARY_REDIRECT => 'Temporary Redirect', |
|
| 70 | + self::STATUS_BAD_REQUEST => 'Bad request', |
|
| 71 | + self::STATUS_UNAUTHORIZED => 'Unauthorized', |
|
| 72 | + self::STATUS_PAYMENT_REQUIRED => 'Payment Required', |
|
| 73 | + self::STATUS_FORBIDDEN => 'Forbidden', |
|
| 74 | + self::STATUS_NOT_FOUND => 'Not Found', |
|
| 75 | + self::STATUS_METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
| 76 | + self::STATUS_NOT_ACCEPTABLE => 'Not Acceptable', |
|
| 77 | + self::STATUS_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
| 78 | + self::STATUS_REQUEST_TIMEOUT => 'Request Timeout', |
|
| 79 | + self::STATUS_CONFLICT => 'Conflict', |
|
| 80 | + self::STATUS_GONE => 'Gone', |
|
| 81 | + self::STATUS_LENGTH_REQUIRED => 'Length Required', |
|
| 82 | + self::STATUS_PRECONDITION_FAILED => 'Precondition failed', |
|
| 83 | + self::STATUS_REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
| 84 | + self::STATUS_REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
| 85 | + self::STATUS_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
| 86 | + self::STATUS_REQUEST_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
| 87 | + self::STATUS_EXPECTATION_FAILED => 'Expectation Failed', |
|
| 88 | + self::STATUS_IM_A_TEAPOT => 'I\'m a teapot', // RFC 2324 |
|
| 89 | + self::STATUS_UNPROCESSABLE_ENTITY => 'Unprocessable Entity', // RFC 4918 |
|
| 90 | + self::STATUS_LOCKED => 'Locked', // RFC 4918 |
|
| 91 | + self::STATUS_FAILED_DEPENDENCY => 'Failed Dependency', // RFC 4918 |
|
| 92 | + self::STATUS_UPGRADE_REQUIRED => 'Upgrade required', |
|
| 93 | + self::STATUS_PRECONDITION_REQUIRED => 'Precondition required', // draft-nottingham-http-new-status |
|
| 94 | + self::STATUS_TOO_MANY_REQUESTS => 'Too Many Requests', // draft-nottingham-http-new-status |
|
| 95 | + self::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large', // draft-nottingham-http-new-status |
|
| 96 | + self::STATUS_INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
| 97 | + self::STATUS_NOT_IMPLEMENTED => 'Not Implemented', |
|
| 98 | + self::STATUS_BAD_GATEWAY => 'Bad Gateway', |
|
| 99 | + self::STATUS_SERVICE_UNAVAILABLE => 'Service Unavailable', |
|
| 100 | + self::STATUS_GATEWAY_TIMEOUT => 'Gateway Timeout', |
|
| 101 | + self::STATUS_HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version not supported', |
|
| 102 | + self::STATUS_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates', |
|
| 103 | + self::STATUS_INSUFFICIENT_STORAGE => 'Insufficient Storage', // RFC 4918 |
|
| 104 | + self::STATUS_LOOP_DETECTED => 'Loop Detected', // RFC 5842 |
|
| 105 | + self::STATUS_BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded', // non-standard |
|
| 106 | + self::STATUS_NOT_EXTENDED => 'Not extended', |
|
| 107 | + self::STATUS_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required', // draft-nottingham-http-new-status |
|
| 108 | + ]; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Gets the correct header |
|
| 114 | - * @param int Http::CONSTANT $status the constant from the Http class |
|
| 115 | - * @param \DateTime $lastModified formatted last modified date |
|
| 116 | - * @param string $ETag the etag |
|
| 117 | - * @return string |
|
| 118 | - */ |
|
| 119 | - public function getStatusHeader($status) { |
|
| 120 | - // we have one change currently for the http 1.0 header that differs |
|
| 121 | - // from 1.1: STATUS_TEMPORARY_REDIRECT should be STATUS_FOUND |
|
| 122 | - // if this differs any more, we want to create childclasses for this |
|
| 123 | - if ($status === self::STATUS_TEMPORARY_REDIRECT |
|
| 124 | - && $this->protocolVersion === 'HTTP/1.0') { |
|
| 125 | - $status = self::STATUS_FOUND; |
|
| 126 | - } |
|
| 112 | + /** |
|
| 113 | + * Gets the correct header |
|
| 114 | + * @param int Http::CONSTANT $status the constant from the Http class |
|
| 115 | + * @param \DateTime $lastModified formatted last modified date |
|
| 116 | + * @param string $ETag the etag |
|
| 117 | + * @return string |
|
| 118 | + */ |
|
| 119 | + public function getStatusHeader($status) { |
|
| 120 | + // we have one change currently for the http 1.0 header that differs |
|
| 121 | + // from 1.1: STATUS_TEMPORARY_REDIRECT should be STATUS_FOUND |
|
| 122 | + // if this differs any more, we want to create childclasses for this |
|
| 123 | + if ($status === self::STATUS_TEMPORARY_REDIRECT |
|
| 124 | + && $this->protocolVersion === 'HTTP/1.0') { |
|
| 125 | + $status = self::STATUS_FOUND; |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - return $this->protocolVersion . ' ' . $status . ' ' . |
|
| 129 | - $this->headers[$status]; |
|
| 130 | - } |
|
| 128 | + return $this->protocolVersion . ' ' . $status . ' ' . |
|
| 129 | + $this->headers[$status]; |
|
| 130 | + } |
|
| 131 | 131 | } |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | * @param array $server $_SERVER |
| 42 | 42 | * @param string $protocolVersion the http version to use defaults to HTTP/1.1 |
| 43 | 43 | */ |
| 44 | - public function __construct($server, $protocolVersion='HTTP/1.1') { |
|
| 44 | + public function __construct($server, $protocolVersion = 'HTTP/1.1') { |
|
| 45 | 45 | $this->server = $server; |
| 46 | 46 | $this->protocolVersion = $protocolVersion; |
| 47 | 47 | |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | $status = self::STATUS_FOUND; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - return $this->protocolVersion . ' ' . $status . ' ' . |
|
| 128 | + return $this->protocolVersion.' '.$status.' '. |
|
| 129 | 129 | $this->headers[$status]; |
| 130 | 130 | } |
| 131 | 131 | } |
@@ -46,147 +46,147 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | class Dispatcher { |
| 48 | 48 | |
| 49 | - /** @var MiddlewareDispatcher */ |
|
| 50 | - private $middlewareDispatcher; |
|
| 51 | - |
|
| 52 | - /** @var Http */ |
|
| 53 | - private $protocol; |
|
| 54 | - |
|
| 55 | - /** @var ControllerMethodReflector */ |
|
| 56 | - private $reflector; |
|
| 57 | - |
|
| 58 | - /** @var IRequest */ |
|
| 59 | - private $request; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param Http $protocol the http protocol with contains all status headers |
|
| 63 | - * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which |
|
| 64 | - * runs the middleware |
|
| 65 | - * @param ControllerMethodReflector $reflector the reflector that is used to inject |
|
| 66 | - * the arguments for the controller |
|
| 67 | - * @param IRequest $request the incoming request |
|
| 68 | - */ |
|
| 69 | - public function __construct(Http $protocol, |
|
| 70 | - MiddlewareDispatcher $middlewareDispatcher, |
|
| 71 | - ControllerMethodReflector $reflector, |
|
| 72 | - IRequest $request) { |
|
| 73 | - $this->protocol = $protocol; |
|
| 74 | - $this->middlewareDispatcher = $middlewareDispatcher; |
|
| 75 | - $this->reflector = $reflector; |
|
| 76 | - $this->request = $request; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Handles a request and calls the dispatcher on the controller |
|
| 82 | - * @param Controller $controller the controller which will be called |
|
| 83 | - * @param string $methodName the method name which will be called on |
|
| 84 | - * the controller |
|
| 85 | - * @return array $array[0] contains a string with the http main header, |
|
| 86 | - * $array[1] contains headers in the form: $key => value, $array[2] contains |
|
| 87 | - * the response output |
|
| 88 | - * @throws \Exception |
|
| 89 | - */ |
|
| 90 | - public function dispatch(Controller $controller, string $methodName): array { |
|
| 91 | - $out = [null, [], null]; |
|
| 92 | - |
|
| 93 | - try { |
|
| 94 | - // prefill reflector with everything thats needed for the |
|
| 95 | - // middlewares |
|
| 96 | - $this->reflector->reflect($controller, $methodName); |
|
| 97 | - |
|
| 98 | - $this->middlewareDispatcher->beforeController($controller, |
|
| 99 | - $methodName); |
|
| 100 | - $response = $this->executeController($controller, $methodName); |
|
| 101 | - |
|
| 102 | - // if an exception appears, the middleware checks if it can handle the |
|
| 103 | - // exception and creates a response. If no response is created, it is |
|
| 104 | - // assumed that theres no middleware who can handle it and the error is |
|
| 105 | - // thrown again |
|
| 106 | - } catch (\Exception $exception) { |
|
| 107 | - $response = $this->middlewareDispatcher->afterException( |
|
| 108 | - $controller, $methodName, $exception); |
|
| 109 | - } catch (\Throwable $throwable) { |
|
| 110 | - $exception = new \Exception($throwable->getMessage(), $throwable->getCode(), $throwable); |
|
| 111 | - $response = $this->middlewareDispatcher->afterException( |
|
| 112 | - $controller, $methodName, $exception); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $response = $this->middlewareDispatcher->afterController( |
|
| 116 | - $controller, $methodName, $response); |
|
| 117 | - |
|
| 118 | - // depending on the cache object the headers need to be changed |
|
| 119 | - $out[0] = $this->protocol->getStatusHeader($response->getStatus()); |
|
| 120 | - $out[1] = array_merge($response->getHeaders()); |
|
| 121 | - $out[2] = $response->getCookies(); |
|
| 122 | - $out[3] = $this->middlewareDispatcher->beforeOutput( |
|
| 123 | - $controller, $methodName, $response->render() |
|
| 124 | - ); |
|
| 125 | - $out[4] = $response; |
|
| 126 | - |
|
| 127 | - return $out; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Uses the reflected parameters, types and request parameters to execute |
|
| 133 | - * the controller |
|
| 134 | - * @param Controller $controller the controller to be executed |
|
| 135 | - * @param string $methodName the method on the controller that should be executed |
|
| 136 | - * @return Response |
|
| 137 | - */ |
|
| 138 | - private function executeController(Controller $controller, string $methodName): Response { |
|
| 139 | - $arguments = []; |
|
| 140 | - |
|
| 141 | - // valid types that will be casted |
|
| 142 | - $types = ['int', 'integer', 'bool', 'boolean', 'float']; |
|
| 143 | - |
|
| 144 | - foreach ($this->reflector->getParameters() as $param => $default) { |
|
| 145 | - |
|
| 146 | - // try to get the parameter from the request object and cast |
|
| 147 | - // it to the type annotated in the @param annotation |
|
| 148 | - $value = $this->request->getParam($param, $default); |
|
| 149 | - $type = $this->reflector->getType($param); |
|
| 150 | - |
|
| 151 | - // if this is submitted using GET or a POST form, 'false' should be |
|
| 152 | - // converted to false |
|
| 153 | - if (($type === 'bool' || $type === 'boolean') && |
|
| 154 | - $value === 'false' && |
|
| 155 | - ( |
|
| 156 | - $this->request->method === 'GET' || |
|
| 157 | - strpos($this->request->getHeader('Content-Type'), |
|
| 158 | - 'application/x-www-form-urlencoded') !== false |
|
| 159 | - ) |
|
| 160 | - ) { |
|
| 161 | - $value = false; |
|
| 162 | - } elseif ($value !== null && \in_array($type, $types, true)) { |
|
| 163 | - settype($value, $type); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $arguments[] = $value; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - $response = \call_user_func_array([$controller, $methodName], $arguments); |
|
| 170 | - |
|
| 171 | - // format response |
|
| 172 | - if ($response instanceof DataResponse || !($response instanceof Response)) { |
|
| 173 | - |
|
| 174 | - // get format from the url format or request format parameter |
|
| 175 | - $format = $this->request->getParam('format'); |
|
| 176 | - |
|
| 177 | - // if none is given try the first Accept header |
|
| 178 | - if ($format === null) { |
|
| 179 | - $headers = $this->request->getHeader('Accept'); |
|
| 180 | - $format = $controller->getResponderByHTTPHeader($headers, null); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - if ($format !== null) { |
|
| 184 | - $response = $controller->buildResponse($response, $format); |
|
| 185 | - } else { |
|
| 186 | - $response = $controller->buildResponse($response); |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $response; |
|
| 191 | - } |
|
| 49 | + /** @var MiddlewareDispatcher */ |
|
| 50 | + private $middlewareDispatcher; |
|
| 51 | + |
|
| 52 | + /** @var Http */ |
|
| 53 | + private $protocol; |
|
| 54 | + |
|
| 55 | + /** @var ControllerMethodReflector */ |
|
| 56 | + private $reflector; |
|
| 57 | + |
|
| 58 | + /** @var IRequest */ |
|
| 59 | + private $request; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param Http $protocol the http protocol with contains all status headers |
|
| 63 | + * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which |
|
| 64 | + * runs the middleware |
|
| 65 | + * @param ControllerMethodReflector $reflector the reflector that is used to inject |
|
| 66 | + * the arguments for the controller |
|
| 67 | + * @param IRequest $request the incoming request |
|
| 68 | + */ |
|
| 69 | + public function __construct(Http $protocol, |
|
| 70 | + MiddlewareDispatcher $middlewareDispatcher, |
|
| 71 | + ControllerMethodReflector $reflector, |
|
| 72 | + IRequest $request) { |
|
| 73 | + $this->protocol = $protocol; |
|
| 74 | + $this->middlewareDispatcher = $middlewareDispatcher; |
|
| 75 | + $this->reflector = $reflector; |
|
| 76 | + $this->request = $request; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Handles a request and calls the dispatcher on the controller |
|
| 82 | + * @param Controller $controller the controller which will be called |
|
| 83 | + * @param string $methodName the method name which will be called on |
|
| 84 | + * the controller |
|
| 85 | + * @return array $array[0] contains a string with the http main header, |
|
| 86 | + * $array[1] contains headers in the form: $key => value, $array[2] contains |
|
| 87 | + * the response output |
|
| 88 | + * @throws \Exception |
|
| 89 | + */ |
|
| 90 | + public function dispatch(Controller $controller, string $methodName): array { |
|
| 91 | + $out = [null, [], null]; |
|
| 92 | + |
|
| 93 | + try { |
|
| 94 | + // prefill reflector with everything thats needed for the |
|
| 95 | + // middlewares |
|
| 96 | + $this->reflector->reflect($controller, $methodName); |
|
| 97 | + |
|
| 98 | + $this->middlewareDispatcher->beforeController($controller, |
|
| 99 | + $methodName); |
|
| 100 | + $response = $this->executeController($controller, $methodName); |
|
| 101 | + |
|
| 102 | + // if an exception appears, the middleware checks if it can handle the |
|
| 103 | + // exception and creates a response. If no response is created, it is |
|
| 104 | + // assumed that theres no middleware who can handle it and the error is |
|
| 105 | + // thrown again |
|
| 106 | + } catch (\Exception $exception) { |
|
| 107 | + $response = $this->middlewareDispatcher->afterException( |
|
| 108 | + $controller, $methodName, $exception); |
|
| 109 | + } catch (\Throwable $throwable) { |
|
| 110 | + $exception = new \Exception($throwable->getMessage(), $throwable->getCode(), $throwable); |
|
| 111 | + $response = $this->middlewareDispatcher->afterException( |
|
| 112 | + $controller, $methodName, $exception); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $response = $this->middlewareDispatcher->afterController( |
|
| 116 | + $controller, $methodName, $response); |
|
| 117 | + |
|
| 118 | + // depending on the cache object the headers need to be changed |
|
| 119 | + $out[0] = $this->protocol->getStatusHeader($response->getStatus()); |
|
| 120 | + $out[1] = array_merge($response->getHeaders()); |
|
| 121 | + $out[2] = $response->getCookies(); |
|
| 122 | + $out[3] = $this->middlewareDispatcher->beforeOutput( |
|
| 123 | + $controller, $methodName, $response->render() |
|
| 124 | + ); |
|
| 125 | + $out[4] = $response; |
|
| 126 | + |
|
| 127 | + return $out; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Uses the reflected parameters, types and request parameters to execute |
|
| 133 | + * the controller |
|
| 134 | + * @param Controller $controller the controller to be executed |
|
| 135 | + * @param string $methodName the method on the controller that should be executed |
|
| 136 | + * @return Response |
|
| 137 | + */ |
|
| 138 | + private function executeController(Controller $controller, string $methodName): Response { |
|
| 139 | + $arguments = []; |
|
| 140 | + |
|
| 141 | + // valid types that will be casted |
|
| 142 | + $types = ['int', 'integer', 'bool', 'boolean', 'float']; |
|
| 143 | + |
|
| 144 | + foreach ($this->reflector->getParameters() as $param => $default) { |
|
| 145 | + |
|
| 146 | + // try to get the parameter from the request object and cast |
|
| 147 | + // it to the type annotated in the @param annotation |
|
| 148 | + $value = $this->request->getParam($param, $default); |
|
| 149 | + $type = $this->reflector->getType($param); |
|
| 150 | + |
|
| 151 | + // if this is submitted using GET or a POST form, 'false' should be |
|
| 152 | + // converted to false |
|
| 153 | + if (($type === 'bool' || $type === 'boolean') && |
|
| 154 | + $value === 'false' && |
|
| 155 | + ( |
|
| 156 | + $this->request->method === 'GET' || |
|
| 157 | + strpos($this->request->getHeader('Content-Type'), |
|
| 158 | + 'application/x-www-form-urlencoded') !== false |
|
| 159 | + ) |
|
| 160 | + ) { |
|
| 161 | + $value = false; |
|
| 162 | + } elseif ($value !== null && \in_array($type, $types, true)) { |
|
| 163 | + settype($value, $type); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $arguments[] = $value; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + $response = \call_user_func_array([$controller, $methodName], $arguments); |
|
| 170 | + |
|
| 171 | + // format response |
|
| 172 | + if ($response instanceof DataResponse || !($response instanceof Response)) { |
|
| 173 | + |
|
| 174 | + // get format from the url format or request format parameter |
|
| 175 | + $format = $this->request->getParam('format'); |
|
| 176 | + |
|
| 177 | + // if none is given try the first Accept header |
|
| 178 | + if ($format === null) { |
|
| 179 | + $headers = $this->request->getHeader('Accept'); |
|
| 180 | + $format = $controller->getResponderByHTTPHeader($headers, null); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + if ($format !== null) { |
|
| 184 | + $response = $controller->buildResponse($response, $format); |
|
| 185 | + } else { |
|
| 186 | + $response = $controller->buildResponse($response); |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $response; |
|
| 191 | + } |
|
| 192 | 192 | } |