[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/htdocs/includes/pear/ -> PEAR.php (source)

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


Généré le : Mon Nov 26 12:29:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics