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