2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 01/27/2011 *
4 * =================== Last change: 01/27/2011 *
6 * -------------------------------------------------------------------- *
7 * File : xml-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for handling XML templates *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen zum Umgang mit XML-Templates *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2011 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Calls back a function based on given XML template data
44 function showEntriesByXmlCallback ($template) {
45 // Generate FQFN for with special path
46 $FQFN = sprintf("%stemplates/xml/%s%s.xml",
48 detectExtraTemplatePath($template),
52 // Is the file readable?
53 if (!isFileReadable($FQFN)) {
54 // No, use without extra path
55 $FQFN = sprintf("%stemplates/xml/%s.xml",
61 // Is it again readable?
62 if (isFileReadable($FQFN)) {
64 $templateContent = readFromFile($FQFN);
67 $GLOBALS['__XML_CALLBACKS'] = array(
68 'callbacks' => array(),
69 'functions' => array()
71 $GLOBALS['__XML_ARGUMENTS'] = array();
73 // Handle it over to the parser
74 parseXmlData($templateContent);
76 // Call the call-back function
77 doCallXmlCallbackFunction();
80 displayMessage('{%message,XML_TEMPLATE_404=' . $template . '%}');
84 // Parses the XML content
85 function parseXmlData ($content) {
87 if (function_exists('recode')) {
88 // Convert 'HTML' to UTF-8
89 $content = recode('html..utf8', $content);
92 debug_report_bug('PHP extension recode is missing. Please install it.');
95 // Create a new XML parser
96 $xmlParser = xml_parser_create();
98 // Force case-folding to on
99 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
102 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
104 // Set handler call-backs
105 xml_set_element_handler($xmlParser, 'startXmlElement', 'endXmlElement');
106 xml_set_character_data_handler($xmlParser, 'xmlCharacterHandler');
108 // Now parse the XML tree
109 if (!xml_parse($xmlParser, $content)) {
110 // Error found in XML!
111 //* DEBUG: */ die('<pre>'.htmlentities($content).'</pre>');
112 debug_report_bug(__FUNCTION__, __LINE__, 'Error found in XML. errorMessage=' . xml_error_string(xml_get_error_code($xmlParser)) . ', line=' . xml_get_current_line_number($xmlParser));
116 xml_parser_free($xmlParser);
119 // Calls the found call-back function
120 function doCallXmlCallbackFunction () {
121 // Loop through all added entries
122 foreach ($GLOBALS['__XML_CALLBACKS']['callbacks'] as $callback) {
123 // Do we have the entry?
124 if ((isset($GLOBALS['__XML_CALLBACKS']['functions'][$callback])) && (isset($GLOBALS['__XML_ARGUMENTS'][$callback]))) {
125 // Run all function callbacks
126 foreach ($GLOBALS['__XML_CALLBACKS']['functions'][$callback] as $function) {
127 // Trim all function names
128 $function = trim($function);
130 // If the function is empty, simply skip to the (maybe) next one
131 if (empty($function)) {
136 // Now construct the call-back function's name with 'Execute' at the end
137 $callbackName = $callback . 'Execute';
140 if (!function_exists($callbackName)) {
141 debug_report_bug(__FUNCTION__, __LINE__, 'callback=' . $callback . ',function=' . $function . 'arguments()=' . count($GLOBALS['__XML_ARGUMENTS'][$callback]) . ' - execute call-back not found.');
145 call_user_func_array($callbackName, array($function, $GLOBALS['__XML_ARGUMENTS'][$callback]));
149 debug_report_bug(__FUNCTION__, __LINE__, 'Entry in callbacks does exist, but not in functions, callback= ' . $callback);
154 //-----------------------------------------------------------------------------
155 // Call-back functions for XML parser
156 //-----------------------------------------------------------------------------
159 function startXmlElement ($resource, $element, $attributes) {
160 // Call-back function for given element
161 $elementCallback = 'doXml' . capitalizeUnderscoreString($element);
163 // Is the call-back function there?
164 if (!function_exists($elementCallback)) {
166 debug_report_bug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $elementCallback . ', please add it.');
169 // Call the call-back function
170 call_user_func_array($elementCallback, array($resource, $attributes));
174 function endXmlElement ($resource, $element) {
175 // Out-of-function for now
179 function xmlCharacterHandler ($resource, $characters) {
181 $characters = trim($characters);
183 // Do we have some to handle?
184 if (strlen($characters) == 0) {
189 // @TODO Handle characters
190 die(__FUNCTION__ . ':characters[]='.strlen($characters));
193 // Checks if given type is valid, makes all lower-case
194 function isInvalidXmlType ($type) {
196 $type = strtolower(trim($type));
199 return (in_array($type, array('string', 'array', 'bool', 'int', 'callback')));
202 // Checks if given condition is valid
203 function isXmlConditionValid ($condition) {
204 // Trim and make lower-case
205 $condition = trim(strtolower($condition));
208 return (in_array($condition, array('equals')));
211 // Checks if given value is valid/verifyable
212 function isXmlValueValid ($type, $value) {
213 // Depends on type, so build a call-back
214 $callbackFunction = 'isXmlType' . trim(capitalizeUnderscoreString($type));
216 // Is the call-back function there?
217 if (!function_exists($callbackFunction)) {
219 debug_report_bug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $callbackFunction . ', please add it.');
222 // Call and return it
223 return call_user_func_array($callbackFunction, array($value));
226 // Converts given condition into a symbol
227 function convertXmlContion ($condition) {
228 // Default is an invalid one
231 // Detect the condition again
232 switch ($condition) {
233 case 'EQUALS': // Equals
237 default: // Unknown condition
238 debug_report_bug(__FUNCTION__, __LINE__, 'Condition ' . $condition . ' is unknown/unsupported.');
246 // "Getter" for sql part back from given array
247 function getSqlPartFromXmlArray ($columns) {
251 // Walk through all entries
252 foreach ($columns as $columnArray) {
256 // Do we have a table/alias
257 if (!empty($columnArray['table'])) {
259 $sqlPart .= $columnArray['table'] . '.';
263 $sqlPart .= '`' . $columnArray['column'] . '`';
265 // Is a function and alias set?
266 if ((!empty($columnArray['function'])) && (!empty($columnArray['alias']))) {
268 $sqlPart = $columnArray['function'] . '(' . $sqlPart . ') AS `' . $columnArray['alias'] . '`';
271 // Add finished SQL part to the query
272 $SQL .= $sqlPart . ',';
275 // Return it without last commata
276 return substr($SQL, 0, -1);
279 // Searches in given XML array for value and returns the parent index
280 function searchXmlArray ($value, $columns, $childKey) {
281 // Default is not found
284 // Walk through whole array
285 foreach ($columns as $key => $columnArray) {
286 // Make sure the element is there
287 assert(isset($columnArray[$childKey]));
289 // Now is it what we are looking for?
290 if ($columnArray[$childKey] == $value) {
291 // Remember this match
294 // And abort any further searches