OSDN Git Service

Moved libraries from root to lib/
[pukiwiki/pukiwiki.git] / lib / proxy.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: proxy.php,v 1.1 2004/08/01 01:54:35 henoheno Exp $
6 //
7
8 /*
9  * http_request($url)
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¤Î²ó¿ôÀ©¸Â
16 */
17
18 // ¥ê¥À¥¤¥ì¥¯¥È²ó¿ôÀ©¸Â¤Î½é´üÃÍ
19 define('HTTP_REQUEST_URL_REDIRECT_MAX',10);
20
21 function http_request($url,$method='GET',$headers='',$post=array(),
22         $redirect_max=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         // proxy¤ÎBasicǧ¾Ú
46         if ($need_proxy_auth and isset($proxy_auth_user) and isset($proxy_auth_pass))
47         {
48                 $query .= 'Proxy-Authorization: Basic '.
49                         base64_encode($proxy_auth_user.':'.$proxy_auth_pass)."\r\n";
50         }
51         // Basic Ç§¾ÚÍÑ
52         if (isset($arr['user']) and isset($arr['pass']))
53         {
54                 $query .= 'Authorization: Basic '.
55                         base64_encode($arr['user'].':'.$arr['pass'])."\r\n";
56         }
57
58         $query .= $headers;
59
60         // POST »þ¤Ï¡¢urlencode ¤·¤¿¥Ç¡¼¥¿¤È¤¹¤ë
61         if (strtoupper($method) == 'POST')
62         {
63                 $POST = array();
64                 foreach ($post as $name=>$val)
65                 {
66                         $POST[] = $name.'='.urlencode($val);
67                 }
68                 $data = join('&',$POST);
69                 $query .= "Content-Type: application/x-www-form-urlencoded\r\n";
70                 $query .= 'Content-Length: '.strlen($data)."\r\n";
71                 $query .= "\r\n";
72                 $query .= $data;
73         }
74         else
75         {
76                 $query .= "\r\n";
77         }
78
79         $errno = 0; $errstr = '';
80         $fp = fsockopen(
81                 $via_proxy ? $proxy_host : $arr['host'],
82                 $via_proxy ? $proxy_port : $arr['port'],
83                 $errno,$errstr,30);
84         if (!$fp)
85         {
86                 return array(
87                         'query'  => $query, // Query String
88                         'rc'     => $errno, // ¥¨¥é¡¼ÈÖ¹æ
89                         'header' => '',     // Header
90                         'data'   => $errstr // ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸
91                 );
92         }
93
94         fputs($fp, $query);
95
96         $response = '';
97         while (!feof($fp))
98         {
99                 $response .= fread($fp,4096);
100         }
101         fclose($fp);
102
103         $resp = explode("\r\n\r\n",$response,2);
104         $rccd = explode(' ',$resp[0],3); // array('HTTP/1.1','200','OK\r\n...')
105         $rc = (integer)$rccd[1];
106
107         // Redirect
108         $matches = array();
109         switch ($rc)
110         {
111                 case 302: // Moved Temporarily
112                 case 301: // Moved Permanently
113                         if (preg_match('/^Location: (.+)$/m',$resp[0],$matches)
114                                 and --$redirect_max > 0)
115                         {
116                                 $url = trim($matches[1]);
117                                 if (!preg_match('/^https?:\//',$url)) // no scheme
118                                 {
119                                         if ($url{0} != '/') // Relative path
120                                         {
121                                                 // to Absolute path
122                                                 $url = substr($url_path,0,strrpos($url_path,'/')).'/'.$url;
123                                         }
124                                         // add sheme,host
125                                         $url = $url_base.$url;
126                                 }
127                                 return http_request($url,$method,$headers,$post,$redirect_max);
128                         }
129         }
130
131         return array(
132                 'query'  => $query,   // Query String
133                 'rc'     => $rc,      // Response Code
134                 'header' => $resp[0], // Header
135                 'data'   => $resp[1]  // Data
136         );
137 }
138
139 // ¥×¥í¥­¥·¤ò·Ðͳ¤¹¤ëɬÍפ¬¤¢¤ë¤«¤É¤¦¤«È½Äê
140 function via_proxy($host)
141 {
142         global $use_proxy, $no_proxy;
143         static $ip_pattern = '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/(.+))?$/';
144
145         if (!$use_proxy)
146         {
147                 return FALSE;
148         }
149         $ip = gethostbyname($host);
150         $l_ip = ip2long($ip);
151         $valid = (is_long($l_ip) and long2ip($l_ip) == $ip); // valid ip address
152
153         $matches = array();
154         foreach ($no_proxy as $network)
155         {
156                 if ($valid and preg_match($ip_pattern,$network,$matches))
157                 {
158                         $l_net = ip2long($matches[1]);
159                         $mask = array_key_exists(2,$matches) ? $matches[2] : 32;
160                         $mask = is_numeric($mask) ?
161                                 pow(2,32) - pow(2,32 - $mask) : // "10.0.0.0/8"
162                                 ip2long($mask);                 // "10.0.0.0/255.0.0.0"
163                         if (($l_ip & $mask) == $l_net)
164                         {
165                                 return FALSE;
166                         }
167                 }
168                 else
169                 {
170                         if (preg_match('/'.preg_quote($network,'/').'/',$host))
171                         {
172                                 return FALSE;
173                         }
174                 }
175         }
176         return TRUE;
177 }
178 ?>