[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/bundled-libs/ -> PEAR.php (source)

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


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics