Exceptions.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * Exceptions Class
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Libraries
  22. * @category Exceptions
  23. * @author EllisLab Dev Team
  24. * @link http://codeigniter.com/user_guide/libraries/exceptions.html
  25. */
  26. class CI_Exceptions {
  27. var $action;
  28. var $severity;
  29. var $message;
  30. var $filename;
  31. var $line;
  32. /**
  33. * Nesting level of the output buffering mechanism
  34. *
  35. * @var int
  36. * @access public
  37. */
  38. var $ob_level;
  39. /**
  40. * List if available error levels
  41. *
  42. * @var array
  43. * @access public
  44. */
  45. var $levels = array(
  46. E_ERROR => 'Error',
  47. E_WARNING => 'Warning',
  48. E_PARSE => 'Parsing Error',
  49. E_NOTICE => 'Notice',
  50. E_CORE_ERROR => 'Core Error',
  51. E_CORE_WARNING => 'Core Warning',
  52. E_COMPILE_ERROR => 'Compile Error',
  53. E_COMPILE_WARNING => 'Compile Warning',
  54. E_USER_ERROR => 'User Error',
  55. E_USER_WARNING => 'User Warning',
  56. E_USER_NOTICE => 'User Notice',
  57. E_STRICT => 'Runtime Notice'
  58. );
  59. /**
  60. * Constructor
  61. */
  62. public function __construct()
  63. {
  64. $this->ob_level = ob_get_level();
  65. // Note: Do not log messages from this constructor.
  66. }
  67. // --------------------------------------------------------------------
  68. /**
  69. * Exception Logger
  70. *
  71. * This function logs PHP generated error messages
  72. *
  73. * @access private
  74. * @param string the error severity
  75. * @param string the error string
  76. * @param string the error filepath
  77. * @param string the error line number
  78. * @return string
  79. */
  80. function log_exception($severity, $message, $filepath, $line)
  81. {
  82. $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
  83. log_message('error', 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line, TRUE);
  84. }
  85. // --------------------------------------------------------------------
  86. /**
  87. * 404 Page Not Found Handler
  88. *
  89. * @access private
  90. * @param string the page
  91. * @param bool log error yes/no
  92. * @return string
  93. */
  94. function show_404($page = '', $log_error = TRUE)
  95. {
  96. $heading = "404 Page Not Found";
  97. $message = "The page you requested was not found.";
  98. // By default we log this, but allow a dev to skip it
  99. if ($log_error)
  100. {
  101. log_message('error', '404 Page Not Found --> '.$page);
  102. }
  103. echo $this->show_error($heading, $message, 'error_404', 404);
  104. exit;
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * General Error Page
  109. *
  110. * This function takes an error message as input
  111. * (either as a string or an array) and displays
  112. * it using the specified template.
  113. *
  114. * @access private
  115. * @param string the heading
  116. * @param string the message
  117. * @param string the template name
  118. * @param int the status code
  119. * @return string
  120. */
  121. function show_error($heading, $message, $template = 'error_general', $status_code = 500)
  122. {
  123. set_status_header($status_code);
  124. $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
  125. if (ob_get_level() > $this->ob_level + 1)
  126. {
  127. ob_end_flush();
  128. }
  129. ob_start();
  130. include(APPPATH.'errors/'.$template.'.php');
  131. $buffer = ob_get_contents();
  132. ob_end_clean();
  133. return $buffer;
  134. }
  135. // --------------------------------------------------------------------
  136. /**
  137. * Native PHP error handler
  138. *
  139. * @access private
  140. * @param string the error severity
  141. * @param string the error string
  142. * @param string the error filepath
  143. * @param string the error line number
  144. * @return string
  145. */
  146. function show_php_error($severity, $message, $filepath, $line)
  147. {
  148. $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
  149. $filepath = str_replace("\\", "/", $filepath);
  150. // For safety reasons we do not show the full file path
  151. if (FALSE !== strpos($filepath, '/'))
  152. {
  153. $x = explode('/', $filepath);
  154. $filepath = $x[count($x)-2].'/'.end($x);
  155. }
  156. if (ob_get_level() > $this->ob_level + 1)
  157. {
  158. ob_end_flush();
  159. }
  160. ob_start();
  161. include(APPPATH.'errors/error_php.php');
  162. $buffer = ob_get_contents();
  163. ob_end_clean();
  164. echo $buffer;
  165. }
  166. }
  167. // END Exceptions Class
  168. /* End of file Exceptions.php */
  169. /* Location: ./system/core/Exceptions.php */