Created
February 7, 2009 18:03
-
-
Save riaf/59952 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* .capa engine | |
* @author riaf | |
*/ | |
$_SECRET = md5('secret'); | |
try { | |
$pdo = new PDO('mysql:host=localhost; dbname=test', 'user', 'pass'); | |
} catch (PDOException $e) { | |
debug($e->getMessage()); | |
exit; | |
} | |
main(); | |
/** | |
* main | |
*/ | |
function main(){ | |
global $_SECRET; | |
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'post') == 0){ | |
debug('method: post'); | |
if(isset($_POST['secret']) && $_POST['secret'] === $_SECRET){ | |
debug('call post'); | |
post(); | |
} | |
} else { | |
if(isset($_GET['secret']) && $_GET['secret'] === $_SECRET){ | |
debug('call get'); | |
get(); | |
} | |
} | |
debug('finished'); | |
exit; | |
} | |
/** | |
* post | |
*/ | |
function post(){ | |
global $pdo; | |
$key = isset($_POST['key'])? $_POST['key']: null; | |
$value = isset($_POST['value'])? $_POST['value']: null; | |
$domain = isset($_POST['domain'])? $_POST['domain']: '__uri'; | |
if(!is_null($key) && !is_null($value) && !is_null($domain)){ | |
$select = $pdo->prepare('select * from capa where id = ? and domain = ?'); | |
$select->execute(array($key, $domain)); | |
$row = $select->fetch(PDO::FETCH_ASSOC); | |
if($row){ | |
if(preg_match('/^\+\+([0-9]+)$/', $value, $match)){ | |
$value = $row['value'] + $match[1]; | |
} | |
$update = $pdo->prepare('update capa set value = ? where id = ? and domain = ?'); | |
try { | |
$update->execute(array($value, $key, $domain)); | |
} catch (PDOException $e){ | |
debug($e->getMessage()); | |
} | |
} else { | |
if(preg_match('/^\+\+([0-9]+)$/', $value, $match)){ | |
$value = $match[1]; | |
} | |
$insert = $pdo->prepare('insert into capa(id, value, domain) values(:id, :value, :domain)'); | |
$insert->bindValue(':id', $key); | |
$insert->bindValue(':value', $value); | |
$insert->bindValue(':domain', $domain); | |
try { | |
$insert->execute(); | |
} catch (PDOException $e){ | |
debug($e->getMessage()); | |
} | |
} | |
} | |
} | |
/** | |
* get | |
* @todo json 以外もやる | |
*/ | |
function get(){ | |
global $pdo; | |
$key = isset($_GET['key'])? $_GET['key']: null; | |
$domain = isset($_GET['domain'])? $_GET['domain']: '__uri'; | |
if(is_null($key)){ | |
$select = $pdo->prepare('select * from capa where domain = ?'); | |
$select->execute(array($domain)); | |
$ret = array(); | |
while($row = $select->fetch(PDO::FETCH_ASSOC)){ | |
$ret[] = $row; | |
} | |
} else { | |
$select = $pdo->prepare('select * from capa where id = ? and domain = ?'); | |
$select->execute(array($key, $domain)); | |
$ret = $select->fetch(PDO::FETCH_ASSOC); | |
} | |
echo json_encode($ret); | |
} | |
/** | |
* create table | |
* only mysql. | |
*/ | |
function install(){ | |
global $pdo; | |
debug('start install...'); | |
$scheme = 'CREATE TABLE capa ('."\n"; | |
$scheme .= ' id blob not null,'."\n"; | |
$scheme .= ' value blob,'."\n"; | |
$scheme .= ' domain blob not null,'."\n"; | |
$scheme .= ' index (id(20), domain(20))'."\n"; | |
$scheme .= ') Engine=InnoDB;'; | |
debug($scheme); | |
try { | |
$pdo->exec($scheme); | |
} catch (PDOException $e){ | |
debug($e->getMessage()); | |
} | |
} | |
/** | |
* debug | |
*/ | |
function debug($msg){ | |
// echo $msg, "\n"; | |
return; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment