Subversion Repositories eFlore/Applications.cel

Rev

Rev 418 | Details | Compare with Previous | Last modification | View Log | RSS feed

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