first commit
David Blume

David Blume commited on 2016-09-10 20:25:26
Showing 5 changed files, with 138 additions and 0 deletions.

... ...
@@ -0,0 +1,9 @@
1
+RewriteEngine On
2
+RewriteCond %{HTTPS} off
3
+RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
4
+DirectoryIndex index.php index.html
5
+<FilesMatch "(\.db|config\.php)$">
6
+Order allow,deny
7
+Deny from all
8
+</FilesMatch>
9
+AddCharset UTF-8 .txt
... ...
@@ -0,0 +1,21 @@
1
+The MIT License (MIT)
2
+
3
+Copyright (c) 2016 David Blume
4
+
5
+Permission is hereby granted, free of charge, to any person obtaining a copy 
6
+of this software and associated documentation files (the "Software"), to deal 
7
+in the Software without restriction, including without limitation the rights
8
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
9
+copies of the Software, and to permit persons to whom the Software is 
10
+furnished to do so, subject to the following conditions:
11
+
12
+The above copyright notice and this permission notice shall be included in all 
13
+copies or substantial portions of the Software.
14
+
15
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
16
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
17
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
18
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
19
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+SOFTWARE.
... ...
@@ -0,0 +1,44 @@
1
+# Key Value Store
2
+
3
+kvs is a rudimentary key/value store written in PHP.
4
+
5
+# Getting the project
6
+
7
+You can get a copy of this project by clicking on the
8
+[ZIP](http://git.dlma.com/kvs-php.git/zipball/master)
9
+or [TAR](http://git.dlma.com/kvs-php.git/tarball/master) buttons
10
+near the top right of the GitList web page.
11
+
12
+If you're me, and you want to contribute to the repo, then you can clone it like so:
13
+
14
+    git clone ssh://USERNAME@dlma.com/~/git/kvs-php.git
15
+
16
+# Building it
17
+
18
+1. Ensure your server's version of PHP supports PDO and SQLite.
19
+2. Move config.php.sample to config.php, and replace `replaceme` with a passcode you choose.
20
+3. Optional: Use secure HTTP. I recommend getting a free SSL certificate from [Let's Encrypt](https://letsencrypt.org/).
21
+
22
+# Using it
23
+
24
+Here is [a live instance that serves an index page](https://kvs-php.dlma.com/).  You can send a key to get a value like so:
25
+
26
+    https://kvs-php.dlma.com/?k=1Gxxxxxxxxxx
27
+
28
+Special use case, here's how to get the value for the most-recently updated key of a list of keys. The list could contain any number of keys, but only one value will be returned. 
29
+
30
+    https://kvs-php.dlma.com/?k=1Gxxxxxxxxxx,1Gxxxxxxxxxz
31
+
32
+Here's the recipe for a cURL command to store a new value for a key: 
33
+
34
+    curl --data "key=value&auth={authorization}" https://{url}
35
+
36
+Here's [the source code for a Roku channel client of such a service](http://git.dlma.com/roku_ip_tagger.git/). 
37
+
38
+# Is it any good?
39
+
40
+[Yes](https://news.ycombinator.com/item?id=3067434).
41
+
42
+# Licence
43
+
44
+This software uses the [MIT license](http://git.dlma.com/kvs-php.git/blob/master/LICENSE.txt).
... ...
@@ -0,0 +1,3 @@
1
+<?php 
2
+$authorization = "replaceme"; 
3
+?>
... ...
@@ -0,0 +1,61 @@
1
+<?php
2
+require('config.php');
3
+try {
4
+    $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
+} catch(PDOException $e) {
7
+    header("Content-Type: text/plain");
8
+    echo 'Exception : '.$e->getMessage();
9
+    exit(0);
10
+}
11
+
12
+if (isset($_GET['k'])) {
13
+    header("Content-Type: text/plain");
14
+    $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
+    foreach($result as $row) {
17
+	echo $row['v'];
18
+    }
19
+    exit(0);
20
+} elseif (isset($_POST['auth'])) {
21
+    if ($_POST['auth'] != $authorization) {
22
+	echo "Not authorized.\n";
23
+        exit(0);
24
+    }
25
+    $stmt = $db->prepare('select k from keys where k=?');
26
+    foreach ($_POST as $key => $value) {
27
+	if ($key == 'auth') {
28
+	    continue;
29
+	}
30
+        $stmt->bindParam(1, $key, PDO::PARAM_STR);
31
+	$stmt->execute();
32
+	$row = $stmt->fetch(PDO::FETCH_ASSOC);
33
+	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']).');');
36
+	} 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).';');
39
+	}
40
+	if ($db->errorCode() != '00000') {
41
+	    echo "Error:", $db->errorInfo()[2], "\n";
42
+	} else {
43
+	    echo 'OK';
44
+	}
45
+    }
46
+    exit(0);
47
+}
48
+header("Content-Type: text/plain");
49
+$headers = array("key        ","value       ","created     ","updated     ","origin      ");
50
+echo implode("\t", $headers)."\n";
51
+$headers = array("-----------","------------","------------","------------","------------");
52
+echo implode("\t", $headers)."\n";
53
+$result = $db->query('select * from keys', PDO::FETCH_ASSOC);
54
+foreach($result as $row) {
55
+    // print_r( $row );
56
+    $row['k'] = str_pad($row['k'], 12);
57
+    $row['v'] = str_pad($row['v'], 12);
58
+    $row['created'] = date('Y-m-d', $row['created']);
59
+    echo implode("\t", $row)."\n";
60
+}
61
+?>
0 62