From 76c7c036938be3cb6f90396de51958a90e8bf8b0 Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Thu, 28 Jul 2022 11:19:17 +0200 Subject: insert lo-keys by ids --- html/db.php | 11 +++++++++++ html/maintenance.php | 7 +++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/html/db.php b/html/db.php index 3e87fcc..9889c71 100644 --- a/html/db.php +++ b/html/db.php @@ -25,6 +25,17 @@ class MyDB extends SQLite3 { die(); return $result; } + function get_state_id($state) { + $stm = $this -> prepare('SELECT states.id AS state_id FROM states WHERE states.name = :state'); + $stm -> bindValue('state', $state); + $result = $stm -> execute(); + if (! $result) + die(); + $result = $result -> fetchArray(); + if (! $result) + die(); + return $result['state_id']; + } function add_key($comment) { $stm = $this -> prepare('INSERT INTO keys (key, comment) VALUES (:key, :comment)'); $stm -> bindValue('key', bin2hex(random_bytes(64))); diff --git a/html/maintenance.php b/html/maintenance.php index 338695e..c7f608f 100644 --- a/html/maintenance.php +++ b/html/maintenance.php @@ -66,9 +66,12 @@ if (array_key_exists('lights_out_machines', $_GET)) { 'SELECT machines.name AS machine FROM machines' . ' WHERE machines.name LIKE ' . "'" . '%-lo' . "'" ); + $power_id = $db -> get_state_id('power'); + $ping_id = $db -> get_state_id('ping'); while ($row = $result -> fetchArray()) { - $db -> add_key_for(substr($row['machine'],0,-3), 'power', 'lights-out'); - $db -> add_key_for(substr($row['machine'],0,-3), 'ping', 'lights-out'); + $machine_id = $db -> get_machine_id(substr($row['machine'],0,-3)); + $db -> add_key_for($machine_id, $power_id, 'lights-out'); + $db -> add_key_for($machine_id, $ping_id, 'lights-out'); } echo 'done.' . "\n"; die(); -- cgit v1.2.3-54-g00ecf