Revize 74a7dbca
Přidáno uživatelem Michal Kliment před více než 9 roky(ů)
application/helpers/network.php | ||
---|---|---|
*
|
||
* @author Michal Kliment
|
||
* @param integer $speed In B/s
|
||
* @param integer $delimiter
|
||
* @return string
|
||
*/
|
||
public static function speed($speed)
|
||
public static function speed($speed, $delimiter = 1024)
|
||
{
|
||
// default unit is nothing
|
||
$unit = '';
|
||
|
||
if ($speed >= 1024)
|
||
if ($speed >= $delimiter)
|
||
{
|
||
$unit = 'k';
|
||
$speed = round($speed / 1024, 2);
|
||
$speed = round($speed / $delimiter, 2);
|
||
|
||
// size is too big
|
||
if ($speed >= 1024)
|
||
if ($speed >= $delimiter)
|
||
{
|
||
// transforms to Mega
|
||
$unit = 'M';
|
||
$speed = round($speed / 1024, 2);
|
||
$speed = round($speed / $delimiter, 2);
|
||
|
||
// size is still too big
|
||
if ($speed >= 1024)
|
||
if ($speed >= $delimiter)
|
||
{
|
||
// transforms to Giga
|
||
$unit = 'G';
|
||
$speed = round($speed / 1024, 2);
|
||
$speed = round($speed / $delimiter, 2);
|
||
|
||
// size is still too big
|
||
if ($speed >= 1024)
|
||
if ($speed >= $delimiter)
|
||
{
|
||
// transforms to Giga
|
||
$unit = 'T';
|
||
$speed = round($speed / 1024, 2);
|
||
$speed = round($speed / $delimiter, 2);
|
||
}
|
||
}
|
||
}
|
||
... | ... | |
|
||
foreach ($ranges as $range_address)
|
||
{
|
||
// address contains / => it's in CIDR format
|
||
if (strpos($range_address, '/') !== FALSE)
|
||
// split address and mask
|
||
list ($range_address, $range_mask) = explode('/', $range_address);
|
||
// address is without / => it's single address
|
||
else
|
||
$range_mask = 32;
|
||
|
||
$net = ip2long($range_address);
|
||
$mask = ip2long(network::cidr2netmask($range_mask));
|
||
|
||
// success
|
||
if (($ip_address & $mask) == $net)
|
||
if (self::ip_address_in_range($ip_address, $range_address))
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Checks whether IP address belongs to given address range
|
||
*
|
||
* @author Michal Kliment
|
||
* @param type $ip_address
|
||
* @param type $range_address
|
||
* @return boolean
|
||
*/
|
||
public static function ip_address_in_range($ip_address, $range_address)
|
||
{
|
||
// address contains / => it's in CIDR format
|
||
if (strpos($range_address, '/') !== FALSE)
|
||
// split address and mask
|
||
list ($range_address, $range_mask) = explode('/', $range_address);
|
||
// address is without / => it's single address
|
||
else
|
||
$range_mask = 32;
|
||
|
||
if (valid::ip($ip_address))
|
||
$ip_address = ip2long ($ip_address);
|
||
|
||
$net = ip2long($range_address);
|
||
$mask = ip2long(network::cidr2netmask($range_mask));
|
||
|
||
// success
|
||
if (($ip_address & $mask) == $net)
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* Converts speed string to integer (bytes)
|
||
*
|
||
... | ... | |
);
|
||
}
|
||
|
||
/**
|
||
* Converts MAC address in binary format to classic format
|
||
*
|
||
* @author Michal Kliment
|
||
* @param string $str
|
||
* @return string|boolean
|
||
*/
|
||
public static function bin2mac ($str)
|
||
{
|
||
$str = bin2hex($str);
|
||
|
||
$regex = '/([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/';
|
||
|
||
$matches = array();
|
||
|
||
if (preg_match($regex, $str, $matches))
|
||
{
|
||
unset($matches[0]);
|
||
|
||
return implode(':', $matches);
|
||
}
|
||
else
|
||
{
|
||
return FALSE;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Converts MAC address in decimal format to classic format
|
||
*
|
||
* @author Michal Kliment
|
||
* @param type $str
|
||
* @return boolean
|
||
*/
|
||
public static function dec2mac ($str)
|
||
{
|
||
$pieces = explode('.', $str);
|
||
|
||
if (count ($pieces) != 6)
|
||
return FALSE;
|
||
|
||
$pieces = array_map('dechex', $pieces);
|
||
|
||
$pieces = array_map(
|
||
'num::null_fill',
|
||
$pieces, array('2', '2', '2', '2', '2', '2')
|
||
);
|
||
|
||
return implode(':', $pieces);
|
||
}
|
||
}
|
Také k dispozici: Unified diff
Merge from SVN branch 1.2.