Possible fix for JavaScript templates #2
authorRoland Häder <roland@mxchange.org>
Fri, 6 Nov 2009 16:14:15 +0000 (16:14 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 6 Nov 2009 16:14:15 +0000 (16:14 +0000)
inc/filters.php
inc/functions.php
inc/wrapper-functions.php
js.php

index 90478a949f36383aa99488d420c6249a4b4d63d2..40fb713d99717057726f740728e1541e1455260b 100644 (file)
@@ -850,7 +850,7 @@ function FILTER_FLUSH_TEMPLATE_CACHE () {
                // Now flush all
                foreach ($GLOBALS['template_eval'] as $template=>$eval) {
                        // Flush the cache (if not yet found)
-                       flushTemplateCache($template, $eval);
+                       //flushTemplateCache($template, $eval);
                } // END - if
        } // END - if
 }
index 7ef9f2e04176de185abf81c2402184cb2b619a99..ee6f261cc15f48e0e3324a9ba91cbd7bb3529b6a 100644 (file)
@@ -184,7 +184,7 @@ function compileFinalOutput () {
 // Output the raw HTML code
 function outputRawCode ($htmlCode) {
        // Output stripped HTML code to avoid broken JavaScript code, etc.
-       print($htmlCode);
+       print(str_replace('{BACK}', "\\", $htmlCode));
 
        // Flush the output if only getPhpCaching() is not 'on'
        if (getPhpCaching() != 'on') {
@@ -288,20 +288,20 @@ function loadTemplate ($template, $return = false, $content = array()) {
                                // Normal HTML output?
                                if (getOutputMode() == '0') {
                                        // Add surrounding HTML comments to help finding bugs faster
-                                       $ret = "<!-- Template " . $template . " - Start -->\n" . $GLOBALS['tpl_content'] . "<!-- Template " . $template . " - End -->\n";
+                                       $ret = '<!-- Template ' . $template . " - Start -->\n" . $GLOBALS['tpl_content'] . '<!-- Template ' . $template . " - End -->\n";
 
                                        // Prepare eval() command
                                        $eval = '$ret = "' . compileCode(escapeQuotes($ret)) . '";';
                                } elseif (substr($template, 0, 3) == 'js_') {
                                        // JavaScripts don't like entities and timings
-                                       $eval = '$ret = decodeEntities("' . compileRawCode(addslashes($GLOBALS['tpl_content'])) . '");';
+                                       $eval = '$ret = decodeEntities("' . compileRawCode(escapeJavaScriptQuotes($GLOBALS['tpl_content'])) . '");';
                                } else {
-                                       // Prepare eval() command
-                                       $eval = '$ret = "' . compileCode(escapeQuotes($GLOBALS['tpl_content'])) . '";';
+                                       // Prepare eval() command, other output doesn't like entities, maybe
+                                       $eval = '$ret = decodeEntities("' . compileRawCode(escapeQuotes($GLOBALS['tpl_content'])) . '");';
                                }
                        } else {
                                // Add surrounding HTML comments to help finding bugs faster
-                               $ret = "<!-- Template " . $template . " - Start -->\n" . $GLOBALS['tpl_content'] . "<!-- Template " . $template . " - End -->\n";
+                               $ret = '<!-- Template ' . $template . " - Start -->\n" . $GLOBALS['tpl_content'] . '<!-- Template ' . $template . " - End -->\n";
                                $eval = '$ret = "' . escapeQuotes($ret) . '";';
                        } // END - if
 
@@ -1076,13 +1076,13 @@ function compileRawCode ($code, $simple = false, $constants = true, $full = true
                                // Replace it in the code
                                //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):key={$key},match={$match}<br />");
                                $newMatch = str_replace('[', "['", str_replace(']', "']", $match));
-                               $code = str_replace($match, "\"." . $newMatch.".\"", $code);
+                               $code = str_replace($match, '".' . $newMatch . '."', $code);
                                $matchesFound[$key . '_' . $matches[4][$key]] = 1;
                                $matchesFound[$match] = 1;
                        } elseif (!isset($matchesFound[$match])) {
                                // Not yet replaced!
                                //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):match={$match}<br />");
-                               $code = str_replace($match, "\"." . $match.".\"", $code);
+                               $code = str_replace($match, '".' . $match . '."', $code);
                                $matchesFound[$match] = 1;
                        }
                } // END - foreach
@@ -3692,6 +3692,15 @@ function escapeQuotes ($str, $single = false) {
        return $str;
 }
 
+// Escapes the JavaScript code, prevents \r and \n becoming char 10/13
+function escapeJavaScriptQuotes ($str) {
+       // Replace all double-quotes and secure back-ticks
+       $str = str_replace('"', '\"', str_replace("\\", '{BACK}', $str));
+
+       // Return it
+       return $str;
+}
+
 //////////////////////////////////////////////////
 // AUTOMATICALLY RE-GENERATED MISSING FUNCTIONS //
 //////////////////////////////////////////////////
@@ -3707,7 +3716,7 @@ if (!function_exists('html_entity_decode')) {
 
 if (!function_exists('http_build_query')) {
        // Taken from documentation on www.php.net, credits to Marco K. (Germany)
-       function http_build_query($data, $prefix='', $sep='', $key='') {
+       function http_build_query($data, $prefix = '', $sep = '', $key = '') {
                $ret = array();
                foreach ((array)$data as $k => $v) {
                        if (is_int($k) && $prefix != null) {
@@ -3727,7 +3736,7 @@ if (!function_exists('http_build_query')) {
 
                return implode($sep, $ret);
        }
-}// // END - if
+} // END - if
 
 // [EOF]
 ?>
index 64ac4a9660af8fd81e9d710b354aabeb447ae24e..13a0fce739768900e76644b92ae6df18134708f4 100644 (file)
@@ -138,9 +138,9 @@ function decodeString ($str, $decompress = true) {
 }
 
 // Decode entities in a nicer way
-function decodeEntities ($str) {
+function decodeEntities ($str, $quote = ENT_NOQUOTES) {
        // Decode the entities to UTF-8 now
-       $decodedString = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8');
+       $decodedString = html_entity_decode($str, $quote, 'UTF-8');
 
        // Return decoded string
        return $decodedString;
diff --git a/js.php b/js.php
index c4e57f9ae3fecd64437fb4beda96f13a6ccc9548..38548c2cd47d63f427fafa6b4891a24eeda08b96 100644 (file)
--- a/js.php
+++ b/js.php
@@ -36,6 +36,9 @@
  * MA  02110-1301  USA                                                  *
  ************************************************************************/
 
+// XDEBUG call
+//* DEBUG: */ xdebug_start_trace();
+
 // Load security stuff here
 require('inc/libs/security_functions.php');