2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: proxy.php,v 1.2 2004/10/13 14:30:58 henoheno Exp $
10 * HTTP¥ê¥¯¥¨¥¹¥È¤òȯ¹Ô¤·¡¢¥Ç¡¼¥¿¤ò¼èÆÀ¤¹¤ë
11 * $url : http://¤«¤é»Ï¤Þ¤ëURL(http://user:pass@host:port/path?query)
12 * $method : GET, POST, HEAD¤Î¤¤¤º¤ì¤«(¥Ç¥Õ¥©¥ë¥È¤ÏGET)
13 * $headers : Ǥ°Õ¤ÎÄɲåإåÀ
14 * $post : POST¤Î»þ¤ËÁ÷¿®¤¹¤ë¥Ç¡¼¥¿¤ò³ÊǼ¤·¤¿ÇÛÎó('ÊÑ¿ô̾'=>'ÃÍ')
15 * $redirect_max : HTTP redirect¤Î²ó¿ôÀ©¸Â
18 // ¥ê¥À¥¤¥ì¥¯¥È²ó¿ôÀ©¸Â¤Î½é´üÃÍ
19 define('HTTP_REQUEST_URL_REDIRECT_MAX', 10);
21 function http_request($url, $method = 'GET', $headers = '', $post = array(),
22 $redirect_max = HTTP_REQUEST_URL_REDIRECT_MAX)
24 global $proxy_host, $proxy_port;
25 global $need_proxy_auth, $proxy_auth_user, $proxy_auth_pass;
28 $arr = parse_url($url);
30 $via_proxy = via_proxy($arr['host']);
33 $arr['query'] = isset($arr['query']) ? '?' . $arr['query'] : '';
35 $arr['port'] = isset($arr['port']) ? $arr['port'] : 80;
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'];
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";
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";
52 if (isset($arr['user']) && isset($arr['pass'])) {
53 $query .= 'Authorization: Basic '.
54 base64_encode($arr['user'] . ':' . $arr['pass']) . "\r\n";
59 // POST »þ¤Ï¡¢urlencode ¤·¤¿¥Ç¡¼¥¿¤È¤¹¤ë
60 if (strtoupper($method) == 'POST') {
62 foreach ($post as $name=>$val) {
63 $POST[] = $name . '=' . urlencode($val);
65 $data = join('&', $POST);
66 $query .= "Content-Type: application/x-www-form-urlencoded\r\n";
67 $query .= 'Content-Length: ' . strlen($data) . "\r\n";
74 $errno = 0; $errstr = '';
76 $via_proxy ? $proxy_host : $arr['host'],
77 $via_proxy ? $proxy_port : $arr['port'],
81 'query' => $query, // Query String
82 'rc' => $errno, // ¥¨¥é¡¼ÈÖ¹æ
83 'header' => '', // Header
84 'data' => $errstr // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸
92 $response .= fread($fp, 4096);
95 $resp = explode("\r\n\r\n", $response, 2);
96 $rccd = explode(' ', $resp[0], 3); // array('HTTP/1.1', '200', 'OK\r\n...')
97 $rc = (integer)$rccd[1];
102 case 302: // Moved Temporarily
103 case 301: // Moved Permanently
104 if (preg_match('/^Location: (.+)$/m', $resp[0], $matches)
105 && --$redirect_max > 0)
107 $url = trim($matches[1]);
108 if (! preg_match('/^https?:\//', $url)) {
110 if ($url{0} != '/') {
111 // Relative path to Absolute
112 $url = substr($url_path, 0, strrpos($url_path, '/')) . '/' . $url;
114 $url = $url_base . $url; // Add sheme, host
116 return http_request($url, $method, $headers, $post, $redirect_max);
121 'query' => $query, // Query String
122 'rc' => $rc, // Response Code
123 'header' => $resp[0], // Header
124 'data' => $resp[1] // Data
128 // ¥×¥í¥¥·¤ò·Ðͳ¤¹¤ëɬÍפ¬¤¢¤ë¤«¤É¤¦¤«È½Äê
129 function via_proxy($host)
131 global $use_proxy, $no_proxy;
132 static $ip_pattern = '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/(.+))?$/';
134 if (! $use_proxy) return FALSE;
136 $ip = gethostbyname($host);
137 $l_ip = ip2long($ip);
138 $valid = (is_long($l_ip) && long2ip($l_ip) == $ip); // valid ip address
141 foreach ($no_proxy as $network) {
142 if ($valid && preg_match($ip_pattern, $network, $matches)) {
143 $l_net = ip2long($matches[1]);
144 $mask = isset($matches[2]) ? $matches[2] : 32;
145 $mask = is_numeric($mask) ?
146 pow(2, 32) - pow(2, 32 - $mask) : // "10.0.0.0/8"
147 ip2long($mask); // "10.0.0.0/255.0.0.0"
149 if (($l_ip & $mask) == $l_net) return FALSE;
151 if (preg_match('/' . preg_quote($network, '/') . '/', $host))