[TASK] Cleanup DebugUtility
authorHelmut Hummel <helmut.hummel@typo3.org>
Thu, 1 Nov 2012 23:45:09 +0000 (00:45 +0100)
committerHelmut Hummel <helmut.hummel@typo3.org>
Thu, 1 Nov 2012 23:59:37 +0000 (00:59 +0100)
The debug utility outputs JavaScript, even when in CLI mode.
Additionally all strings are not correctly encoded for
JavaScript.

Additionally deprecate the method which does incomplete encoding.

Releases: 6.0
Resolves: #42611

Change-Id: I68fadc5d8f10e967913d1b79bc7758a5a18d77f6
Reviewed-on: http://review.typo3.org/16117
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
typo3/sysext/core/Classes/Utility/DebugUtility.php

index 8325baa..32cd142 100644 (file)
@@ -52,6 +52,12 @@ class DebugUtility {
                </tr>
        </table>
        ';
+
+       /**
+        * @var \TYPO3\CMS\Core\Encoder\JavaScriptEncoder
+        */
+       static protected $javaScriptEncoder;
+
        /**
         * Debug
         *
@@ -69,19 +75,13 @@ class DebugUtility {
                if ($header) {
                        $debug = sprintf(self::DEBUG_TABLE_TEMPLATE, htmlspecialchars((string) $header), $debug);
                }
-               if (TYPO3_MODE === 'BE') {
-                       $debugString = self::prepareVariableForJavascript($debug, is_object($var));
-                       $group = htmlspecialchars($group);
-                       if ($header !== '') {
-                               $tabHeader = htmlspecialchars($header);
-                       } else {
-                               $tabHeader = 'Debug';
-                       }
+               if (TYPO3_MODE === 'BE' && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI)) {
+                       $tabHeader = $header ?: 'Debug';
                        $script = '
                                (function debug() {
-                                       var debugMessage = "' . $debugString . '";
-                                       var header = "' . $tabHeader . '";
-                                       var group = "' . $group . '";
+                                       var debugMessage = "' . static::getJavaScriptEncoder()->encode($debug) . '";
+                                       var header = "' . static::getJavaScriptEncoder()->encode($tabHeader) . '";
+                                       var group = "' . static::getJavaScriptEncoder()->encode($group) . '";
 
                                        if (typeof Ext !== "object" && (top && typeof top.Ext !== "object")) {
                                                document.write(debugMessage);
@@ -121,38 +121,11 @@ class DebugUtility {
         * @param string $string
         * @param boolean $asObject
         * @return string
+        * @deprecated since 6.0 will be removed with 6.2
         */
        static public function prepareVariableForJavascript($string, $asObject) {
-               if ($asObject) {
-                       $string = str_replace(array(
-                               '"',
-                               '/',
-                               '<',
-                               LF,
-                               CR
-                       ), array(
-                               '\\"',
-                               '\\/',
-                               '\\<',
-                               '<br />',
-                               ''
-                       ), $string);
-               } else {
-                       $string = str_replace(array(
-                               '"',
-                               '/',
-                               '<',
-                               LF,
-                               CR
-                       ), array(
-                               '\\"',
-                               '\\/',
-                               '\\<',
-                               '',
-                               ''
-                       ), $string);
-               }
-               return $string;
+               GeneralUtility::logDeprecatedFunction();
+               return self::getJavaScriptEncoder()->encode($string);
        }
 
        /**
@@ -185,12 +158,12 @@ class DebugUtility {
         * @param string $group
         */
        static public function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug') {
-               $debugString = self::prepareVariableForJavascript(self::convertVariableToString($debugVariable), is_object($debugVariable));
+               $debugString = self::convertVariableToString($debugVariable);
                $script = '
                        (function debug() {
-                               var debugMessage = "' . $debugString . '",
-                                       header = "' . htmlspecialchars($header) . '",
-                                       group = "' . htmlspecialchars($group) . '",
+                               var debugMessage = "' . static::getJavaScriptEncoder()->encode($debugString) . '",
+                                       header = "' . static::getJavaScriptEncoder()->encode($header) . '",
+                                       group = "' . static::getJavaScriptEncoder()->encode($group) . '",
 
                                        browserWindow = function(debug, header, group) {
                                                var newWindow = window.open("", "TYPO3DebugWindow_" + group,
@@ -379,6 +352,16 @@ class DebugUtility {
                echo self::viewArray($array_in);
        }
 
+       /**
+        * @return \TYPO3\CMS\Core\Encoder\JavaScriptEncoder
+        */
+       static protected function getJavaScriptEncoder() {
+               if (empty(self::$javaScriptEncoder)) {
+                       self::$javaScriptEncoder = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Encoder\\JavaScriptEncoder');
+               }
+
+               return self::$javaScriptEncoder;
+       }
 }