summaryrefslogtreecommitdiff
path: root/html/index.php
blob: 0964dc30ad5e3a36bd4638ffc0c952055e99923e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

$db = new SQLite3('/srv/http/vhosts/eckner.net/colocation.eckner.net/backend/sqlite.db');

if ($_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR']) {
  if (array_key_exists('new',$_GET)) {
    $stm = $db -> prepare('SELECT COUNT(1) FROM machines WHERE machines.name=:name');
    $stm -> bindValue('name', $_GET['new']);
    $result = $stm -> execute();
    $result = $result -> fetchArray();
    $stm -> close();

    if ($result[0] == 0) {
      $stm = $db -> prepare('INSERT INTO machines (name,key,online) VALUES (:name,:key,0)');
      $stm -> bindValue('name', $_GET['new']);
      $stm -> bindValue('key', bin2hex(random_bytes(64)));
      $stm -> execute();
      $stm -> close();
    }

    $stm = $db -> prepare('SELECT machines.key FROM machines WHERE machines.name=:name');
    $stm -> bindValue('name', $_GET['new']);
    $result = $stm -> execute();
    $result = $result -> fetchArray();
    echo 'key = ' . $result['key'] . "\n";
    die();
  }
}

$columns = array('name', 'online', 'last_update');

$result = $db -> query('SELECT machines.name,machines.online,machines.last_update FROM machines;');

echo '<html>' . "\n";
echo '<body>' . "\n";
echo '<table>' . "\n";
echo '  <tr>' . "\n";
foreach ($columns as $column) {
  echo '    <th>' . $column . '</th>' . "\n";
}
echo '  </tr>' . "\n";

while ($row = $result -> fetchArray()) {
  echo '  <tr>' . "\n";
  foreach ($columns as $column) {
    echo '    <td>' . $row[$column] . '</td>' . "\n";
  }
  echo '  </tr>' . "\n";
}
echo '</table>' . "\n";
echo '</body>' . "\n";
echo '</html>' . "\n";