servererroraction.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Server error action.
  18. *
  19. * @category Action
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Zach Copley <zach@status.net>
  23. * @copyright 2008, 2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Class for displaying HTTP server errors
  29. *
  30. * Note: The older util.php class simply printed a string, but the spec
  31. * says that 500 errors should be treated similarly to 400 errors, and
  32. * it's easier to give an HTML response. Maybe we can customize these
  33. * to display some funny animal cartoons. If not, we can probably role
  34. * these classes up into a single class.
  35. *
  36. * See: http://tools.ietf.org/html/rfc2616#section-10
  37. *
  38. * @category Action
  39. * @package GNUsocial
  40. * @author Zach Copley <zach@status.net>
  41. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  42. */
  43. class ServerErrorAction extends ErrorAction
  44. {
  45. public static $status = [
  46. 500 => 'Internal Server Error',
  47. 501 => 'Not Implemented',
  48. 502 => 'Bad Gateway',
  49. 503 => 'Service Unavailable',
  50. 504 => 'Gateway Timeout',
  51. 505 => 'HTTP Version Not Supported'
  52. ];
  53. public function __construct($message = 'Error', $code = 500, $ex = null)
  54. {
  55. parent::__construct($message, $code);
  56. $this->default = 500;
  57. if (!$this->code || $this->code < 500 || $this->code > 599) {
  58. $this->code = $this->default;
  59. }
  60. if (!$this->message) {
  61. $this->message = "Server Error $this->code";
  62. }
  63. // Server errors must be logged.
  64. $log = "ServerErrorAction: $code $message";
  65. if ($ex) {
  66. $log .= "\n" . $ex->getTraceAsString();
  67. }
  68. common_log(LOG_ERR, $log);
  69. $this->showPage();
  70. }
  71. /**
  72. * To specify additional HTTP headers for the action
  73. *
  74. * @return void
  75. */
  76. public function extraHeaders()
  77. {
  78. http_response_code($this->code);
  79. }
  80. /**
  81. * Page title.
  82. *
  83. * @return string page title
  84. */
  85. public function title()
  86. {
  87. return self::$status[$this->code];
  88. }
  89. }