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.

104 lines
2.5 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  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. if ($this->isAjaxActionError()) {
  27. if (!headers_sent()) {
  28. header('HTTP/1.0 404 Not Found');
  29. die();
  30. }
  31. }
  32. $this->template = 404;
  33. }
  34. $this->logError();
  35. $this->sendHTTPCode();
  36. }
  37. protected function getTemplate()
  38. {
  39. return '/actions/' . $this->template;
  40. }
  41. protected function sendHttpCode()
  42. {
  43. if (headers_sent()) {
  44. return;
  45. }
  46. switch ($this->template) {
  47. case 404:
  48. header('HTTP/1.0 404 Not Found');
  49. break;
  50. default:
  51. header('HTTP/1.0 500 Internal Server Error');
  52. }
  53. }
  54. protected function logError()
  55. {
  56. if ($this->template == 500) {
  57. $error = 0;
  58. $ex = $this->exception;
  59. if ($ex instanceof ErrorException) {
  60. $error = $ex->getSeverity();
  61. }
  62. switch ($error) {
  63. case E_NOTICE:
  64. $error = 'Notice';
  65. break;
  66. case E_WARNING:
  67. $error = 'Warning';
  68. break;
  69. case E_ERROR:
  70. $error = 'Fatal Error';
  71. break;
  72. default:
  73. $error = 'Unknown Error';
  74. break;
  75. }
  76. $message = 'PHP ' . $error . ': ' . $ex->getMessage() . ' in ' . $ex->getFile()
  77. . ' on line ' . $ex->getLine();
  78. error_log($message);
  79. }
  80. }
  81. /**
  82. * Check, if exception was thrown from AjaxAction Class
  83. * @return bool
  84. */
  85. protected function isAjaxActionError()
  86. {
  87. $trace = $this->exception->getTrace();
  88. foreach ($trace as $line) {
  89. if (isset($line['class']) && $line['class'] === 'AjaxAction') {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. }