Created
April 23, 2010 07:59
-
-
Save erikeldridge/376323 to your computer and use it in GitHub Desktop.
a restful-ish web api for sqlitestore ( http://gist.github.com/373491 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// a web api for sqlitestore ( http://gist.github.com/373491 ) | |
// usage: GET retrieves data, POST inserts. Value to insert is validated as json before writing to store. | |
// example: http://github.com/erikeldridge/openid-oauth-yql-yui-party/blob/master/start.xml | |
// license: http://gist.github.com/375593 | |
require 'SqliteStore.php'; | |
$filters = array( | |
'key' => FILTER_SANITIZESTRING, | |
'val' => FILTER_SANITIZESTRING, | |
'hash' => FILTER_SANITIZESTRING | |
); | |
$input = filter_var_array( $_REQUEST, $filters ); | |
header('Content-type: text/javascript'); | |
//minor security | |
$hash = md5( 'secret'.$input['key'] ); | |
if ( $hash != $input['hash'] ) { | |
die('{"error":"invalid hash: '.$input['hash'].'"}'); | |
} | |
$store = new SqliteStore('kv'); | |
//rest handling | |
switch ( $_SERVER['REQUEST_METHOD'] ) { | |
case 'GET': | |
$val = $store->get( $input['key'] ); | |
if ( $val ) { | |
header("HTTP/1.0 200 OK"); | |
echo $val; | |
} else { | |
header("HTTP/1.0 404 Not Found"); | |
} | |
break; | |
case 'POST': | |
//validate, normalize json | |
$obj = json_decode( stripslashes( urldecode( $input['val'] ) ) ); | |
if ( !$obj ) { | |
header("HTTP/1.0 400 Bad Request: invalid json"); | |
die; | |
} | |
$store->set( $input['key'], json_encode( $obj ) ); | |
header("HTTP/1.0 200"); | |
echo $store->get( $input['key'] ); | |
break; | |
default: | |
header("HTTP/1.0 405 Method Not Allowed"); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment