root/trunk/scripts/jlogHTTP_Request.php

Revision 1736, 59.0 KB (checked in by jeena, 4 months ago)

changed PHP_SELF to SCRIPT_NAME

Line 
1<?php
2 /**
3  * PEAR, HTTP_Request, Net_Socket, Net_URL in one file for Jlog
4  *
5  * I have deleted all comments please download the PEAR packages to see
6  * the comments. I have added all code from the HTTP_Request.php,
7  * Net_Socket.php and URL.php into this file so it is easier to handle.
8  */
9
10/**
11 * PEAR, the PHP Extension and Application Repository
12 *
13 * PEAR class and PEAR_Error class
14 *
15 * PHP versions 4 and 5
16 *
17 * LICENSE: This source file is subject to version 3.0 of the PHP license
18 * that is available through the world-wide-web at the following URI:
19 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
20 * the PHP License and are unable to obtain it through the web, please
21 * send a note to license@php.net so we can mail you a copy immediately.
22 *
23 * @category   pear
24 * @package    PEAR
25 * @author     Sterling Hughes <sterling@php.net>
26 * @author     Stig Bakken <ssb@php.net>
27 * @author     Tomas V.V.Cox <cox@idecnet.com>
28 * @author     Greg Beaver <cellog@php.net>
29 * @copyright  1997-2005 The PHP Group
30 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
31 * @version    CVS: $Id: PEAR.php,v 1.96 2005/09/21 00:12:35 cellog Exp $
32 * @link       http://pear.php.net/package/PEAR
33 * @since      File available since Release 0.1
34 */
35
36define('PEAR_ERROR_RETURN',     1);
37define('PEAR_ERROR_PRINT',      2);
38define('PEAR_ERROR_TRIGGER',    4);
39define('PEAR_ERROR_DIE',        8);
40define('PEAR_ERROR_CALLBACK',  16);
41
42define('PEAR_ERROR_EXCEPTION', 32);
43
44define('PEAR_ZE2', (function_exists('version_compare') &&
45                    version_compare(zend_version(), "2-dev", "ge")));
46
47if (substr(PHP_OS, 0, 3) == 'WIN') {
48    define('OS_WINDOWS', true);
49    define('OS_UNIX',    false);
50    define('PEAR_OS',    'Windows');
51} else {
52    define('OS_WINDOWS', false);
53    define('OS_UNIX',    true);
54    define('PEAR_OS',    'Unix'); // blatant assumption
55}
56
57if (!defined('PATH_SEPARATOR')) {
58    if (OS_WINDOWS) {
59        define('PATH_SEPARATOR', ';');
60    } else {
61        define('PATH_SEPARATOR', ':');
62    }
63}
64
65$GLOBALS['_PEAR_default_error_mode']     = PEAR_ERROR_RETURN;
66$GLOBALS['_PEAR_default_error_options']  = E_USER_NOTICE;
67$GLOBALS['_PEAR_destructor_object_list'] = array();
68$GLOBALS['_PEAR_shutdown_funcs']         = array();
69$GLOBALS['_PEAR_error_handler_stack']    = array();
70
71@ini_set('track_errors', true);
72
73class PEAR
74{
75
76    var $_debug = false;
77    var $_default_error_mode = null;
78    var $_default_error_options = null;
79    var $_default_error_handler = '';
80    var $_error_class = 'PEAR_Error';
81    var $_expected_errors = array();
82
83    function PEAR($error_class = null)
84    {
85        $classname = strtolower(get_class($this));
86        if ($this->_debug) {
87            print "PEAR constructor called, class=$classname\n";
88        }
89        if ($error_class !== null) {
90            $this->_error_class = $error_class;
91        }
92        while ($classname && strcasecmp($classname, "pear")) {
93            $destructor = "_$classname";
94            if (method_exists($this, $destructor)) {
95                global $_PEAR_destructor_object_list;
96                $_PEAR_destructor_object_list[] = &$this;
97                if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
98                    register_shutdown_function("_PEAR_call_destructors");
99                    $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
100                }
101                break;
102            } else {
103                $classname = get_parent_class($classname);
104            }
105        }
106    }
107
108    function _PEAR() {
109        if ($this->_debug) {
110            printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
111        }
112    }
113
114    function &getStaticProperty($class, $var)
115    {
116        static $properties;
117        return $properties[$class][$var];
118    }
119
120    function registerShutdownFunc($func, $args = array())
121    {
122        $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
123    }
124
125    function isError($data, $code = null)
126    {
127        if (is_a($data, 'PEAR_Error')) {
128            if (is_null($code)) {
129                return true;
130            } elseif (is_string($code)) {
131                return $data->getMessage() == $code;
132            } else {
133                return $data->getCode() == $code;
134            }
135        }
136        return false;
137    }
138
139    function setErrorHandling($mode = null, $options = null)
140    {
141        if (isset($this) && is_a($this, 'PEAR')) {
142            $setmode     = &$this->_default_error_mode;
143            $setoptions  = &$this->_default_error_options;
144        } else {
145            $setmode     = &$GLOBALS['_PEAR_default_error_mode'];
146            $setoptions  = &$GLOBALS['_PEAR_default_error_options'];
147        }
148
149        switch ($mode) {
150            case PEAR_ERROR_EXCEPTION:
151            case PEAR_ERROR_RETURN:
152            case PEAR_ERROR_PRINT:
153            case PEAR_ERROR_TRIGGER:
154            case PEAR_ERROR_DIE:
155            case null:
156                $setmode = $mode;
157                $setoptions = $options;
158                break;
159
160            case PEAR_ERROR_CALLBACK:
161                $setmode = $mode;
162                // class/object method callback
163                if (is_callable($options)) {
164                    $setoptions = $options;
165                } else {
166                    trigger_error("invalid error callback", E_USER_WARNING);
167                }
168                break;
169
170            default:
171                trigger_error("invalid error mode", E_USER_WARNING);
172                break;
173        }
174    }
175
176    function expectError($code = '*')
177    {
178        if (is_array($code)) {
179            array_push($this->_expected_errors, $code);
180        } else {
181            array_push($this->_expected_errors, array($code));
182        }
183        return sizeof($this->_expected_errors);
184    }
185
186    function popExpect()
187    {
188        return array_pop($this->_expected_errors);
189    }
190
191    function _checkDelExpect($error_code)
192    {
193        $deleted = false;
194
195        foreach ($this->_expected_errors AS $key => $error_array) {
196            if (in_array($error_code, $error_array)) {
197                unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
198                $deleted = true;
199            }
200
201            if (0 == count($this->_expected_errors[$key])) {
202                unset($this->_expected_errors[$key]);
203            }
204        }
205        return $deleted;
206    }
207
208    function delExpect($error_code)
209    {
210        $deleted = false;
211
212        if ((is_array($error_code) && (0 != count($error_code)))) {
213            foreach($error_code as $key => $error) {
214                if ($this->_checkDelExpect($error)) {
215                    $deleted =  true;
216                } else {
217                    $deleted = false;
218                }
219            }
220            return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
221        } elseif (!empty($error_code)) {
222            if ($this->_checkDelExpect($error_code)) {
223                return true;
224            } else {
225                return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
226            }
227        } else {
228            return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
229        }
230    }
231
232    function &raiseError($message = null,
233                         $code = null,
234                         $mode = null,
235                         $options = null,
236                         $userinfo = null,
237                         $error_class = null,
238                         $skipmsg = false)
239    {
240        // The error is yet a PEAR error object
241        if (is_object($message)) {
242            $code        = $message->getCode();
243            $userinfo    = $message->getUserInfo();
244            $error_class = $message->getType();
245            $message->error_message_prefix = '';
246            $message     = $message->getMessage();
247        }
248
249        if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
250            if ($exp[0] == "*" ||
251                (is_int(reset($exp)) && in_array($code, $exp)) ||
252                (is_string(reset($exp)) && in_array($message, $exp))) {
253                $mode = PEAR_ERROR_RETURN;
254            }
255        }
256        if ($mode === null) {
257            if (isset($this) && isset($this->_default_error_mode)) {
258                $mode    = $this->_default_error_mode;
259                $options = $this->_default_error_options;
260            } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
261                $mode    = $GLOBALS['_PEAR_default_error_mode'];
262                $options = $GLOBALS['_PEAR_default_error_options'];
263            }
264        }
265
266        if ($error_class !== null) {
267            $ec = $error_class;
268        } elseif (isset($this) && isset($this->_error_class)) {
269            $ec = $this->_error_class;
270        } else {
271            $ec = 'PEAR_Error';
272        }
273        if ($skipmsg) {
274            $a = &new $ec($code, $mode, $options, $userinfo);
275            return $a;
276        } else {
277            $a = &new $ec($message, $code, $mode, $options, $userinfo);
278            return $a;
279        }
280    }
281
282    function &throwError($message = null,
283                         $code = null,
284                         $userinfo = null)
285    {
286        if (isset($this) && is_a($this, 'PEAR')) {
287            $a = &$this->raiseError($message, $code, null, null, $userinfo);
288            return $a;
289        } else {
290            $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
291            return $a;
292        }
293    }
294
295    function staticPushErrorHandling($mode, $options = null)
296    {
297        $stack = &$GLOBALS['_PEAR_error_handler_stack'];
298        $def_mode    = &$GLOBALS['_PEAR_default_error_mode'];
299        $def_options = &$GLOBALS['_PEAR_default_error_options'];
300        $stack[] = array($def_mode, $def_options);
301        switch ($mode) {
302            case PEAR_ERROR_EXCEPTION:
303            case PEAR_ERROR_RETURN:
304            case PEAR_ERROR_PRINT:
305            case PEAR_ERROR_TRIGGER:
306            case PEAR_ERROR_DIE:
307            case null:
308                $def_mode = $mode;
309                $def_options = $options;
310                break;
311
312            case PEAR_ERROR_CALLBACK:
313                $def_mode = $mode;
314                if (is_callable($options)) {
315                    $def_options = $options;
316                } else {
317                    trigger_error("invalid error callback", E_USER_WARNING);
318                }
319                break;
320
321            default:
322                trigger_error("invalid error mode", E_USER_WARNING);
323                break;
324        }
325        $stack[] = array($mode, $options);
326        return true;
327    }
328
329    function staticPopErrorHandling()
330    {
331        $stack = &$GLOBALS['_PEAR_error_handler_stack'];
332        $setmode     = &$GLOBALS['_PEAR_default_error_mode'];
333        $setoptions  = &$GLOBALS['_PEAR_default_error_options'];
334        array_pop($stack);
335        list($mode, $options) = $stack[sizeof($stack) - 1];
336        array_pop($stack);
337        switch ($mode) {
338            case PEAR_ERROR_EXCEPTION:
339            case PEAR_ERROR_RETURN:
340            case PEAR_ERROR_PRINT:
341            case PEAR_ERROR_TRIGGER:
342            case PEAR_ERROR_DIE:
343            case null:
344                $setmode = $mode;
345                $setoptions = $options;
346                break;
347
348            case PEAR_ERROR_CALLBACK:
349                $setmode = $mode;
350                // class/object method callback
351                if (is_callable($options)) {
352                    $setoptions = $options;
353                } else {
354                    trigger_error("invalid error callback", E_USER_WARNING);
355                }
356                break;
357
358            default:
359                trigger_error("invalid error mode", E_USER_WARNING);
360                break;
361        }
362        return true;
363    }
364
365    function pushErrorHandling($mode, $options = null)
366    {
367        $stack = &$GLOBALS['_PEAR_error_handler_stack'];
368        if (isset($this) && is_a($this, 'PEAR')) {
369            $def_mode    = &$this->_default_error_mode;
370            $def_options = &$this->_default_error_options;
371        } else {
372            $def_mode    = &$GLOBALS['_PEAR_default_error_mode'];
373            $def_options = &$GLOBALS['_PEAR_default_error_options'];
374        }
375        $stack[] = array($def_mode, $def_options);
376
377        if (isset($this) && is_a($this, 'PEAR')) {
378            $this->setErrorHandling($mode, $options);
379        } else {
380            PEAR::setErrorHandling($mode, $options);
381        }
382        $stack[] = array($mode, $options);
383        return true;
384    }
385
386    function popErrorHandling()
387    {
388        $stack = &$GLOBALS['_PEAR_error_handler_stack'];
389        array_pop($stack);
390        list($mode, $options) = $stack[sizeof($stack) - 1];
391        array_pop($stack);
392        if (isset($this) && is_a($this, 'PEAR')) {
393            $this->setErrorHandling($mode, $options);
394        } else {
395            PEAR::setErrorHandling($mode, $options);
396        }
397        return true;
398    }
399
400    function loadExtension($ext)
401    {
402        if (!extension_loaded($ext)) {
403            if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
404                return false;
405            }
406            if (OS_WINDOWS) {
407                $suffix = '.dll';
408            } elseif (PHP_OS == 'HP-UX') {
409                $suffix = '.sl';
410            } elseif (PHP_OS == 'AIX') {
411                $suffix = '.a';
412            } elseif (PHP_OS == 'OSX') {
413                $suffix = '.bundle';
414            } else {
415                $suffix = '.so';
416            }
417            return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
418        }
419        return true;
420    }
421
422}
423
424function _PEAR_call_destructors()
425{
426    global $_PEAR_destructor_object_list;
427    if (is_array($_PEAR_destructor_object_list) &&
428        sizeof($_PEAR_destructor_object_list))
429    {
430        reset($_PEAR_destructor_object_list);
431        if (@PEAR::getStaticProperty('PEAR', 'destructlifo')) {
432            $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
433        }
434        while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
435            $classname = get_class($objref);
436            while ($classname) {
437                $destructor = "_$classname";
438                if (method_exists($objref, $destructor)) {
439                    $objref->$destructor();
440                    break;
441                } else {
442                    $classname = get_parent_class($classname);
443                }
444            }
445        }
446        $_PEAR_destructor_object_list = array();
447    }
448
449    if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
450        foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
451            call_user_func_array($value[0], $value[1]);
452        }
453    }
454}
455
456class PEAR_Error
457{
458
459    function PEAR_Error($message = 'unknown error', $code = null,
460                        $mode = null, $options = null, $userinfo = null)
461    {
462        if ($mode === null) {
463            $mode = PEAR_ERROR_RETURN;
464        }
465        $this->message   = $message;
466        $this->code      = $code;
467        $this->mode      = $mode;
468        $this->userinfo  = $userinfo;
469        if (function_exists("debug_backtrace")) {
470            if (@!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) {
471                $this->backtrace = debug_backtrace();
472            }
473        }
474        if ($mode & PEAR_ERROR_CALLBACK) {
475            $this->level = E_USER_NOTICE;
476            $this->callback = $options;
477        } else {
478            if ($options === null) {
479                $options = E_USER_NOTICE;
480            }
481            $this->level = $options;
482            $this->callback = null;
483        }
484        if ($this->mode & PEAR_ERROR_PRINT) {
485            if (is_null($options) || is_int($options)) {
486                $format = "%s";
487            } else {
488                $format = $options;
489            }
490            printf($format, $this->getMessage());
491        }
492        if ($this->mode & PEAR_ERROR_TRIGGER) {
493            trigger_error($this->getMessage(), $this->level);
494        }
495        if ($this->mode & PEAR_ERROR_DIE) {
496            $msg = $this->getMessage();
497            if (is_null($options) || is_int($options)) {
498                $format = "%s";
499                if (substr($msg, -1) != "\n") {
500                    $msg .= "\n";
501                }
502            } else {
503                $format = $options;
504            }
505            die(sprintf($format, $msg));
506        }
507        if ($this->mode & PEAR_ERROR_CALLBACK) {
508            if (is_callable($this->callback)) {
509                call_user_func($this->callback, $this);
510            }
511        }
512        if ($this->mode & PEAR_ERROR_EXCEPTION) {
513            trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
514            eval('$e = new Exception($this->message, $this->code);throw($e);');
515        }
516    }
517
518    function getMode() {
519        return $this->mode;
520    }
521
522    function getCallback() {
523        return $this->callback;
524    }
525
526    function getMessage()
527    {
528        return ($this->error_message_prefix . $this->message);
529    }
530
531     function getCode()
532     {
533        return $this->code;
534     }
535
536    function getType()
537    {
538        return get_class($this);
539    }
540
541    function getUserInfo()
542    {
543        return $this->userinfo;
544    }
545
546    function getDebugInfo()
547    {
548        return $this->getUserInfo();
549    }
550
551    function getBacktrace($frame = null)
552    {
553        if (defined('PEAR_IGNORE_BACKTRACE')) {
554            return null;
555        }
556        if ($frame === null) {
557            return $this->backtrace;
558        }
559        return $this->backtrace[$frame];
560    }
561
562    function addUserInfo($info)
563    {
564        if (empty($this->userinfo)) {
565            $this->userinfo = $info;
566        } else {
567            $this->userinfo .= " ** $info";
568        }
569    }
570
571    function toString() {
572        $modes = array();
573        $levels = array(E_USER_NOTICE  => 'notice',
574                        E_USER_WARNING => 'warning',
575                        E_USER_ERROR   => 'error');
576        if ($this->mode & PEAR_ERROR_CALLBACK) {
577            if (is_array($this->callback)) {
578                $callback = (is_object($this->callback[0]) ?
579                    strtolower(get_class($this->callback[0])) :
580                    $this->callback[0]) . '::' .
581                    $this->callback[1];
582            } else {
583                $callback = $this->callback;
584            }
585            return sprintf('[%s: message="%s" code=%d mode=callback '.
586                           'callback=%s prefix="%s" info="%s"]',
587                           strtolower(get_class($this)), $this->message, $this->code,
588                           $callback, $this->error_message_prefix,
589                           $this->userinfo);
590        }
591        if ($this->mode & PEAR_ERROR_PRINT) {
592            $modes[] = 'print';
593        }
594        if ($this->mode & PEAR_ERROR_TRIGGER) {
595            $modes[] = 'trigger';
596        }
597        if ($this->mode & PEAR_ERROR_DIE) {
598            $modes[] = 'die';
599        }
600        if ($this->mode & PEAR_ERROR_RETURN) {
601            $modes[] = 'return';
602        }
603        return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
604                       'prefix="%s" info="%s"]',
605                       strtolower(get_class($this)), $this->message, $this->code,
606                       implode("|", $modes), $levels[$this->level],
607                       $this->error_message_prefix,
608                       $this->userinfo);
609    }
610
611}
612
613/**
614 * HTTP_Request.php code:
615 */
616
617// +-----------------------------------------------------------------------+
618// | Copyright (c) 2002-2003, Richard Heyes                                |
619// | All rights reserved.                                                  |
620// +-----------------------------------------------------------------------+
621
622define('HTTP_REQUEST_METHOD_GET',     'GET',     true);
623define('HTTP_REQUEST_METHOD_HEAD',    'HEAD',    true);
624define('HTTP_REQUEST_METHOD_POST',    'POST',    true);
625define('HTTP_REQUEST_METHOD_PUT',     'PUT',     true);
626define('HTTP_REQUEST_METHOD_DELETE',  'DELETE'