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