diff options
Diffstat (limited to 'html/maintenance.php')
-rw-r--r-- | html/maintenance.php | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/html/maintenance.php b/html/maintenance.php index 9dd5806..6d36eb0 100644 --- a/html/maintenance.php +++ b/html/maintenance.php @@ -4,25 +4,27 @@ if ($_SERVER['REMOTE_ADDR'] !== $_SERVER['SERVER_ADDR']) { die(); } -$db = new SQLite3('/srv/http/vhosts/eckner.net/colocation.eckner.net/backend/sqlite.db'); - -$states = array('running', 'ping', 'power'); +include "db.php"; if (array_key_exists('drop',$_GET) && ($_GET['drop']=='DROP')) { - $db -> exec('DROP TABLE "machines"'); - $db -> exec('DROP TABLE "keys"'); - $db -> exec('DROP TABLE "permissions"'); - $db -> exec('DROP TABLE "states"'); - $db -> exec('DROP TABLE "values"'); - $db -> exec('CREATE TABLE "machines" (id integer primary key AUTOINCREMENT, name varchar(32) not null unique, ' . implode(' int not null default 0, ', $states) . ' int not null default 0, last_update TIMESTAMP default CURRENT_TIMESTAMP);'); + $db -> exec('DROP TABLE IF EXISTS "machines"'); + $db -> exec('DROP TABLE IF EXISTS "keys"'); + $db -> exec('DROP TABLE IF EXISTS "permissions"'); + $db -> exec('DROP TABLE IF EXISTS "states"'); + $db -> exec('DROP TABLE IF EXISTS "values"'); + $db -> exec('CREATE TABLE "machines" (id integer primary key AUTOINCREMENT, name varchar(32) not null unique, last_update TIMESTAMP default CURRENT_TIMESTAMP);'); $db -> exec('CREATE TABLE "keys" (id integer primary key AUTOINCREMENT, key varchar(128) not null unique);'); $db -> exec('CREATE TABLE "states" (id integer primary key AUTOINCREMENT, name varchar(32) not null unique)'); - $db -> exec('CREATE TABLE "permissions" (key_id integer not null, machine_id integer not null, state_id integer not null)'); - $db -> exec('CREATE TABLE "values" (machine_id integer not null, state_id integer not null, value integer)'); + $db -> exec('CREATE TABLE "permissions" (key_id integer not null, value_id integer not null)'); + $db -> exec('CREATE TABLE "values" (id integer primary key AUTOINCREMENT, machine_id integer not null, state_id integer not null, value integer)'); + $stm = $db -> prepare('INSERT INTO states (name) VALUES (:state)'); - foreach ($states as $state) { + if (! $stm) + die(); + foreach (array('running', 'ping', 'power') as $state) { $stm -> bindValue('state', $state); - $stm -> execute(); + if (! $stm -> execute()) + die(); } echo 'ok' . "\n"; die(); @@ -50,24 +52,26 @@ if (array_key_exists('machine',$_GET)) { $stm -> close(); $stm = $db -> prepare( - 'INSERT INTO permissions (key_id, machine_id, state_id) VALUES (:key,:machine,1)'); - $stm -> bindValue('machine', $machine_id); - $stm -> bindValue('key', $key_id); - $stm -> execute(); - $stm -> close(); - - $stm = $db -> prepare( 'INSERT INTO "values" (machine_id, state_id) VALUES (:machine, :state)' ); $stm -> bindValue('machine', $machine_id); $result = $db -> query( - 'SELECT states.id FROM states' + 'SELECT states.name, states.id FROM states' ); while ($row = $result -> fetchArray()) { + if ($row['name'] == 'running') + $running_id = $row['id']; $stm -> bindValue('state', $row['id']); $stm -> execute(); } $stm -> close(); + + $stm = $db -> prepare( + 'INSERT INTO permissions (key_id, value_id) VALUES (:key,:value)'); + $stm -> bindValue('value', $running_id); + $stm -> bindValue('key', $key_id); + $stm -> execute(); + $stm -> close(); } $stm = $db -> prepare( |