Merge pull request #223 from Focuslinkstech/master

Update Package.php and devices/Radius.php
This commit is contained in:
iBNu Maksum 2024-07-26 16:07:30 +07:00 committed by GitHub
commit 2f1ee0cfbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 121 additions and 30 deletions

View File

@ -235,7 +235,7 @@ class Package
} }
} }
if ($b['status'] == 'off') { if ($b['status'] == 'on') {
$dvc = Package::getDevice($p); $dvc = Package::getDevice($p);
if ($_app_stage != 'demo') { if ($_app_stage != 'demo') {
if (file_exists($dvc)) { if (file_exists($dvc)) {

View File

@ -28,14 +28,56 @@ class Radius
function add_customer($customer, $plan) function add_customer($customer, $plan)
{ {
$b = ORM::for_table('tbl_user_recharges') $p = ORM::for_table('tbl_plans')
->where('customer_id', $customer['id']) ->where('id', $plan['id'])
->where('plan_id', $plan['id']) ->findOne();
->where('status', 'on')
->findMany();
if($b){ $date_only = date("Y-m-d");
$this->customerAddPlan($customer, $plan, $b['expiration'] . ' ' . $b['time']); if ($p['validity_unit'] == 'Period') {
// if customer has attribute Expired Date use it
$day_exp = User::getAttribute("Expired Date", $customer['id']);
if (!$day_exp) {
// if customer no attribute Expired Date use plan expired date
$day_exp = 20;
if ($p['prepaid'] == 'no') {
$day_exp = $p['expired_date'];
}
if (empty($day_exp)) {
$day_exp = 20;
}
}
}
if ($p['validity_unit'] == 'Months') {
$date_exp = date("Y-m-d", strtotime('+' . $p['validity'] . ' month'));
} else if ($p['validity_unit'] == 'Period') {
$date_tmp = date("Y-m-$day_exp", strtotime('+' . $p['validity'] . ' month'));
$dt1 = new DateTime("$date_only");
$dt2 = new DateTime("$date_tmp");
$diff = $dt2->diff($dt1);
$sum = $diff->format("%a"); // => 453
if ($sum >= 35 * $p['validity']) {
$date_exp = date("Y-m-$day_exp", strtotime('+0 month'));
} else {
$date_exp = date("Y-m-$day_exp", strtotime('+' . $p['validity'] . ' month'));
};
$time = date("23:59:00");
} else if ($p['validity_unit'] == 'Days') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' day')));
$date_exp = $datetime[0];
$time = $datetime[1];
} else if ($p['validity_unit'] == 'Hrs') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' hour')));
$date_exp = $datetime[0];
$time = $datetime[1];
} else if ($p['validity_unit'] == 'Mins') {
$datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' minute')));
$date_exp = $datetime[0];
$time = $datetime[1];
}
if ($p) {
$this->customerAddPlan($customer, $plan, $date_exp . ' ' . $time);
} }
} }
@ -219,16 +261,23 @@ class Radius
/** /**
* When add a plan to Customer, use this * When add a plan to Customer, use this
*/ */
public function customerAddPlan($customer, $plan, $expired = null) public function customerAddPlan($customer, $plan, $expired = '')
{ {
global $config; global $config;
if ($this->customerUpsert($customer, $plan)) { if ($this->customerUpsert($customer, $plan)) {
$p = $this->getTableUserPackage()->where_equal('username', $customer['username'])->findOne(); $p = $this->getTableUserPackage()->where_equal('username', $customer['username'])->findOne();
if ($p) { if ($p) {
// if exists // if exists
// session timeout [it reset everyday, am still making my research] we can use it for something like 1H/Day - since it reset daily, and Max-All-Session clear everything
//$this->delAtribute($customer['username'], 'Session-Timeout', 3600); // 3600 = 1 hour
$this->delAtribute($this->getTableCustomer(), 'Max-All-Session', 'username', $customer['username']); $this->delAtribute($this->getTableCustomer(), 'Max-All-Session', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'Max-Volume', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'Max-Data', 'username', $customer['username']); $this->delAtribute($this->getTableCustomer(), 'Max-Data', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'Mikrotik-Rate-Limit', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'WISPr-Session-Terminate-Time', 'username', $customer['username']);
//$this->delAtribute($this->getTableCustomer(), 'Ascend-Data-Rate', 'username', $customer['username']);
//we are removing the below in the next two updates, some users may have that attribute, it will remove them before we remove it
$this->delAtribute($this->getTableCustomer(), 'access-period', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'Max-Volume', 'username', $customer['username']);
$p->groupname = "plan_" . $plan['id']; $p->groupname = "plan_" . $plan['id'];
$p->save(); $p->save();
} else { } else {
@ -238,53 +287,63 @@ class Radius
$p->priority = 1; $p->priority = 1;
$p->save(); $p->save();
} }
$this->addBandwidth($customer, $plan);
if ($plan['type'] == 'Hotspot' && $plan['typebp'] == "Limited") { if ($plan['type'] == 'Hotspot' && $plan['typebp'] == "Limited") {
if ($plan['limit_type'] == "Time_Limit") { if ($plan['limit_type'] == "Time_Limit") {
if ($plan['time_unit'] == 'Hrs') if ($plan['time_unit'] == 'Hrs')
$timelimit = $plan['time_limit'] * 60 * 60; $timelimit = $plan['time_limit'] * 60 * 60;
else else
$timelimit = $plan['time_limit'] * 60; $timelimit = $plan['time_limit'] * 60;
// session timeout [it reset everyday, am still making my research] we can use it for something like 1H/Day - since it reset daily, and Max-All-Session clear everything
//$this->upsertCustomer($customer['username'], 'Session-Timeout', 3600); // 3600 = 1 hour
$this->upsertCustomer($customer['username'], 'Max-All-Session', $timelimit); $this->upsertCustomer($customer['username'], 'Max-All-Session', $timelimit);
$this->upsertCustomer($customer['username'], 'Expire-After', $timelimit);
} else if ($plan['limit_type'] == "Data_Limit") { } else if ($plan['limit_type'] == "Data_Limit") {
if ($plan['data_unit'] == 'GB') if ($plan['data_unit'] == 'GB')
$datalimit = $plan['data_limit'] . "000000000"; $datalimit = $plan['data_limit'] . "000000000";
else else
$datalimit = $plan['data_limit'] . "000000"; $datalimit = $plan['data_limit'] . "000000";
//$this->upsertCustomer($customer['username'], 'Max-Volume', $datalimit);
// Mikrotik Spesific
$this->upsertCustomer($customer['username'], 'Max-Data', $datalimit); $this->upsertCustomer($customer['username'], 'Max-Data', $datalimit);
//$this->upsertCustomer($customer['username'], 'Mikrotik-Total-Limit', $datalimit);
} else if ($plan['limit_type'] == "Both_Limit") { } else if ($plan['limit_type'] == "Both_Limit") {
if ($plan['time_unit'] == 'Hrs') if ($plan['time_unit'] == 'Hrs')
$timelimit = $plan['time_limit'] * 60 * 60; $timelimit = $plan['time_limit'] * 60 * 60;
else else
$timelimit = $plan['time_limit'] * 60; $timelimit = $plan['time_limit'] * 60;
// session timeout [it reset everyday, am still making my research] we can use it for something like 1H/Day - since it reset daily, and Max-All-Session clear everything
//$this->upsertCustomer($customer['username'], 'Session-Timeout', 3600); // 3600 = 1 hour
$this->upsertCustomer($customer['username'], 'Max-All-Session', $timelimit);
if ($plan['data_unit'] == 'GB') if ($plan['data_unit'] == 'GB')
$datalimit = $plan['data_limit'] . "000000000"; $datalimit = $plan['data_limit'] . "000000000";
else else
$datalimit = $plan['data_limit'] . "000000"; $datalimit = $plan['data_limit'] . "000000";
//$this->upsertCustomer($customer['username'], 'Max-Volume', $datalimit);
$this->upsertCustomer($customer['username'], 'Max-All-Session', $timelimit);
// Mikrotik Spesific // Mikrotik Spesific
$this->upsertCustomer($customer['username'], 'Max-Data', $datalimit); $this->upsertCustomer($customer['username'], 'Max-Data', $datalimit);
//$this->upsertCustomer($customer['username'], 'Mikrotik-Total-Limit', $datalimit);
} }
} else { } else {
//$this->delAtribute($this->getTableCustomer(), 'Max-Volume', 'username', $customer['username']); // session timeout [it reset everyday, am still making my research] we can use it for something like 1H/Day - since it reset daily, and Max-All-Session clear everything
//$this->delAtribute($customer['username'], 'Session-Timeout', 3600); // 3600 = 1 hour
$this->delAtribute($this->getTableCustomer(), 'Max-All-Session', 'username', $customer['username']); $this->delAtribute($this->getTableCustomer(), 'Max-All-Session', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'Max-Data', 'username', $customer['username']); $this->delAtribute($this->getTableCustomer(), 'Max-Data', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'Mikrotik-Rate-Limit', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'WISPr-Session-Terminate-Time', 'username', $customer['username']);
//we are removing the below in the next two updates, some users may have that attribute, it will remove them before we remove it
$this->delAtribute($this->getTableCustomer(), 'access-period', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'Max-Volume', 'username', $customer['username']);
} }
$this->disconnectCustomer($customer['username']); $this->disconnectCustomer($customer['username']);
$this->getTableAcct()->where_equal('username', $customer['username'])->delete_many(); $this->getTableAcct()->where_equal('username', $customer['username'])->delete_many();
// expired user // expired user
if ($expired != null) { if ($expired != '') {
//$this->upsertCustomer($customer['username'], 'access-period', strtotime($expired) - time()); //extend session time only if the plan are the same
$this->upsertCustomer($customer['username'], 'Max-All-Session', strtotime($expired) - time()); if ($plan['plan_id'] == $p['plan_id'] && $config['extend_expiry'] != 'no') {
$this->upsertCustomer($customer['username'], 'expiration', date('d M Y H:i:s', strtotime($expired))); // session timeout [it reset everyday, am still making my research] we can use it for something like 1H/Day - since it reset daily, and Max-All-Session clear everything
//$this->upsertCustomer($customer['username'], 'Session-Timeout', 3600); // 3600 = 1 hour
$this->upsertCustomer($customer['username'], 'Max-All-Session', strtotime($expired) - time());
$this->upsertCustomer($customer['username'], 'Expiration', date('d M Y H:i:s', strtotime($expired)));
}
// Mikrotik Spesific // Mikrotik Spesific
$this->upsertCustomer( $this->upsertCustomer(
$customer['username'], $customer['username'],
@ -293,8 +352,11 @@ class Radius
); );
} else { } else {
$this->delAtribute($this->getTableCustomer(), 'Max-All-Session', 'username', $customer['username']); $this->delAtribute($this->getTableCustomer(), 'Max-All-Session', 'username', $customer['username']);
//$this->delAtribute($this->getTableCustomer(), 'access-period', 'username', $customer['username']); $this->delAtribute($this->getTableCustomer(), 'Expiration', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'expiration', 'username', $customer['username']); $this->delAtribute($this->getTableCustomer(), 'Mikrotik-Rate-Limit', 'username', $customer['username']);
$this->delAtribute($this->getTableCustomer(), 'WISPr-Session-Terminate-Time', 'username', $customer['username']);
//we are removing the below in the next two updates, some users may have that attribute, it will remove them before we remove it
$this->delAtribute($this->getTableCustomer(), 'access-period', 'username', $customer['username']);
} }
if ($plan['type'] == 'PPPOE') { if ($plan['type'] == 'PPPOE') {
@ -307,7 +369,8 @@ class Radius
return false; return false;
} }
public function customerUpsert($customer, $plan)
public function customerUpsert($customer, $plan) //Update or Insert customer plan
{ {
if ($plan['type'] == 'PPPOE') { if ($plan['type'] == 'PPPOE') {
$this->upsertCustomer($customer['username'], 'Cleartext-Password', (empty($customer['pppoe_password'])) ? $customer['password'] : $customer['pppoe_password']); $this->upsertCustomer($customer['username'], 'Cleartext-Password', (empty($customer['pppoe_password'])) ? $customer['password'] : $customer['pppoe_password']);
@ -321,9 +384,9 @@ class Radius
return true; return true;
} }
private function delAtribute($tabel, $attribute, $key, $value) private function delAtribute($table, $attribute, $key, $value)
{ {
$r = $tabel->where_equal($key, $value)->whereEqual('attribute', $attribute)->findOne(); $r = $table->where_equal($key, $value)->whereEqual('attribute', $attribute)->findOne();
if ($r) $r->delete(); if ($r) $r->delete();
} }
@ -357,7 +420,8 @@ class Radius
$r->attribute = $attr; $r->attribute = $attr;
$r->op = $op; $r->op = $op;
$r->value = $value; $r->value = $value;
return $r->save(); $r->save();
return true;
} }
/** /**
* To insert or update existing customer Attribute * To insert or update existing customer Attribute
@ -398,4 +462,31 @@ class Radius
} }
return $result; return $result;
} }
public function addBandwidth($customer, $plan)
{
$bw = ORM::for_table("tbl_bandwidth")->find_one($plan['id_bw']);
$unitdown = ($bw['rate_down_unit'] == 'Kbps') ? 'K' : 'M';
$unitup = ($bw['rate_up_unit'] == 'Kbps') ? 'K' : 'M';
// TODO Burst mode [ 2M/1M 256K/128K 128K/64K 1s 1 64K/32K]
if (!empty(trim($bw['burst']))) {
// burst format: 2M/1M 256K/128K 128K/64K 1s 1 64K/32K
$pattern = '/(\d+[KM])\/(\d+[KM]) (\d+[KM])\/(\d+[KM]) (\d+) (\d+) (\d+[KM])\/(\d+[KM])/';
preg_match($pattern, $bw['burst'], $matches);
if (count($matches) == 9) {
$burst = $bw['rate_up'] . $unitup . "/" . $bw['rate_down'] . $unitdown . ' ' . $matches[1] . '/' . $matches[2] . ' ' . $matches[3] . '/' . $matches[4] . ' ' . $matches[5] . ' ' . $matches[6] . ' ' . $matches[7] . '/' . $matches[8];
$this->upsertCustomer($customer['username'], 'Mikrotik-Rate-Limit', $burst);
} else {
_log("Unexpected burst format for customer " . $customer['username']);
}
} else {
//$this->upsertCustomer($customer['username'], 'Ascend-Data-Rate', $this->stringToInteger($bw['rate_up'] . $unitup) . "/" . $this->stringToInteger($bw['rate_down'] . $unitdown));
$this->upsertCustomer($customer['username'], 'Mikrotik-Rate-Limit', $bw['rate_up'] . $unitup . "/" . $bw['rate_down'] . $unitdown);
}
return true;
}
} }