<?php
class FileExtractor {
private $userName;
private $world = "billing/world/";
private $city = "billing/city/";
private $router = "billing/router/";
private $date;
function __construct() {
//////////////////////////////////////////////////
$this->userName['192.168.100.253'] = "Nokia E51";
////////////////////////////////////////////////////
$this->date = date('Y-m-d');
}
public function getUserName($ip) {
if($this->userName != null && array_key_exists($ip, $this->userName)){
return $this->userName[$ip];
}
return $ip;
}
public function getUserTrafByDay(&$totalDay, &$totalIP) {
$fileNames = scandir($this->world);
foreach ($fileNames as $fileName) {
if ($fileName != "." && $fileName != "..") {
$handle1 = fopen($this->world.$fileName, "r");
$handle2 = fopen($this->city.$fileName, "r");
$sum['win'] = '0';
$sum['wout'] = '0';
$sum['cin'] = '0';
$sum['cout'] = '0';
while ($userinfo = fscanf($handle1, "%s\t%s\t%s\n")) {
$userByIp[$userinfo[0]]['win'] = $userinfo[1];
$userByIp[$userinfo[0]]['wout'] = $userinfo[2];
$sum['win'] = bcadd($sum['win'], $userinfo[1]);
$sum['wout'] = bcadd($sum['wout'], $userinfo[2]);
if (isset($totalIP[$userinfo[0]])) {
$totalIP[$userinfo[0]]['win'] = bcadd($totalIP[$userinfo[0]]['win'], $userinfo[1]);
$totalIP[$userinfo[0]]['wout'] = bcadd($totalIP[$userinfo[0]]['wout'], $userinfo[2]);
}
else {
$totalIP[$userinfo[0]]['win'] = $userinfo[1];
$totalIP[$userinfo[0]]['wout'] = $userinfo[2];
$totalIP[$userinfo[0]]['cin'] = '0';
$totalIP[$userinfo[0]]['cout'] = '0';
}
}
while ($userinfo = fscanf($handle2, "%s\t%s\t%s\n")) {
$userByIp[$userinfo[0]]['cin'] = $userinfo[1];
$userByIp[$userinfo[0]]['cout'] = $userinfo[2];
$sum['cin'] = bcadd($sum['cin'], $userinfo[1]);
$sum['cout'] = bcadd($sum['cout'], $userinfo[2]);
if (isset($totalIP[$userinfo[0]])) {
$totalIP[$userinfo[0]]['cin'] = bcadd($totalIP[$userinfo[0]]['cin'], $userinfo[1]);
$totalIP[$userinfo[0]]['cout'] = bcadd($totalIP[$userinfo[0]]['cout'], $userinfo[2]);
}
else {
$totalIP[$userinfo[0]]['cin'] = $userinfo[1];
$totalIP[$userinfo[0]]['cout'] = $userinfo[2];
}
}
fclose($handle1);
fclose($handle2);
$trafByDay[$fileName] = $userByIp;
$totalDay[$fileName] = $sum;
unset ($userByIp);
unset ($sum);
}
}
return $trafByDay;
}
public function getRouterInfo(&$rTotal){
$fileNames = scandir($this->router);
$rTotal['win'] = '0';
$rTotal['wout'] = '0';
$rTotal['cin'] = '0';
$rTotal['cout'] = '0';
foreach ($fileNames as $fileName) {
if ($fileName != "." && $fileName != "..") {
$handle = fopen($this->router.$fileName, "r");
$info = fscanf($handle, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n");
$routerInfo[$fileName]['win'] = $info[0];
$routerInfo[$fileName]['wout'] = $info[1];
$routerInfo[$fileName]['cin'] = $info[2];
$routerInfo[$fileName]['cout'] = $info[3];
$rTotal['win'] = bcadd($rTotal['win'] , $info[0]);
$rTotal['wout'] = bcadd($rTotal['wout'] , $info[1]);
$rTotal['cin'] = bcadd($rTotal['cin'] , $info[2]);
$rTotal['cout'] = bcadd($rTotal['cout'] , $info[3]);
$routerInfo[$fileName]['dwin'] = $info[4];
$routerInfo[$fileName]['dwout'] = $info[5];
$routerInfo[$fileName]['dcin'] = $info[6];
$routerInfo[$fileName]['dcout'] = $info[7];
fclose($handle);
}
}
return $routerInfo;
}
public function getDayTraffic($type){
switch ($type) {
case 'city':
$fileName = $this->city.$this->date;
break;
case 'world':
$fileName = $this->world.$this->date;
break;
default :
return;
}
$handle = fopen($fileName, "r");
while ($userinfo = fscanf($handle, "%s\t%s\t%s\t%s\t%s\n")) {
$userByIp[$userinfo[0]]['in'] = $userinfo[1];
$userByIp[$userinfo[0]]['out'] = $userinfo[2];
$userByIp[$userinfo[0]]['din'] = $userinfo[3];
$userByIp[$userinfo[0]]['dout'] = $userinfo[4];
}
fclose($handle);
return $userByIp;
}
}
?>
<?php
class Limit {
private $limitFile = "billing/limit";
private $users = "/usr/local/etc/ethers";
private $userMAC;
private $userLimit;
private function getLimit() {
$handle = fopen($this->limitFile, "r");
while ($userinfo = fscanf($handle, "%s\t%s\t%s\t%s\n")) {
$this->userLimit[$userinfo[0]]['in'] = $userinfo[1];
$this->userLimit[$userinfo[0]]['out'] = $userinfo[2];
$this->userLimit[$userinfo[0]]['total'] = $userinfo[3];
}
fclose($handle);
}
private function getUserMAC() {
$handle = fopen($this->users, "r");
while ($userinfo = fscanf($handle, "%s\t%s\n")) {
$this->userMAC[$userinfo[1]] = $userinfo[0];
}
fclose($handle);
}
public function disableUsers() {
$this->getLimit();
$this->getUserMAC();
include_once("FileExtractor.php");
$fe = new FileExtractor();
$trafByDay = $fe->getUserTrafByDay($totalDay, $totalIP);
foreach ($this->userLimit as $ip => $limit) {
if (isset ($this->userMAC[$ip]) && isset ($totalIP[$ip]) &&
( bccomp(bcdiv($totalIP[$ip]['win'], '1048576'), $limit['in']) == 1
|| bccomp(bcdiv($totalIP[$ip]['wout'], '1048576'), $limit['out']) == 1
|| bccomp(bcdiv(bcadd($totalIP[$ip]['win'], $totalIP[$ip]['wout']), '1048576'), $limit['total']) == 1
)
){
shell_exec('iptables -D MAC_IP -s '.$ip.' -m mac --mac-source '.$this->userMAC[$ip].' -j RETURN');
}
}
}
}
$limit = new Limit();
$limit->disableUsers();
?>