freenetis-github/application/helpers/network.php @ 18ac9009
8baed187 | Michal Kliment | <?php defined('SYSPATH') or die('No direct script access.');
|
|
/*
|
|||
* This file is part of open source system FreenetIS
|
|||
* and it is released under GPLv3 licence.
|
|||
*
|
|||
* More info about licence can be found:
|
|||
* http://www.gnu.org/licenses/gpl-3.0.html
|
|||
*
|
|||
* More info about project can be found:
|
|||
* http://www.freenetis.org/
|
|||
*
|
|||
*/
|
|||
/**
|
|||
* Network helper.
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @package Helper
|
|||
*/
|
|||
class network
|
|||
{
|
|||
private static $sizes = array
|
|||
(
|
|||
'K' => 1024,
|
|||
'M' => 1048576,
|
|||
'G' => 1073741824,
|
|||
'T' => 1099511627776
|
|||
);
|
|||
/**
|
|||
* Converts netmask in classic format (eg. 255.255.255.0) to CIDR format (/24)
|
|||
* @author Michal Kliment
|
|||
* @param string
|
|||
* @return string
|
|||
*/
|
|||
public static function netmask2cidr($netmask)
|
|||
{
|
|||
if (!preg_match("/^\d+\.\d+\.\d+\.\d+$/", $netmask))
|
|||
{
|
|||
return false;
|
|||
}
|
|||
return 32 - log((~ip2long($netmask) & 0xffffffff) + 1, 2);
|
|||
}
|
|||
/**
|
|||
* Converts netmask in CIDR format (eg. 24) to classic format (255.255.255.0)
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @param string $cidr
|
|||
* @return string
|
|||
*/
|
|||
public static function cidr2netmask($cidr)
|
|||
{
|
|||
if (!preg_match("/^\d+$/", $cidr))
|
|||
{
|
|||
return false;
|
|||
}
|
|||
return long2ip(~(pow(2, 32 - $cidr) - 1) & 0xffffffff);
|
|||
}
|
|||
/**
|
|||
* Formats size
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @param integer $size
|
|||
* @return string
|
|||
*/
|
|||
public static function size($size, $byte = TRUE)
|
|||
{
|
|||
// default unit is kilo
|
|||
$unit = 'k';
|
|||
// size is too big
|
|||
if ($size >= 1024)
|
|||
{
|
|||
// transforms to Mega
|
|||
$unit = 'M';
|
|||
$size = round($size / 1024, 2);
|
|||
// size is still too big
|
|||
if ($size >= 1024)
|
|||
{
|
|||
// transforms to Giga
|
|||
$unit = 'G';
|
|||
$size = round($size / 1024, 2);
|
|||
// size is still too big
|
|||
if ($size >= 1024)
|
|||
{
|
|||
// transforms to Giga
|
|||
$unit = 'T';
|
|||
$size = round($size / 1024, 2);
|
|||
}
|
|||
}
|
|||
}
|
|||
$unit .= ($byte) ? 'B' : 'b';
|
|||
return ($size) ? $size . ' ' . $unit : '0 ' . $unit;
|
|||
}
|
|||
c1bdc1c4 | Michal Kliment | /**
|
|
* Formats speed
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @param integer $speed In B/s
|
|||
18ac9009 | Ondřej Fibich | * @param integer $delimiter
|
|
c1bdc1c4 | Michal Kliment | * @return string
|
|
*/
|
|||
18ac9009 | Ondřej Fibich | public static function speed($speed, $delimiter = 1024)
|
|
c1bdc1c4 | Michal Kliment | {
|
|
// default unit is nothing
|
|||
$unit = '';
|
|||
18ac9009 | Ondřej Fibich | if ($speed >= $delimiter)
|
|
c1bdc1c4 | Michal Kliment | {
|
|
$unit = 'k';
|
|||
18ac9009 | Ondřej Fibich | $speed = round($speed / $delimiter, 2);
|
|
c1bdc1c4 | Michal Kliment | ||
// size is too big
|
|||
18ac9009 | Ondřej Fibich | if ($speed >= $delimiter)
|
|
c1bdc1c4 | Michal Kliment | {
|
|
// transforms to Mega
|
|||
$unit = 'M';
|
|||
18ac9009 | Ondřej Fibich | $speed = round($speed / $delimiter, 2);
|
|
c1bdc1c4 | Michal Kliment | ||
// size is still too big
|
|||
18ac9009 | Ondřej Fibich | if ($speed >= $delimiter)
|
|
c1bdc1c4 | Michal Kliment | {
|
|
// transforms to Giga
|
|||
$unit = 'G';
|
|||
18ac9009 | Ondřej Fibich | $speed = round($speed / $delimiter, 2);
|
|
c1bdc1c4 | Michal Kliment | ||
// size is still too big
|
|||
18ac9009 | Ondřej Fibich | if ($speed >= $delimiter)
|
|
c1bdc1c4 | Michal Kliment | {
|
|
// transforms to Giga
|
|||
$unit = 'T';
|
|||
18ac9009 | Ondřej Fibich | $speed = round($speed / $delimiter, 2);
|
|
c1bdc1c4 | Michal Kliment | }
|
|
}
|
|||
}
|
|||
}
|
|||
return ($speed) ? $speed . $unit : '0' . $unit;
|
|||
}
|
|||
8baed187 | Michal Kliment | /**
|
|
* Checks whether ip address belongs to default address ranges
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @param string $ip_address
|
|||
* @return boolean
|
|||
*/
|
|||
public static function ip_address_in_ranges($ip_address)
|
|||
{
|
|||
// default address range is not set, return true
|
|||
if (($ranges = Settings::get('address_ranges')) == '')
|
|||
return true;
|
|||
// transform ip to long
|
|||
$ip_address = ip2long($ip_address);
|
|||
// transform string to array
|
|||
$ranges = explode(',', $ranges);
|
|||
foreach ($ranges as $range_address)
|
|||
{
|
|||
18ac9009 | Ondřej Fibich | if (self::ip_address_in_range($ip_address, $range_address))
|
|
8baed187 | Michal Kliment | return true;
|
|
}
|
|||
return false;
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | /**
|
|
* 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;
|
|||
}
|
|||
8baed187 | Michal Kliment | /**
|
|
c1bdc1c4 | Michal Kliment | * Converts speed string to integer (bytes)
|
|
8baed187 | Michal Kliment | *
|
|
* @author Michal Kliment
|
|||
* @param string $str
|
|||
* @return integer
|
|||
*/
|
|||
public static function str2bytes ($str)
|
|||
{
|
|||
$unit = strtoupper(substr($str,-1));
|
|||
$size = isset(self::$sizes[$unit]) ? self::$sizes[$unit] : 1;
|
|||
return ((int) $str) * $size;
|
|||
}
|
|||
/**
|
|||
* Converts speed string to array
|
|||
*
|
|||
* @param string $str
|
|||
* @return array
|
|||
*/
|
|||
public static function str2array ($str)
|
|||
{
|
|||
$unit = strtoupper(substr($str,-1));
|
|||
if (!isset(self::$sizes[$unit]))
|
|||
$unit = "";
|
|||
return array
|
|||
(
|
|||
'size' => (int) $str,
|
|||
'unit' => $unit
|
|||
);
|
|||
}
|
|||
/**
|
|||
c1bdc1c4 | Michal Kliment | * Converts integer (bytes) to speed string
|
|
8baed187 | Michal Kliment | *
|
|
* @author Michal Kliment
|
|||
* @param type $bytes
|
|||
* @param type $unit
|
|||
* @return type
|
|||
*/
|
|||
public static function bytes2str ($bytes, $unit = "")
|
|||
{
|
|||
$size = isset(self::$sizes[$unit]) ? self::$sizes[$unit] : 1;
|
|||
return (ceil($bytes/$size*10)/10).$unit;
|
|||
}
|
|||
/**
|
|||
* Tranfers bytes from unit do unit
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @param type $size
|
|||
* @param type $from_unit
|
|||
* @param type $to_unit
|
|||
* @return type
|
|||
*/
|
|||
public static function transfer_unit ($size, $from_unit, $to_unit)
|
|||
{
|
|||
$from_size = isset(self::$sizes[$from_unit]) ? self::$sizes[$from_unit] : 1;
|
|||
$to_size = isset(self::$sizes[$to_unit]) ? self::$sizes[$to_unit] : 1;
|
|||
return ceil($size*($from_size/$to_size*10))/10;
|
|||
}
|
|||
/**
|
|||
* Parses speed string to array
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @param type $str
|
|||
* @param type $unit
|
|||
* @return type
|
|||
*/
|
|||
public static function speed_size ($str, $unit = 'M')
|
|||
{
|
|||
if (!valid::speed_size($str))
|
|||
return FALSE;
|
|||
$pieces = explode("/", $str);
|
|||
$upload_str = $pieces[0];
|
|||
$download_str = $pieces[count($pieces)-1];
|
|||
$upload = network::str2array($upload_str);
|
|||
$download = network::str2array($download_str);
|
|||
return array
|
|||
(
|
|||
'upload' => network::transfer_unit($upload['size'], $upload['unit'], $unit),
|
|||
'download' => network::transfer_unit($download['size'], $download['unit'], $unit),
|
|||
'unit' => $unit
|
|||
);
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | /**
|
|
* 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);
|
|||
}
|
|||
8baed187 | Michal Kliment | }
|