Project

General

Profile

Download (33.9 KB) Statistics
| Branch: | Tag: | Revision:
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
 * LICENSE: This source file is subject to version 3.0 of the PHP license
10
 * that is available through the world-wide-web at the following URI:
11
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
12
 * the PHP License and are unable to obtain it through the web, please
13
 * send a note to license@php.net so we can mail you a copy immediately.
14
 *
15
 * @category   pear
16
 * @package    PEAR
17
 * @author     Sterling Hughes <sterling@php.net>
18
 * @author     Stig Bakken <ssb@php.net>
19
 * @author     Tomas V.V.Cox <cox@idecnet.com>
20
 * @author     Greg Beaver <cellog@php.net>
21
 * @copyright  1997-2005 The PHP Group
22
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
23
 * @version    CVS: $Id$
24
 * @link       http://pear.php.net/package/PEAR
25
 * @since      File available since Release 0.1
26
 */
27

    
28
/**#@+
29
 * ERROR constants
30
 */
31
define('PEAR_ERROR_RETURN',     1);
32
define('PEAR_ERROR_PRINT',      2);
33
define('PEAR_ERROR_TRIGGER',    4);
34
define('PEAR_ERROR_DIE',        8);
35
define('PEAR_ERROR_CALLBACK',  16);
36
/**
37
 * WARNING: obsolete
38
 * @deprecated
39
 */
40
define('PEAR_ERROR_EXCEPTION', 32);
41
/**#@-*/
42
define('PEAR_ZE2', (function_exists('version_compare') &&
43
                    version_compare(zend_version(), "2-dev", "ge")));
44

    
45
if (substr(PHP_OS, 0, 3) == 'WIN') {
46
    define('OS_WINDOWS', true);
47
    define('OS_UNIX',    false);
48
    define('PEAR_OS',    'Windows');
49
} else {
50
    define('OS_WINDOWS', false);
51
    define('OS_UNIX',    true);
52
    define('PEAR_OS',    'Unix'); // blatant assumption
53
}
54

    
55
// instant backwards compatibility
56
if (!defined('PATH_SEPARATOR')) {
57
    if (OS_WINDOWS) {
58
        define('PATH_SEPARATOR', ';');
59
    } else {
60
        define('PATH_SEPARATOR', ':');
61
    }
62
}
63

    
64
$GLOBALS['_PEAR_default_error_mode']     = PEAR_ERROR_RETURN;
65
$GLOBALS['_PEAR_default_error_options']  = E_USER_NOTICE;
66
$GLOBALS['_PEAR_destructor_object_list'] = array();
67
$GLOBALS['_PEAR_shutdown_funcs']         = array();
68
$GLOBALS['_PEAR_error_handler_stack']    = array();
69

    
70
@ini_set('track_errors', true);
71

    
72
/**
73
 * Base class for other PEAR classes.  Provides rudimentary
74
 * emulation of destructors.
75
 *
76
 * If you want a destructor in your class, inherit PEAR and make a
77
 * destructor method called _yourclassname (same name as the
78
 * constructor, but with a "_" prefix).  Also, in your constructor you
79
 * have to call the PEAR constructor: $this->PEAR();.
80
 * The destructor method will be called without parameters.  Note that
81
 * at in some SAPI implementations (such as Apache), any output during
82
 * the request shutdown (in which destructors are called) seems to be
83
 * discarded.  If you need to get any debug information from your
84
 * destructor, use error_log(), syslog() or something similar.
85
 *
86
 * IMPORTANT! To use the emulated destructors you need to create the
87
 * objects by reference: $obj =& new PEAR_child;
88
 *
89
 * @category   pear
90
 * @package    PEAR
91
 * @author     Stig Bakken <ssb@php.net>
92
 * @author     Tomas V.V. Cox <cox@idecnet.com>
93
 * @author     Greg Beaver <cellog@php.net>
94
 * @copyright  1997-2005 The PHP Group
95
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
96
 * @version    Release: 1.4.0a8
97
 * @link       http://pear.php.net/package/PEAR
98
 * @see        PEAR_Error
99
 * @since      Class available since PHP 4.0.2
100
 * @link        http://pear.php.net/manual/en/core.pear.php#core.pear.pear
101
 */
