PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file PEAR.php

Documentation is available at PEAR.php

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

Documentation generated on Mon, 11 Mar 2019 14:23:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.