Location: PHPKode > projects > phpBandwidth > phpbandwidth/user_functions.php
<?
function conv_mb($bytes){
	return $bytes / 1048576; // 1024*1024 = 1048576
}
function conv_kb($bytes){
	return $bytes / 1024; // 1024*1024 = 1048576
}

if (! isset($current_year))
	$current_year = date('Y');
if (! isset($current_month))
	$current_month = date('m');

function html_headers(){
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");			// Date in the past
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");		// always modified
	header("Cache-Control: no-store, no-cache, must-revalidate");  		// HTTP/1.1
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");                          			// HTTP/1.0
}

function html_bar_month($main_width, $width, $height){
	 return html_double_bar($main_width, $width, $height, 
				// Seconds into this month - timezone / seconds in month
				(mktime(0,0,0,1,date('d'),1970) + date('Z'))   /   (date('t') * 3600 * 24)
				*100 . "%", 10, "green_bar.jpg");
}
function html_bar_day($main_width, $width, $height){
	$time = getdate(time());
	
	 return html_double_bar($main_width, $width, $height, 
				// Seconds into day- total seconds in a day
				(mktime($time[hours],$time[minutes],$time[seconds],1,1,1970) + date('Z'))   /   (24 * 3600)
				*100 . "%", 10, "green_bar.jpg");
		
}

function html_double_bar($main_width, $width, $height, $width2, $height2, $bar="bar.gif")
{
if (strpos($width, "%") !== FALSE){
	if ($width > 100){
		$over = round($width - 100);
		if ($over > 100)
			$over = 100;
		$width = 100 - $over;
	}
	$width = number_format($main_width * $width / 100, 0);
	$over  = number_format($main_width * $over / 100, 0);
}
if (strpos($width2, "%") !== FALSE){
	if ($width2 > 100)
		$width2 = 100;
	$width2 = number_format($main_width * $width2 / 100, 0);
}
if (! is_numeric($width))
	$width =0;
if (! is_numeric($width2))
	$width2 =0;
$data .= "
<table bgcolor=\"#999999\" cellpadding=\"0\" cellspacing=\"0\" border=0>
<tr>
  <td><img src=\"dot.jpg\" alt=\"\" width=$main_width height=1></td>
</tr>
<tr>
  <td align=left><img src=\"bar.gif\" alt=\"\" width=$width height=$height>";
  if ($over)
	$data .=  "<img src=\"over_bar.jpg\" alt=\"\" width=$over height=$height>";
$data .= "</td>
</tr>
<tr>
  <td><img src=\"dot.jpg\" alt=\"\" width=$main_width height=1></td>
</tr>
<tr>
  <td align=left><img src=\"$bar\" alt=\"\" width=$width2 height=$height2></td>
</tr>
</table>";
return $data;
}

function html_bar($main_width, $width, $height = 25)
{
echo "\n<!-- html_bar start -->\n";
if (strpos($width, "%") !== FALSE){
	if ($width > 100){
		$over = round($width - 100);
		if ($over > 100)
			$over = 100;
		$width = 100 - $over;
	}
	$width = number_format($main_width * $width / 100, 0);
	$over  = number_format($main_width * $over / 100, 0);
}

if (! is_numeric($width))
	$width =0;
$data .= "
<table bgcolor=\"#999999\" cellpadding=\"0\" cellspacing=\"0\" border=0>
<tr><td><img src=\"dot.jpg\" alt=\"\" width=$main_width height=1></td></tr>
<tr><td align=left><img src=\"bar.gif\" alt=\"\" width=$width height=$height>";

if ($over)
	$data .=  "<img src=\"over_bar.jpg\" alt=\"\" width=$over height=$height>";

$data .= "</td></tr>
<tr><td><img src=\"dot.jpg\" alt=\"\" width=$main_width height=1></td></tr>
</table>
<!-- html_bar stop  -->\n";
return $data;
}

function get_bw_from_buffer($buff, $chain, $route_nr)
{
	for ($i=0; $i < count($buff);$i++)
		if (strncmp("Chain $chain", $buff[$i], 6+strlen($chain)) == 0){
			$tok = strtok($buff[$i + $route_nr +1]," ");
			$tok = strtok(" ");
			return $tok;
		}

}
function get_unlogged_bw($conn, $uid, $in="", $iptables)
{
	$routes = db_get_users_routes($conn, $uid, "in");
	while (list ($key, $route) = each ($routes))
		$in_bandwidth += get_bw_from_buffer($iptables, $route[chain], $route[route_nr]);

	$routes = db_get_users_routes($conn, $uid, "out");
	while (list ($key, $route) = each ($routes))
		$out_bandwidth += get_bw_from_buffer($iptables, $route[chain], $route[route_nr]);

	$unlogged_bw_in  = $in_bandwidth  - db_get_bw_today($conn, $uid, "in");
	$unlogged_bw_out = $out_bandwidth - db_get_bw_today($conn, $uid, "out");
	$unlogged_bw_in  = $in_bandwidth;
	$unlogged_bw_out = $out_bandwidth;
	if ($in == "in")
		return $unlogged_bw_in;
	if ($in == "out")
		return $unlogged_bw_out;
	return $unlogged_bw_out + $unlogged_bw_in;
}

function fill_bandwidth_array($conn, $uid){
	exec( "sudo /sbin/iptables -v -x -L  " , $iptables );
	
	$bandwidth[limit]		= db_get_user_download_limit($conn, $uid);
	$bandwidth[unlogged_in]		= conv_mb(get_unlogged_bw($conn, $uid, "in", $iptables));
	$bandwidth[unlogged_out]	= conv_mb(get_unlogged_bw($conn, $uid, "out", $iptables));
	$bandwidth[logged_in]		= conv_mb(db_get_total_bw  ($conn, $uid, 't'));
	$bandwidth[logged_out]		= conv_mb(db_get_total_bw  ($conn, $uid, 'f'));
	$bandwidth[today_in]		= conv_mb(db_get_bw_today($conn, $uid, "in")) + $bandwidth[unlogged_in];
	$bandwidth[today_out]		= conv_mb(db_get_bw_today($conn, $uid, "out")) + $bandwidth[unlogged_out];
	$bandwidth[today_total]		= $bandwidth[today_in] + $bandwidth[today_out]	;
	$bandwidth[total_in]		= $bandwidth[unlogged_in] + $bandwidth[logged_in];
	$bandwidth[total_out]		= $bandwidth[unlogged_out] + $bandwidth[logged_out];
	$bandwidth[total]		= $bandwidth[total_in] + $bandwidth[total_out];
	$bandwidth[left]		= $bandwidth[limit] - $bandwidth[total];
	return $bandwidth;
}

function store_bandwidth($conn, $users, $clear="no")
{
	global $phpbandwidth;
	exec( "sudo /sbin/iptables -v -x -Z -L  " , $iptables );

	while (list ($uid, $name) = each ($users))
	{
	
		$unlogged_bw = get_unlogged_bw($conn, $uid, "in", $iptables);
			if ($unlogged_bw)
				db_store_bw($conn, $uid, $unlogged_bw, "in");
		$unlogged_bw = get_unlogged_bw($conn, $uid, "out", $iptables);
			if ($unlogged_bw)
				db_store_bw($conn, $uid, $unlogged_bw, "out");
	}
}

?>
Return current item: phpBandwidth