102
class PEAR
103
{
104
    // {{{ properties
105

    
106
    /**
107
     * Whether to enable internal debug messages.
108
     *
109
     * @var     bool
110
     * @access  private
111
     */
112
    var $_debug = false;
113

    
114
    /**
115
     * Default error mode for this object.
116
     *
117
     * @var     int
118
     * @access  private
119
     */
120
    var $_default_error_mode = null;
121

    
122
    /**
123
     * Default error options used for this object when error mode
124
     * is PEAR_ERROR_TRIGGER.
125
     *
126
     * @var     int
127
     * @access  private
128
     */
129
    var $_default_error_options = null;
130

    
131
    /**
132
     * Default error handler (callback) for this object, if error mode is
133
     * PEAR_ERROR_CALLBACK.
134
     *
135
     * @var     string
136
     * @access  private
137
     */
138
    var $_default_error_handler = '';
139

    
140
    /**
141
     * Which class to use for error objects.
142
     *
143
     * @var     string
144
     * @access  private
145
     */
146
    var $_error_class = 'PEAR_Error';
147

    
148
    /**
149
     * An array of expected errors.
150
     *
151
     * @var     array
152
     * @access  private
153
     */
154
    var $_expected_errors = array();
155

    
156
    // }}}
157

    
158
    // {{{ constructor
159

    
160
    /**
161
     * Constructor.  Registers this object in
162
     * $_PEAR_destructor_object_list for destructor emulation if a
163
     * destructor object exists.
164
     *
165
     * @param string $error_class  (optional) which class to use for
166
     *        error objects, defaults to PEAR_Error.
167
     * @access public
168
     * @return void
169
     */
170
    function PEAR($error_class = null)
171
    {
172
        $classname = strtolower(get_class($this));
173
        if ($this->_debug) {
174
            print "PEAR constructor called, class=$classname\n";
175
        }
176
        if ($error_class !== null) {
177
            $this->_error_class = $error_class;
178
        }
179
        while ($classname && strcasecmp($classname, "pear")) {
180
            $destructor = "_$classname";
181
            if (method_exists($this, $destructor)) {
182
                global $_PEAR_destructor_object_list;
183
                $_PEAR_destructor_object_list[] = &$this;
184
                if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
185
                    register_shutdown_function("_PEAR_call_destructors");
186
                    $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
187
                }
188
                break;
189
            } else {
190
                $classname = get_parent_class($classname);
191
            }
192
        }
193
    }
194

    
195
    // }}}
196
    // {{{ destructor
197

    
198
    /**
199
     * Destructor (the emulated type of...).  Does nothing right now,
200
     * but is included for forward compatibility, so subclass
201
     * destructors should always call it.
202
     *
203
     * See the note in the class desciption about output from
204
     * destructors.
205
     *
206
     * @access public
207
     * @return void
208
     */
209
    function _PEAR() {
210
        if ($this->_debug) {
211
            printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
212
        }
213
    }
214

    
215
    // }}}
216
    // {{{ getStaticProperty()
217

    
218
    /**
219
    * If you have a class that's mostly/entirely static, and you need static
220
    * properties, you can use this method to simulate them. Eg. in your method(s)
221
    * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
222
    * You MUST use a reference, or they will not persist!
223
    *
224
    * @access public
225
    * @param  string $class  The calling classname, to prevent clashes
226
    * @param  string $var    The variable to retrieve.
227
    * @return mixed   A reference to the variable. If not set it will be
228
    *                 auto initialised to NULL.
229
    */
230
    function &getStaticProperty($class, $var)
231
    {
232
        static $properties;
233
        return $properties[$class][$var];
234
    }
235

    
236
    // }}}
237
    // {{{ registerShutdownFunc()
238

    
239
    /**
240
    * Use this function to register a shutdown method for static
241
    * classes.
242
    *
243
    * @access public
244
    * @param  mixed $func  The function name (or array of class/method) to call
245
    * @param  mixed $args  The arguments to pass to the function
246
    * @return void
247
    */
248
    function registerShutdownFunc($func, $args = array())
249
    {
250
        $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
251
    }
252

    
253
    // }}}
254
    // {{{ isError()
255

    
256
    /**
257
     * Tell whether a value is a PEAR error.
258
     *
259
     * @param   mixed $data   the value to test
260
     * @param   int   $code   if $data is an error object, return true
261
     *                        only if $code is a string and
262
     *                        $obj->getMessage() == $code or
263
     *                        $code is an integer and $obj->getCode() == $code
264
     * @access  public
265
     * @return  bool    true if parameter is an error
266
     */
