Skip to content

Commit

Permalink
- changed a lot of code to force coding standards
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/Net_IPv4/trunk@201041 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
bate committed Nov 22, 2005
1 parent 955a3f6 commit 5736f13
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions IPv4.php
Expand Up @@ -97,9 +97,9 @@ class Net_IPv4
function validateIP($ip)
{
if ($ip == long2ip(ip2long($ip))) {
return(TRUE);
return true;
} else {
return(FALSE);
return false;
}
}

Expand All @@ -114,7 +114,7 @@ function validateIP($ip)
*/
function check_ip($ip)
{
return($this->validateIP($ip));
return $this->validateIP($ip);
}

/**
Expand All @@ -131,9 +131,9 @@ function check_ip($ip)
function validateNetmask($netmask)
{
if (! in_array($netmask, $GLOBALS['Net_IPv4_Netmask_Map'])) {
return(FALSE);
return false;
}
return(TRUE);
return true;
}

/**
Expand Down Expand Up @@ -177,7 +177,7 @@ function parseAddress($address)
if (strchr($address, "/")) {
$parts = explode("/", $address);
if (! $myself->validateIP($parts[0])) {
return(PEAR::raiseError("invalid IP address"));
return PEAR::raiseError("invalid IP address");
}
$myself->ip = $parts[0];

Expand All @@ -195,7 +195,7 @@ function parseAddress($address)
*/
} else if (strchr($parts[1], ".")) {
if (! $myself->validateNetmask($parts[1])) {
return(PEAR::raiseError("invalid netmask value"));
return PEAR::raiseError("invalid netmask value");
}
$myself->netmask = $parts[1];

Expand All @@ -210,15 +210,15 @@ function parseAddress($address)
* Some unknown format of netmask was entered
*/
} else {
return(PEAR::raiseError("invalid netmask value"));
return PEAR::raiseError("invalid netmask value");
}
$myself->calculate();
return($myself);
return $myself;
} else if ($myself->validateIP($address)) {
$myself->ip = $address;
return($myself);
return $myself;
} else {
return(PEAR::raiseError("invalid IP address"));
return PEAR::raiseError("invalid IP address");
}
}

Expand All @@ -237,7 +237,7 @@ function calculate() {

if (! is_a($this, "net_ipv4")) {
$myself = new Net_IPv4;
return(PEAR::raiseError("cannot calculate on uninstantiated Net_IPv4 class"));
return PEAR::raiseError("cannot calculate on uninstantiated Net_IPv4 class");
}

/* Find out if we were given an ip address in dot quad notation or
Expand All @@ -246,13 +246,13 @@ function calculate() {
*/
if (strlen($this->ip)) {
if (! $this->validateIP($this->ip)) {
return(PEAR::raiseError("invalid IP address"));
return PEAR::raiseError("invalid IP address");
}
$this->long = $this->ip2double($this->ip);
} else if (is_numeric($this->long)) {
$this->ip = long2ip($this->long);
} else {
return(PEAR::raiseError("ip address not specified"));
return PEAR::raiseError("ip address not specified");
}

/*
Expand All @@ -265,59 +265,59 @@ function calculate() {
$validNM_rev = array_flip($validNM);
$this->bitmask = $validNM_rev[$this->netmask];
} else {
return(PEAR::raiseError("netmask or bitmask are required for calculation"));
return PEAR::raiseError("netmask or bitmask are required for calculation");
}
$this->network = long2ip(ip2long($this->ip) & ip2long($this->netmask));
$this->broadcast = long2ip(ip2long($this->ip) |
(ip2long($this->netmask) ^ ip2long("255.255.255.255")));
return(TRUE);
return true;
}

function getNetmask($length) {
if (! PEAR::isError($ipobj = Net_IPv4::parseAddress("0.0.0.0/" . $length))) {
$mask = $ipobj->netmask;
unset($ipobj);
return($mask);
return $mask;
}
return(FALSE);
return false;
}

function getNetLength($netmask) {
if (! PEAR::isError($ipobj = Net_IPv4::parseAddress("0.0.0.0/" . $netmask))) {
$bitmask = $ipobj->bitmask;
unset($ipobj);
return($bitmask);
return $bitmask;
}
return(FALSE);
return false;
}

function getSubnet($ip, $netmask) {
if (! PEAR::isError($ipobj = Net_IPv4::parseAddress($ip . "/" . $netmask))) {
$net = $ipobj->network;
unset($ipobj);
return($net);
return $net;
}
return(FALSE);
return false;
}

function inSameSubnet($ip1, $ip2) {
if (! is_object($ip1) || strcasecmp(get_class($ip1), 'net_ipv4') <> 0) {
$ipobj1 = Net_IPv4::parseAddress($ip1);
if (PEAR::isError($ipobj)) {
return(PEAR::raiseError("IP addresses must be an understood format or a Net_IPv4 object"));
return PEAR::raiseError("IP addresses must be an understood format or a Net_IPv4 object");
}
}
if (! is_object($ip2) || strcasecmp(get_class($ip2), 'net_ipv4') <> 0) {
$ipobj2 = Net_IPv4::parseAddress($ip2);
if (PEAR::isError($ipobj)) {
return(PEAR::raiseError("IP addresses must be an understood format or a Net_IPv4 object"));
return PEAR::raiseError("IP addresses must be an understood format or a Net_IPv4 object");
}
}
if ($ipobj1->network == $ipobj2->network &&
$ipobj1->bitmask == $ipobj2->bitmask) {
return(TRUE);
return true;
}
return(FALSE);
return false;
}

/**
Expand All @@ -326,12 +326,10 @@ function inSameSubnet($ip1, $ip2) {
function atoh($addr)
{
if (! Net_IPv4::validateIP($addr)) {
return(FALSE);
return false;
}
$ap = explode(".", $addr);
return(sprintf("%02x%02x%02x%02x",
$ap[0], $ap[1],
$ap[2], $ap[3]));
return sprintf("%02x%02x%02x%02x", $ap[0], $ap[1], $ap[2], $ap[3]);
}

/**
Expand All @@ -341,18 +339,18 @@ function htoa($addr)
{
if (eregi("^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$",
$addr, $regs)) {
return(hexdec($regs[1]) . "." . hexdec($regs[2]) . "." .
hexdec($regs[3]) . "." . hexdec($regs[4]));
return hexdec($regs[1]) . "." . hexdec($regs[2]) . "." .
hexdec($regs[3]) . "." . hexdec($regs[4]);
}
return(FALSE);
return false;
}
/**
* Converts an IP address to a PHP double. Better than ip2long because
* a long in PHP is a signed integer.
*/
function ip2double($ip)
{
return((double)(sprintf("%u", ip2long($ip))));
return (double)(sprintf("%u", ip2long($ip)));
}

/**
Expand Down Expand Up @@ -387,4 +385,4 @@ function ipInNetwork($ip, $network)
/*
* vim: sts=4 ts=4 sw=4 cindent fdm=marker
*/
?>
?>

0 comments on commit 5736f13

Please sign in to comment.