phpnuxbill/system/autoload/Radius.php

284 lines
9.8 KiB
PHP
Raw Normal View History

2022-10-05 11:51:18 +08:00
<?php
2023-10-03 16:46:55 +08:00
class Radius
{
2022-10-05 11:51:18 +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-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-04 15:00:04 +08:00
public static function nasList($search = null)
{
if ($search == null) {
2023-10-03 16:46:55 +08:00
return ORM::for_table('nas', 'radius')->find_many();
2023-10-04 15:00:04 +08:00
} else {
2023-10-03 16:46:55 +08:00
return ORM::for_table('nas', 'radius')
->where_like('nasname', $search)
->where_like('shortname', $search)
->where_like('description', $search)
->find_many();
}
}
public static function nasAdd($name, $ip, $ports, $secret, $description = "", $type = 'other', $server = null, $community = null)
{
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;
$n->save();
return $n->id();
}
2023-10-03 16:03:47 +08:00
2023-10-03 16:46:55 +08:00
public static function nasUpdate($id, $name, $ip, $ports, $secret, $description = "", $type = 'other', $server = null, $community = null)
{
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-03 16:03:47 +08:00
return $n->save();
2023-08-28 09:45:35 +08:00
}
2023-10-04 15:16:39 +08:00
public static function planAdd($plan_id, $rate, $pool = null)
2023-10-03 16:46:55 +08:00
{
$rates = explode('/', $rate);
$r = Radius::getTablePackage()->create();
2023-10-04 15:16:39 +08:00
$r->groupname = "plan_".$plan_id;
2023-10-03 16:46:55 +08:00
$r->attribute = 'Ascend-Data-Rate';
$r->op = ':=';
$r->value = $rates[1];
$r->plan_id = $plan_id;
if ($r->save()) {
$r = Radius::getTablePackage()->create();
2023-10-04 15:16:39 +08:00
$r->groupname = "plan_".$plan_id;
2023-10-03 16:46:55 +08:00
$r->attribute = 'Ascend-Xmit-Rate';
$r->op = ':=';
$r->value = $rates[0];
$r->plan_id = $plan_id;
if ($r->save()) {
if ($pool != null) {
$r = Radius::getTablePackage()->create();
2023-10-04 15:16:39 +08:00
$r->groupname = "plan_".$plan_id;
2023-10-03 16:46:55 +08:00
$r->attribute = 'Framed-Pool';
$r->op = ':=';
$r->value = $pool;
$r->plan_id = $plan_id;
if ($r->save()) {
return true;
}
} else {
return true;
}
}
}
return false;
}
2023-10-04 15:16:39 +08:00
public static function planUpdate($plan_id, $rate, $pool = null)
2023-10-03 16:46:55 +08:00
{
$rates = explode('/', $rate);
2023-10-04 15:00:04 +08:00
if (Radius::getTablePackage()->where_equal('plan_id', $plan_id)->find_one()) {
2023-10-04 12:37:32 +08:00
$r = Radius::getTablePackage()->where_equal('plan_id', $plan_id)->whereEqual('attribute', 'Ascend-Data-Rate')->findOne();
2023-10-04 15:16:39 +08:00
$r->groupname = "plan_".$plan_id;
2023-10-04 12:37:32 +08:00
$r->value = $rates[1];
2023-10-03 16:46:55 +08:00
if ($r->save()) {
2023-10-04 12:37:32 +08:00
$r = Radius::getTablePackage()->where_equal('plan_id', $plan_id)->whereEqual('attribute', 'Ascend-Xmit-Rate')->findOne();
2023-10-04 15:16:39 +08:00
$r->groupname = "plan_".$plan_id;
2023-10-04 12:37:32 +08:00
$r->value = $rates[0];
if ($r->save()) {
if ($pool != null) {
$r = Radius::getTablePackage()->where_equal('plan_id', $plan_id)->whereEqual('attribute', 'Framed-Pool')->findOne();
2023-10-04 15:16:39 +08:00
$r->groupname = "plan_".$plan_id;
2023-10-04 12:37:32 +08:00
$r->value = $pool;
if ($r->save()) {
return true;
}
} else {
2023-10-03 16:46:55 +08:00
return true;
}
}
}
2023-10-04 15:00:04 +08:00
} else {
if (!empty($plan_id)) {
2023-10-04 15:16:39 +08:00
return Radius::planAdd($plan_id, $rate, $pool);
2023-10-04 12:37:32 +08:00
}
2023-10-03 16:46:55 +08:00
}
return false;
}
2023-10-04 16:41:48 +08:00
public static function planDelete($plan_id){
// Delete Plan
Radius::getTablePackage()->where_equal('plan_id', "plan_".$plan_id)->delete_many();
// Delete User Plan
Radius::getTableUserPackage()->where_equal('groupname', "plan_".$plan_id)->delete_many();
}
2023-10-04 15:16:39 +08:00
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($customer){
global $radius_pass;
$r = Radius::getTableCustomer()->where_equal('username', $customer['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().$customer['username'].$radius_pass);
$r->save();
}
}
2023-10-04 15:00:04 +08:00
2023-10-04 16:41:48 +08:00
public static function customerDelete($username){
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
*/
public static function customerAddPlan($customer, $plan){
if(Radius::customerAdd($customer, $plan)){
$p = Radius::getTableUserPackage()->where_equal('username', $customer['username'])->findOne();
if ($p) {
// if exists
2023-10-04 15:16:39 +08:00
$p->groupname = "plan_".$plan['id'];
2023-10-04 15:00:04 +08:00
return $p->save();
}else{
$p = Radius::getTableUserPackage()->create();
$p->username = $customer['username'];
2023-10-04 15:16:39 +08:00
$p->groupname = "plan_".$plan['id'];
2023-10-04 15:00:04 +08:00
$p->priority = 1;
return $p->save();
}
}
return false;
}
public static function customerAdd($customer, $plan)
{
if (Radius::getTableCustomer()->where_equal('username', $customer['username'])->findOne()) {
// Edit if exists
$r = Radius::getTableCustomer()->where_equal('username', $customer['username'])->whereEqual('attribute', 'Cleartext-Password')->findOne();
if($r){
if($plan['type']=='PPPOE'){
if(empty($customer['pppoe_password'])){
$r->value = $customer['password'];
}else{
$r->value = $customer['pppoe_password'];
}
}else{
$r->value = $customer['password'];
}
$r->save();
}else{
$r = Radius::getTableCustomer()->create();
$r->username = $customer['username'];
$r->attribute = 'Cleartext-Password';
$r->op = ':=';
if($plan['type']=='PPPOE'){
if(empty($customer['pppoe_password'])){
$r->value = $customer['password'];
}else{
$r->value = $customer['pppoe_password'];
}
}else{
$r->value = $customer['password'];
}
$r->save();
}
$r = Radius::getTableCustomer()->where_equal('username', $customer['username'])->whereEqual('attribute', 'Simultaneous-Use')->findOne();
if($r){
if($plan['type']=='PPPOE'){
$r->value = 1;
}else{
$r->value = $plan['shared_users'];
}
$r->save();
}else{
$r = Radius::getTableCustomer()->create();
$r->username = $customer['username'];
$r->attribute = 'Simultaneous-Use';
$r->op = ':=';
if($plan['type']=='PPPOE'){
$r->value = 1;
}else{
$r->value = $plan['shared_users'];
}
$r->save();
}
return true;
} else {
// add if not exists
$r = Radius::getTableCustomer()->create();
$r->username = $customer['username'];
$r->attribute = 'Cleartext-Password';
$r->op = ':=';
if($plan['type']=='PPPOE'){
if(empty($customer['pppoe_password'])){
$r->value = $customer['password'];
}else{
$r->value = $customer['pppoe_password'];
}
}else{
$r->value = $customer['password'];
}
if ($r->save()) {
$r = Radius::getTableCustomer()->create();
$r->username = $customer['username'];
$r->attribute = 'Simultaneous-Use';
$r->op = ':=';
if($plan['type']=='PPPOE'){
$r->value = 1;
}else{
$r->value = $plan['shared_users'];
}
$r->save();
return true;
}
}
return false;
}
2022-10-05 11:51:18 +08:00
}