267
    function isError($data, $code = null)
268
    {
269
        if (is_a($data, 'PEAR_Error')) {
270
            if (is_null($code)) {
271
                return true;
272
            } elseif (is_string($code)) {
273
                return $data->getMessage() == $code;
274
            } else {
275
                return $data->getCode() == $code;
276
            }
277
        }
278
        return false;
279
    }
280

    
281
    // }}}
282
    // {{{ setErrorHandling()
283

    
284
    /**
285
     * Sets how errors generated by this object should be handled.
286
     * Can be invoked both in objects and statically.  If called
287
     * statically, setErrorHandling sets the default behaviour for all
288
     * PEAR objects.  If called in an object, setErrorHandling sets
289
     * the default behaviour for that object.
290
     *
291
     * @param int $mode
292
     *        One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
293
     *        PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
294
     *        PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
295
     *
296
     * @param mixed $options
297
     *        When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
298
     *        of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
299
     *
300
     *        When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
301
     *        to be the callback function or method.  A callback
302
     *        function is a string with the name of the function, a
303
     *        callback method is an array of two elements: the element
304
     *        at index 0 is the object, and the element at index 1 is
305
     *        the name of the method to call in the object.
306
     *
307
     *        When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
308
     *        a printf format string used when printing the error
309
     *        message.
310
     *
311
     * @access public
312
     * @return void
313
     * @see PEAR_ERROR_RETURN
314
     * @see PEAR_ERROR_PRINT
315
     * @see PEAR_ERROR_TRIGGER
316
     * @see PEAR_ERROR_DIE
317
     * @see PEAR_ERROR_CALLBACK
318
     * @see PEAR_ERROR_EXCEPTION
319
     *
320
     * @since PHP 4.0.5
321
     */
322

    
323
    function setErrorHandling($mode = null, $options = null)
324
    {
325
        if (isset($this) && is_a($this, 'PEAR')) {
326
            $setmode     = &$this->_default_error_mode;
327
            $setoptions  = &$this->_default_error_options;
328
        } else {
329
            $setmode     = &$GLOBALS['_PEAR_default_error_mode'];
330
            $setoptions  = &$GLOBALS['_PEAR_default_error_options'];
331
        }
332

    
333
        switch ($mode) {
334
            case PEAR_ERROR_EXCEPTION:
335
            case PEAR_ERROR_RETURN:
336
            case PEAR_ERROR_PRINT:
337
            case PEAR_ERROR_TRIGGER:
338
            case PEAR_ERROR_DIE:
339
            case null:
340
                $setmode = $mode;
341
                $setoptions = $options;
342
                break;
343

    
344
            case PEAR_ERROR_CALLBACK:
345
                $setmode = $mode;
346
                // class/object method callback
347
                if (is_callable($options)) {
348
                    $setoptions = $options;
349
                } else {
350
                    trigger_error("invalid error callback", E_USER_WARNING);
351
                }
352
                break;
353

    
354
            default:
355
                trigger_error("invalid error mode", E_USER_WARNING);
356
                break;
357
        }
358
    }
359

    
360
    // }}}
361
    // {{{ expectError()
362

    
363
    /**
364
     * This method is used to tell which errors you expect to get.
365
     * Expected errors are always returned with error mode
366
     * PEAR_ERROR_RETURN.  Expected error codes are stored in a stack,
367
     * and this method pushes a new element onto it.  The list of
368
     * expected errors are in effect until they are popped off the
369
     * stack with the popExpect() method.
370
     *
371
     * Note that this method can not be called statically
372
     *
373
     * @param mixed $code a single error code or an array of error codes to expect
374
     *
375
     * @return int     the new depth of the "expected errors" stack
376
     * @access public
377
     */
378
    function expectError($code = '*')
379
    {
380
        if (is_array($code)) {
381
            array_push($this->_expected_errors, $code);
382
        } else {
383
            array_push($this->_expected_errors, array($code));
384
        }
385
        return sizeof($this->_expected_errors);
386
    }
387

    
388
    // }}}
389
    // {{{ popExpect()
390

    
391
    /**
392
     * This method pops one element off the expected error codes
393
     * stack.
394
     *
395
     * @return array   the list of error codes that were popped
396
     */
397
    function popExpect()
398
    {
399
        return array_pop($this->_expected_errors);
400
    }
