[TASK] Replace JavaScriptEncoder with json_encode()
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / DebugUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Steffen Kamper <steffen@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Class to handle debug
31 *
32 * @author Steffen Kamper <steffen@typo3.org>
33 */
34 class DebugUtility {
35
36 /**
37 * Template for debug output
38 *
39 * @var string
40 */
41 const DEBUG_TABLE_TEMPLATE = '
42 <table class="typo3-debug" border="0" cellpadding="0" cellspacing="0" bgcolor="white" style="border:0px; margin-top:3px; margin-bottom:3px;">
43 <tr>
44 <td style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;">%s</td>
45 </tr>
46 <tr>
47 <td>
48 %s
49 </td>
50 </tr>
51 </table>
52 ';
53
54 /**
55 * Debug
56 *
57 * @param string $var
58 * @param string $header
59 * @param string $group
60 * @return void
61 */
62 static public function debug($var = '', $header = '', $group = 'Debug') {
63 // buffer the output of debug if no buffering started before
64 if (ob_get_level() == 0) {
65 ob_start();
66 }
67 $debug = self::convertVariableToString($var);
68 if ($header) {
69 $debug = sprintf(self::DEBUG_TABLE_TEMPLATE, htmlspecialchars((string) $header), $debug);
70 }
71 if (TYPO3_MODE === 'BE' && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI)) {
72 $tabHeader = $header ?: 'Debug';
73 $script = '
74 (function debug() {
75 var debugMessage = ' . GeneralUtility::quoteJSvalue($debug) . ';
76 var header = ' . GeneralUtility::quoteJSvalue($tabHeader) . ';
77 var group = ' . GeneralUtility::quoteJSvalue($group) . ';
78
79 if (typeof Ext !== "object" && (top && typeof top.Ext !== "object")) {
80 document.write(debugMessage);
81 return;
82 }
83
84 if (top && typeof Ext !== "object") {
85 Ext = top.Ext;
86 }
87
88 Ext.onReady(function() {
89 var TYPO3ViewportInstance = null;
90
91 if (top && top.TYPO3 && typeof top.TYPO3.Backend === "object") {
92 TYPO3ViewportInstance = top.TYPO3.Backend;
93 } else if (typeof TYPO3 === "object" && typeof TYPO3.Backend === "object") {
94 TYPO3ViewportInstance = TYPO3.Backend;
95 }
96
97 if (TYPO3ViewportInstance !== null) {
98 TYPO3ViewportInstance.DebugConsole.addTab(debugMessage, header, group);
99 } else {
100 document.write(debugMessage);
101 }
102 });
103 })();
104 ';
105 echo \TYPO3\CMS\Core\Utility\GeneralUtility::wrapJS($script);
106 } else {
107 echo $debug;
108 }
109 }
110
111 /**
112 * Replaces special characters for the usage inside javascript
113 *
114 * @param string $string
115 * @param boolean $asObject
116 * @return string
117 * @deprecated since 6.0 will be removed with 6.2
118 */
119 static public function prepareVariableForJavascript($string, $asObject) {
120 GeneralUtility::logDeprecatedFunction();
121 return self::getJavaScriptEncoder()->encode($string);
122 }
123
124 /**
125 * Converts a variable to a string
126 *
127 * @param mixed $variable
128 * @return string
129 */
130 static public function convertVariableToString($variable) {
131 if (is_array($variable)) {
132 $string = self::viewArray($variable);
133 } elseif (is_object($variable)) {
134 $string = '<strong>|Object:<pre>';
135 $string .= print_r($variable, TRUE);
136 $string .= '</pre>|</strong>';
137 } elseif ((string) $variable !== '') {
138 $string = '<strong>|' . htmlspecialchars((string) $variable) . '|</strong>';
139 } else {
140 $string = '<strong>| debug |</strong>';
141 }
142 return $string;
143 }
144
145 /**
146 * Opens a debug message inside a popup window
147 *
148 * @param mixed $debugVariable
149 * @param string $header
150 * @param string $group
151 */
152 static public function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug') {
153 $debugString = self::convertVariableToString($debugVariable);
154 $script = '
155 (function debug() {
156 var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
157 header = ' . GeneralUtility::quoteJSvalue($header) . ',
158 group = ' . GeneralUtility::quoteJSvalue($group) . ',
159
160 browserWindow = function(debug, header, group) {
161 var newWindow = window.open("", "TYPO3DebugWindow_" + group,
162 "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
163 );
164 if (newWindow.document.body.innerHTML) {
165 newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
166 "<hr />" + debugMessage;
167 } else {
168 newWindow.document.writeln(
169 "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
170 + "<body onload=\\"self.focus()\\">"
171 + debugMessage
172 + "</body></html>"
173 );
174 }
175 }
176
177 if (!top.Ext) {
178 browserWindow(debugMessage, header, group);
179 } else {
180 top.Ext.onReady(function() {
181 if (top && top.TYPO3 && top.TYPO3.Backend) {
182 top.TYPO3.Backend.DebugConsole.openBrowserWindow(header, debugMessage, group);
183 } else {
184 browserWindow(debugMessage, header, group);
185 }
186 });
187 }
188 })();
189 ';
190 echo \TYPO3\CMS\Core\Utility\GeneralUtility::wrapJS($script);
191 }
192
193 /**
194 * Displays the "path" of the function call stack in a string, using debug_backtrace
195 *
196 * @return string
197 */
198 static public function debugTrail() {
199 $trail = debug_backtrace();
200 $trail = array_reverse($trail);
201 array_pop($trail);
202 $path = array();
203 foreach ($trail as $dat) {
204 $pathFragment = $dat['class'] . $dat['type'] . $dat['function'];
205 // add the path of the included file
206 if (in_array($dat['function'], array('require', 'include', 'require_once', 'include_once'))) {
207 $pathFragment .= '(' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($dat['file']);
208 }
209 $path[] = $pathFragment . '#' . $dat['line'];
210 }
211 return implode(' // ', $path);
212 }
213
214 /**
215 * Displays an array as rows in a table. Useful to debug output like an array of database records.
216 *
217 * @param mixed $rows Array of arrays with similar keys
218 * @param string $header Table header
219 * @param boolean $returnHTML If TRUE, will return content instead of echo'ing out.
220 * @return void Outputs to browser.
221 */
222 static public function debugRows($rows, $header = '', $returnHTML = FALSE) {
223 if (is_array($rows)) {
224 $firstEl = reset($rows);
225 if (is_array($firstEl)) {
226 $headerColumns = array_keys($firstEl);
227 $tRows = array();
228 // Header:
229 $tRows[] = '<tr><td colspan="' . count($headerColumns) . '" style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;"><strong>' . htmlspecialchars($header) . '</strong></td></tr>';
230 $tCells = array();
231 foreach ($headerColumns as $key) {
232 $tCells[] = '
233 <td><font face="Verdana,Arial" size="1"><strong>' . htmlspecialchars($key) . '</strong></font></td>';
234 }
235 $tRows[] = '
236 <tr>' . implode('', $tCells) . '
237 </tr>';
238 // Rows:
239 foreach ($rows as $singleRow) {
240 $tCells = array();
241 foreach ($headerColumns as $key) {
242 $tCells[] = '
243 <td><font face="Verdana,Arial" size="1">' . (is_array($singleRow[$key]) ? self::debugRows($singleRow[$key], '', TRUE) : htmlspecialchars($singleRow[$key])) . '</font></td>';
244 }
245 $tRows[] = '
246 <tr>' . implode('', $tCells) . '
247 </tr>';
248 }
249 $table = '
250 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">' . implode('', $tRows) . '
251 </table>';
252 if ($returnHTML) {
253 return $table;
254 } else {
255 echo $table;
256 }
257 } else {
258 debug('Empty array of rows', $header);
259 }
260 } else {
261 debug('No array of rows', $header);
262 }
263 }
264
265 /**
266 * Returns a string with a list of ascii-values for the first $characters characters in $string
267 *
268 * @param string $string String to show ASCII value for
269 * @param integer $characters Number of characters to show
270 * @return string The string with ASCII values in separated by a space char.
271 */
272 static public function ordinalValue($string, $characters = 100) {
273 if (strlen($string) < $characters) {
274 $characters = strlen($string);
275 }
276 $valuestring = '';
277 for ($i = 0; $i < $characters; $i++) {
278 $valuestring .= ' ' . ord(substr($string, $i, 1));
279 }
280 return trim($valuestring);
281 }
282
283 /**
284 * Returns HTML-code, which is a visual representation of a multidimensional array
285 * use \TYPO3\CMS\Core\Utility\GeneralUtility::print_array() in order to print an array
286 * Returns FALSE if $array_in is not an array
287 *
288 * @param mixed $array_in Array to view
289 * @return string HTML output
290 */
291 static public function viewArray($array_in) {
292 if (is_array($array_in)) {
293 $result = '
294 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">';
295 if (count($array_in) == 0) {
296 $result .= '<tr><td><font face="Verdana,Arial" size="1"><strong>EMPTY!</strong></font></td></tr>';
297 } else {
298 foreach ($array_in as $key => $val) {
299 $result .= '<tr>
300 <td valign="top"><font face="Verdana,Arial" size="1">' . htmlspecialchars((string) $key) . '</font></td>
301 <td>';
302 if (is_array($val)) {
303 $result .= self::viewArray($val);
304 } elseif (is_object($val)) {
305 $string = '';
306 if (method_exists($val, '__toString')) {
307 $string .= get_class($val) . ': ' . (string) $val;
308 } else {
309 $string .= print_r($val, TRUE);
310 }
311 $result .= '<font face="Verdana,Arial" size="1" color="red">' . nl2br(htmlspecialchars($string)) . '<br /></font>';
312 } else {
313 if (gettype($val) == 'object') {
314 $string = 'Unknown object';
315 } else {
316 $string = (string) $val;
317 }
318 $result .= '<font face="Verdana,Arial" size="1" color="red">' . nl2br(htmlspecialchars($string)) . '<br /></font>';
319 }
320 $result .= '</td>
321 </tr>';
322 }
323 }
324 $result .= '</table>';
325 } else {
326 $result = '<table border="1" cellpadding="1" cellspacing="0" bgcolor="white">
327 <tr>
328 <td><font face="Verdana,Arial" size="1" color="red">' . nl2br(htmlspecialchars((string) $array_in)) . '<br /></font></td>
329 </tr>
330 </table>';
331 }
332 // Output it as a string.
333 return $result;
334 }
335
336 /**
337 * Prints an array
338 *
339 * @param mixed $array_in Array to print visually (in a table).
340 * @return void
341 * @see viewArray()
342 */
343 static public function printArray($array_in) {
344 echo self::viewArray($array_in);
345 }
346
347 }