From 54e57d9687169c5308005db90316216e2608df8f Mon Sep 17 00:00:00 2001 From: Jari Kanerva Date: Wed, 30 Dec 2009 03:23:23 +0000 Subject: [PATCH] Added a new MOD text template parser and removed the old parser. Moved the functions for outputing the parser data to a new class. --- constants.php | 4 +- functions.php | 4 +- index.php | 7 +- mod_parser.php | 373 ++++++++++++++++++++++++++++++++++ modx_parser.php | 469 +------------------------------------------ parser_outdata.php | 485 +++++++++++++++++++++++++++++++++++++++++++++ read_mod.php | 310 ----------------------------- read_modx.php | 30 +-- 8 files changed, 889 insertions(+), 793 deletions(-) create mode 100644 mod_parser.php create mode 100644 parser_outdata.php delete mode 100644 read_mod.php diff --git a/constants.php b/constants.php index 92a1268..95c5748 100644 --- a/constants.php +++ b/constants.php @@ -21,8 +21,8 @@ define('MOD', 1); define('MODX', 2); -define('MODX_V1', 1); -define('MODX_V2', 2); +define('MODX_10', 1); +define('MODX_12', 2); define('EASY', 1); define('INTERMEDIATE', 2); diff --git a/functions.php b/functions.php index 57e739c..8a3ca71 100644 --- a/functions.php +++ b/functions.php @@ -294,11 +294,11 @@ function modx_version($xmlns) { if(preg_match('#modx\\-1\\.0(\\.\d)*\\.xsd#s', $xmlns)) { - return(MODX_V1); + return(MODX_10); } if(preg_match('#modx\\-1\\.2(\\.\d)\\.xsd#s', $xmlns)) { - return(MODX_V2); + return(MODX_12); } return(false); } diff --git a/index.php b/index.php index 6acbd2f..658bfe2 100644 --- a/index.php +++ b/index.php @@ -30,7 +30,7 @@ { // Lets start with the extension... $extension = strtolower(array_pop(explode('.', $_FILES['upload-file']['name']))); - $str = file_get_contents($_FILES['upload-file']['tmp_name'], 0, NULL, 0, 20); + $str = file_get_contents($_FILES['upload-file']['tmp_name'], 0, NULL, 0, 200); // We'll need to know what kind of file it is $submit_file = get_mod_type($str, $extension); @@ -56,7 +56,10 @@ } else if($submit_file == MOD) { - include('./read_mod.php'); + include('./mod_parser.php'); + $parser = new mod_parser($modx_data); + + include('./read_modx.php'); } else { diff --git a/mod_parser.php b/mod_parser.php new file mode 100644 index 0000000..543e2e3 --- /dev/null +++ b/mod_parser.php @@ -0,0 +1,373 @@ +cnt_action = -1; + $this->cnt_edit = -1; + $this->cnt_open = -1; + + + // Think it's faster too loop trough the array twice and get the anything except the action first. + $cnt = 0; + foreach ($mod_arr as $key => $data) + { + // Check if it's only a separator. + if (substr($data, 0, 4) == '####' || trim($data) == '#') + { + unset($mod_arr[$key]); + continue; + } + + // First the ones that only supposed to occur once in the MOD file. + if(substr($data, 0, 2) == '##') + { + if(substr($data, 0, 3) == '###') + { + continue; + } + else + { + $field_id = 'p_' . $cnt++; + switch($data) + { + case (strpos(substr($data, 0, 13), 'MOD Title') !== false && empty($this->title)): + $this->title[0]['data'] = trim(preg_replace(array('/##/', '/MOD/', '/Title/', '/:/'), '', $data, 1)); + $this->title[0]['lang'] = 'en'; + break; + + case (strpos(substr($data, 0, 14), 'MOD Author') !== false): + $tmp_str = preg_replace(array('/##/', '/MOD/', '/Author/', '/:/'), '', $data, 1); + // The author string also contains some other info. Lets remove that. + $this->author[0]['username'] = trim(strtok($tmp_str, '<')); + break; + + case (strpos(substr($data, 0, 20), 'MOD Description') !== false && empty($this->description)): + $this->description[0]['lang'] = 'en'; + $this->description[0]['data'] = trim(preg_replace(array('/##/', '/MOD/', '/Description/', '/:/'), '', $data, 1)); + + // The description + $ccnt = $key + 1; + $tmp_str = $mod_arr[$ccnt++]; + $tmp_str = trim($tmp_str, '#'); + $tmp_str = trim($tmp_str); + while($tmp_str != '' && strpos(substr($tmp_str, 0, 7), 'MOD') === false && strpos(substr($tmp_str, 0, 11), 'Install') === false) + { + $this->description[0]['data'] .= $tmp_str; + $tmp_str = $mod_arr[$ccnt++]; + $tmp_str = trim($tmp_str, '#'); + $tmp_str = trim($tmp_str); + } + break; + + case (strpos(substr($data, 0, 15), 'MOD Version') !== false && empty($this->mod_version)): + $this->mod_version = trim(preg_replace(array('/##/', '/MOD/', '/Version/', '/:/'), '', $data, 1)); + break; + + case (strpos(substr($data, 0, 22), 'Installation Level') !== false && empty($this->installation_level)): + $this->installation_level = strtolower(trim(preg_replace(array('/##/', '/Installation/', '/Level/', '/:/'), '', $data, 1))); + break; + + case (strpos(substr($data, 0, 22), 'Installation Time') !== false && empty($this->installation_time)): + $this->installation_time = intval(preg_replace(array('/##/', '/Installation/', '/Time/', '/:/'), '', $data, 1)); + break; + + case (strpos(substr($data, 0, 18), 'Compatibility') !== false && empty($this->target_version)): + $this->target_version = trim(preg_replace(array('/##/', '/Compatibility/', '/:/'), '', $data, 1)); + break; + + case (strpos(substr($data, 0, 19), 'phpBB Version') !== false && empty($this->target_version)): + $this->target_version = trim(preg_replace(array('/##/', '/phpBB/', '/Version/', '/:/'), '', $data, 1)); + break; + + case (strpos(substr($data, 0, 12), 'License') !== false && empty($this->license)): + $this->license = trim(preg_replace(array('/##/', '/License/', '/:/'), '', $data, 1)); + break; + + default: + continue; + break; + } + } + unset($mod_arr[$key]); + } + } + + // a quick sort to get the keys in order and remove leftover rows... + foreach($mod_arr as $data) + { + if(substr($data, 0, 2) != '##') + { + $tmp_arr[] = $data; + } + } + unset($mod_arr); + $mod_arr = $tmp_arr; + unset($tmp_arr); + + // The damage part... + foreach($mod_arr as $key => $data) + { + $field_id = 'a_' . $cnt++; + if(substr($data, 0, 1) == '#' && strpos($data, '#--') !== false) + { + $inline = false; + $check_str = strtolower($data); + if(strpos($check_str, 'sql') !== false) + { + $this->in_action = false; + // SQL + $this->sql[$this->cnt_sql]['dbms'] = ''; + $this->sql[$this->cnt_sql]['data'] = ''; + + $ccnt = $key + 1; + while(isset($mod_arr[$ccnt]) && substr($mod_arr[$ccnt], 0, 1) != '#') + { + if(trim($mod_arr[$ccnt]) == '') + { + $this->sql[$this->cnt_sql]['data'] = trim($this->sql[$this->cnt_sql]['data']); + $this->cnt_sql++; + $this->sql[$this->cnt_sql]['data'] = ''; + $this->sql[$this->cnt_sql]['dbms'] = ''; + } + else + { + $this->sql[$this->cnt_sql]['data'] .= $mod_arr[$ccnt] . "\n"; + } + $ccnt++; + } + $this->sql[$this->cnt_sql]['data'] = trim($this->sql[$this->cnt_sql]['data']); + $this->cnt_sql++; + } + else if(strpos($check_str, 'copy') !== false) + { + $this->in_action = false; + // copy + $ccnt = $key + 1; + while(isset($mod_arr[$ccnt]) && substr($mod_arr[$ccnt], 0, 1) != '#') + { + if(trim($mod_arr[$ccnt]) != '') + { + $tmp_str = trim($mod_arr[$ccnt]); + $dummy = strtok($tmp_str, ' '); + $this->copy[$this->cnt_copy]['from'] = trim(strtok(' ')); + $dummy = strtok(' '); + $this->copy[$this->cnt_copy]['to'] = trim(strtok(' ')); + $this->cnt_copy++; + } + $ccnt++; + } + } + else if(strpos($check_str, 'diy') !== false) + { + $this->in_action = false; + $this->diy[$this->cnt_diy]['lang'] = 'en'; + $this->diy[$this->cnt_diy]['data'] = ''; + + $ccnt = $key + 1; + while(isset($mod_arr[$ccnt]) && substr($mod_arr[$ccnt], 0, 1) != '#') + { + $this->diy[$this->cnt_diy]['data'] .= $mod_arr[$ccnt] . "\n"; + $ccnt++; + } + $this->diy[$this->cnt_diy]['data'] = rtrim($this->diy[$this->cnt_diy]['data']); + } + else if(strpos($check_str, 'open') !== false) + { + $this->in_action = $this->in_edit = false; + $action_done = true; + $ccnt = $key + 1; + while(isset($mod_arr[$ccnt]) && substr($mod_arr[$ccnt], 0, 1) != '#') + { + $tmp_str = trim($mod_arr[$ccnt]); + if($tmp_str != '') + { + $this->cnt_open++; + $this->action[$this->cnt_open]['file'] = $tmp_str; + } + $ccnt++; + } + } + // The rest of them also needs a open file so $file_id can't be empty + else if((strpos($check_str, 'in-line find') !== false || strpos($check_str, 'inline find') !== false) && $this->in_edit && $this->cnt_open > -1 && $this->cnt_edit > -1) + { + $inline = true; + $this->in_action = true; + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'inline-find'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else if(strpos($check_str, 'find') !== false && $this->cnt_open > -1) + { + $this->in_action = true; + if(!$action_done) + { + // This is just a additional find whithin the same edit. + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'find'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else + { + // a new edit. + $this->cnt_action++; + $this->cnt_edit++; + $action_done = false; + $this->in_edit = true; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'find'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + } + else if((strpos($check_str, 'in-line after') !== false || strpos($check_str, 'inline after') !== false) && $this->cnt_open > -1 && $this->in_edit) + { + $inline = true; + $action_done = true; + $this->in_action = true; + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'inline-after-add'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else if((strpos($check_str, 'in-line before') !== false || strpos($check_str, 'inline before') !== false) && $this->cnt_open > -1 && $this->in_edit) + { + $inline = true; + $action_done = true; + $this->in_action = true; + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'inline-before-add'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else if((strpos($check_str, 'in-line replace') !== false || strpos($check_str, 'inline replace') !== false) && $this->cnt_open > -1 && $this->in_edit) + { + $inline = true; + $action_done = true; + $this->in_action = true; + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'inline-replace-with'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else if((strpos($check_str, 'in-line operation') !== false || strpos($check_str, 'inline operation') !== false) && $this->cnt_open > -1 && $this->in_edit) + { + $inline = true; + $action_done = true; + $this->in_action = true; + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'inline-operation'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else if((strpos($check_str, 'after') !== false || strpos($check_str, 'after') !== false) && $this->cnt_open > -1 && $this->in_edit) + { + $action_done = true; + $this->in_action = true; + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'after-add'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else if((strpos($check_str, 'before') !== false || strpos($check_str, 'before') !== false) && $this->cnt_open > -1 && $this->in_edit) + { + $action_done = true; + $this->in_action = true; + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'before-add'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else if((strpos($check_str, 'replace') !== false || strpos($check_str, 'replace') !== false) && $this->cnt_open > -1 && $this->in_edit) + { + $action_done = true; + $this->in_action = true; + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'replace-with'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else if((strpos($check_str, 'operation') !== false || strpos($check_str, 'operation') !== false) && $this->cnt_open > -1 && $this->in_edit) + { + $action_done = true; + $this->in_action = true; + $this->cnt_action++; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['type'] = 'operation'; + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] = ''; + } + else + { + $this->in_action = false; + } + } + else if($this->in_action) + { + if ($inline) + { + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] .= trim($data); + } + else + { + $this->action[$this->cnt_open][$this->cnt_edit][$this->cnt_action]['data'] .= $data . "\n"; + } + } + } + + // We need to set the counters back to zero. + $this->cnt_action = 0; + $this->cnt_author = 0; + $this->cnt_author_notes = 0; + $this->cnt_change = 0; + $this->cnt_changelog = 0; + $this->cnt_copy = 0; + $this->cnt_description = 0; + $this->cnt_diy = 0; + $this->cnt_edit = 0; + $this->cnt_history = 0; + $this->cnt_link = 0; + $this->cnt_meta = 0; + $this->cnt_open = 0; + $this->cnt_sql = 0; + $this->cnt_title = 0; + } +} diff --git a/modx_parser.php b/modx_parser.php index 112c2aa..5e72f81 100644 --- a/modx_parser.php +++ b/modx_parser.php @@ -20,49 +20,10 @@ * All internal vars are protected and only accessible trough the public functions. */ -define('MODX_10', 1); -define('MODX_12', 2); +include('./parser_outdata.php'); -class modx_parser +class modx_parser extends parser_outdata { - // MODX strings - protected $installation_level = ''; - protected $installation_time = ''; - protected $license = ''; - protected $mod_version = ''; - protected $modx_version = ''; - protected $target_version = ''; - - // MODX arrays - protected $action = array(); - protected $author = array(); - protected $author_notes = array(); - protected $copy = array(); - protected $description = array(); - protected $diy = array(); - protected $history = array(); - protected $link = array(); - protected $meta = array(); - protected $sql = array(); - protected $title = array(); - - // Counters - protected $cnt_action = 0; - protected $cnt_author = 0; - protected $cnt_author_notes = 0; - protected $cnt_change = 0; - protected $cnt_changelog = 0; - protected $cnt_copy = 0; - protected $cnt_description = 0; - protected $cnt_diy = 0; - protected $cnt_edit = 0; - protected $cnt_history = 0; - protected $cnt_link = 0; - protected $cnt_meta = 0; - protected $cnt_open = 0; - protected $cnt_sql = 0; - protected $cnt_title = 0; - // Switches only used while parsing. protected $in_author = false; protected $in_copy = false; @@ -70,7 +31,7 @@ class modx_parser protected $in_history = false; protected $in_mod_version = false; - // Temporary vars + // Temporary vars for parsing protected $tmp_data = ''; protected $used_modx = 0; protected $version_major = 0; @@ -79,7 +40,7 @@ class modx_parser protected $version_release = ''; protected $tmp_key = -1; - // Public functions + // Public function public function modx_parser($modx_data) { $xml_parser = xml_parser_create(); @@ -112,428 +73,6 @@ public function modx_parser($modx_data) $this->cnt_title = 0; } - /** - * get_modx_installation_level - * - * Get the installation level for the MOD - * @param $string, bool - * @return string if $string is true or predefined int if $string is false - */ - public function get_modx_installation_level($string = false) - { - if ($string) - { - return($this->mod_version); - } - else - { - switch ($this->mod_version) - { - case 'easy': - return(EASY); - break; - - case 'advanced': - return(ADVANCED); - break; - - default: - return(INTERMEDIATE); - break; - } - } - } - - /** - * get_modx_installation_time - * - * Get the installation time, in seconds or minutes. - * @param $seconds, bool - * @return int, minutes if $seconds is false otherwise seconds - */ - public function get_modx_installation_time($seconds = true) - { - if($seconds) - { - return($this->installation_time); - } - else - { - return($this->installation_time / 60); - } - } - - /** - * get_modx_license - * - * Get the license for the MOD - * @return string license - */ - public function get_modx_license() - { - return($this->license); - } - - /** - * get_modx_mod_version - * - * @return string, the MOD version - */ - public function get_modx_mod_version() - { - return($this->mod_version); - } - - /** - * get_modx_target - * - * @return string, the target phpBB version - */ - public function get_modx_target_version() - { - return($this->target_version); - } - - /** - * get_modx_version - * - * @return string, the MODX version used in this file - */ - public function get_modx_version() - { - return($this->modx_version); - } - - /** - * get_modx_authors - * - * Get the MOD authors. Loop this to get all authors. - * - * $author[] = array( - * 'realname', - * 'username', - * 'phpbbcom', - * 'homepage', - * 'email', - * 'contributions'[] => array( - * 'status', - * 'from', - * 'to', - * 'position', - * ), - * ); - * - * @return array, the MOD author array or false when there is no more. - */ - public function get_modx_authors() - { - if ($this->cnt_author < sizeof($this->author)) - { - return($this->author[$this->cnt_author++]); - } - else - { - return(false); - } - } - - /** - * get_modx_copy - * - * Get the copy actions for this MOD. Loop this to get all. - * - * $copy[] = array( - * 'from', - * 'to', - * ); - * - * @return array, the copy actions, or false when done. - */ - public function get_modx_copy() - { - if ($this->cnt_copy < sizeof($this->copy)) - { - return($this->copy[$this->cnt_copy++]); - } - else - { - return(false); - } - } - - /** - * get_modx_description - * - * Get the MOD descriptions. Loop this to get the all. - * - * $description[] = array( - * 'lang', - * 'data', - * ); - * - * @param $lang, string with language code. Returns false if $lang is not found. - * @return string or false if $lang is set. - * @return array with the description or false if $lang is specified and don't exist. - */ - public function get_modx_description($lang = '') - { - if ($lang != '') - {} - else - { - if ($this->cnt_description < sizeof($this->description)) - { - return($this->description[$this->cnt_description++]); - } - else - { - return(false); - } - } - } - - /** - * get_modx_history - * - * Get the MOD history. Loop this for all. - * - * $history[] = array( - * 'date', - * 'version', - * 'changelog'[] => array( - * 'lang', - * 'change'[] => change - * ), - * ); - * - * @param $lang, string with language code. Returns false if $lang is not found. - * @return string or false if $lang is set. - * @return array, the MOD history or false when done. - */ - public function get_modx_history($lang = '') - { - if ($lang != '') - {} - else - { - if ($this->cnt_history < sizeof($this->history)) - { - return($this->history[$this->cnt_history++]); - } - else - { - return(false); - } - } - } - - /** - * get_modx_links - * - * Get the links for the MOD. Loop this to get them all. - * - * $link[] = array( - * 'type', - * 'href', - * 'lang', - * 'realname', - * 'data', - * ); - * - * @param $lang, string with language code. Returns false if $lang is not found. - * @return array with the selected lang or false if $lang is set. - * @return array with the link or false when there are no more links. - */ - public function get_modx_links($lang = '') - { - if ($lang != '') - {} - else - { - if ($this->cnt_link < sizeof($this->link)) - { - return($this->link[$this->cnt_link++]); - } - else - { - return(false); - } - } - } - - /** - * get_modx_meta - * - * Get the meta tags. Loop this to get all. - * - * $meta[] = array( - * 'name', - * 'content', - * ); - * - * @return array or false if there are no more. - */ - public function get_modx_meta() - { - if ($this->cnt_meta < sizeof($this->meta)) - { - return($this->meta[$this->cnt_meta++]); - } - else - { - return(false); - } - } - - /** - * get_modx_notes - * - * Get the author notes for this MOD. Loop to get them all. - * - * $author_notes[] = array( - * 'lang', - * 'data', - * ); - * - * @param $lang, string with language code. Returns false if $lang is not found. - * @return string or false if $lang is set. - * @return array or false if done. - */ - public function get_modx_notes($lang = '') - { - if ($lang != '') - {} - else - { - if ($this->cnt_author_notes < sizeof($this->author_notes)) - { - return($this->author_notes[$this->cnt_author_notes++]); - } - else - { - return(false); - } - } - } - - /** - * get_modx_query - * - * Get the querys for this MOD. Loop to get all. - * - * $sql[] = array( - * 'dbms', - * 'data', - * ); - * - * @return array or false when done. - */ - public function get_modx_sql($dbms = '') - { - if ($dbms != '') - {} - else - { - if ($this->cnt_sql < sizeof($this->sql)) - { - return($this->sql[$this->cnt_sql++]); - } - else - { - return(false); - } - } - } - - /** - * get_modx_title - * - * Get the MOD title. Loop for all titles. - * - * $title[] = array( - * 'lang', - * 'data', - * ); - * - * @param $lang, string with language code. Returns false if $lang is not found. - * @return string or false if $lang is set. - * @return array or false when done - */ - public function get_modx_title($lang = '') - { - if ($lang != '') - {} - else - { - if ($this->cnt_title < sizeof($this->title)) - { - return($this->title[$this->cnt_title++]); - } - else - { - return(false); - } - } - } - - /** - * get_modx_diy - * - * Get the MOD DIY. Loop for all DIYs. - * - * $diy[] = array( - * 'lang', - * 'data', - * ); - * - * @param $lang, string with language code. Returns false if $lang is not found. - * @return string or false if $lang is set. - * @return array or false when done - */ - public function get_modx_diy($lang = '') - { - if ($lang != '') - {} - else - { - if ($this->cnt_diy < sizeof($this->diy)) - { - return($this->diy[$this->cnt_diy++]); - } - else - { - return(false); - } - } - } - - /** - * get_modx_action - * - * Get the action in order. - * - * $action[] = array( - * 'src', - * x[] => array( // x == int - * 'type', - * 'data', - * ), - * ); - * - * @return array with change, string with file name or false when done - */ - public function get_modx_action() - { - if ($this->cnt_action < sizeof($this->action)) - { - return($this->action[$this->cnt_action++]); - } - else - { - return(false); - } - } - // Private functions. private function startElement($parser, $name, $attrs) { diff --git a/parser_outdata.php b/parser_outdata.php new file mode 100644 index 0000000..6f14e09 --- /dev/null +++ b/parser_outdata.php @@ -0,0 +1,485 @@ +mod_version); + } + else + { + switch ($this->mod_version) + { + case 'easy': + return(EASY); + break; + + case 'advanced': + return(ADVANCED); + break; + + default: + return(INTERMEDIATE); + break; + } + } + } + + /** + * get_modx_installation_time + * + * Get the installation time, in seconds or minutes. + * @param $seconds, bool + * @return int, minutes if $seconds is false otherwise seconds + */ + public function get_modx_installation_time($seconds = true) + { + if($seconds) + { + return($this->installation_time); + } + else + { + return($this->installation_time / 60); + } + } + + /** + * get_modx_license + * + * Get the license for the MOD + * @return string license + */ + public function get_modx_license() + { + return($this->license); + } + + /** + * get_modx_mod_version + * + * @return string, the MOD version + */ + public function get_modx_mod_version() + { + return($this->mod_version); + } + + /** + * get_modx_target + * + * @return string, the target phpBB version + */ + public function get_modx_target_version() + { + return($this->target_version); + } + + /** + * get_modx_version + * + * @return string, the MODX version used in this file + */ + public function get_modx_version() + { + return($this->modx_version); + } + + /** + * get_modx_authors + * + * Get the MOD authors. Loop this to get all authors. + * + * $author[] = array( + * 'realname', + * 'username', + * 'phpbbcom', + * 'homepage', + * 'email', + * 'contributions'[] => array( + * 'status', + * 'from', + * 'to', + * 'position', + * ), + * ); + * + * @return array, the MOD author array or false when there is no more. + */ + public function get_modx_authors() + { + if ($this->cnt_author < sizeof($this->author)) + { + return($this->author[$this->cnt_author++]); + } + else + { + return(false); + } + } + + /** + * get_modx_copy + * + * Get the copy actions for this MOD. Loop this to get all. + * + * $copy[] = array( + * 'from', + * 'to', + * ); + * + * @return array, the copy actions, or false when done. + */ + public function get_modx_copy() + { + if ($this->cnt_copy < sizeof($this->copy)) + { + return($this->copy[$this->cnt_copy++]); + } + else + { + return(false); + } + } + + /** + * get_modx_description + * + * Get the MOD descriptions. Loop this to get the all. + * + * $description[] = array( + * 'lang', + * 'data', + * ); + * + * @param $lang, string with language code. Returns false if $lang is not found. + * @return string or false if $lang is set. + * @return array with the description or false if $lang is specified and don't exist. + */ + public function get_modx_description($lang = '') + { + if ($lang != '') + {} + else + { + if ($this->cnt_description < sizeof($this->description)) + { + return($this->description[$this->cnt_description++]); + } + else + { + return(false); + } + } + } + + /** + * get_modx_history + * + * Get the MOD history. Loop this for all. + * + * $history[] = array( + * 'date', + * 'version', + * 'changelog'[] => array( + * 'lang', + * 'change'[] => change + * ), + * ); + * + * @param $lang, string with language code. Returns false if $lang is not found. + * @return string or false if $lang is set. + * @return array, the MOD history or false when done. + */ + public function get_modx_history($lang = '') + { + if ($lang != '') + {} + else + { + if ($this->cnt_history < sizeof($this->history)) + { + return($this->history[$this->cnt_history++]); + } + else + { + return(false); + } + } + } + + /** + * get_modx_links + * + * Get the links for the MOD. Loop this to get them all. + * + * $link[] = array( + * 'type', + * 'href', + * 'lang', + * 'realname', + * 'data', + * ); + * + * @param $lang, string with language code. Returns false if $lang is not found. + * @return array with the selected lang or false if $lang is set. + * @return array with the link or false when there are no more links. + */ + public function get_modx_links($lang = '') + { + if ($lang != '') + {} + else + { + if ($this->cnt_link < sizeof($this->link)) + { + return($this->link[$this->cnt_link++]); + } + else + { + return(false); + } + } + } + + /** + * get_modx_meta + * + * Get the meta tags. Loop this to get all. + * + * $meta[] = array( + * 'name', + * 'content', + * ); + * + * @return array or false if there are no more. + */ + public function get_modx_meta() + { + if ($this->cnt_meta < sizeof($this->meta)) + { + return($this->meta[$this->cnt_meta++]); + } + else + { + return(false); + } + } + + /** + * get_modx_notes + * + * Get the author notes for this MOD. Loop to get them all. + * + * $author_notes[] = array( + * 'lang', + * 'data', + * ); + * + * @param $lang, string with language code. Returns false if $lang is not found. + * @return string or false if $lang is set. + * @return array or false if done. + */ + public function get_modx_notes($lang = '') + { + if ($lang != '') + {} + else + { + if ($this->cnt_author_notes < sizeof($this->author_notes)) + { + return($this->author_notes[$this->cnt_author_notes++]); + } + else + { + return(false); + } + } + } + + /** + * get_modx_query + * + * Get the querys for this MOD. Loop to get all. + * + * $sql[] = array( + * 'dbms', + * 'data', + * ); + * + * @return array or false when done. + */ + public function get_modx_sql($dbms = '') + { + if ($dbms != '') + {} + else + { + if ($this->cnt_sql < sizeof($this->sql)) + { + return($this->sql[$this->cnt_sql++]); + } + else + { + return(false); + } + } + } + + /** + * get_modx_title + * + * Get the MOD title. Loop for all titles. + * + * $title[] = array( + * 'lang', + * 'data', + * ); + * + * @param $lang, string with language code. Returns false if $lang is not found. + * @return string or false if $lang is set. + * @return array or false when done + */ + public function get_modx_title($lang = '') + { + if ($lang != '') + {} + else + { + if ($this->cnt_title < sizeof($this->title)) + { + return($this->title[$this->cnt_title++]); + } + else + { + return(false); + } + } + } + + /** + * get_modx_diy + * + * Get the MOD DIY. Loop for all DIYs. + * + * $diy[] = array( + * 'lang', + * 'data', + * ); + * + * @param $lang, string with language code. Returns false if $lang is not found. + * @return string or false if $lang is set. + * @return array or false when done + */ + public function get_modx_diy($lang = '') + { + if ($lang != '') + {} + else + { + if ($this->cnt_diy < sizeof($this->diy)) + { + return($this->diy[$this->cnt_diy++]); + } + else + { + return(false); + } + } + } + + /** + * get_modx_action + * + * Get the action in order. + * + * $action[] = array( + * 'src', + * x[] => array( // x == int + * 'type', + * 'data', + * ), + * ); + * + * @return array with change, string with file name or false when done + */ + public function get_modx_action() + { + if ($this->cnt_action < sizeof($this->action)) + { + return($this->action[$this->cnt_action++]); + } + else + { + return(false); + } + } +} diff --git a/read_mod.php b/read_mod.php deleted file mode 100644 index 04cb243..0000000 --- a/read_mod.php +++ /dev/null @@ -1,310 +0,0 @@ - $modx_data) -{ - // Check if it's only a separator. - if(substr($modx_data, 0, 4) == '####' || trim($modx_data) == '#') - { - unset($mod_arr[$key]); - continue; - } - - // First the ones that only supposed to occur once in the MOD file. - if(substr($modx_data, 0, 2) == '##') - { - if(substr($modx_data, 0, 3) == '###') - { - continue; - } - else - { - $field_id = 'p_' . $cnt++; - switch($modx_data) - { - case (strpos(substr($modx_data, 0, 13), 'MOD Title') !== false && $title == ''): - $title[$field_id]['title'] = trim(preg_replace(array('/##/', '/MOD/', '/Title/', '/:/'), '', $modx_data, 1)); - $title[$field_id]['lang'] = 'en'; - break; - - case (strpos(substr($modx_data, 0, 14), 'MOD Author') !== false): - $tmp_str = preg_replace(array('/##/', '/MOD/', '/Author/', '/:/'), '', $modx_data, 1); - // The author string also contains some other info. Lets remove that. - $author[$field_id]['username'] = trim(strtok($tmp_str, '<')); - break; - - case (strpos(substr($modx_data, 0, 20), 'MOD Description') !== false && $desc == ''): - $desc[$field_id]['lang'] = 'en'; - $desc[$field_id]['desc'] = trim(preg_replace(array('/##/', '/MOD/', '/Description/', '/:/'), '', $modx_data, 1)); - - // The description - $ccnt = $key + 1; - $tmp_str = $mod_arr[$ccnt++]; - $tmp_str = trim($tmp_str, '#'); - $tmp_str = trim($tmp_str); - while($tmp_str != '' && strpos(substr($tmp_str, 0, 7), 'MOD') === false && strpos(substr($tmp_str, 0, 11), 'Install') === false) - { - $desc[$field_id]['desc'] .= $tmp_str; - $tmp_str = $mod_arr[$ccnt++]; - $tmp_str = trim($tmp_str, '#'); - $tmp_str = trim($tmp_str); - } - break; - - case (strpos(substr($modx_data, 0, 15), 'MOD Version') !== false && $version == ''): - $version = trim(preg_replace(array('/##/', '/MOD/', '/Version/', '/:/'), '', $modx_data, 1)); - break; - - case (strpos(substr($modx_data, 0, 22), 'Installation Level') !== false && $install_level == ''): - $install_level = strtolower(trim(preg_replace(array('/##/', '/Installation/', '/Level/', '/:/'), '', $modx_data, 1))); - break; - - case (strpos(substr($modx_data, 0, 22), 'Installation Time') !== false && $install_time == ''): - $install_time = intval(preg_replace(array('/##/', '/Installation/', '/Time/', '/:/'), '', $modx_data, 1)); - break; - - case (strpos(substr($modx_data, 0, 18), 'Compatibility') !== false && $target == ''): - $target = trim(preg_replace(array('/##/', '/Compatibility/', '/:/'), '', $modx_data, 1)); - break; - - case (strpos(substr($modx_data, 0, 19), 'phpBB Version') !== false && $target == ''): - $target = trim(preg_replace(array('/##/', '/phpBB/', '/Version/', '/:/'), '', $modx_data, 1)); - break; - - case (strpos(substr($modx_data, 0, 12), 'License') !== false && $license == ''): - $license = trim(preg_replace(array('/##/', '/License/', '/:/'), '', $modx_data, 1)); - break; - - default: - // nothing - break; - } - } - unset($mod_arr[$key]); - } -} - -// a quick sort to get the keys in order and remove leftover rows... -foreach($mod_arr as $modx_data) -{ - if(substr($modx_data, 0, 2) != '##') - { - $tmp_arr[] = $modx_data; - } -} -unset($mod_arr); -$mod_arr = $tmp_arr; -unset($tmp_arr); - -$file_cnt = $edit_cnt = $dl_cnt = $sql_cnt = $copy_cnt = $diy_cnt = 0; -$file_id = $edit_id = $dl_id = ''; -$in_edit = $action_done = $in_action = false; -foreach($mod_arr as $key => $modx_data) -{ - $field_id = 'a_' . $cnt++; - if(substr($modx_data, 0, 1) == '#' && strpos($modx_data, '#--') !== false) - { - $check_str = strtolower($modx_data); - if(strpos($check_str, 'sql') !== false) - { - $in_action = false; - // SQL - $sql_id = 'sql_' . $sql_cnt++; - $sql[$sql_id]['dbms'] = ''; - $sql[$sql_id]['query'] = ''; - - $ccnt = $key + 1; - while(isset($mod_arr[$ccnt]) && substr($mod_arr[$ccnt], 0, 1) != '#') - { - if(trim($mod_arr[$ccnt]) == '') - { - $sql[$sql_id]['query'] = rtrim($sql[$sql_id]['query']); - $sql_id = 'sql_' . $sql_cnt++; - $sql[$sql_id]['dbms'] = ''; - $sql[$sql_id]['query'] = ''; - } - else - { - $sql[$sql_id]['query'] .= $mod_arr[$ccnt] . "\n"; - } - $ccnt++; - } - $sql[$sql_id]['query'] = rtrim($sql[$sql_id]['query']); - } - else if(strpos($check_str, 'copy') !== false) - { - $in_action = false; - // copy - $copy_id = 'copy_' . $copy_cnt++; - $ccnt = $key + 1; - $i = 0; - while(isset($mod_arr[$ccnt]) && substr($mod_arr[$ccnt], 0, 1) != '#') - { - if(trim($mod_arr[$ccnt]) != '') - { - $copy_field = 'cpy_' . $i++; - $tmp_str = trim($mod_arr[$ccnt]); - $dummy = strtok($tmp_str, ' '); - $copy[$copy_id][$copy_field]['from'] = trim(strtok(' ')); - $dummy = strtok(' '); - $copy[$copy_id][$copy_field]['to'] = trim(strtok(' ')); - } - $ccnt++; - } - } - else if(strpos($check_str, 'diy') !== false) - { - $in_action = false; - $diy_id = 'diy_' . $diy_cnt++; - $diy[$diy_id]['lang'] = 'en'; - $diy[$diy_id]['diy'] = ''; - - $ccnt = $key + 1; - while(isset($mod_arr[$ccnt]) && substr($mod_arr[$ccnt], 0, 1) != '#') - { - $diy[$diy_id]['diy'] .= $mod_arr[$ccnt] . "\n"; - $ccnt++; - } - $diy[$diy_id]['diy'] = rtrim($diy[$diy_id]['diy']); - } - else if(strpos($check_str, 'open') !== false) - { - $in_edit = $in_action = false; - $action_done = true; - $file_id = 'file_' . $file_cnt++; - $ccnt = $key + 1; - while(isset($mod_arr[$ccnt]) && substr($mod_arr[$ccnt], 0, 1) != '#') - { - $tmp_str = trim($mod_arr[$ccnt]); - if($tmp_str != '') - { - $modx[$file_id]['file'] = $tmp_str; - } - $ccnt++; - } - } - // The rest of them also needs a open file so $file_id can't be empty - else if((strpos($check_str, 'in-line find') !== false || strpos($check_str, 'inline find') !== false) && $file_id != '' && $in_edit) - { - $in_action = true; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'inline-find'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else if(strpos($check_str, 'find') !== false && $file_id != '') - { - $in_action = true; - if(!$action_done) - { - // This is just a additional find whithin the same edit. - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'find'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else - { - // a new edit. - $action_done = false; - $in_edit = true; - $edit_id = 'edit_' . $edit_cnt++; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'find'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - } - else if((strpos($check_str, 'in-line after') !== false || strpos($check_str, 'inline after') !== false) && $file_id != '' && $in_edit) - { - $action_done = true; - $in_action = true; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'inline-after-add'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else if((strpos($check_str, 'in-line before') !== false || strpos($check_str, 'inline before') !== false) && $file_id != '' && $in_edit) - { - $action_done = true; - $in_action = true; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'inline-before-add'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else if((strpos($check_str, 'in-line replace') !== false || strpos($check_str, 'inline replace') !== false) && $file_id != '' && $in_edit) - { - $action_done = true; - $in_action = true; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'inline-replace-with'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else if((strpos($check_str, 'in-line operation') !== false || strpos($check_str, 'inline operation') !== false) && $file_id != '' && $in_edit) - { - $action_done = true; - $in_action = true; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'inline-operation'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else if((strpos($check_str, 'after') !== false || strpos($check_str, 'after') !== false) && $file_id != '' && $in_edit) - { - $action_done = true; - $in_action = true; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'after-add'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else if((strpos($check_str, 'before') !== false || strpos($check_str, 'before') !== false) && $file_id != '' && $in_edit) - { - $action_done = true; - $in_action = true; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'before-add'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else if((strpos($check_str, 'replace') !== false || strpos($check_str, 'replace') !== false) && $file_id != '' && $in_edit) - { - $action_done = true; - $in_action = true; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'replace-with'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else if((strpos($check_str, 'operation') !== false || strpos($check_str, 'operation') !== false) && $file_id != '' && $in_edit) - { - $action_done = true; - $in_action = true; - $dl_id = 'dl_' . $dl_cnt++; - $modx[$file_id][$edit_id][$dl_id]['type'] = 'operation'; - $modx[$file_id][$edit_id][$dl_id]['data'] = ''; - } - else - { - $in_action = false; - } - } - else if($in_action) - { - $modx[$file_id][$edit_id][$dl_id]['data'] .= $modx_data . "\n"; - } -} diff --git a/read_modx.php b/read_modx.php index 570b771..5deefbb 100644 --- a/read_modx.php +++ b/read_modx.php @@ -23,11 +23,6 @@ * First I need to add a new MOD template parser and a parser for the post data. */ -// Temporary var to use for loops. -$temp_data = ''; - -// Initiate the parser. - $license = $parser->get_modx_license(); $version = $parser->get_modx_mod_version(); $target = $parser->get_modx_target_version(); @@ -63,7 +58,10 @@ while($temp_data = $parser->get_modx_authors()) { - $contributor[] = $temp_data['contributions']; + if (!empty($temp_data['contributions'])) + { + $contributor[] = $temp_data['contributions']; + } unset($temp_data['contributions']); $author[] = $temp_data; } @@ -95,23 +93,31 @@ while($temp_data = $parser->get_modx_meta()) { - $meta[] = $temp_data; + if(!empty($temp_data['content'])) + { + $meta[] = $temp_data; + } } while($temp_data = $parser->get_modx_sql()) { - $temp_data['query'] = $temp_data['data']; - unset($temp_data['data']); - $sql[] = $temp_data; + if(!empty($temp_data['data'])) + { + $temp_data['query'] = $temp_data['data']; + unset($temp_data['data']); + $sql[] = $temp_data; + } } while($temp_data = $parser->get_modx_copy()) { - $copy[0][] = $temp_data; + if(!empty($temp_data['from'])) + { + $copy[0][] = $temp_data; + } } while($temp_data = $parser->get_modx_action()) { $modx[] = $temp_data; } -