12-13-2009, 05:34 AM
The wonders of parsing XML in PHP.
Code taken from StackOverflow (First function, Obj->Arr) and the rest from Creative Framework.
Code over complication. Anyone?
PHP Code:
// http://stackoverflow.com/questions/834875/recursive-cast-from-simplexmlobject-to-array
function _convertXmlObjToArr($obj, &$arr) {
$children = $obj->children();
foreach ($children as $elementName => $node)
{
$nextIdx = count($arr);
$arr[$nextIdx] = array();
$arr[$nextIdx]['name'] = strtolower((string)$elementName);
$arr[$nextIdx]['attributes'] = array();
$attributes = $node->attributes();
foreach ($attributes as $attributeName => $attributeValue)
{
$attribName = strtolower(trim((string)$attributeName));
$attribVal = trim((string)$attributeValue);
$arr[$nextIdx]['attributes'][$attribName] = $attribVal;
}
$text = (string)$node;
$text = trim($text);
if (strlen($text) > 0)
{
$arr[$nextIdx]['text'] = $text;
}
$arr[$nextIdx]['children'] = array();
$this->_convertXmlObjToArr($node, $arr[$nextIdx]['children']);
}
return;
}
// Application part...
function Apps(){ $this->Applications(); } // Yeah, redir.
function Applications() {
global $DB, $Smarty, $UI, $Main;
// OK - Go get it!
// Path is APPLICATION_PATH.universal.
if($d = opendir(APPLICATION_PATH."universal/")) {
while(($file = readdir($d)) !== false) {
if($file !== "." and $file !== ".." and is_dir(APPLICATION_PATH."universal/".$file)) {
$this->_convertXmlObjToArr(simplexml_load_file(APPLICATION_PATH."universal/".$file."/application.xml"), $appdata[$file]);
}
}
}
// OK- show it
// Foreach operates on a copy of the array therefore
// we cannot modify it. Silly it is.
foreach($appdata as $key => $value) {
$apps[$key]["name"] = $value[0]["children"][1]["text"];
$apps[$key]["desc"] = $value[0]["children"][2]["text"];
$apps[$key]["uuid"] = $value[0]["children"][0]["text"];
$apps[$key]["author"] = $value[0]["children"][3]["text"];
$apps[$key]["version"] = $value[0]["children"][4]["text"];
$apps[$key]["key"] = $value[0]["children"][5]["text"];
$apps[$key]["icon"] = $value[0]["children"][6]["text"];
$apps[$key]["xml"] = $value;
}
/* Originally
foreach($appdata as $key => $value) {
// Parse it...
// $appdata[$key]
$data = simplexml_load_string($value);
$appdata[$key]["name"] = $data->info->title;
$appdata[$key]["uuid"] = $data->info->uuid;
$appdata[$key]["author"] = $data->info->author;
$appdata[$key]["version"] = $data->info->version;
$appdata[$key]["key"] = $data->info->key;
$appdata[$key]["xml"] = $data;
} */
// Now that we're all set, output
$UI->message("From here, you can see what applications are present on your CF Installation.", "info");
$UI->message("This page shows all applications available on your system. If you find any application that you didn't install and/or is suspicious, investigate it immediately.");
// nice_dump($apps); // Testing code
$output = "";
foreach($apps as $key => $val) {
$Smarty->assign("app", $val);
$output .= $Smarty->fetch("global/admin/app_single.tpl");
}
// Get it out
echo $UI->basicTable("Installed Applications", $output, "silk/plugin.png", false);
}
Code taken from StackOverflow (First function, Obj->Arr) and the rest from Creative Framework.
Code over complication. Anyone?
Jimmie Lin - Community Manager & THT.Next Developer