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