From 3a4ac7c4a1ae0fd9633000531e001bdf13146f67 Mon Sep 17 00:00:00 2001 From: nestict Date: Sat, 24 May 2025 11:20:57 +0200 Subject: [PATCH] Upload files to "system/controllers" Signed-off-by: nestict --- system/controllers/onlinehotspot.php | 130 +++++++ system/controllers/onlineusers.php | 347 +++++++++++++++++++ system/controllers/order.php | 493 +++++++++++++++++++++++++++ system/controllers/page.php | 21 ++ system/controllers/pages.php | 71 ++++ 5 files changed, 1062 insertions(+) create mode 100644 system/controllers/onlinehotspot.php create mode 100644 system/controllers/onlineusers.php create mode 100644 system/controllers/order.php create mode 100644 system/controllers/page.php create mode 100644 system/controllers/pages.php diff --git a/system/controllers/onlinehotspot.php b/system/controllers/onlinehotspot.php new file mode 100644 index 0000000..4b4fd04 --- /dev/null +++ b/system/controllers/onlinehotspot.php @@ -0,0 +1,130 @@ +assign('_title', Lang::T('online')); +$ui->assign('_system_menu', 'onlineusers'); +$ui->assign('onlineusers', $online); + +$action = $routes['1']; +$ui->assign('_admin', $admin); + +use PEAR2\Net\RouterOS; + +function handle_action($action) +{ + switch ($action) { + case 'get_hotspot_online_users': + mikrotik_get_hotspot_online_users(); + break; + case 'disconnect_online_user': + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + mikrotik_disconnect_online_user($_POST['router'], $_POST['username'], $_POST['userType']); + } + break; + case 'list': + // Assuming you have a function to fetch the data for the online hotspot users list + $onlineHotspotUsers = fetch_online_hotspot_users(); + $ui->assign('onlineHotspotUsers', $onlineHotspotUsers); + $ui->display('onlinehotspot.tpl'); + break; + default: + // Handle default case, maybe return an error or redirect + break; + } +} + +function mikrotik_get_hotspot_online_users() +{ + global $routes; + $router = $routes['2']; + $mikrotik = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($router); + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + $hotspotActive = $client->sendSync(new RouterOS\Request('/ip/hotspot/active/print')); + $hotspotList = []; + + foreach ($hotspotActive as $hotspot) { + $username = $hotspot->getProperty('user'); + $address = $hotspot->getProperty('address'); + $uptime = $hotspot->getProperty('uptime'); + $server = $hotspot->getProperty('server'); + $mac = $hotspot->getProperty('mac-address'); + $sessionTime = $hotspot->getProperty('session-time-left'); + $rxBytes = $hotspot->getProperty('bytes-in'); + $txBytes = $hotspot->getProperty('bytes-out'); + $hotspotList[] = [ + 'username' => $username, + 'address' => $address, + 'uptime' => $uptime, + 'server' => $server, + 'mac' => $mac, + 'session_time' => $sessionTime, + 'rx_bytes' => mikrotik_formatBytes($rxBytes), + 'tx_bytes' => mikrotik_formatBytes($txBytes), + 'total' => mikrotik_formatBytes($txBytes + $rxBytes), + ]; + } + + // Return the Hotspot online user list as JSON + header('Content-Type: application/json'); + echo json_encode($hotspotList); +} + +function mikrotik_disconnect_online_user($router, $username, $userType) +{ + // Check if the form was submitted + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + // Retrieve the form data + $router = $_POST['router']; + $username = $_POST['username']; + $userType = $_POST['userType']; + + $mikrotik = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($router); + if (!$mikrotik) { + // Handle the error response or redirection + return; + } + + try { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + if ($userType == 'hotspot') { + Mikrotik::removeHotspotActiveUser($client, $username); + // Handle the success response or redirection + } elseif ($userType == 'pppoe') { + Mikrotik::removePpoeActive($client, $username); + // Handle the success response or redirection + } else { + // Handle the error response or redirection + return; + } + } catch (Exception $e) { + // Handle the error response or redirection + } finally { + // Disconnect from the MikroTik router + if (isset($client)) { + $client->disconnect(); + } + } + } +} + +// Helper function to format bytes +function mikrotik_formatBytes($bytes) +{ + $units = ['B', 'KB', 'MB', 'GB', 'TB']; + $factor = floor((strlen($bytes) - 1) / 3); + return sprintf("%.2f %s", $bytes / pow(1024, $factor), @$units[$factor]); +} + +// Call the main function with the action provided in the URL +$action = $routes['1'] ?? ''; // Assuming $routes is defined elsewhere +handle_action($action); + +$ui->assign('onlineusers', $online); + +$ui->display('onlinehotspot.tpl'); +?> diff --git a/system/controllers/onlineusers.php b/system/controllers/onlineusers.php new file mode 100644 index 0000000..6dc1925 --- /dev/null +++ b/system/controllers/onlineusers.php @@ -0,0 +1,347 @@ +assign('_title', Lang::T('Online Users')); +$ui->assign('_system_menu', 'onlineusers'); + +$action = $routes['1']; +$ui->assign('_admin', $admin); + +use PEAR2\Net\RouterOS; + +require_once 'system/autoload/PEAR2/Autoload.php'; + +if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) { + _alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard"); +} + +// Handle cases for hotspot users and PPP users +switch ($action) { + case 'hotspot': + $ui->display('hotspot_users.tpl'); + break; +case 'hotspot_users': + $hotspotUsers = mikrotik_get_hotspot_online_users(); + + // Filter out entries where all values are null + $filteredHotspotUsers = array_filter($hotspotUsers, function($user) { + // Check if all specified fields are null + return !( + is_null($user['username']) && + is_null($user['address']) && + is_null($user['uptime']) && + is_null($user['server']) && + is_null($user['mac']) && + is_null($user['session_time']) && + $user['rx_bytes'] === '0 B' && + $user['tx_bytes'] === '0 B' && + $user['total'] === '0 B' + ); + }); + + header('Content-Type: application/json'); + echo json_encode($filteredHotspotUsers); + exit; + break; + +case 'pppoe': + $ui->display('ppp_users.tpl'); + break; + +case 'ppp_users': + $pppUsers = mikrotik_get_ppp_online_users(); + header('Content-Type: application/json'); + echo json_encode($pppUsers); + exit; + break; + + case 'disconnect': + $routerId = $routes['2']; + $username = $routes['3']; + $userType = $routes['4']; + mikrotik_disconnect_online_user($routerId, $username, $userType); + // Redirect or handle the response as needed + break; + + case 'summary': + // Fetch summary of online users and total bytes used + $summary = mikrotik_get_online_users_summary(); + header('Content-Type: application/json'); + echo json_encode($summary); + exit; + break; + + default: + // Handle default case or invalid action + break; +} + +// Function to round the value and append the appropriate unit +function mikrotik_formatBytes($bytes, $precision = 2) +{ +$units = array('B', 'KB', 'MB', 'GB', 'TB'); + + $bytes = max($bytes, 0); + $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); + $pow = min($pow, count($units) - 1); + + $bytes /= pow(1024, $pow); + + return round($bytes, $precision) . ' ' . $units[$pow]; +} + +function filter_null_users($users) { + return array_filter($users, function($user) { + return array_reduce($user, function($carry, $value) { + return $carry || $value !== null; + }, false); + }); +} + +function mikrotik_get_hotspot_online_users() +{ + global $routes; + $routerId = $routes['2']; + $mikrotik = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($routerId); + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + $hotspotActive = $client->sendSync(new RouterOS\Request('/ip/hotspot/active/print')); + + $hotspotList = []; + foreach ($hotspotActive as $hotspot) { + $username = $hotspot->getProperty('user'); + $address = $hotspot->getProperty('address'); + $uptime = $hotspot->getProperty('uptime'); + $server = $hotspot->getProperty('server'); + $mac = $hotspot->getProperty('mac-address'); + $sessionTime = $hotspot->getProperty('session-time-left'); + $rxBytes = $hotspot->getProperty('bytes-in'); + $txBytes = $hotspot->getProperty('bytes-out'); + + $hotspotList[] = [ + 'username' => $username, + 'address' => $address, + 'uptime' => $uptime, + 'server' => $server, + 'mac' => $mac, + 'session_time' => $sessionTime, + 'rx_bytes' => mikrotik_formatBytes($rxBytes), + 'tx_bytes' => mikrotik_formatBytes($txBytes), + 'total' => mikrotik_formatBytes($rxBytes + $txBytes), + ]; + } + + // Filter out users with all null properties + $filteredHotspotList = filter_null_users($hotspotList); + + // Return an empty array if no users are left after filtering + return empty($filteredHotspotList) ? [] : $filteredHotspotList; +} + + + +function mikrotik_get_ppp_online_users() +{ + global $routes; + $routerId = $routes['2']; + $mikrotik = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($routerId); + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + $pppUsers = $client->sendSync(new RouterOS\Request('/ppp/active/print')); + + $userList = []; + foreach ($pppUsers as $pppUser) { + $username = $pppUser->getProperty('name'); + $address = $pppUser->getProperty('address'); + $uptime = $pppUser->getProperty('uptime'); + $service = $pppUser->getProperty('service'); + $callerid = $pppUser->getProperty('caller-id'); + $bytes_in = $pppUser->getProperty('limit-bytes-in'); + $bytes_out = $pppUser->getProperty('limit-bytes-out'); + + $userList[] = [ + 'username' => $username, + 'address' => $address, + 'uptime' => $uptime, + 'service' => $service, + 'caller_id' => $callerid, + 'bytes_in' => $bytes_in, + 'bytes_out' => $bytes_out, + ]; + } + + // Filter out users with all null properties + return filter_null_users($userList); +} + +function save_data_usage($username, $bytes_in, $bytes_out, $connection_type) { + if (!$username) { + error_log("Error: Missing username in save_data_usage()"); + return; + } + + $currentTime = date('Y-m-d H:i:s'); + $currentDate = date('Y-m-d'); + + // Check if there's an existing record for this user today + $existingRecord = ORM::for_table('tbl_user_data_usage') + ->where('username', $username) + ->where('connection_type', $connection_type) + ->where_raw('DATE(timestamp) = ?', [$currentDate]) + ->find_one(); + + if ($existingRecord) { + // Update existing record for today + $existingRecord->bytes_in = ($bytes_in ?: 0); + $existingRecord->bytes_out = ($bytes_out ?: 0); + $existingRecord->last_updated = $currentTime; + $existingRecord->save(); + } else { + // Create new record for today + $newRecord = ORM::for_table('tbl_user_data_usage')->create(); + $newRecord->username = $username; + $newRecord->bytes_in = ($bytes_in ?: 0); + $newRecord->bytes_out = ($bytes_out ?: 0); + $newRecord->connection_type = $connection_type; + $newRecord->timestamp = $currentTime; + $newRecord->last_updated = $currentTime; + $newRecord->save(); + } +} + +function mikrotik_get_online_users_summary() +{ + global $routes; + $routerId = $routes['2']; + $mikrotik = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($routerId); + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + + // Get Hotspot users + $hotspotActive = $client->sendSync(new RouterOS\Request('/ip/hotspot/active/print')); + $hotspotList = []; + $totalHotspotUsage = 0; + foreach ($hotspotActive as $hotspot) { + $rxBytes = $hotspot->getProperty('bytes-in'); + $txBytes = $hotspot->getProperty('bytes-out'); + $totalHotspotUsage += $rxBytes + $txBytes; + $username = $hotspot->getProperty('user'); + save_data_usage($username, $rxBytes, $txBytes, 'hotspot'); + + $hotspotList[] = [ + 'username' => $username, + 'address' => $hotspot->getProperty('address'), + 'uptime' => $hotspot->getProperty('uptime'), + 'server' => $hotspot->getProperty('server'), + 'mac' => $hotspot->getProperty('mac-address'), + 'session_time' => $hotspot->getProperty('session-time-left'), + 'rx_bytes' => mikrotik_formatBytes($rxBytes), + 'tx_bytes' => mikrotik_formatBytes($txBytes), + 'total' => mikrotik_formatBytes($rxBytes + $txBytes), + ]; + } + + // Filter out null hotspot users + $hotspotList = array_filter($hotspotList, function($user) { + return !( + is_null($user['username']) && + is_null($user['address']) && + is_null($user['uptime']) && + is_null($user['server']) && + is_null($user['mac']) && + is_null($user['session_time']) && + $user['rx_bytes'] === '0 B' && + $user['tx_bytes'] === '0 B' && + $user['total'] === '0 B' + ); + }); + + // Get PPPoE users + $pppUsers = $client->sendSync(new RouterOS\Request('/ppp/active/print')); + $pppoeList = []; + $totalPPPoEUsage = 0; + foreach ($pppUsers as $pppUser) { + $bytes_in = $pppUser->getProperty('limit-bytes-in'); + $bytes_out = $pppUser->getProperty('limit-bytes-out'); + $totalPPPoEUsage += $bytes_in + $bytes_out; + $username = $pppUser->getProperty('name'); + save_data_usage($username, $bytes_in, $bytes_out, 'pppoe'); + + $pppoeList[] = [ + 'username' => $username, + 'address' => $pppUser->getProperty('address'), + 'uptime' => $pppUser->getProperty('uptime'), + 'service' => $pppUser->getProperty('service'), + 'caller_id' => $pppUser->getProperty('caller-id'), + 'bytes_in' => mikrotik_formatBytes($bytes_in), + 'bytes_out' => mikrotik_formatBytes($bytes_out), + 'total' => mikrotik_formatBytes($bytes_in + $bytes_out), + ]; + } + + // Filter out null PPPoE users + $pppoeList = array_filter($pppoeList, function($user) { + return !( + is_null($user['username']) && + is_null($user['address']) && + is_null($user['uptime']) && + is_null($user['service']) && + is_null($user['caller_id']) && + $user['bytes_in'] === '0 B' && + $user['bytes_out'] === '0 B' && + $user['total'] === '0 B' + ); + }); + // Calculate total data usage + $totalDataUsage = $totalHotspotUsage + $totalPPPoEUsage; + + // Calculate total users + $totalHotspotUsers = count($hotspotList); + $totalPPPoEUsers = count($pppoeList); + $totalUsers = $totalHotspotUsers + $totalPPPoEUsers; + + return [ + 'hotspot_users' => $totalHotspotUsers, + 'ppoe_users' => $totalPPPoEUsers, + 'total_users' => $totalUsers, + 'total_bytes' => mikrotik_formatBytes($totalDataUsage), + ]; +} + +function mikrotik_disconnect_online_user($router, $username, $userType) +{ + // Check if the form was submitted + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + // Retrieve the form data + $router = $_POST['router']; + $username = $_POST['username']; + $userType = $_POST['userType']; + $mikrotik = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($router); + if (!$mikrotik) { + // Handle the error response or redirection + return; + } + try { + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + if ($userType == 'hotspot') { + Mikrotik::removeHotspotActiveUser($client, $username); + // Handle the success response or redirection + } elseif ($userType == 'pppoe') { + Mikrotik::removePpoeActive($client, $username); + // Handle the success response or redirection + } else { + // Handle the error response or redirection + return; + } + } catch (Exception $e) { + // Handle the error response or redirection + } finally { + // Disconnect from the MikroTik router + if (isset($client)) { + $client->disconnect(); + } + } + } +} + +?> diff --git a/system/controllers/order.php b/system/controllers/order.php new file mode 100644 index 0000000..bc07be1 --- /dev/null +++ b/system/controllers/order.php @@ -0,0 +1,493 @@ +assign('_user', $user); + +switch ($action) { + case 'voucher': + $ui->assign('_system_menu', 'voucher'); + $ui->assign('_title', Lang::T('Order Voucher')); + run_hook('customer_view_order'); #HOOK + $ui->display('user-order.tpl'); + break; + case 'history': + $ui->assign('_system_menu', 'history'); + $query = ORM::for_table('tbl_payment_gateway')->where('username', $user['username'])->order_by_desc('id'); + $d = Paginator::findMany($query); + $ui->assign('d', $d); + $ui->assign('_title', Lang::T('Order History')); + run_hook('customer_view_order_history'); #HOOK + $ui->display('user-orderHistory.tpl'); + break; + case 'balance': + if (strpos($user['email'], '@') === false) { + r2(U . 'accounts/profile', 'e', Lang::T("Please enter your email address")); + } + $ui->assign('_title', 'Top Up'); + $ui->assign('_system_menu', 'balance'); + $plans_balance = ORM::for_table('tbl_plans')->where('enabled', '1')->where('type', 'Balance')->where('prepaid', 'yes')->find_many(); + $ui->assign('plans_balance', $plans_balance); + $ui->display('user-orderBalance.tpl'); + break; + case 'package': + if (strpos($user['email'], '@') === false) { + r2(U . 'accounts/profile', 'e', Lang::T("Please enter your email address")); + } + $ui->assign('_title', 'Order Plan'); + $ui->assign('_system_menu', 'package'); + $account_type = $user['account_type']; + if (empty($account_type)) { + $account_type = 'Personal'; + } + if (!empty($_SESSION['nux-router'])) { + if ($_SESSION['nux-router'] == 'radius') { + $radius_pppoe = ORM::for_table('tbl_plans')->where('plan_type', $account_type)->where('enabled', '1')->where('is_radius', 1)->where('type', 'PPPOE')->where('prepaid', 'yes')->find_many(); + $radius_hotspot = ORM::for_table('tbl_plans')->where('plan_type', $account_type)->where('enabled', '1')->where('is_radius', 1)->where('type', 'Hotspot')->where('prepaid', 'yes')->find_many(); + } else { + $routers = ORM::for_table('tbl_routers')->where('id', $_SESSION['nux-router'])->find_many(); + $rs = []; + foreach ($routers as $r) { + $rs[] = $r['name']; + } + $plans_pppoe = ORM::for_table('tbl_plans')->where('plan_type', $account_type)->where('enabled', '1')->where_in('routers', $rs)->where('is_radius', 0)->where('type', 'PPPOE')->where('prepaid', 'yes')->find_many(); + $plans_hotspot = ORM::for_table('tbl_plans')->where('plan_type', $account_type)->where('enabled', '1')->where_in('routers', $rs)->where('is_radius', 0)->where('type', 'Hotspot')->where('prepaid', 'yes')->find_many(); + } + } else { + $radius_pppoe = ORM::for_table('tbl_plans')->where('plan_type', $account_type)->where('enabled', '1')->where('is_radius', 1)->where('type', 'PPPOE')->where('prepaid', 'yes')->find_many(); + $radius_hotspot = ORM::for_table('tbl_plans')->where('plan_type', $account_type)->where('enabled', '1')->where('is_radius', 1)->where('type', 'Hotspot')->where('prepaid', 'yes')->find_many(); + + $routers = ORM::for_table('tbl_routers')->find_many(); + $plans_pppoe = ORM::for_table('tbl_plans')->where('plan_type', $account_type)->where('enabled', '1')->where('is_radius', 0)->where('type', 'PPPOE')->where('prepaid', 'yes')->find_many(); + $plans_hotspot = ORM::for_table('tbl_plans')->where('plan_type', $account_type)->where('enabled', '1')->where('is_radius', 0)->where('type', 'Hotspot')->where('prepaid', 'yes')->find_many(); + } + $ui->assign('routers', $routers); + $ui->assign('radius_pppoe', $radius_pppoe); + $ui->assign('radius_hotspot', $radius_hotspot); + $ui->assign('plans_pppoe', $plans_pppoe); + $ui->assign('plans_hotspot', $plans_hotspot); + run_hook('customer_view_order_plan'); #HOOK + $ui->display('user-orderPlan.tpl'); + break; + case 'unpaid': + $d = ORM::for_table('tbl_payment_gateway') + ->where('username', $user['username']) + ->where('status', 1) + ->find_one(); + run_hook('custome + r_find_unpaid'); #HOOK + if ($d) { + if (empty($d['pg_url_payment'])) { + r2(U . "order/buy/" . $trx['routers_id'] . '/' . $trx['plan_id'], 'w', Lang::T("Checking payment")); + } else { + r2(U . "order/view/" . $d['id'] . '/check/', 's', Lang::T("You have unpaid transaction")); + } + } else { + r2(U . "order/package/", 's', Lang::T("You have no unpaid transaction")); + } + break; + case 'view': + $trxid = $routes['2']; + $trx = ORM::for_table('tbl_payment_gateway') + ->where('username', $user['username']) + ->find_one($trxid); + run_hook('customer_view_payment'); #HOOK + // jika tidak ditemukan, berarti punya orang lain + if (empty($trx)) { + r2(U . "order/package", 'w', Lang::T("Payment not found")); + } + // jika url kosong, balikin ke buy, kecuali cancel + if (empty($trx['pg_url_payment']) && $routes['3'] != 'cancel') { + r2(U . "order/buy/" . (($trx['routers_id'] == 0) ? $trx['routers'] : $trx['routers_id']) . '/' . $trx['plan_id'], 'w', Lang::T("Checking payment")); + } + if ($routes['3'] == 'check') { + if (!file_exists($PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR . $trx['gateway'] . '.php')) { + r2(U . 'order/view/' . $trxid, 'e', Lang::T("No Payment Gateway Available")); + } + run_hook('customer_check_payment_status'); #HOOK + include $PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR . $trx['gateway'] . '.php'; + call_user_func($trx['gateway'] . '_validate_config'); + call_user_func($trx['gateway'] . '_get_status', $trx, $user); + } else if ($routes['3'] == 'cancel') { + run_hook('customer_cancel_payment'); #HOOK + $trx->pg_paid_response = '{}'; + $trx->status = 4; + $trx->paid_date = date('Y-m-d H:i:s'); + $trx->save(); + $trx = ORM::for_table('tbl_payment_gateway') + ->where('username', $user['username']) + ->find_one($trxid); + } + if (empty($trx)) { + r2(U . "order/package", 'e', Lang::T("Transaction Not found")); + } + + $router = Mikrotik::info($trx['routers']); + $plan = ORM::for_table('tbl_plans')->find_one($trx['plan_id']); + $bandw = ORM::for_table('tbl_bandwidth')->find_one($plan['id_bw']); + $invoice = ORM::for_table('tbl_transactions')->where("invoice", $trx['trx_invoice'])->find_one(); + $ui->assign('invoice', $invoice); + $ui->assign('trx', $trx); + $ui->assign('router', $router); + $ui->assign('plan', $plan); + $ui->assign('bandw', $bandw); + $ui->assign('_title', 'TRX #' . $trxid); + $ui->display('user-orderView.tpl'); + break; + case 'pay': + if ($config['enable_balance'] != 'yes') { + r2(U . "order/package", 'e', Lang::T("Balance not enabled")); + } + if (!empty(App::getTokenValue($_GET['stoken']))) { + r2(U . "voucher/invoice/"); + die(); + } + if ($user['status'] != 'Active') { + _alert(Lang::T('This account status') . ' : ' . Lang::T($user['status']), 'danger', ""); + } + $plan = ORM::for_table('tbl_plans')->where('enabled', '1')->find_one($routes['3']); + if (empty($plan)) { + r2(U . "order/package", 'e', Lang::T("Plan Not found")); + } + if (!$plan['enabled']) { + r2(U . "home", 'e', 'Plan is not exists'); + } + if ($routes['2'] == 'radius') { + $router_name = 'radius'; + } else { + $router_name = $plan['routers']; + } + + list($bills, $add_cost) = User::getBills($id_customer); + + // Tax calculation start + $tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no'; + $tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null; + $custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null; + + if ($tax_rate_setting === 'custom') { + $tax_rate = $custom_tax_rate; + } else { + $tax_rate = $tax_rate_setting; + } + + if ($tax_enable === 'yes') { + $tax = Package::tax($plan['price'], $tax_rate); + } else { + $tax = 0; + } + // Tax calculation stop + + if ($plan && $plan['enabled'] && $user['balance'] >= $plan['price'] + $tax) { + if (Package::rechargeUser($user['id'], $router_name, $plan['id'], 'Customer', 'Balance')) { + // if success, then get the balance + Balance::min($user['id'], $plan['price'] + $add_cost + $tax); + App::setToken($_GET['stoken'], "success"); + r2(U . "voucher/invoice/", 's', Lang::T("Success to buy package")); + } else { + r2(U . "order/package", 'e', Lang::T("Failed to buy package")); + Message::sendTelegram("Buy Package with Balance Failed\n\n#u$c[username] #buy \n" . $plan['name_plan'] . + "\nRouter: " . $router_name . + "\nPrice: " . $plan['price'] + $tax); + } + } else { + r2(U . "home", 'e', 'Plan is not exists'); + } + break; + + case 'send': + if ($config['enable_balance'] != 'yes') { + r2(U . "order/package", 'e', Lang::T("Balance not enabled")); + } + if ($user['status'] != 'Active') { + _alert(Lang::T('This account status') . ' : ' . Lang::T($user['status']), 'danger', ""); + } + $ui->assign('_title', Lang::T('Buy for friend')); + $ui->assign('_system_menu', 'package'); + $plan = ORM::for_table('tbl_plans')->find_one($routes['3']); + if (empty($plan)) { + r2(U . "order/package", 'e', Lang::T("Plan Not found")); + } + if (!$plan['enabled']) { + r2(U . "home", 'e', 'Plan is not exists'); + } + if ($routes['2'] == 'radius') { + $router_name = 'radius'; + } else { + $router_name = $plan['routers']; + } + $tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null; + $custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null; + + if ($tax_rate_setting === 'custom') { + $tax_rate = $custom_tax_rate; + } else { + $tax_rate = $tax_rate_setting; + } + + $tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no'; + + if ($tax_enable === 'yes') { + $tax = Package::tax($plan['price'], $tax_rate); + $ui->assign('tax', $tax); + } else { + $tax = 0; + } + + // Add tax to plan price + $plan['price'] += $tax; + + if (isset($_POST['send']) && $_POST['send'] == 'plan') { + $target = ORM::for_table('tbl_customers')->where('username', _post('username'))->find_one(); + list($bills, $add_cost) = User::getBills($target['id']); + if (!empty($add_cost)) { + $ui->assign('bills', $bills); + $ui->assign('add_cost', $add_cost); + $plan['price'] += $add_cost; + } + + if (!$target) { + r2(U . 'home', 'd', Lang::T('Username not found')); + } + if ($user['balance'] < $plan['price']) { + r2(U . 'home', 'd', Lang::T('insufficient balance')); + } + if ($user['username'] == $target['username']) { + r2(U . "order/pay/$routes[2]/$routes[3]", 's', '^_^ v'); + } + $active = ORM::for_table('tbl_user_recharges') + ->where('username', _post('username')) + ->where('status', 'on') + ->find_one(); + + if ($active && $active['plan_id'] != $plan['id']) { + r2(U . "order/package", 'e', Lang::T("Target has active plan, different with current plant.") . " [ $active[namebp] ]"); + } + $result = Package::rechargeUser($target['id'], $router_name, $plan['id'], $user['username'], 'Balance'); + if (!empty($result)) { + // if success, then get the balance + Balance::min($user['id'], $plan['price']); + //sender + $d = ORM::for_table('tbl_payment_gateway')->create(); + $d->username = $user['username']; + $d->gateway = $target['username']; + $d->plan_id = $plan['id']; + $d->plan_name = $plan['name_plan']; + $d->routers_id = $routes['2']; + $d->routers = $router_name; + $d->price = $plan['price']; + $d->payment_method = "Balance"; + $d->payment_channel = "Send Plan"; + $d->created_date = date('Y-m-d H:i:s'); + $d->paid_date = date('Y-m-d H:i:s'); + $d->expired_date = date('Y-m-d H:i:s'); + $d->pg_url_payment = 'balance'; + $d->trx_invoice = $result; + $d->status = 2; + $d->save(); + $trx_id = $d->id(); + //receiver + $d = ORM::for_table('tbl_payment_gateway')->create(); + $d->username = $target['username']; + $d->gateway = $user['username']; + $d->plan_id = $plan['id']; + $d->plan_name = $plan['name_plan']; + $d->routers_id = $routes['2']; + $d->routers = $router_name; + $d->price = $plan['price']; + $d->payment_method = "Balance"; + $d->payment_channel = "Received Plan"; + $d->created_date = date('Y-m-d H:i:s'); + $d->paid_date = date('Y-m-d H:i:s'); + $d->expired_date = date('Y-m-d H:i:s'); + $d->pg_url_payment = 'balance'; + $d->trx_invoice = $result; + $d->status = 2; + $d->save(); + r2(U . "order/view/$trx_id", 's', Lang::T("Success to send package")); + } else { + $errorMessage = "Send Package with Balance Failed\n\n#u$user[username] #send \n" . $plan['name_plan'] . + "\nRouter: " . $router_name . + "\nPrice: " . $plan['price']; + + if ($tax_enable === 'yes') { + $errorMessage .= "\nTax: " . $tax; + } + + r2(U . "order/package", 'e', Lang::T("Failed to Send package")); + Message::sendTelegram($errorMessage); + } + } + $ui->assign('username', $_GET['u']); + $ui->assign('router', $router_name); + $ui->assign('plan', $plan); + $ui->assign('tax', $tax); + $ui->display('user-sendPlan.tpl'); + break; + case 'gateway': + $ui->assign('_title', Lang::T('Select Payment Gateway')); + $ui->assign('_system_menu', 'package'); + if (strpos($user['email'], '@') === false) { + r2(U . 'accounts/profile', 'e', Lang::T("Please enter your email address")); + } + $tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no'; + $tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null; + $custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null; + if ($tax_rate_setting === 'custom') { + $tax_rate = $custom_tax_rate; + } else { + $tax_rate = $tax_rate_setting; + } + $plan = ORM::for_table('tbl_plans')->find_one($routes['3']); + $tax = Package::tax($plan['price'], $tax_rate); + $pgs = array_values(explode(',', $config['payment_gateway'])); + if (count($pgs) == 0) { + sendTelegram("Payment Gateway not set, please set it in Settings"); + _log(Lang::T("Payment Gateway not set, please set it in Settings")); + r2(U . "home", 'e', Lang::T("Failed to create Transaction..")); + } + if (count($pgs) > 1) { + $ui->assign('pgs', $pgs); + if ($tax_enable === 'yes') { + $ui->assign('tax', $tax); + } + $ui->assign('route2', $routes[2]); + $ui->assign('route3', $routes[3]); + $ui->assign('plan', $plan); + $ui->display('user-selectGateway.tpl'); + break; + } else { + if (empty($pgs[0])) { + sendTelegram("Payment Gateway not set, please set it in Settings"); + _log(Lang::T("Payment Gateway not set, please set it in Settings")); + r2(U . "home", 'e', Lang::T("Failed to create Transaction..")); + } else { + $_POST['gateway'] = $pgs[0]; + } + } + case 'buy': + $gateway = _post('gateway'); + if (empty($gateway) && !empty($_SESSION['gateway'])) { + $gateway = $_SESSION['gateway']; + } else if (!empty($gateway)) { + $_SESSION['gateway'] = $gateway; + } + if ($user['status'] != 'Active') { + _alert(Lang::T('This account status') . ' : ' . Lang::T($user['status']), 'danger', ""); + } + if (empty($gateway)) { + r2(U . 'order/gateway/' . $routes[2] . '/' . $routes[3], 'w', Lang::T("Please select Payment Gateway")); + } + run_hook('customer_buy_plan'); #HOOK + include $PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR . $gateway . '.php'; + call_user_func($gateway . '_validate_config'); + + if ($routes['2'] == 'radius') { + $router['id'] = 0; + $router['name'] = 'radius'; + } else if ($routes['2'] > 0) { + $router = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($routes['2']); + } else { + $router['id'] = 0; + $router['name'] = 'balance'; + } + $plan = ORM::for_table('tbl_plans')->where('enabled', '1')->find_one($routes['3']); + if (empty($router) || empty($plan)) { + r2(U . "order/package", 'e', Lang::T("Plan Not found")); + } + $d = ORM::for_table('tbl_payment_gateway') + ->where('username', $user['username']) + ->where('status', 1) + ->find_one(); + if ($d) { + if ($d['pg_url_payment']) { + r2(U . "order/view/" . $d['id'], 'w', Lang::T("You already have unpaid transaction, cancel it or pay it.")); + } else { + if ($gateway == $d['gateway']) { + $id = $d['id']; + } else { + $d->status = 4; + $d->save(); + } + } + } + $add_cost = 0; + $tax = 0; + if ($router['name'] != 'balance') { + list($bills, $add_cost) = User::getBills($id_customer); + } + // Tax calculation start + $tax_enable = isset($config['enable_tax']) ? $config['enable_tax'] : 'no'; + $tax_rate_setting = isset($config['tax_rate']) ? $config['tax_rate'] : null; + $custom_tax_rate = isset($config['custom_tax_rate']) ? (float)$config['custom_tax_rate'] : null; + if ($tax_rate_setting === 'custom') { + $tax_rate = $custom_tax_rate; + } else { + $tax_rate = $tax_rate_setting; + } + if ($tax_enable === 'yes') { + $tax = Package::tax($plan['price'], $tax_rate); + } + // Tax calculation stop + if (empty($id)) { + $d = ORM::for_table('tbl_payment_gateway')->create(); + $d->username = $user['username']; + $d->gateway = $gateway; + $d->plan_id = $plan['id']; + $d->plan_name = $plan['name_plan']; + $d->routers_id = $router['id']; + $d->routers = $router['name']; + if ($plan['validity_unit'] == 'Period') { + // Postpaid price from field + $add_inv = User::getAttribute("Invoice", $id_customer); + if (empty($add_inv) or $add_inv == 0) { + $d->price = ($plan['price'] + $add_cost + $tax); + } else { + $d->price = ($add_inv + $add_cost + $tax); + } + } else { + $d->price = ($plan['price'] + $add_cost + $tax); + } + //$d->price = ($plan['price'] + $add_cost); + $d->created_date = date('Y-m-d H:i:s'); + $d->status = 1; + $d->save(); + $id = $d->id(); + } else { + $d->username = $user['username']; + $d->gateway = $gateway; + $d->plan_id = $plan['id']; + $d->plan_name = $plan['name_plan']; + $d->routers_id = $router['id']; + $d->routers = $router['name']; + if ($plan['validity_unit'] == 'Period') { + // Postpaid price from field + $add_inv = User::getAttribute("Invoice", $id_customer); + if (empty($add_inv) or $add_inv == 0) { + $d->price = ($plan['price'] + $add_cost + $tax); + } else { + $d->price = ($add_inv + $add_cost + $tax); + } + } else { + $d->price = ($plan['price'] + $add_cost + $tax); + } + //$d->price = ($plan['price'] + $add_cost); + $d->created_date = date('Y-m-d H:i:s'); + $d->status = 1; + $d->save(); + } + if (!$id) { + r2(U . "order/package/" . $d['id'], 'e', Lang::T("Failed to create Transaction..")); + } else { + call_user_func($gateway . '_create_transaction', $d, $user); + } + break; + default: + r2(U . "order/package/", 's', ''); +} diff --git a/system/controllers/page.php b/system/controllers/page.php new file mode 100644 index 0000000..d1bd9ad --- /dev/null +++ b/system/controllers/page.php @@ -0,0 +1,21 @@ +assign('_title', Lang::T('Order Voucher')); +$ui->assign('_system_menu', 'order'); + +$action = $routes['1']; +$user = User::_info(); +$ui->assign('_user', $user); + +if(file_exists(__DIR__."/../../pages/".str_replace(".","",$action).".html")){ + $ui->assign("PageFile",$action); + $ui->assign("pageHeader",$action); + run_hook('customer_view_page'); #HOOK + $ui->display('user-pages.tpl'); +}else + $ui->display('404.tpl'); \ No newline at end of file diff --git a/system/controllers/pages.php b/system/controllers/pages.php new file mode 100644 index 0000000..1d60dfa --- /dev/null +++ b/system/controllers/pages.php @@ -0,0 +1,71 @@ +assign('_title', 'Pages'); +$ui->assign('_system_menu', 'pages'); + +$action = $routes['1']; +$ui->assign('_admin', $admin); + +if(strpos($action,"-reset")!==false){ + if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) { + _alert(Lang::T('You do not have permission to access this page'),'danger', "dashboard"); + } + $action = str_replace("-reset","",$action); + $path = "pages/".str_replace(".","",$action).".html"; + $temp = "pages_template/".str_replace(".","",$action).".html"; + if(file_exists($temp)){ + if(!copy($temp, $path)){ + file_put_contents($path, Http::getData('https://raw.githubusercontent.com/hotspotbilling/phpnuxbill/master/pages_template/'.$action.'.html')); + } + }else{ + file_put_contents($path, Http::getData('https://raw.githubusercontent.com/hotspotbilling/phpnuxbill/master/pages_template/'.$action.'.html')); + } + r2(U . 'pages/'.$action); +}else if(strpos($action,"-post")===false){ + if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) { + _alert(Lang::T('You do not have permission to access this page'),'danger', "dashboard"); + } + $path = "pages/".str_replace(".","",$action).".html"; + //echo $path; + run_hook('view_edit_pages'); #HOOK + if(!file_exists($path)){ + $temp = "pages_template/".str_replace(".","",$action).".html"; + if(file_exists($temp)){ + if(!copy($temp, $path)){ + touch($path); + } + }else{ + touch($path); + } + } + if(file_exists($path)){ + $html = file_get_contents($path); + $ui->assign("htmls",str_replace([""],"",$html)); + $ui->assign("writeable",is_writable($path)); + $ui->assign("pageHeader",str_replace('_', ' ', $action)); + $ui->assign("PageFile",$action); + $ui->display('page-edit.tpl'); + }else + $ui->display('a404.tpl'); +}else{ + if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin'])) { + _alert(Lang::T('You do not have permission to access this page'),'danger', "dashboard"); + } + $action = str_replace("-post","",$action); + $path = "pages/".str_replace(".","",$action).".html"; + if(file_exists($path)){ + $html = _post("html"); + run_hook('save_pages'); #HOOK + if(file_put_contents($path, str_replace([""],"",$html))){ + r2(U . 'pages/'.$action, 's', Lang::T("Saving page success")); + }else{ + r2(U . 'pages/'.$action, 'e', Lang::T("Failed to save page, make sure i can write to folder pages, chmod 664 pages/*.html")); + } + }else + $ui->display('a404.tpl'); +} \ No newline at end of file