OSDN Git Service

* Cleanup. Japanese => English. Shrink. a static variable => a define
[pukiwiki/pukiwiki.git] / lib / proxy.php
1 <?php
2 // $Id: proxy.php,v 1.3 2005/04/10 07:04: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', 10);
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 */
21 function http_request($url, $method = 'GET', $headers = '', $post = array(),
22         $redirect_max = PKWK_HTTP_REQUEST_URL_REDIRECT_MAX)
23 {
24         global $proxy_host, $proxy_port;
25         global $need_proxy_auth, $proxy_auth_user, $proxy_auth_pass;
26
27         $rc  = array();
28         $arr = parse_url($url);
29
30         $via_proxy = via_proxy($arr['host']);
31
32         // query
33         $arr['query'] = isset($arr['query']) ? '?' . $arr['query'] : '';
34         // port
35         $arr['port']  = isset($arr['port'])  ? $arr['port'] : 80;
36
37         $url_base = $arr['scheme'] . '://' . $arr['host'] . ':' . $arr['port'];
38         $url_path = isset($arr['path']) ? $arr['path'] : '/';
39         $url = ($via_proxy ? $url_base : '') . $url_path . $arr['query'];
40
41         $query = $method . ' ' . $url . ' HTTP/1.0' . "\r\n";
42         $query .= 'Host: ' . $arr['host'] . "\r\n";
43         $query .= 'User-Agent: PukiWiki/' . S_VERSION . "\r\n";
44
45         // Basic-auth for HTTP proxy server
46         if ($need_proxy_auth && isset($proxy_auth_user) && isset($proxy_auth_pass))
47                 $query .= 'Proxy-Authorization: Basic '.
48                         base64_encode($proxy_auth_user . ':' . $proxy_auth_pass) . "\r\n";
49
50         // (Normal) Basic-auth for remote host
51         if (isset($arr['user']) && isset($arr['pass']))
52                 $query .= 'Authorization: Basic '.
53                         base64_encode($arr['user'] . ':' . $arr['pass']) . "\r\n";
54
55         $query .= $headers;
56
57         if (strtoupper($method) == 'POST') {
58                 $POST = array();
59                 foreach ($post as $name=>$val) $POST[] = $name . '=' . urlencode($val);
60                 $data = join('&', $POST);
61                 $query .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
62                 $query .= 'Content-Length: ' . strlen($data) . "\r\n";
63                 $query .= "\r\n";
64                 $query .= $data;
65         } else {
66                 $query .= "\r\n";
67         }
68
69         $errno  = 0;
70         $errstr = '';
71         $fp = fsockopen(
72                 $via_proxy ? $proxy_host : $arr['host'],
73                 $via_proxy ? $proxy_port : $arr['port'],
74                 $errno, $errstr, 30);
75         if ($fp === FALSE) {
76                 return array(
77                         'query'  => $query, // Query string
78                         'rc'     => $errno, // Error number
79                         'header' => '',     // Header
80                         'data'   => $errstr // Error message
81                 );
82         }
83         fputs($fp, $query);
84         $response = '';
85         while (! feof($fp)) $response .= fread($fp, 4096);
86         fclose($fp);
87
88         $resp = explode("\r\n\r\n", $response, 2);
89         $rccd = explode(' ', $resp[0], 3); // array('HTTP/1.1', '200', 'OK\r\n...')
90         $rc   = (integer)$rccd[1];
91
92         switch ($rc) {
93         case 301: // Moved Permanently
94         case 302: // Moved Temporarily
95                 $matches = array();
96                 if (preg_match('/^Location: (.+)$/m', $resp[0], $matches)
97                         && --$redirect_max > 0)
98                 {
99                         $url = trim($matches[1]);
100                         if (! preg_match('/^https?:\//', $url)) {
101                                 // Relative path to Absolute
102                                 if ($url{0} != '/')
103                                         $url = substr($url_path, 0, strrpos($url_path, '/')) . '/' . $url;
104                                 $url = $url_base . $url; // Add sheme, host
105                         }
106                         // Redirect
107                         return http_request($url, $method, $headers, $post, $redirect_max);
108                 }
109         }
110         return array(
111                 'query'  => $query,   // Query String
112                 'rc'     => $rc,      // Response Code
113                 'header' => $resp[0], // Header
114                 'data'   => $resp[1]  // Data
115         );
116 }
117
118 // Check HTTP proxy server is needed or not for the $host
119 function via_proxy($host)
120 {
121         global $use_proxy, $no_proxy;
122
123         if (! $use_proxy) return FALSE;
124
125         $ip   = gethostbyname($host);
126         $l_ip = ip2long($ip);
127         $is_valid = (is_long($l_ip) && long2ip($l_ip) == $ip); // Valid IP address
128
129         $matches = array();
130         foreach ($no_proxy as $network) {
131                 if ($is_valid && preg_match(PKWK_CIDR_NETWORK_REGEX, $network, $matches)) {
132                         // Sample: '10.0.0.0/8' or '10.0.0.0/255.0.0.0'
133                         $l_net = ip2long($matches[1]); // '10.0.0.0'
134                         $mask  = isset($matches[2]) ? $matches[2] : 32; // '8' or '255.0.0.0'
135                         $mask  = is_numeric($mask) ?
136                                 pow(2, 32) - pow(2, 32 - $mask) : // '8' means '8-bit mask'
137                                 ip2long($mask);                   // '255.0.0.0' (the same)
138
139                         if (($l_ip & $mask) == $l_net) return FALSE;
140                 } else {
141                         // Hostname, or a part of hostname
142                         if (preg_match('/' . preg_quote($network, '/') . '$/', $host))
143                                 return FALSE;
144                 }
145         }
146
147         return TRUE; // Proxy needed
148 }
149 ?>