Add Domain Group Management

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2019-12-13 15:29:42 +00:00
parent 600c16c269
commit 912e2ddbd9
6 changed files with 563 additions and 11 deletions

View File

@@ -358,12 +358,160 @@ elseif($_REQUEST['action'] == "delete_client")
// Delete client identified by ID
try
{
$stmt = $db->prepare('DELETE FROM client_by_group WHERE client_id=:id');
if(!$stmt)
{
throw new Exception("While preparing client_by_group statement: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
{
throw new Exception("While binding id to client_by_group statement: ".$db->lastErrorMsg());
}
if(!$stmt->execute())
{
throw new Exception("While executing client_by_group statement: ".$db->lastErrorMsg());
}
$stmt = $db->prepare('DELETE FROM client WHERE id=:id');
if(!$stmt)
{
throw new Exception("While preparing client statement: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
{
throw new Exception("While binding id to client statement: ".$db->lastErrorMsg());
}
if(!$stmt->execute())
{
throw new Exception("While executing client statement: ".$db->lastErrorMsg());
}
$reload = true;
return JSON_success();
}
catch (\Exception $ex)
{
return JSON_error($ex->getMessage());
}
}
elseif($_REQUEST['action'] == "get_domains")
{
// List all available groups
try
{
$query = $db->query("SELECT * FROM domainlist;");
if(!$query)
{
throw new Exception("Error while querying gravity's domainlist table: ".$db->lastErrorMsg());
}
$data = array();
while($res = $query->fetchArray(SQLITE3_ASSOC))
{
$group_query = $db->query("SELECT group_id FROM domainlist_by_group WHERE domainlist_id = ".$res["id"].";");
if(!$group_query)
{
throw new Exception("Error while querying gravity's domainlist_by_group table: ".$db->lastErrorMsg());
}
$groups = array();
while($gres = $group_query->fetchArray(SQLITE3_ASSOC))
{
array_push($groups,$gres["group_id"]);
}
$res["groups"] = $groups;
array_push($data,$res);
}
echo json_encode(array("data" => $data));
}
catch (\Exception $ex)
{
return JSON_error($ex->getMessage());
}
}
elseif($_REQUEST['action'] == "add_domain")
{
// Add new domain
try
{
$stmt = $db->prepare('INSERT INTO domainlist (domain,type,comment) VALUES (:domain,:type,:comment)');
if(!$stmt)
{
throw new Exception("While preparing statement: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':domain', $_POST["domain"], SQLITE3_TEXT))
{
throw new Exception("While binding domain: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':type', intval($_POST["type"]), SQLITE3_TEXT))
{
throw new Exception("While binding type: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':comment', $_POST["comment"], SQLITE3_TEXT))
{
throw new Exception("While binding comment: ".$db->lastErrorMsg());
}
if(!$stmt->execute())
{
throw new Exception("While executing: ".$db->lastErrorMsg());
}
$reload = true;
return JSON_success();
}
catch (\Exception $ex)
{
return JSON_error($ex->getMessage());
}
}
elseif($_REQUEST['action'] == "edit_domain")
{
// Edit domain identified by ID
try
{
$stmt = $db->prepare('UPDATE domainlist SET enabled=:enabled, comment=:comment, type=:type WHERE id = :id');
if(!$stmt)
{
throw new Exception("While preparing statement: ".$db->lastErrorMsg());
}
$status = intval($_POST["status"]);
if($status !== 0)
{
$status = 1;
}
if(!$stmt->bindValue(':enabled', $status, SQLITE3_INTEGER))
{
throw new Exception("While binding enabled: ".$db->lastErrorMsg());
}
$comment = $_POST["comment"];
if(strlen($comment) == 0)
{
// Store NULL in database for empty comments
$comment = null;
}
if(!$stmt->bindValue(':comment', $comment, SQLITE3_TEXT))
{
throw new Exception("While binding comment: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':type', intval($_POST["type"]), SQLITE3_INTEGER))
{
throw new Exception("While binding type: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
{
throw new Exception("While binding id: ".$db->lastErrorMsg());
@@ -374,6 +522,93 @@ elseif($_REQUEST['action'] == "delete_client")
throw new Exception("While executing: ".$db->lastErrorMsg());
}
$stmt = $db->prepare('DELETE FROM domainlist_by_group WHERE domainlist_id = :id');
if(!$stmt)
{
throw new Exception("While preparing DELETE statement: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
{
throw new Exception("While binding id: ".$db->lastErrorMsg());
}
if(!$stmt->execute())
{
throw new Exception("While executing DELETE statement: ".$db->lastErrorMsg());
}
$db->query("BEGIN TRANSACTION;");
foreach ($_POST["groups"] as $gid)
{
$stmt = $db->prepare('INSERT INTO domainlist_by_group (domainlist_id,group_id) VALUES(:id,:gid);');
if(!$stmt)
{
throw new Exception("While preparing INSERT INTO statement: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
{
throw new Exception("While binding id: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':gid', intval($gid), SQLITE3_INTEGER))
{
throw new Exception("While binding gid: ".$db->lastErrorMsg());
}
if(!$stmt->execute())
{
throw new Exception("While executing INSERT INTO statement: ".$db->lastErrorMsg());
}
}
$db->query("COMMIT;");
$reload = true;
return JSON_success();
}
catch (\Exception $ex)
{
return JSON_error($ex->getMessage());
}
}
elseif($_REQUEST['action'] == "delete_domain")
{
// Delete domain identified by ID
try
{
$stmt = $db->prepare('DELETE FROM domainlist_by_group WHERE domainlist_id=:id');
if(!$stmt)
{
throw new Exception("While preparing domainlist_by_group statement: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
{
throw new Exception("While binding id to domainlist_by_group statement: ".$db->lastErrorMsg());
}
if(!$stmt->execute())
{
throw new Exception("While executing domainlist_by_group statement: ".$db->lastErrorMsg());
}
$stmt = $db->prepare('DELETE FROM domainlist WHERE id=:id');
if(!$stmt)
{
throw new Exception("While preparing domainlist statement: ".$db->lastErrorMsg());
}
if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
{
throw new Exception("While binding id to domainlist statement: ".$db->lastErrorMsg());
}
if(!$stmt->execute())
{
throw new Exception("While executing domainlist statement: ".$db->lastErrorMsg());
}
$reload = true;
return JSON_success();
}