401

    
402
    // }}}
403
    // {{{ _checkDelExpect()
404

    
405
    /**
406
     * This method checks unsets an error code if available
407
     *
408
     * @param mixed error code
409
     * @return bool true if the error code was unset, false otherwise
410
     * @access private
411
     * @since PHP 4.3.0
412
     */
413
    function _checkDelExpect($error_code)
414
    {
415
        $deleted = false;
416

    
417
        foreach ($this->_expected_errors AS $key => $error_array) {
418
            if (in_array($error_code, $error_array)) {
419
                unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
420
                $deleted = true;
421
            }
422

    
423
            // clean up empty arrays
424
            if (0 == count($this->_expected_errors[$key])) {
425
                unset($this->_expected_errors[$key]);
426
            }
427
        }
428
        return $deleted;
429
    }
430

    
431
    // }}}
432
    // {{{ delExpect()
433

    
434
    /**
435
     * This method deletes all occurences of the specified element from
436
     * the expected error codes stack.
437
     *
438
     * @param  mixed $error_code error code that should be deleted
439
     * @return mixed list of error codes that were deleted or error
440
     * @access public
441
     * @since PHP 4.3.0
442
     */
443
    function delExpect($error_code)
444
    {
445
        $deleted = false;
446

    
447
        if ((is_array($error_code) && (0 != count($error_code)))) {
448
            // $error_code is a non-empty array here;
449
            // we walk through it trying to unset all
450
            // values
451
            foreach($error_code as $key => $error) {
452
                if ($this->_checkDelExpect($error)) {
453
                    $deleted =  true;
454
                } else {
455
                    $deleted = false;
456
                }
457
            }
458
            return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
459
        } elseif (!empty($error_code)) {
460
            // $error_code comes alone, trying to unset it
461
            if ($this->_checkDelExpect($error_code)) {
462
                return true;
463
            } else {
464
                return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
465
            }
466
        } else {
467
            // $error_code is empty
468
            return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
469
        }
470
    }
471

    
472
    // }}}
473
    // {{{ raiseError()
474

    
475
    /**
476
     * This method is a wrapper that returns an instance of the
477
     * configured error class with this object's default error
478
     * handling applied.  If the $mode and $options parameters are not
479
     * specified, the object's defaults are used.
480
     *
481
     * @param mixed $message a text error message or a PEAR error object
482
     *
483
     * @param int $code      a numeric error code (it is up to your class
484
     *                  to define these if you want to use codes)
485
     *
486
     * @param int $mode      One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
487
     *                  PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
488
     *                  PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
489
     *
490
     * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
491
     *                  specifies the PHP-internal error level (one of
492
     *                  E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
493
     *                  If $mode is PEAR_ERROR_CALLBACK, this
494
     *                  parameter specifies the callback function or
495
     *                  method.  In other error modes this parameter
496
     *                  is ignored.
497
     *
498
     * @param string $userinfo If you need to pass along for example debug
499
     *                  information, this parameter is meant for that.
500
     *
501
     * @param string $error_class The returned error object will be
502
     *                  instantiated from this class, if specified.
503
     *
504
     * @param bool $skipmsg If true, raiseError will only pass error codes,
505
     *                  the error message parameter will be dropped.
506
     *
507
     * @access public
508
     * @return object   a PEAR error object
509
     * @see PEAR::setErrorHandling
510
     * @since PHP 4.0.5
511
     */
512
    function raiseError($message = null,
513
                         $code = null,
514
                         $mode = null,
515
                         $options = null,
516
                         $userinfo = null,
517
                         $error_class = null,
518
                         $skipmsg = false)
519
    {
520
        // The error is yet a PEAR error object
521
        if (is_object($message)) {
522
            $code        = $message->getCode();
523
            $userinfo    = $message->getUserInfo();
524
            $error_class = $message->getType();
525
            $message->error_message_prefix = '';
526
            $message     = $message->getMessage();
527
        }
528

    
529
        if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
530
            if ($exp[0] == "*" ||
531
                (is_int(reset($exp)) && in_array($code, $exp)) ||
532
                (is_string(reset($exp)) && in_array($message, $exp))) {
533
                $mode = PEAR_ERROR_RETURN;
534
            }
535
        }
536
        // No mode given, try global ones
