diff --git a/fn/common.php b/fn/common.php index f3fd671..161c624 100644 --- a/fn/common.php +++ b/fn/common.php @@ -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) { diff --git a/fn/ht.php b/fn/ht.php index 08e3f1a..a7afaca 100644 --- a/fn/ht.php +++ b/fn/ht.php @@ -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) { diff --git a/pages/auth/register.php b/pages/auth/register.php index dc0927b..936bfde 100644 --- a/pages/auth/register.php +++ b/pages/auth/register.php @@ -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']; diff --git a/pages/ns/zone-add.php b/pages/ns/zone-add.php index 4561c21..a3bb6c8 100644 --- a/pages/ns/zone-add.php +++ b/pages/ns/zone-add.php @@ -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"; diff --git a/pages/reg/register.php b/pages/reg/register.php index 11e4594..8f807d6 100644 --- a/pages/reg/register.php +++ b/pages/reg/register.php @@ -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."); }