Change to devices

This commit is contained in:
Ibnu Maksum
2024-06-05 17:19:24 +07:00
parent c21c7689a8
commit 8ea853c617
16 changed files with 814 additions and 1652 deletions

View File

@ -8,6 +8,8 @@
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
use PEAR2\Net\RouterOS;
require $root_path . 'system/autoload/mail/Exception.php';
require $root_path . 'system/autoload/mail/PHPMailer.php';
require $root_path . 'system/autoload/mail/SMTP.php';
@ -37,10 +39,8 @@ class Message
if (strlen($txt) > 160) {
$txts = str_split($txt, 160);
try {
$mikrotik = Mikrotik::info($config['sms_url']);
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
foreach ($txts as $txt) {
Mikrotik::sendSMS($client, $phone, $txt);
self::sendSMS($config['sms_url'], $phone, $txt);
}
} catch (Exception $e) {
// ignore, add to logs
@ -48,9 +48,7 @@ class Message
}
} else {
try {
$mikrotik = Mikrotik::info($config['sms_url']);
$client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
Mikrotik::sendSMS($client, $phone, $txt);
self::sendSMS($config['sms_url'], $phone, $txt);
} catch (Exception $e) {
// ignore, add to logs
_log("Failed to send SMS using Mikrotik.\n" . $e->getMessage(), 'SMS', 0);
@ -64,6 +62,24 @@ class Message
}
}
public static function MikrotikSendSMS($router_name, $to, $message)
{
global $_app_stage, $client_m;
if ($_app_stage == 'demo') {
return null;
}
if(!isset($client_m)){
$mikrotik = ORM::for_table('tbl_routers')->where('name', $router_name)->find_one();
$iport = explode(":", $mikrotik['ip_address']);
$client_m = new RouterOS\Client($iport[0], $mikrotik['username'], $mikrotik['password'], ($iport[1]) ? $iport[1] : null);
}
$smsRequest = new RouterOS\Request('/tool sms send');
$smsRequest
->setArgument('phone-number', $to)
->setArgument('message', $message);
$client_m->sendSync($smsRequest);
}
public static function sendWhatsapp($phone, $txt)
{
global $config;

View File

@ -1,562 +0,0 @@
<?php
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
**/
use PEAR2\Net\RouterOS;
class Mikrotik
{
public static function info($name)
{
return ORM::for_table('tbl_routers')->where('name', $name)->find_one();
}
public static function getClient($ip, $user, $pass)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$iport = explode(":", $ip);
return new RouterOS\Client($iport[0], $user, $pass, ($iport[1]) ? $iport[1] : null);
}
public static function isUserLogin($client, $username)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip hotspot active print',
RouterOS\Query::where('user', $username)
);
return $client->sendSync($printRequest)->getProperty('.id');
}
public static function logMeIn($client, $user, $pass, $ip, $mac)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$addRequest = new RouterOS\Request('/ip/hotspot/active/login');
$client->sendSync(
$addRequest
->setArgument('user', $user)
->setArgument('password', $pass)
->setArgument('ip', $ip)
->setArgument('mac-address', $mac)
);
}
public static function logMeOut($client, $user)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip hotspot active print',
RouterOS\Query::where('user', $user)
);
$id = $client->sendSync($printRequest)->getProperty('.id');
$removeRequest = new RouterOS\Request('/ip/hotspot/active/remove');
$client->sendSync(
$removeRequest
->setArgument('numbers', $id)
);
}
public static function addHotspotPlan($client, $name, $sharedusers, $rate)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$addRequest = new RouterOS\Request('/ip/hotspot/user/profile/add');
$client->sendSync(
$addRequest
->setArgument('name', $name)
->setArgument('shared-users', $sharedusers)
->setArgument('rate-limit', $rate)
);
}
public static function setHotspotPlan($client, $name, $sharedusers, $rate)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip hotspot user profile print .proplist=.id',
RouterOS\Query::where('name', $name)
);
$profileID = $client->sendSync($printRequest)->getProperty('.id');
if (empty($profileID)) {
Mikrotik::addHotspotPlan($client, $name, $sharedusers, $rate);
} else {
$setRequest = new RouterOS\Request('/ip/hotspot/user/profile/set');
$client->sendSync(
$setRequest
->setArgument('numbers', $profileID)
->setArgument('shared-users', $sharedusers)
->setArgument('rate-limit', $rate)
);
}
}
public static function setHotspotExpiredPlan($client, $name, $pool)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip hotspot user profile print .proplist=.id',
RouterOS\Query::where('name', $name)
);
$profileID = $client->sendSync($printRequest)->getProperty('.id');
if (empty($profileID)) {
$addRequest = new RouterOS\Request('/ip/hotspot/user/profile/add');
$client->sendSync(
$addRequest
->setArgument('name', $name)
->setArgument('shared-users', 3)
->setArgument('address-pool', $pool)
->setArgument('rate-limit', '512K/512K')
);
} else {
$setRequest = new RouterOS\Request('/ip/hotspot/user/profile/set');
$client->sendSync(
$setRequest
->setArgument('numbers', $profileID)
->setArgument('shared-users', 3)
->setArgument('address-pool', $pool)
->setArgument('rate-limit', '512K/512K')
);
}
}
public static function removeHotspotPlan($client, $name)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip hotspot user profile print .proplist=.id',
RouterOS\Query::where('name', $name)
);
$profileID = $client->sendSync($printRequest)->getProperty('.id');
$removeRequest = new RouterOS\Request('/ip/hotspot/user/profile/remove');
$client->sendSync(
$removeRequest
->setArgument('numbers', $profileID)
);
}
public static function removeHotspotUser($client, $username)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip hotspot user print .proplist=.id',
RouterOS\Query::where('name', $username)
);
$userID = $client->sendSync($printRequest)->getProperty('.id');
$removeRequest = new RouterOS\Request('/ip/hotspot/user/remove');
$client->sendSync(
$removeRequest
->setArgument('numbers', $userID)
);
}
public static function addHotspotUser($client, $plan, $customer)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$addRequest = new RouterOS\Request('/ip/hotspot/user/add');
if ($plan['typebp'] == "Limited") {
if ($plan['limit_type'] == "Time_Limit") {
if ($plan['time_unit'] == 'Hrs')
$timelimit = $plan['time_limit'] . ":00:00";
else
$timelimit = "00:" . $plan['time_limit'] . ":00";
$client->sendSync(
$addRequest
->setArgument('name', $customer['username'])
->setArgument('profile', $plan['name_plan'])
->setArgument('password', $customer['password'])
->setArgument('comment', $customer['fullname'])
->setArgument('email', $customer['email'])
->setArgument('limit-uptime', $timelimit)
);
} else if ($plan['limit_type'] == "Data_Limit") {
if ($plan['data_unit'] == 'GB')
$datalimit = $plan['data_limit'] . "000000000";
else
$datalimit = $plan['data_limit'] . "000000";
$client->sendSync(
$addRequest
->setArgument('name', $customer['username'])
->setArgument('profile', $plan['name_plan'])
->setArgument('password', $customer['password'])
->setArgument('comment', $customer['fullname'])
->setArgument('email', $customer['email'])
->setArgument('limit-bytes-total', $datalimit)
);
} else if ($plan['limit_type'] == "Both_Limit") {
if ($plan['time_unit'] == 'Hrs')
$timelimit = $plan['time_limit'] . ":00:00";
else
$timelimit = "00:" . $plan['time_limit'] . ":00";
if ($plan['data_unit'] == 'GB')
$datalimit = $plan['data_limit'] . "000000000";
else
$datalimit = $plan['data_limit'] . "000000";
$client->sendSync(
$addRequest
->setArgument('name', $customer['username'])
->setArgument('profile', $plan['name_plan'])
->setArgument('password', $customer['password'])
->setArgument('comment', $customer['fullname'])
->setArgument('email', $customer['email'])
->setArgument('limit-uptime', $timelimit)
->setArgument('limit-bytes-total', $datalimit)
);
}
} else {
$client->sendSync(
$addRequest
->setArgument('name', $customer['username'])
->setArgument('profile', $plan['name_plan'])
->setArgument('comment', $customer['fullname'])
->setArgument('email', $customer['email'])
->setArgument('password', $customer['password'])
);
}
}
public static function setHotspotUser($client, $user, $pass)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request('/ip/hotspot/user/print');
$printRequest->setArgument('.proplist', '.id');
$printRequest->setQuery(RouterOS\Query::where('name', $user));
$id = $client->sendSync($printRequest)->getProperty('.id');
$setRequest = new RouterOS\Request('/ip/hotspot/user/set');
$setRequest->setArgument('numbers', $id);
$setRequest->setArgument('password', $pass);
$client->sendSync($setRequest);
}
public static function setHotspotUserPackage($client, $user, $plan)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request('/ip/hotspot/user/print');
$printRequest->setArgument('.proplist', '.id');
$printRequest->setQuery(RouterOS\Query::where('name', $user));
$id = $client->sendSync($printRequest)->getProperty('.id');
$setRequest = new RouterOS\Request('/ip/hotspot/user/set');
$setRequest->setArgument('numbers', $id);
$setRequest->setArgument('profile', $plan);
$client->sendSync($setRequest);
}
public static function removeHotspotActiveUser($client, $username)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$onlineRequest = new RouterOS\Request('/ip/hotspot/active/print');
$onlineRequest->setArgument('.proplist', '.id');
$onlineRequest->setQuery(RouterOS\Query::where('user', $username));
$id = $client->sendSync($onlineRequest)->getProperty('.id');
$removeRequest = new RouterOS\Request('/ip/hotspot/active/remove');
$removeRequest->setArgument('numbers', $id);
$client->sendSync($removeRequest);
}
public static function removePpoeUser($client, $username)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request('/ppp/secret/print');
//$printRequest->setArgument('.proplist', '.id');
$printRequest->setQuery(RouterOS\Query::where('name', $username));
$id = $client->sendSync($printRequest)->getProperty('.id');
$removeRequest = new RouterOS\Request('/ppp/secret/remove');
$removeRequest->setArgument('numbers', $id);
$client->sendSync($removeRequest);
}
public static function addPpoeUser($client, $plan, $customer)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$addRequest = new RouterOS\Request('/ppp/secret/add');
if (!empty($customer['pppoe_password'])) {
$pass = $customer['pppoe_password'];
} else {
$pass = $customer['password'];
}
$client->sendSync(
$addRequest
->setArgument('name', $customer['username'])
->setArgument('service', 'pppoe')
->setArgument('profile', $plan['name_plan'])
->setArgument('comment', $customer['fullname'] . ' | ' . $customer['email'])
->setArgument('password', $pass)
);
}
public static function setPpoeUser($client, $user, $pass)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request('/ppp/secret/print');
$printRequest->setArgument('.proplist', '.id');
$printRequest->setQuery(RouterOS\Query::where('name', $user));
$id = $client->sendSync($printRequest)->getProperty('.id');
$setRequest = new RouterOS\Request('/ppp/secret/set');
$setRequest->setArgument('numbers', $id);
$setRequest->setArgument('password', $pass);
$client->sendSync($setRequest);
}
public static function setPpoeUserPlan($client, $user, $plan)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request('/ppp/secret/print');
$printRequest->setArgument('.proplist', '.id');
$printRequest->setQuery(RouterOS\Query::where('name', $user));
$id = $client->sendSync($printRequest)->getProperty('.id');
$setRequest = new RouterOS\Request('/ppp/secret/set');
$setRequest->setArgument('numbers', $id);
$setRequest->setArgument('profile', $plan);
$client->sendSync($setRequest);
}
public static function removePpoeActive($client, $username)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$onlineRequest = new RouterOS\Request('/ppp/active/print');
$onlineRequest->setArgument('.proplist', '.id');
$onlineRequest->setQuery(RouterOS\Query::where('name', $username));
$id = $client->sendSync($onlineRequest)->getProperty('.id');
$removeRequest = new RouterOS\Request('/ppp/active/remove');
$removeRequest->setArgument('numbers', $id);
$client->sendSync($removeRequest);
}
public static function removePool($client, $name)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip pool print .proplist=.id',
RouterOS\Query::where('name', $name)
);
$poolID = $client->sendSync($printRequest)->getProperty('.id');
$removeRequest = new RouterOS\Request('/ip/pool/remove');
$client->sendSync(
$removeRequest
->setArgument('numbers', $poolID)
);
}
public static function addPool($client, $name, $ip_address)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$addRequest = new RouterOS\Request('/ip/pool/add');
$client->sendSync(
$addRequest
->setArgument('name', $name)
->setArgument('ranges', $ip_address)
);
}
public static function setPool($client, $name, $ip_address)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip pool print .proplist=.id',
RouterOS\Query::where('name', $name)
);
$poolID = $client->sendSync($printRequest)->getProperty('.id');
if (empty($poolID)) {
self::addPool($client, $name, $ip_address);
} else {
$setRequest = new RouterOS\Request('/ip/pool/set');
$client->sendSync(
$setRequest
->setArgument('numbers', $poolID)
->setArgument('ranges', $ip_address)
);
}
}
public static function addPpoePlan($client, $name, $pool, $rate)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$addRequest = new RouterOS\Request('/ppp/profile/add');
$client->sendSync(
$addRequest
->setArgument('name', $name)
->setArgument('local-address', $pool)
->setArgument('remote-address', $pool)
->setArgument('rate-limit', $rate)
);
}
public static function setPpoePlan($client, $name, $pool, $rate)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ppp profile print .proplist=.id',
RouterOS\Query::where('name', $name)
);
$profileID = $client->sendSync($printRequest)->getProperty('.id');
if (empty($profileID)) {
self::addPpoePlan($client, $name, $pool, $rate);
} else {
$setRequest = new RouterOS\Request('/ppp/profile/set');
$client->sendSync(
$setRequest
->setArgument('numbers', $profileID)
->setArgument('local-address', $pool)
->setArgument('remote-address', $pool)
->setArgument('rate-limit', $rate)
);
}
}
public static function removePpoePlan($client, $name)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ppp profile print .proplist=.id',
RouterOS\Query::where('name', $name)
);
$profileID = $client->sendSync($printRequest)->getProperty('.id');
$removeRequest = new RouterOS\Request('/ppp/profile/remove');
$client->sendSync(
$removeRequest
->setArgument('numbers', $profileID)
);
}
public static function sendSMS($client, $to, $message)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$smsRequest = new RouterOS\Request('/tool sms send');
$smsRequest
->setArgument('phone-number', $to)
->setArgument('message', $message);
$client->sendSync($smsRequest);
}
public static function getIpHotspotUser($client, $username){
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip hotspot active print',
RouterOS\Query::where('user', $username)
);
return $client->sendSync($printRequest)->getProperty('address');
}
public static function addIpToAddressList($client, $ip, $listName, $comment = '')
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$addRequest = new RouterOS\Request('/ip/firewall/address-list/add');
$client->sendSync(
$addRequest
->setArgument('address', $ip)
->setArgument('comment', $comment)
->setArgument('list', $listName)
);
}
public static function removeIpFromAddressList($client, $ip)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
$printRequest = new RouterOS\Request(
'/ip firewall address-list print .proplist=.id',
RouterOS\Query::where('address', $ip)
);
$id = $client->sendSync($printRequest)->getProperty('.id');
$removeRequest = new RouterOS\Request('/ip/firewall/address-list/remove');
$client->sendSync(
$removeRequest
->setArgument('numbers', $id)
);
}
}

