Factorize "INSERT INTO" SQL queries with insert()

This commit is contained in:
Miraty 2022-09-14 17:19:17 +02:00
parent f06e42645a
commit 5885f7a416
5 changed files with 52 additions and 40 deletions

View File

@ -37,6 +37,35 @@ function processForm($requireLogin = true) {
return true;
}
function insert($table, $values) {
$query = 'INSERT INTO ' . $table . '(';
foreach ($values as $key => $val) {
if ($key === array_key_last($values))
$query .= "$key";
else
$query .= "$key, ";
}
$query .= ") VALUES(";
foreach ($values as $key => $val) {
if ($key === array_key_last($values))
$query .= ":$key";
else
$query .= ":$key, ";
}
$query .= ")";
$db = new PDO('sqlite:' . DB_PATH);
$op = $db->prepare($query);
foreach ($values as $key => $val)
$op->bindValue(":$key", $val);
$op->execute();
}
function query($action, $table, $conditions = [], $column = NULL) {
$query = match ($action) {

View File

@ -22,22 +22,15 @@ function listFsDirs($username) {
}
function addSite($username, $siteDir, $domain, $domainType, $protocol) {
$db = new PDO('sqlite:' . DB_PATH);
$op = $db->prepare("INSERT INTO sites(username, site_dir, domain, domain_type, protocol, creation_date, le_enabled) VALUES(:username, :site_dir, :domain, :domain_type, :protocol, :creation_date, :le_enabled)");
$op->bindValue(':username', $username);
$op->bindValue(':site_dir', $siteDir);
$op->bindValue(':domain', $domain);
$op->bindValue(':domain_type', $domainType);
$op->bindValue(':protocol', $protocol);
$op->bindValue(':creation_date', date("Y-m-d H:i:s"));
if ($domainType === "dns" AND $protocol === "http")
$op->bindValue(':le_enabled', 0);
else
$op->bindValue(':le_enabled', NULL);
$op->execute();
insert('sites', [
'username' => $username,
'site_dir' => $siteDir,
'domain' => $domain,
'domain_type' => $domainType,
'protocol' => $protocol,
'creation_date' => date("Y-m-d H:i:s"),
'le_enabled' => (($domainType === "dns" AND $protocol === "http") ? 0 : NULL),
]);
}
function dirsStatuses($username, $domainType, $protocol) {

View File

@ -25,15 +25,11 @@ if (processForm(false)) {
if ($code !== 0)
serverError("Can't create Tor keys directory.");
$db = new PDO('sqlite:' . DB_PATH);
$stmt = $db->prepare("INSERT INTO users(username, password, registration_date) VALUES(:username, :password, :registration_date)");
$stmt->bindValue(':username', $_POST['username']);
$stmt->bindValue(':password', hashPassword($_POST['password']));
$stmt->bindValue(':registration_date', date("Y-m-d H:i:s"));
$stmt->execute();
insert('users', [
'username' => $_POST['username'],
'password' => hashPassword($_POST['password']),
'registration_date' => date("Y-m-d H:i:s"),
]);
$_SESSION['username'] = $_POST['username'];

View File

@ -14,13 +14,10 @@ if (processForm()) {
if (equalArrays(CONF['ns']['servers'], $matches[1]) !== true)
userError("Les serveurs ayant autorité dans cette zone indiqués par la zone parente ne sont pas ceux de Niver.");
$db = new PDO('sqlite:' . DB_PATH);
$stmt = $db->prepare("INSERT INTO zones(zone, username) VALUES(:zone, :username)");
$stmt->bindValue(':zone', $_POST['domain']);
$stmt->bindValue(':username', $_SESSION['username']);
$stmt->execute();
insert('zones', [
'zone' => $_POST['domain'],
'username' => $_SESSION['username'],
]);
$knotZonePath = CONF['ns']['knot_zones_path'] . "/" . $_POST['domain'] . "zone";
$knotZone = $_POST['domain'] . ' 3600 SOA ' . CONF['ns']['servers'][0] . ' admin.niver.test. 1 21600 7200 3628800 3600' . "\n";

View File

@ -12,14 +12,11 @@ if (processForm()) {
if (in_array($_POST['subdomain'], explode("\n", file_get_contents(CONF['common']['root_path'] . '/pages/reg/reserved.txt'))))
userError("Ce domaine n'est pas disponible à l'enregistrement. Il est réservé.");
$db = new PDO('sqlite:' . DB_PATH);
$stmt = $db->prepare("INSERT INTO registry(domain, username, last_renewal) VALUES(:domain, :username, :last_renewal)");
$stmt->bindValue(':domain', $domain);
$stmt->bindValue(':username', $_SESSION['username']);
$stmt->bindValue(':last_renewal', date("Y-m-d H:i:s"));
$stmt->execute();
insert('registry', [
'domain' => $domain,
'username' => $_SESSION['username'],
'last_renewal' => date("Y-m-d H:i:s"),
]);
success("Domaine ajouté au registre.");
}