index.php 4.5 KB

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