537
        if ($mode === null) {
538
            // Class error handler
539
            if (isset($this) && isset($this->_default_error_mode)) {
540
                $mode    = $this->_default_error_mode;
541
                $options = $this->_default_error_options;
542
            // Global error handler
543
            } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
544
                $mode    = $GLOBALS['_PEAR_default_error_mode'];
545
                $options = $GLOBALS['_PEAR_default_error_options'];
546
            }
547
        }
548

    
549
        if ($error_class !== null) {
550
            $ec = $error_class;
551
        } elseif (isset($this) && isset($this->_error_class)) {
552
            $ec = $this->_error_class;
553
        } else {
554
            $ec = 'PEAR_Error';
555
        }
556
        if ($skipmsg) {
557
            return new $ec($code, $mode, $options, $userinfo);
558
        } else {
559
            return new $ec($message, $code, $mode, $options, $userinfo);
560
        }
561
    }
562

    
563
    // }}}
564
    // {{{ throwError()
565

    
566
    /**
567
     * Simpler form of raiseError with fewer options.  In most cases
568
     * message, code and userinfo are enough.
569
     *
570
     * @param string $message
571
     *
572
     */
573
    function throwError($message = null,
574
                         $code = null,
575
                         $userinfo = null)
576
    {
577
        if (isset($this) && is_a($this, 'PEAR')) {
578
            return $this->raiseError($message, $code, null, null, $userinfo);
579
        } else {
580
            return PEAR::raiseError($message, $code, null, null, $userinfo);
581
        }
582
    }
583

    
584
    // }}}
585
    function staticPushErrorHandling($mode, $options = null)
586
    {
587
        $stack = &$GLOBALS['_PEAR_error_handler_stack'];
588
        $def_mode    = &$GLOBALS['_PEAR_default_error_mode'];
589
        $def_options = &$GLOBALS['_PEAR_default_error_options'];
590
        $stack[] = array($def_mode, $def_options);
591
        switch ($mode) {
592
            case PEAR_ERROR_EXCEPTION:
593
            case PEAR_ERROR_RETURN:
594
            case PEAR_ERROR_PRINT:
595
            case PEAR_ERROR_TRIGGER:
596
            case PEAR_ERROR_DIE:
597
            case null:
598
                $def_mode = $mode;
599
                $def_options = $options;
600
                break;
601

    
602
            case PEAR_ERROR_CALLBACK:
603
                $def_mode = $mode;
604
                // class/object method callback
605
                if (is_callable($options)) {
606
                    $def_options = $options;
607
                } else {
608
                    trigger_error("invalid error callback", E_USER_WARNING);
609
                }
610
                break;
611

    
612
            default:
613
                trigger_error("invalid error mode", E_USER_WARNING);
614
                break;
615
        }
616
        $stack[] = array($mode, $options);
617
        return true;
618
    }
619

    
620
    function staticPopErrorHandling()
621
    {
622
        $stack = &$GLOBALS['_PEAR_error_handler_stack'];
623
        $setmode     = &$GLOBALS['_PEAR_default_error_mode'];
624
        $setoptions  = &$GLOBALS['_PEAR_default_error_options'];
625
        array_pop($stack);
626
        list($mode, $options) = $stack[sizeof($stack) - 1];
627
        array_pop($stack);
628
        switch ($mode) {
629
            case PEAR_ERROR_EXCEPTION:
630
            case PEAR_ERROR_RETURN:
631
            case PEAR_ERROR_PRINT:
632
            case PEAR_ERROR_TRIGGER:
633
            case PEAR_ERROR_DIE:
634
            case null:
635
                $setmode = $mode;
636
                $setoptions = $options;
637
                break;
638

    
639
            case PEAR_ERROR_CALLBACK:
640
                $setmode = $mode;
641
                // class/object method callback
642
                if (is_callable($options)) {
643
                    $setoptions = $options;
644
                } else {
645
                    trigger_error("invalid error callback", E_USER_WARNING);
646
                }
647
                break;
648

    
649
            default:
650
                trigger_error("invalid error mode", E_USER_WARNING);
651
                break;
652
        }
653
        return true;
654
    }
655

    
656
    // {{{ pushErrorHandling()
657

    
658
    /**
659
     * Push a new error handler on top of the error handler options stack. With this
660
     * you can easily override the actual error handler for some code and restore
661
     * it later with popErrorHandling.
662
     *
663
     * @param mixed $mode (same as setErrorHandling)
664
     * @param mixed $options (same as setErrorHandling)
665
     *
666
     * @return bool Always true
667
     *
668
     * @see PEAR::setErrorHandling
669
     */
