phpnuxbill/system/autoload/Radius.php

314 lines
12 KiB
PHP
Raw Normal View History

2022-10-05 11:51:18 +08:00
<?php
2023-10-12 16:01:49 +08:00
2023-10-12 16:55:42 +08:00
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
**/
2023-10-12 15:33:37 +08:00
/**
* Radius Class
* based https://gist.github.com/nasirhafeez/6669b24aab0bda545f60f9da5ed14f25
*/
2023-10-03 16:46:55 +08:00
class Radius
{
2022-10-05 11:51:18 +08:00
2023-10-12 16:01:49 +08:00
public static function getClient()
{
global $config;
2023-10-27 09:57:57 +08:00
if(empty($config['radius_client'])){
if(function_exists("shell_exec")){
shell_exec('which radclient');
}else{
return "";
}
}else{
$config['radius_client'];
}
2023-10-12 16:01:49 +08:00
}
2023-10-03 16:46:55 +08:00
public static function getTableNas()
{
2023-08-28 09:45:35 +08:00
return ORM::for_table('nas', 'radius');
}
2023-10-03 16:46:55 +08:00
public static function getTableCustomer()
{
2023-08-28 09:45:35 +08:00
return ORM::for_table('radcheck', 'radius');
}
2023-10-16 11:09:58 +08:00
public static function getTableCustomerAttr()
{
return ORM::for_table('radreply', 'radius');
}
2023-10-03 16:46:55 +08:00
public static function getTablePackage()
{
2023-08-28 09:45:35 +08:00
return ORM::for_table('radgroupreply', 'radius');
}
2023-10-03 16:46:55 +08:00
public static function getTableUserPackage()
{
2023-08-28 09:45:35 +08:00
return ORM::for_table('radusergroup', 'radius');
}
2023-10-24 14:55:34 +08:00
public static function nasAdd($name, $ip, $ports, $secret, $routers = "", $description = "", $type = 'other', $server = null, $community = null)
2023-10-03 16:46:55 +08:00
{
2023-10-03 16:03:47 +08:00
$n = Radius::getTableNas()->create();
2023-08-28 09:45:35 +08:00
$n->nasname = $ip;
$n->shortname = $name;
$n->type = $type;
$n->ports = $ports;
$n->secret = $secret;
$n->description = $description;
$n->server = $server;
$n->community = $community;
2023-10-24 14:55:34 +08:00
$n->routers = $routers;
2023-08-28 09:45:35 +08:00
$n->save();
return $n->id();
}
2023-10-03 16:03:47 +08:00
2023-10-24 14:55:34 +08:00
public static function nasUpdate($id, $name, $ip, $ports, $secret, $routers = "", $description = "", $type = 'other', $server = null, $community = null)
2023-10-03 16:46:55 +08:00
{
2023-10-03 16:03:47 +08:00
$n = Radius::getTableNas()->find_one($id);
2023-10-03 16:46:55 +08:00
if (empty($n)) {
2023-08-28 09:45:35 +08:00
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;
2023-10-24 14:55:34 +08:00
$n->routers = $routers;
2023-10-03 16:03:47 +08:00
return $n->save();
2023-08-28 09:45:35 +08:00
}
2023-10-12 14:32:45 +08:00
public static function planUpSert($plan_id, $rate, $pool = null)
2023-10-03 16:46:55 +08:00
{
$rates = explode('/', $rate);
2023-10-12 14:32:45 +08:00
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', $rate, ':=');
2023-10-16 11:09:58 +08:00
// if ($pool != null) {
// Radius::upsertPackage($plan_id, 'Framed-Pool', $pool, ':=');
// }
2023-10-03 16:46:55 +08:00
}
2023-10-12 14:32:45 +08:00
public static function planDelete($plan_id)
2023-10-03 16:46:55 +08:00
{
2023-10-04 16:41:48 +08:00
// Delete Plan
2023-10-12 14:32:45 +08:00
Radius::getTablePackage()->where_equal('plan_id', "plan_" . $plan_id)->delete_many();
2023-10-04 17:11:55 +08:00
// Reset User Plan
2023-10-12 14:32:45 +08:00
$c = Radius::getTableUserPackage()->where_equal('groupname', "plan_" . $plan_id)->findMany();
2023-10-04 17:11:55 +08:00
if ($c) {
2023-10-12 14:32:45 +08:00
foreach ($c as $u) {
2023-10-04 17:11:55 +08:00
$u->groupname = '';
$u->save();
}
}
2023-10-04 16:41:48 +08:00
}
2023-10-12 14:32:45 +08:00
public static function customerChangeUsername($from, $to)
{
2023-10-04 15:16:39 +08:00
$c = Radius::getTableCustomer()->where_equal('username', $from)->findMany();
if ($c) {
2023-10-12 14:32:45 +08:00
foreach ($c as $u) {
2023-10-04 15:16:39 +08:00
$u->username = $to;
$u->save();
}
}
$c = Radius::getTableUserPackage()->where_equal('username', $from)->findMany();
if ($c) {
2023-10-12 14:32:45 +08:00
foreach ($c as $u) {
2023-10-04 15:16:39 +08:00
$u->username = $to;
$u->save();
}
}
}
2023-10-24 14:55:34 +08:00
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);
2023-10-17 17:32:18 +08:00
}
2023-10-04 15:16:39 +08:00
}
2023-10-12 16:47:45 +08:00
return '';
2023-10-04 15:16:39 +08:00
}
2023-10-04 15:00:04 +08:00
2023-10-12 14:32:45 +08:00
public static function customerDelete($username)
{
2023-10-04 16:41:48 +08:00
Radius::getTableCustomer()->where_equal('username', $username)->delete_many();
Radius::getTableUserPackage()->where_equal('username', $username)->delete_many();
}
2023-10-04 15:00:04 +08:00
/**
* When add a plan to Customer, use this
*/
2023-10-12 15:33:37 +08:00
public static function customerAddPlan($customer, $plan, $expired = null)
2023-10-12 14:32:45 +08:00
{
global $config;
2023-10-12 14:32:45 +08:00
if (Radius::customerUpsert($customer, $plan)) {
2023-10-04 15:00:04 +08:00
$p = Radius::getTableUserPackage()->where_equal('username', $customer['username'])->findOne();
if ($p) {
// if exists
2023-10-12 14:32:45 +08:00
$p->groupname = "plan_" . $plan['id'];
2023-10-12 15:33:37 +08:00
$p->save();
2023-10-12 14:32:45 +08:00
} else {
2023-10-04 15:00:04 +08:00
$p = Radius::getTableUserPackage()->create();
$p->username = $customer['username'];
2023-10-12 14:32:45 +08:00
$p->groupname = "plan_" . $plan['id'];
2023-10-04 15:00:04 +08:00
$p->priority = 1;
2023-10-12 15:33:37 +08:00
$p->save();
2023-10-04 15:00:04 +08:00
}
2023-10-12 16:47:45 +08:00
if ($plan['type'] == 'Hotspot' && $plan['typebp'] == "Limited") {
2023-10-12 15:33:37 +08:00
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'], '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'], 'Mikrotik-Total-Limit', $datalimit);
2023-10-12 15:33:37 +08:00
} else if ($plan['limit_type'] == "Both_Limit") {
if ($plan['time_unit'] == 'Hrs')
$timelimit = $plan['time_limit'] * 60 * 60;
else
$timelimit = $plan['time_limit'] . ":00";
if ($plan['data_unit'] == 'GB')
$datalimit = $plan['data_limit'] . "000000000";
else
$datalimit = $plan['data_limit'] . "000000";
//Radius::upsertCustomer($customer['username'], 'Max-Volume', $datalimit);
2023-10-12 16:01:49 +08:00
Radius::upsertCustomer($customer['username'], 'Expire-After', $timelimit);
// Mikrotik Spesific
2023-10-13 17:58:07 +08:00
Radius::upsertCustomer($customer['username'], 'Mikrotik-Total-Limit', $datalimit);
2023-10-12 15:33:37 +08:00
}
2023-10-12 16:01:49 +08:00
} else {
//Radius::delAtribute(Radius::getTableCustomer(), 'Max-Volume', 'username', $customer['username']);
Radius::delAtribute(Radius::getTableCustomer(), 'Expire-After', 'username', $customer['username']);
2023-10-13 17:58:07 +08:00
Radius::delAtribute(Radius::getTableCustomer(), 'Mikrotik-Total-Limit', 'username', $customer['username']);
2023-10-12 15:33:37 +08:00
}
// expired user
2023-10-12 16:01:49 +08:00
if ($expired != null) {
2023-10-16 17:53:56 +08:00
//Radius::upsertCustomer($customer['username'], 'access-period', strtotime($expired) - time());
2023-10-12 15:33:37 +08:00
Radius::upsertCustomer($customer['username'], 'expiration', date('d M Y H:i:s', strtotime($expired)));
// Mikrotik Spesific
Radius::upsertCustomer(
$customer['username'],
'WISPr-Session-Terminate-Time',
2023-10-16 10:57:22 +08:00
date('Y-m-d', strtotime($expired)) . 'T' . date('H:i:s', strtotime($expired)) . Timezone::getTimeOffset($config['timezone'])
);
2023-10-12 16:01:49 +08:00
} else {
2023-10-16 17:53:56 +08:00
//Radius::delAtribute(Radius::getTableCustomer(), 'access-period', 'username', $customer['username']);
2023-10-16 10:57:22 +08:00
Radius::delAtribute(Radius::getTableCustomer(), 'expiration', 'username', $customer['username']);
2023-10-12 14:42:52 +08:00
}
2023-10-16 11:09:58 +08:00
if ($plan['type'] == 'PPPOE') {
Radius::upsertCustomerAttr($customer['username'], 'Framed-Pool', $plan['pool'], ':=');
}
2023-10-12 15:33:37 +08:00
return true;
2023-10-04 15:00:04 +08:00
}
return false;
}
2023-10-12 14:32:45 +08:00
public static function customerUpsert($customer, $plan)
2023-10-04 15:00:04 +08:00
{
2023-10-12 14:32:45 +08:00
if ($plan['type'] == 'PPPOE') {
Radius::upsertCustomer($customer['username'], 'Cleartext-Password', (empty($customer['pppoe_password'])) ? $customer['password'] : $customer['pppoe_password']);
2023-10-04 15:00:04 +08:00
} else {
2023-10-12 14:32:45 +08:00
Radius::upsertCustomer($customer['username'], 'Cleartext-Password', $customer['password']);
2023-10-04 15:00:04 +08:00
}
2023-10-12 14:42:52 +08:00
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']);
2023-10-16 10:57:22 +08:00
Radius::upsertCustomer($customer['username'], 'Mikrotik-Wireless-Comment', $customer['fullname']);
2023-10-12 16:47:45 +08:00
return true;
2023-10-04 15:00:04 +08:00
}
2023-10-12 14:32:45 +08:00
2023-10-16 10:57:22 +08:00
private static function delAtribute($tabel, $attribute, $key, $value)
{
$r = $tabel->where_equal($key, $value)->whereEqual('attribute', $attribute)->findOne();
2023-10-16 10:57:22 +08:00
if ($r) $r->delete();
}
2023-10-12 14:32:45 +08:00
/**
* 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
*/
private 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();
}
2023-10-16 11:09:58 +08:00
/**
* To insert or update existing customer Attribute
*/
2023-10-16 11:15:45 +08:00
public static function upsertCustomerAttr($username, $attr, $value, $op = ':=')
2023-10-16 11:09:58 +08:00
{
$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();
}
2023-10-12 16:01:49 +08:00
2023-10-16 10:57:22 +08:00
public static function disconnectCustomer($username)
{
2023-10-12 16:01:49 +08:00
$nas = Radius::getTableNas()->findMany();
2023-10-16 10:57:22 +08:00
$count = count($nas) * 15;
2023-10-12 16:01:49 +08:00
set_time_limit($count);
2023-10-12 16:47:45 +08:00
$result = [];
2023-10-16 10:57:22 +08:00
foreach ($nas as $n) {
2023-10-12 16:01:49 +08:00
$port = 3799;
2023-10-16 10:57:22 +08:00
if (!empty($n['ports'])) {
2023-10-12 16:01:49 +08:00
$port = $n['ports'];
}
2023-10-17 17:32:18 +08:00
$result[] = $n['nasname'] . ': ' . @shell_exec("echo 'User-Name = $username' | " . Radius::getClient() . " " . trim($n['nasname']) . ":$port disconnect '" . $n['secret'] . "'");
2023-10-12 16:01:49 +08:00
}
2023-10-12 16:47:45 +08:00
return $result;
2023-10-12 16:01:49 +08:00
}
2022-10-05 11:51:18 +08:00
}