BrowserErrorPagePlugin.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Files;
  26. use OC\AppFramework\Http\Request;
  27. use OC_Template;
  28. use OCP\AppFramework\Http\ContentSecurityPolicy;
  29. use OCP\IRequest;
  30. use Sabre\DAV\Exception;
  31. use Sabre\DAV\Server;
  32. use Sabre\DAV\ServerPlugin;
  33. class BrowserErrorPagePlugin extends ServerPlugin {
  34. /** @var Server */
  35. private $server;
  36. /**
  37. * This initializes the plugin.
  38. *
  39. * This function is called by Sabre\DAV\Server, after
  40. * addPlugin is called.
  41. *
  42. * This method should set up the required event subscriptions.
  43. *
  44. * @param Server $server
  45. * @return void
  46. */
  47. function initialize(Server $server) {
  48. $this->server = $server;
  49. $server->on('exception', [$this, 'logException'], 1000);
  50. }
  51. /**
  52. * @param IRequest $request
  53. * @return bool
  54. */
  55. public static function isBrowserRequest(IRequest $request) {
  56. if ($request->getMethod() !== 'GET') {
  57. return false;
  58. }
  59. return $request->isUserAgent([
  60. Request::USER_AGENT_IE,
  61. Request::USER_AGENT_MS_EDGE,
  62. Request::USER_AGENT_CHROME,
  63. Request::USER_AGENT_FIREFOX,
  64. Request::USER_AGENT_SAFARI,
  65. ]);
  66. }
  67. /**
  68. * @param \Exception $ex
  69. */
  70. public function logException(\Exception $ex) {
  71. if ($ex instanceof Exception) {
  72. $httpCode = $ex->getHTTPCode();
  73. $headers = $ex->getHTTPHeaders($this->server);
  74. } else {
  75. $httpCode = 500;
  76. $headers = [];
  77. }
  78. $this->server->httpResponse->addHeaders($headers);
  79. $this->server->httpResponse->setStatus($httpCode);
  80. $body = $this->generateBody($httpCode);
  81. $this->server->httpResponse->setBody($body);
  82. $csp = new ContentSecurityPolicy();
  83. $this->server->httpResponse->addHeader('Content-Security-Policy', $csp->buildPolicy());
  84. $this->sendResponse();
  85. }
  86. /**
  87. * @codeCoverageIgnore
  88. * @return bool|string
  89. */
  90. public function generateBody(int $httpCode) {
  91. $request = \OC::$server->getRequest();
  92. $templateName = 'exception';
  93. if($httpCode === 403 || $httpCode === 404) {
  94. $templateName = (string)$httpCode;
  95. }
  96. $content = new OC_Template('core', $templateName, 'guest');
  97. $content->assign('title', $this->server->httpResponse->getStatusText());
  98. $content->assign('remoteAddr', $request->getRemoteAddress());
  99. $content->assign('requestID', $request->getId());
  100. return $content->fetchPage();
  101. }
  102. /**
  103. * @codeCoverageIgnore
  104. */
  105. public function sendResponse() {
  106. $this->server->sapi->sendResponse($this->server->httpResponse);
  107. exit();
  108. }
  109. }