/system/core/Exceptions.php

https://github.com/dchill42/CodeIgniter · PHP · 192 lines · 72 code · 20 blank · 100 comment · 4 complexity · efe883dd592c730904fd1ae2c3d60f83 MD5 · raw file

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