index.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. require_once __DIR__ . '/lib/versioncheck.php';
  9. use OC\ServiceUnavailableException;
  10. use OC\User\LoginException;
  11. use OCP\HintException;
  12. use OCP\IRequest;
  13. use OCP\Security\Bruteforce\MaxDelayReached;
  14. use OCP\Server;
  15. use Psr\Log\LoggerInterface;
  16. try {
  17. require_once __DIR__ . '/lib/base.php';
  18. OC::handleRequest();
  19. } catch (ServiceUnavailableException $ex) {
  20. Server::get(LoggerInterface::class)->error($ex->getMessage(), [
  21. 'app' => 'index',
  22. 'exception' => $ex,
  23. ]);
  24. //show the user a detailed error page
  25. OC_Template::printExceptionErrorPage($ex, 503);
  26. } catch (HintException $ex) {
  27. try {
  28. OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), 503);
  29. } catch (Exception $ex2) {
  30. try {
  31. Server::get(LoggerInterface::class)->error($ex->getMessage(), [
  32. 'app' => 'index',
  33. 'exception' => $ex,
  34. ]);
  35. Server::get(LoggerInterface::class)->error($ex2->getMessage(), [
  36. 'app' => 'index',
  37. 'exception' => $ex2,
  38. ]);
  39. } catch (Throwable $e) {
  40. // no way to log it properly - but to avoid a white page of death we try harder and ignore this one here
  41. }
  42. //show the user a detailed error page
  43. OC_Template::printExceptionErrorPage($ex, 500);
  44. }
  45. } catch (LoginException $ex) {
  46. $request = Server::get(IRequest::class);
  47. /**
  48. * Routes with the @CORS annotation and other API endpoints should
  49. * not return a webpage, so we only print the error page when html is accepted,
  50. * otherwise we reply with a JSON array like the SecurityMiddleware would do.
  51. */
  52. if (stripos($request->getHeader('Accept'), 'html') === false) {
  53. http_response_code(401);
  54. header('Content-Type: application/json; charset=utf-8');
  55. echo json_encode(['message' => $ex->getMessage()]);
  56. exit();
  57. }
  58. OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 401);
  59. } catch (MaxDelayReached $ex) {
  60. $request = Server::get(IRequest::class);
  61. /**
  62. * Routes with the @CORS annotation and other API endpoints should
  63. * not return a webpage, so we only print the error page when html is accepted,
  64. * otherwise we reply with a JSON array like the BruteForceMiddleware would do.
  65. */
  66. if (stripos($request->getHeader('Accept'), 'html') === false) {
  67. http_response_code(429);
  68. header('Content-Type: application/json; charset=utf-8');
  69. echo json_encode(['message' => $ex->getMessage()]);
  70. exit();
  71. }
  72. http_response_code(429);
  73. OC_Template::printGuestPage('core', '429');
  74. } catch (Exception $ex) {
  75. Server::get(LoggerInterface::class)->error($ex->getMessage(), [
  76. 'app' => 'index',
  77. 'exception' => $ex,
  78. ]);
  79. //show the user a detailed error page
  80. OC_Template::printExceptionErrorPage($ex, 500);
  81. } catch (Error $ex) {
  82. try {
  83. Server::get(LoggerInterface::class)->error($ex->getMessage(), [
  84. 'app' => 'index',
  85. 'exception' => $ex,
  86. ]);
  87. } catch (Error $e) {
  88. http_response_code(500);
  89. header('Content-Type: text/plain; charset=utf-8');
  90. print("Internal Server Error\n\n");
  91. print("The server encountered an internal error and was unable to complete your request.\n");
  92. print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
  93. print("More details can be found in the webserver log.\n");
  94. throw $ex;
  95. }
  96. OC_Template::printExceptionErrorPage($ex, 500);
  97. }