Subversion Repositories Applications.gtt

Rev

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

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