670
    function pushErrorHandling($mode, $options = null)
671
    {
672
        $stack = &$GLOBALS['_PEAR_error_handler_stack'];
673
        if (isset($this) && is_a($this, 'PEAR')) {
674
            $def_mode    = &$this->_default_error_mode;
675
            $def_options = &$this->_default_error_options;
676
        } else {
677
            $def_mode    = &$GLOBALS['_PEAR_default_error_mode'];
678
            $def_options = &$GLOBALS['_PEAR_default_error_options'];
679
        }
680
        $stack[] = array($def_mode, $def_options);
681

    
682
        if (isset($this) && is_a($this, 'PEAR')) {
683
            $this->setErrorHandling($mode, $options);
684
        } else {
685
            PEAR::setErrorHandling($mode, $options);
686
        }
687
        $stack[] = array($mode, $options);
688
        return true;
689
    }
690

    
691
    // }}}
692
    // {{{ popErrorHandling()
693

    
694
    /**
695
    * Pop the last error handler used
696
    *
697
    * @return bool Always true
698
    *
699
    * @see PEAR::pushErrorHandling
700
    */
701
    function popErrorHandling()
702
    {
703
        $stack = &$GLOBALS['_PEAR_error_handler_stack'];
704
        array_pop($stack);
705
        list($mode, $options) = $stack[sizeof($stack) - 1];
706
        array_pop($stack);
707
        if (isset($this) && is_a($this, 'PEAR')) {
708
            $this->setErrorHandling($mode, $options);
709
        } else {
710
            PEAR::setErrorHandling($mode, $options);
711
        }
712
        return true;
713
    }
714

    
715
    // }}}
716
    // {{{ loadExtension()
717

    
718
    /**
719
    * OS independant PHP extension load. Remember to take care
720
    * on the correct extension name for case sensitive OSes.
721
    *
722
    * @param string $ext The extension name
723
    * @return bool Success or not on the dl() call
724
    */
725
    function loadExtension($ext)
726
    {
727
        if (!extension_loaded($ext)) {
728
            // if either returns true dl() will produce a FATAL error, stop that
729
            if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
730
                return false;
731
            }
732
            if (OS_WINDOWS) {
733
                $suffix = '.dll';
734
            } elseif (PHP_OS == 'HP-UX') {
735
                $suffix = '.sl';
736
            } elseif (PHP_OS == 'AIX') {
737
                $suffix = '.a';
738
            } elseif (PHP_OS == 'OSX') {
739
                $suffix = '.bundle';
740
            } else {
741
                $suffix = '.so';
742
            }
743
            return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
744
        }
745
        return true;
746
    }
747

    
748
    // }}}
749
}
750

    
751
// {{{ _PEAR_call_destructors()
752

    
753
function _PEAR_call_destructors()
754
{
755
    global $_PEAR_destructor_object_list;
756
    if (is_array($_PEAR_destructor_object_list) &&
757
        sizeof($_PEAR_destructor_object_list))
758
    {
759
        reset($_PEAR_destructor_object_list);
760
        while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
761
            $classname = get_class($objref);
762
            while ($classname) {
763
                $destructor = "_$classname";
764
                if (method_exists($objref, $destructor)) {
765
                    $objref->$destructor();
766
                    break;
767
                } else {
768
                    $classname = get_parent_class($classname);
769
                }
770
            }
771
        }
772
        // Empty the object list to ensure that destructors are
773
        // not called more than once.
774
        $_PEAR_destructor_object_list = array();
775
    }
776

    
777
    // Now call the shutdown functions
778
    if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
779
        foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
780
            call_user_func_array($value[0], $value[1]);
781
        }
782
    }
783
}
784

    
785
// }}}
786
/**
787
 * Standard PEAR error class for PHP 4
788
 *
789
 * This class is supserseded by {@link PEAR_Exception} in PHP 5
790
 *
791
 * @category   pear
792
 * @package    PEAR
793
 * @author     Stig Bakken <ssb@php.net>
794
 * @author     Tomas V.V. Cox <cox@idecnet.com>
795
 * @author     Gregory Beaver <cellog@php.net>
796
 * @copyright  1997-2005 The PHP Group
797
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
798
 * @version    Release: 1.4.0a8
799
 * @link       http://pear.php.net/manual/en/core.pear.pear-error.php
800
 * @see        PEAR::raiseError(), PEAR::throwError()
801
 * @since      Class available since PHP 4.0.2
802
 */
