Moved ipv6 validation in to function.class.php and made the validate code a lot prettier

This commit is contained in:
michael@ilovecode.dk 2014-04-26 09:22:57 +02:00
parent f525f31d6a
commit ce5e1d8bc6
2 changed files with 16 additions and 20 deletions

View File

@ -529,6 +529,21 @@ function psm_is_cli() {
return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
}
/**
* Check if ip is IPv6 or not
*
* @param string $ip
* @return boolean
*/
function psm_validate_ipv6($ip) {
// if $ip is a valid ipv6 address it returns true
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){
return true;
} else {
return false;
}
}
###############################################
#
# Debug functions

View File

@ -243,7 +243,7 @@ class StatusUpdater {
//if(psm_is_cli()) {
// IPv6 ready
if ($this->is_ipv6($this->server['ip'])) {
if (psm_validate_ipv6($this->server['ip'])) {
$socket = socket_create(AF_INET6, SOCK_RAW, 1);
} else {
$socket = socket_create(AF_INET, SOCK_RAW, 1);
@ -286,23 +286,4 @@ class StatusUpdater {
public function getRtime() {
return $this->rtime;
}
/**
* Test if ip is IPv6
* @param string $ip
* @return boolean
*/
private function is_ipv6($ip) {
// If it contains anything other than hex characters, periods, colons or a / it's not IPV6
if (!preg_match("/^([0-9a-f\.\/:]+)$/",strtolower($ip))) { return false; }
// An IPV6 address needs at minimum two colons in it
if (substr_count($ip,":") < 2) { return false; }
// If any of the "octets" are longer than 4 characters it's not valid
$part = preg_split("/[:\/]/",$ip);
foreach ($part as $i) { if (strlen($i) > 4) { return false; } }
return true;
}
}