-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·94 lines (82 loc) · 2.55 KB
/
index.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
define("DATABASE", 'database.sqlite');
// Collect input from CLI or web server
if($argc > 0) {
if($argc === 3) {
list($script, $method, $key) = $argv;
$value = null;
} else if ($argc === 4) {
list($script, $method, $key, $value) = $argv;
} else {
die("Usage: php $file [method=get|post] [key] [json if post]\n");
}
} else {
$script = $_SERVER['PHP_SELF'];
$dir = dirname($script);
$regex = "!^$dir|$script!";
$key = preg_replace($regex,'',$_SERVER['REQUEST_URI']);
$method = $_SERVER['REQUEST_METHOD'];
$value = file_get_contents('php://input');
}
$kvqlite = new KVQLite(DATABASE);
switch ($method) {
case 'GET': case 'get':
$result = $kvqlite->get($key);
break;
case 'POST': case 'post':
$result = $kvqlite->post($key,$value);
break;
default:
die("$method not yet supported.");
}
if(!isset($result)) {
header('HTTP/1.0 404 Not Found');
echo "Not found.\n";
} else {
header('HTTP/1.1 200 OK');
header('Content-Type: application/json');
echo $result;
echo "\n";
}
die;
// Utility Class
class KVQLite {
public $_pdo;
public $_table = 'kvqlite';
function __construct($database) {
$this->_pdo = new PDO("sqlite:$database");
$this->_ensureTableExists();
}
function _ensureTableExists() {
$createTable =
"CREATE TABLE IF NOT EXISTS \"$this->_table\" (
\"key\" VARCHAR PRIMARY KEY NOT NULL UNIQUE,
\"value\" TEXT
)";
$this->_pdo->query($createTable);
}
function get($key) {
$key = preg_replace('/\*/','%',$key);
$query = "SELECT key, value FROM \"$this->_table\" WHERE key LIKE :key";
$statement = $this->_pdo->prepare($query);
$statement->execute(array(':key'=>$key));
$results = $statement->fetchAll(PDO::FETCH_CLASS,get_class((object)array()));
$count = count($results);
if($count === 1) {
return $results[0]->value;
} else if($count > 1) {
$keyed = array();
foreach($results as $result) {
$keyed[$result->key] = json_decode($result->value);
}
return json_encode($keyed);
}
return null;
}
function post($key, $value) {
$query = "INSERT OR REPLACE INTO \"$this->_table\" (key,value) VALUES (:key,:value);";
$statement = $this->_pdo->prepare($query);
$statement->execute(array(':key'=>$key,':value'=>$value));
return $this->get($key);
}
}