Chris@18
|
1 <?php
|
Chris@18
|
2 /**
|
Chris@18
|
3 * PEAR, the PHP Extension and Application Repository
|
Chris@18
|
4 *
|
Chris@18
|
5 * PEAR class and PEAR_Error class
|
Chris@18
|
6 *
|
Chris@18
|
7 * PHP versions 4 and 5
|
Chris@18
|
8 *
|
Chris@18
|
9 * @category pear
|
Chris@18
|
10 * @package PEAR
|
Chris@18
|
11 * @author Sterling Hughes <sterling@php.net>
|
Chris@18
|
12 * @author Stig Bakken <ssb@php.net>
|
Chris@18
|
13 * @author Tomas V.V.Cox <cox@idecnet.com>
|
Chris@18
|
14 * @author Greg Beaver <cellog@php.net>
|
Chris@18
|
15 * @copyright 1997-2010 The Authors
|
Chris@18
|
16 * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
Chris@18
|
17 * @link http://pear.php.net/package/PEAR
|
Chris@18
|
18 * @since File available since Release 0.1
|
Chris@18
|
19 */
|
Chris@18
|
20
|
Chris@18
|
21 /**#@+
|
Chris@18
|
22 * ERROR constants
|
Chris@18
|
23 */
|
Chris@18
|
24 define('PEAR_ERROR_RETURN', 1);
|
Chris@18
|
25 define('PEAR_ERROR_PRINT', 2);
|
Chris@18
|
26 define('PEAR_ERROR_TRIGGER', 4);
|
Chris@18
|
27 define('PEAR_ERROR_DIE', 8);
|
Chris@18
|
28 define('PEAR_ERROR_CALLBACK', 16);
|
Chris@18
|
29 /**
|
Chris@18
|
30 * WARNING: obsolete
|
Chris@18
|
31 * @deprecated
|
Chris@18
|
32 */
|
Chris@18
|
33 define('PEAR_ERROR_EXCEPTION', 32);
|
Chris@18
|
34 /**#@-*/
|
Chris@18
|
35
|
Chris@18
|
36 if (substr(PHP_OS, 0, 3) == 'WIN') {
|
Chris@18
|
37 define('OS_WINDOWS', true);
|
Chris@18
|
38 define('OS_UNIX', false);
|
Chris@18
|
39 define('PEAR_OS', 'Windows');
|
Chris@18
|
40 } else {
|
Chris@18
|
41 define('OS_WINDOWS', false);
|
Chris@18
|
42 define('OS_UNIX', true);
|
Chris@18
|
43 define('PEAR_OS', 'Unix'); // blatant assumption
|
Chris@18
|
44 }
|
Chris@18
|
45
|
Chris@18
|
46 $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
|
Chris@18
|
47 $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
|
Chris@18
|
48 $GLOBALS['_PEAR_destructor_object_list'] = array();
|
Chris@18
|
49 $GLOBALS['_PEAR_shutdown_funcs'] = array();
|
Chris@18
|
50 $GLOBALS['_PEAR_error_handler_stack'] = array();
|
Chris@18
|
51
|
Chris@18
|
52 @ini_set('track_errors', true);
|
Chris@18
|
53
|
Chris@18
|
54 /**
|
Chris@18
|
55 * Base class for other PEAR classes. Provides rudimentary
|
Chris@18
|
56 * emulation of destructors.
|
Chris@18
|
57 *
|
Chris@18
|
58 * If you want a destructor in your class, inherit PEAR and make a
|
Chris@18
|
59 * destructor method called _yourclassname (same name as the
|
Chris@18
|
60 * constructor, but with a "_" prefix). Also, in your constructor you
|
Chris@18
|
61 * have to call the PEAR constructor: $this->PEAR();.
|
Chris@18
|
62 * The destructor method will be called without parameters. Note that
|
Chris@18
|
63 * at in some SAPI implementations (such as Apache), any output during
|
Chris@18
|
64 * the request shutdown (in which destructors are called) seems to be
|
Chris@18
|
65 * discarded. If you need to get any debug information from your
|
Chris@18
|
66 * destructor, use error_log(), syslog() or something similar.
|
Chris@18
|
67 *
|
Chris@18
|
68 * IMPORTANT! To use the emulated destructors you need to create the
|
Chris@18
|
69 * objects by reference: $obj =& new PEAR_child;
|
Chris@18
|
70 *
|
Chris@18
|
71 * @category pear
|
Chris@18
|
72 * @package PEAR
|
Chris@18
|
73 * @author Stig Bakken <ssb@php.net>
|
Chris@18
|
74 * @author Tomas V.V. Cox <cox@idecnet.com>
|
Chris@18
|
75 * @author Greg Beaver <cellog@php.net>
|
Chris@18
|
76 * @copyright 1997-2006 The PHP Group
|
Chris@18
|
77 * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
Chris@18
|
78 * @version Release: @package_version@
|
Chris@18
|
79 * @link http://pear.php.net/package/PEAR
|
Chris@18
|
80 * @see PEAR_Error
|
Chris@18
|
81 * @since Class available since PHP 4.0.2
|
Chris@18
|
82 * @link http://pear.php.net/manual/en/core.pear.php#core.pear.pear
|
Chris@18
|
83 */
|
Chris@18
|
84 class PEAR
|
Chris@18
|
85 {
|
Chris@18
|
86 /**
|
Chris@18
|
87 * Whether to enable internal debug messages.
|
Chris@18
|
88 *
|
Chris@18
|
89 * @var bool
|
Chris@18
|
90 * @access private
|
Chris@18
|
91 */
|
Chris@18
|
92 var $_debug = false;
|
Chris@18
|
93
|
Chris@18
|
94 /**
|
Chris@18
|
95 * Default error mode for this object.
|
Chris@18
|
96 *
|
Chris@18
|
97 * @var int
|
Chris@18
|
98 * @access private
|
Chris@18
|
99 */
|
Chris@18
|
100 var $_default_error_mode = null;
|
Chris@18
|
101
|
Chris@18
|
102 /**
|
Chris@18
|
103 * Default error options used for this object when error mode
|
Chris@18
|
104 * is PEAR_ERROR_TRIGGER.
|
Chris@18
|
105 *
|
Chris@18
|
106 * @var int
|
Chris@18
|
107 * @access private
|
Chris@18
|
108 */
|
Chris@18
|
109 var $_default_error_options = null;
|
Chris@18
|
110
|
Chris@18
|
111 /**
|
Chris@18
|
112 * Default error handler (callback) for this object, if error mode is
|
Chris@18
|
113 * PEAR_ERROR_CALLBACK.
|
Chris@18
|
114 *
|
Chris@18
|
115 * @var string
|
Chris@18
|
116 * @access private
|
Chris@18
|
117 */
|
Chris@18
|
118 var $_default_error_handler = '';
|
Chris@18
|
119
|
Chris@18
|
120 /**
|
Chris@18
|
121 * Which class to use for error objects.
|
Chris@18
|
122 *
|
Chris@18
|
123 * @var string
|
Chris@18
|
124 * @access private
|
Chris@18
|
125 */
|
Chris@18
|
126 var $_error_class = 'PEAR_Error';
|
Chris@18
|
127
|
Chris@18
|
128 /**
|
Chris@18
|
129 * An array of expected errors.
|
Chris@18
|
130 *
|
Chris@18
|
131 * @var array
|
Chris@18
|
132 * @access private
|
Chris@18
|
133 */
|
Chris@18
|
134 var $_expected_errors = array();
|
Chris@18
|
135
|
Chris@18
|
136 /**
|
Chris@18
|
137 * List of methods that can be called both statically and non-statically.
|
Chris@18
|
138 * @var array
|
Chris@18
|
139 */
|
Chris@18
|
140 protected static $bivalentMethods = array(
|
Chris@18
|
141 'setErrorHandling' => true,
|
Chris@18
|
142 'raiseError' => true,
|
Chris@18
|
143 'throwError' => true,
|
Chris@18
|
144 'pushErrorHandling' => true,
|
Chris@18
|
145 'popErrorHandling' => true,
|
Chris@18
|
146 );
|
Chris@18
|
147
|
Chris@18
|
148 /**
|
Chris@18
|
149 * Constructor. Registers this object in
|
Chris@18
|
150 * $_PEAR_destructor_object_list for destructor emulation if a
|
Chris@18
|
151 * destructor object exists.
|
Chris@18
|
152 *
|
Chris@18
|
153 * @param string $error_class (optional) which class to use for
|
Chris@18
|
154 * error objects, defaults to PEAR_Error.
|
Chris@18
|
155 * @access public
|
Chris@18
|
156 * @return void
|
Chris@18
|
157 */
|
Chris@18
|
158 function __construct($error_class = null)
|
Chris@18
|
159 {
|
Chris@18
|
160 $classname = strtolower(get_class($this));
|
Chris@18
|
161 if ($this->_debug) {
|
Chris@18
|
162 print "PEAR constructor called, class=$classname\n";
|
Chris@18
|
163 }
|
Chris@18
|
164
|
Chris@18
|
165 if ($error_class !== null) {
|
Chris@18
|
166 $this->_error_class = $error_class;
|
Chris@18
|
167 }
|
Chris@18
|
168
|
Chris@18
|
169 while ($classname && strcasecmp($classname, "pear")) {
|
Chris@18
|
170 $destructor = "_$classname";
|
Chris@18
|
171 if (method_exists($this, $destructor)) {
|
Chris@18
|
172 global $_PEAR_destructor_object_list;
|
Chris@18
|
173 $_PEAR_destructor_object_list[] = $this;
|
Chris@18
|
174 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
|
Chris@18
|
175 register_shutdown_function("_PEAR_call_destructors");
|
Chris@18
|
176 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
|
Chris@18
|
177 }
|
Chris@18
|
178 break;
|
Chris@18
|
179 } else {
|
Chris@18
|
180 $classname = get_parent_class($classname);
|
Chris@18
|
181 }
|
Chris@18
|
182 }
|
Chris@18
|
183 }
|
Chris@18
|
184
|
Chris@18
|
185 /**
|
Chris@18
|
186 * Only here for backwards compatibility.
|
Chris@18
|
187 * E.g. Archive_Tar calls $this->PEAR() in its constructor.
|
Chris@18
|
188 *
|
Chris@18
|
189 * @param string $error_class Which class to use for error objects,
|
Chris@18
|
190 * defaults to PEAR_Error.
|
Chris@18
|
191 */
|
Chris@18
|
192 public function PEAR($error_class = null)
|
Chris@18
|
193 {
|
Chris@18
|
194 self::__construct($error_class);
|
Chris@18
|
195 }
|
Chris@18
|
196
|
Chris@18
|
197 /**
|
Chris@18
|
198 * Destructor (the emulated type of...). Does nothing right now,
|
Chris@18
|
199 * but is included for forward compatibility, so subclass
|
Chris@18
|
200 * destructors should always call it.
|
Chris@18
|
201 *
|
Chris@18
|
202 * See the note in the class desciption about output from
|
Chris@18
|
203 * destructors.
|
Chris@18
|
204 *
|
Chris@18
|
205 * @access public
|
Chris@18
|
206 * @return void
|
Chris@18
|
207 */
|
Chris@18
|
208 function _PEAR() {
|
Chris@18
|
209 if ($this->_debug) {
|
Chris@18
|
210 printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
|
Chris@18
|
211 }
|
Chris@18
|
212 }
|
Chris@18
|
213
|
Chris@18
|
214 public function __call($method, $arguments)
|
Chris@18
|
215 {
|
Chris@18
|
216 if (!isset(self::$bivalentMethods[$method])) {
|
Chris@18
|
217 trigger_error(
|
Chris@18
|
218 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR
|
Chris@18
|
219 );
|
Chris@18
|
220 }
|
Chris@18
|
221 return call_user_func_array(
|
Chris@18
|
222 array(get_class(), '_' . $method),
|
Chris@18
|
223 array_merge(array($this), $arguments)
|
Chris@18
|
224 );
|
Chris@18
|
225 }
|
Chris@18
|
226
|
Chris@18
|
227 public static function __callStatic($method, $arguments)
|
Chris@18
|
228 {
|
Chris@18
|
229 if (!isset(self::$bivalentMethods[$method])) {
|
Chris@18
|
230 trigger_error(
|
Chris@18
|
231 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR
|
Chris@18
|
232 );
|
Chris@18
|
233 }
|
Chris@18
|
234 return call_user_func_array(
|
Chris@18
|
235 array(get_class(), '_' . $method),
|
Chris@18
|
236 array_merge(array(null), $arguments)
|
Chris@18
|
237 );
|
Chris@18
|
238 }
|
Chris@18
|
239
|
Chris@18
|
240 /**
|
Chris@18
|
241 * If you have a class that's mostly/entirely static, and you need static
|
Chris@18
|
242 * properties, you can use this method to simulate them. Eg. in your method(s)
|
Chris@18
|
243 * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
|
Chris@18
|
244 * You MUST use a reference, or they will not persist!
|
Chris@18
|
245 *
|
Chris@18
|
246 * @param string $class The calling classname, to prevent clashes
|
Chris@18
|
247 * @param string $var The variable to retrieve.
|
Chris@18
|
248 * @return mixed A reference to the variable. If not set it will be
|
Chris@18
|
249 * auto initialised to NULL.
|
Chris@18
|
250 */
|
Chris@18
|
251 public static function &getStaticProperty($class, $var)
|
Chris@18
|
252 {
|
Chris@18
|
253 static $properties;
|
Chris@18
|
254 if (!isset($properties[$class])) {
|
Chris@18
|
255 $properties[$class] = array();
|
Chris@18
|
256 }
|
Chris@18
|
257
|
Chris@18
|
258 if (!array_key_exists($var, $properties[$class])) {
|
Chris@18
|
259 $properties[$class][$var] = null;
|
Chris@18
|
260 }
|
Chris@18
|
261
|
Chris@18
|
262 return $properties[$class][$var];
|
Chris@18
|
263 }
|
Chris@18
|
264
|
Chris@18
|
265 /**
|
Chris@18
|
266 * Use this function to register a shutdown method for static
|
Chris@18
|
267 * classes.
|
Chris@18
|
268 *
|
Chris@18
|
269 * @param mixed $func The function name (or array of class/method) to call
|
Chris@18
|
270 * @param mixed $args The arguments to pass to the function
|
Chris@18
|
271 *
|
Chris@18
|
272 * @return void
|
Chris@18
|
273 */
|
Chris@18
|
274 public static function registerShutdownFunc($func, $args = array())
|
Chris@18
|
275 {
|
Chris@18
|
276 // if we are called statically, there is a potential
|
Chris@18
|
277 // that no shutdown func is registered. Bug #6445
|
Chris@18
|
278 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
|
Chris@18
|
279 register_shutdown_function("_PEAR_call_destructors");
|
Chris@18
|
280 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
|
Chris@18
|
281 }
|
Chris@18
|
282 $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
|
Chris@18
|
283 }
|
Chris@18
|
284
|
Chris@18
|
285 /**
|
Chris@18
|
286 * Tell whether a value is a PEAR error.
|
Chris@18
|
287 *
|
Chris@18
|
288 * @param mixed $data the value to test
|
Chris@18
|
289 * @param int $code if $data is an error object, return true
|
Chris@18
|
290 * only if $code is a string and
|
Chris@18
|
291 * $obj->getMessage() == $code or
|
Chris@18
|
292 * $code is an integer and $obj->getCode() == $code
|
Chris@18
|
293 *
|
Chris@18
|
294 * @return bool true if parameter is an error
|
Chris@18
|
295 */
|
Chris@18
|
296 public static function isError($data, $code = null)
|
Chris@18
|
297 {
|
Chris@18
|
298 if (!is_a($data, 'PEAR_Error')) {
|
Chris@18
|
299 return false;
|
Chris@18
|
300 }
|
Chris@18
|
301
|
Chris@18
|
302 if (is_null($code)) {
|
Chris@18
|
303 return true;
|
Chris@18
|
304 } elseif (is_string($code)) {
|
Chris@18
|
305 return $data->getMessage() == $code;
|
Chris@18
|
306 }
|
Chris@18
|
307
|
Chris@18
|
308 return $data->getCode() == $code;
|
Chris@18
|
309 }
|
Chris@18
|
310
|
Chris@18
|
311 /**
|
Chris@18
|
312 * Sets how errors generated by this object should be handled.
|
Chris@18
|
313 * Can be invoked both in objects and statically. If called
|
Chris@18
|
314 * statically, setErrorHandling sets the default behaviour for all
|
Chris@18
|
315 * PEAR objects. If called in an object, setErrorHandling sets
|
Chris@18
|
316 * the default behaviour for that object.
|
Chris@18
|
317 *
|
Chris@18
|
318 * @param object $object
|
Chris@18
|
319 * Object the method was called on (non-static mode)
|
Chris@18
|
320 *
|
Chris@18
|
321 * @param int $mode
|
Chris@18
|
322 * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
|
Chris@18
|
323 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
|
Chris@18
|
324 * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
|
Chris@18
|
325 *
|
Chris@18
|
326 * @param mixed $options
|
Chris@18
|
327 * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
|
Chris@18
|
328 * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
|
Chris@18
|
329 *
|
Chris@18
|
330 * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
|
Chris@18
|
331 * to be the callback function or method. A callback
|
Chris@18
|
332 * function is a string with the name of the function, a
|
Chris@18
|
333 * callback method is an array of two elements: the element
|
Chris@18
|
334 * at index 0 is the object, and the element at index 1 is
|
Chris@18
|
335 * the name of the method to call in the object.
|
Chris@18
|
336 *
|
Chris@18
|
337 * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
|
Chris@18
|
338 * a printf format string used when printing the error
|
Chris@18
|
339 * message.
|
Chris@18
|
340 *
|
Chris@18
|
341 * @access public
|
Chris@18
|
342 * @return void
|
Chris@18
|
343 * @see PEAR_ERROR_RETURN
|
Chris@18
|
344 * @see PEAR_ERROR_PRINT
|
Chris@18
|
345 * @see PEAR_ERROR_TRIGGER
|
Chris@18
|
346 * @see PEAR_ERROR_DIE
|
Chris@18
|
347 * @see PEAR_ERROR_CALLBACK
|
Chris@18
|
348 * @see PEAR_ERROR_EXCEPTION
|
Chris@18
|
349 *
|
Chris@18
|
350 * @since PHP 4.0.5
|
Chris@18
|
351 */
|
Chris@18
|
352 protected static function _setErrorHandling(
|
Chris@18
|
353 $object, $mode = null, $options = null
|
Chris@18
|
354 ) {
|
Chris@18
|
355 if ($object !== null) {
|
Chris@18
|
356 $setmode = &$object->_default_error_mode;
|
Chris@18
|
357 $setoptions = &$object->_default_error_options;
|
Chris@18
|
358 } else {
|
Chris@18
|
359 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
|
Chris@18
|
360 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
|
Chris@18
|
361 }
|
Chris@18
|
362
|
Chris@18
|
363 switch ($mode) {
|
Chris@18
|
364 case PEAR_ERROR_EXCEPTION:
|
Chris@18
|
365 case PEAR_ERROR_RETURN:
|
Chris@18
|
366 case PEAR_ERROR_PRINT:
|
Chris@18
|
367 case PEAR_ERROR_TRIGGER:
|
Chris@18
|
368 case PEAR_ERROR_DIE:
|
Chris@18
|
369 case null:
|
Chris@18
|
370 $setmode = $mode;
|
Chris@18
|
371 $setoptions = $options;
|
Chris@18
|
372 break;
|
Chris@18
|
373
|
Chris@18
|
374 case PEAR_ERROR_CALLBACK:
|
Chris@18
|
375 $setmode = $mode;
|
Chris@18
|
376 // class/object method callback
|
Chris@18
|
377 if (is_callable($options)) {
|
Chris@18
|
378 $setoptions = $options;
|
Chris@18
|
379 } else {
|
Chris@18
|
380 trigger_error("invalid error callback", E_USER_WARNING);
|
Chris@18
|
381 }
|
Chris@18
|
382 break;
|
Chris@18
|
383
|
Chris@18
|
384 default:
|
Chris@18
|
385 trigger_error("invalid error mode", E_USER_WARNING);
|
Chris@18
|
386 break;
|
Chris@18
|
387 }
|
Chris@18
|
388 }
|
Chris@18
|
389
|
Chris@18
|
390 /**
|
Chris@18
|
391 * This method is used to tell which errors you expect to get.
|
Chris@18
|
392 * Expected errors are always returned with error mode
|
Chris@18
|
393 * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
|
Chris@18
|
394 * and this method pushes a new element onto it. The list of
|
Chris@18
|
395 * expected errors are in effect until they are popped off the
|
Chris@18
|
396 * stack with the popExpect() method.
|
Chris@18
|
397 *
|
Chris@18
|
398 * Note that this method can not be called statically
|
Chris@18
|
399 *
|
Chris@18
|
400 * @param mixed $code a single error code or an array of error codes to expect
|
Chris@18
|
401 *
|
Chris@18
|
402 * @return int the new depth of the "expected errors" stack
|
Chris@18
|
403 * @access public
|
Chris@18
|
404 */
|
Chris@18
|
405 function expectError($code = '*')
|
Chris@18
|
406 {
|
Chris@18
|
407 if (is_array($code)) {
|
Chris@18
|
408 array_push($this->_expected_errors, $code);
|
Chris@18
|
409 } else {
|
Chris@18
|
410 array_push($this->_expected_errors, array($code));
|
Chris@18
|
411 }
|
Chris@18
|
412 return count($this->_expected_errors);
|
Chris@18
|
413 }
|
Chris@18
|
414
|
Chris@18
|
415 /**
|
Chris@18
|
416 * This method pops one element off the expected error codes
|
Chris@18
|
417 * stack.
|
Chris@18
|
418 *
|
Chris@18
|
419 * @return array the list of error codes that were popped
|
Chris@18
|
420 */
|
Chris@18
|
421 function popExpect()
|
Chris@18
|
422 {
|
Chris@18
|
423 return array_pop($this->_expected_errors);
|
Chris@18
|
424 }
|
Chris@18
|
425
|
Chris@18
|
426 /**
|
Chris@18
|
427 * This method checks unsets an error code if available
|
Chris@18
|
428 *
|
Chris@18
|
429 * @param mixed error code
|
Chris@18
|
430 * @return bool true if the error code was unset, false otherwise
|
Chris@18
|
431 * @access private
|
Chris@18
|
432 * @since PHP 4.3.0
|
Chris@18
|
433 */
|
Chris@18
|
434 function _checkDelExpect($error_code)
|
Chris@18
|
435 {
|
Chris@18
|
436 $deleted = false;
|
Chris@18
|
437 foreach ($this->_expected_errors as $key => $error_array) {
|
Chris@18
|
438 if (in_array($error_code, $error_array)) {
|
Chris@18
|
439 unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
|
Chris@18
|
440 $deleted = true;
|
Chris@18
|
441 }
|
Chris@18
|
442
|
Chris@18
|
443 // clean up empty arrays
|
Chris@18
|
444 if (0 == count($this->_expected_errors[$key])) {
|
Chris@18
|
445 unset($this->_expected_errors[$key]);
|
Chris@18
|
446 }
|
Chris@18
|
447 }
|
Chris@18
|
448
|
Chris@18
|
449 return $deleted;
|
Chris@18
|
450 }
|
Chris@18
|
451
|
Chris@18
|
452 /**
|
Chris@18
|
453 * This method deletes all occurrences of the specified element from
|
Chris@18
|
454 * the expected error codes stack.
|
Chris@18
|
455 *
|
Chris@18
|
456 * @param mixed $error_code error code that should be deleted
|
Chris@18
|
457 * @return mixed list of error codes that were deleted or error
|
Chris@18
|
458 * @access public
|
Chris@18
|
459 * @since PHP 4.3.0
|
Chris@18
|
460 */
|
Chris@18
|
461 function delExpect($error_code)
|
Chris@18
|
462 {
|
Chris@18
|
463 $deleted = false;
|
Chris@18
|
464 if ((is_array($error_code) && (0 != count($error_code)))) {
|
Chris@18
|
465 // $error_code is a non-empty array here; we walk through it trying
|
Chris@18
|
466 // to unset all values
|
Chris@18
|
467 foreach ($error_code as $key => $error) {
|
Chris@18
|
468 $deleted = $this->_checkDelExpect($error) ? true : false;
|
Chris@18
|
469 }
|
Chris@18
|
470
|
Chris@18
|
471 return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
|
Chris@18
|
472 } elseif (!empty($error_code)) {
|
Chris@18
|
473 // $error_code comes alone, trying to unset it
|
Chris@18
|
474 if ($this->_checkDelExpect($error_code)) {
|
Chris@18
|
475 return true;
|
Chris@18
|
476 }
|
Chris@18
|
477
|
Chris@18
|
478 return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
|
Chris@18
|
479 }
|
Chris@18
|
480
|
Chris@18
|
481 // $error_code is empty
|
Chris@18
|
482 return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
|
Chris@18
|
483 }
|
Chris@18
|
484
|
Chris@18
|
485 /**
|
Chris@18
|
486 * This method is a wrapper that returns an instance of the
|
Chris@18
|
487 * configured error class with this object's default error
|
Chris@18
|
488 * handling applied. If the $mode and $options parameters are not
|
Chris@18
|
489 * specified, the object's defaults are used.
|
Chris@18
|
490 *
|
Chris@18
|
491 * @param mixed $message a text error message or a PEAR error object
|
Chris@18
|
492 *
|
Chris@18
|
493 * @param int $code a numeric error code (it is up to your class
|
Chris@18
|
494 * to define these if you want to use codes)
|
Chris@18
|
495 *
|
Chris@18
|
496 * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
|
Chris@18
|
497 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
|
Chris@18
|
498 * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
|
Chris@18
|
499 *
|
Chris@18
|
500 * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
|
Chris@18
|
501 * specifies the PHP-internal error level (one of
|
Chris@18
|
502 * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
|
Chris@18
|
503 * If $mode is PEAR_ERROR_CALLBACK, this
|
Chris@18
|
504 * parameter specifies the callback function or
|
Chris@18
|
505 * method. In other error modes this parameter
|
Chris@18
|
506 * is ignored.
|
Chris@18
|
507 *
|
Chris@18
|
508 * @param string $userinfo If you need to pass along for example debug
|
Chris@18
|
509 * information, this parameter is meant for that.
|
Chris@18
|
510 *
|
Chris@18
|
511 * @param string $error_class The returned error object will be
|
Chris@18
|
512 * instantiated from this class, if specified.
|
Chris@18
|
513 *
|
Chris@18
|
514 * @param bool $skipmsg If true, raiseError will only pass error codes,
|
Chris@18
|
515 * the error message parameter will be dropped.
|
Chris@18
|
516 *
|
Chris@18
|
517 * @return object a PEAR error object
|
Chris@18
|
518 * @see PEAR::setErrorHandling
|
Chris@18
|
519 * @since PHP 4.0.5
|
Chris@18
|
520 */
|
Chris@18
|
521 protected static function _raiseError($object,
|
Chris@18
|
522 $message = null,
|
Chris@18
|
523 $code = null,
|
Chris@18
|
524 $mode = null,
|
Chris@18
|
525 $options = null,
|
Chris@18
|
526 $userinfo = null,
|
Chris@18
|
527 $error_class = null,
|
Chris@18
|
528 $skipmsg = false)
|
Chris@18
|
529 {
|
Chris@18
|
530 // The error is yet a PEAR error object
|
Chris@18
|
531 if (is_object($message)) {
|
Chris@18
|
532 $code = $message->getCode();
|
Chris@18
|
533 $userinfo = $message->getUserInfo();
|
Chris@18
|
534 $error_class = $message->getType();
|
Chris@18
|
535 $message->error_message_prefix = '';
|
Chris@18
|
536 $message = $message->getMessage();
|
Chris@18
|
537 }
|
Chris@18
|
538
|
Chris@18
|
539 if (
|
Chris@18
|
540 $object !== null &&
|
Chris@18
|
541 isset($object->_expected_errors) &&
|
Chris@18
|
542 count($object->_expected_errors) > 0 &&
|
Chris@18
|
543 count($exp = end($object->_expected_errors))
|
Chris@18
|
544 ) {
|
Chris@18
|
545 if ($exp[0] === "*" ||
|
Chris@18
|
546 (is_int(reset($exp)) && in_array($code, $exp)) ||
|
Chris@18
|
547 (is_string(reset($exp)) && in_array($message, $exp))
|
Chris@18
|
548 ) {
|
Chris@18
|
549 $mode = PEAR_ERROR_RETURN;
|
Chris@18
|
550 }
|
Chris@18
|
551 }
|
Chris@18
|
552
|
Chris@18
|
553 // No mode given, try global ones
|
Chris@18
|
554 if ($mode === null) {
|
Chris@18
|
555 // Class error handler
|
Chris@18
|
556 if ($object !== null && isset($object->_default_error_mode)) {
|
Chris@18
|
557 $mode = $object->_default_error_mode;
|
Chris@18
|
558 $options = $object->_default_error_options;
|
Chris@18
|
559 // Global error handler
|
Chris@18
|
560 } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
|
Chris@18
|
561 $mode = $GLOBALS['_PEAR_default_error_mode'];
|
Chris@18
|
562 $options = $GLOBALS['_PEAR_default_error_options'];
|
Chris@18
|
563 }
|
Chris@18
|
564 }
|
Chris@18
|
565
|
Chris@18
|
566 if ($error_class !== null) {
|
Chris@18
|
567 $ec = $error_class;
|
Chris@18
|
568 } elseif ($object !== null && isset($object->_error_class)) {
|
Chris@18
|
569 $ec = $object->_error_class;
|
Chris@18
|
570 } else {
|
Chris@18
|
571 $ec = 'PEAR_Error';
|
Chris@18
|
572 }
|
Chris@18
|
573
|
Chris@18
|
574 if ($skipmsg) {
|
Chris@18
|
575 $a = new $ec($code, $mode, $options, $userinfo);
|
Chris@18
|
576 } else {
|
Chris@18
|
577 $a = new $ec($message, $code, $mode, $options, $userinfo);
|
Chris@18
|
578 }
|
Chris@18
|
579
|
Chris@18
|
580 return $a;
|
Chris@18
|
581 }
|
Chris@18
|
582
|
Chris@18
|
583 /**
|
Chris@18
|
584 * Simpler form of raiseError with fewer options. In most cases
|
Chris@18
|
585 * message, code and userinfo are enough.
|
Chris@18
|
586 *
|
Chris@18
|
587 * @param mixed $message a text error message or a PEAR error object
|
Chris@18
|
588 *
|
Chris@18
|
589 * @param int $code a numeric error code (it is up to your class
|
Chris@18
|
590 * to define these if you want to use codes)
|
Chris@18
|
591 *
|
Chris@18
|
592 * @param string $userinfo If you need to pass along for example debug
|
Chris@18
|
593 * information, this parameter is meant for that.
|
Chris@18
|
594 *
|
Chris@18
|
595 * @return object a PEAR error object
|
Chris@18
|
596 * @see PEAR::raiseError
|
Chris@18
|
597 */
|
Chris@18
|
598 protected static function _throwError($object, $message = null, $code = null, $userinfo = null)
|
Chris@18
|
599 {
|
Chris@18
|
600 if ($object !== null) {
|
Chris@18
|
601 $a = $object->raiseError($message, $code, null, null, $userinfo);
|
Chris@18
|
602 return $a;
|
Chris@18
|
603 }
|
Chris@18
|
604
|
Chris@18
|
605 $a = PEAR::raiseError($message, $code, null, null, $userinfo);
|
Chris@18
|
606 return $a;
|
Chris@18
|
607 }
|
Chris@18
|
608
|
Chris@18
|
609 public static function staticPushErrorHandling($mode, $options = null)
|
Chris@18
|
610 {
|
Chris@18
|
611 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
|
Chris@18
|
612 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
|
Chris@18
|
613 $def_options = &$GLOBALS['_PEAR_default_error_options'];
|
Chris@18
|
614 $stack[] = array($def_mode, $def_options);
|
Chris@18
|
615 switch ($mode) {
|
Chris@18
|
616 case PEAR_ERROR_EXCEPTION:
|
Chris@18
|
617 case PEAR_ERROR_RETURN:
|
Chris@18
|
618 case PEAR_ERROR_PRINT:
|
Chris@18
|
619 case PEAR_ERROR_TRIGGER:
|
Chris@18
|
620 case PEAR_ERROR_DIE:
|
Chris@18
|
621 case null:
|
Chris@18
|
622 $def_mode = $mode;
|
Chris@18
|
623 $def_options = $options;
|
Chris@18
|
624 break;
|
Chris@18
|
625
|
Chris@18
|
626 case PEAR_ERROR_CALLBACK:
|
Chris@18
|
627 $def_mode = $mode;
|
Chris@18
|
628 // class/object method callback
|
Chris@18
|
629 if (is_callable($options)) {
|
Chris@18
|
630 $def_options = $options;
|
Chris@18
|
631 } else {
|
Chris@18
|
632 trigger_error("invalid error callback", E_USER_WARNING);
|
Chris@18
|
633 }
|
Chris@18
|
634 break;
|
Chris@18
|
635
|
Chris@18
|
636 default:
|
Chris@18
|
637 trigger_error("invalid error mode", E_USER_WARNING);
|
Chris@18
|
638 break;
|
Chris@18
|
639 }
|
Chris@18
|
640 $stack[] = array($mode, $options);
|
Chris@18
|
641 return true;
|
Chris@18
|
642 }
|
Chris@18
|
643
|
Chris@18
|
644 public static function staticPopErrorHandling()
|
Chris@18
|
645 {
|
Chris@18
|
646 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
|
Chris@18
|
647 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
|
Chris@18
|
648 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
|
Chris@18
|
649 array_pop($stack);
|
Chris@18
|
650 list($mode, $options) = $stack[sizeof($stack) - 1];
|
Chris@18
|
651 array_pop($stack);
|
Chris@18
|
652 switch ($mode) {
|
Chris@18
|
653 case PEAR_ERROR_EXCEPTION:
|
Chris@18
|
654 case PEAR_ERROR_RETURN:
|
Chris@18
|
655 case PEAR_ERROR_PRINT:
|
Chris@18
|
656 case PEAR_ERROR_TRIGGER:
|
Chris@18
|
657 case PEAR_ERROR_DIE:
|
Chris@18
|
658 case null:
|
Chris@18
|
659 $setmode = $mode;
|
Chris@18
|
660 $setoptions = $options;
|
Chris@18
|
661 break;
|
Chris@18
|
662
|
Chris@18
|
663 case PEAR_ERROR_CALLBACK:
|
Chris@18
|
664 $setmode = $mode;
|
Chris@18
|
665 // class/object method callback
|
Chris@18
|
666 if (is_callable($options)) {
|
Chris@18
|
667 $setoptions = $options;
|
Chris@18
|
668 } else {
|
Chris@18
|
669 trigger_error("invalid error callback", E_USER_WARNING);
|
Chris@18
|
670 }
|
Chris@18
|
671 break;
|
Chris@18
|
672
|
Chris@18
|
673 default:
|
Chris@18
|
674 trigger_error("invalid error mode", E_USER_WARNING);
|
Chris@18
|
675 break;
|
Chris@18
|
676 }
|
Chris@18
|
677 return true;
|
Chris@18
|
678 }
|
Chris@18
|
679
|
Chris@18
|
680 /**
|
Chris@18
|
681 * Push a new error handler on top of the error handler options stack. With this
|
Chris@18
|
682 * you can easily override the actual error handler for some code and restore
|
Chris@18
|
683 * it later with popErrorHandling.
|
Chris@18
|
684 *
|
Chris@18
|
685 * @param mixed $mode (same as setErrorHandling)
|
Chris@18
|
686 * @param mixed $options (same as setErrorHandling)
|
Chris@18
|
687 *
|
Chris@18
|
688 * @return bool Always true
|
Chris@18
|
689 *
|
Chris@18
|
690 * @see PEAR::setErrorHandling
|
Chris@18
|
691 */
|
Chris@18
|
692 protected static function _pushErrorHandling($object, $mode, $options = null)
|
Chris@18
|
693 {
|
Chris@18
|
694 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
|
Chris@18
|
695 if ($object !== null) {
|
Chris@18
|
696 $def_mode = &$object->_default_error_mode;
|
Chris@18
|
697 $def_options = &$object->_default_error_options;
|
Chris@18
|
698 } else {
|
Chris@18
|
699 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
|
Chris@18
|
700 $def_options = &$GLOBALS['_PEAR_default_error_options'];
|
Chris@18
|
701 }
|
Chris@18
|
702 $stack[] = array($def_mode, $def_options);
|
Chris@18
|
703
|
Chris@18
|
704 if ($object !== null) {
|
Chris@18
|
705 $object->setErrorHandling($mode, $options);
|
Chris@18
|
706 } else {
|
Chris@18
|
707 PEAR::setErrorHandling($mode, $options);
|
Chris@18
|
708 }
|
Chris@18
|
709 $stack[] = array($mode, $options);
|
Chris@18
|
710 return true;
|
Chris@18
|
711 }
|
Chris@18
|
712
|
Chris@18
|
713 /**
|
Chris@18
|
714 * Pop the last error handler used
|
Chris@18
|
715 *
|
Chris@18
|
716 * @return bool Always true
|
Chris@18
|
717 *
|
Chris@18
|
718 * @see PEAR::pushErrorHandling
|
Chris@18
|
719 */
|
Chris@18
|
720 protected static function _popErrorHandling($object)
|
Chris@18
|
721 {
|
Chris@18
|
722 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
|
Chris@18
|
723 array_pop($stack);
|
Chris@18
|
724 list($mode, $options) = $stack[sizeof($stack) - 1];
|
Chris@18
|
725 array_pop($stack);
|
Chris@18
|
726 if ($object !== null) {
|
Chris@18
|
727 $object->setErrorHandling($mode, $options);
|
Chris@18
|
728 } else {
|
Chris@18
|
729 PEAR::setErrorHandling($mode, $options);
|
Chris@18
|
730 }
|
Chris@18
|
731 return true;
|
Chris@18
|
732 }
|
Chris@18
|
733
|
Chris@18
|
734 /**
|
Chris@18
|
735 * OS independent PHP extension load. Remember to take care
|
Chris@18
|
736 * on the correct extension name for case sensitive OSes.
|
Chris@18
|
737 *
|
Chris@18
|
738 * @param string $ext The extension name
|
Chris@18
|
739 * @return bool Success or not on the dl() call
|
Chris@18
|
740 */
|
Chris@18
|
741 public static function loadExtension($ext)
|
Chris@18
|
742 {
|
Chris@18
|
743 if (extension_loaded($ext)) {
|
Chris@18
|
744 return true;
|
Chris@18
|
745 }
|
Chris@18
|
746
|
Chris@18
|
747 // if either returns true dl() will produce a FATAL error, stop that
|
Chris@18
|
748 if (
|
Chris@18
|
749 function_exists('dl') === false ||
|
Chris@18
|
750 ini_get('enable_dl') != 1
|
Chris@18
|
751 ) {
|
Chris@18
|
752 return false;
|
Chris@18
|
753 }
|
Chris@18
|
754
|
Chris@18
|
755 if (OS_WINDOWS) {
|
Chris@18
|
756 $suffix = '.dll';
|
Chris@18
|
757 } elseif (PHP_OS == 'HP-UX') {
|
Chris@18
|
758 $suffix = '.sl';
|
Chris@18
|
759 } elseif (PHP_OS == 'AIX') {
|
Chris@18
|
760 $suffix = '.a';
|
Chris@18
|
761 } elseif (PHP_OS == 'OSX') {
|
Chris@18
|
762 $suffix = '.bundle';
|
Chris@18
|
763 } else {
|
Chris@18
|
764 $suffix = '.so';
|
Chris@18
|
765 }
|
Chris@18
|
766
|
Chris@18
|
767 return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
|
Chris@18
|
768 }
|
Chris@18
|
769 }
|
Chris@18
|
770
|
Chris@18
|
771 function _PEAR_call_destructors()
|
Chris@18
|
772 {
|
Chris@18
|
773 global $_PEAR_destructor_object_list;
|
Chris@18
|
774 if (is_array($_PEAR_destructor_object_list) &&
|
Chris@18
|
775 sizeof($_PEAR_destructor_object_list))
|
Chris@18
|
776 {
|
Chris@18
|
777 reset($_PEAR_destructor_object_list);
|
Chris@18
|
778
|
Chris@18
|
779 $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo');
|
Chris@18
|
780
|
Chris@18
|
781 if ($destructLifoExists) {
|
Chris@18
|
782 $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
|
Chris@18
|
783 }
|
Chris@18
|
784
|
Chris@18
|
785 foreach ($_PEAR_destructor_object_list as $k => $objref) {
|
Chris@18
|
786 $classname = get_class($objref);
|
Chris@18
|
787 while ($classname) {
|
Chris@18
|
788 $destructor = "_$classname";
|
Chris@18
|
789 if (method_exists($objref, $destructor)) {
|
Chris@18
|
790 $objref->$destructor();
|
Chris@18
|
791 break;
|
Chris@18
|
792 } else {
|
Chris@18
|
793 $classname = get_parent_class($classname);
|
Chris@18
|
794 }
|
Chris@18
|
795 }
|
Chris@18
|
796 }
|
Chris@18
|
797 // Empty the object list to ensure that destructors are
|
Chris@18
|
798 // not called more than once.
|
Chris@18
|
799 $_PEAR_destructor_object_list = array();
|
Chris@18
|
800 }
|
Chris@18
|
801
|
Chris@18
|
802 // Now call the shutdown functions
|
Chris@18
|
803 if (
|
Chris@18
|
804 isset($GLOBALS['_PEAR_shutdown_funcs']) &&
|
Chris@18
|
805 is_array($GLOBALS['_PEAR_shutdown_funcs']) &&
|
Chris@18
|
806 !empty($GLOBALS['_PEAR_shutdown_funcs'])
|
Chris@18
|
807 ) {
|
Chris@18
|
808 foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
|
Chris@18
|
809 call_user_func_array($value[0], $value[1]);
|
Chris@18
|
810 }
|
Chris@18
|
811 }
|
Chris@18
|
812 }
|
Chris@18
|
813
|
Chris@18
|
814 /**
|
Chris@18
|
815 * Standard PEAR error class for PHP 4
|
Chris@18
|
816 *
|
Chris@18
|
817 * This class is supserseded by {@link PEAR_Exception} in PHP 5
|
Chris@18
|
818 *
|
Chris@18
|
819 * @category pear
|
Chris@18
|
820 * @package PEAR
|
Chris@18
|
821 * @author Stig Bakken <ssb@php.net>
|
Chris@18
|
822 * @author Tomas V.V. Cox <cox@idecnet.com>
|
Chris@18
|
823 * @author Gregory Beaver <cellog@php.net>
|
Chris@18
|
824 * @copyright 1997-2006 The PHP Group
|
Chris@18
|
825 * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
Chris@18
|
826 * @version Release: @package_version@
|
Chris@18
|
827 * @link http://pear.php.net/manual/en/core.pear.pear-error.php
|
Chris@18
|
828 * @see PEAR::raiseError(), PEAR::throwError()
|
Chris@18
|
829 * @since Class available since PHP 4.0.2
|
Chris@18
|
830 */
|
Chris@18
|
831 class PEAR_Error
|
Chris@18
|
832 {
|
Chris@18
|
833 var $error_message_prefix = '';
|
Chris@18
|
834 var $mode = PEAR_ERROR_RETURN;
|
Chris@18
|
835 var $level = E_USER_NOTICE;
|
Chris@18
|
836 var $code = -1;
|
Chris@18
|
837 var $message = '';
|
Chris@18
|
838 var $userinfo = '';
|
Chris@18
|
839 var $backtrace = null;
|
Chris@18
|
840
|
Chris@18
|
841 /**
|
Chris@18
|
842 * PEAR_Error constructor
|
Chris@18
|
843 *
|
Chris@18
|
844 * @param string $message message
|
Chris@18
|
845 *
|
Chris@18
|
846 * @param int $code (optional) error code
|
Chris@18
|
847 *
|
Chris@18
|
848 * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
|
Chris@18
|
849 * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
|
Chris@18
|
850 * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
|
Chris@18
|
851 *
|
Chris@18
|
852 * @param mixed $options (optional) error level, _OR_ in the case of
|
Chris@18
|
853 * PEAR_ERROR_CALLBACK, the callback function or object/method
|
Chris@18
|
854 * tuple.
|
Chris@18
|
855 *
|
Chris@18
|
856 * @param string $userinfo (optional) additional user/debug info
|
Chris@18
|
857 *
|
Chris@18
|
858 * @access public
|
Chris@18
|
859 *
|
Chris@18
|
860 */
|
Chris@18
|
861 function __construct($message = 'unknown error', $code = null,
|
Chris@18
|
862 $mode = null, $options = null, $userinfo = null)
|
Chris@18
|
863 {
|
Chris@18
|
864 if ($mode === null) {
|
Chris@18
|
865 $mode = PEAR_ERROR_RETURN;
|
Chris@18
|
866 }
|
Chris@18
|
867 $this->message = $message;
|
Chris@18
|
868 $this->code = $code;
|
Chris@18
|
869 $this->mode = $mode;
|
Chris@18
|
870 $this->userinfo = $userinfo;
|
Chris@18
|
871
|
Chris@18
|
872 $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
|
Chris@18
|
873
|
Chris@18
|
874 if (!$skiptrace) {
|
Chris@18
|
875 $this->backtrace = debug_backtrace();
|
Chris@18
|
876 if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
|
Chris@18
|
877 unset($this->backtrace[0]['object']);
|
Chris@18
|
878 }
|
Chris@18
|
879 }
|
Chris@18
|
880
|
Chris@18
|
881 if ($mode & PEAR_ERROR_CALLBACK) {
|
Chris@18
|
882 $this->level = E_USER_NOTICE;
|
Chris@18
|
883 $this->callback = $options;
|
Chris@18
|
884 } else {
|
Chris@18
|
885 if ($options === null) {
|
Chris@18
|
886 $options = E_USER_NOTICE;
|
Chris@18
|
887 }
|
Chris@18
|
888
|
Chris@18
|
889 $this->level = $options;
|
Chris@18
|
890 $this->callback = null;
|
Chris@18
|
891 }
|
Chris@18
|
892
|
Chris@18
|
893 if ($this->mode & PEAR_ERROR_PRINT) {
|
Chris@18
|
894 if (is_null($options) || is_int($options)) {
|
Chris@18
|
895 $format = "%s";
|
Chris@18
|
896 } else {
|
Chris@18
|
897 $format = $options;
|
Chris@18
|
898 }
|
Chris@18
|
899
|
Chris@18
|
900 printf($format, $this->getMessage());
|
Chris@18
|
901 }
|
Chris@18
|
902
|
Chris@18
|
903 if ($this->mode & PEAR_ERROR_TRIGGER) {
|
Chris@18
|
904 trigger_error($this->getMessage(), $this->level);
|
Chris@18
|
905 }
|
Chris@18
|
906
|
Chris@18
|
907 if ($this->mode & PEAR_ERROR_DIE) {
|
Chris@18
|
908 $msg = $this->getMessage();
|
Chris@18
|
909 if (is_null($options) || is_int($options)) {
|
Chris@18
|
910 $format = "%s";
|
Chris@18
|
911 if (substr($msg, -1) != "\n") {
|
Chris@18
|
912 $msg .= "\n";
|
Chris@18
|
913 }
|
Chris@18
|
914 } else {
|
Chris@18
|
915 $format = $options;
|
Chris@18
|
916 }
|
Chris@18
|
917 printf($format, $msg);
|
Chris@18
|
918 exit($code);
|
Chris@18
|
919 }
|
Chris@18
|
920
|
Chris@18
|
921 if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) {
|
Chris@18
|
922 call_user_func($this->callback, $this);
|
Chris@18
|
923 }
|
Chris@18
|
924
|
Chris@18
|
925 if ($this->mode & PEAR_ERROR_EXCEPTION) {
|
Chris@18
|
926 trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
|
Chris@18
|
927 eval('$e = new Exception($this->message, $this->code);throw($e);');
|
Chris@18
|
928 }
|
Chris@18
|
929 }
|
Chris@18
|
930
|
Chris@18
|
931 /**
|
Chris@18
|
932 * Only here for backwards compatibility.
|
Chris@18
|
933 *
|
Chris@18
|
934 * Class "Cache_Error" still uses it, among others.
|
Chris@18
|
935 *
|
Chris@18
|
936 * @param string $message Message
|
Chris@18
|
937 * @param int $code Error code
|
Chris@18
|
938 * @param int $mode Error mode
|
Chris@18
|
939 * @param mixed $options See __construct()
|
Chris@18
|
940 * @param string $userinfo Additional user/debug info
|
Chris@18
|
941 */
|
Chris@18
|
942 public function PEAR_Error(
|
Chris@18
|
943 $message = 'unknown error', $code = null, $mode = null,
|
Chris@18
|
944 $options = null, $userinfo = null
|
Chris@18
|
945 ) {
|
Chris@18
|
946 self::__construct($message, $code, $mode, $options, $userinfo);
|
Chris@18
|
947 }
|
Chris@18
|
948
|
Chris@18
|
949 /**
|
Chris@18
|
950 * Get the error mode from an error object.
|
Chris@18
|
951 *
|
Chris@18
|
952 * @return int error mode
|
Chris@18
|
953 * @access public
|
Chris@18
|
954 */
|
Chris@18
|
955 function getMode()
|
Chris@18
|
956 {
|
Chris@18
|
957 return $this->mode;
|
Chris@18
|
958 }
|
Chris@18
|
959
|
Chris@18
|
960 /**
|
Chris@18
|
961 * Get the callback function/method from an error object.
|
Chris@18
|
962 *
|
Chris@18
|
963 * @return mixed callback function or object/method array
|
Chris@18
|
964 * @access public
|
Chris@18
|
965 */
|
Chris@18
|
966 function getCallback()
|
Chris@18
|
967 {
|
Chris@18
|
968 return $this->callback;
|
Chris@18
|
969 }
|
Chris@18
|
970
|
Chris@18
|
971 /**
|
Chris@18
|
972 * Get the error message from an error object.
|
Chris@18
|
973 *
|
Chris@18
|
974 * @return string full error message
|
Chris@18
|
975 * @access public
|
Chris@18
|
976 */
|
Chris@18
|
977 function getMessage()
|
Chris@18
|
978 {
|
Chris@18
|
979 return ($this->error_message_prefix . $this->message);
|
Chris@18
|
980 }
|
Chris@18
|
981
|
Chris@18
|
982 /**
|
Chris@18
|
983 * Get error code from an error object
|
Chris@18
|
984 *
|
Chris@18
|
985 * @return int error code
|
Chris@18
|
986 * @access public
|
Chris@18
|
987 */
|
Chris@18
|
988 function getCode()
|
Chris@18
|
989 {
|
Chris@18
|
990 return $this->code;
|
Chris@18
|
991 }
|
Chris@18
|
992
|
Chris@18
|
993 /**
|
Chris@18
|
994 * Get the name of this error/exception.
|
Chris@18
|
995 *
|
Chris@18
|
996 * @return string error/exception name (type)
|
Chris@18
|
997 * @access public
|
Chris@18
|
998 */
|
Chris@18
|
999 function getType()
|
Chris@18
|
1000 {
|
Chris@18
|
1001 return get_class($this);
|
Chris@18
|
1002 }
|
Chris@18
|
1003
|
Chris@18
|
1004 /**
|
Chris@18
|
1005 * Get additional user-supplied information.
|
Chris@18
|
1006 *
|
Chris@18
|
1007 * @return string user-supplied information
|
Chris@18
|
1008 * @access public
|
Chris@18
|
1009 */
|
Chris@18
|
1010 function getUserInfo()
|
Chris@18
|
1011 {
|
Chris@18
|
1012 return $this->userinfo;
|
Chris@18
|
1013 }
|
Chris@18
|
1014
|
Chris@18
|
1015 /**
|
Chris@18
|
1016 * Get additional debug information supplied by the application.
|
Chris@18
|
1017 *
|
Chris@18
|
1018 * @return string debug information
|
Chris@18
|
1019 * @access public
|
Chris@18
|
1020 */
|
Chris@18
|
1021 function getDebugInfo()
|
Chris@18
|
1022 {
|
Chris@18
|
1023 return $this->getUserInfo();
|
Chris@18
|
1024 }
|
Chris@18
|
1025
|
Chris@18
|
1026 /**
|
Chris@18
|
1027 * Get the call backtrace from where the error was generated.
|
Chris@18
|
1028 * Supported with PHP 4.3.0 or newer.
|
Chris@18
|
1029 *
|
Chris@18
|
1030 * @param int $frame (optional) what frame to fetch
|
Chris@18
|
1031 * @return array Backtrace, or NULL if not available.
|
Chris@18
|
1032 * @access public
|
Chris@18
|
1033 */
|
Chris@18
|
1034 function getBacktrace($frame = null)
|
Chris@18
|
1035 {
|
Chris@18
|
1036 if (defined('PEAR_IGNORE_BACKTRACE')) {
|
Chris@18
|
1037 return null;
|
Chris@18
|
1038 }
|
Chris@18
|
1039 if ($frame === null) {
|
Chris@18
|
1040 return $this->backtrace;
|
Chris@18
|
1041 }
|
Chris@18
|
1042 return $this->backtrace[$frame];
|
Chris@18
|
1043 }
|
Chris@18
|
1044
|
Chris@18
|
1045 function addUserInfo($info)
|
Chris@18
|
1046 {
|
Chris@18
|
1047 if (empty($this->userinfo)) {
|
Chris@18
|
1048 $this->userinfo = $info;
|
Chris@18
|
1049 } else {
|
Chris@18
|
1050 $this->userinfo .= " ** $info";
|
Chris@18
|
1051 }
|
Chris@18
|
1052 }
|
Chris@18
|
1053
|
Chris@18
|
1054 function __toString()
|
Chris@18
|
1055 {
|
Chris@18
|
1056 return $this->getMessage();
|
Chris@18
|
1057 }
|
Chris@18
|
1058
|
Chris@18
|
1059 /**
|
Chris@18
|
1060 * Make a string representation of this object.
|
Chris@18
|
1061 *
|
Chris@18
|
1062 * @return string a string with an object summary
|
Chris@18
|
1063 * @access public
|
Chris@18
|
1064 */
|
Chris@18
|
1065 function toString()
|
Chris@18
|
1066 {
|
Chris@18
|
1067 $modes = array();
|
Chris@18
|
1068 $levels = array(E_USER_NOTICE => 'notice',
|
Chris@18
|
1069 E_USER_WARNING => 'warning',
|
Chris@18
|
1070 E_USER_ERROR => 'error');
|
Chris@18
|
1071 if ($this->mode & PEAR_ERROR_CALLBACK) {
|
Chris@18
|
1072 if (is_array($this->callback)) {
|
Chris@18
|
1073 $callback = (is_object($this->callback[0]) ?
|
Chris@18
|
1074 strtolower(get_class($this->callback[0])) :
|
Chris@18
|
1075 $this->callback[0]) . '::' .
|
Chris@18
|
1076 $this->callback[1];
|
Chris@18
|
1077 } else {
|
Chris@18
|
1078 $callback = $this->callback;
|
Chris@18
|
1079 }
|
Chris@18
|
1080 return sprintf('[%s: message="%s" code=%d mode=callback '.
|
Chris@18
|
1081 'callback=%s prefix="%s" info="%s"]',
|
Chris@18
|
1082 strtolower(get_class($this)), $this->message, $this->code,
|
Chris@18
|
1083 $callback, $this->error_message_prefix,
|
Chris@18
|
1084 $this->userinfo);
|
Chris@18
|
1085 }
|
Chris@18
|
1086 if ($this->mode & PEAR_ERROR_PRINT) {
|
Chris@18
|
1087 $modes[] = 'print';
|
Chris@18
|
1088 }
|
Chris@18
|
1089 if ($this->mode & PEAR_ERROR_TRIGGER) {
|
Chris@18
|
1090 $modes[] = 'trigger';
|
Chris@18
|
1091 }
|
Chris@18
|
1092 if ($this->mode & PEAR_ERROR_DIE) {
|
Chris@18
|
1093 $modes[] = 'die';
|
Chris@18
|
1094 }
|
Chris@18
|
1095 if ($this->mode & PEAR_ERROR_RETURN) {
|
Chris@18
|
1096 $modes[] = 'return';
|
Chris@18
|
1097 }
|
Chris@18
|
1098 return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
|
Chris@18
|
1099 'prefix="%s" info="%s"]',
|
Chris@18
|
1100 strtolower(get_class($this)), $this->message, $this->code,
|
Chris@18
|
1101 implode("|", $modes), $levels[$this->level],
|
Chris@18
|
1102 $this->error_message_prefix,
|
Chris@18
|
1103 $this->userinfo);
|
Chris@18
|
1104 }
|
Chris@18
|
1105 }
|
Chris@18
|
1106
|
Chris@18
|
1107 /*
|
Chris@18
|
1108 * Local Variables:
|
Chris@18
|
1109 * mode: php
|
Chris@18
|
1110 * tab-width: 4
|
Chris@18
|
1111 * c-basic-offset: 4
|
Chris@18
|
1112 * End:
|
Chris@18
|
1113 */
|