1302 lines
48 KiB
PHP
Raw Permalink Normal View History

2017-03-11 02:51:06 +07:00
<?php
2023-10-16 13:54:34 +07:00
2017-03-11 02:51:06 +07:00
/**
2023-10-12 15:55:42 +07:00
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
2022-08-23 16:33:21 +07:00
**/
2017-03-11 02:51:06 +07:00
_admin();
2024-02-13 13:54:01 +07:00
$ui->assign('_title', Lang::T('Hotspot Plans'));
2017-03-11 02:51:06 +07:00
$ui->assign('_system_menu', 'services');
$action = $routes['1'];
$ui->assign('_admin', $admin);
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) {
2024-03-18 22:55:08 +03:00
_alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard");
2017-03-11 02:51:06 +07:00
}
switch ($action) {
2023-09-15 14:03:23 +07:00
case 'sync':
set_time_limit(-1);
if ($routes['2'] == 'hotspot') {
2024-06-20 14:57:19 +07:00
$plans = ORM::for_table('tbl_plans')->where('type', 'Hotspot')->find_many();
2023-09-15 14:03:23 +07:00
$log = '';
foreach ($plans as $plan) {
2024-06-05 17:19:24 +07:00
$dvc = Package::getDevice($plan);
2024-06-20 14:16:09 +07:00
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
2024-06-20 14:57:19 +07:00
(new $plan['device'])->add_plan($plan);
$log .= "DONE : $plan[name_plan], $plan[device]<br>";
2024-06-20 14:16:09 +07:00
} else {
2024-06-20 14:57:19 +07:00
$log .= "FAILED : $plan[name_plan], $plan[device] | Device Not Found<br>";
2024-06-20 14:16:09 +07:00
}
2023-09-15 14:03:23 +07:00
}
}
2025-01-31 16:22:58 +07:00
r2(getUrl('services/hotspot'), 's', $log);
2023-10-04 11:37:32 +07:00
} else if ($routes['2'] == 'pppoe') {
2024-06-20 14:57:19 +07:00
$plans = ORM::for_table('tbl_plans')->where('type', 'PPPOE')->find_many();
2023-09-15 14:03:23 +07:00
$log = '';
foreach ($plans as $plan) {
2024-06-05 17:19:24 +07:00
$dvc = Package::getDevice($plan);
2024-06-20 14:16:09 +07:00
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
2024-06-20 14:57:19 +07:00
(new $plan['device'])->add_plan($plan);
$log .= "DONE : $plan[name_plan], $plan[device]<br>";
2024-06-20 14:16:09 +07:00
} else {
2024-06-20 14:57:19 +07:00
$log .= "FAILED : $plan[name_plan], $plan[device] | Device Not Found<br>";
2024-06-20 14:16:09 +07:00
}
2023-09-15 14:03:23 +07:00
}
}
2025-01-31 16:22:58 +07:00
r2(getUrl('services/pppoe'), 's', $log);
2023-09-15 14:03:23 +07:00
}
2025-01-31 16:22:58 +07:00
r2(getUrl('services/hotspot'), 'w', 'Unknown command');
2017-03-11 02:51:06 +07:00
case 'hotspot':
2024-06-21 15:06:31 +07:00
$name = _req('name');
$type1 = _req('type1');
$type2 = _req('type2');
$type3 = _req('type3');
$bandwidth = _req('bandwidth');
$valid = _req('valid');
$device = _req('device');
$status = _req('status');
$router = _req('router');
$ui->assign('type1', $type1);
$ui->assign('type2', $type2);
$ui->assign('type3', $type3);
$ui->assign('bandwidth', $bandwidth);
$ui->assign('valid', $valid);
$ui->assign('device', $device);
$ui->assign('status', $status);
$ui->assign('router', $router);
2022-08-23 16:33:21 +07:00
2024-06-21 15:06:31 +07:00
$append_url = "&type1=" . urlencode($type1)
. "&type2=" . urlencode($type2)
. "&type3=" . urlencode($type3)
. "&bandwidth=" . urlencode($bandwidth)
. "&valid=" . urlencode($valid)
. "&device=" . urlencode($device)
. "&status=" . urlencode($status)
. "&router=" . urlencode($router);
$bws = ORM::for_table('tbl_plans')->distinct()->select("id_bw")->where('tbl_plans.type', 'Hotspot')->findArray();
$ids = array_column($bws, 'id_bw');
2024-09-23 16:43:24 +07:00
if (count($ids)) {
$ui->assign('bws', ORM::for_table('tbl_bandwidth')->select("id")->select('name_bw')->where_id_in($ids)->findArray());
2024-09-23 16:43:24 +07:00
} else {
$ui->assign('bws', []);
}
2024-06-21 15:06:31 +07:00
$ui->assign('type2s', ORM::for_table('tbl_plans')->getEnum("plan_type"));
$ui->assign('type3s', ORM::for_table('tbl_plans')->getEnum("typebp"));
$ui->assign('valids', ORM::for_table('tbl_plans')->getEnum("validity_unit"));
$ui->assign('routers', array_column(ORM::for_table('tbl_plans')->distinct()->select("routers")->where('tbl_plans.type', 'Hotspot')->whereNotEqual('routers', '')->findArray(), 'routers'));
$devices = [];
$files = scandir($DEVICE_PATH);
foreach ($files as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == 'php') {
$devices[] = pathinfo($file, PATHINFO_FILENAME);
}
2022-08-23 16:33:21 +07:00
}
2024-06-21 15:06:31 +07:00
$ui->assign('devices', $devices);
$query = ORM::for_table('tbl_bandwidth')
->left_outer_join('tbl_plans', array('tbl_bandwidth.id', '=', 'tbl_plans.id_bw'))
->where('tbl_plans.type', 'Hotspot');
2022-08-23 16:33:21 +07:00
2024-06-21 15:06:31 +07:00
if (!empty($type1)) {
$query->where('tbl_plans.prepaid', $type1);
}
if (!empty($type2)) {
$query->where('tbl_plans.plan_type', $type2);
}
if (!empty($type3)) {
$query->where('tbl_plans.typebp', $type3);
}
if (!empty($bandwidth)) {
$query->where('tbl_plans.id_bw', $bandwidth);
}
if (!empty($valid)) {
$query->where('tbl_plans.validity_unit', $valid);
}
if (!empty($router)) {
if ($router == 'radius') {
$query->where('tbl_plans.is_radius', '1');
} else {
$query->where('tbl_plans.routers', $router);
}
}
if (!empty($device)) {
$query->where('tbl_plans.device', $device);
}
if (in_array($status, ['0', '1'])) {
$query->where('tbl_plans.enabled', $status);
}
if ($name != '') {
$query->where_like('tbl_plans.name_plan', '%' . $name . '%');
}
$d = Paginator::findMany($query, ['name' => $name], 20, $append_url);
2022-08-23 16:33:21 +07:00
$ui->assign('d', $d);
2022-09-18 00:00:40 +07:00
run_hook('view_list_plans'); #HOOK
2025-02-04 10:22:14 +07:00
$ui->display('admin/hotspot/list.tpl');
2017-03-11 02:51:06 +07:00
break;
case 'add':
2022-08-23 16:33:21 +07:00
$d = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('d', $d);
$r = ORM::for_table('tbl_routers')->find_many();
$ui->assign('r', $r);
2024-06-10 17:33:49 +07:00
$devices = [];
$files = scandir($DEVICE_PATH);
2024-06-11 13:28:05 +07:00
foreach ($files as $file) {
2024-06-10 17:33:49 +07:00
$ext = pathinfo($file, PATHINFO_EXTENSION);
2024-06-11 13:28:05 +07:00
if ($ext == 'php') {
2024-06-10 17:33:49 +07:00
$devices[] = pathinfo($file, PATHINFO_FILENAME);
}
}
$ui->assign('devices', $devices);
2022-09-18 00:00:40 +07:00
run_hook('view_add_plan'); #HOOK
2025-02-04 10:22:14 +07:00
$ui->display('admin/hotspot/add.tpl');
2017-03-11 02:51:06 +07:00
break;
case 'edit':
2024-03-18 22:55:08 +03:00
$id = $routes['2'];
2017-03-11 02:51:06 +07:00
$d = ORM::for_table('tbl_plans')->find_one($id);
2022-08-23 16:33:21 +07:00
if ($d) {
2024-06-11 13:28:05 +07:00
if (empty($d['device'])) {
if ($d['is_radius']) {
2024-06-10 17:33:49 +07:00
$d->device = 'Radius';
2024-06-11 13:28:05 +07:00
} else {
2024-06-10 17:33:49 +07:00
$d->device = 'MikrotikHotspot';
}
$d->save();
}
2022-08-23 16:33:21 +07:00
$ui->assign('d', $d);
$b = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('b', $b);
2024-06-10 17:33:49 +07:00
$devices = [];
$files = scandir($DEVICE_PATH);
2024-06-11 13:28:05 +07:00
foreach ($files as $file) {
2024-06-10 17:33:49 +07:00
$ext = pathinfo($file, PATHINFO_EXTENSION);
2024-06-11 13:28:05 +07:00
if ($ext == 'php') {
2024-06-10 17:33:49 +07:00
$devices[] = pathinfo($file, PATHINFO_FILENAME);
}
}
$ui->assign('devices', $devices);
2024-06-11 13:28:05 +07:00
//select expired plan
2024-06-12 15:27:33 +07:00
if ($d['is_radius']) {
$exps = ORM::for_table('tbl_plans')->selects('id', 'name_plan')->where('type', 'Hotspot')->where("is_radius", 1)->findArray();
} else {
$exps = ORM::for_table('tbl_plans')->selects('id', 'name_plan')->where('type', 'Hotspot')->where("routers", $d['routers'])->findArray();
2024-06-11 13:28:05 +07:00
}
$ui->assign('exps', $exps);
2022-09-18 00:00:40 +07:00
run_hook('view_edit_plan'); #HOOK
2025-02-04 10:22:14 +07:00
$ui->display('admin/hotspot/edit.tpl');
2022-08-23 16:33:21 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/hotspot'), 'e', Lang::T('Account Not Found'));
2017-03-11 02:51:06 +07:00
}
break;
case 'delete':
2024-03-18 22:55:08 +03:00
$id = $routes['2'];
2022-08-23 16:33:21 +07:00
2017-03-11 02:51:06 +07:00
$d = ORM::for_table('tbl_plans')->find_one($id);
2022-08-23 16:33:21 +07:00
if ($d) {
2022-09-18 00:00:40 +07:00
run_hook('delete_plan'); #HOOK
2024-06-05 17:19:24 +07:00
$dvc = Package::getDevice($d);
2024-06-20 14:16:09 +07:00
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
2024-06-21 09:44:49 +07:00
(new $d['device'])->remove_plan($d);
2024-06-20 14:16:09 +07:00
} else {
new Exception(Lang::T("Devices Not Found"));
}
2022-08-23 16:33:21 +07:00
}
2017-03-11 02:51:06 +07:00
$d->delete();
2022-08-23 16:33:21 +07:00
2025-01-31 16:22:58 +07:00
r2(getUrl('services/hotspot'), 's', Lang::T('Data Deleted Successfully'));
2017-03-11 02:51:06 +07:00
}
break;
case 'add-post':
$name = _post('name');
2024-03-18 22:55:08 +03:00
$plan_type = _post('plan_type'); //Personal / Business
2023-10-03 15:46:55 +07:00
$radius = _post('radius');
2017-03-11 02:51:06 +07:00
$typebp = _post('typebp');
2022-08-23 16:33:21 +07:00
$limit_type = _post('limit_type');
$time_limit = _post('time_limit');
$time_unit = _post('time_unit');
$data_limit = _post('data_limit');
$data_unit = _post('data_unit');
$id_bw = _post('id_bw');
2023-08-15 13:24:44 +07:00
$price = _post('price');
2022-08-23 16:33:21 +07:00
$sharedusers = _post('sharedusers');
2017-03-11 02:51:06 +07:00
$validity = _post('validity');
2022-08-23 16:33:21 +07:00
$validity_unit = _post('validity_unit');
$routers = _post('routers');
2024-06-10 17:33:49 +07:00
$device = _post('device');
2023-08-14 15:01:47 +07:00
$enabled = _post('enabled');
2024-03-13 14:32:10 +07:00
$prepaid = _post('prepaid');
2024-06-19 15:26:26 +07:00
$expired_date = _post('expired_date');
2022-08-23 16:33:21 +07:00
2017-03-11 02:51:06 +07:00
$msg = '';
2022-08-23 16:33:21 +07:00
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
}
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
2017-03-11 02:51:06 +07:00
}
2023-10-03 15:46:55 +07:00
if ($name == '' or $id_bw == '' or $price == '' or $validity == '') {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('All field is required') . '<br>';
2017-03-11 02:51:06 +07:00
}
if (empty($radius)) {
2023-10-03 15:46:55 +07:00
if ($routers == '') {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('All field is required') . '<br>';
2023-10-03 15:46:55 +07:00
}
}
2022-08-23 16:33:21 +07:00
$d = ORM::for_table('tbl_plans')->where('name_plan', $name)->where('type', 'Hotspot')->find_one();
if ($d) {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('Name Plan Already Exist') . '<br>';
2017-03-11 02:51:06 +07:00
}
2022-09-18 00:00:40 +07:00
run_hook('add_plan'); #HOOK
2022-08-23 16:33:21 +07:00
if ($msg == '') {
// Create new plan
2017-03-11 02:51:06 +07:00
$d = ORM::for_table('tbl_plans')->create();
$d->name_plan = $name;
$d->id_bw = $id_bw;
$d->price = $price; // Set price with or without tax based on configuration
2022-08-23 16:33:21 +07:00
$d->type = 'Hotspot';
2017-03-11 02:51:06 +07:00
$d->typebp = $typebp;
2024-03-18 22:55:08 +03:00
$d->plan_type = $plan_type;
2022-08-23 16:33:21 +07:00
$d->limit_type = $limit_type;
$d->time_limit = $time_limit;
$d->time_unit = $time_unit;
$d->data_limit = $data_limit;
$d->data_unit = $data_unit;
$d->validity = $validity;
2017-03-11 02:51:06 +07:00
$d->validity_unit = $validity_unit;
2022-08-23 16:33:21 +07:00
$d->shared_users = $sharedusers;
if (!empty($radius)) {
2023-10-03 15:46:55 +07:00
$d->is_radius = 1;
$d->routers = '';
2023-10-04 11:37:32 +07:00
} else {
2023-10-03 15:46:55 +07:00
$d->is_radius = 0;
$d->routers = $routers;
}
2022-09-08 10:43:46 +07:00
$d->enabled = $enabled;
2024-03-13 14:32:10 +07:00
$d->prepaid = $prepaid;
2024-06-10 17:33:49 +07:00
$d->device = $device;
2024-06-20 14:16:09 +07:00
if ($prepaid == 'no') {
if ($expired_date > 28 && $expired_date < 1) {
2024-06-19 15:26:26 +07:00
$expired_date = 20;
}
$d->expired_date = $expired_date;
2024-06-20 14:16:09 +07:00
} else {
2024-06-21 09:44:49 +07:00
$d->expired_date = 20;
2024-06-19 15:26:26 +07:00
}
2017-03-11 02:51:06 +07:00
$d->save();
2023-10-03 15:46:55 +07:00
2024-06-05 17:19:24 +07:00
$dvc = Package::getDevice($d);
2024-06-20 14:16:09 +07:00
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
2024-06-21 09:44:49 +07:00
(new $d['device'])->add_plan($d);
2024-06-20 14:16:09 +07:00
} else {
new Exception(Lang::T("Devices Not Found"));
}
2023-10-03 15:46:55 +07:00
}
2025-01-31 16:22:58 +07:00
r2(getUrl('services/edit/') . $d->id(), 's', Lang::T('Data Created Successfully'));
2022-08-23 16:33:21 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/add'), 'e', $msg);
2017-03-11 02:51:06 +07:00
}
break;
case 'edit-post':
2022-08-23 16:33:21 +07:00
$id = _post('id');
2017-03-11 02:51:06 +07:00
$name = _post('name');
2024-03-18 22:55:08 +03:00
$plan_type = _post('plan_type');
2017-03-11 02:51:06 +07:00
$id_bw = _post('id_bw');
2022-08-23 16:33:21 +07:00
$typebp = _post('typebp');
2017-03-11 02:51:06 +07:00
$price = _post('price');
2024-09-23 16:43:24 +07:00
$price_old = _post('price_old');
2022-08-23 16:33:21 +07:00
$limit_type = _post('limit_type');
$time_limit = _post('time_limit');
$time_unit = _post('time_unit');
$data_limit = _post('data_limit');
$data_unit = _post('data_unit');
$sharedusers = _post('sharedusers');
2017-03-11 02:51:06 +07:00
$validity = _post('validity');
2022-08-23 16:33:21 +07:00
$validity_unit = _post('validity_unit');
2024-06-11 13:28:05 +07:00
$plan_expired = _post('plan_expired', '0');
2024-06-10 17:33:49 +07:00
$device = _post('device');
2023-08-14 15:01:47 +07:00
$enabled = _post('enabled');
2024-03-13 14:32:10 +07:00
$prepaid = _post('prepaid');
$routers = _post('routers');
$on_login = _post('on_login');
$on_logout = _post('on_logout');
2024-06-19 15:26:26 +07:00
$expired_date = _post('expired_date');
2017-03-11 02:51:06 +07:00
$msg = '';
2022-08-23 16:33:21 +07:00
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
}
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
2017-03-11 02:51:06 +07:00
}
2023-10-03 15:46:55 +07:00
if ($name == '' or $id_bw == '' or $price == '' or $validity == '') {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('All field is required') . '<br>';
2017-03-11 02:51:06 +07:00
}
2022-08-23 16:33:21 +07:00
$d = ORM::for_table('tbl_plans')->where('id', $id)->find_one();
2024-06-05 17:19:24 +07:00
$old = ORM::for_table('tbl_plans')->where('id', $id)->find_one();
2022-08-23 16:33:21 +07:00
if ($d) {
} else {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('Data Not Found') . '<br>';
2017-03-11 02:51:06 +07:00
}
2024-09-23 16:43:24 +07:00
if ($price_old <= $price) {
$price_old = '';
}
2022-09-18 00:00:40 +07:00
run_hook('edit_plan'); #HOOK
2022-08-23 16:33:21 +07:00
if ($msg == '') {
$b = ORM::for_table('tbl_bandwidth')->where('id', $id_bw)->find_one();
if ($b['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
2023-10-03 15:46:55 +07:00
$raddown = '000';
2022-08-23 16:33:21 +07:00
} else {
$unitdown = 'M';
2023-10-03 15:46:55 +07:00
$raddown = '000000';
2022-08-23 16:33:21 +07:00
}
if ($b['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
2023-10-03 15:46:55 +07:00
$radup = '000';
2022-08-23 16:33:21 +07:00
} else {
$unitup = 'M';
2023-10-03 15:46:55 +07:00
$radup = '000000';
2022-08-23 16:33:21 +07:00
}
$rate = $b['rate_up'] . $unitup . "/" . $b['rate_down'] . $unitdown;
2024-05-07 08:11:16 +07:00
$radiusRate = $b['rate_up'] . $radup . '/' . $b['rate_down'] . $raddown . '/' . $b['burst'];
2022-08-23 16:33:21 +07:00
2024-02-19 18:03:40 +07:00
$rate = trim($rate . " " . $b['burst']);
2017-03-11 02:51:06 +07:00
$d->name_plan = $name;
$d->id_bw = $id_bw;
$d->price = $price; // Set price with or without tax based on configuration
2024-09-23 16:43:24 +07:00
$d->price_old = $price_old;
2017-03-11 02:51:06 +07:00
$d->typebp = $typebp;
2022-08-23 16:33:21 +07:00
$d->limit_type = $limit_type;
$d->time_limit = $time_limit;
$d->time_unit = $time_unit;
$d->data_limit = $data_limit;
2024-03-18 22:55:08 +03:00
$d->plan_type = $plan_type;
2022-08-23 16:33:21 +07:00
$d->data_unit = $data_unit;
$d->validity = $validity;
2017-03-11 02:51:06 +07:00
$d->validity_unit = $validity_unit;
2022-08-23 16:33:21 +07:00
$d->shared_users = $sharedusers;
2024-06-11 13:28:05 +07:00
$d->plan_expired = $plan_expired;
2022-09-08 10:43:46 +07:00
$d->enabled = $enabled;
2024-03-13 14:32:10 +07:00
$d->prepaid = $prepaid;
$d->on_login = $on_login;
$d->on_logout = $on_logout;
2024-06-10 17:33:49 +07:00
$d->device = $device;
2024-06-20 14:16:09 +07:00
if ($prepaid == 'no') {
if ($expired_date > 28 && $expired_date < 1) {
2024-06-19 15:26:26 +07:00
$expired_date = 20;
}
$d->expired_date = $expired_date;
2024-06-20 14:16:09 +07:00
} else {
2024-06-21 09:44:49 +07:00
$d->expired_date = 20;
2024-06-19 15:26:26 +07:00
}
2017-03-11 02:51:06 +07:00
$d->save();
2022-08-23 16:33:21 +07:00
2024-06-05 17:19:24 +07:00
$dvc = Package::getDevice($d);
2024-06-20 14:16:09 +07:00
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
(new $d['device'])->update_plan($old, $d);
} else {
new Exception(Lang::T("Devices Not Found"));
}
2024-06-05 17:19:24 +07:00
}
2025-01-31 16:22:58 +07:00
r2(getUrl('services/hotspot'), 's', Lang::T('Data Updated Successfully'));
2022-08-23 16:33:21 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/edit/') . $id, 'e', $msg);
2017-03-11 02:51:06 +07:00
}
break;
2022-08-23 16:33:21 +07:00
2017-03-11 02:51:06 +07:00
case 'pppoe':
2024-02-13 13:54:01 +07:00
$ui->assign('_title', Lang::T('PPPOE Plans'));
2022-08-23 16:33:21 +07:00
$name = _post('name');
2024-06-21 15:06:31 +07:00
$name = _req('name');
$type1 = _req('type1');
$type2 = _req('type2');
$type3 = _req('type3');
$bandwidth = _req('bandwidth');
$valid = _req('valid');
$device = _req('device');
$status = _req('status');
$router = _req('router');
$ui->assign('type1', $type1);
$ui->assign('type2', $type2);
$ui->assign('type3', $type3);
$ui->assign('bandwidth', $bandwidth);
$ui->assign('valid', $valid);
$ui->assign('device', $device);
$ui->assign('status', $status);
$ui->assign('router', $router);
$append_url = "&type1=" . urlencode($type1)
. "&type2=" . urlencode($type2)
. "&type3=" . urlencode($type3)
. "&bandwidth=" . urlencode($bandwidth)
. "&valid=" . urlencode($valid)
. "&device=" . urlencode($device)
. "&status=" . urlencode($status)
. "&router=" . urlencode($router);
$bws = ORM::for_table('tbl_plans')->distinct()->select("id_bw")->where('tbl_plans.type', 'PPPOE')->findArray();
$ids = array_column($bws, 'id_bw');
2024-09-23 16:43:24 +07:00
if (count($ids)) {
$ui->assign('bws', ORM::for_table('tbl_bandwidth')->select("id")->select('name_bw')->where_id_in($ids)->findArray());
2024-09-23 16:43:24 +07:00
} else {
$ui->assign('bws', []);
}
2024-06-21 15:06:31 +07:00
$ui->assign('type2s', ORM::for_table('tbl_plans')->getEnum("plan_type"));
$ui->assign('type3s', ORM::for_table('tbl_plans')->getEnum("typebp"));
$ui->assign('valids', ORM::for_table('tbl_plans')->getEnum("validity_unit"));
$ui->assign('routers', array_column(ORM::for_table('tbl_plans')->distinct()->select("routers")->whereNotEqual('routers', '')->findArray(), 'routers'));
$devices = [];
$files = scandir($DEVICE_PATH);
foreach ($files as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == 'php') {
$devices[] = pathinfo($file, PATHINFO_FILENAME);
}
}
$ui->assign('devices', $devices);
$query = ORM::for_table('tbl_bandwidth')
->left_outer_join('tbl_plans', array('tbl_bandwidth.id', '=', 'tbl_plans.id_bw'))
->where('tbl_plans.type', 'PPPOE');
if (!empty($type1)) {
$query->where('tbl_plans.prepaid', $type1);
}
if (!empty($type2)) {
$query->where('tbl_plans.plan_type', $type2);
}
if (!empty($type3)) {
$query->where('tbl_plans.typebp', $type3);
}
if (!empty($bandwidth)) {
$query->where('tbl_plans.id_bw', $bandwidth);
}
if (!empty($valid)) {
$query->where('tbl_plans.validity_unit', $valid);
}
if (!empty($router)) {
if ($router == 'radius') {
$query->where('tbl_plans.is_radius', '1');
} else {
$query->where('tbl_plans.routers', $router);
}
}
if (!empty($device)) {
$query->where('tbl_plans.device', $device);
}
if (in_array($status, ['0', '1'])) {
$query->where('tbl_plans.enabled', $status);
}
2022-08-23 16:33:21 +07:00
if ($name != '') {
2024-06-21 15:06:31 +07:00
$query->where_like('tbl_plans.name_plan', '%' . $name . '%');
2022-08-23 16:33:21 +07:00
}
2024-06-21 15:06:31 +07:00
$d = Paginator::findMany($query, ['name' => $name], 20, $append_url);
2022-08-23 16:33:21 +07:00
$ui->assign('d', $d);
2022-09-18 00:00:40 +07:00
run_hook('view_list_ppoe'); #HOOK
2025-02-04 10:56:02 +07:00
$ui->display('admin/pppoe/list.tpl');
2017-03-11 02:51:06 +07:00
break;
case 'pppoe-add':
2024-02-13 13:54:01 +07:00
$ui->assign('_title', Lang::T('PPPOE Plans'));
2022-08-23 16:33:21 +07:00
$d = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('d', $d);
$r = ORM::for_table('tbl_routers')->find_many();
$ui->assign('r', $r);
2024-06-10 17:33:49 +07:00
$devices = [];
$files = scandir($DEVICE_PATH);
2024-06-11 13:28:05 +07:00
foreach ($files as $file) {
2024-06-10 17:33:49 +07:00
$ext = pathinfo($file, PATHINFO_EXTENSION);
2024-06-11 13:28:05 +07:00
if ($ext == 'php') {
2024-06-10 17:33:49 +07:00
$devices[] = pathinfo($file, PATHINFO_FILENAME);
}
}
$ui->assign('devices', $devices);
2022-09-18 00:00:40 +07:00
run_hook('view_add_ppoe'); #HOOK
2025-02-04 10:56:02 +07:00
$ui->display('admin/pppoe/add.tpl');
2017-03-11 02:51:06 +07:00
break;
case 'pppoe-edit':
2024-02-13 13:54:01 +07:00
$ui->assign('_title', Lang::T('PPPOE Plans'));
2024-03-18 22:55:08 +03:00
$id = $routes['2'];
2017-03-11 02:51:06 +07:00
$d = ORM::for_table('tbl_plans')->find_one($id);
2022-08-23 16:33:21 +07:00
if ($d) {
2024-06-11 13:28:05 +07:00
if (empty($d['device'])) {
if ($d['is_radius']) {
2024-06-10 17:33:49 +07:00
$d->device = 'Radius';
2024-06-11 13:28:05 +07:00
} else {
2024-06-10 17:33:49 +07:00
$d->device = 'MikrotikPppoe';
}
$d->save();
}
2022-08-23 16:33:21 +07:00
$ui->assign('d', $d);
2023-10-04 11:37:32 +07:00
$p = ORM::for_table('tbl_pool')->where('routers', ($d['is_radius']) ? 'radius' : $d['routers'])->find_many();
2023-09-05 16:40:23 +07:00
$ui->assign('p', $p);
2022-08-23 16:33:21 +07:00
$b = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('b', $b);
2023-10-04 11:37:32 +07:00
$r = [];
2023-10-04 16:11:55 +07:00
if ($d['is_radius']) {
2023-10-04 11:37:32 +07:00
$r = ORM::for_table('tbl_routers')->find_many();
}
2022-08-23 16:33:21 +07:00
$ui->assign('r', $r);
2024-06-10 17:33:49 +07:00
$devices = [];
$files = scandir($DEVICE_PATH);
2024-06-11 13:28:05 +07:00
foreach ($files as $file) {
2024-06-10 17:33:49 +07:00
$ext = pathinfo($file, PATHINFO_EXTENSION);
2024-06-11 13:28:05 +07:00
if ($ext == 'php') {
2024-06-10 17:33:49 +07:00
$devices[] = pathinfo($file, PATHINFO_FILENAME);
}
}
$ui->assign('devices', $devices);
2024-06-11 13:28:05 +07:00
//select expired plan
2024-06-12 15:27:33 +07:00
if ($d['is_radius']) {
$exps = ORM::for_table('tbl_plans')->selects('id', 'name_plan')->where('type', 'PPPOE')->where("is_radius", 1)->findArray();
} else {
$exps = ORM::for_table('tbl_plans')->selects('id', 'name_plan')->where('type', 'PPPOE')->where("routers", $d['routers'])->findArray();
2024-06-11 13:28:05 +07:00
}
$ui->assign('exps', $exps);
2022-09-18 00:00:40 +07:00
run_hook('view_edit_ppoe'); #HOOK
2025-02-04 10:56:02 +07:00
$ui->display('admin/pppoe/edit.tpl');
2022-08-23 16:33:21 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/pppoe'), 'e', Lang::T('Account Not Found'));
2017-03-11 02:51:06 +07:00
}
break;
case 'pppoe-delete':
2024-03-18 22:55:08 +03:00
$id = $routes['2'];
2022-08-23 16:33:21 +07:00
2017-03-11 02:51:06 +07:00
$d = ORM::for_table('tbl_plans')->find_one($id);
2022-08-23 16:33:21 +07:00
if ($d) {
2022-09-18 00:00:40 +07:00
run_hook('delete_ppoe'); #HOOK
2024-06-05 17:19:24 +07:00
$dvc = Package::getDevice($d);
2024-06-20 14:16:09 +07:00
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
2024-06-21 09:44:49 +07:00
(new $d['device'])->remove_plan($d);
2024-06-20 14:16:09 +07:00
} else {
new Exception(Lang::T("Devices Not Found"));
}
2022-08-23 16:33:21 +07:00
}
2017-03-11 02:51:06 +07:00
$d->delete();
2025-01-31 16:22:58 +07:00
r2(getUrl('services/pppoe'), 's', Lang::T('Data Deleted Successfully'));
2017-03-11 02:51:06 +07:00
}
break;
case 'pppoe-add-post':
$name = _post('name_plan');
2024-03-18 22:55:08 +03:00
$plan_type = _post('plan_type');
2023-10-04 11:37:32 +07:00
$radius = _post('radius');
2022-08-23 16:33:21 +07:00
$id_bw = _post('id_bw');
$price = _post('price');
2017-03-11 02:51:06 +07:00
$validity = _post('validity');
2022-08-23 16:33:21 +07:00
$validity_unit = _post('validity_unit');
$routers = _post('routers');
2024-06-10 17:33:49 +07:00
$device = _post('device');
2022-08-23 16:33:21 +07:00
$pool = _post('pool_name');
2023-08-14 15:01:47 +07:00
$enabled = _post('enabled');
2024-03-13 14:32:10 +07:00
$prepaid = _post('prepaid');
2024-06-19 15:26:26 +07:00
$expired_date = _post('expired_date');
2022-08-23 16:33:21 +07:00
2017-03-11 02:51:06 +07:00
$msg = '';
2022-08-23 16:33:21 +07:00
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
2017-03-11 02:51:06 +07:00
}
2022-08-23 16:33:21 +07:00
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
2017-03-11 02:51:06 +07:00
}
2023-10-04 11:37:32 +07:00
if ($name == '' or $id_bw == '' or $price == '' or $validity == '' or $pool == '') {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('All field is required') . '<br>';
2017-03-11 02:51:06 +07:00
}
if (empty($radius)) {
2023-10-04 11:37:32 +07:00
if ($routers == '') {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('All field is required') . '<br>';
2023-10-04 11:37:32 +07:00
}
}
2017-03-11 02:51:06 +07:00
2022-08-23 16:33:21 +07:00
$d = ORM::for_table('tbl_plans')->where('name_plan', $name)->find_one();
if ($d) {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('Name Plan Already Exist') . '<br>';
2022-08-23 16:33:21 +07:00
}
2022-09-18 00:00:40 +07:00
run_hook('add_ppoe'); #HOOK
2022-08-23 16:33:21 +07:00
if ($msg == '') {
$b = ORM::for_table('tbl_bandwidth')->where('id', $id_bw)->find_one();
if ($b['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
2023-10-04 11:37:32 +07:00
$raddown = '000';
2022-08-23 16:33:21 +07:00
} else {
$unitdown = 'M';
2023-10-04 11:37:32 +07:00
$raddown = '000000';
2022-08-23 16:33:21 +07:00
}
if ($b['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
2023-10-04 11:37:32 +07:00
$radup = '000';
2022-08-23 16:33:21 +07:00
} else {
$unitup = 'M';
2023-10-04 11:37:32 +07:00
$radup = '000000';
2022-08-23 16:33:21 +07:00
}
$rate = $b['rate_up'] . $unitup . "/" . $b['rate_down'] . $unitdown;
2024-05-07 08:11:16 +07:00
$radiusRate = $b['rate_up'] . $radup . '/' . $b['rate_down'] . $raddown . '/' . $b['burst'];
2024-02-19 18:03:40 +07:00
$rate = trim($rate . " " . $b['burst']);
2017-03-11 02:51:06 +07:00
$d = ORM::for_table('tbl_plans')->create();
$d->type = 'PPPOE';
2022-08-23 16:33:21 +07:00
$d->name_plan = $name;
2017-03-11 02:51:06 +07:00
$d->id_bw = $id_bw;
$d->price = $price;
2024-03-18 22:55:08 +03:00
$d->plan_type = $plan_type;
2022-08-23 16:33:21 +07:00
$d->validity = $validity;
2017-03-11 02:51:06 +07:00
$d->validity_unit = $validity_unit;
2022-08-23 16:33:21 +07:00
$d->pool = $pool;
if (!empty($radius)) {
2023-10-04 11:37:32 +07:00
$d->is_radius = 1;
$d->routers = '';
} else {
$d->is_radius = 0;
$d->routers = $routers;
}
2024-06-20 14:16:09 +07:00
if ($prepaid == 'no') {
if ($expired_date > 28 && $expired_date < 1) {
2024-06-19 15:26:26 +07:00
$expired_date = 20;
}
$d->expired_date = $expired_date;
2024-06-20 14:16:09 +07:00
} else {
2024-06-19 15:26:26 +07:00
$d->expired_date = 0;
}
2022-09-08 10:43:46 +07:00
$d->enabled = $enabled;
2024-03-13 14:32:10 +07:00
$d->prepaid = $prepaid;
2024-06-10 17:33:49 +07:00
$d->device = $device;
2017-03-11 02:51:06 +07:00
$d->save();
2023-10-04 11:37:32 +07:00
2024-06-05 17:19:24 +07:00
$dvc = Package::getDevice($d);
2024-06-20 14:16:09 +07:00
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
2024-06-21 09:44:49 +07:00
(new $d['device'])->add_plan($d);
2024-06-20 14:16:09 +07:00
} else {
new Exception(Lang::T("Devices Not Found"));
}
2023-10-04 11:37:32 +07:00
}
2025-01-31 16:22:58 +07:00
r2(getUrl('services/pppoe'), 's', Lang::T('Data Created Successfully'));
2022-08-23 16:33:21 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/pppoe-add'), 'e', $msg);
2017-03-11 02:51:06 +07:00
}
break;
case 'edit-pppoe-post':
2022-08-23 16:33:21 +07:00
$id = _post('id');
2024-03-18 22:55:08 +03:00
$plan_type = _post('plan_type');
2017-03-11 02:51:06 +07:00
$name = _post('name_plan');
2022-08-23 16:33:21 +07:00
$id_bw = _post('id_bw');
$price = _post('price');
2024-09-23 16:43:24 +07:00
$price_old = _post('price_old');
2017-03-11 02:51:06 +07:00
$validity = _post('validity');
2022-08-23 16:33:21 +07:00
$validity_unit = _post('validity_unit');
$routers = _post('routers');
2024-06-10 17:33:49 +07:00
$device = _post('device');
2022-08-23 16:33:21 +07:00
$pool = _post('pool_name');
2024-06-11 13:28:05 +07:00
$plan_expired = _post('plan_expired');
2023-08-14 15:01:47 +07:00
$enabled = _post('enabled');
2024-03-13 14:32:10 +07:00
$prepaid = _post('prepaid');
2024-06-19 15:26:26 +07:00
$expired_date = _post('expired_date');
$on_login = _post('on_login');
$on_logout = _post('on_logout');
2022-08-23 16:33:21 +07:00
2017-03-11 02:51:06 +07:00
$msg = '';
2022-08-23 16:33:21 +07:00
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
}
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
2017-03-11 02:51:06 +07:00
}
2023-10-04 11:37:32 +07:00
if ($name == '' or $id_bw == '' or $price == '' or $validity == '' or $pool == '') {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('All field is required') . '<br>';
2017-03-11 02:51:06 +07:00
}
2022-08-23 16:33:21 +07:00
2024-09-23 16:43:24 +07:00
if ($price_old <= $price) {
$price_old = '';
}
2022-08-23 16:33:21 +07:00
$d = ORM::for_table('tbl_plans')->where('id', $id)->find_one();
2024-06-05 17:19:24 +07:00
$old = ORM::for_table('tbl_plans')->where('id', $id)->find_one();
2022-08-23 16:33:21 +07:00
if ($d) {
} else {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('Data Not Found') . '<br>';
2017-03-11 02:51:06 +07:00
}
2022-09-18 00:00:40 +07:00
run_hook('edit_ppoe'); #HOOK
2022-08-23 16:33:21 +07:00
if ($msg == '') {
$b = ORM::for_table('tbl_bandwidth')->where('id', $id_bw)->find_one();
if ($b['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
2023-10-04 11:37:32 +07:00
$raddown = '000';
2022-08-23 16:33:21 +07:00
} else {
$unitdown = 'M';
2023-10-04 11:37:32 +07:00
$raddown = '000000';
2022-08-23 16:33:21 +07:00
}
if ($b['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
2023-10-04 11:37:32 +07:00
$radup = '000';
2022-08-23 16:33:21 +07:00
} else {
$unitup = 'M';
2023-10-04 11:37:32 +07:00
$radup = '000000';
2022-08-23 16:33:21 +07:00
}
$rate = $b['rate_up'] . $unitup . "/" . $b['rate_down'] . $unitdown;
2024-05-07 08:11:16 +07:00
$radiusRate = $b['rate_up'] . $radup . '/' . $b['rate_down'] . $raddown . '/' . $b['burst'];
2024-02-19 18:03:40 +07:00
$rate = trim($rate . " " . $b['burst']);
2022-08-23 16:33:21 +07:00
2017-03-11 02:51:06 +07:00
$d->name_plan = $name;
$d->id_bw = $id_bw;
$d->price = $price;
2024-09-23 16:43:24 +07:00
$d->price_old = $price_old;
2024-03-18 22:55:08 +03:00
$d->plan_type = $plan_type;
2022-08-23 16:33:21 +07:00
$d->validity = $validity;
2017-03-11 02:51:06 +07:00
$d->validity_unit = $validity_unit;
2022-08-23 16:33:21 +07:00
$d->routers = $routers;
$d->pool = $pool;
2024-06-11 13:28:05 +07:00
$d->plan_expired = $plan_expired;
2022-09-08 10:43:46 +07:00
$d->enabled = $enabled;
2024-03-13 14:32:10 +07:00
$d->prepaid = $prepaid;
2024-06-10 17:33:49 +07:00
$d->device = $device;
$d->on_login = $on_login;
$d->on_logout = $on_logout;
2024-06-20 14:16:09 +07:00
if ($prepaid == 'no') {
if ($expired_date > 28 && $expired_date < 1) {
2024-06-19 15:26:26 +07:00
$expired_date = 20;
}
$d->expired_date = $expired_date;
2024-06-20 14:16:09 +07:00
} else {
2024-06-19 15:26:26 +07:00
$d->expired_date = 0;
}
2017-03-11 02:51:06 +07:00
$d->save();
2022-08-23 16:33:21 +07:00
2024-06-05 17:19:24 +07:00
$dvc = Package::getDevice($d);
2024-06-20 14:16:09 +07:00
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
(new $d['device'])->update_plan($old, $d);
} else {
new Exception(Lang::T("Devices Not Found"));
}
2024-06-05 17:19:24 +07:00
}
2025-01-31 16:22:58 +07:00
r2(getUrl('services/pppoe'), 's', Lang::T('Data Updated Successfully'));
2022-08-23 16:33:21 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/pppoe-edit/') . $id, 'e', $msg);
2017-03-11 02:51:06 +07:00
}
break;
2023-08-14 15:01:47 +07:00
case 'balance':
$ui->assign('_title', Lang::T('Balance Plans'));
$name = _post('name');
if ($name != '') {
$query = ORM::for_table('tbl_plans')->where('tbl_plans.type', 'Balance')->where_like('tbl_plans.name_plan', '%' . $name . '%');
2024-03-27 09:44:48 +07:00
$d = Paginator::findMany($query, ['name' => $name]);
2023-08-14 15:01:47 +07:00
} else {
$query = ORM::for_table('tbl_plans')->where('tbl_plans.type', 'Balance');
2024-03-27 09:44:48 +07:00
$d = Paginator::findMany($query);
2023-08-14 15:01:47 +07:00
}
$ui->assign('d', $d);
run_hook('view_list_balance'); #HOOK
2025-02-04 10:22:14 +07:00
$ui->display('admin/balance/list.tpl');
2023-08-14 15:01:47 +07:00
break;
case 'balance-add':
$ui->assign('_title', Lang::T('Balance Plans'));
run_hook('view_add_balance'); #HOOK
2025-02-04 10:22:14 +07:00
$ui->display('admin/balance/add.tpl');
2023-08-14 15:01:47 +07:00
break;
case 'balance-edit':
$ui->assign('_title', Lang::T('Balance Plans'));
2024-03-18 22:55:08 +03:00
$id = $routes['2'];
2023-08-14 15:01:47 +07:00
$d = ORM::for_table('tbl_plans')->find_one($id);
$ui->assign('d', $d);
run_hook('view_edit_balance'); #HOOK
2025-02-04 10:22:14 +07:00
$ui->display('admin/balance/edit.tpl');
2023-08-14 15:01:47 +07:00
break;
case 'balance-delete':
2024-03-18 22:55:08 +03:00
$id = $routes['2'];
2023-08-14 15:01:47 +07:00
$d = ORM::for_table('tbl_plans')->find_one($id);
if ($d) {
run_hook('delete_balance'); #HOOK
$d->delete();
2025-01-31 16:22:58 +07:00
r2(getUrl('services/balance'), 's', Lang::T('Data Deleted Successfully'));
2023-08-14 15:01:47 +07:00
}
break;
case 'balance-edit-post':
$id = _post('id');
$name = _post('name');
$price = _post('price');
2024-09-23 16:43:24 +07:00
$price_old = _post('price_old');
2023-08-14 15:01:47 +07:00
$enabled = _post('enabled');
2024-03-13 14:32:10 +07:00
$prepaid = _post('prepaid');
2023-08-14 15:01:47 +07:00
$msg = '';
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
}
if ($name == '') {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('All field is required') . '<br>';
2023-08-14 15:01:47 +07:00
}
2022-08-23 16:33:21 +07:00
2023-08-14 15:01:47 +07:00
$d = ORM::for_table('tbl_plans')->where('id', $id)->find_one();
if ($d) {
} else {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('Data Not Found') . '<br>';
2023-08-14 15:01:47 +07:00
}
2024-09-23 16:43:24 +07:00
if ($price_old <= $price) {
$price_old = '';
}
2023-08-14 15:01:47 +07:00
run_hook('edit_ppoe'); #HOOK
if ($msg == '') {
$d->name_plan = $name;
$d->price = $price;
2023-08-14 15:01:47 +07:00
$d->enabled = $enabled;
2024-09-23 16:43:24 +07:00
$d->price_old = $price_old;
2024-03-13 14:32:10 +07:00
$d->prepaid = 'yes';
2023-08-14 15:01:47 +07:00
$d->save();
2025-01-31 16:22:58 +07:00
r2(getUrl('services/balance'), 's', Lang::T('Data Updated Successfully'));
2023-08-14 15:01:47 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/balance-edit/') . $id, 'e', $msg);
2023-08-14 15:01:47 +07:00
}
break;
case 'balance-add-post':
$name = _post('name');
$price = _post('price');
$enabled = _post('enabled');
$msg = '';
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
}
if ($name == '') {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('All field is required') . '<br>';
2023-08-14 15:01:47 +07:00
}
$d = ORM::for_table('tbl_plans')->where('name_plan', $name)->find_one();
if ($d) {
2024-02-13 13:54:01 +07:00
$msg .= Lang::T('Name Plan Already Exist') . '<br>';
2023-08-14 15:01:47 +07:00
}
run_hook('add_ppoe'); #HOOK
if ($msg == '') {
$d = ORM::for_table('tbl_plans')->create();
$d->type = 'Balance';
$d->name_plan = $name;
$d->id_bw = 0;
$d->price = $price;
2023-08-14 15:01:47 +07:00
$d->validity = 0;
$d->validity_unit = 'Months';
$d->routers = '';
$d->pool = '';
$d->enabled = $enabled;
2024-03-13 14:32:10 +07:00
$d->prepaid = 'yes';
2023-08-14 15:01:47 +07:00
$d->save();
2025-01-31 16:22:58 +07:00
r2(getUrl('services/balance'), 's', Lang::T('Data Created Successfully'));
2023-08-14 15:01:47 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/balance-add'), 'e', $msg);
2023-08-14 15:01:47 +07:00
}
break;
2024-09-13 00:43:46 +07:00
case 'vpn':
$ui->assign('_title', Lang::T('VPN Plans'));
$name = _post('name');
$name = _req('name');
$type1 = _req('type1');
$type2 = _req('type2');
$type3 = _req('type3');
$bandwidth = _req('bandwidth');
$valid = _req('valid');
$device = _req('device');
$status = _req('status');
$router = _req('router');
$ui->assign('type1', $type1);
$ui->assign('type2', $type2);
$ui->assign('type3', $type3);
$ui->assign('bandwidth', $bandwidth);
$ui->assign('valid', $valid);
$ui->assign('device', $device);
$ui->assign('status', $status);
$ui->assign('router', $router);
$append_url = "&type1=" . urlencode($type1)
. "&type2=" . urlencode($type2)
. "&type3=" . urlencode($type3)
. "&bandwidth=" . urlencode($bandwidth)
. "&valid=" . urlencode($valid)
. "&device=" . urlencode($device)
. "&status=" . urlencode($status)
. "&router=" . urlencode($router);
$bws = ORM::for_table('tbl_plans')->distinct()->select("id_bw")->where('tbl_plans.type', 'VPN')->findArray();
$ids = array_column($bws, 'id_bw');
2024-09-23 16:43:24 +07:00
if (count($ids)) {
2024-09-13 00:43:46 +07:00
$ui->assign('bws', ORM::for_table('tbl_bandwidth')->select("id")->select('name_bw')->where_id_in($ids)->findArray());
2024-09-23 16:43:24 +07:00
} else {
2024-09-13 00:43:46 +07:00
$ui->assign('bws', []);
}
$ui->assign('type2s', ORM::for_table('tbl_plans')->getEnum("plan_type"));
$ui->assign('type3s', ORM::for_table('tbl_plans')->getEnum("typebp"));
$ui->assign('valids', ORM::for_table('tbl_plans')->getEnum("validity_unit"));
$ui->assign('routers', array_column(ORM::for_table('tbl_plans')->distinct()->select("routers")->whereNotEqual('routers', '')->findArray(), 'routers'));
$devices = [];
$files = scandir($DEVICE_PATH);
foreach ($files as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == 'php') {
$devices[] = pathinfo($file, PATHINFO_FILENAME);
}
}
$ui->assign('devices', $devices);
$query = ORM::for_table('tbl_bandwidth')
->left_outer_join('tbl_plans', array('tbl_bandwidth.id', '=', 'tbl_plans.id_bw'))
->where('tbl_plans.type', 'VPN');
if (!empty($type1)) {
$query->where('tbl_plans.prepaid', $type1);
}
if (!empty($type2)) {
$query->where('tbl_plans.plan_type', $type2);
}
if (!empty($type3)) {
$query->where('tbl_plans.typebp', $type3);
}
if (!empty($bandwidth)) {
$query->where('tbl_plans.id_bw', $bandwidth);
}
if (!empty($valid)) {
$query->where('tbl_plans.validity_unit', $valid);
}
if (!empty($router)) {
if ($router == 'radius') {
$query->where('tbl_plans.is_radius', '1');
} else {
$query->where('tbl_plans.routers', $router);
}
}
if (!empty($device)) {
$query->where('tbl_plans.device', $device);
}
if (in_array($status, ['0', '1'])) {
$query->where('tbl_plans.enabled', $status);
}
if ($name != '') {
$query->where_like('tbl_plans.name_plan', '%' . $name . '%');
}
$d = Paginator::findMany($query, ['name' => $name], 20, $append_url);
$ui->assign('d', $d);
run_hook('view_list_vpn'); #HOOK
2025-02-04 10:56:02 +07:00
$ui->display('admin/vpn/list.tpl');
2024-09-13 00:43:46 +07:00
break;
case 'vpn-add':
$ui->assign('_title', Lang::T('VPN Plans'));
$d = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('d', $d);
$r = ORM::for_table('tbl_routers')->find_many();
$ui->assign('r', $r);
$devices = [];
$files = scandir($DEVICE_PATH);
foreach ($files as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == 'php') {
$devices[] = pathinfo($file, PATHINFO_FILENAME);
}
}
$ui->assign('devices', $devices);
run_hook('view_add_vpn'); #HOOK
2025-02-04 10:56:02 +07:00
$ui->display('admin/vpn/add.tpl');
2024-09-13 00:43:46 +07:00
break;
case 'vpn-edit':
$ui->assign('_title', Lang::T('VPN Plans'));
$id = $routes['2'];
$d = ORM::for_table('tbl_plans')->find_one($id);
if ($d) {
if (empty($d['device'])) {
if ($d['is_radius']) {
$d->device = 'Radius';
} else {
$d->device = 'MikrotikVpn';
}
$d->save();
}
$ui->assign('d', $d);
$p = ORM::for_table('tbl_pool')->where('routers', ($d['is_radius']) ? 'radius' : $d['routers'])->find_many();
$ui->assign('p', $p);
$b = ORM::for_table('tbl_bandwidth')->find_many();
$ui->assign('b', $b);
$r = [];
if ($d['is_radius']) {
$r = ORM::for_table('tbl_routers')->find_many();
}
$ui->assign('r', $r);
$devices = [];
$files = scandir($DEVICE_PATH);
foreach ($files as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == 'php') {
$devices[] = pathinfo($file, PATHINFO_FILENAME);
}
}
$ui->assign('devices', $devices);
//select expired plan
if ($d['is_radius']) {
$exps = ORM::for_table('tbl_plans')->selects('id', 'name_plan')->where('type', 'VPN')->where("is_radius", 1)->findArray();
} else {
$exps = ORM::for_table('tbl_plans')->selects('id', 'name_plan')->where('type', 'VPN')->where("routers", $d['routers'])->findArray();
}
$ui->assign('exps', $exps);
run_hook('view_edit_vpn'); #HOOK
2025-02-04 10:56:02 +07:00
$ui->display('admin/vpn/edit.tpl');
2024-09-13 00:43:46 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/vpn'), 'e', Lang::T('Account Not Found'));
2024-09-13 00:43:46 +07:00
}
break;
case 'vpn-delete':
$id = $routes['2'];
$d = ORM::for_table('tbl_plans')->find_one($id);
if ($d) {
run_hook('delete_vpn'); #HOOK
$dvc = Package::getDevice($d);
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
(new $d['device'])->remove_plan($d);
} else {
new Exception(Lang::T("Devices Not Found"));
}
}
$d->delete();
2025-01-31 16:22:58 +07:00
r2(getUrl('services/vpn'), 's', Lang::T('Data Deleted Successfully'));
2024-09-13 00:43:46 +07:00
}
break;
case 'vpn-add-post':
$name = _post('name_plan');
$plan_type = _post('plan_type');
$radius = _post('radius');
$id_bw = _post('id_bw');
$price = _post('price');
$validity = _post('validity');
$validity_unit = _post('validity_unit');
$routers = _post('routers');
$device = _post('device');
$pool = _post('pool_name');
$enabled = _post('enabled');
$prepaid = _post('prepaid');
$expired_date = _post('expired_date');
$msg = '';
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
}
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
}
if ($name == '' or $id_bw == '' or $price == '' or $validity == '' or $pool == '') {
$msg .= Lang::T('All field is required') . '<br>';
}
if (empty($radius)) {
if ($routers == '') {
$msg .= Lang::T('All field is required') . '<br>';
}
}
$d = ORM::for_table('tbl_plans')->where('name_plan', $name)->find_one();
if ($d) {
$msg .= Lang::T('Name Plan Already Exist') . '<br>';
}
run_hook('add_vpn'); #HOOK
if ($msg == '') {
$b = ORM::for_table('tbl_bandwidth')->where('id', $id_bw)->find_one();
if ($b['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
$raddown = '000';
} else {
$unitdown = 'M';
$raddown = '000000';
}
if ($b['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
$radup = '000';
} else {
$unitup = 'M';
$radup = '000000';
}
$rate = $b['rate_up'] . $unitup . "/" . $b['rate_down'] . $unitdown;
$radiusRate = $b['rate_up'] . $radup . '/' . $b['rate_down'] . $raddown . '/' . $b['burst'];
$rate = trim($rate . " " . $b['burst']);
$d = ORM::for_table('tbl_plans')->create();
$d->type = 'VPN';
$d->name_plan = $name;
$d->id_bw = $id_bw;
$d->price = $price;
$d->plan_type = $plan_type;
$d->validity = $validity;
$d->validity_unit = $validity_unit;
$d->pool = $pool;
if (!empty($radius)) {
$d->is_radius = 1;
$d->routers = '';
} else {
$d->is_radius = 0;
$d->routers = $routers;
}
if ($prepaid == 'no') {
if ($expired_date > 28 && $expired_date < 1) {
$expired_date = 20;
}
$d->expired_date = $expired_date;
} else {
$d->expired_date = 0;
}
$d->enabled = $enabled;
$d->prepaid = $prepaid;
$d->device = $device;
$d->save();
$dvc = Package::getDevice($d);
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
(new $d['device'])->add_plan($d);
} else {
new Exception(Lang::T("Devices Not Found"));
}
}
2025-01-31 16:22:58 +07:00
r2(getUrl('services/vpn'), 's', Lang::T('Data Created Successfully'));
2024-09-13 00:43:46 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/vpn-add'), 'e', $msg);
2024-09-13 00:43:46 +07:00
}
break;
case 'edit-vpn-post':
$id = _post('id');
$plan_type = _post('plan_type');
$name = _post('name_plan');
$id_bw = _post('id_bw');
$price = _post('price');
2024-09-23 16:43:24 +07:00
$price_old = _post('price_old');
2024-09-13 00:43:46 +07:00
$validity = _post('validity');
$validity_unit = _post('validity_unit');
$routers = _post('routers');
$device = _post('device');
$pool = _post('pool_name');
$plan_expired = _post('plan_expired');
$enabled = _post('enabled');
$prepaid = _post('prepaid');
$expired_date = _post('expired_date');
$on_login = _post('on_login');
$on_logout = _post('on_logout');
$msg = '';
if (Validator::UnsignedNumber($validity) == false) {
$msg .= 'The validity must be a number' . '<br>';
}
if (Validator::UnsignedNumber($price) == false) {
$msg .= 'The price must be a number' . '<br>';
}
if ($name == '' or $id_bw == '' or $price == '' or $validity == '' or $pool == '') {
$msg .= Lang::T('All field is required') . '<br>';
}
2024-09-23 16:43:24 +07:00
if($price_old<=$price){
$price_old = '';
}
2024-09-13 00:43:46 +07:00
$d = ORM::for_table('tbl_plans')->where('id', $id)->find_one();
$old = ORM::for_table('tbl_plans')->where('id', $id)->find_one();
if ($d) {
} else {
$msg .= Lang::T('Data Not Found') . '<br>';
}
run_hook('edit_vpn'); #HOOK
if ($msg == '') {
$b = ORM::for_table('tbl_bandwidth')->where('id', $id_bw)->find_one();
if ($b['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
$raddown = '000';
} else {
$unitdown = 'M';
$raddown = '000000';
}
if ($b['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
$radup = '000';
} else {
$unitup = 'M';
$radup = '000000';
}
$rate = $b['rate_up'] . $unitup . "/" . $b['rate_down'] . $unitdown;
$radiusRate = $b['rate_up'] . $radup . '/' . $b['rate_down'] . $raddown . '/' . $b['burst'];
$rate = trim($rate . " " . $b['burst']);
$d->name_plan = $name;
$d->id_bw = $id_bw;
$d->price = $price;
2024-09-23 16:43:24 +07:00
$d->price_old = $price_old;
2024-09-13 00:43:46 +07:00
$d->plan_type = $plan_type;
$d->validity = $validity;
$d->validity_unit = $validity_unit;
$d->routers = $routers;
$d->pool = $pool;
$d->plan_expired = $plan_expired;
$d->enabled = $enabled;
$d->prepaid = $prepaid;
$d->device = $device;
$d->on_login = $on_login;
$d->on_logout = $on_logout;
if ($prepaid == 'no') {
if ($expired_date > 28 && $expired_date < 1) {
$expired_date = 20;
}
$d->expired_date = $expired_date;
} else {
$d->expired_date = 0;
}
$d->save();
$dvc = Package::getDevice($d);
if ($_app_stage != 'demo') {
if (file_exists($dvc)) {
require_once $dvc;
(new $d['device'])->update_plan($old, $d);
} else {
new Exception(Lang::T("Devices Not Found"));
}
}
2025-01-31 16:22:58 +07:00
r2(getUrl('services/vpn'), 's', Lang::T('Data Updated Successfully'));
2024-09-13 00:43:46 +07:00
} else {
2025-01-31 16:22:58 +07:00
r2(getUrl('services/vpn-edit/') . $id, 'e', $msg);
2024-09-13 00:43:46 +07:00
}
break;
2017-03-11 02:51:06 +07:00
default:
2025-02-04 09:23:55 +07:00
$ui->display('admin/404.tpl');
2022-08-23 16:33:21 +07:00
}