WIP Ivt Import 2024-05-02

This commit is contained in:
Frank Schubert
2024-05-06 13:24:25 +02:00
parent df108dca2d
commit 6002876343
17 changed files with 1298 additions and 404 deletions

78
lib/RadiusDB/Client.php Normal file
View File

@@ -0,0 +1,78 @@
<?php
class RadiusDB_Client {
private $db;
public function __construct($host=false,$user=false,$pass=false,$dbname=false) {
if(!$host) $host=RADIUSDB_DBHOST;
if(!$user) $user=RADIUSDB_DBUSER;
if(!$pass) $pass=RADIUSDB_DBPASS;
if(!$dbname) $dbname=RADIUSDB_DBNAME;
$this->db = new FronkDB($host,$user,$pass,$dbname);
}
public function getUser($username) {
$user = new RadiusDB_User($this->db);
$user->load($username);
return $user;
}
public function searchUsers($search, $exact=false) {
$users = array();
$search = $this->db->escape($search);
if($exact) {
$res = $this->db->select("radcheck", "username", "username='$search' GROUP BY username ORDER BY LENGTH(username), username");
} else {
$res = $this->db->select("radcheck", "username", "username like '$search' GROUP BY username ORDER BY LENGTH(username), username");
}
if($this->db->num_rows($res)) {
while($data = $this->db->fetch_object($res)) {
$users[] = $data->username;
}
}
return $users;
}
public function searchUsersByIp($ip) {
$users = array();
$ip = $this->db->escape($ip);
$res = $this->db->select("radreply", "username", "attribute='Framed-IP-Address' AND value='$ip' GROUP BY username ORDER BY username");
if($this->db->num_rows($res)) {
while($data = $this->db->fetch_object($res)) {
$users[] = $data->username;
}
}
return $users;
}
public function getUserByCustnum($custnum, $e = false) {
$users = array();
$search = $this->db->escape($custnum);
$field = "Custnum";
if($e) {
$field = "Custnume";
}
$res = $this->db->select("Hotspot_Usersettings", "Username", "$field = '$custnum' GROUP BY Username ORDER BY Username LIMIT 1");
if($this->db->num_rows($res)) {
$data = $this->db->fetch_object($res);
$user = $this->getUser($data->Username);
if($user->username) {
return $user;
}
}
return false;
}
public function getNas($nasname) {
$nas = new RadiusDB_Nas($this->db);
$nas->load($nasname);
return $nas;
}
public function restartRadiusServer() {
exec("/usr/bin/sudo /usr/sbin/service freeradius restart");
return true;
}
}

69
lib/RadiusDB/Nas.php Normal file
View File

@@ -0,0 +1,69 @@
<?php
class RadiusDB_Nas {
private $db;
public $id;
public $nasname;
public $intaddress;
public $shortname;
public $type;
public $secret;
public $description;
public function __construct($db) {
$this->db = $db;
}
public function load($nasname) {
//var_dump($this->db);
$nasname = $this->db->escape($nasname);
$res = $this->db->select("nas", "*", "nasname='$nasname'");
if(!$this->db->num_rows($res)) {
$this->type = "other";
$this->secret = Helper::getNewPassword(24);
return true;
}
$nas = $this->db->fetch_object($res);
$this->id = $nas->id;
$this->nasname = $nas->nasname;
$this->intaddress = $nas->intaddress;
$this->shortname = $nas->shortname;
$this->type = $nas->type;
$this->secret = $nas->secret;
$this->description = $nas->description;
return true;
}
public function save() {
if(!$this->nasname) {
return false;
}
if(!$this->secret) {
return false;
}
if($this->id) {
$id = $this->id;
}
$values['nasname'] = $this->nasname;
$values['intaddress'] = $this->nasname; // is supposed to be the same as nasname
$values['shortname'] = $this->shortname;
$values['type'] = $this->type;
$values['secret'] = $this->secret;
$values['description'] = $this->description;
if($id) {
if(!$this->db->update("nas", $values, "id=$id")) {
return false;
}
} else {
if(!$id = $this->db->insert("nas", $values)) {
return false;
}
}
return $id;
}
}

