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.

83 lines
2.0 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage app
  7. * @since 2010-02-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class ErrorAction extends Action
  12. {
  13. /**
  14. * @var ErrorException
  15. */
  16. public $exception;
  17. public function __construct($exception)
  18. {
  19. $this->exception = $exception;
  20. parent::__construct();
  21. }
  22. protected function execute()
  23. {
  24. $this->template = 500;
  25. if ($this->exception instanceof Error404Exception) {
  26. $this->template = 404;
  27. }
  28. $this->logError();
  29. $this->sendHTTPCode();
  30. }
  31. protected function getTemplate()
  32. {
  33. return '/actions/' . $this->template;
  34. }
  35. protected function sendHttpCode()
  36. {
  37. if (headers_sent()) {
  38. return;
  39. }
  40. switch ($this->template) {
  41. case 404:
  42. header('HTTP/1.0 404 Not Found');
  43. break;
  44. default:
  45. header('HTTP/1.0 500 Internal Server Error');
  46. }
  47. }
  48. protected function logError()
  49. {
  50. if ($this->template == 500) {
  51. $error = 0;
  52. $ex = $this->exception;
  53. if ($ex instanceof ErrorException) {
  54. $error = $ex->getSeverity();
  55. }
  56. switch ($error) {
  57. case E_NOTICE:
  58. $error = 'Notice';
  59. break;
  60. case E_WARNING:
  61. $error = 'Warning';
  62. break;
  63. case E_ERROR:
  64. $error = 'Fatal Error';
  65. break;
  66. default:
  67. $error = 'Unknown Error';
  68. break;
  69. }
  70. $message = 'PHP ' . $error . ': ' . $ex->getMessage() . ' in ' . $ex->getFile()
  71. . ' on line ' . $ex->getLine();
  72. error_log($message);
  73. }
  74. }
  75. }