OSDN Git Service

BugTrack/2514 Support PHP 8
[pukiwiki/pukiwiki.git] / lib / proxy.php
index 8c296bc..d090aa3 100644 (file)
@@ -1,6 +1,7 @@
 <?php
-// $Id: proxy.php,v 1.8 2005/06/27 14:47:40 henoheno Exp $
-// Copyright (C) 2003-2005 PukiWiki Developers Team
+// PukiWiki - Yet another WikiWikiWeb clone
+// proxy.php
+// Copyright: 2003-2021 PukiWiki Development Team
 // License: GPL v2 or (at your option) any later version
 //
 // HTTP-Proxy related functions
@@ -8,8 +9,19 @@
 // Max number of 'track' redirection message with 301 or 302 response
 define('PKWK_HTTP_REQUEST_URL_REDIRECT_MAX', 2);
 
+// We also define deprecated function 'http_request' for backward compatibility
+if (!function_exists('http_request')) {
+       // pecl_http extension also have the function named 'http_request'
+       function http_request($url, $method = 'GET', $headers = '',
+               $post = array(), $redirect_max = PKWK_HTTP_REQUEST_URL_REDIRECT_MAX,
+               $content_charset = '') {
+               return pkwk_http_request($url, $method, $headers, $post,
+                       $redirect_max, $content_charset);
+       }
+}
+
 /*
- * http_request($url)
+ * pkwk_http_request($url)
  *     Get / Send data via HTTP request
  * $url     : URI started with http:// (http://user:pass@host:port/path?query)
  * $method  : GET, POST, or HEAD
@@ -18,12 +30,11 @@ define('PKWK_HTTP_REQUEST_URL_REDIRECT_MAX', 2);
  * $redirect_max : Max number of HTTP redirect
  * $content_charset : Content charset. Use '' or CONTENT_CHARSET
 */
-function http_request($url, $method = 'GET', $headers = '', $post = array(),
+function pkwk_http_request($url, $method = 'GET', $headers = '', $post = array(),
        $redirect_max = PKWK_HTTP_REQUEST_URL_REDIRECT_MAX, $content_charset = '')
 {
        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);
 
@@ -32,7 +43,13 @@ function http_request($url, $method = 'GET', $headers = '', $post = array(),
        // query
        $arr['query'] = isset($arr['query']) ? '?' . $arr['query'] : '';
        // port
-       $arr['port']  = isset($arr['port'])  ? $arr['port'] : 80;
+       if (!isset($arr['port'])) {
+               if ($arr['scheme'] === 'https') {
+                       $arr['port'] = 443;
+               } else {
+                       $arr['port'] = 80;
+               }
+       }
 
        $url_base = $arr['scheme'] . '://' . $arr['host'] . ':' . $arr['port'];
        $url_path = isset($arr['path']) ? $arr['path'] : '/';
@@ -78,8 +95,12 @@ function http_request($url, $method = 'GET', $headers = '', $post = array(),
 
        $errno  = 0;
        $errstr = '';
+       $ssl_prefix = '';
+       if ($arr['scheme'] === 'https') {
+               $ssl_prefix = 'ssl://';
+       }
        $fp = fsockopen(
-               $via_proxy ? $proxy_host : $arr['host'],
+               $ssl_prefix . ($via_proxy ? $proxy_host : $arr['host']),
                $via_proxy ? $proxy_port : $arr['port'],
                $errno, $errstr, 30);
        if ($fp === FALSE) {
@@ -109,12 +130,12 @@ function http_request($url, $method = 'GET', $headers = '', $post = array(),
                        $url = trim($matches[1]);
                        if (! preg_match('/^https?:\//', $url)) {
                                // Relative path to Absolute
-                               if ($url{0} != '/')
+                               if ($url[0] != '/')
                                        $url = substr($url_path, 0, strrpos($url_path, '/')) . '/' . $url;
                                $url = $url_base . $url; // Add sheme, host
                        }
                        // Redirect
-                       return http_request($url, $method, $headers, $post, $redirect_max);
+                       return pkwk_http_request($url, $method, $headers, $post, $redirect_max);
                }
        }
        return array(
@@ -131,7 +152,8 @@ define('PKWK_CIDR_NETWORK_REGEX', '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/([
 // Check if the $host is in the specified network(s)
 function in_the_net($networks = array(), $host = '')
 {
-       if (empty($networks) || $hosts == '') return FALSE;
+       if (empty($networks) || $host == '') return FALSE;
+       if (! is_array($networks)) $networks = array($networks);
 
        $matches = array();
 
@@ -142,27 +164,25 @@ function in_the_net($networks = array(), $host = '')
        }
        $l_ip = ip2long($ip);
 
-       if (is_long($l_ip) && long2ip($l_ip) == $ip) {
-               // $host seems valid IPv4 address
-               foreach ($networks as $network) {
-                       if (preg_match(PKWK_CIDR_NETWORK_REGEX, $network, $matches)) {
-                               // 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'
-                               $mask  = is_numeric($mask) ?
-                                       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 TRUE;
-                       }
+       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'
+                       $mask  = is_numeric($mask) ?
+                               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 TRUE;
+               } else {
+                       // $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;
                }
-       } else {
-               // $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 FALSE; // Not found
 }
-?>