04550bc6da6afa1f1901b1efb6bbd93ff1cdc5b4
[mailer.git] / inc / xml-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 01/27/2011 *
4  * ===================                          Last change: 01/27/2011 *
5  *                                                                      *
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  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
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.                                  *
26  *                                                                      *
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.                         *
31  *                                                                      *
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,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
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",
47                 getPath(),
48                 detectExtraTemplatePath($template),
49                 $template
50         );
51
52         // Is the file readable?
53         if (!isFileReadable($FQFN)) {
54                 // No, use without extra path
55                 $FQFN = sprintf("%stemplates/xml/%s.xml",
56                         getPath(),
57                         $template
58                 );
59         } // END - if
60
61         // Is it again readable?
62         if (isFileReadable($FQFN)) {
63                 // Read it
64                 $templateContent = readFromFile($FQFN);
65
66                 // Handle it over to the parser
67                 parseXmlData($templateContent);
68
69                 // Call the call-back function
70                 doCallXmlCallbackFunction();
71         } else {
72                 // Template not found
73                 displayMessage('{%message,XML_TEMPLATE_404=' . $template . '%}');
74         }
75 }
76
77 // Parses the XML content
78 function parseXmlData ($content) {
79         // Do we have recode?
80         if (function_exists('recode')) {
81                 // Convert 'HTML' to UTF-8
82                 $content = recode('html..utf8', $content);
83         } else {
84                 // No fallback ATM
85                 debug_report_bug('PHP extension recode is missing. Please install it.');
86         }
87
88         // Create a new XML parser
89         $xmlParser = xml_parser_create();
90
91         // Force case-folding to on
92         xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
93
94         // Set UTF-8
95         xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
96
97         // Set handler call-backs
98         xml_set_element_handler($xmlParser, 'startXmlElement', 'endXmlElement');
99         xml_set_character_data_handler($xmlParser, 'xmlCharacterHandler');
100
101         // Now parse the XML tree
102         if (!xml_parse($xmlParser, $content)) {
103                 // Error found in XML!
104                 //* DEBUG: */ die('<pre>'.htmlentities($content).'</pre>');
105                 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));
106         } // END - if
107
108         // Free the parser
109         xml_parser_free($xmlParser);
110 }
111
112 // Calls the found call-back function
113 function doCallXmlCallbackFunction () {
114         // Loop through all added entries
115         foreach ($GLOBALS['__XML_CALLBACKS']['callbacks'] as $callback) {
116                 // Do we have the entry?
117                 if ((isset($GLOBALS['__XML_CALLBACKS']['functions'][$callback])) && (isset($GLOBALS['__XML_ARGUMENTS'][$callback]))) {
118                         // Run all function callbacks
119                         foreach ($GLOBALS['__XML_CALLBACKS']['functions'][$callback] as $function) {
120                                 // Now construct the call-back function's name with 'Execute' at the end
121                                 $callbackName = $callback . 'Execute';
122
123                                 // Is it there?
124                                 if (!function_exists($callbackName)) {
125                                         debug_report_bug(__FUNCTION__, __LINE__, 'callback=' . $callback . ',function=' . $function . 'arguments()=' . count($GLOBALS['__XML_ARGUMENTS'][$callback]) . ' - execute call-back not found.');
126                                 } // END - if
127
128                                 // Call it
129                                 call_user_func_array($callbackName, array($function, $GLOBALS['__XML_ARGUMENTS'][$callback]));
130                         } // END - foreach
131                 } else {
132                         // Not found
133                         debug_report_bug(__FUNCTION__, __LINE__, 'Entry in callbacks does exist, but not in functions, callback= ' . $callback);
134                 }
135         } // END - foreach
136 }
137
138 // ----------------------------------------------------------------------------
139 //                     Call-back functions for XML parser
140 // ----------------------------------------------------------------------------
141
142 // Starts an element
143 function startXmlElement ($resource, $element, $attributes) {
144         // Call-back function for given element
145         $elementCallback = 'doXml' . capitalizeUnderscoreString($element);
146
147         // Is the call-back function there?
148         if (!function_exists($elementCallback)) {
149                 // Not there
150                 debug_report_bug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $elementCallback . ', please add it.');
151         } // END - if
152
153         // Call the call-back function
154         call_user_func_array($elementCallback, array($resource, $attributes));
155 }
156
157 // Ends an element
158 function endXmlElement ($resource, $element) {
159         // Out-of-function for now
160 }
161
162 // Handle characters
163 function xmlCharacterHandler ($resource, $characters) {
164         // Trim spaces away
165         $characters = trim($characters);
166
167         // Do we have some to handle?
168         if (strlen($characters) == 0) {
169                 // Nothing to handle
170                 return;
171         } // END - if
172         die('characters[]='.strlen($characters));
173 }
174
175 // Checks if given type is valid, makes all lower-case
176 function isInvalidXmlType ($type) {
177         // All lower-case
178         $type = strtolower(trim($type));
179
180         // Is it found?
181         return (in_array($type, array('string', 'array', 'bool')));
182 }
183
184 // Checks if given value is valid/verifyable
185 function isXmlValueValid ($type, $value) {
186         // Depends on type, so build a call-back
187         $callbackFunction = 'isXmlType' . trim(capitalizeUnderscoreString($type));
188
189         // Is the call-back function there?
190         if (!function_exists($callbackFunction)) {
191                 // Not there
192                 debug_report_bug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $callbackFunction . ', please add it.');
193         } // END - if
194
195         // Call and return it
196         return call_user_func_array($callbackFunction, array($value));
197 }
198
199 // [EOF]
200 ?>