| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* IP utilities. |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @since 161110 |
| 16 |
*/ |
| 17 |
if (!class_exists('c_ws_plugin__s2member_utils_ip')) { |
| 18 |
/** |
| 19 |
* IP utilities. |
| 20 |
* |
| 21 |
* @since 161110 |
| 22 |
*/ |
| 23 |
class c_ws_plugin__s2member_utils_ip |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Get the current visitor's real IP address. |
| 27 |
* |
| 28 |
* @since 161110 IP utilities. |
| 29 |
* |
| 30 |
* @return string Real IP address, else `unknown` on failure. |
| 31 |
* |
| 32 |
* @note This supports both IPv4 and IPv6 addresses. |
| 33 |
* @note See my tests against this here: http://3v4l.org/fVWUp |
| 34 |
*/ |
| 35 |
public static function current() |
| 36 |
{ |
| 37 |
static $ip; // Static cache. |
| 38 |
|
| 39 |
if ($ip !== null) { |
| 40 |
return $ip; |
| 41 |
} |
| 42 |
$sources = array( |
| 43 |
'HTTP_CF_CONNECTING_IP', |
| 44 |
'HTTP_CLIENT_IP', |
| 45 |
'HTTP_X_FORWARDED_FOR', |
| 46 |
'HTTP_X_FORWARDED', |
| 47 |
'HTTP_X_CLUSTER_CLIENT_IP', |
| 48 |
'HTTP_FORWARDED_FOR', |
| 49 |
'HTTP_FORWARDED', |
| 50 |
'HTTP_VIA', |
| 51 |
'REMOTE_ADDR', |
| 52 |
); |
| 53 |
$sources = apply_filters('ws_plugin__s2member_current_ip_sources', $sources); |
| 54 |
$prioritize_remote_addr = apply_filters('ws_plugin__s2member_current_ip_prioritize_remote_addr', false); |
| 55 |
|
| 56 |
if (!empty($_SERVER['REMOTE_ADDR']) && $prioritize_remote_addr) { |
| 57 |
if (($_valid_public_ip = self::valid_public((string) $_SERVER['REMOTE_ADDR']))) { |
| 58 |
return $ip = $_valid_public_ip; |
| 59 |
} // unset($_valid_public_ip); // Housekeeping. |
| 60 |
} |
| 61 |
foreach ($sources as $_key => $_source) { |
| 62 |
if (!empty($_SERVER[$_source])) { // Exists? |
| 63 |
if (($_valid_public_ip = self::valid_public((string) $_SERVER[$_source]))) { |
| 64 |
return $ip = $_valid_public_ip; |
| 65 |
} |
| 66 |
} // unset($_key, $_source, $_valid_public_ip); // Housekeeping. |
| 67 |
} |
| 68 |
if (!empty($_SERVER['REMOTE_ADDR'])) { |
| 69 |
return $ip = strtolower((string) $_SERVER['REMOTE_ADDR']); |
| 70 |
} |
| 71 |
return $ip = 'unknown'; // Not possible. |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Gets a valid/public IP address. |
| 76 |
* |
| 77 |
* @since 161110 IP utilities. |
| 78 |
* |
| 79 |
* @param string $list_of_possible_ips A single IP, or a comma-delimited list of IPs. |
| 80 |
* |
| 81 |
* @return string A valid/public IP address (if one is found), else an empty string. |
| 82 |
* |
| 83 |
* @note This supports both IPv4 and IPv6 addresses. |
| 84 |
* @note See my tests against this here: http://3v4l.org/fVWUp |
| 85 |
*/ |
| 86 |
public static function valid_public($list_of_possible_ips) |
| 87 |
{ |
| 88 |
if (!$list_of_possible_ips || !is_string($list_of_possible_ips)) { |
| 89 |
return ''; // Empty or invalid data. |
| 90 |
} elseif (!($list_of_possible_ips = trim($list_of_possible_ips))) { |
| 91 |
return ''; // Not possible; i.e., empty string. |
| 92 |
} |
| 93 |
foreach (preg_split('/[\s;,]+/', $list_of_possible_ips, -1, PREG_SPLIT_NO_EMPTY) as $_key => $_possible_ip) { |
| 94 |
if (($_valid_public_ip = filter_var(strtolower($_possible_ip), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))) { |
| 95 |
return $_valid_public_ip; // A valid public IPv4 or IPv6 address. |
| 96 |
} |
| 97 |
} // unset($_key, $_possible_ip, $_valid_public_ip); // Housekeeping. |
| 98 |
|
| 99 |
return ''; // Default return value. |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|