View File

@ -221,10 +221,10 @@ class Package
}
if ($isChangePlan || $b['status'] == 'off') {
$dvc = $DEVICE_PATH . DIRECTORY_SEPARATOR . $p['device'] . '.php';
$dvc = Package::getDevice($p);
if (file_exists($dvc)) {
include $dvc;
new $p['device']->connect_customer($c, $p);
require_once $dvc;
new $p['device']->add_customer($c, $p);
} else {
new Exception(Lang::T("Devices Not Found"));
}
@ -303,11 +303,11 @@ class Package
"\nPrice: " . Lang::moneyFormat($p['price'] + $add_cost) .
"\nNote:\n" . $note);
} else {
// plan not exists
$dvc = $DEVICE_PATH . DIRECTORY_SEPARATOR . $p['device'] . '.php';
// active plan not exists
$dvc = Package::getDevice($p);
if (file_exists($dvc)) {
include $dvc;
new $p['device']->connect_customer($c, $p);
require_once $dvc;
new $p['device']->add_customer($c, $p);
} else {
new Exception(Lang::T("Devices Not Found"));
}
@ -412,86 +412,13 @@ class Package
$p = ORM::for_table('tbl_plans')->where('id', $plan_id)->find_one();
$b = ORM::for_table('tbl_user_recharges')->find_one($from_id);
$dvc = $DEVICE_PATH . DIRECTORY_SEPARATOR . $p['device'] . '.php';
$dvc = Package::getDevice($p);
if (file_exists($dvc)) {
include $dvc;
require_once $dvc;
new $p['device']->change_customer($b, $c, $p);
} else {
new Exception(Lang::T("Devices Not Found"));
}
// if ($p['routers'] == $b['routers'] && $b['routers'] != 'radius') {
// $mikrotik = Mikrotik::info($p['routers']);
// } else {
// $mikrotik = Mikrotik::info($b['routers']);
// }
// // delete first
// if ($p['type'] == 'Hotspot') {
// if ($b) {
// if (!$p['is_radius']) {
// $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
// Mikrotik::removeHotspotUser($client, $c['username']);
// Mikrotik::removeHotspotActiveUser($client, $c['username']);
// }
// } else {
// if (!$p['is_radius']) {
// $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
// Mikrotik::removeHotspotUser($client, $c['username']);
// Mikrotik::removeHotspotActiveUser($client, $c['username']);
// }
// }
// } else {
// if ($b) {
// if (!$p['is_radius']) {
// $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
// Mikrotik::removePpoeUser($client, $c['username']);
// Mikrotik::removePpoeActive($client, $c['username']);
// }
// } else {
// if (!$p['is_radius']) {
// $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
// Mikrotik::removePpoeUser($client, $c['username']);
// Mikrotik::removePpoeActive($client, $c['username']);
// }
// }
// }
// // call the next mikrotik
// if ($p['routers'] != $b['routers'] && $p['routers'] != 'radius') {
// $mikrotik = Mikrotik::info($p['routers']);
// }
// if ($p['type'] == 'Hotspot') {
// if ($b) {
// if ($p['is_radius']) {
// Radius::customerAddPlan($c, $p, $b['expiration'] . '' . $b['time']);
// } else {
// $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
// Mikrotik::addHotspotUser($client, $p, $c);
// }
// } else {
// if ($p['is_radius']) {
// Radius::customerAddPlan($c, $p, $b['expiration'] . '' . $b['time']);
// } else {
// $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
// Mikrotik::addHotspotUser($client, $p, $c);
// }
// }
// } else {
// if ($b) {
// if ($p['is_radius']) {
// Radius::customerAddPlan($c, $p);
// } else {
// $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
// Mikrotik::addPpoeUser($client, $p, $c);
// }
// } else {
// if ($p['is_radius']) {
// Radius::customerAddPlan($c, $p);
// } else {
// $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']);
// Mikrotik::addPpoeUser($client, $p, $c);
// }
// }
// }
}
@ -620,4 +547,25 @@ class Package
$tax = $price * $tax_rate_decimal;
return $tax;
}
public static function getDevice($plan)
{
global $DEVICE_PATH;
if (!empty($plan['device'])) {
return $DEVICE_PATH . DIRECTORY_SEPARATOR . $plan['device'] . '.php';
}
if ($plan['is_radius'] == 1) {
$plan->device = 'Radius';
$plan->save();
return $DEVICE_PATH . DIRECTORY_SEPARATOR . 'Radius' . '.php';
}
if ($plan['type'] == 'PPPOE') {
$plan->device = 'MikrotikPppoe';
$plan->save();
return $DEVICE_PATH . DIRECTORY_SEPARATOR . 'MikrotikPppoe' . '.php';
}
$plan->device = 'MikrotikHotspot';
$plan->save();
return $DEVICE_PATH . DIRECTORY_SEPARATOR . 'MikrotikHotspot' . '.php';
}
}

