48b0c4229bb8ddf7b9aac21f2abdaa010ae619b6
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 Use one 'INSERT OR REPLACE'...

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) 
David Blume Use one 'INSERT OR REPLACE'...

David Blume authored 8 years ago

28)     // We have an authorized request to insert or replace a key/value pair.
29)     $stmt = $db->prepare(
30)         'INSERT OR REPLACE INTO keys
31)          VALUES (:k, :v,
32)                  COALESCE((SELECT created FROM keys WHERE k = :k), :rtime),
33)                  :rtime, :raddr);');
34)     $stmt->bindParam(':rtime', $_SERVER['REQUEST_TIME'], PDO::PARAM_INT);
35)     $stmt->bindParam(':raddr', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
36) 
37)     foreach($_POST as $key => $value) {
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

38)         if ($key == 'auth') {
39)             continue;
40)         }
David Blume Use one 'INSERT OR REPLACE'...

David Blume authored 8 years ago

41)         $stmt->bindParam(':k', $key, PDO::PARAM_STR);
42)         $stmt->bindParam(':v', $value, PDO::PARAM_STR);
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

43)         $stmt->execute();
44)         if ($db->errorCode() != '00000') {
45)             echo "Error:", $db->errorInfo()[2], "\n";
46)         } else {
David Blume Use one 'INSERT OR REPLACE'...

David Blume authored 8 years ago

47)             echo "OK\n";
David Blume Replace tabs with spaces fo...

David Blume authored 8 years ago

48)         }
David Blume first commit

David Blume authored 8 years ago

49)     }
50)     exit(0);
51) }
David Blume Use one 'INSERT OR REPLACE'...

David Blume authored 8 years ago

52) $headers = array("key          ","value         ","created     ","updated     ","origin      ");
David Blume first commit

David Blume authored 8 years ago

53) echo implode("\t", $headers)."\n";
David Blume Use one 'INSERT OR REPLACE'...

David Blume authored 8 years ago

54) $headers = array("-------------","--------------","------------","------------","------------");
David Blume first commit

David Blume authored 8 years ago

55) echo implode("\t", $headers)."\n";
David Blume Use same order as the Pytho...

David Blume authored 8 years ago

56) $result = $db->query('SELECT * FROM keys ORDER BY updated DESC', PDO::FETCH_ASSOC);