From d6144537c41f76c3af9ff2e414bda5001e25b838 Mon Sep 17 00:00:00 2001 From: gerandonk Date: Tue, 3 Dec 2024 11:15:15 +0700 Subject: [PATCH] add sync function add sync function fix radius time monthly validity fix sync button for hotspot with uptime limit and data limit --- system/controllers/customers.php | 2 +- system/controllers/home.php | 2 +- system/controllers/plan.php | 2 +- system/devices/MikrotikHotspot.php | 25 ++++++++++++++ system/devices/MikrotikPppoe.php | 5 +++ system/devices/MikrotikVpn.php | 5 +++ system/devices/Radius.php | 54 ++++++++++++++++++++++++------ system/devices/RadiusRest.php | 5 +++ 8 files changed, 86 insertions(+), 14 deletions(-) diff --git a/system/controllers/customers.php b/system/controllers/customers.php index e791563e..02957a89 100644 --- a/system/controllers/customers.php +++ b/system/controllers/customers.php @@ -284,7 +284,7 @@ switch ($action) { if ($_app_stage != 'demo') { if (file_exists($dvc)) { require_once $dvc; - (new $p['device'])->add_customer($c, $p); + (new $p['device'])->sync_customer($c, $p); } else { new Exception(Lang::T("Devices Not Found")); } diff --git a/system/controllers/home.php b/system/controllers/home.php index 56ed7d45..3e8649b7 100644 --- a/system/controllers/home.php +++ b/system/controllers/home.php @@ -110,7 +110,7 @@ if (isset($_GET['sync']) && !empty($_GET['sync'])) { if ($_app_stage != 'demo') { if (file_exists($dvc)) { require_once $dvc; - (new $p['device'])->add_customer($c, $p); + (new $p['device'])->sync_customer($c, $p); } else { new Exception(Lang::T("Devices Not Found")); } diff --git a/system/controllers/plan.php b/system/controllers/plan.php index ccc5ceda..5af4e848 100644 --- a/system/controllers/plan.php +++ b/system/controllers/plan.php @@ -49,7 +49,7 @@ switch ($action) { if ($_app_stage != 'demo') { if (file_exists($dvc)) { require_once $dvc; - (new $p['device'])->add_customer($c, $p); + (new $p['device'])->sync_customer($c, $p); } else { new Exception(Lang::T("Devices Not Found")); } diff --git a/system/devices/MikrotikHotspot.php b/system/devices/MikrotikHotspot.php index 7f115cb3..c82d74df 100644 --- a/system/devices/MikrotikHotspot.php +++ b/system/devices/MikrotikHotspot.php @@ -40,6 +40,31 @@ class MikrotikHotspot } $this->addHotspotUser($client, $plan, $customer); } + + function sync_customer($customer, $plan) + { + $mikrotik = $this->info($plan['routers']); + $client = $this->getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + $t = ORM::for_table('tbl_user_recharges')->where('username', $customer['username'])->where('status', 'on')->find_one(); + if ($t) { + $printRequest = new RouterOS\Request('/ip/hotspot/user/print'); + $printRequest->setArgument('.proplist', '.id,limit-uptime,limit-bytes-total'); + $printRequest->setQuery(RouterOS\Query::where('name', $customer['username'])); + $userInfo = $client->sendSync($printRequest); + $id = $userInfo->getProperty('.id'); + $uptime = $userInfo->getProperty('limit-uptime'); + $data = $userInfo->getProperty('limit-bytes-total'); + if (!empty($id) && (!empty($uptime) || !empty($data))) { + $setRequest = new RouterOS\Request('/ip/hotspot/user/set'); + $setRequest->setArgument('numbers', $id); + $setRequest->setArgument('profile', $t['namebp']); + $client->sendSync($setRequest); + } else { + $this->add_customer($customer, $plan); + } + } + } + function remove_customer($customer, $plan) { diff --git a/system/devices/MikrotikPppoe.php b/system/devices/MikrotikPppoe.php index 6ad96475..a3419d52 100644 --- a/system/devices/MikrotikPppoe.php +++ b/system/devices/MikrotikPppoe.php @@ -77,6 +77,11 @@ class MikrotikPppoe } } } + + function sync_customer($customer, $plan) + { + $this->add_customer($customer, $plan); + } function remove_customer($customer, $plan) { diff --git a/system/devices/MikrotikVpn.php b/system/devices/MikrotikVpn.php index 502cd594..fa935b6d 100644 --- a/system/devices/MikrotikVpn.php +++ b/system/devices/MikrotikVpn.php @@ -57,6 +57,11 @@ class MikrotikVpn } } } + + function sync_customer($customer, $plan) + { + $this->add_customer($customer, $plan); + } function remove_customer($customer, $plan) { diff --git a/system/devices/Radius.php b/system/devices/Radius.php index 2ee9afe0..4532be63 100644 --- a/system/devices/Radius.php +++ b/system/devices/Radius.php @@ -50,18 +50,42 @@ class Radius if ($p['validity_unit'] == 'Months') { $date_exp = date("Y-m-d", strtotime('+' . $p['validity'] . ' month')); + $time = date("H:i:s"); } 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"); + $current_date = new DateTime($date_only); + $exp_date = clone $current_date; + $exp_date->modify('first day of next month'); + $exp_date->setDate($exp_date->format('Y'), $exp_date->format('m'), $day_exp); + + $min_days = 7 * $p['validity']; + $max_days = 35 * $p['validity']; + + $days_until_exp = $exp_date->diff($current_date)->days; + + // If less than min_days away, move to the next period + while ($days_until_exp < $min_days) { + $exp_date->modify('+1 month'); + $days_until_exp = $exp_date->diff($current_date)->days; + } + + // If more than max_days away, move to the previous period + while ($days_until_exp > $max_days) { + $exp_date->modify('-1 month'); + $days_until_exp = $exp_date->diff($current_date)->days; + } + + // Final check to ensure we're not less than min_days or in the past + if ($days_until_exp < $min_days || $exp_date <= $current_date) { + $exp_date->modify('+1 month'); + } + + // Adjust for multiple periods + if ($p['validity'] > 1) { + $exp_date->modify('+' . ($p['validity'] - 1) . ' months'); + } + + $date_exp = $exp_date->format('Y-m-d'); + $time = "23:59:59"; } else if ($p['validity_unit'] == 'Days') { $datetime = explode(' ', date("Y-m-d H:i:s", strtotime('+' . $p['validity'] . ' day'))); $date_exp = $datetime[0]; @@ -80,6 +104,14 @@ class Radius $this->customerAddPlan($customer, $plan, $date_exp . ' ' . $time); } } + + function sync_customer($customer, $plan) + { + $t = ORM::for_table('tbl_user_recharges')->where('username', $customer['username'])->where('status', 'on')->findOne(); + $date_exp = $t['expiration']; + $time = $t['time']; + $this->customerAddPlan($customer, $plan, $date_exp . ' ' . $time); + } function remove_customer($customer, $plan) { diff --git a/system/devices/RadiusRest.php b/system/devices/RadiusRest.php index 51f9f236..68522170 100644 --- a/system/devices/RadiusRest.php +++ b/system/devices/RadiusRest.php @@ -21,6 +21,11 @@ class RadiusRest { function add_customer($customer, $plan) { } + + function sync_customer($customer, $plan) + { + $this->add_customer($customer, $plan); + } // Remove Customer to Mikrotik/Device function remove_customer($customer, $plan)