OSDN Git Service

* BugTrack2/246: Should use array_key_exists($maybe, $null) here (patched by sonots).
[pukiwiki/pukiwiki.git] / lib / proxy.php
index 6538291..fb63966 100644 (file)
@@ -1,13 +1,12 @@
 <?php
-// $Id: proxy.php,v 1.4 2005/04/10 08:30:34 henoheno Exp $
+// $Id: proxy.php,v 1.9 2005/06/28 14:03:29 henoheno Exp $
+// Copyright (C) 2003-2005 PukiWiki Developers Team
+// License: GPL v2 or (at your option) any later version
 //
-// HTTP Proxy related functions
+// HTTP-Proxy related functions
 
 // Max number of 'track' redirection message with 301 or 302 response
-define('PKWK_HTTP_REQUEST_URL_REDIRECT_MAX', 10);
-
-// Separate IPv4 network-address and its netmask
-define('PKWK_CIDR_NETWORK_REGEX', '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/([0-9.]+))?$/');
+define('PKWK_HTTP_REQUEST_URL_REDIRECT_MAX', 2);
 
 /*
  * http_request($url)
@@ -17,17 +16,18 @@ define('PKWK_CIDR_NETWORK_REGEX', '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/([
  * $headers : Additional HTTP headers, ended with "\r\n"
  * $post    : An array of data to send via POST method ('key'=>'value')
  * $redirect_max : Max number of HTTP redirect
+ * $content_charset : Content charset. Use '' or CONTENT_CHARSET
 */
 function http_request($url, $method = 'GET', $headers = '', $post = array(),
-       $redirect_max = PKWK_HTTP_REQUEST_URL_REDIRECT_MAX)
+       $redirect_max = PKWK_HTTP_REQUEST_URL_REDIRECT_MAX, $content_charset = '')
 {
-       global $proxy_host, $proxy_port;
+       global $use_proxy, $no_proxy, $proxy_host, $proxy_port;
        global $need_proxy_auth, $proxy_auth_user, $proxy_auth_pass;
 
        $rc  = array();
        $arr = parse_url($url);
 
-       $via_proxy = via_proxy($arr['host']);
+       $via_proxy = $use_proxy ? ! in_the_net($no_proxy, $arr['host']) : FALSE;
 
        // query
        $arr['query'] = isset($arr['query']) ? '?' . $arr['query'] : '';
@@ -59,7 +59,16 @@ function http_request($url, $method = 'GET', $headers = '', $post = array(),
                $POST = array();
                foreach ($post as $name=>$val) $POST[] = $name . '=' . urlencode($val);
                $data = join('&', $POST);
-               $query .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
+
+               if (preg_match('/^[a-zA-Z0-9_-]+$/', $content_charset)) {
+                       // Legacy but simple
+                       $query .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
+               } else {
+                       // With charset (NOTE: Some implementation may hate this)
+                       $query .= 'Content-Type: application/x-www-form-urlencoded' .
+                               '; charset=' . strtolower($content_charset) . "\r\n";
+               }
+
                $query .= 'Content-Length: ' . strlen($data) . "\r\n";
                $query .= "\r\n";
                $query .= $data;
@@ -116,20 +125,28 @@ function http_request($url, $method = 'GET', $headers = '', $post = array(),
        );
 }
 
-// Check HTTP proxy server is needed or not for the $host
-function via_proxy($host)
+// Separate IPv4 network-address and its netmask
+define('PKWK_CIDR_NETWORK_REGEX', '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/([0-9.]+))?$/');
+
+// Check if the $host is in the specified network(s)
+function in_the_net($networks = array(), $host = '')
 {
-       global $use_proxy, $no_proxy;
+       if (empty($networks) || $host == '') return FALSE;
+       if (! is_array($networks)) $networks = array($networks);
 
-       if (! $use_proxy) return FALSE;
+       $matches = array();
 
-       $ip   = gethostbyname($host);
+       if (preg_match(PKWK_CIDR_NETWORK_REGEX, $host, $matches)) {
+               $ip = $matches[1];
+       } else {
+               $ip = gethostbyname($host); // May heavy
+       }
        $l_ip = ip2long($ip);
-       $is_valid = (is_long($l_ip) && long2ip($l_ip) == $ip); // Valid IP address
 
-       $matches = array();
-       foreach ($no_proxy as $network) {
-               if ($is_valid && preg_match(PKWK_CIDR_NETWORK_REGEX, $network, $matches)) {
+       foreach ($networks as $network) {
+               if (preg_match(PKWK_CIDR_NETWORK_REGEX, $network, $matches) &&
+                   is_long($l_ip) && long2ip($l_ip) == $ip) {
+                       // $host seems valid IPv4 address
                        // Sample: '10.0.0.0/8' or '10.0.0.0/255.0.0.0'
                        $l_net = ip2long($matches[1]); // '10.0.0.0'
                        $mask  = isset($matches[2]) ? $matches[2] : 32; // '8' or '255.0.0.0'
@@ -137,14 +154,15 @@ function via_proxy($host)
                                pow(2, 32) - pow(2, 32 - $mask) : // '8' means '8-bit mask'
                                ip2long($mask);                   // '255.0.0.0' (the same)
 
-                       if (($l_ip & $mask) == $l_net) return FALSE;
+                       if (($l_ip & $mask) == $l_net) return TRUE;
                } else {
-                       // Hostname, or a part of hostname
-                       if (preg_match('/' . preg_quote($network, '/') . '$/', $host))
-                               return FALSE;
+                       // $host seems not IPv4 address. May be a DNS name like 'foobar.example.com'?
+                       foreach ($networks as $network)
+                               if (preg_match('/\.?\b' . preg_quote($network, '/') . '$/', $host))
+                                       return TRUE;
                }
        }
 
-       return TRUE; // Proxy needed
+       return FALSE; // Not found
 }
 ?>