servererroraction.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. require_once INSTALLDIR.'/lib/error.php';
  34. /**
  35. * Class for displaying HTTP server errors
  36. *
  37. * Note: The older util.php class simply printed a string, but the spec
  38. * says that 500 errors should be treated similarly to 400 errors, and
  39. * it's easier to give an HTML response. Maybe we can customize these
  40. * to display some funny animal cartoons. If not, we can probably role
  41. * these classes up into a single class.
  42. *
  43. * See: http://tools.ietf.org/html/rfc2616#section-10
  44. *
  45. * @category Action
  46. * @package StatusNet
  47. * @author Zach Copley <zach@status.net>
  48. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  49. * @link http://status.net/
  50. */
  51. class ServerErrorAction extends ErrorAction
  52. {
  53. static $status = array(500 => 'Internal Server Error',
  54. 501 => 'Not Implemented',
  55. 502 => 'Bad Gateway',
  56. 503 => 'Service Unavailable',
  57. 504 => 'Gateway Timeout',
  58. 505 => 'HTTP Version Not Supported');
  59. function __construct($message='Error', $code=500, $ex=null)
  60. {
  61. parent::__construct($message, $code);
  62. $this->default = 500;
  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. }
  70. // XXX: Should these error actions even be invokable via URI?
  71. function handle($args)
  72. {
  73. parent::handle($args);
  74. $this->code = $this->trimmed('code');
  75. if (!$this->code || $code < 500 || $code > 599) {
  76. $this->code = $this->default;
  77. }
  78. $this->message = $this->trimmed('message');
  79. if (!$this->message) {
  80. $this->message = "Server Error $this->code";
  81. }
  82. $this->showPage();
  83. }
  84. /**
  85. * To specify additional HTTP headers for the action
  86. *
  87. * @return void
  88. */
  89. function extraHeaders()
  90. {
  91. $status_string = @self::$status[$this->code];
  92. header('HTTP/1.1 '.$this->code.' '.$status_string);
  93. }
  94. /**
  95. * Page title.
  96. *
  97. * @return page title
  98. */
  99. function title()
  100. {
  101. return @self::$status[$this->code];
  102. }
  103. }