Debian Codegraph @

vendor/pear/pear-core-minimal/src/PEAR.php

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