Smutty

MVC Framework

View Code: Smutty_Error

Browse: All · Classes · Plugins

1
<?php
2
3
define( 'ERR_ALL', 1 );
4
define( 'ERR_STANDARD', 2 );
5
6
/**
7
* this class handles all exceptions and warnings
8
* generated by smutty.
9
*
10
*/
11
12
class Smutty_Error extends Smutty_Object {
13
14
// warnings that have been generated
15
private $warnings;
16
17
// are templates enabled?
18
private $useTemplates;
19
20
// error types to show
21
private $errorTypes;
22
23
// singleton
24
private static $instance = null;
25
26
/**
27
* constructor
28
*
29
* @return nothing
30
*
31
*/
32
33
function __construct() {
34
$this->warnings = array();
35
$this->useTemplates = false;
36
$this->errorTypes = ERR_STANDARD;
37
}
38
39
/**
40
* enables the use of smarty templates for error
41
* reporting purposes
42
*
43
* @return nothing
44
*
45
*/
46
47
function enableTemplates() {
48
$this->useTemplates = true;
49
}
50
51
/**
52
* returns the singleton instance for this object. this singleton
53
* can then be used to set global error reporting options.
54
*
55
* @return Smutty_Error the singleton
56
*
57
*/
58
59
public static function &getInstance() {
60
if ( self::$instance == null )
61
self::$instance = new Smutty_Error();
62
return self::$instance;
63
}
64
65
/**
66
* returns a boolean indicating if templates will be
67
* used when reporting an error
68
*
69
* @return boolean are we using templates
70
*
71
*/
72
73
public function isUsingTemplates() {
74
return $this->useTemplates;
75
}
76
77
/**
78
* call this to raise a fatal error
79
*
80
* @param String $message the error message
81
* @param String $wikiHelp name of wiki help page
82
* @param int $code http status code
83
* @return nothing
84
*
85
*/
86
87
public static function fatal( $message, $wikiHelp, $code = 500 ) {
88
89
$cfg = Smutty_Config::getInstance();
90
91
// has the user defined their own handler?
92
if ( $handler = $cfg->get('errors.handler') ) {
93
94
$parts = split( '/', $handler );
95
$controller = $parts[ 0 ];
96
$method = $parts[ 1 ];
97
98
eval( ucfirst($controller) . 'Controller::'
99
. $method . "( \$message );" );
100
101
}
102
103
else {
104
105
$e =& Smutty_Error::getInstance();
106
if ( !headers_sent() )
107
header( 'HTTP/1.1 ' . $code );
108
if ( $e->useTemplates )
109
$e->showTemplate($message,$wikiHelp,$code);
110
else
111
$e->showStandard($message,$wikiHelp,$code);
112
exit();
113
114
}
115
116
}
117
118
/**
119
* this method is used to indicate that something has gone wrong, but
120
* that it's not serious. just a warning. these warning messages
121
* can then be reached by the getWarnings() method
122
*
123
* @param String $message the warning message
124
* @param String $wikiHelp the wiki help page
125
* @return nothing
126
*
127
*/
128
129
public static function warning( $message, $wikiHelp ) {
130
$e =& Smutty_Error::getInstance();
131
$e->addWarning( $message, $wikiHelp );
132
}
133
134
/**
135
* adds a warning message
136
*
137
* @param String $message the warning message
138
* @param String $wikiHelp the wiki help page
139
* @return nothing
140
*
141
*/
142
143
function addWarning( $message, $wikiHelp ) {
144
$warning = new stdclass();
145
$warning->message = $message;
146
$warning->wikiHelp = $wikiHelp;
147
$this->warnings[] = $warning;
148
}
149
150
/**
151
* returns an array of warning messages that have been generated
152
*
153
* @return array array of warning strings
154
*
155
*/
156
157
public function getWarnings() {
158
return $this->warnings;
159
}
160
161
/**
162
* handles an error from php.
163
*
164
* @param int $errno severity
165
* @param String $errstr description
166
* @param String $errfile the files full path
167
* @param int $errline the line
168
* @param int $errcon the contect
169
* @return nothing
170
*
171
*/
172
173
public function errorHandler( $errno, $errstr, $errfile, $errline, $errcon ) {
174
$showError = true;
175
switch ( $this->errorTypes ) {
176
case ERR_ALL:
177
break;
178
case ERR_STANDARD:
179
if ( in_array($errno,array(E_NOTICE)) )
180
$showError = false;
181
break;
182
}
183
// really show the error?
184
if ( $showError )
185
Smutty_Error::fatal(
186
"($errno) $errstr ($errfile, line $errline)",
187
'SmuttyOverview'
188
);
189
}
190
191
/**
192
* shows an error message using smarty templates
193
*
194
* @param String $message the error message
195
* @param String $wikiHelp name of wiki help page
196
* @return nothing
197
*
198
*/
199
200
private function showTemplate( $message, $wikiHelp, $code ) {
201
202
$tpl = null;
203
$tplFile = "error-$code.tpl";
204
205
if ( file_exists("application/views/$tplFile") )
206
$tpl = new Smutty_Template();
207
else {
208
$tpl = new Smutty_Template_Smutty();
209
$tplFile = 'exception.tpl';
210
}
211
212
$tpl->assign( 'stack', debug_backtrace() );
213
$tpl->assign( 'message', $message );
214
$tpl->assign( 'wikiHelp', $wikiHelp );
215
$tpl->display( $tplFile );
216
217
}
218
219
/**
220
* shows an error message with standard output
221
*
222
* @param String $message the error message
223
* @param String $wikiHelp name of wiki help page
224
* @return nothing
225
*
226
*/
227
228
private function showStandard( $message, $wikiHelp, $code ) {
229
?>
230
<html><head><title>Smutty Error</title></head><body>
231
<h1>&lt;Smutty Error&gt;</h1>
232
<p>Burp! An error has occurred, the following message was reported:</p>
233
<blockquote><?= htmlspecialchars($message) ?></blockquote>
234
<p>You may be able to <a href="http://smutty.pu-gh.com/wiki/<?= urlencode($wikiHelp) ?>">find help
235
on the wiki</a>! (<?= htmlspecialchars($wikiHelp) ?>)</p>
236
</body></html>
237
<?
238
}
239
240
/**
241
* sets the level of error reporting
242
*
243
* @param int $type the errors types to show
244
* @return nothing
245
*
246
*/
247
248
public static function setErrorReporting( $type ) {
249
$e =& Smutty_Error::getInstance();
250
$e->errorTypes = $type;
251
}
252
253
/**
254
* returns the current level of error reporting
255
*
256
* @return integer error reporting level
257
*
258
*/
259
260
public static function getErrorReporting() {
261
$e =& Smutty_Error::getInstance();
262
return $e->errorTypes;
263
}
264
265
}
266
267
?>

The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.