803
class PEAR_Error
804
{
805
    // {{{ properties
806

    
807
    var $error_message_prefix = '';
808
    var $mode                 = PEAR_ERROR_RETURN;
809
    var $level                = E_USER_NOTICE;
810
    var $code                 = -1;
811
    var $message              = '';
812
    var $userinfo             = '';
813
    var $backtrace            = null;
814

    
815
    // }}}
816
    // {{{ constructor
817

    
818
    /**
819
     * PEAR_Error constructor
820
     *
821
     * @param string $message  message
822
     *
823
     * @param int $code     (optional) error code
824
     *
825
     * @param int $mode     (optional) error mode, one of: PEAR_ERROR_RETURN,
826
     * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
827
     * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
828
     *
829
     * @param mixed $options   (optional) error level, _OR_ in the case of
830
     * PEAR_ERROR_CALLBACK, the callback function or object/method
831
     * tuple.
832
     *
833
     * @param string $userinfo (optional) additional user/debug info
834
     *
835
     * @access public
836
     *
837
     */
838
    function PEAR_Error($message = 'unknown error', $code = null,
839
                        $mode = null, $options = null, $userinfo = null)
840
    {
841
        if ($mode === null) {
842
            $mode = PEAR_ERROR_RETURN;
843
        }
844
        $this->message   = $message;
845
        $this->code      = $code;
846
        $this->mode      = $mode;
847
        $this->userinfo  = $userinfo;
848
        if (function_exists("debug_backtrace") && !defined('PEAR_IGNORE_BACKTRACE')) {
849
            $this->backtrace = debug_backtrace();
850
        }
851
        if ($mode & PEAR_ERROR_CALLBACK) {
852
            $this->level = E_USER_NOTICE;
853
            $this->callback = $options;
854
        } else {
855
            if ($options === null) {
856
                $options = E_USER_NOTICE;
857
            }
858
            $this->level = $options;
859
            $this->callback = null;
860
        }
861
        if ($this->mode & PEAR_ERROR_PRINT) {
862
            if (is_null($options) || is_int($options)) {
863
                $format = "%s";
864
            } else {
865
                $format = $options;
866
            }
867
            printf($format, $this->getMessage());
868
        }
869
        if ($this->mode & PEAR_ERROR_TRIGGER) {
870
            trigger_error($this->getMessage(), $this->level);
871
        }
872
        if ($this->mode & PEAR_ERROR_DIE) {
873
            $msg = $this->getMessage();
874
            if (is_null($options) || is_int($options)) {
875
                $format = "%s";
876
                if (substr($msg, -1) != "\n") {
877
                    $msg .= "\n";
878
                }
879
            } else {
880
                $format = $options;
881
            }
882
            die(sprintf($format, $msg));
883
        }
884
        if ($this->mode & PEAR_ERROR_CALLBACK) {
885
            if (is_callable($this->callback)) {
886
                call_user_func($this->callback, $this);
887
            }
888
        }
889
        if ($this->mode & PEAR_ERROR_EXCEPTION) {
890
            trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_ErrorStack for exceptions", E_USER_WARNING);
891
            eval('$e = new Exception($this->message, $this->code);$e->PEAR_Error = $this;throw($e);');
892
        }
893
    }
894

    
895
    // }}}
896
    // {{{ getMode()
897

    
898
    /**
899
     * Get the error mode from an error object.
900
     *
901
     * @return int error mode
902
     * @access public
903
     */
904
    function getMode() {
905
        return $this->mode;
906
    }
907

    
908
    // }}}
909
    // {{{ getCallback()
910

    
911
    /**
912
     * Get the callback function/method from an error object.
913
     *
914
     * @return mixed callback function or object/method array
915
     * @access public
916
     */
917
    function getCallback() {
918
        return $this->callback;
919
    }
920

    
921
    // }}}
922
    // {{{ getMessage()
923

    
924

    
925
    /**
926
     * Get the error message from an error object.
927
     *
928
     * @return  string  full error message
929
     * @access public
930
     */
931
    function getMessage()
932
    {
933
        return ($this->error_message_prefix . $this->message);
934
    }
935

    
936

    
937
    // }}}
938
    // {{{ getCode()
939

    
940
    /**
941
     * Get error code from an error object
942
     *
943
     * @return int error code
944
     * @access public
945
     */
946
     function getCode()
947
     {
948
        return $this->code;
949
     }
950

    
951
    // }}}
952
    // {{{ getType()
953

    
954
    /**
955
     * Get the name of this error/exception.
956
     *
957
     * @return string error/exception name (type)
958
     * @access public
959
     */
960
    function getType()
961
    {
962
        return get_class($this);
963
    }
964

    
965
    // }}}
966
    // {{{ getUserInfo()
967

    
968
    /**
969
     * Get additional user-supplied information.
970
     *
971
     * @return string user-supplied information
972
     * @access public
973
     */
974
    function getUserInfo()
975
    {
976
        return $this->userinfo;
977
    }
978

    
979
    // }}}
980
    // {{{ getDebugInfo()
981

    
982
    /**
983
     * Get additional debug information supplied by the application.
984
     *
985
     * @return string debug information
986
     * @access public
987
     */
988
    function getDebugInfo()
989
    {
990
        return $this->getUserInfo();
991
    }
992

    
993
    // }}}
994
    // {{{ getBacktrace()
995

    
996
    /**
997
     * Get the call backtrace from where the error was generated.
998
     * Supported with PHP 4.3.0 or newer.
999
     *
1000
     * @param int $frame (optional) what frame to fetch
1001
     * @return array Backtrace, or NULL if not available.
1002
     * @access public
1003
     */
1004
    function getBacktrace($frame = null)
1005
    {
1006
        if (defined('PEAR_IGNORE_BACKTRACE')) {
1007
            return null;
1008
        }
1009
        if ($frame === null) {
1010
            return $this->backtrace;
1011
        }
1012
        return $this->backtrace[$frame];
1013
    }
1014

    
1015
    // }}}
1016
    // {{{ addUserInfo()
1017

    
1018
    function addUserInfo($info)
1019
    {
1020
        if (empty($this->userinfo)) {
1021
            $this->userinfo = $info;
1022
        } else {
1023
            $this->userinfo .= " ** $info";
1024
        }
1025
    }
1026

    
1027
    // }}}
1028
    // {{{ toString()
1029

    
1030
    /**
1031
     * Make a string representation of this object.
1032
     *
1033
     * @return string a string with an object summary
1034
     * @access public
1035
     */
1036
    function toString() {
1037
        $modes = array();
1038
        $levels = array(E_USER_NOTICE  => 'notice',
1039
                        E_USER_WARNING => 'warning',
1040
                        E_USER_ERROR   => 'error');
1041
        if ($this->mode & PEAR_ERROR_CALLBACK) {
1042
            if (is_array($this->callback)) {
1043
                $callback = (is_object($this->callback[0]) ?
1044
                    strtolower(get_class($this->callback[0])) :
1045
                    $this->callback[0]) . '::' .
1046
                    $this->callback[1];
1047
            } else {
1048
                $callback = $this->callback;
1049
            }
1050
            return sprintf('[%s: message="%s" code=%d mode=callback '.
1051
                           'callback=%s prefix="%s" info="%s"]',
1052
                           strtolower(get_class($this)), $this->message, $this->code,
1053
                           $callback, $this->error_message_prefix,
1054
                           $this->userinfo);
1055
        }
1056
        if ($this->mode & PEAR_ERROR_PRINT) {
1057
            $modes[] = 'print';
1058
        }
1059
        if ($this->mode & PEAR_ERROR_TRIGGER) {
1060
            $modes[] = 'trigger';
1061
        }
1062
        if ($this->mode & PEAR_ERROR_DIE) {
1063
            $modes[] = 'die';
1064
        }
1065
        if ($this->mode & PEAR_ERROR_RETURN) {
1066
            $modes[] = 'return';
1067
        }
1068
        return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
1069
                       'prefix="%s" info="%s"]',
1070
                       strtolower(get_class($this)), $this->message, $this->code,
1071
                       implode("|", $modes), $levels[$this->level],
1072
                       $this->error_message_prefix,
1073
                       $this->userinfo);
1074
    }
1075

    
1076
    // }}}
1077
}
1078

    
1079
/*
1080
 * Local Variables:
1081
 * mode: php
1082
 * tab-width: 4
1083
 * c-basic-offset: 4
1084
 * End:
1085
 */
1086
?>
(1-1/25)