db = new db('sqlite:' . dirname(__FILE__) . '/oho.db');
//$this->db = new db('mysql:host=localhost;dbname=oho');
$this->attach_person();
echo "{$this->params['me']}
";
$result = $this->process($command, $raw);
if($result != null) $this->msgq[$this->params['me']][] = $result;
}
private function paramprocess($a)
{
return ($a{0}=="$") ? $this->params[substr($a,1)] : $a;
}
private function process($cmd, $raw = null, $enforcecontext = true)
{
if(is_string($cmd)) {
while(preg_match('/\\{([^{]*?)\\}/', $cmd, $match)) {
$cmd = str_replace($match[0], $this->process($match[1]), $cmd);
}
preg_match_all('/".*?"|\\S+/', $cmd, $matches);
$coms = $matches[0];
$coms = array_map(create_function('$a', 'return trim($a, \'"\');'), $coms);
}
if(is_array($cmd)) {
$coms = $cmd;
}
if($raw != null) array_push($coms, $raw);
$coms = array_map(array(&$this, 'paramprocess'), $coms);
if($coms[0]{0} == '@') {
$com = 'cmd_' . substr(array_shift($coms) , 1);
if(method_exists($this, $com)) {
$output = $this->$com($coms);
}
else {
$output = '?';
}
//if(!$this->quiet) $this->msgq[$this->params['me']][] = $output;
return $output;
}
else {
$this->quiet = true;
$result = $this->do_action($coms, $enforcecontext);
return null;
}
}
private function attach_person()
{
$this->person = null;
if(isset($_COOKIE['person'])) {
$personid = $this->getthingbyhash($_COOKIE['person']);
if($personid) {
$this->person = $personid;
}
}
if($this->person == null) {
$newhash = md5(microtime() . $_SERVER['REMOTE_ADDR']);
$personid = $this->cmd_create(array('person'));
$this->cmd_set(array($personid, 'hash', $newhash));
$this->person = $personid;
setcookie('person', $newhash);
}
$this->params['me'] = $this->person;
$this->params['location'] = $this->cmd_get(array($this->person, 'location'));
return $this->person;
}
private function getthingbyhash($hash)
{
$thing = $this->db->get_row("SELECT * FROM Properties WHERE prop = 'hash' AND value = ?", array($hash));
if($thing) return $thing->id;
return false;
}
public function output()
{
$response = array_merge(
(array)$this->msgq['global'],
(array)$this->msgq[$this->params['location']],
(array)$this->msgq[$this->params['me']]
);
return implode('
', $response);
}
private function cmd_put($coms)
{
$thingid = $this->get_thing($coms[0]);
$destid = $this->get_thing($coms[1]);
$this->cmd_set(array($thingid, 'location', $destid));
return $thingid;
}
private function cmd_at($coms)
{
$thingid = $this->get_thing($coms[0]);
return $this->cmd_get(array($thingid, 'location'));
}
private function cmd_tel($coms)
{
$thingid = $this->get_thing($coms[0]);
return $this->cmd_set(array($this->params['me'], 'location', $thingid));
}
private function cmd_create($coms)
{
$objectname = $coms[0];
$this->db->query("INSERT INTO Things (name) VALUES ('{$objectname}');");
return $this->db->insertID();
return "Created {$objectname} as object #" . $this->db->insertID() . ".";
}
private function cmd_emit($coms)
{
$output = implode(' ', $coms);
$this->msgq[$this->params['location']][] = $output;
return null;
}
private function cmd_whisper($coms)
{
$output = implode(' ', $coms);
$this->msgq[$this->params['me']][] = $output;
return null;
}
private function cmd_set($coms)
{
$thingid = $this->get_thing($coms[0]);
$prop = $coms[1];
switch(count($coms)) {
case 2:
$m = 'action' . md5($prop . microtime());
$n = 'name' . md5($prop . microtime());
$f = 'form' . md5($prop . microtime());
$row = $this->db->get_row("SELECT * FROM Properties WHERE id = ? AND prop = ?", array($thingid, $prop));
$output = '
';
return $output;
case 3:
$value = $coms[2];
if($this->db->get_row("SELECT * FROM Properties WHERE id = ? AND prop = ?", array($thingid, $prop))) {
$this->db->query("UPDATE Properties SET value = ? WHERE id = ? AND prop = ?", array($value, $thingid, $prop));
return "Ok, zset. {$thingid} {$prop} {$value}";
}
else {
$r = $this->db->query("INSERT INTO Properties (id, prop, value) VALUES (?, ?, ?)", array($thingid, $prop, $value));
return "Ok, set. {$thingid} {$prop} {$value} {$r}";
}
default:
return "Sorry, that didn't work.\n".print_r($coms,1);
}
return $value;
}
private function cmd_get($coms)
{
$thingid = $this->get_thing($coms[0]);
$prop = $coms[1];
$result = $this->db->get_row("SELECT * FROM Properties WHERE id = ? AND prop = ?", array($thingid, $prop));
if($result) {
return $result->value;
}
else {
return '';
}
}
private function cmd_delete($coms)
{
foreach($coms as $com) {
$thingid = $this->get_thing($com);
$this->db->query("DELETE FROM Things WHERE id = ?", array($thingid));
$this->db->query("DELETE FROM Properties WHERE id = ?", array($thingid));
$this->db->query("DELETE FROM Actions WHERE id = ?", array($thingid));
$this->db->query("DELETE FROM Inherit WHERE childid = ? OR parentid = ?", array($thingid, $thingid));
}
return 'Deleted #' . $thingid;
}
private function cmd_iam($coms)
{
$thingid = $this->get_thing($coms[0]);
$meid = $this->get_thing($this->params['me']);
owd($meid);
echo $this->cmd_set(array($thingid, 'hash', $_COOKIE['person']));
//$this->cmd_delete(array($thingid));
}
private function cmd_rename($coms)
{
$thingid = $this->get_thing($coms[0]);
$newname = $coms[1];
$this->db->query("UPDATE Things SET name = ? WHERE id = ?", array($newname, $thingid));
return $thingid;
}
private function cmd_describe($coms)
{
$thingid = $this->get_thing($coms[0]);
$thing = $this->db->get_row("SELECT * FROM Things WHERE id = ?", array($thingid));
$output = "{$thing->name} (#{$thing->id})
";
$props = $this->db->get_results("SELECT * FROM Properties WHERE id = ?", array($thingid));
$output .= 'Properties';
foreach((array)$props as $prop) {
$output .= "- {$prop->prop}
- " . str_replace("\n", '
', $prop->value) . " \n";
}
$output .= '
';
$actions = $this->db->get_results("SELECT * FROM Actions WHERE id = ?", array($thingid));
$output .= 'Actions';
foreach((array)$actions as $action) {
$output .= "- {$action->action}
- " . str_replace("\n", '
', $action->value) . " \n";
}
$output .= '
';
$inherits = $this->db->get_results("SELECT * FROM Inherit WHERE childid = ?", array($thingid));
$output .= 'Child Of';
foreach((array)$inherits as $inherit) {
$output .= "- {$inherit->parentid}
\n";
}
$output .= '
';
$inherits = $this->db->get_results("SELECT * FROM Inherit WHERE parentid = ?", array($thingid));
$output .= 'Parent Of';
foreach((array)$inherits as $inherit) {
$output .= "- {$inherit->childid}
\n";
}
$output .= '
';
return $output;
}
private function get_thing($desc)
{
if(is_numeric($desc)) return intval($desc);
$row = $this->db->get_row("SELECT * FROM Things WHERE name LIKE ?", array($desc));
return intval($row->id);
}
private function cmd_id($coms)
{
return '#' . $this->get_thing($coms[0]);
}
private function cmd_listall($coms)
{
$things = $this->db->get_results("SELECT * FROM Things ORDER BY name ASC, id ASC");
$output = '';
foreach($things as $thing) {
$output .= "- #{$thing->id} - {$thing->name}
";
}
$output .= '
';
return $output;
}
private function cmd_match($coms)
{
$c1 = array_shift($coms);
$c2 = array_shift($coms);
if($c1 == $c2) {
return $this->process($coms);
}
return '';
}
private function cmd_deaction($coms)
{
$thingid = $this->get_thing($coms[0]);
$action = $coms[1];
$this->db->query("DELETE FROM Actions WHERE id = ? AND action = ?", array($thingid, $action));
return $thingid;
}
private function cmd_action($coms)
{
$thingid = $this->get_thing($coms[0]);
$action = $coms[1];
//cho "" . print_r($coms,1) . "
";
$row = $this->db->get_row("SELECT * FROM Actions WHERE id = ? AND action = ?", array($thingid, $action));
switch(count($coms)) {
case 3:
$value = $coms[2];
$parts = count(explode(' ', $action));
if($row) {
$this->db->query("UPDATE Actions SET value = ?, part = ? WHERE id = ? and action = ?", array($value, $parts, $thingid, $action));
}
else {
$this->db->query("INSERT INTO Actions (value, part, id, action) VALUES (?, ?, ?, ?)", array($value, $parts, $thingid, $action));
}
//echo "" . print_r($row,1) . "
";
//echo "" . print_r($this->db->errors,1) . "
";
return "Action \"{$action}\" set on #{$thingid}.";
case 2:
$m = 'action' . md5($action . microtime());
$n = 'name' . md5($action . microtime());
$f = 'form' . md5($action . microtime());
$output = '';
return $output;
case 1:
$actions = $this->db->get_results("SELECT * FROM Actions WHERE id = ?", array($thingid));
$output = '';
foreach($actions as $action) {
$output .= "- {$action->action}
";
}
$output .= '
';
return $output;
}
}
private function cmd_inherit($coms)
{
$thingid = $this->get_thing($coms[0]);
$parentid = $this->get_thing($coms[1]);
$this->db->query("INSERT INTO Inherit (childid, parentid) VALUES (?, ?);", array($thingid, $parentid));
return 'Ok.';
}
private function inherits_from($child, $parent)
{
if(isset($this->ancestors[$child][$parent])) return $this->ancestors[$child][$parent];
$result = (false != $this->db->get_row("SELECT * FROM Inherit WHERE parentid = ? AND childid = ?", array($parent, $child)));
$this->ancestors[$child][$parent] = $result;
return $result;
}
private function do_action($coms, $enforcecontext = true)
{
if(false && $enforcecontext) {
if(isset($this->params['location'])) $locations[] = $this->params['location'];
if(isset($this->params['me'])) $locations[] = $this->params['me'];
$location = implode(',', $locations);
$qry = "SELECT actions.*, properties.* FROM actions, properties WHERE (part = " . count($coms) . ") AND (prop = 'location') AND (properties.value IN ({$this->params['location']},{$this->params['me']}))";
$qry = "
SELECT
actions.*,
properties.*
FROM
actions,
properties
WHERE
(Actions.id = Properties.id) AND
(part = " . count($coms) . ") AND
(prop = 'location') AND
(properties.value IN ({$location}))";
}
else {
$qry = "SELECT * FROM Actions WHERE part = " . count($coms);
}
$actions = $this->db->get_results($qry);
owd($locations,$qry, $actions);
$fail = true;
foreach((array)$actions as $action) {
preg_match_all('/".*?"|\\S+/', $action->action, $matches);
$acts = $matches[0];
$params = array();
$fail = false;
for($z = 0; $z < count($coms); $z++) {
$act = $acts[$z];
$com = $coms[$z];
switch(true) {
case $act == '$$':
$thingid = $this->get_thing($com);
if($thingid != $action->id && !$this->inherits_from($thingid, $action->id)) {
$fail = true;
break 2;
}
$params['$'] = $thingid;
break;
case $act{0} == '$':
$params[substr($act,1)] = $com;
break;
case $act == $com:
break;
case $act != $com:
$fail = true;
break 2;
}
}
if(!$fail) {
$setaction = $action;
break;
}
}
if(!$fail) {
if(!isset($this->params['$'])) $this->params['$'] = $action->id;
$this->params = array_merge($this->params, $params);
$actions = explode("\n", $setaction->value);
$output = '';
foreach($actions as $action) {
$output .= $this->process($action, null, false);
}
}
return $output;
}
}
?>