From 2fe04a4707412ff2f4f8df5f5524bb1b2dcbb3d7 Mon Sep 17 00:00:00 2001 From: neoraider Date: Tue, 11 Apr 2006 18:05:04 +0000 Subject: XML-Parser und Modulverwaltung implementiert. --- code/handlers.inc.php | 5 ++++ code/modules.inc.php | 71 ++++++++++++++++++++++++++++++++++++++++++++++ code/xmlparser.inc.php | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 code/xmlparser.inc.php diff --git a/code/handlers.inc.php b/code/handlers.inc.php index 5410de0..a167ba8 100644 --- a/code/handlers.inc.php +++ b/code/handlers.inc.php @@ -1,4 +1,6 @@ handlers as $file) + include($file); ?> diff --git a/code/modules.inc.php b/code/modules.inc.php index a1bf361..1e84d45 100644 --- a/code/modules.inc.php +++ b/code/modules.inc.php @@ -1,5 +1,76 @@ code[$file])) + require_once($GLOBALS['modules']->code[$file]); + elseif(isset($GLOBALS['modules']->handlers[$file])) + require_once($GLOBALS['modules']->handlers[$file]); + else + require_once($file); + } + class Modules { + var $modules; + var $code; + var $handlers; + function Modules() { + $this->modules = array(); + $this->code = array(); + $this->handlers = array(); + + $dir = opendir('modules'); + + while($moddir = readdir($dir)) { + if($moddir[0] != '.' && is_dir('modules/' . $moddir)) { + $data = $GLOBALS['xmlparser']->ParseFile('modules/' . $moddir . '/module.xml'); + + if(!$data) continue; + + $info = $GLOBALS['xmlparser']->FindTag($data, 'info'); + if(!is_array($info)) continue; + + $name = $GLOBALS['xmlparser']->FindTag($info, 'name'); + if(!is_array($name)) continue; + if(count($name['children']) != 1) continue; + if(!is_string($name['children'][0])) continue; + + $name = $name['children'][0]; + + $this->module[$name] = array('name' => $name, 'code' => array(), + 'handlers' => array()); + + $files = $GLOBALS['xmlparser']->FindTag($data, 'files'); + if(!is_array($files)) continue; + + foreach($files['children'] as $file) { + if(!is_array($file)) continue; + if(count($file['children']) != 1) continue; + if(!is_string($file['children'][0])) continue; + + switch($file['tag']) { + case 'code': + $filename = 'code/' . $file['children'][0] . '.inc.php'; + $realname = 'modules/' . $moddir . '/code/' . $file['children'][0] . '.inc.php'; + + $this->code[$filename] = $realname; + $this->modules[$name]['code'][$filename] = $realname; + break; + case 'handler': + $filename = 'handlers/' . $file['children'][0] . '.inc.php'; + $realname = 'modules/' . $moddir . '/handlers/' . $file['children'][0] . '.inc.php'; + + $this->handlers[$filename] = $realname; + $this->modules[$name]['handlers'][$filename] = $realname; + } + } + } + } + + closedir($dir); + } } + + $GLOBALS['modules'] = new Modules; ?> diff --git a/code/xmlparser.inc.php b/code/xmlparser.inc.php new file mode 100644 index 0000000..2d53a1f --- /dev/null +++ b/code/xmlparser.inc.php @@ -0,0 +1,76 @@ +parser = xml_parser_create(); + + xml_set_object($this->parser, $this); + xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); + + xml_set_element_handler($this->parser, 'openTagHandler', 'closeTagHandler'); + xml_set_character_data_handler($this->parser, 'cdataHandler'); + } + + function ParseFile($filename) { + $this->tags = array(); + $this->depth = 0; + + $file = fopen($filename, 'r'); + + while ($xml = fread($file, 4096)) { + if(!xml_parse($this->parser, $xml, feof($file))) { + $this->tags = null; + break; + } + } + + fclose($file); + + return $this->tags[0]; + } + + function ParseXML($xml) { + $this->tags = array(); + $this->depth = 0; + + if(!xml_parse($this->parser, $xml, true)) + $this->tags = null; + + return $this->tags[0]; + } + + function FindTag($data, $tag, $start = 0) { + for($i = $start; $i < count($data['children']); $i++) { + if(!is_array($data['children'][$i])) continue; + if($data['children'][$i]['tag'] == $tag) return $data['children'][$i]; + } + } + + function openTagHandler($parser, $name, $attribs) { + $this->tags[$this->depth] = array('tag' => $name, + 'attribs' => $attribs, + 'children' => array()); + + $this->depth++; + } + + function closeTagHandler($parser, $name) { + $this->depth--; + + if($this->depth > 0) + array_push($this->tags[$this->depth-1]['children'], $this->tags[$this->depth]); + } + + function cdataHandler($parser, $data) { + $data = trim($data); + + if($data != '') + array_push($this->tags[$this->depth-1]['children'], $data); + } + } + + $GLOBALS['xmlparser'] = new XMLParser; +?> -- cgit v1.2.3