punbb/include/xml.php
changeset 6 5e1f1e916419
equal deleted inserted replaced
5:e3d7322305bf 6:5e1f1e916419
       
     1 <?php
       
     2 /***********************************************************************
       
     3 
       
     4   Copyright (C) 2002-2008  PunBB.org
       
     5 
       
     6   This file is part of PunBB.
       
     7 
       
     8   PunBB is free software; you can redistribute it and/or modify it
       
     9   under the terms of the GNU General Public License as published
       
    10   by the Free Software Foundation; either version 2 of the License,
       
    11   or (at your option) any later version.
       
    12 
       
    13   PunBB is distributed in the hope that it will be useful, but
       
    14   WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16   GNU General Public License for more details.
       
    17 
       
    18   You should have received a copy of the GNU General Public License
       
    19   along with this program; if not, write to the Free Software
       
    20   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
       
    21   MA  02111-1307  USA
       
    22 
       
    23 ************************************************************************/
       
    24 
       
    25 
       
    26 // Make sure no one attempts to run this script "directly"
       
    27 if (!defined('PUN'))
       
    28 	exit;
       
    29 
       
    30 
       
    31 //
       
    32 // Parse XML data into an array
       
    33 //
       
    34 function xml_to_array($raw_xml)
       
    35 {
       
    36 	$xml_parser = xml_parser_create();
       
    37 	xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
       
    38 	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 0);
       
    39 	xml_parse_into_struct($xml_parser, $raw_xml, $vals);
       
    40 	xml_parser_free($xml_parser);
       
    41 
       
    42 	$_tmp = '';
       
    43 	foreach ($vals as $xml_elem)
       
    44 	{
       
    45 		$x_tag = $xml_elem['tag'];
       
    46 		$x_level = $xml_elem['level'];
       
    47 		$x_type = $xml_elem['type'];
       
    48 
       
    49 		if ($x_level != 1 && $x_type == 'close')
       
    50 		{
       
    51 			if (isset($multi_key[$x_tag][$x_level]))
       
    52 				$multi_key[$x_tag][$x_level] = 1;
       
    53 			else
       
    54 				$multi_key[$x_tag][$x_level] = 0;
       
    55 		}
       
    56 
       
    57 		if ($x_level != 1 && $x_type == 'complete')
       
    58 		{
       
    59 			if ($_tmp == $x_tag)
       
    60 				$multi_key[$x_tag][$x_level] = 1;
       
    61 
       
    62 			$_tmp = $x_tag;
       
    63 		}
       
    64 	}
       
    65 
       
    66 	foreach ($vals as $xml_elem)
       
    67 	{
       
    68 		$x_tag = $xml_elem['tag'];
       
    69 		$x_level = $xml_elem['level'];
       
    70 		$x_type = $xml_elem['type'];
       
    71 
       
    72 		if ($x_type == 'open')
       
    73 			$level[$x_level] = $x_tag;
       
    74 
       
    75 		$start_level = 1;
       
    76 		$php_stmt = '$xml_array';
       
    77 		if ($x_type == 'close' && $x_level != 1)
       
    78 			$multi_key[$x_tag][$x_level]++;
       
    79 
       
    80 		while ($start_level < $x_level)
       
    81 		{
       
    82 			$php_stmt .= '[$level['.$start_level.']]';
       
    83 			if (isset($multi_key[$level[$start_level]][$start_level]) && $multi_key[$level[$start_level]][$start_level])
       
    84 				$php_stmt .= '['.($multi_key[$level[$start_level]][$start_level]-1).']';
       
    85 
       
    86 			++$start_level;
       
    87 		}
       
    88 
       
    89 		$add = '';
       
    90 		if (isset($multi_key[$x_tag][$x_level]) && $multi_key[$x_tag][$x_level] && ($x_type == 'open' || $x_type == 'complete'))
       
    91 		{
       
    92 			if (!isset($multi_key2[$x_tag][$x_level]))
       
    93 				$multi_key2[$x_tag][$x_level] = 0;
       
    94 			else
       
    95 				$multi_key2[$x_tag][$x_level]++;
       
    96 
       
    97 			$add = '['.$multi_key2[$x_tag][$x_level].']';
       
    98 		}
       
    99 
       
   100 		if (isset($xml_elem['value']) && trim($xml_elem['value']) != '' && !array_key_exists('attributes', $xml_elem))
       
   101 		{
       
   102 			if ($x_type == 'open')
       
   103 				$php_stmt_main = $php_stmt.'[$x_type]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
       
   104 			else
       
   105 				$php_stmt_main = $php_stmt.'[$x_tag]'.$add.' = $xml_elem[\'value\'];';
       
   106 
       
   107 			eval($php_stmt_main);
       
   108 		}
       
   109 
       
   110 		if (array_key_exists('attributes', $xml_elem))
       
   111 		{
       
   112 			if (isset($xml_elem['value']))
       
   113 			{
       
   114 				$php_stmt_main = $php_stmt.'[$x_tag]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
       
   115 				eval($php_stmt_main);
       
   116 			}
       
   117 
       
   118 			foreach ($xml_elem['attributes'] as $key=>$value)
       
   119 			{
       
   120 				$php_stmt_att=$php_stmt.'[$x_tag]'.$add.'[\'attributes\'][$key] = $value;';
       
   121 				eval($php_stmt_att);
       
   122 			}
       
   123 		}
       
   124 	}
       
   125 
       
   126 	if (isset($xml_array))
       
   127 	{
       
   128 		// Make sure there's an array of notes (even if there is only one)
       
   129 		if (isset($xml_array['extension']['note']))
       
   130 		{
       
   131 			if (!is_array(current($xml_array['extension']['note'])))
       
   132 				$xml_array['extension']['note'] = array($xml_array['extension']['note']);
       
   133 		}
       
   134 		else
       
   135 			$xml_array['extension']['note'] = array();
       
   136 
       
   137 		// Make sure there's an array of hooks (even if there is only one)
       
   138 		if (isset($xml_array['extension']['hooks']) && isset($xml_array['extension']['hooks']['hook']))
       
   139 		{
       
   140 			if (!is_array(current($xml_array['extension']['hooks']['hook'])))
       
   141 				$xml_array['extension']['hooks']['hook'] = array($xml_array['extension']['hooks']['hook']);
       
   142 		}
       
   143 	}
       
   144 
       
   145 	return isset($xml_array) ? $xml_array : array();
       
   146 }
       
   147 
       
   148 
       
   149 //
       
   150 // Validate the syntax of an extension manifest file
       
   151 //
       
   152 function validate_manifest($xml_array, $folder_name)
       
   153 {
       
   154 	global $lang_admin, $pun_config;
       
   155 
       
   156 	$errors = array();
       
   157 
       
   158 	if (!isset($xml_array['extension']) || !is_array($xml_array['extension']))
       
   159 		$errors[] = $lang_admin['extension root error'];
       
   160 	else
       
   161 	{
       
   162 		$ext = $xml_array['extension'];
       
   163 		if (!isset($ext['attributes']['engine']))
       
   164 			$errors[] = $lang_admin['extension/engine error'];
       
   165 		else if ($ext['attributes']['engine'] != '1.0')
       
   166 			$errors[] = $lang_admin['extension/engine error2'];
       
   167 		if (!isset($ext['id']) || $ext['id'] == '')
       
   168 			$errors[] = $lang_admin['extension/id error'];
       
   169 		if ($ext['id'] != $folder_name)
       
   170 			$errors[] = $lang_admin['extension/id error2'];
       
   171 		if (!isset($ext['title']) || $ext['title'] == '')
       
   172 			$errors[] = $lang_admin['extension/title error'];
       
   173 		if (!isset($ext['version']) || $ext['version'] == '')
       
   174 			$errors[] = $lang_admin['extension/version error'];
       
   175 		if (!isset($ext['description']) || $ext['description'] == '')
       
   176 			$errors[] = $lang_admin['extension/description error'];
       
   177 		if (!isset($ext['author']) || $ext['author'] == '')
       
   178 			$errors[] = $lang_admin['extension/author error'];
       
   179 		if (!isset($ext['minversion']) || $ext['minversion'] == '')
       
   180 			$errors[] = $lang_admin['extension/minversion error'];
       
   181 		if (isset($ext['minversion']) && version_compare(clean_version($pun_config['o_cur_version']), clean_version($ext['minversion']), '<'))
       
   182 			$errors[] = sprintf($lang_admin['extension/minversion error2'], $ext['minversion']);
       
   183 		if (!isset($ext['maxtestedon']) || $ext['maxtestedon'] == '')
       
   184 			$errors[] = $lang_admin['extension/maxtestedon error'];
       
   185 		if (isset($ext['note']))
       
   186 		{
       
   187 			foreach ($ext['note'] as $note)
       
   188 			{
       
   189 				if (!isset($note['content']) || $note['content'] == '')
       
   190 					$errors[] = $lang_admin['extension/note error'];
       
   191 				if (!isset($note['attributes']['type']) || $note['attributes']['type'] == '')
       
   192 					$errors[] = $lang_admin['extension/note error2'];
       
   193 			}
       
   194 		}
       
   195 		if (isset($ext['hooks']) && is_array($ext['hooks']))
       
   196 		{
       
   197 			if (!isset($ext['hooks']['hook']) || !is_array($ext['hooks']['hook']))
       
   198 				$errors[] = $lang_admin['extension/hooks/hook error'];
       
   199 			else
       
   200 			{
       
   201 				foreach ($ext['hooks']['hook'] as $hook)
       
   202 				{
       
   203 					if (!isset($hook['content']) || $hook['content'] == '')
       
   204 						$errors[] = $lang_admin['extension/hooks/hook error'];
       
   205 					if (!isset($hook['attributes']['id']) || $hook['attributes']['id'] == '')
       
   206 						$errors[] = $lang_admin['extension/hooks/hook error2'];
       
   207 				}
       
   208 			}
       
   209 		}
       
   210 	}
       
   211 
       
   212 	return $errors;
       
   213 }