servererroraction.php 3.1 KB

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