OSDN Git Service

Reduce PKWK_HTTP_REQUEST_URL_REDIRECT_MAX 10 => 2. Enable http_request() POST with...
[pukiwiki/pukiwiki.git] / lib / proxy.php
1 <?php
2 // $Id: proxy.php,v 1.5 2005/04/10 09:09:13 henoheno Exp $
3 //
4 // HTTP Proxy related functions
5
6 // Max number of 'track' redirection message with 301 or 302 response
7 define('PKWK_HTTP_REQUEST_URL_REDIRECT_MAX', 2);
8
9 // Separate IPv4 network-address and its netmask
10 define('PKWK_CIDR_NETWORK_REGEX', '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/([0-9.]+))?$/');
11
12 /*
13  * http_request($url)
14  *     Get / Send data via HTTP request
15  * $url     : URI started with http:// (http://user:pass@host:port/path?query)
16  * $method  : GET, POST, or HEAD
17  * $headers : Additional HTTP headers, ended with "\r\n"
18  * $post    : An array of data to send via POST method ('key'=>'value')
19  * $redirect_max : Max number of HTTP redirect
20  * $content_charset : Content charset. Use '' or CONTENT_CHARSET
21 */
22 function http_request($url, $method = 'GET', $headers = '', $post = array(),
23         $redirect_max = PKWK_HTTP_REQUEST_URL_REDIRECT_MAX, $content_charset = '')
24 {
25         global $proxy_host, $proxy_port;
26         global $need_proxy_auth, $proxy_auth_user, $proxy_auth_pass;
27
28         $rc  = array();
29         $arr = parse_url($url);
30
31         $via_proxy = via_proxy($arr['host']);
32
33         // query
34         $arr['query'] = isset($arr['query']) ? '?' . $arr['query'] : '';
35         // port
36         $arr['port']  = isset($arr['port'])  ? $arr['port'] : 80;
37
38         $url_base = $arr['scheme'] . '://' . $arr['host'] . ':' . $arr['port'];
39         $url_path = isset($arr['path']) ? $arr['path'] : '/';
40         $url = ($via_proxy ? $url_base : '') . $url_path . $arr['query'];
41
42         $query = $method . ' ' . $url . ' HTTP/1.0' . "\r\n";
43         $query .= 'Host: ' . $arr['host'] . "\r\n";
44         $query .= 'User-Agent: PukiWiki/' . S_VERSION . "\r\n";
45
46         // Basic-auth for HTTP proxy server
47         if ($need_proxy_auth && isset($proxy_auth_user) && isset($proxy_auth_pass))
48                 $query .= 'Proxy-Authorization: Basic '.
49                         base64_encode($proxy_auth_user . ':' . $proxy_auth_pass) . "\r\n";
50
51         // (Normal) Basic-auth for remote host
52         if (isset($arr['user']) && isset($arr['pass']))
53                 $query .= 'Authorization: Basic '.
54                         base64_encode($arr['user'] . ':' . $arr['pass']) . "\r\n";
55
56         $query .= $headers;
57
58         if (strtoupper($method) == 'POST') {
59                 // 'application/x-www-form-urlencoded', especially for TrackBack ping
60                 $POST = array();
61                 foreach ($post as $name=>$val) $POST[] = $name . '=' . urlencode($val);
62                 $data = join('&', $POST);
63
64                 if (preg_match('/^[a-zA-Z0-9_-]+$/', $content_charset)) {
65                         // Legacy but simple
66                         $query .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
67                 } else {
68                         // With charset (NOTE: Some implementation may hate this)
69                         $query .= 'Content-Type: application/x-www-form-urlencoded' .
70                                 '; charset=' . strtolower($content_charset) . "\r\n";
71                 }
72
73                 $query .= 'Content-Length: ' . strlen($data) . "\r\n";
74                 $query .= "\r\n";
75                 $query .= $data;
76         } else {
77                 $query .= "\r\n";
78         }
79
80         $errno  = 0;
81         $errstr = '';
82         $fp = fsockopen(
83                 $via_proxy ? $proxy_host : $arr['host'],
84                 $via_proxy ? $proxy_port : $arr['port'],
85                 $errno, $errstr, 30);
86         if ($fp === FALSE) {
87                 return array(
88                         'query'  => $query, // Query string
89                         'rc'     => $errno, // Error number
90                         'header' => '',     // Header
91                         'data'   => $errstr // Error message
92                 );
93         }
94         fputs($fp, $query);
95         $response = '';
96         while (! feof($fp)) $response .= fread($fp, 4096);
97         fclose($fp);
98
99         $resp = explode("\r\n\r\n", $response, 2);
100         $rccd = explode(' ', $resp[0], 3); // array('HTTP/1.1', '200', 'OK\r\n...')
101         $rc   = (integer)$rccd[1];
102
103         switch ($rc) {
104         case 301: // Moved Permanently
105         case 302: // Moved Temporarily
106                 $matches = array();
107                 if (preg_match('/^Location: (.+)$/m', $resp[0], $matches)
108                         && --$redirect_max > 0)
109                 {
110                         $url = trim($matches[1]);
111                         if (! preg_match('/^https?:\//', $url)) {
112                                 // Relative path to Absolute
113                                 if ($url{0} != '/')
114                                         $url = substr($url_path, 0, strrpos($url_path, '/')) . '/' . $url;
115                                 $url = $url_base . $url; // Add sheme, host
116                         }
117                         // Redirect
118                         return http_request($url, $method, $headers, $post, $redirect_max);
119                 }
120         }
121         return array(
122                 'query'  => $query,   // Query String
123                 'rc'     => $rc,      // Response Code
124                 'header' => $resp[0], // Header
125                 'data'   => $resp[1]  // Data
126         );
127 }
128
129 // Check HTTP proxy server is needed or not for the $host
130 function via_proxy($host)
131 {
132         global $use_proxy, $no_proxy;
133
134         if (! $use_proxy) return FALSE;
135
136         $ip   = gethostbyname($host);
137         $l_ip = ip2long($ip);
138         $is_valid = (is_long($l_ip) && long2ip($l_ip) == $ip); // Valid IP address
139
140         $matches = array();
141         foreach ($no_proxy as $network) {
142                 if ($is_valid && preg_match(PKWK_CIDR_NETWORK_REGEX, $network, $matches)) {
143                         // Sample: '10.0.0.0/8' or '10.0.0.0/255.0.0.0'
144                         $l_net = ip2long($matches[1]); // '10.0.0.0'
145                         $mask  = isset($matches[2]) ? $matches[2] : 32; // '8' or '255.0.0.0'
146                         $mask  = is_numeric($mask) ?
147                                 pow(2, 32) - pow(2, 32 - $mask) : // '8' means '8-bit mask'
148                                 ip2long($mask);                   // '255.0.0.0' (the same)
149
150                         if (($l_ip & $mask) == $l_net) return FALSE;
151                 } else {
152                         // Hostname, or a part of hostname
153                         if (preg_match('/' . preg_quote($network, '/') . '$/', $host))
154                                 return FALSE;
155                 }
156         }
157
158         return TRUE; // Proxy needed
159 }
160 ?>