PageRenderTime 121ms CodeModel.GetById 60ms app.highlight 33ms RepoModel.GetById 18ms app.codeStats 0ms

/htdocs/includes/nusoap/lib/Mail/PEAR.php

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