275
lib/RadiusDB/User.php Normal file
View File

@@ -0,0 +1,275 @@
<?php
class RadiusDB_User {
private $db;
private $username;
private $Usergroup;
private $Checks;
private $Attributes;
private $AllowedNas;
private $Info;
private $_oldUsergroup;
private $_oldChecks;
private $_oldAttributes;
private $_oldAllowedNas;
private $_oldInfo;
private $checkop = ':=';
private $attribop = '=';
public function __construct($db) {
$this->db = $db;
$this->Checks = array();
$this->Attributes = array();
$this->AllowedNas = array();
$this->Info = false;
$this->_oldChecks = array();
$this->_oldAttributes = array();
$this->_oldAllowedNas = array();
$this->_oldInfo = false;
}
public function load($username) {
if(!$username) {
return false;
}
$this->username = $username;
$res = $this->db->select("radcheck","*","username='$username'");
if($this->db->num_rows($res)) {
while($radcheck = $this->db->fetch_object($res)) {
$this->Checks[$radcheck->attribute] = $radcheck->value;
}
} else {
$this->setPassword("");
return false;
}
$res = $this->db->select("radreply","*","username='$username'");
if($this->db->num_rows($res)) {
while($radreply = $this->db->fetch_object($res)) {
$this->Attributes[$radreply->attribute] = $radreply->value;
}
}
$res = $this->db->select("radusergroup", "*", "username='$username'");
if($this->db->num_rows($res)) {
$radusergroup = $this->db->fetch_object($res);
$this->Usergroup = $radusergroup->groupname;
}
$res = $this->db->select("radnascheck", "*", "username='$username'");
if($this->db->num_rows($res)) {
while($nascheck = $this->db->fetch_object($res)) {
$this->AllowedNas[] = $nascheck->nasname;
}
}
$this->Info = new RadiusDB_UserInfo($username);
$this->_oldUsergroup = $this->Usergroup;
$this->_oldChecks = $this->Checks;
$this->_oldAttributes = $this->Attributes;
$this->_oldAllowedNas = $this->AllowedNas;
return true;
}
public function getCheck($name) {
if(isset($this->Checks[$name])) {
return $this->Checks[$name];
}
return null;
}
public function getAttribute($name) {
if(isset($this->Attributes[$name])) {
return $this->Attributes[$name];
}
return null;
}
public function setCheck($name, $value) {
$this->Checks[$name] = $value;
}
public function setAttribute($name, $value) {
$this->Attributes[$name] = $value;
}
public function getPassword() {
return $this->getCheck("Cleartext-Password");
}
public function setPassword($password) {
$this->setCheck("Cleartext-Password", $password);
}
public function getUsergroup() {
return $this->Usergroup;
}
public function setUsergroup($groupname) {
$this->Usergroup = $groupname;
}
public function isNasAllowed($nas_ip) {
if(in_array($nas_ip, $this->AllowedNas)) {
return true;
}
return false;
}
public function addAllowedNas($nas_ip) {
if(!in_array($nas_ip, $this->AllowedNas,true)) {
$this->AllowedNas[] = $nas_ip;
}
}
public function removeAllowedNas($nas_ip) {
if(in_array($nas_ip, $this->AllowedNas, true) !== false) {
unset($this->AllowedNas[array_search($nas_ip, $this->AllowedNas)]);
}
}
public function unset($name) {
if($name == "usergroup") {
$this->Usergroup = false;
}
if(isset($this->Checks[$name])) {
unset($this->Checks[$name]);
return true;
}
if(isset($this->Attributes[$name])) {
unset($this->Attributes[$name]);
return true;
}
return false;
}
public function save() {
if(!$this->username) {
return false;
}
$username = $this->username;
$error = false;
$this->db->query("START TRANSACTION"); // XXX should be put moved to a FronkDB function
// check queries
foreach($this->Checks as $attribute => $value) {
if(isset($this->_oldChecks[$attribute])) {
if(!$this->db->update("radcheck", ['value' => $value], "username='$username' AND attribute='$attribute'")) {
$error = true;
}
} else {
if(!$this->db->insert("radcheck", ['username' => $username, 'attribute' => $attribute, 'op' => $this->checkop, 'value' => $value])) {
$error = true;
}
}
}
// attribute queries
foreach($this->Attributes as $attribute => $value) {
if(isset($this->_oldAttributes[$attribute])) {
if(!$this->db->update("radreply", ['value' => $value], "username='$username' AND attribute='$attribute'")) {
$error = true;
}
} else {
if(!$this->db->insert("radreply", ['username' => $username, 'attribute' => $attribute, 'op' => $this->attribop, 'value' => $value])) {
$error = true;
}
}
}
// update usergroup
if($this->Usergroup) {
if($this->_oldUsergroup) {
if(!$this->db->update("radusergroup", ['groupname' => $this->Usergroup], "username='$username'")) {
$error = true;
}
} else {
if(!$this->db->insert("radusergroup", ['username' => $username, 'groupname' => $this->Usergroup, 'priority' => 1])) {
$error = true;
}
}
}
// update Nascheck
foreach($this->AllowedNas as $nas) {
if(!in_array($nas, $this->_oldAllowedNas, true)) {
if(!$this->db->insert("radnascheck", ['username' => $username, 'nasname' => $nas])) {
$error = true;
}
}
}
// delete attributes
foreach($this->_oldChecks as $attribute => $value) {
if(!isset($this->Checks[$attribute])) {
if(!$this->db->delete("radcheck", "username='$username' AND attribute='$attribute'")) {
$error = true;
}
}
}
foreach($this->_oldAttributes as $attribute => $old) {
if(!isset($this->Attributes[$attribute])) {
if(!$this->db->delete("radreply", "username='$username' AND attribute='$attribute'")) {
$error = true;
}
}
}
if($this->_oldUsergroup && !$this->Usergroup) {
if(!$this->db->delete("radusergroup", "username='$username'")) {
$error = true;
}
}
foreach($this->_oldAllowedNas as $oldnas) {
if(!in_array($oldnas, $this->AllowedNas, true)) {
if(!$this->db->delete("radnascheck", "username='$username' AND nasname='$oldnas'")) {
$error = true;
}
}
}
if($error) {
$this->db->query("ROLLBACK");
return false;
} else {
$this->db->query("COMMIT");
}
// reinitialize _old* values
$this->_oldUsergroup = $this->Usergroup;
$this->_oldChecks = $this->Checks;
$this->_oldAttributes = $this->Attributes;
$this->_oldAllowedNas = $this->AllowedNas;
return true;
}
public function __get($name) {
if($name === "username") {
return $this->username;
}
if(in_array($name, array_keys($this->Checks))) {
return $this->getCheck($name);
}
if(in_array($name, array_keys($this->Attributes))) {
return $this->getAttribute($name);
}
}
public function __toString() {
return (string) $this->username;
}
}

43
lib/RadiusDB/UserInfo.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
class RadiusDB_UserInfo {
private $id;
private $Username;
private $custnum;
private $custnume;
private $ContractUp;
private $ContractDown;
private $Duraction;
private $Info;
private $Wifikey;
private $ont_sn;
public function __construct($db) {
$this->db = $db;
}
public function load($username) {
if(!$username) {
return false;
}
$this->username = $username;
$res = $this->db->select("Hotspot_Usersettings", "*", "Username='$username'");
if($this->db->num_rows($res)) {
$rad = $this->db->fetch_object($res);
$this->id = $rad->id;
$this->Username = $rad->Username;
$this->Custnum = $rad->Custnum;
$this->Custnume = $rad->Custnume;
$this->ContractUp = $rad->ContratUp;
$this->ContractDown = $rad->ContractDown;
$this->Duration = $rad->Duration;
$this->Info = $rad->Info;
$this->Wifikey = $rad->Wifikey;
$this->ont_sn = $rad->ont_sn;
}
}
}