exec('create table if not exists keys(k varchar(80) primary key, v varchar(140), created integer, updated integer, origin varchar(16))'); } catch(PDOException $e) { header("Content-Type: text/plain"); echo 'Exception : '.$e->getMessage(); exit(0); } if (isset($_GET['k'])) { header("Content-Type: text/plain"); $keys = array_map(array($db, 'quote'), explode(',', $_GET['k'])); $result = $db->query('select v from keys where k in ('.implode(',', $keys).') order by updated desc limit 1', PDO::FETCH_ASSOC); foreach($result as $row) { echo $row['v']; } exit(0); } elseif (isset($_POST['auth'])) { if ($_POST['auth'] != $authorization) { echo "Not authorized.\n"; exit(0); } $stmt = $db->prepare('select k from keys where k=?'); foreach ($_POST as $key => $value) { if ($key == 'auth') { continue; } $stmt->bindParam(1, $key, PDO::PARAM_STR); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); if(!$row) { $db->exec('insert into keys (k, v, created, updated, origin) values ('.$db->quote($key).', '.$db->quote($value). ', '.$_SERVER['REQUEST_TIME'].', '.$_SERVER['REQUEST_TIME'].', '.$db->quote($_SERVER['REMOTE_ADDR']).');'); } else { $db->exec('update keys set v = '.$db->quote($value).', updated = '.$_SERVER['REQUEST_TIME'].', origin = '.$db->quote($_SERVER['REMOTE_ADDR']). ' where k = '.$db->quote($key).';'); } if ($db->errorCode() != '00000') { echo "Error:", $db->errorInfo()[2], "\n"; } else { echo 'OK'; } } exit(0); } header("Content-Type: text/plain"); $headers = array("key ","value ","created ","updated ","origin "); echo implode("\t", $headers)."\n"; $headers = array("-----------","------------","------------","------------","------------"); echo implode("\t", $headers)."\n"; $result = $db->query('select * from keys', PDO::FETCH_ASSOC); foreach($result as $row) { // print_r( $row ); $row['k'] = str_pad($row['k'], 12); $row['v'] = str_pad($row['v'], 12); $row['created'] = date('Y-m-d', $row['created']); echo implode("\t", $row)."\n"; } ?>