summaryrefslogtreecommitdiff
path: root/html/db.php
blob: b10f679ed05663f1b864ab40c1e036d99ddec222 (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
<?php

if (isset($db))
  return;

class MyDB extends SQLite3 {
  function __construct() {
    parent::__construct('/srv/http/vhosts/eckner.net/colocation.eckner.net/backend/sqlite.db');
  }
  function exec($query) {
    $result = parent::exec($query);
    if (!$result)
      die();
    return $result;
  }
  function query($query) {
    $result = parent::query($query);
    if (!$result)
      die();
    return $result;
  }
  function prepare($query) {
    $result = parent::prepare($query);
    if (!$result)
      die();
    return $result;
  }
  function add_key() {
    $stm = prepare('INSERT INTO keys (key) VALUES (:key)');
    $stm -> bindValue('key', bin2hex(random_bytes(64)));
    $stm -> execute();
    $key_id = $db -> lastInsertRowID();
    $stm -> close();
    return $key_id;
  }
}

$db = new MyDB();
if(!$db) {
  echo $db->lastErrorMsg();
  die();
}