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 - 2012 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')) {
46 $GLOBALS['__XML_CONDITIONS'] = array(
48 'NOT-EQUALS' => ' != ',
50 'IS-NOT' => ' IS NOT ',
58 // Calls back a function based on given XML template data
59 function doGenericXmlTemplateCallback ($template, $content = array(), $compileCode = TRUE) {
60 // Init XML system as sch calls will be only used once per run
63 // Generate FQFN for with special path
64 $FQFN = sprintf("%stemplates/xml/%s%s.xml",
66 detectExtraTemplatePath('xml', $template),
70 // Is the file readable?
71 if (!isFileReadable($FQFN)) {
72 // No, use without extra path
73 $FQFN = sprintf("%stemplates/xml/%s.xml",
79 // Is it again readable?
80 if (isFileReadable($FQFN)) {
82 if ((!isDebuggingTemplateCache()) && (isTemplateCached('xml', $template))) {
84 eval(readTemplateCache('xml', $template));
87 $templateContent = readFromFile($FQFN);
89 // Prepare it for finaly eval() command
90 $GLOBALS['template_eval']['xml'][$template] = '$templateContent = decodeEntities("' . compileRawCode(escapeJavaScriptQuotes($templateContent), FALSE, TRUE, TRUE, $compileCode) . '");';
92 // Eval the code, this does insert any array elements from $content
93 eval($GLOBALS['template_eval']['xml'][$template]);
97 $GLOBALS['__XML_CALLBACKS'] = array(
98 'callbacks' => array(),
99 'functions' => array()
101 $GLOBALS['__XML_ARGUMENTS'] = array();
102 $GLOBALS['__COLUMN_INDEX'] = array();
103 $GLOBALS['__XML_CONTENT'] = $content;
105 // Handle it over to the parser
106 parseXmlData($templateContent);
108 // Add special elements, e.g. column index
109 addXmlSpecialElements($template);
111 // Call the call-back function
112 doCallXmlCallbackFunction($content);
114 // Template not found
115 displayMessage('{%message,XML_TEMPLATE_404=' . $template . '%}');
119 // Adds special elements by calling back another template-depending function
120 function addXmlSpecialElements ($template) {
121 // Generate the FQCN (Full-Qualified Callback Name)
122 $FQCN = 'addXmlSpecial' . capitalizeUnderscoreString($template);
125 if (function_exists($FQCN)) {
127 call_user_func($FQCN);
128 } elseif (isDebugModeEnabled()) {
129 // This callback function is only optional
130 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $FQCN . ' for template ' . $template . ' does not exist.');
134 // Parses the XML content
135 function parseXmlData ($content) {
137 if (!function_exists('recode')) {
139 reportBug('PHP extension recode is missing. Please install it.');
142 // Convert HTML entities to UTF-8
143 $content = recode('html..utf8', $content);
145 // Create a new XML parser
146 $xmlParser = xml_parser_create();
148 // Force case-folding to on
149 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, TRUE);
152 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
154 // Set handler call-backs
155 xml_set_element_handler($xmlParser, 'startXmlElement', 'endXmlElement');
156 xml_set_character_data_handler($xmlParser, 'xmlCharacterHandler');
158 // Now parse the XML tree
159 if (!xml_parse($xmlParser, $content)) {
160 // Error found in XML!
161 //* DEBUG: */ die('<pre>'.htmlentities($content).'</pre>');
162 reportBug(__FUNCTION__, __LINE__, 'Error found in XML. errorMessage=' . xml_error_string(xml_get_error_code($xmlParser)) . ', line=' . xml_get_current_line_number($xmlParser));
166 xml_parser_free($xmlParser);
169 // Calls the found call-back function
170 function doCallXmlCallbackFunction () {
171 // Loop through all added entries
172 foreach ($GLOBALS['__XML_CALLBACKS']['callbacks'] as $callback) {
173 // Is there the entry?
174 if ((isset($GLOBALS['__XML_CALLBACKS']['functions'][$callback])) && (isset($GLOBALS['__XML_ARGUMENTS'][$callback]))) {
175 // Run all function callbacks
176 foreach ($GLOBALS['__XML_CALLBACKS']['functions'][$callback] as $function) {
177 // Trim all function names
178 $function = trim($function);
180 // If the function is empty, simply skip to the (maybe) next one
181 if (empty($function)) {
186 // Now construct the call-back function's name with 'Execute' at the end
187 $callbackName = $callback . 'Execute';
190 if (!function_exists($callbackName)) {
191 // No, then please add it
192 reportBug(__FUNCTION__, __LINE__, 'callback=' . $callback . ',function=' . $function . ',arguments()=' . count($GLOBALS['__XML_ARGUMENTS'][$callback]) . ',content()=' . count($GLOBALS['__XML_CONTENT']) . ' - execute call-back does not exist.');
196 call_user_func_array($callbackName, array($function, $GLOBALS['__XML_ARGUMENTS'][$callback], $GLOBALS['__COLUMN_INDEX'][$callback], $GLOBALS['__XML_CONTENT']));
200 reportBug(__FUNCTION__, __LINE__, 'Entry in callbacks does exist, but not in functions, callback= ' . $callback);
205 //-----------------------------------------------------------------------------
206 // Call-back functions for XML parser
207 //-----------------------------------------------------------------------------
210 function startXmlElement ($resource, $element, $attributes) {
211 // Call-back function for given element
212 $elementCallback = 'doXml' . capitalizeUnderscoreString($element);
214 // Is the call-back function there?
215 if (!function_exists($elementCallback)) {
217 reportBug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $elementCallback . ', please add it.');
220 // Call the call-back function
221 call_user_func_array($elementCallback, array($resource, $attributes));
225 function endXmlElement ($resource, $element) {
226 // Out-of-function for now
230 function xmlCharacterHandler ($resource, $characters) {
232 $characters = trim($characters);
234 // Are there some to handle?
235 if (empty($characters)) {
240 // @TODO Handle characters
241 die(__FUNCTION__ . ':characters[]='.strlen($characters));
244 // Checks if given type is valid, makes all lower-case
245 function isInvalidXmlType ($type) {
247 $type = strtolower(trim($type));
250 return (in_array($type, array('string', 'array', 'bool', 'int', 'callback')));
253 // Checks if given condition is valid
254 function isXmlConditionValid ($condition) {
255 // Trim and make lower-case
256 $condition = trim(strtolower($condition));
259 return (in_array($condition, array('equals')));
262 // Checks if given value is valid/verifyable
263 function isXmlValueValid ($type, $value) {
264 // Depends on type, so build a call-back
265 $callbackName = 'isXmlType' . trim(capitalizeUnderscoreString($type));
267 // Is the call-back function there?
268 if (!function_exists($callbackName)) {
270 reportBug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $callbackName . ', please add it.');
273 // Call and return it
274 return call_user_func_array($callbackName, array($value));
277 // Converts given condition into a symbol
278 function convertXmlContion ($condition) {
279 // Detect the condition again
280 if (!isset($GLOBALS['__XML_CONDITIONS'][$condition])) {
281 reportBug(__FUNCTION__, __LINE__, 'Condition ' . $condition . ' is unknown/unsupported.');
285 return $GLOBALS['__XML_CONDITIONS'][$condition];
288 // "Getter" for sql part back from given array
289 function getSqlPartFromXmlArray ($columns) {
293 // Walk through all entries
294 foreach ($columns as $columnArray) {
298 // Is there a table/alias
299 if (!empty($columnArray['table'])) {
301 $sqlPart .= $columnArray['table'] . '.';
305 $sqlPart .= '`' . $columnArray['column'] . '`';
307 // Is a function and alias set?
308 if ((!empty($columnArray['function'])) && (!empty($columnArray['alias']))) {
310 $sqlPart = $columnArray['function'] . '(' . $sqlPart . ') AS `' . $columnArray['alias'] . '`';
313 // Add finished SQL part to the query
314 $SQL .= $sqlPart . ',';
317 // Return it without last commata
318 return substr($SQL, 0, -1);
321 // Searches in given XML array for value and returns the parent index
322 function searchXmlArray ($value, $columns, $childKey) {
323 // Default is not found
326 // Walk through whole array
327 foreach ($columns as $key => $columnArray) {
328 // Make sure the element is there
329 assert(isset($columnArray[$childKey]));
331 // Now is it what we are looking for?
332 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $value . ',key=' . $key . ',childKey=' . $childKey . ',columnArray=' . $columnArray[$childKey]);
333 if ($columnArray[$childKey] === $value) {
334 // Remember this match
337 // And abort any further searches