BrowserErrorPagePlugin.php 2.8 KB

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