array("mgd_list_sitegroups"),
"hosts" => array("mgd_list_hosts") ,
"host" => array("mgd_get_page") ,
"page" => array("mgd_list_pages","mgd_list_page_elements"),
"style" => array("mgd_list_styles","mgd_list_elements"),
"topic" => array("mgd_list_topics","mgd_list_topic_articles"),
"snippetdir" => array("mgd_list_snippetdirs","mgd_list_snippets"),
"person" => array("mgd_list_persons"),
"grp" => array("mgd_list_groups","mgd_list_persons"),
"group" => array("mgd_list_groups","mgd_list_members"),
"sitegroup" => array("mgd_list_sitegroups")
);
// generic tree
// for demo purposes - show hosts! & pages!
function rdf_tree_types($id) {
$types = array( "group","hosts","page","style","topic","snippetdir");
$ret = "";
foreach ($types as $k)
$ret .= "\r\n".
" <__table__>sg__$k\r\n".
" $id\r\n".
" $k\r\n".
"\r\n";
return $ret;
}
function output_object(&$obj,$is_single=0) {
if (!$obj) {
return "NO OBJECT";
}
$ret = "\r\n<{$obj->__table__}>\r\n";
$list = get_object_vars($obj);
while (list ($k,$v) = each($list)) {
if ($k == "__res__") continue;
if (ereg("[^0-9A-z_]",$v)) {
$ret .="<$k><"."!"."["."CDATA"."[$v]"."]".">$k>\r\n";
} else {
$ret .= "<$k>".$obj->$k ."$k>\r\n";
} // end if else
} /// end loop
if (method_exists($obj,"guid"))
$ret .= "".$obj->guid()."\r\n";
if (isset($obj->sitegroup)) {
if ($obj->sitegroup) {
$sgobj = mgd_get_sitegroup($obj->sitegroup);
$ret .= "".$sgobj->guid()."\r\n";
} else {
/* not very standard */
$ret .= "0\r\n";
}
}
if ($is_single && method_exists($obj,"listparameters"))
if ($p = $obj->listparameters())
while ($p->fetch())
if($pp=$obj->listparameters($p->domain))
while ($pp->fetch())
$ret .= "domain) ."\" ".
"name=\"". urlencode($pp->name) ."\">".
"<"."!"."["."CDATA"."[{$pp->value}]"."]".">".
"\r\n";
// membership for person!
if ($is_single && $obj->__table__ == "person")
if ($mm = mgd_list_memberships($obj->id))
while ($mm->fetch())
$ret .= "{$mm->gid}\r\n";
if ($is_single && $obj->__table__ == "grp")
if ($mm = mgd_list_members($obj->id))
while ($mm->fetch())
$ret .= "{$mm->uid}\r\n";
$ret .= "\r\n{$obj->__table__}>\r\n";
return $ret;
}
function rdf_get_object($type,$id) {
if ($type == "blobs") $type ="attachment";
$function = "mgd_get_" .$type ;
if (!function_exists($function)) return;
return $function($id);
}
function rdf_tree($id,$functions) {
//echo "RDF_TREE: $id , ". serialize($functions);
$ret ="";
foreach($functions as $function) {
if (!function_exists($function)) return;
if ($function == "mgd_get_page") {
$obj = mgd_get_host($id);
$id = $obj->root;
}
//echo "calling function $function $id
";
if (!$id && $function != "mgd_list_members") {
$list = $function();
} else {
$list = $function($id);
}
// if ($child[0]=="*" && $id) $id = $parentlist->root;
if ($function == "mgd_list_sitegroups") {
$obj = new stdClass;
$obj->id=0;
$obj->__table__="sitegroup";
$obj->name = "System Admin";
$obj->sitegroup=0;
$obj->realm="System Admin";
$ret .= $this->output_object($obj);
}
if ($list)
if ($function == "mgd_get_page") {
$ret .= $this->output_object($list);
} else {
while ($list->fetch()) {
if ($function == "mgd_list_members") {
$item = mgd_get_person($list->uid);
$ret .= $this->output_object($item);
} else {
$ret .= $this->output_object($list);
}
}
}
}
return $ret;
}
function rdf_attachments(&$object) {
if (!$object) return;
$ret = "";
if (!method_exists($object,"listattachments")) return;
if ($a = $object->listattachments())
while ($a->fetch()) {
$ret .= $this->output_object($a);
}
return $ret;
}
function rdf_filtered_tree($id,$functions,$all=0) {
$function= $functions[0];
$ret ="";
if (!function_exists($function)) return;
// echo "calling function $function $id
";
switch ($function) {
case "mgd_list_hosts":
case "mgd_list_sitegroups":
$list = $function();
break;
case "mgd_list_groups":
// get the sitegroup admin!
if ($id && !$all) {
$sg = mgd_get_sitegroup($id);
$obj = mgd_get_group($sg->admingroup);
$ret .= $this->output_object($obj);
return $ret;
} else if ( !$all) {
//$all=0;
$list = $function();
break;
}
default:
$list = $function(0);
break;
}
if (isset($list)) {
while ($list->fetch()) {
// nasty filtering hack :
//
if (($list->sitegroup != $id) && ($function == "mgd_list_groups"))
continue;
if (($list->sitegroup == $id)
|| ($all && !$list->sitegroup)
&& !$this->sitegroup) {
$ret .= $this->output_object($list);
if (isset($functions[1]) && $all && $function != "mgd_list_pages") {
$ret .= $this->rdf_tree($list->id,array($functions[0]));
}
} else if (
(($function == "mgd_list_sitegroups") || ($function == "mgd_list_groups")) &&
($list->id == $id)) {
$ret .= $this->output_object($list);
} else if ($this->sitegroup) {
if ($function == "mgd_list_styles" && $all)
$ret .= $this->output_object($list);
}
}
if (isset($functions[1]) && ($functions[1] == "mgd_list_members") && !$id) {
$this->rdf_tree($id,array("mgd_list_members"));
}
return $ret;
}
}
function update_membership($id,$array) {
/* needs to overlay current membership with new membership
eg. new array - 1,2,3,4,5
old member = 1,3,6
*/
$old_array = array();
if ($list = mgd_list_memberships($id))
while ($list->fetch()) {
$old_array[] = $list->gid;
$old_mappings[$list->gid] = $list->id;
}
/*
- how to do this:
add new membership - eg. stuff that is not in old
*/
if ($array)
foreach($array as $k)
if (!in_array($k,$old_array))
mgd_create_member($id,$k,"");
// remove deleted membership - eg. stuff that is in old but not in new
if (isset($old_array)) {
foreach ($old_array as $k) {
if (!in_array($k, $array)) {
mgd_delete_member($old_mappings[$k]);
}
}
}
}
function update_members($id,$array) { // group -> array of people
//echo "UPDATING MEMBERS $id: ". serialize($array);
$old_array = array();
if ($list = mgd_list_members($id))
while ($list->fetch()) {
$old_array[] = $list->uid;
$old_mappings[$list->uid] = $list->id;
}
if ($array)
foreach($array as $k)
if (!in_array($k,$old_array))
mgd_create_member($k,$id,"");
// remove deleted membership - eg. stuff that is in old but not in new
if ($old_array)
foreach ($old_array as $k)
if (!in_array($k, $array))
mgd_delete_member($old_mappings[$k]);
}
/* and now for the really complex one! - parameters!
this is a little more difficult than members because of the multi dimensional nature
build a list of current values $old_array == set to current value
go through new array
-- if it exists
-- update it if it is different
-- remove from old array
-- if not exists
-- create it.
-- go through old array deleteing values..
*/
function update_parameters(&$object, $array) {
echo "UPDATE PARAMETERS: ". serialize($array);
// build a list of current values $old_array == set to current value
if ($list = $object->listparameters())
while($list->fetch())
if ($list2 = $object->listparameters($list->domain))
while($list2->fetch())
$old_array[$list->domain][$list2->name] = $list2->value;
/* add and update */
if ($array)
foreach($array as $row) {
echo "\n".serialize($row);
if (!$row) continue; // deal with dummies;
if (!trim($row[0]) || !trim($row[1])) continue; // more dummies
if (isset($old_array[$row[0]][$row[1]])) {
if ($old_array[$row[0]][$row[1]] != $row[2])
$object->parameter($row[0],$row[1], $row[2]);
// now clear it.
unset($old_array[$row[0]][$row[1]]); // its the same, remove it.
if (!count($old_array[$row[0]]))
unset($old_array[$row[0]]); // remove whole thing!
} else {
if (!trim($row[2])) continue; // setting to blank on new entry!
echo "ADDING " . $row[0]."_".$row[1]." ". $row[2];
$object->parameter($row[0],$row[1], $row[2]);
}
}
// now delete items that are left
if ($old_array)
foreach($old_array as $domain=>$ar)
foreach ($ar as $name=>$value) {
echo "DELETING? $domain,$name\n";
$object->parameter($domain,$name,"");
}
}
function check_calender($new) {
if ($new["__table__"] != "article") return $new;
if (!$new["calender_active"]) {
echo "CALENDAR INACTIVE?";
$new["calstart"] = 0;
$new["caldays"] = 0;
if ($new["enddate"]) unset($new["enddate"]);
if ($new["startdate"]) unset($new["startdate"]);
return $new;
}
$a= explode(":",trim($new["startdate"]));
$new["calstart"] = sprintf("%02d",$a[0]).".". sprintf("%02d",$a[1]).".". $a[2];
$s = mktime(0,0,0,$a[1],$a[0],$a[2]);
echo $new["enddate"];
$a= explode(":",trim($new["enddate"]));
$e = mktime(0,0,0,$a[1],$a[0],$a[2]);
if ($e < $s) {
$new["caldays"] = 0;
if ($new["enddate"]) unset($new["enddate"]);
if ($new["startdate"]) unset($new["startdate"]);
return $new;
}
$new["caldays"] = (int) (($e-$s) / (60 * 60 * 24));
if ($new["enddate"]) unset($new["enddate"]);
if ($new["startdate"]) unset($new["startdate"]);
return $new;
}
/* tree:
midgard : <- type=server
SG0 <- sitegroup = 0 !
host (name - sg_hosts) <- sitegroup=X
list of hosts;
SG1
*/
// not a raw request or md5 sum
function output_header() {
if (($this->request != 99)) {
header ("Content-Type: text/plain");
$this->ret = "<"."?xml version=\"1.0\"?".">\n\n";
}
}
function output_footer() {
$this->ret .= "\n\n";
}
/*
* The main routine - process request
*
*/
function process_request() {
global $HTTP_POST_VARS,$HTTP_POST_FILES; // for image upload
if (!$this->request) $this->request=0;
$ret ="";
switch ($this->request) { // calltype!
case 0:
// get object!
$obj = $this->rdf_get_object($this->table,$this->id);
if (!$obj) break;
$obj->sgguid="0";
//$obj->guid = $obj->guid();
$this->ret .= $this->output_object($obj,1);
return;
break;
case 1:
// display a list
$ret = "";
if ($this->table == "sitegroup") {
$this->ret .= $this->rdf_tree_types($this->id);
} else if (substr($this->table,0,4) == "sg__") {
$this->ret .= $this->rdf_filtered_tree($this->id,$this->children[substr($this->table,4)]);
} else if (substr($this->table,0,5) == "sga__") {
$this->ret .= $this->rdf_filtered_tree($this->id,$this->children[substr($this->table,5)],1);
} else if ($this->children[$this->table]) {
$this->ret .= $this->rdf_tree($this->id,$this->children[$this->table]);
}
$obj = $this->rdf_get_object($this->table,$this->id);
$this->ret .= $this->rdf_attachments($obj);
break;
case 2: // save object!
$obj = $this->rdf_get_object($this->table,$this->id);
$list = get_object_vars($obj);
$new = $HTTP_POST_VARS["new"];
echo serialize($new);
echo "\n\n\n";
$new = $this->check_calender($new);
echo serialize($new);
echo "\n\n\n";
if ($new["__table__"] != "host")
$this->re_auth($new["sitegroup"]);
$article_score = $obj->score;
$article_type = $obj->type;
$locked = 0;
if ($obj->locker)
$locked = 1;
if (isset($new["username"]))
if ($new["__table__"] == "person")
if (($obj->username != $new["username"]) || $new["password"])
mgd_update_password( $this->id, $new["username"], $new["password"]);
if (isset($new["parameters"])) {
$this->update_parameters($obj,$new["parameters"]);
}
//echo "UPDATING {$this->id}";
while (list ($k,$v) = each($list)) {
if ($k == "sitegroup") continue;
if ($k == "__table__") continue;
if (isset($new[$k])) {
if ($k == "parameters") continue;
//echo "setting $k" . substr($new[$k],0,10)."\n";
$obj->$k = $new[$k];
}
}
$this->ret .= "\n".mgd_errstr()."\n";
//echo serialize($HTTP_POST_VARS);
$obj->update();
if (isset($new["sitegroup"]) && $obj->sitegroup != $new["sitegroup"]) {
$obj->setsitegroup($new["sitegroup"]);
}
//echo "\n APPROVED " . $new["approved"] . "\n";
/* object specific stuff */
if ($new["__table__"] == "article") {
if (($new["approved"] == 1) || ($new["approved"] == 0))
mgd_approve_article($this->id,$new["approved"]);
echo "CHECK LOCk OLD: $locked NEW " . $new["locker"] . "\n";
if ($new["locker"] != $locked)
mgd_toggle_article_lock($this->id);
echo mgd_errstr();
echo "TEST: ". $new["score"] . " $article_score \n";
if (isset($new["score"]) && ($article_score != $new["score"]))
mgd_update_article_score($this->id,$new["score"]);
if (isset($new["type"]) && ($article_type != $new["type"]))
mgd_update_article_type($this->id,$new["type"]);
}
if ($new["__table__"] == "topic") {
if (isset($new["score"]) && ($article_score != $new["score"]))
mgd_update_topic_score($this->id,$new["score"]);
}
if (isset($new["membership"]))
$this->update_membership($obj->id,$new["membership"]);
//echo "CHECKING ." . serialize($new["members"]);
if (isset($new["members"]))
$this->update_members($obj->id,$new["members"]);
$this->md5="";
touch("/tmp/midgard_modified");
$this->ret .= "\n".mgd_errstr()."\n";
return;
case 3: // new object
$new = $HTTP_POST_VARS["new"];
$new = $this->check_calender($new);
if ($new["__table__"] != "host")
$this->re_auth($new["sitegroup"]);
$func = "mgd_get_" . $new["__table__"];
if (!function_exists($func)) {
$this->ret .= "\nFUNCTION $func DOES NOT EXIST \n";
return;
}
$obj = $func();
$list = get_object_vars($obj);
while (list ($k,$v) = each($list)) {
// echo "checking $k ".$new[$k];
if ($new[$k]) {
$obj->$k = $new[$k];
}
}
$this->id = $obj->create();
if ($new["username"])
mgd_update_password( $this->id, $new["username"], $new["password"]);
$this->md5="";
//echo serialize($new);
touch("/tmp/midgard_modified");
if ($this->id) {
$this->ret .= "\n".mgd_errstr()."\n".
"\n\n".
"\n$this->id\n";
return;
} else {
$this->ret .= "\n".mgd_errstr()."\n";
return;
}
break;
case 4: // delete object
if (!$this->id) {
$this->ret .="error : NO ID";
return;
}
if ($this->table == "blobs") $this->table = "attachment";
$func = "mgd_delete_" . $this->table;
if (!function_exists($func)) {
$this->ret .= "\nFUNCTION $func DOES NOT EXIST \n";
return;
}
$func($this->id);
touch("/tmp/midgard_modified");
$this->ret .= "\n".mgd_errstr()."\n";
return;
case 5: // test authentication!
$this->ret .= "AUTHENTICATION FAILED";
return;
case 6: // delete a tree!
if (!$this->id) {
$this->ret .="error : NO ID";
return;
}
$func = "mgd_delete_" . $this->table."_tree";
if (!function_exists($func)) {
$this->ret .= "\nFUNCTION $func DOES NOT EXIST \n";
return;
}
$func($this->id);
touch("/tmp/midgard_modified");
$this->ret .= "\n".mgd_errstr()."\n";
return;
case 8: // upload a image or attachment
//echo serialize($GLOBALS);
$new = $HTTP_POST_VARS["new"];
$obj = $this->rdf_get_object($this->table,$this->id);
$this->re_auth($obj->getsitegroup());
$basecode = implode("",file($HTTP_POST_FILES["test"]["tmp_name"]));
$basecode = str_replace("\n","",$basecode );
$basecode = str_replace("\r","",$basecode );
$data = base64_decode($basecode);
if (($att = $obj->getattachment($new["name"]))) {
$this->id = $att->id;
} else {
$this->id=$obj->createattachment($new["name"],$new["name"],$new["mimetype"]);
}
$attobj = mgd_get_attachment($this->id);
$attobj->setsitegroup($obj->sitegroup);
$fh = mgd_open_attachment($this->id,"w");
fwrite($fh,$data);
fclose($fh);
$this->ret .= "\n".$this->id."\n".
"\n".strlen($data)."\n".
"\n".mgd_errstr()."\n";
break;
case 9: // get a list of attachments;
$obj = $this->rdf_get_object($this->table,$this->id);
if ($list = $obj->listattachments())
while ($list->fetch())
$this->ret.= $this->output_object($list);
return;
case 99: // raw output!!!
// this may not work!!! - may have to use the mgd_open_att...
// md5 sum check here!
$x=mgd_stat_attachment($this->id);
$fh = mgd_open_attachment($this->id,"r");
//echo $x[7];
$string = fread($fh,$x[7]);
fclose($fh);
if ($this->md5 == "x") {
echo $string ;
exit;
}
if ($this->md5 == md5($string)) {
echo $this->md5;
exit;
}
mgd_serve_attachment($this->id);
exit;
}
}
function auth() {
global $PHP_AUTH_USER,$PHP_AUTH_PW ;
if (!isset($PHP_AUTH_USER)) {
header("WWW-Authenticate: Basic realm=\"My Realm\"");
header("HTTP/1.0 401 Unauthorized");
$this->table="";
$this->id=0;
$this->request=1;
$this->md5="";
return;
} else {
mgd_auth_midgard($PHP_AUTH_USER,$PHP_AUTH_PW ,0);
$midgard = mgd_get_midgard();
if (!$midgard->user) {
$this->request = 5;
header("WWW-Authenticate: Basic realm=\"My Realm\"");
header("HTTP/1.0 401 Unauthorized");
return;
}
$this->user = $PHP_AUTH_USER;
$this->sitegroup = 0;
if ($i=strpos($PHP_AUTH_USER,"+")) {
$this->user = substr($PHP_AUTH_USER,0,$i);
$this->sitegroup_name = substr($PHP_AUTH_USER,$i);
$user = mgd_get_person($midgard->user);
$sitegroup_obj = $user->getsitegroup();
$this->sitegroup = $sitegroup_obj->id;
}
}
}
function re_auth($sitegroup) {
global $PHP_AUTH_PW;
if ($this->sitegroup) return;
$sg = mgd_get_sitegroup($sitegroup);
if (!$sg) return;
$auth_as = $this->user ."*" .$sg->name;
mgd_auth_midgard($auth_as,$PHP_AUTH_PW ,0);
}
function start() {
global $HTTP_GET_VARS,$HTTP_POST_VARS,$REQUEST_METHOD;
$request= &$HTTP_GET_VARS;
if (isset($request["table"])) $this->table = $request["table"];
if (isset($request["id"])) $this->id = $request["id"];
if (isset($request["depth"])) $this->request = $request["depth"];
if (isset($request["md5"])) $this->md5 = $request["md5"];
$request= &$HTTP_POST_VARS;
if (isset($request["table"])) $this->table = $request["table"];
if (isset($request["id"])) $this->id = $request["id"];
if (isset($request["depth"])) $this->request = $request["depth"];
if (isset($request["md5"])) $this->md5 = $request["md5"];
$this->auth();
if (!$this->table) {
$this->table = "server";
if ($this->sitegroup) {
$this->table= "sitegroup";
$this->id = $this->sitegroup;
}
}
$this->output_header();
$this->process_request();
$this->output_footer();
if ($this->md5 == "x") echo md5($this->ret);
if ($this->md5 && md5($this->ret) == $this->md5) {
echo $this->md5;
exit;
}
echo $this->ret; flush();
}
}
$reciever = new phpmole_midgard_reciever();
$reciever->start();
?>