[TASK] Remove deprecated prepareVariableForJavascript from DebugUtility
[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 * Converts a variable to a string
113 *
114 * @param mixed $variable
115 * @return string
116 */
117 static public function convertVariableToString($variable) {
118 if (is_array($variable)) {
119 $string = self::viewArray($variable);
120 } elseif (is_object($variable)) {
121 $string = '<strong>|Object:<pre>';
122 $string .= print_r($variable, TRUE);
123 $string .= '</pre>|</strong>';
124 } elseif ((string) $variable !== '') {
125 $string = '<strong>|' . htmlspecialchars((string) $variable) . '|</strong>';
126 } else {
127 $string = '<strong>| debug |</strong>';
128 }
129 return $string;
130 }
131
132 /**
133 * Opens a debug message inside a popup window
134 *
135 * @param mixed $debugVariable
136 * @param string $header
137 * @param string $group
138 */
139 static public function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug') {
140 $debugString = self::convertVariableToString($debugVariable);
141 $script = '
142 (function debug() {
143 var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
144 header = ' . GeneralUtility::quoteJSvalue($header) . ',
145 group = ' . GeneralUtility::quoteJSvalue($group) . ',
146
147 browserWindow = function(debug, header, group) {
148 var newWindow = window.open("", "TYPO3DebugWindow_" + group,
149 "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
150 );
151 if (newWindow.document.body.innerHTML) {
152 newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
153 "<hr />" + debugMessage;
154 } else {
155 newWindow.document.writeln(
156 "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
157 + "<body onload=\\"self.focus()\\">"
158 + debugMessage
159 + "</body></html>"
160 );
161 }
162 }
163
164 if (!top.Ext) {
165 browserWindow(debugMessage, header, group);
166 } else {
167 top.Ext.onReady(function() {
168 if (top && top.TYPO3 && top.TYPO3.Backend) {
169 top.TYPO3.Backend.DebugConsole.openBrowserWindow(header, debugMessage, group);
170 } else {
171 browserWindow(debugMessage, header, group);
172 }
173 });
174 }
175 })();
176 ';
177 echo \TYPO3\CMS\Core\Utility\GeneralUtility::wrapJS($script);
178 }
179
180 /**
181 * Displays the "path" of the function call stack in a string, using debug_backtrace
182 *
183 * @return string
184 */
185 static public function debugTrail() {
186 $trail = debug_backtrace();
187 $trail = array_reverse($trail);
188 array_pop($trail);
189 $path = array();
190 foreach ($trail as $dat) {
191 $pathFragment = $dat['class'] . $dat['type'] . $dat['function'];
192 // add the path of the included file
193 if (in_array($dat['function'], array('require', 'include', 'require_once', 'include_once'))) {
194 $pathFragment .= '(' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($dat['file']);
195 }
196 $path[] = $pathFragment . '#' . $dat['line'];
197 }
198 return implode(' // ', $path);
199 }
200
201 /**
202 * Displays an array as rows in a table. Useful to debug output like an array of database records.
203 *
204 * @param mixed $rows Array of arrays with similar keys
205 * @param string $header Table header
206 * @param boolean $returnHTML If TRUE, will return content instead of echo'ing out.
207 * @return void Outputs to browser.
208 */
209 static public function debugRows($rows, $header = '', $returnHTML = FALSE) {
210 if (is_array($rows)) {
211 $firstEl = reset($rows);
212 if (is_array($firstEl)) {
213 $headerColumns = array_keys($firstEl);
214 $tRows = array();
215 // Header:
216 $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>';
217 $tCells = array();
218 foreach ($headerColumns as $key) {
219 $tCells[] = '
220 <td><font face="Verdana,Arial" size="1"><strong>' . htmlspecialchars($key) . '</strong></font></td>';
221 }
222 $tRows[] = '
223 <tr>' . implode('', $tCells) . '
224 </tr>';
225 // Rows:
226 foreach ($rows as $singleRow) {
227 $tCells = array();
228 foreach ($headerColumns as $key) {
229 $tCells[] = '
230 <td><font face="Verdana,Arial" size="1">' . (is_array($singleRow[$key]) ? self::debugRows($singleRow[$key], '', TRUE) : htmlspecialchars($singleRow[$key])) . '</font></td>';
231 }
232 $tRows[] = '
233 <tr>' . implode('', $tCells) . '
234 </tr>';
235 }
236 $table = '
237 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">' . implode('', $tRows) . '
238 </table>';
239 if ($returnHTML) {
240 return $table;
241 } else {
242 echo $table;
243 }
244 } else {
245 debug('Empty array of rows', $header);
246 }
247 } else {
248 debug('No array of rows', $header);
249 }
250 }
251
252 /**
253 * Returns a string with a list of ascii-values for the first $characters characters in $string
254 *
255 * @param string $string String to show ASCII value for
256 * @param integer $characters Number of characters to show
257 * @return string The string with ASCII values in separated by a space char.
258 */
259 static public function ordinalValue($string, $characters = 100) {
260 if (strlen($string) < $characters) {
261 $characters = strlen($string);
262 }
263 $valuestring = '';
264 for ($i = 0; $i < $characters; $i++) {
265 $valuestring .= ' ' . ord(substr($string, $i, 1));
266 }
267 return trim($valuestring);
268 }
269
270 /**
271 * Returns HTML-code, which is a visual representation of a multidimensional array
272 * use \TYPO3\CMS\Core\Utility\GeneralUtility::print_array() in order to print an array
273 * Returns FALSE if $array_in is not an array
274 *
275 * @param mixed $array_in Array to view
276 * @return string HTML output
277 */
278 static public function viewArray($array_in) {
279 if (is_array($array_in)) {
280 $result = '
281 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">';
282 if (count($array_in) == 0) {
283 $result .= '<tr><td><font face="Verdana,Arial" size="1"><strong>EMPTY!</strong></font></td></tr>';
284 } else {
285 foreach ($array_in as $key => $val) {
286 $result .= '<tr>
287 <td valign="top"><font face="Verdana,Arial" size="1">' . htmlspecialchars((string) $key) . '</font></td>
288 <td>';
289 if (is_array($val)) {
290 $result .= self::viewArray($val);
291 } elseif (is_object($val)) {
292 $string = '';
293 if (method_exists($val, '__toString')) {
294 $string .= get_class($val) . ': ' . (string) $val;
295 } else {
296 $string .= print_r($val, TRUE);
297 }
298 $result .= '<font face="Verdana,Arial" size="1" color="red">' . nl2br(htmlspecialchars($string)) . '<br /></font>';
299 } else {
300 if (gettype($val) == 'object') {
301 $string = 'Unknown object';
302 } else {
303 $string = (string) $val;
304 }
305 $result .= '<font face="Verdana,Arial" size="1" color="red">' . nl2br(htmlspecialchars($string)) . '<br /></font>';
306 }
307 $result .= '</td>
308 </tr>';
309 }
310 }
311 $result .= '</table>';
312 } else {
313 $result = '<table border="1" cellpadding="1" cellspacing="0" bgcolor="white">
314 <tr>
315 <td><font face="Verdana,Arial" size="1" color="red">' . nl2br(htmlspecialchars((string) $array_in)) . '<br /></font></td>
316 </tr>
317 </table>';
318 }
319 // Output it as a string.
320 return $result;
321 }
322
323 /**
324 * Prints an array
325 *
326 * @param mixed $array_in Array to print visually (in a table).
327 * @return void
328 * @see viewArray()
329 */
330 static public function printArray($array_in) {
331 echo self::viewArray($array_in);
332 }
333
334 }