1
0

ErrorPagePlugin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Files;
  8. use OC_Template;
  9. use OCP\AppFramework\Http\ContentSecurityPolicy;
  10. use OCP\IConfig;
  11. use OCP\IRequest;
  12. use Sabre\DAV\Exception;
  13. use Sabre\DAV\Server;
  14. use Sabre\DAV\ServerPlugin;
  15. class ErrorPagePlugin extends ServerPlugin {
  16. private ?Server $server = null;
  17. public function __construct(
  18. private IRequest $request,
  19. private IConfig $config,
  20. ) {
  21. }
  22. /**
  23. * This initializes the plugin.
  24. *
  25. * This function is called by Sabre\DAV\Server, after
  26. * addPlugin is called.
  27. *
  28. * This method should set up the required event subscriptions.
  29. */
  30. public function initialize(Server $server): void {
  31. $this->server = $server;
  32. $server->on('exception', [$this, 'logException'], 1000);
  33. }
  34. public function logException(\Throwable $ex): void {
  35. if ($ex instanceof Exception) {
  36. $httpCode = $ex->getHTTPCode();
  37. $headers = $ex->getHTTPHeaders($this->server);
  38. } else {
  39. $httpCode = 500;
  40. $headers = [];
  41. }
  42. $this->server->httpResponse->addHeaders($headers);
  43. $this->server->httpResponse->setStatus($httpCode);
  44. $body = $this->generateBody($ex, $httpCode);
  45. $this->server->httpResponse->setBody($body);
  46. $csp = new ContentSecurityPolicy();
  47. $this->server->httpResponse->addHeader('Content-Security-Policy', $csp->buildPolicy());
  48. $this->sendResponse();
  49. }
  50. /**
  51. * @codeCoverageIgnore
  52. * @return bool|string
  53. */
  54. public function generateBody(\Throwable $ex, int $httpCode): mixed {
  55. if ($this->acceptHtml()) {
  56. $templateName = 'exception';
  57. $renderAs = 'guest';
  58. if ($httpCode === 403 || $httpCode === 404) {
  59. $templateName = (string)$httpCode;
  60. }
  61. } else {
  62. $templateName = 'xml_exception';
  63. $renderAs = null;
  64. $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
  65. }
  66. $debug = $this->config->getSystemValueBool('debug', false);
  67. $content = new OC_Template('core', $templateName, $renderAs);
  68. $content->assign('title', $this->server->httpResponse->getStatusText());
  69. $content->assign('remoteAddr', $this->request->getRemoteAddress());
  70. $content->assign('requestID', $this->request->getId());
  71. $content->assign('debugMode', $debug);
  72. $content->assign('errorClass', get_class($ex));
  73. $content->assign('errorMsg', $ex->getMessage());
  74. $content->assign('errorCode', $ex->getCode());
  75. $content->assign('file', $ex->getFile());
  76. $content->assign('line', $ex->getLine());
  77. $content->assign('exception', $ex);
  78. return $content->fetchPage();
  79. }
  80. /**
  81. * @codeCoverageIgnore
  82. */
  83. public function sendResponse() {
  84. $this->server->sapi->sendResponse($this->server->httpResponse);
  85. }
  86. private function acceptHtml(): bool {
  87. foreach (explode(',', $this->request->getHeader('Accept')) as $part) {
  88. $subparts = explode(';', $part);
  89. if (str_ends_with($subparts[0], '/html')) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. }