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();
104 // Handle it over to the parser
105 parseXmlData($templateContent);
107 // Add special elements, e.g. column index
108 addXmlSpecialElements($template);
110 // Call the call-back function
111 doCallXmlCallbackFunction();
113 // Template not found
114 displayMessage('{%message,XML_TEMPLATE_404=' . $template . '%}');
118 // Adds special elements by calling back another template-depending function
119 function addXmlSpecialElements ($template) {
120 // Generate the FQCN (Full-Qualified Callback Name)
121 $FQCN = 'addXmlSpecial' . capitalizeUnderscoreString($template);
124 if (function_exists($FQCN)) {
126 call_user_func($FQCN);
127 } elseif (isDebugModeEnabled()) {
128 // This callback function is only optional
129 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $FQCN . ' for template ' . $template . ' does not exist.');
133 // Parses the XML content
134 function parseXmlData ($content) {
136 if (!function_exists('recode')) {
138 reportBug('PHP extension recode is missing. Please install it.');
141 // Convert HTML entities to UTF-8
142 $content = recode('html..utf8', $content);
144 // Create a new XML parser
145 $xmlParser = xml_parser_create();
147 // Force case-folding to on
148 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, TRUE);
151 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
153 // Set handler call-backs
154 xml_set_element_handler($xmlParser, 'startXmlElement', 'endXmlElement');
155 xml_set_character_data_handler($xmlParser, 'xmlCharacterHandler');
157 // Now parse the XML tree
158 if (!xml_parse($xmlParser, $content)) {
159 // Error found in XML!
160 //* DEBUG: */ die('<pre>'.htmlentities($content).'</pre>');
161 reportBug(__FUNCTION__, __LINE__, 'Error found in XML. errorMessage=' . xml_error_string(xml_get_error_code($xmlParser)) . ', line=' . xml_get_current_line_number($xmlParser));
165 xml_parser_free($xmlParser);
168 // Calls the found call-back function
169 function doCallXmlCallbackFunction () {
170 // Loop through all added entries
171 foreach ($GLOBALS['__XML_CALLBACKS']['callbacks'] as $callback) {
172 // Is there the entry?
173 if ((isset($GLOBALS['__XML_CALLBACKS']['functions'][$callback])) && (isset($GLOBALS['__XML_ARGUMENTS'][$callback]))) {
174 // Run all function callbacks
175 foreach ($GLOBALS['__XML_CALLBACKS']['functions'][$callback] as $function) {
176 // Trim all function names
177 $function = trim($function);
179 // If the function is empty, simply skip to the (maybe) next one
180 if (empty($function)) {
185 // Now construct the call-back function's name with 'Execute' at the end
186 $callbackName = $callback . 'Execute';
189 if (!function_exists($callbackName)) {
190 // No, then please add it
191 reportBug(__FUNCTION__, __LINE__, 'callback=' . $callback . ',function=' . $function . 'arguments()=' . count($GLOBALS['__XML_ARGUMENTS'][$callback]) . ' - execute call-back does not exist.');
195 call_user_func_array($callbackName, array($function, $GLOBALS['__XML_ARGUMENTS'][$callback], $GLOBALS['__COLUMN_INDEX'][$callback]));
199 reportBug(__FUNCTION__, __LINE__, 'Entry in callbacks does exist, but not in functions, callback= ' . $callback);
204 //-----------------------------------------------------------------------------
205 // Call-back functions for XML parser
206 //-----------------------------------------------------------------------------
209 function startXmlElement ($resource, $element, $attributes) {
210 // Call-back function for given element
211 $elementCallback = 'doXml' . capitalizeUnderscoreString($element);
213 // Is the call-back function there?
214 if (!function_exists($elementCallback)) {
216 reportBug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $elementCallback . ', please add it.');
219 // Call the call-back function
220 call_user_func_array($elementCallback, array($resource, $attributes));
224 function endXmlElement ($resource, $element) {
225 // Out-of-function for now
229 function xmlCharacterHandler ($resource, $characters) {
231 $characters = trim($characters);
233 // Are there some to handle?
234 if (strlen($characters) == 0) {
239 // @TODO Handle characters
240 die(__FUNCTION__ . ':characters[]='.strlen($characters));
243 // Checks if given type is valid, makes all lower-case
244 function isInvalidXmlType ($type) {
246 $type = strtolower(trim($type));
249 return (in_array($type, array('string', 'array', 'bool', 'int', 'callback')));
252 // Checks if given condition is valid
253 function isXmlConditionValid ($condition) {
254 // Trim and make lower-case
255 $condition = trim(strtolower($condition));
258 return (in_array($condition, array('equals')));
261 // Checks if given value is valid/verifyable
262 function isXmlValueValid ($type, $value) {
263 // Depends on type, so build a call-back
264 $callbackName = 'isXmlType' . trim(capitalizeUnderscoreString($type));
266 // Is the call-back function there?
267 if (!function_exists($callbackName)) {
269 reportBug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $callbackName . ', please add it.');
272 // Call and return it
273 return call_user_func_array($callbackName, array($value));
276 // Converts given condition into a symbol
277 function convertXmlContion ($condition) {
278 // Detect the condition again
279 if (!isset($GLOBALS['__XML_CONDITIONS'][$condition])) {
280 reportBug(__FUNCTION__, __LINE__, 'Condition ' . $condition . ' is unknown/unsupported.');
284 return $GLOBALS['__XML_CONDITIONS'][$condition];
287 // "Getter" for sql part back from given array
288 function getSqlPartFromXmlArray ($columns) {
292 // Walk through all entries
293 foreach ($columns as $columnArray) {
297 // Is there a table/alias
298 if (!empty($columnArray['table'])) {
300 $sqlPart .= $columnArray['table'] . '.';
304 $sqlPart .= '`' . $columnArray['column'] . '`';
306 // Is a function and alias set?
307 if ((!empty($columnArray['function'])) && (!empty($columnArray['alias']))) {
309 $sqlPart = $columnArray['function'] . '(' . $sqlPart . ') AS `' . $columnArray['alias'] . '`';
312 // Add finished SQL part to the query
313 $SQL .= $sqlPart . ',';
316 // Return it without last commata
317 return substr($SQL, 0, -1);
320 // Searches in given XML array for value and returns the parent index
321 function searchXmlArray ($value, $columns, $childKey) {
322 // Default is not found
325 // Walk through whole array
326 foreach ($columns as $key => $columnArray) {
327 // Make sure the element is there
328 assert(isset($columnArray[$childKey]));
330 // Now is it what we are looking for?
331 if ($columnArray[$childKey] === $value) {
332 // Remember this match
335 // And abort any further searches