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