David Blume commited on 2016-09-10 21:10:59
Showing 1 changed files, with 14 additions and 9 deletions.
... | ... |
@@ -1,18 +1,20 @@ |
1 | 1 |
<?php |
2 | 2 |
require('config.php'); |
3 |
+header("Content-Type: text/plain"); |
|
3 | 4 |
try { |
4 | 5 |
$db = new PDO('sqlite:store.db'); |
5 |
- $db->exec('create table if not exists keys(k varchar(80) primary key, v varchar(140), created integer, updated integer, origin varchar(16))'); |
|
6 |
+ $db->exec('create table if not exists keys(k varchar(80) primary key, |
|
7 |
+ v varchar(140), created integer, updated integer, origin varchar(16));'); |
|
6 | 8 |
} catch(PDOException $e) { |
7 |
- header("Content-Type: text/plain"); |
|
8 | 9 |
echo 'Exception : '.$e->getMessage(); |
9 | 10 |
exit(0); |
10 | 11 |
} |
11 | 12 |
|
12 | 13 |
if (isset($_GET['k'])) { |
13 |
- header("Content-Type: text/plain"); |
|
14 |
+ // This is a request to get one specific value. |
|
14 | 15 |
$keys = array_map(array($db, 'quote'), explode(',', $_GET['k'])); |
15 |
- $result = $db->query('select v from keys where k in ('.implode(',', $keys).') order by updated desc limit 1', PDO::FETCH_ASSOC); |
|
16 |
+ $result = $db->query('select v from keys where k in ('.implode(',', $keys).') |
|
17 |
+ order by updated desc limit 1;', PDO::FETCH_ASSOC); |
|
16 | 18 |
foreach($result as $row) { |
17 | 19 |
echo $row['v']; |
18 | 20 |
} |
... | ... |
@@ -22,6 +24,8 @@ if (isset($_GET['k'])) { |
22 | 24 |
echo "Not authorized.\n"; |
23 | 25 |
exit(0); |
24 | 26 |
} |
27 |
+ |
|
28 |
+ // We have an authorized request to add or update a key/value pair. |
|
25 | 29 |
$stmt = $db->prepare('select k from keys where k=?'); |
26 | 30 |
foreach ($_POST as $key => $value) { |
27 | 31 |
if ($key == 'auth') { |
... | ... |
@@ -31,11 +35,13 @@ if (isset($_GET['k'])) { |
31 | 35 |
$stmt->execute(); |
32 | 36 |
$row = $stmt->fetch(PDO::FETCH_ASSOC); |
33 | 37 |
if(!$row) { |
34 |
- $db->exec('insert into keys (k, v, created, updated, origin) values ('.$db->quote($key).', '.$db->quote($value). |
|
35 |
- ', '.$_SERVER['REQUEST_TIME'].', '.$_SERVER['REQUEST_TIME'].', '.$db->quote($_SERVER['REMOTE_ADDR']).');'); |
|
38 |
+ $db->exec('insert into keys (k, v, created, updated, origin) values ('. |
|
39 |
+ $db->quote($key).', '.$db->quote($value).', '. |
|
40 |
+ $_SERVER['REQUEST_TIME'].', '.$_SERVER['REQUEST_TIME'].', '. |
|
41 |
+ $db->quote($_SERVER['REMOTE_ADDR']).');'); |
|
36 | 42 |
} else { |
37 |
- $db->exec('update keys set v = '.$db->quote($value).', updated = '.$_SERVER['REQUEST_TIME'].', origin = '.$db->quote($_SERVER['REMOTE_ADDR']). |
|
38 |
- ' where k = '.$db->quote($key).';'); |
|
43 |
+ $db->exec('update keys set v = '.$db->quote($value).', updated = '.$_SERVER['REQUEST_TIME']. |
|
44 |
+ ', origin = '.$db->quote($_SERVER['REMOTE_ADDR']).' where k = '.$db->quote($key).';'); |
|
39 | 45 |
} |
40 | 46 |
if ($db->errorCode() != '00000') { |
41 | 47 |
echo "Error:", $db->errorInfo()[2], "\n"; |
... | ... |
@@ -45,7 +51,6 @@ if (isset($_GET['k'])) { |
45 | 51 |
} |
46 | 52 |
exit(0); |
47 | 53 |
} |
48 |
-header("Content-Type: text/plain"); |
|
49 | 54 |
$headers = array("key ","value ","created ","updated ","origin "); |
50 | 55 |
echo implode("\t", $headers)."\n"; |
51 | 56 |
$headers = array("-----------","------------","------------","------------","------------"); |
52 | 57 |