blob: ced65ece4d951d4be70b0452ab5b91734bd69486 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev7ac33d72012-01-07 19:39:39 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7ac33d72012-01-07 19:39:39 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Exceptions Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Exceptions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/exceptions.html
37 */
38class CI_Exceptions {
Andrey Andreev7ac33d72012-01-07 19:39:39 +020039
David Behler209b2cf2011-08-14 23:00:43 +020040 /**
41 * Nesting level of the output buffering mechanism
David Behler209b2cf2011-08-14 23:00:43 +020042 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040043 * @var int
David Behler209b2cf2011-08-14 23:00:43 +020044 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020045 public $ob_level;
Derek Allard2067d1a2008-11-13 22:59:24 +000046
David Behler209b2cf2011-08-14 23:00:43 +020047 /**
48 * List if available error levels
49 *
Timothy Warren48a7fbb2012-04-23 11:58:16 -040050 * @var array
David Behler209b2cf2011-08-14 23:00:43 +020051 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020052 public $levels = array(
Timothy Warren40403d22012-04-19 16:38:50 -040053 E_ERROR => 'Error',
54 E_WARNING => 'Warning',
55 E_PARSE => 'Parsing Error',
56 E_NOTICE => 'Notice',
57 E_CORE_ERROR => 'Core Error',
58 E_CORE_WARNING => 'Core Warning',
59 E_COMPILE_ERROR => 'Compile Error',
60 E_COMPILE_WARNING => 'Compile Warning',
61 E_USER_ERROR => 'User Error',
62 E_USER_WARNING => 'User Warning',
63 E_USER_NOTICE => 'User Notice',
64 E_STRICT => 'Runtime Notice'
65 );
Derek Allard2067d1a2008-11-13 22:59:24 +000066
Timothy Warrenad475052012-04-19 13:21:06 -040067 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030068 * Class constructor
Andrey Andreev92ebfb62012-05-17 12:49:24 +030069 *
70 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -040071 */
Greg Akera9263282010-11-10 15:26:43 -060072 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
74 $this->ob_level = ob_get_level();
Andrey Andreev7ac33d72012-01-07 19:39:39 +020075 // Note: Do not log messages from this constructor.
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 // --------------------------------------------------------------------
79
80 /**
81 * Exception Logger
82 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030083 * Logs PHP generated error messages
Derek Allard2067d1a2008-11-13 22:59:24 +000084 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030085 * @param int $severity Log level
86 * @param string $message Error message
87 * @param string $filepath File path
88 * @param int $line Line number
Andrey Andreev7ac33d72012-01-07 19:39:39 +020089 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000090 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +020091 public function log_exception($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +020092 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +030093 $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
Derek Jones37f4b9c2011-07-01 17:56:50 -050094 log_message('error', 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000095 }
96
97 // --------------------------------------------------------------------
98
99 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300100 * 404 Error Handler
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300102 * @uses CI_Exceptions::show_error()
103 *
104 * @param string $page Page URI
105 * @param bool $log_error Whether to log the error
106 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200108 public function show_404($page = '', $log_error = TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200109 {
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200110 $heading = '404 Page Not Found';
111 $message = 'The page you requested was not found.';
Derek Allard2067d1a2008-11-13 22:59:24 +0000112
Derek Allard2ddc9492010-08-05 10:08:33 -0400113 // By default we log this, but allow a dev to skip it
114 if ($log_error)
115 {
116 log_message('error', '404 Page Not Found --> '.$page);
117 }
118
Derek Jonesf6b442b2009-07-11 18:23:30 +0000119 echo $this->show_error($heading, $message, 'error_404', 404);
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 exit;
121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // --------------------------------------------------------------------
124
125 /**
126 * General Error Page
127 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300128 * Takes an error message as input (either as a string or an array)
129 * and displays it using the specified template.
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300131 * @param string $heading Page heading
132 * @param string|string[] $message Error message
133 * @param string $template Template name
Andrey Andreev5232ba02012-10-27 15:25:05 +0300134 * @param int $status_code (default: 500)
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300135 *
136 * @return string Error page output
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 */
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200138 public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Derek Jones817163a2009-07-11 17:05:58 +0000140 set_status_header($status_code);
Barry Mienydd671972010-10-04 16:33:58 +0200141
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300142 $message = '<p>'.implode('</p><p>', is_array($message) ? $message : array($message)).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000143
144 if (ob_get_level() > $this->ob_level + 1)
145 {
Barry Mienydd671972010-10-04 16:33:58 +0200146 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
148 ob_start();
Nicholas Avenellc59d25d2012-06-24 10:57:18 +0000149 include(VIEWPATH.'errors/'.$template.'.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 $buffer = ob_get_contents();
151 ob_end_clean();
152 return $buffer;
153 }
154
155 // --------------------------------------------------------------------
156
157 /**
158 * Native PHP error handler
159 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300160 * @param int $severity Error level
161 * @param string $message Error message
162 * @param string $filepath File path
163 * @param int $line Line number
164 * @return string Error page output
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300166 public function show_php_error($severity, $message, $filepath, $line)
Barry Mienydd671972010-10-04 16:33:58 +0200167 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300168 $severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
Andrey Andreev7ac33d72012-01-07 19:39:39 +0200169 $filepath = str_replace('\\', '/', $filepath);
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // For safety reasons we do not show the full file path
172 if (FALSE !== strpos($filepath, '/'))
173 {
174 $x = explode('/', $filepath);
175 $filepath = $x[count($x)-2].'/'.end($x);
176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 if (ob_get_level() > $this->ob_level + 1)
179 {
Barry Mienydd671972010-10-04 16:33:58 +0200180 ob_end_flush();
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
182 ob_start();
Nicholas Avenellc59d25d2012-06-24 10:57:18 +0000183 include(VIEWPATH.'errors/error_php.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 $buffer = ob_get_contents();
185 ob_end_clean();
186 echo $buffer;
187 }
188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189}
Derek Allard2067d1a2008-11-13 22:59:24 +0000190
191/* End of file Exceptions.php */
Andrey Andreevc6a68e02012-03-26 14:30:10 +0300192/* Location: ./system/core/Exceptions.php */