You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.6 KiB

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