View File

@ -1,350 +0,0 @@
<?php
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
**/
/**
* Radius Class
* based https://gist.github.com/nasirhafeez/6669b24aab0bda545f60f9da5ed14f25
*/
class Radius
{
public static function getClient()
{
global $config;
if (empty($config['radius_client'])) {
if (function_exists("shell_exec")) {
shell_exec('which radclient');
} else {
return "";
}
} else {
$config['radius_client'];
}
}
public static function getTableNas()
{
return ORM::for_table('nas', 'radius');
}
public static function getTableAcct()
{
return ORM::for_table('radacct', 'radius');
}
public static function getTableCustomer()
{
return ORM::for_table('radcheck', 'radius');
}
public static function getTableCustomerAttr()
{
return ORM::for_table('radreply', 'radius');
}
public static function getTablePackage()
{
return ORM::for_table('radgroupreply', 'radius');
}
public static function getTableUserPackage()
{
return ORM::for_table('radusergroup', 'radius');
}
public static function nasAdd($name, $ip, $ports, $secret, $routers = "", $description = "", $type = 'other', $server = null, $community = null)
{
$n = Radius::getTableNas()->create();
$n->nasname = $ip;
$n->shortname = $name;
$n->type = $type;
$n->ports = $ports;
$n->secret = $secret;
$n->description = $description;
$n->server = $server;
$n->community = $community;
$n->routers = $routers;
$n->save();
return $n->id();
}
public static function nasUpdate($id, $name, $ip, $ports, $secret, $routers = "", $description = "", $type = 'other', $server = null, $community = null)
{
$n = Radius::getTableNas()->find_one($id);
if (empty($n)) {
return false;
}
$n->nasname = $ip;
$n->shortname = $name;
$n->type = $type;
$n->ports = $ports;
$n->secret = $secret;
$n->description = $description;
$n->server = $server;
$n->community = $community;
$n->routers = $routers;
return $n->save();
}
public static function planUpSert($plan_id, $rate, $pool = null)
{
$rates = explode('/', $rate);
##burst fixed
if (strpos($rate, ' ')) {
$ratos = $rates[0] . '/' . $rates[1] . ' ' . $rates[2] . '/' . $rates[3] . '/' . $rates[4] . '/' . $rates[5] . '/' . $rates[6];
} else {
$ratos = $rates[0] . '/' . $rates[1];
}
Radius::upsertPackage($plan_id, 'Ascend-Data-Rate', $rates[1], ':=');
Radius::upsertPackage($plan_id, 'Ascend-Xmit-Rate', $rates[0], ':=');
Radius::upsertPackage($plan_id, 'Mikrotik-Rate-Limit', $ratos, ':=');
// if ($pool != null) {
// Radius::upsertPackage($plan_id, 'Framed-Pool', $pool, ':=');
// }
}
public static function planDelete($plan_id)
{
// Delete Plan
Radius::getTablePackage()->where_equal('plan_id', "plan_" . $plan_id)->delete_many();
// Reset User Plan
$c = Radius::getTableUserPackage()->where_equal('groupname', "plan_" . $plan_id)->findMany();
if ($c) {
foreach ($c as $u) {
$u->groupname = '';
$u->save();
}
}
}
public static function customerChangeUsername($from, $to)
{
$c = Radius::getTableCustomer()->where_equal('username', $from)->findMany();
if ($c) {
foreach ($c as $u) {
$u->username = $to;
$u->save();
}
}
$c = Radius::getTableUserPackage()->where_equal('username', $from)->findMany();
if ($c) {
foreach ($c as $u) {
$u->username = $to;
$u->save();
}
}
}
public static function customerDeactivate($username, $radiusDisconnect = true)
{ {
global $radius_pass;
$r = Radius::getTableCustomer()->where_equal('username', $username)->whereEqual('attribute', 'Cleartext-Password')->findOne();
if ($r) {
// no need to delete, because it will make ID got higher
// we just change the password
$r->value = md5(time() . $username . $radius_pass);
$r->save();
if ($radiusDisconnect)
return Radius::disconnectCustomer($username);
}
}
return '';
}
public static function customerDelete($username)
{
Radius::getTableCustomer()->where_equal('username', $username)->delete_many();
Radius::getTableUserPackage()->where_equal('username', $username)->delete_many();
}
/**
* When add a plan to Customer, use this
*/
public static function customerAddPlan($customer, $plan, $expired = null)
{
global $config;
if (Radius::customerUpsert($customer, $plan)) {
$p = Radius::getTableUserPackage()->where_equal('username', $customer['username'])->findOne();
if ($p) {
// if exists
Radius::delAtribute(Radius::getTableCustomer(), 'Max-All-Session', 'username', $customer['username']);
//Radius::delAtribute(Radius::getTableCustomer(), 'Max-Volume', 'username', $customer['username']);
Radius::delAtribute(Radius::getTableCustomer(), 'Max-Data', 'username', $customer['username']);
$p->groupname = "plan_" . $plan['id'];
$p->save();
} else {
$p = Radius::getTableUserPackage()->create();
$p->username = $customer['username'];
$p->groupname = "plan_" . $plan['id'];
$p->priority = 1;
$p->save();
}
if ($plan['type'] == 'Hotspot' && $plan['typebp'] == "Limited") {
if ($plan['limit_type'] == "Time_Limit") {
if ($plan['time_unit'] == 'Hrs')
$timelimit = $plan['time_limit'] * 60 * 60;
else
$timelimit = $plan['time_limit'] * 60;
Radius::upsertCustomer($customer['username'], 'Max-All-Session', $timelimit);
//Radius::upsertCustomer($customer['username'], 'Expire-After', $timelimit);
} else if ($plan['limit_type'] == "Data_Limit") {
if ($plan['data_unit'] == 'GB')
$datalimit = $plan['data_limit'] . "000000000";
else
$datalimit = $plan['data_limit'] . "000000";
//Radius::upsertCustomer($customer['username'], 'Max-Volume', $datalimit);
// Mikrotik Spesific
Radius::upsertCustomer($customer['username'], 'Max-Data', $datalimit);
//Radius::upsertCustomer($customer['username'], 'Mikrotik-Total-Limit', $datalimit);
} else if ($plan['limit_type'] == "Both_Limit") {
if ($plan['time_unit'] == 'Hrs')
$timelimit = $plan['time_limit'] * 60 * 60;
else
$timelimit = $plan['time_limit'] * 60;
if ($plan['data_unit'] == 'GB')
$datalimit = $plan['data_limit'] . "000000000";
else
$datalimit = $plan['data_limit'] . "000000";
//Radius::upsertCustomer($customer['username'], 'Max-Volume', $datalimit);
Radius::upsertCustomer($customer['username'], 'Max-All-Session', $timelimit);
// Mikrotik Spesific
Radius::upsertCustomer($customer['username'], 'Max-Data', $datalimit);
//Radius::upsertCustomer($customer['username'], 'Mikrotik-Total-Limit', $datalimit);
}
} else {
//Radius::delAtribute(Radius::getTableCustomer(), 'Max-Volume', 'username', $customer['username']);
Radius::delAtribute(Radius::getTableCustomer(), 'Max-All-Session', 'username', $customer['username']);
Radius::delAtribute(Radius::getTableCustomer(), 'Max-Data', 'username', $customer['username']);
}
Radius::disconnectCustomer($customer['username']);
Radius::getTableAcct()->where_equal('username', $customer['username'])->delete_many();
// expired user
if ($expired != null) {
//Radius::upsertCustomer($customer['username'], 'access-period', strtotime($expired) - time());
Radius::upsertCustomer($customer['username'], 'Max-All-Session', strtotime($expired) - time());
//Radius::upsertCustomer($customer['username'], 'expiration', date('d M Y H:i:s', strtotime($expired)));
// Mikrotik Spesific
Radius::upsertCustomer(
$customer['username'],
'WISPr-Session-Terminate-Time',
date('Y-m-d', strtotime($expired)) . 'T' . date('H:i:s', strtotime($expired)) . Timezone::getTimeOffset($config['timezone'])
);
} else {
Radius::delAtribute(Radius::getTableCustomer(), 'Max-All-Session', 'username', $customer['username']);
//Radius::delAtribute(Radius::getTableCustomer(), 'access-period', 'username', $customer['username']);
//Radius::delAtribute(Radius::getTableCustomer(), 'expiration', 'username', $customer['username']);
}
if ($plan['type'] == 'PPPOE') {
Radius::upsertCustomerAttr($customer['username'], 'Framed-Pool', $plan['pool'], ':=');
}
return true;
}
return false;
}
public static function customerUpsert($customer, $plan)
{
if ($plan['type'] == 'PPPOE') {
Radius::upsertCustomer($customer['username'], 'Cleartext-Password', (empty($customer['pppoe_password'])) ? $customer['password'] : $customer['pppoe_password']);
} else {
Radius::upsertCustomer($customer['username'], 'Cleartext-Password', $customer['password']);
}
Radius::upsertCustomer($customer['username'], 'Simultaneous-Use', ($plan['type'] == 'PPPOE') ? 1 : $plan['shared_users']);
// Mikrotik Spesific
Radius::upsertCustomer($customer['username'], 'Port-Limit', ($plan['type'] == 'PPPOE') ? 1 : $plan['shared_users']);
Radius::upsertCustomer($customer['username'], 'Mikrotik-Wireless-Comment', $customer['fullname']);
return true;
}
private static function delAtribute($tabel, $attribute, $key, $value)
{
$r = $tabel->where_equal($key, $value)->whereEqual('attribute', $attribute)->findOne();
if ($r) $r->delete();
}
/**
* To insert or update existing plan
*/
private static function upsertPackage($plan_id, $attr, $value, $op = ':=')
{
$r = Radius::getTablePackage()->where_equal('plan_id', $plan_id)->whereEqual('attribute', $attr)->find_one();
if (!$r) {
$r = Radius::getTablePackage()->create();
$r->groupname = "plan_" . $plan_id;
$r->plan_id = $plan_id;
}
$r->attribute = $attr;
$r->op = $op;
$r->value = $value;
return $r->save();
}
/**
* To insert or update existing customer
*/
public static function upsertCustomer($username, $attr, $value, $op = ':=')
{
$r = Radius::getTableCustomer()->where_equal('username', $username)->whereEqual('attribute', $attr)->find_one();
if (!$r) {
$r = Radius::getTableCustomer()->create();
$r->username = $username;
}
$r->attribute = $attr;
$r->op = $op;
$r->value = $value;
return $r->save();
}
/**
* To insert or update existing customer Attribute
*/
public static function upsertCustomerAttr($username, $attr, $value, $op = ':=')
{
$r = Radius::getTableCustomerAttr()->where_equal('username', $username)->whereEqual('attribute', $attr)->find_one();
if (!$r) {
$r = Radius::getTableCustomerAttr()->create();
$r->username = $username;
}
$r->attribute = $attr;
$r->op = $op;
$r->value = $value;
return $r->save();
}
public static function disconnectCustomer($username)
{
global $_app_stage;
if ($_app_stage == 'demo') {
return null;
}
/**
* Fix loop to all Nas but still detecting Hotspot Multylogin from other Nas
*/
$act = ORM::for_table('radacct')->where_raw("acctstoptime IS NULL")->where('username', $username)->find_one();
$nas = Radius::getTableNas()->where('nasname', $act['nasipaddress'])->find_many();
$count = count($nas) * 15;
set_time_limit($count);
$result = [];
foreach ($nas as $n) {
$port = 3799;
if (!empty($n['ports'])) {
$port = $n['ports'];
}
$result[] = $n['nasname'] . ': ' . @shell_exec("echo 'User-Name = $username,Framed-IP-Address = " . $act['framedipaddress'] . "' | radclient -x " . trim($n['nasname']) . ":$port disconnect '" . $n['secret'] . "'");
}
return $result;
}
}