98f66101f18e09e4aba224dc64ac58cad89a562b
David Blume first commit

David Blume authored 8 years ago

1) <?php
2) require('config.php');
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

3) header("Content-Type: text/plain");
David Blume first commit

David Blume authored 8 years ago

4) try {
5)     $db = new PDO('sqlite:store.db');
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

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));');
David Blume first commit

David Blume authored 8 years ago

8) } catch(PDOException $e) {
9)     echo 'Exception : '.$e->getMessage();
10)     exit(0);
11) }
12) 
13) if (isset($_GET['k'])) {
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

14)     // This is a request to get one specific value.
David Blume first commit

David Blume authored 8 years ago

15)     $keys = array_map(array($db, 'quote'), explode(',', $_GET['k']));
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

16)     $result = $db->query('select v from keys where k in ('.implode(',', $keys).') 
17)                           order by updated desc limit 1;', PDO::FETCH_ASSOC);
David Blume first commit

David Blume authored 8 years ago

18)     foreach($result as $row) {
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

19)         echo $row['v'];
David Blume first commit

David Blume authored 8 years ago

20)     }
21)     exit(0);
22) } elseif (isset($_POST['auth'])) {
23)     if ($_POST['auth'] != $authorization) {
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

24)         echo "Not authorized.\n";
David Blume first commit

David Blume authored 8 years ago

25)         exit(0);
26)     }
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

27) 
28)     // We have an authorized request to add or update a key/value pair.
David Blume first commit

David Blume authored 8 years ago

29)     $stmt = $db->prepare('select k from keys where k=?');
30)     foreach ($_POST as $key => $value) {
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

31)         if ($key == 'auth') {
32)             continue;
33)         }
David Blume first commit

David Blume authored 8 years ago

34)         $stmt->bindParam(1, $key, PDO::PARAM_STR);
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

35)         $stmt->execute();
36)         $row = $stmt->fetch(PDO::FETCH_ASSOC);
37)         if(!$row) {
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']).');');
42)         } else {
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).';');
45)         }
46)         if ($db->errorCode() != '00000') {
47)             echo "Error:", $db->errorInfo()[2], "\n";
48)         } else {
49)             echo 'OK';
50)         }