@@ -32,48 +32,48 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | interface IOutput { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @param string $out |
|
| 37 | - * @since 8.1.0 |
|
| 38 | - */ |
|
| 39 | - public function setOutput($out); |
|
| 35 | + /** |
|
| 36 | + * @param string $out |
|
| 37 | + * @since 8.1.0 |
|
| 38 | + */ |
|
| 39 | + public function setOutput($out); |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @param string|resource $path or file handle |
|
| 43 | - * |
|
| 44 | - * @return bool false if an error occurred |
|
| 45 | - * @since 8.1.0 |
|
| 46 | - */ |
|
| 47 | - public function setReadfile($path); |
|
| 41 | + /** |
|
| 42 | + * @param string|resource $path or file handle |
|
| 43 | + * |
|
| 44 | + * @return bool false if an error occurred |
|
| 45 | + * @since 8.1.0 |
|
| 46 | + */ |
|
| 47 | + public function setReadfile($path); |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @param string $header |
|
| 51 | - * @since 8.1.0 |
|
| 52 | - */ |
|
| 53 | - public function setHeader($header); |
|
| 49 | + /** |
|
| 50 | + * @param string $header |
|
| 51 | + * @since 8.1.0 |
|
| 52 | + */ |
|
| 53 | + public function setHeader($header); |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * @return int returns the current http response code |
|
| 57 | - * @since 8.1.0 |
|
| 58 | - */ |
|
| 59 | - public function getHttpResponseCode(); |
|
| 55 | + /** |
|
| 56 | + * @return int returns the current http response code |
|
| 57 | + * @since 8.1.0 |
|
| 58 | + */ |
|
| 59 | + public function getHttpResponseCode(); |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @param int $code sets the http status code |
|
| 63 | - * @since 8.1.0 |
|
| 64 | - */ |
|
| 65 | - public function setHttpResponseCode($code); |
|
| 61 | + /** |
|
| 62 | + * @param int $code sets the http status code |
|
| 63 | + * @since 8.1.0 |
|
| 64 | + */ |
|
| 65 | + public function setHttpResponseCode($code); |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @param string $name |
|
| 69 | - * @param string $value |
|
| 70 | - * @param int $expire |
|
| 71 | - * @param string $path |
|
| 72 | - * @param string $domain |
|
| 73 | - * @param bool $secure |
|
| 74 | - * @param bool $httpOnly |
|
| 75 | - * @param string $sameSite (added in 20) |
|
| 76 | - * @since 8.1.0 |
|
| 77 | - */ |
|
| 78 | - public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax'); |
|
| 67 | + /** |
|
| 68 | + * @param string $name |
|
| 69 | + * @param string $value |
|
| 70 | + * @param int $expire |
|
| 71 | + * @param string $path |
|
| 72 | + * @param string $domain |
|
| 73 | + * @param bool $secure |
|
| 74 | + * @param bool $httpOnly |
|
| 75 | + * @param string $sameSite (added in 20) |
|
| 76 | + * @since 8.1.0 |
|
| 77 | + */ |
|
| 78 | + public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax'); |
|
| 79 | 79 | } |
@@ -45,359 +45,359 @@ |
||
| 45 | 45 | */ |
| 46 | 46 | class Response { |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate'] |
|
| 50 | - * @var array |
|
| 51 | - */ |
|
| 52 | - private $headers = [ |
|
| 53 | - 'Cache-Control' => 'no-cache, no-store, must-revalidate' |
|
| 54 | - ]; |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Cookies that will be need to be constructed as header |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - private $cookies = []; |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * HTTP status code - defaults to STATUS OK |
|
| 66 | - * @var int |
|
| 67 | - */ |
|
| 68 | - private $status = Http::STATUS_OK; |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Last modified date |
|
| 73 | - * @var \DateTime |
|
| 74 | - */ |
|
| 75 | - private $lastModified; |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * ETag |
|
| 80 | - * @var string |
|
| 81 | - */ |
|
| 82 | - private $ETag; |
|
| 83 | - |
|
| 84 | - /** @var ContentSecurityPolicy|null Used Content-Security-Policy */ |
|
| 85 | - private $contentSecurityPolicy = null; |
|
| 86 | - |
|
| 87 | - /** @var FeaturePolicy */ |
|
| 88 | - private $featurePolicy; |
|
| 89 | - |
|
| 90 | - /** @var bool */ |
|
| 91 | - private $throttled = false; |
|
| 92 | - /** @var array */ |
|
| 93 | - private $throttleMetadata = []; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @since 17.0.0 |
|
| 97 | - */ |
|
| 98 | - public function __construct() { |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Caches the response |
|
| 103 | - * @param int $cacheSeconds the amount of seconds that should be cached |
|
| 104 | - * if 0 then caching will be disabled |
|
| 105 | - * @return $this |
|
| 106 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 107 | - */ |
|
| 108 | - public function cacheFor(int $cacheSeconds, bool $public = false) { |
|
| 109 | - if ($cacheSeconds > 0) { |
|
| 110 | - $pragma = $public ? 'public' : 'private'; |
|
| 111 | - $this->addHeader('Cache-Control', $pragma . ', max-age=' . $cacheSeconds . ', must-revalidate'); |
|
| 112 | - $this->addHeader('Pragma', $pragma); |
|
| 113 | - |
|
| 114 | - // Set expires header |
|
| 115 | - $expires = new \DateTime(); |
|
| 116 | - /** @var ITimeFactory $time */ |
|
| 117 | - $time = \OC::$server->query(ITimeFactory::class); |
|
| 118 | - $expires->setTimestamp($time->getTime()); |
|
| 119 | - $expires->add(new \DateInterval('PT'.$cacheSeconds.'S')); |
|
| 120 | - $this->addHeader('Expires', $expires->format(\DateTime::RFC2822)); |
|
| 121 | - } else { |
|
| 122 | - $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); |
|
| 123 | - unset($this->headers['Expires'], $this->headers['Pragma']); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $this; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Adds a new cookie to the response |
|
| 131 | - * @param string $name The name of the cookie |
|
| 132 | - * @param string $value The value of the cookie |
|
| 133 | - * @param \DateTime|null $expireDate Date on that the cookie should expire, if set |
|
| 134 | - * to null cookie will be considered as session |
|
| 135 | - * cookie. |
|
| 136 | - * @param string $sameSite The samesite value of the cookie. Defaults to Lax. Other possibilities are Strict or None |
|
| 137 | - * @return $this |
|
| 138 | - * @since 8.0.0 |
|
| 139 | - */ |
|
| 140 | - public function addCookie($name, $value, \DateTime $expireDate = null, $sameSite = 'Lax') { |
|
| 141 | - $this->cookies[$name] = ['value' => $value, 'expireDate' => $expireDate, 'sameSite' => $sameSite]; |
|
| 142 | - return $this; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Set the specified cookies |
|
| 148 | - * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) |
|
| 149 | - * @return $this |
|
| 150 | - * @since 8.0.0 |
|
| 151 | - */ |
|
| 152 | - public function setCookies(array $cookies) { |
|
| 153 | - $this->cookies = $cookies; |
|
| 154 | - return $this; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Invalidates the specified cookie |
|
| 160 | - * @param string $name |
|
| 161 | - * @return $this |
|
| 162 | - * @since 8.0.0 |
|
| 163 | - */ |
|
| 164 | - public function invalidateCookie($name) { |
|
| 165 | - $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00')); |
|
| 166 | - return $this; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Invalidates the specified cookies |
|
| 171 | - * @param array $cookieNames array('foo', 'bar') |
|
| 172 | - * @return $this |
|
| 173 | - * @since 8.0.0 |
|
| 174 | - */ |
|
| 175 | - public function invalidateCookies(array $cookieNames) { |
|
| 176 | - foreach ($cookieNames as $cookieName) { |
|
| 177 | - $this->invalidateCookie($cookieName); |
|
| 178 | - } |
|
| 179 | - return $this; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Returns the cookies |
|
| 184 | - * @return array |
|
| 185 | - * @since 8.0.0 |
|
| 186 | - */ |
|
| 187 | - public function getCookies() { |
|
| 188 | - return $this->cookies; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Adds a new header to the response that will be called before the render |
|
| 193 | - * function |
|
| 194 | - * @param string $name The name of the HTTP header |
|
| 195 | - * @param string $value The value, null will delete it |
|
| 196 | - * @return $this |
|
| 197 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 198 | - */ |
|
| 199 | - public function addHeader($name, $value) { |
|
| 200 | - $name = trim($name); // always remove leading and trailing whitespace |
|
| 201 | - // to be able to reliably check for security |
|
| 202 | - // headers |
|
| 203 | - |
|
| 204 | - if (is_null($value)) { |
|
| 205 | - unset($this->headers[$name]); |
|
| 206 | - } else { |
|
| 207 | - $this->headers[$name] = $value; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - return $this; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Set the headers |
|
| 216 | - * @param array $headers value header pairs |
|
| 217 | - * @return $this |
|
| 218 | - * @since 8.0.0 |
|
| 219 | - */ |
|
| 220 | - public function setHeaders(array $headers) { |
|
| 221 | - $this->headers = $headers; |
|
| 222 | - |
|
| 223 | - return $this; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Returns the set headers |
|
| 229 | - * @return array the headers |
|
| 230 | - * @since 6.0.0 |
|
| 231 | - */ |
|
| 232 | - public function getHeaders() { |
|
| 233 | - $mergeWith = []; |
|
| 234 | - |
|
| 235 | - if ($this->lastModified) { |
|
| 236 | - $mergeWith['Last-Modified'] = |
|
| 237 | - $this->lastModified->format(\DateTime::RFC2822); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - $this->headers['Content-Security-Policy'] = $this->getContentSecurityPolicy()->buildPolicy(); |
|
| 241 | - $this->headers['Feature-Policy'] = $this->getFeaturePolicy()->buildPolicy(); |
|
| 242 | - |
|
| 243 | - if ($this->ETag) { |
|
| 244 | - $mergeWith['ETag'] = '"' . $this->ETag . '"'; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - return array_merge($mergeWith, $this->headers); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * By default renders no output |
|
| 253 | - * @return string |
|
| 254 | - * @since 6.0.0 |
|
| 255 | - */ |
|
| 256 | - public function render() { |
|
| 257 | - return ''; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Set response status |
|
| 263 | - * @param int $status a HTTP status code, see also the STATUS constants |
|
| 264 | - * @return Response Reference to this object |
|
| 265 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 266 | - */ |
|
| 267 | - public function setStatus($status) { |
|
| 268 | - $this->status = $status; |
|
| 269 | - |
|
| 270 | - return $this; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Set a Content-Security-Policy |
|
| 275 | - * @param EmptyContentSecurityPolicy $csp Policy to set for the response object |
|
| 276 | - * @return $this |
|
| 277 | - * @since 8.1.0 |
|
| 278 | - */ |
|
| 279 | - public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) { |
|
| 280 | - $this->contentSecurityPolicy = $csp; |
|
| 281 | - return $this; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Get the currently used Content-Security-Policy |
|
| 286 | - * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if |
|
| 287 | - * none specified. |
|
| 288 | - * @since 8.1.0 |
|
| 289 | - */ |
|
| 290 | - public function getContentSecurityPolicy() { |
|
| 291 | - if ($this->contentSecurityPolicy === null) { |
|
| 292 | - $this->setContentSecurityPolicy(new EmptyContentSecurityPolicy()); |
|
| 293 | - } |
|
| 294 | - return $this->contentSecurityPolicy; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * @since 17.0.0 |
|
| 300 | - */ |
|
| 301 | - public function getFeaturePolicy(): EmptyFeaturePolicy { |
|
| 302 | - if ($this->featurePolicy === null) { |
|
| 303 | - $this->setFeaturePolicy(new EmptyFeaturePolicy()); |
|
| 304 | - } |
|
| 305 | - return $this->featurePolicy; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * @since 17.0.0 |
|
| 310 | - */ |
|
| 311 | - public function setFeaturePolicy(EmptyFeaturePolicy $featurePolicy): self { |
|
| 312 | - $this->featurePolicy = $featurePolicy; |
|
| 313 | - |
|
| 314 | - return $this; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Get response status |
|
| 321 | - * @since 6.0.0 |
|
| 322 | - */ |
|
| 323 | - public function getStatus() { |
|
| 324 | - return $this->status; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Get the ETag |
|
| 330 | - * @return string the etag |
|
| 331 | - * @since 6.0.0 |
|
| 332 | - */ |
|
| 333 | - public function getETag() { |
|
| 334 | - return $this->ETag; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Get "last modified" date |
|
| 340 | - * @return \DateTime RFC2822 formatted last modified date |
|
| 341 | - * @since 6.0.0 |
|
| 342 | - */ |
|
| 343 | - public function getLastModified() { |
|
| 344 | - return $this->lastModified; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Set the ETag |
|
| 350 | - * @param string $ETag |
|
| 351 | - * @return Response Reference to this object |
|
| 352 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 353 | - */ |
|
| 354 | - public function setETag($ETag) { |
|
| 355 | - $this->ETag = $ETag; |
|
| 356 | - |
|
| 357 | - return $this; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Set "last modified" date |
|
| 363 | - * @param \DateTime $lastModified |
|
| 364 | - * @return Response Reference to this object |
|
| 365 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 366 | - */ |
|
| 367 | - public function setLastModified($lastModified) { |
|
| 368 | - $this->lastModified = $lastModified; |
|
| 369 | - |
|
| 370 | - return $this; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Marks the response as to throttle. Will be throttled when the |
|
| 375 | - * @BruteForceProtection annotation is added. |
|
| 376 | - * |
|
| 377 | - * @param array $metadata |
|
| 378 | - * @since 12.0.0 |
|
| 379 | - */ |
|
| 380 | - public function throttle(array $metadata = []) { |
|
| 381 | - $this->throttled = true; |
|
| 382 | - $this->throttleMetadata = $metadata; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Returns the throttle metadata, defaults to empty array |
|
| 387 | - * |
|
| 388 | - * @return array |
|
| 389 | - * @since 13.0.0 |
|
| 390 | - */ |
|
| 391 | - public function getThrottleMetadata() { |
|
| 392 | - return $this->throttleMetadata; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Whether the current response is throttled. |
|
| 397 | - * |
|
| 398 | - * @since 12.0.0 |
|
| 399 | - */ |
|
| 400 | - public function isThrottled() { |
|
| 401 | - return $this->throttled; |
|
| 402 | - } |
|
| 48 | + /** |
|
| 49 | + * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate'] |
|
| 50 | + * @var array |
|
| 51 | + */ |
|
| 52 | + private $headers = [ |
|
| 53 | + 'Cache-Control' => 'no-cache, no-store, must-revalidate' |
|
| 54 | + ]; |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Cookies that will be need to be constructed as header |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + private $cookies = []; |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * HTTP status code - defaults to STATUS OK |
|
| 66 | + * @var int |
|
| 67 | + */ |
|
| 68 | + private $status = Http::STATUS_OK; |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Last modified date |
|
| 73 | + * @var \DateTime |
|
| 74 | + */ |
|
| 75 | + private $lastModified; |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * ETag |
|
| 80 | + * @var string |
|
| 81 | + */ |
|
| 82 | + private $ETag; |
|
| 83 | + |
|
| 84 | + /** @var ContentSecurityPolicy|null Used Content-Security-Policy */ |
|
| 85 | + private $contentSecurityPolicy = null; |
|
| 86 | + |
|
| 87 | + /** @var FeaturePolicy */ |
|
| 88 | + private $featurePolicy; |
|
| 89 | + |
|
| 90 | + /** @var bool */ |
|
| 91 | + private $throttled = false; |
|
| 92 | + /** @var array */ |
|
| 93 | + private $throttleMetadata = []; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @since 17.0.0 |
|
| 97 | + */ |
|
| 98 | + public function __construct() { |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Caches the response |
|
| 103 | + * @param int $cacheSeconds the amount of seconds that should be cached |
|
| 104 | + * if 0 then caching will be disabled |
|
| 105 | + * @return $this |
|
| 106 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 107 | + */ |
|
| 108 | + public function cacheFor(int $cacheSeconds, bool $public = false) { |
|
| 109 | + if ($cacheSeconds > 0) { |
|
| 110 | + $pragma = $public ? 'public' : 'private'; |
|
| 111 | + $this->addHeader('Cache-Control', $pragma . ', max-age=' . $cacheSeconds . ', must-revalidate'); |
|
| 112 | + $this->addHeader('Pragma', $pragma); |
|
| 113 | + |
|
| 114 | + // Set expires header |
|
| 115 | + $expires = new \DateTime(); |
|
| 116 | + /** @var ITimeFactory $time */ |
|
| 117 | + $time = \OC::$server->query(ITimeFactory::class); |
|
| 118 | + $expires->setTimestamp($time->getTime()); |
|
| 119 | + $expires->add(new \DateInterval('PT'.$cacheSeconds.'S')); |
|
| 120 | + $this->addHeader('Expires', $expires->format(\DateTime::RFC2822)); |
|
| 121 | + } else { |
|
| 122 | + $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); |
|
| 123 | + unset($this->headers['Expires'], $this->headers['Pragma']); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $this; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Adds a new cookie to the response |
|
| 131 | + * @param string $name The name of the cookie |
|
| 132 | + * @param string $value The value of the cookie |
|
| 133 | + * @param \DateTime|null $expireDate Date on that the cookie should expire, if set |
|
| 134 | + * to null cookie will be considered as session |
|
| 135 | + * cookie. |
|
| 136 | + * @param string $sameSite The samesite value of the cookie. Defaults to Lax. Other possibilities are Strict or None |
|
| 137 | + * @return $this |
|
| 138 | + * @since 8.0.0 |
|
| 139 | + */ |
|
| 140 | + public function addCookie($name, $value, \DateTime $expireDate = null, $sameSite = 'Lax') { |
|
| 141 | + $this->cookies[$name] = ['value' => $value, 'expireDate' => $expireDate, 'sameSite' => $sameSite]; |
|
| 142 | + return $this; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Set the specified cookies |
|
| 148 | + * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) |
|
| 149 | + * @return $this |
|
| 150 | + * @since 8.0.0 |
|
| 151 | + */ |
|
| 152 | + public function setCookies(array $cookies) { |
|
| 153 | + $this->cookies = $cookies; |
|
| 154 | + return $this; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Invalidates the specified cookie |
|
| 160 | + * @param string $name |
|
| 161 | + * @return $this |
|
| 162 | + * @since 8.0.0 |
|
| 163 | + */ |
|
| 164 | + public function invalidateCookie($name) { |
|
| 165 | + $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00')); |
|
| 166 | + return $this; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Invalidates the specified cookies |
|
| 171 | + * @param array $cookieNames array('foo', 'bar') |
|
| 172 | + * @return $this |
|
| 173 | + * @since 8.0.0 |
|
| 174 | + */ |
|
| 175 | + public function invalidateCookies(array $cookieNames) { |
|
| 176 | + foreach ($cookieNames as $cookieName) { |
|
| 177 | + $this->invalidateCookie($cookieName); |
|
| 178 | + } |
|
| 179 | + return $this; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Returns the cookies |
|
| 184 | + * @return array |
|
| 185 | + * @since 8.0.0 |
|
| 186 | + */ |
|
| 187 | + public function getCookies() { |
|
| 188 | + return $this->cookies; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Adds a new header to the response that will be called before the render |
|
| 193 | + * function |
|
| 194 | + * @param string $name The name of the HTTP header |
|
| 195 | + * @param string $value The value, null will delete it |
|
| 196 | + * @return $this |
|
| 197 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 198 | + */ |
|
| 199 | + public function addHeader($name, $value) { |
|
| 200 | + $name = trim($name); // always remove leading and trailing whitespace |
|
| 201 | + // to be able to reliably check for security |
|
| 202 | + // headers |
|
| 203 | + |
|
| 204 | + if (is_null($value)) { |
|
| 205 | + unset($this->headers[$name]); |
|
| 206 | + } else { |
|
| 207 | + $this->headers[$name] = $value; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + return $this; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Set the headers |
|
| 216 | + * @param array $headers value header pairs |
|
| 217 | + * @return $this |
|
| 218 | + * @since 8.0.0 |
|
| 219 | + */ |
|
| 220 | + public function setHeaders(array $headers) { |
|
| 221 | + $this->headers = $headers; |
|
| 222 | + |
|
| 223 | + return $this; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Returns the set headers |
|
| 229 | + * @return array the headers |
|
| 230 | + * @since 6.0.0 |
|
| 231 | + */ |
|
| 232 | + public function getHeaders() { |
|
| 233 | + $mergeWith = []; |
|
| 234 | + |
|
| 235 | + if ($this->lastModified) { |
|
| 236 | + $mergeWith['Last-Modified'] = |
|
| 237 | + $this->lastModified->format(\DateTime::RFC2822); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + $this->headers['Content-Security-Policy'] = $this->getContentSecurityPolicy()->buildPolicy(); |
|
| 241 | + $this->headers['Feature-Policy'] = $this->getFeaturePolicy()->buildPolicy(); |
|
| 242 | + |
|
| 243 | + if ($this->ETag) { |
|
| 244 | + $mergeWith['ETag'] = '"' . $this->ETag . '"'; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + return array_merge($mergeWith, $this->headers); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * By default renders no output |
|
| 253 | + * @return string |
|
| 254 | + * @since 6.0.0 |
|
| 255 | + */ |
|
| 256 | + public function render() { |
|
| 257 | + return ''; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Set response status |
|
| 263 | + * @param int $status a HTTP status code, see also the STATUS constants |
|
| 264 | + * @return Response Reference to this object |
|
| 265 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 266 | + */ |
|
| 267 | + public function setStatus($status) { |
|
| 268 | + $this->status = $status; |
|
| 269 | + |
|
| 270 | + return $this; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Set a Content-Security-Policy |
|
| 275 | + * @param EmptyContentSecurityPolicy $csp Policy to set for the response object |
|
| 276 | + * @return $this |
|
| 277 | + * @since 8.1.0 |
|
| 278 | + */ |
|
| 279 | + public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) { |
|
| 280 | + $this->contentSecurityPolicy = $csp; |
|
| 281 | + return $this; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Get the currently used Content-Security-Policy |
|
| 286 | + * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if |
|
| 287 | + * none specified. |
|
| 288 | + * @since 8.1.0 |
|
| 289 | + */ |
|
| 290 | + public function getContentSecurityPolicy() { |
|
| 291 | + if ($this->contentSecurityPolicy === null) { |
|
| 292 | + $this->setContentSecurityPolicy(new EmptyContentSecurityPolicy()); |
|
| 293 | + } |
|
| 294 | + return $this->contentSecurityPolicy; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * @since 17.0.0 |
|
| 300 | + */ |
|
| 301 | + public function getFeaturePolicy(): EmptyFeaturePolicy { |
|
| 302 | + if ($this->featurePolicy === null) { |
|
| 303 | + $this->setFeaturePolicy(new EmptyFeaturePolicy()); |
|
| 304 | + } |
|
| 305 | + return $this->featurePolicy; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * @since 17.0.0 |
|
| 310 | + */ |
|
| 311 | + public function setFeaturePolicy(EmptyFeaturePolicy $featurePolicy): self { |
|
| 312 | + $this->featurePolicy = $featurePolicy; |
|
| 313 | + |
|
| 314 | + return $this; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Get response status |
|
| 321 | + * @since 6.0.0 |
|
| 322 | + */ |
|
| 323 | + public function getStatus() { |
|
| 324 | + return $this->status; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Get the ETag |
|
| 330 | + * @return string the etag |
|
| 331 | + * @since 6.0.0 |
|
| 332 | + */ |
|
| 333 | + public function getETag() { |
|
| 334 | + return $this->ETag; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Get "last modified" date |
|
| 340 | + * @return \DateTime RFC2822 formatted last modified date |
|
| 341 | + * @since 6.0.0 |
|
| 342 | + */ |
|
| 343 | + public function getLastModified() { |
|
| 344 | + return $this->lastModified; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Set the ETag |
|
| 350 | + * @param string $ETag |
|
| 351 | + * @return Response Reference to this object |
|
| 352 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 353 | + */ |
|
| 354 | + public function setETag($ETag) { |
|
| 355 | + $this->ETag = $ETag; |
|
| 356 | + |
|
| 357 | + return $this; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Set "last modified" date |
|
| 363 | + * @param \DateTime $lastModified |
|
| 364 | + * @return Response Reference to this object |
|
| 365 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 366 | + */ |
|
| 367 | + public function setLastModified($lastModified) { |
|
| 368 | + $this->lastModified = $lastModified; |
|
| 369 | + |
|
| 370 | + return $this; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Marks the response as to throttle. Will be throttled when the |
|
| 375 | + * @BruteForceProtection annotation is added. |
|
| 376 | + * |
|
| 377 | + * @param array $metadata |
|
| 378 | + * @since 12.0.0 |
|
| 379 | + */ |
|
| 380 | + public function throttle(array $metadata = []) { |
|
| 381 | + $this->throttled = true; |
|
| 382 | + $this->throttleMetadata = $metadata; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Returns the throttle metadata, defaults to empty array |
|
| 387 | + * |
|
| 388 | + * @return array |
|
| 389 | + * @since 13.0.0 |
|
| 390 | + */ |
|
| 391 | + public function getThrottleMetadata() { |
|
| 392 | + return $this->throttleMetadata; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Whether the current response is throttled. |
|
| 397 | + * |
|
| 398 | + * @since 12.0.0 |
|
| 399 | + */ |
|
| 400 | + public function isThrottled() { |
|
| 401 | + return $this->throttled; |
|
| 402 | + } |
|
| 403 | 403 | } |
@@ -31,81 +31,81 @@ |
||
| 31 | 31 | * Very thin wrapper class to make output testable |
| 32 | 32 | */ |
| 33 | 33 | class Output implements IOutput { |
| 34 | - /** @var string */ |
|
| 35 | - private $webRoot; |
|
| 34 | + /** @var string */ |
|
| 35 | + private $webRoot; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @param $webRoot |
|
| 39 | - */ |
|
| 40 | - public function __construct($webRoot) { |
|
| 41 | - $this->webRoot = $webRoot; |
|
| 42 | - } |
|
| 37 | + /** |
|
| 38 | + * @param $webRoot |
|
| 39 | + */ |
|
| 40 | + public function __construct($webRoot) { |
|
| 41 | + $this->webRoot = $webRoot; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @param string $out |
|
| 46 | - */ |
|
| 47 | - public function setOutput($out) { |
|
| 48 | - print($out); |
|
| 49 | - } |
|
| 44 | + /** |
|
| 45 | + * @param string $out |
|
| 46 | + */ |
|
| 47 | + public function setOutput($out) { |
|
| 48 | + print($out); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param string|resource $path or file handle |
|
| 53 | - * |
|
| 54 | - * @return bool false if an error occurred |
|
| 55 | - */ |
|
| 56 | - public function setReadfile($path) { |
|
| 57 | - if (is_resource($path)) { |
|
| 58 | - $output = fopen('php://output', 'w'); |
|
| 59 | - return stream_copy_to_stream($path, $output) > 0; |
|
| 60 | - } else { |
|
| 61 | - return @readfile($path); |
|
| 62 | - } |
|
| 63 | - } |
|
| 51 | + /** |
|
| 52 | + * @param string|resource $path or file handle |
|
| 53 | + * |
|
| 54 | + * @return bool false if an error occurred |
|
| 55 | + */ |
|
| 56 | + public function setReadfile($path) { |
|
| 57 | + if (is_resource($path)) { |
|
| 58 | + $output = fopen('php://output', 'w'); |
|
| 59 | + return stream_copy_to_stream($path, $output) > 0; |
|
| 60 | + } else { |
|
| 61 | + return @readfile($path); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * @param string $header |
|
| 67 | - */ |
|
| 68 | - public function setHeader($header) { |
|
| 69 | - header($header); |
|
| 70 | - } |
|
| 65 | + /** |
|
| 66 | + * @param string $header |
|
| 67 | + */ |
|
| 68 | + public function setHeader($header) { |
|
| 69 | + header($header); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @param int $code sets the http status code |
|
| 74 | - */ |
|
| 75 | - public function setHttpResponseCode($code) { |
|
| 76 | - http_response_code($code); |
|
| 77 | - } |
|
| 72 | + /** |
|
| 73 | + * @param int $code sets the http status code |
|
| 74 | + */ |
|
| 75 | + public function setHttpResponseCode($code) { |
|
| 76 | + http_response_code($code); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * @return int returns the current http response code |
|
| 81 | - */ |
|
| 82 | - public function getHttpResponseCode() { |
|
| 83 | - return http_response_code(); |
|
| 84 | - } |
|
| 79 | + /** |
|
| 80 | + * @return int returns the current http response code |
|
| 81 | + */ |
|
| 82 | + public function getHttpResponseCode() { |
|
| 83 | + return http_response_code(); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * @param string $name |
|
| 88 | - * @param string $value |
|
| 89 | - * @param int $expire |
|
| 90 | - * @param string $path |
|
| 91 | - * @param string $domain |
|
| 92 | - * @param bool $secure |
|
| 93 | - * @param bool $httpOnly |
|
| 94 | - */ |
|
| 95 | - public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax') { |
|
| 96 | - $path = $this->webRoot ? : '/'; |
|
| 86 | + /** |
|
| 87 | + * @param string $name |
|
| 88 | + * @param string $value |
|
| 89 | + * @param int $expire |
|
| 90 | + * @param string $path |
|
| 91 | + * @param string $domain |
|
| 92 | + * @param bool $secure |
|
| 93 | + * @param bool $httpOnly |
|
| 94 | + */ |
|
| 95 | + public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax') { |
|
| 96 | + $path = $this->webRoot ? : '/'; |
|
| 97 | 97 | |
| 98 | - if (PHP_VERSION_ID < 70300) { |
|
| 99 | - setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly); |
|
| 100 | - } else { |
|
| 101 | - setcookie($name, $value, [ |
|
| 102 | - 'expires' => $expire, |
|
| 103 | - 'path' => $path, |
|
| 104 | - 'domain' => $domain, |
|
| 105 | - 'secure' => $secure, |
|
| 106 | - 'httponly' => $httpOnly, |
|
| 107 | - 'samesite' => $sameSite |
|
| 108 | - ]); |
|
| 109 | - } |
|
| 110 | - } |
|
| 98 | + if (PHP_VERSION_ID < 70300) { |
|
| 99 | + setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly); |
|
| 100 | + } else { |
|
| 101 | + setcookie($name, $value, [ |
|
| 102 | + 'expires' => $expire, |
|
| 103 | + 'path' => $path, |
|
| 104 | + 'domain' => $domain, |
|
| 105 | + 'secure' => $secure, |
|
| 106 | + 'httponly' => $httpOnly, |
|
| 107 | + 'samesite' => $sameSite |
|
| 108 | + ]); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | } |
@@ -93,7 +93,7 @@ |
||
| 93 | 93 | * @param bool $httpOnly |
| 94 | 94 | */ |
| 95 | 95 | public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax') { |
| 96 | - $path = $this->webRoot ? : '/'; |
|
| 96 | + $path = $this->webRoot ?: '/'; |
|
| 97 | 97 | |
| 98 | 98 | if (PHP_VERSION_ID < 70300) { |
| 99 | 99 | setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly); |
@@ -48,167 +48,167 @@ |
||
| 48 | 48 | */ |
| 49 | 49 | class App { |
| 50 | 50 | |
| 51 | - /** @var string[] */ |
|
| 52 | - private static $nameSpaceCache = []; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Turns an app id into a namespace by either reading the appinfo.xml's |
|
| 56 | - * namespace tag or uppercasing the appid's first letter |
|
| 57 | - * @param string $appId the app id |
|
| 58 | - * @param string $topNamespace the namespace which should be prepended to |
|
| 59 | - * the transformed app id, defaults to OCA\ |
|
| 60 | - * @return string the starting namespace for the app |
|
| 61 | - */ |
|
| 62 | - public static function buildAppNamespace(string $appId, string $topNamespace='OCA\\'): string { |
|
| 63 | - // Hit the cache! |
|
| 64 | - if (isset(self::$nameSpaceCache[$appId])) { |
|
| 65 | - return $topNamespace . self::$nameSpaceCache[$appId]; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $appInfo = \OC_App::getAppInfo($appId); |
|
| 69 | - if (isset($appInfo['namespace'])) { |
|
| 70 | - self::$nameSpaceCache[$appId] = trim($appInfo['namespace']); |
|
| 71 | - } else { |
|
| 72 | - if ($appId !== 'spreed') { |
|
| 73 | - // if the tag is not found, fall back to uppercasing the first letter |
|
| 74 | - self::$nameSpaceCache[$appId] = ucfirst($appId); |
|
| 75 | - } else { |
|
| 76 | - // For the Talk app (appid spreed) the above fallback doesn't work. |
|
| 77 | - // This leads to a problem when trying to install it freshly, |
|
| 78 | - // because the apps namespace is already registered before the |
|
| 79 | - // app is downloaded from the appstore, because of the hackish |
|
| 80 | - // global route index.php/call/{token} which is registered via |
|
| 81 | - // the core/routes.php so it does not have the app namespace. |
|
| 82 | - // @ref https://github.com/nextcloud/server/pull/19433 |
|
| 83 | - self::$nameSpaceCache[$appId] = 'Talk'; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - return $topNamespace . self::$nameSpaceCache[$appId]; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Shortcut for calling a controller method and printing the result |
|
| 93 | - * @param string $controllerName the name of the controller under which it is |
|
| 94 | - * stored in the DI container |
|
| 95 | - * @param string $methodName the method that you want to call |
|
| 96 | - * @param DIContainer $container an instance of a pimple container. |
|
| 97 | - * @param array $urlParams list of URL parameters (optional) |
|
| 98 | - * @throws HintException |
|
| 99 | - */ |
|
| 100 | - public static function main(string $controllerName, string $methodName, DIContainer $container, array $urlParams = null) { |
|
| 101 | - if (!is_null($urlParams)) { |
|
| 102 | - $container->query(IRequest::class)->setUrlParameters($urlParams); |
|
| 103 | - } elseif (isset($container['urlParams']) && !is_null($container['urlParams'])) { |
|
| 104 | - $container->query(IRequest::class)->setUrlParameters($container['urlParams']); |
|
| 105 | - } |
|
| 106 | - $appName = $container['AppName']; |
|
| 107 | - |
|
| 108 | - // first try $controllerName then go for \OCA\AppName\Controller\$controllerName |
|
| 109 | - try { |
|
| 110 | - $controller = $container->query($controllerName); |
|
| 111 | - } catch (QueryException $e) { |
|
| 112 | - if (strpos($controllerName, '\\Controller\\') !== false) { |
|
| 113 | - // This is from a global registered app route that is not enabled. |
|
| 114 | - [/*OC(A)*/, $app, /* Controller/Name*/] = explode('\\', $controllerName, 3); |
|
| 115 | - throw new HintException('App ' . strtolower($app) . ' is not enabled'); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if ($appName === 'core') { |
|
| 119 | - $appNameSpace = 'OC\\Core'; |
|
| 120 | - } else { |
|
| 121 | - $appNameSpace = self::buildAppNamespace($appName); |
|
| 122 | - } |
|
| 123 | - $controllerName = $appNameSpace . '\\Controller\\' . $controllerName; |
|
| 124 | - $controller = $container->query($controllerName); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // initialize the dispatcher and run all the middleware before the controller |
|
| 128 | - /** @var Dispatcher $dispatcher */ |
|
| 129 | - $dispatcher = $container['Dispatcher']; |
|
| 130 | - |
|
| 131 | - list( |
|
| 132 | - $httpHeaders, |
|
| 133 | - $responseHeaders, |
|
| 134 | - $responseCookies, |
|
| 135 | - $output, |
|
| 136 | - $response |
|
| 137 | - ) = $dispatcher->dispatch($controller, $methodName); |
|
| 138 | - |
|
| 139 | - $io = $container[IOutput::class]; |
|
| 140 | - |
|
| 141 | - if (!is_null($httpHeaders)) { |
|
| 142 | - $io->setHeader($httpHeaders); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - foreach ($responseHeaders as $name => $value) { |
|
| 146 | - $io->setHeader($name . ': ' . $value); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - foreach ($responseCookies as $name => $value) { |
|
| 150 | - $expireDate = null; |
|
| 151 | - if ($value['expireDate'] instanceof \DateTime) { |
|
| 152 | - $expireDate = $value['expireDate']->getTimestamp(); |
|
| 153 | - } |
|
| 154 | - $sameSite = $value['sameSite'] ?? 'Lax'; |
|
| 155 | - |
|
| 156 | - $io->setCookie( |
|
| 157 | - $name, |
|
| 158 | - $value['value'], |
|
| 159 | - $expireDate, |
|
| 160 | - $container->getServer()->getWebRoot(), |
|
| 161 | - null, |
|
| 162 | - $container->getServer()->getRequest()->getServerProtocol() === 'https', |
|
| 163 | - true, |
|
| 164 | - $sameSite |
|
| 165 | - ); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /* |
|
| 51 | + /** @var string[] */ |
|
| 52 | + private static $nameSpaceCache = []; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Turns an app id into a namespace by either reading the appinfo.xml's |
|
| 56 | + * namespace tag or uppercasing the appid's first letter |
|
| 57 | + * @param string $appId the app id |
|
| 58 | + * @param string $topNamespace the namespace which should be prepended to |
|
| 59 | + * the transformed app id, defaults to OCA\ |
|
| 60 | + * @return string the starting namespace for the app |
|
| 61 | + */ |
|
| 62 | + public static function buildAppNamespace(string $appId, string $topNamespace='OCA\\'): string { |
|
| 63 | + // Hit the cache! |
|
| 64 | + if (isset(self::$nameSpaceCache[$appId])) { |
|
| 65 | + return $topNamespace . self::$nameSpaceCache[$appId]; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $appInfo = \OC_App::getAppInfo($appId); |
|
| 69 | + if (isset($appInfo['namespace'])) { |
|
| 70 | + self::$nameSpaceCache[$appId] = trim($appInfo['namespace']); |
|
| 71 | + } else { |
|
| 72 | + if ($appId !== 'spreed') { |
|
| 73 | + // if the tag is not found, fall back to uppercasing the first letter |
|
| 74 | + self::$nameSpaceCache[$appId] = ucfirst($appId); |
|
| 75 | + } else { |
|
| 76 | + // For the Talk app (appid spreed) the above fallback doesn't work. |
|
| 77 | + // This leads to a problem when trying to install it freshly, |
|
| 78 | + // because the apps namespace is already registered before the |
|
| 79 | + // app is downloaded from the appstore, because of the hackish |
|
| 80 | + // global route index.php/call/{token} which is registered via |
|
| 81 | + // the core/routes.php so it does not have the app namespace. |
|
| 82 | + // @ref https://github.com/nextcloud/server/pull/19433 |
|
| 83 | + self::$nameSpaceCache[$appId] = 'Talk'; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + return $topNamespace . self::$nameSpaceCache[$appId]; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Shortcut for calling a controller method and printing the result |
|
| 93 | + * @param string $controllerName the name of the controller under which it is |
|
| 94 | + * stored in the DI container |
|
| 95 | + * @param string $methodName the method that you want to call |
|
| 96 | + * @param DIContainer $container an instance of a pimple container. |
|
| 97 | + * @param array $urlParams list of URL parameters (optional) |
|
| 98 | + * @throws HintException |
|
| 99 | + */ |
|
| 100 | + public static function main(string $controllerName, string $methodName, DIContainer $container, array $urlParams = null) { |
|
| 101 | + if (!is_null($urlParams)) { |
|
| 102 | + $container->query(IRequest::class)->setUrlParameters($urlParams); |
|
| 103 | + } elseif (isset($container['urlParams']) && !is_null($container['urlParams'])) { |
|
| 104 | + $container->query(IRequest::class)->setUrlParameters($container['urlParams']); |
|
| 105 | + } |
|
| 106 | + $appName = $container['AppName']; |
|
| 107 | + |
|
| 108 | + // first try $controllerName then go for \OCA\AppName\Controller\$controllerName |
|
| 109 | + try { |
|
| 110 | + $controller = $container->query($controllerName); |
|
| 111 | + } catch (QueryException $e) { |
|
| 112 | + if (strpos($controllerName, '\\Controller\\') !== false) { |
|
| 113 | + // This is from a global registered app route that is not enabled. |
|
| 114 | + [/*OC(A)*/, $app, /* Controller/Name*/] = explode('\\', $controllerName, 3); |
|
| 115 | + throw new HintException('App ' . strtolower($app) . ' is not enabled'); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if ($appName === 'core') { |
|
| 119 | + $appNameSpace = 'OC\\Core'; |
|
| 120 | + } else { |
|
| 121 | + $appNameSpace = self::buildAppNamespace($appName); |
|
| 122 | + } |
|
| 123 | + $controllerName = $appNameSpace . '\\Controller\\' . $controllerName; |
|
| 124 | + $controller = $container->query($controllerName); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // initialize the dispatcher and run all the middleware before the controller |
|
| 128 | + /** @var Dispatcher $dispatcher */ |
|
| 129 | + $dispatcher = $container['Dispatcher']; |
|
| 130 | + |
|
| 131 | + list( |
|
| 132 | + $httpHeaders, |
|
| 133 | + $responseHeaders, |
|
| 134 | + $responseCookies, |
|
| 135 | + $output, |
|
| 136 | + $response |
|
| 137 | + ) = $dispatcher->dispatch($controller, $methodName); |
|
| 138 | + |
|
| 139 | + $io = $container[IOutput::class]; |
|
| 140 | + |
|
| 141 | + if (!is_null($httpHeaders)) { |
|
| 142 | + $io->setHeader($httpHeaders); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + foreach ($responseHeaders as $name => $value) { |
|
| 146 | + $io->setHeader($name . ': ' . $value); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + foreach ($responseCookies as $name => $value) { |
|
| 150 | + $expireDate = null; |
|
| 151 | + if ($value['expireDate'] instanceof \DateTime) { |
|
| 152 | + $expireDate = $value['expireDate']->getTimestamp(); |
|
| 153 | + } |
|
| 154 | + $sameSite = $value['sameSite'] ?? 'Lax'; |
|
| 155 | + |
|
| 156 | + $io->setCookie( |
|
| 157 | + $name, |
|
| 158 | + $value['value'], |
|
| 159 | + $expireDate, |
|
| 160 | + $container->getServer()->getWebRoot(), |
|
| 161 | + null, |
|
| 162 | + $container->getServer()->getRequest()->getServerProtocol() === 'https', |
|
| 163 | + true, |
|
| 164 | + $sameSite |
|
| 165 | + ); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /* |
|
| 169 | 169 | * Status 204 does not have a body and no Content Length |
| 170 | 170 | * Status 304 does not have a body and does not need a Content Length |
| 171 | 171 | * https://tools.ietf.org/html/rfc7230#section-3.3 |
| 172 | 172 | * https://tools.ietf.org/html/rfc7230#section-3.3.2 |
| 173 | 173 | */ |
| 174 | - $emptyResponse = false; |
|
| 175 | - if (preg_match('/^HTTP\/\d\.\d (\d{3}) .*$/', $httpHeaders, $matches)) { |
|
| 176 | - $status = (int)$matches[1]; |
|
| 177 | - if ($status === Http::STATUS_NO_CONTENT || $status === Http::STATUS_NOT_MODIFIED) { |
|
| 178 | - $emptyResponse = true; |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if (!$emptyResponse) { |
|
| 183 | - if ($response instanceof ICallbackResponse) { |
|
| 184 | - $response->callback($io); |
|
| 185 | - } elseif (!is_null($output)) { |
|
| 186 | - $io->setHeader('Content-Length: ' . strlen($output)); |
|
| 187 | - $io->setOutput($output); |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Shortcut for calling a controller method and printing the result. |
|
| 194 | - * Similar to App:main except that no headers will be sent. |
|
| 195 | - * This should be used for example when registering sections via |
|
| 196 | - * \OC\AppFramework\Core\API::registerAdmin() |
|
| 197 | - * |
|
| 198 | - * @param string $controllerName the name of the controller under which it is |
|
| 199 | - * stored in the DI container |
|
| 200 | - * @param string $methodName the method that you want to call |
|
| 201 | - * @param array $urlParams an array with variables extracted from the routes |
|
| 202 | - * @param DIContainer $container an instance of a pimple container. |
|
| 203 | - */ |
|
| 204 | - public static function part(string $controllerName, string $methodName, array $urlParams, |
|
| 205 | - DIContainer $container) { |
|
| 206 | - $container['urlParams'] = $urlParams; |
|
| 207 | - $controller = $container[$controllerName]; |
|
| 208 | - |
|
| 209 | - $dispatcher = $container['Dispatcher']; |
|
| 210 | - |
|
| 211 | - list(, , $output) = $dispatcher->dispatch($controller, $methodName); |
|
| 212 | - return $output; |
|
| 213 | - } |
|
| 174 | + $emptyResponse = false; |
|
| 175 | + if (preg_match('/^HTTP\/\d\.\d (\d{3}) .*$/', $httpHeaders, $matches)) { |
|
| 176 | + $status = (int)$matches[1]; |
|
| 177 | + if ($status === Http::STATUS_NO_CONTENT || $status === Http::STATUS_NOT_MODIFIED) { |
|
| 178 | + $emptyResponse = true; |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if (!$emptyResponse) { |
|
| 183 | + if ($response instanceof ICallbackResponse) { |
|
| 184 | + $response->callback($io); |
|
| 185 | + } elseif (!is_null($output)) { |
|
| 186 | + $io->setHeader('Content-Length: ' . strlen($output)); |
|
| 187 | + $io->setOutput($output); |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Shortcut for calling a controller method and printing the result. |
|
| 194 | + * Similar to App:main except that no headers will be sent. |
|
| 195 | + * This should be used for example when registering sections via |
|
| 196 | + * \OC\AppFramework\Core\API::registerAdmin() |
|
| 197 | + * |
|
| 198 | + * @param string $controllerName the name of the controller under which it is |
|
| 199 | + * stored in the DI container |
|
| 200 | + * @param string $methodName the method that you want to call |
|
| 201 | + * @param array $urlParams an array with variables extracted from the routes |
|
| 202 | + * @param DIContainer $container an instance of a pimple container. |
|
| 203 | + */ |
|
| 204 | + public static function part(string $controllerName, string $methodName, array $urlParams, |
|
| 205 | + DIContainer $container) { |
|
| 206 | + $container['urlParams'] = $urlParams; |
|
| 207 | + $controller = $container[$controllerName]; |
|
| 208 | + |
|
| 209 | + $dispatcher = $container['Dispatcher']; |
|
| 210 | + |
|
| 211 | + list(, , $output) = $dispatcher->dispatch($controller, $methodName); |
|
| 212 | + return $output; |
|
| 213 | + } |
|
| 214 | 214 | } |