]> git.mxchange.org Git - mailer.git/blobdiff - inc/xml-functions.php
Some text shorter, alternative text added to forced-ads table row
[mailer.git] / inc / xml-functions.php
index 94a9b6a0ed43f82e04506fdd51056054575456d8..09373597f1b5e8b1e8aed563caeca0264cfda4e3 100644 (file)
@@ -40,5 +40,206 @@ if (!defined('__SECURITY')) {
        die();
 } // END - if
 
+// Calls back a function based on given XML template data
+function showEntriesByXmlCallback ($template) {
+       // Generate FQFN for with special path
+       $FQFN = sprintf("%stemplates/xml/%s%s.xml",
+               getPath(),
+               detectExtraTemplatePath($template),
+               $template
+       );
+
+       // Is the file readable?
+       if (!isFileReadable($FQFN)) {
+               // No, use without extra path
+               $FQFN = sprintf("%stemplates/xml/%s.xml",
+                       getPath(),
+                       $template
+               );
+       } // END - if
+
+       // Is it again readable?
+       if (isFileReadable($FQFN)) {
+               // Read it
+               $templateContent = readFromFile($FQFN);
+
+               // Init main arrays
+               $GLOBALS['__XML_CALLBACKS'] = array(
+                       'callbacks' => array(),
+                       'functions' => array()
+               );
+               $GLOBALS['__XML_ARGUMENTS'] = array();
+
+               // Handle it over to the parser
+               parseXmlData($templateContent);
+
+               // Call the call-back function
+               doCallXmlCallbackFunction();
+       } else {
+               // Template not found
+               displayMessage('{%message,XML_TEMPLATE_404=' . $template . '%}');
+       }
+}
+
+// Parses the XML content
+function parseXmlData ($content) {
+       // Do we have recode?
+       if (function_exists('recode')) {
+               // Convert 'HTML' to UTF-8
+               $content = recode('html..utf8', $content);
+       } else {
+               // No fallback ATM
+               debug_report_bug('PHP extension recode is missing. Please install it.');
+       }
+
+       // Create a new XML parser
+       $xmlParser = xml_parser_create();
+
+       // Force case-folding to on
+       xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
+
+       // Set UTF-8
+       xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
+
+       // Set handler call-backs
+       xml_set_element_handler($xmlParser, 'startXmlElement', 'endXmlElement');
+       xml_set_character_data_handler($xmlParser, 'xmlCharacterHandler');
+
+       // Now parse the XML tree
+       if (!xml_parse($xmlParser, $content)) {
+               // Error found in XML!
+               //* DEBUG: */ die('<pre>'.htmlentities($content).'</pre>');
+               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));
+       } // END - if
+
+       // Free the parser
+       xml_parser_free($xmlParser);
+}
+
+// Calls the found call-back function
+function doCallXmlCallbackFunction () {
+       // Loop through all added entries
+       foreach ($GLOBALS['__XML_CALLBACKS']['callbacks'] as $callback) {
+               // Do we have the entry?
+               if ((isset($GLOBALS['__XML_CALLBACKS']['functions'][$callback])) && (isset($GLOBALS['__XML_ARGUMENTS'][$callback]))) {
+                       // Run all function callbacks
+                       foreach ($GLOBALS['__XML_CALLBACKS']['functions'][$callback] as $function) {
+                               // Trim all function names
+                               $function = trim($function);
+
+                               // If the function is empty, simply skip to the (maybe) next one
+                               if (empty($function)) {
+                                       // Skip this
+                                       continue;
+                               } // END - if
+
+                               // Now construct the call-back function's name with 'Execute' at the end
+                               $callbackName = $callback . 'Execute';
+
+                               // Is it there?
+                               if (!function_exists($callbackName)) {
+                                       debug_report_bug(__FUNCTION__, __LINE__, 'callback=' . $callback . ',function=' . $function . 'arguments()=' . count($GLOBALS['__XML_ARGUMENTS'][$callback]) . ' - execute call-back not found.');
+                               } // END - if
+
+                               // Call it
+                               call_user_func_array($callbackName, array($function, $GLOBALS['__XML_ARGUMENTS'][$callback]));
+                       } // END - foreach
+               } else {
+                       // Not found
+                       debug_report_bug(__FUNCTION__, __LINE__, 'Entry in callbacks does exist, but not in functions, callback= ' . $callback);
+               }
+       } // END - foreach
+}
+
+//-----------------------------------------------------------------------------
+//                     Call-back functions for XML parser
+//-----------------------------------------------------------------------------
+
+// Starts an element
+function startXmlElement ($resource, $element, $attributes) {
+       // Call-back function for given element
+       $elementCallback = 'doXml' . capitalizeUnderscoreString($element);
+
+       // Is the call-back function there?
+       if (!function_exists($elementCallback)) {
+               // Not there
+               debug_report_bug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $elementCallback . ', please add it.');
+       } // END - if
+
+       // Call the call-back function
+       call_user_func_array($elementCallback, array($resource, $attributes));
+}
+
+// Ends an element
+function endXmlElement ($resource, $element) {
+       // Out-of-function for now
+}
+
+// Handle characters
+function xmlCharacterHandler ($resource, $characters) {
+       // Trim spaces away
+       $characters = trim($characters);
+
+       // Do we have some to handle?
+       if (strlen($characters) == 0) {
+               // Nothing to handle
+               return;
+       } // END - if
+       die('characters[]='.strlen($characters));
+}
+
+// Checks if given type is valid, makes all lower-case
+function isInvalidXmlType ($type) {
+       // All lower-case
+       $type = strtolower(trim($type));
+
+       // Is it found?
+       return (in_array($type, array('string', 'array', 'bool', 'int', 'callback')));
+}
+
+// Checks if given condition is valid
+function isXmlConditionValid ($condition) {
+       // Trim and make lower-case
+       $condition = trim(strtolower($condition));
+
+       // Is it valid?
+       return (in_array($condition, array('equals')));
+}
+
+// Checks if given value is valid/verifyable
+function isXmlValueValid ($type, $value) {
+       // Depends on type, so build a call-back
+       $callbackFunction = 'isXmlType' . trim(capitalizeUnderscoreString($type));
+
+       // Is the call-back function there?
+       if (!function_exists($callbackFunction)) {
+               // Not there
+               debug_report_bug(__FUNCTION__, __LINE__, 'Missing call-back function ' . $callbackFunction . ', please add it.');
+       } // END - if
+
+       // Call and return it
+       return call_user_func_array($callbackFunction, array($value));
+}
+
+// Converts given condition into a symbol
+function convertXmlContion ($condition) {
+       // Default is an invalid one
+       $return = '???';
+
+       // Detect the condition again
+       switch ($condition) {
+               case 'EQUALS': // Equals
+                       $return = '=';
+                       break;
+
+               default: // Unknown condition
+                       debug_report_bug(__FUNCTION__, __LINE__, 'Condition ' . $condition . ' is unknown/unsupported.');
+                       break;
+       } // END - switch
+
+       // Return it
+       return $return;
+}
+
 // [EOF]
 ?>