[TASK] Change "false" into "FALSE"
[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 reset($rows);
245 $firstEl = current($rows);
246 if (is_array($firstEl)) {
247 $headerColumns = array_keys($firstEl);
248 $tRows = array();
249
250 // Header:
251 $tRows[] = '<tr><td colspan="' . count($headerColumns) .
252 '" style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;"><strong>' .
253 htmlspecialchars($header) . '</strong></td></tr>';
254 $tCells = array();
255 foreach ($headerColumns as $key) {
256 $tCells[] = '
257 <td><font face="Verdana,Arial" size="1"><strong>' . htmlspecialchars($key) . '</strong></font></td>';
258 }
259 $tRows[] = '
260 <tr>' . implode('', $tCells) . '
261 </tr>';
262
263 // Rows:
264 foreach ($rows as $singleRow) {
265 $tCells = array();
266 foreach ($headerColumns as $key) {
267 $tCells[] = '
268 <td><font face="Verdana,Arial" size="1">' .
269 (is_array($singleRow[$key]) ? self::debugRows($singleRow[$key], '', TRUE) : htmlspecialchars($singleRow[$key])) .
270 '</font></td>';
271 }
272 $tRows[] = '
273 <tr>' . implode('', $tCells) . '
274 </tr>';
275 }
276
277 $table = '
278 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">' . implode('', $tRows) . '
279 </table>';
280 if ($returnHTML) {
281 return $table;
282 }
283 else
284 {
285 echo $table;
286 }
287 } else
288 {
289 debug('Empty array of rows', $header);
290 }
291 } else {
292 debug('No array of rows', $header);
293 }
294 }
295
296 /**
297 * Returns a string with a list of ascii-values for the first $characters characters in $string
298 *
299 * @param string String to show ASCII value for
300 * @param integer Number of characters to show
301 * @return string The string with ASCII values in separated by a space char.
302 */
303 public static function ordinalValue($string, $characters = 100) {
304 if (strlen($string) < $characters) {
305 $characters = strlen($string);
306 }
307 for ($i = 0; $i < $characters; $i++) {
308 $valuestring .= ' ' . ord(substr($string, $i, 1));
309 }
310 return trim($valuestring);
311 }
312
313 /**
314 * Returns HTML-code, which is a visual representation of a multidimensional array
315 * use t3lib_div::print_array() in order to print an array
316 * Returns FALSE if $array_in is not an array
317 *
318 * @param mixed Array to view
319 * @return string HTML output
320 */
321 public static function viewArray($array_in) {
322 if (is_array($array_in)) {
323 $result = '
324 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">';
325 if (count($array_in) == 0) {
326 $result .= '<tr><td><font face="Verdana,Arial" size="1"><strong>EMPTY!</strong></font></td></tr>';
327 } else {
328 foreach ($array_in as $key => $val) {
329 $result .= '<tr>
330 <td valign="top"><font face="Verdana,Arial" size="1">' . htmlspecialchars((string) $key) . '</font></td>
331 <td>';
332 if (is_array($val)) {
333 $result .= self::viewArray($val);
334 } elseif (is_object($val)) {
335 $string = '';
336 if (method_exists($val, '__toString')) {
337 $string .= get_class($val) . ': ' . (string) $val;
338 } else {
339 $string .= print_r($val, TRUE);
340 }
341 $result .= '<font face="Verdana,Arial" size="1" color="red">' .
342 nl2br(htmlspecialchars($string)) .
343 '<br /></font>';
344 } else {
345 if (gettype($val) == 'object') {
346 $string = 'Unknown object';
347 } else {
348 $string = (string) $val;
349 }
350 $result .= '<font face="Verdana,Arial" size="1" color="red">' .
351 nl2br(htmlspecialchars($string)) .
352 '<br /></font>';
353 }
354 $result .= '</td>
355 </tr>';
356 }
357 }
358 $result .= '</table>';
359 } else {
360 $result = '<table border="1" cellpadding="1" cellspacing="0" bgcolor="white">
361 <tr>
362 <td><font face="Verdana,Arial" size="1" color="red">' .
363 nl2br(htmlspecialchars((string) $array_in)) .
364 '<br /></font></td>
365 </tr>
366 </table>'; // Output it as a string.
367 }
368 return $result;
369 }
370
371 /**
372 * Prints an array
373 *
374 * @param mixed Array to print visually (in a table).
375 * @return void
376 * @see view_array()
377 */
378 public static function printArray($array_in) {
379 echo self::viewArray($array_in);
380 }
381 }
382
383 ?>