Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

BrowserErrorPagePlugin.php 3.3KB

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