index.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Sergio Bertolín <sbertolin@solidgear.es>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <vincent@nextcloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. require_once __DIR__ . '/lib/versioncheck.php';
  32. use OCP\Security\Bruteforce\MaxDelayReached;
  33. use Psr\Log\LoggerInterface;
  34. try {
  35. require_once __DIR__ . '/lib/base.php';
  36. OC::handleRequest();
  37. } catch (\OC\ServiceUnavailableException $ex) {
  38. \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
  39. 'app' => 'index',
  40. 'exception' => $ex,
  41. ]);
  42. //show the user a detailed error page
  43. OC_Template::printExceptionErrorPage($ex, 503);
  44. } catch (\OCP\HintException $ex) {
  45. try {
  46. OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), 503);
  47. } catch (Exception $ex2) {
  48. try {
  49. \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
  50. 'app' => 'index',
  51. 'exception' => $ex,
  52. ]);
  53. \OC::$server->get(LoggerInterface::class)->error($ex2->getMessage(), [
  54. 'app' => 'index',
  55. 'exception' => $ex2,
  56. ]);
  57. } catch (Throwable $e) {
  58. // no way to log it properly - but to avoid a white page of death we try harder and ignore this one here
  59. }
  60. //show the user a detailed error page
  61. OC_Template::printExceptionErrorPage($ex, 500);
  62. }
  63. } catch (\OC\User\LoginException $ex) {
  64. $request = \OC::$server->getRequest();
  65. /**
  66. * Routes with the @CORS annotation and other API endpoints should
  67. * not return a webpage, so we only print the error page when html is accepted,
  68. * otherwise we reply with a JSON array like the SecurityMiddleware would do.
  69. */
  70. if (stripos($request->getHeader('Accept'), 'html') === false) {
  71. http_response_code(401);
  72. header('Content-Type: application/json; charset=utf-8');
  73. echo json_encode(['message' => $ex->getMessage()]);
  74. exit();
  75. }
  76. OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 401);
  77. } catch (MaxDelayReached $ex) {
  78. $request = \OC::$server->getRequest();
  79. /**
  80. * Routes with the @CORS annotation and other API endpoints should
  81. * not return a webpage, so we only print the error page when html is accepted,
  82. * otherwise we reply with a JSON array like the BruteForceMiddleware would do.
  83. */
  84. if (stripos($request->getHeader('Accept'), 'html') === false) {
  85. http_response_code(429);
  86. header('Content-Type: application/json; charset=utf-8');
  87. echo json_encode(['message' => $ex->getMessage()]);
  88. exit();
  89. }
  90. http_response_code(429);
  91. OC_Template::printGuestPage('core', '429');
  92. } catch (Exception $ex) {
  93. \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
  94. 'app' => 'index',
  95. 'exception' => $ex,
  96. ]);
  97. //show the user a detailed error page
  98. OC_Template::printExceptionErrorPage($ex, 500);
  99. } catch (Error $ex) {
  100. try {
  101. \OC::$server->get(LoggerInterface::class)->error($ex->getMessage(), [
  102. 'app' => 'index',
  103. 'exception' => $ex,
  104. ]);
  105. } catch (Error $e) {
  106. http_response_code(500);
  107. header('Content-Type: text/plain; charset=utf-8');
  108. print("Internal Server Error\n\n");
  109. print("The server encountered an internal error and was unable to complete your request.\n");
  110. print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
  111. print("More details can be found in the webserver log.\n");
  112. throw $ex;
  113. }
  114. OC_Template::printExceptionErrorPage($ex, 500);
  115. }