Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bah.
#1
The wonders of parsing XML in PHP.

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. Big Grin

Code over complication. Anyone?
Jimmie Lin - Community Manager & THT.Next Developer
Reply
#2
If this means a plugin system for THT, I love you. Big Grin
Nelson - Retired TheHostingTool Developer
[Image: sleek.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)