2 /***************************************************************
5 * (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * Class to handle debug
32 * @author Steffen Kamper <steffen@typo3.org>
36 final class t3lib_utility_Debug
{
39 * Template for debug output
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;">
46 <td style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;">%s</td>
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) {
63 $debug = self
::convertVariableToString($var);
65 $debug = sprintf(self
::DEBUG_TABLE_TEMPLATE
, htmlspecialchars((string) $header), $debug);
68 if (TYPO3_MODE
=== 'BE') {
69 $debugString = self
::prepareVariableForJavascript($debug, is_object($var));
70 $group = htmlspecialchars($group);
73 $tabHeader = htmlspecialchars($header);
80 var debugMessage = "' . $debugString . '";
81 var header = "' . $tabHeader . '";
82 var group = "' . $group . '";
84 if (typeof Ext !== "object" && (top && typeof top.Ext !== "object")) {
85 document.write(debugMessage);
89 if (top && typeof Ext !== "object") {
93 Ext.onReady(function() {
94 var TYPO3ViewportInstance = null;
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;
102 if (TYPO3ViewportInstance !== null) {
103 TYPO3ViewportInstance.DebugConsole.addTab(debugMessage, header, group);
105 document.write(debugMessage);
110 echo t3lib_div
::wrapJS($script);
117 * Replaces special characters for the usage inside javascript
119 * @param string $string
120 * @param boolean $asObject
123 public static function prepareVariableForJavascript($string, $asObject) {
125 $string = str_replace(array(
126 '"', '/', '<', "\n", "\r"
128 '\"', '\/', '\<', '<br />', ''
131 $string = str_replace(array(
132 '"', '/', '<', "\n", "\r"
134 '\"', '\/', '\<', '', ''
142 * Converts a variable to a string
144 * @param mixed $variable
147 public static function convertVariableToString($variable) {
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>';
158 $string = '<strong>| debug |</strong>';
165 * Opens a debug message inside a popup window
167 * @param mixed $debugVariable
168 * @param string $header
169 * @param string $group
171 public static function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug') {
172 $debugString = self
::prepareVariableForJavascript(
173 self
::convertVariableToString($debugVariable),
174 is_object($debugVariable)
179 var debugMessage = "' . $debugString . '",
180 header = "' . htmlspecialchars($header) . '",
181 group = "' . htmlspecialchars($group) . '",
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"
187 if (newWindow.document.body.innerHTML) {
188 newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
189 "<hr />" + debugMessage;
191 newWindow.document.writeln(
192 "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
193 + "<body onload=\"self.focus()\">"
201 browserWindow(debugMessage, header, group);
203 top.Ext.onReady(function() {
204 if (top && top.TYPO3 && top.TYPO3.Backend) {
205 top.TYPO3.Backend.DebugConsole.openBrowserWindow(header, debugMessage, group);
207 browserWindow(debugMessage, header, group);
213 echo t3lib_div
::wrapJS($script);
217 * Displays the "path" of the function call stack in a string, using debug_backtrace
221 public static function debugTrail() {
222 $trail = debug_backtrace();
223 $trail = array_reverse($trail);
227 foreach ($trail as $dat) {
228 $pathFragment = $dat['class'] . $dat['type'] . $dat['function'];
229 // add the path of the included file
230 if (in_array($dat['function'], array('require', 'include', 'require_once', 'include_once'))) {
231 $pathFragment .= '(' . substr($dat['args'][0], strlen(PATH_site
)) . '),' . substr($dat['file'], strlen(PATH_site
));
233 $path[] = $pathFragment . '#' . $dat['line'];
236 return implode(' // ', $path);
240 * Displays an array as rows in a table. Useful to debug output like an array of database records.
242 * @param mixed Array of arrays with similar keys
243 * @param string Table header
244 * @param boolean If TRUE, will return content instead of echo'ing out.
245 * @return void Outputs to browser.
247 public static function debugRows($rows, $header = '', $returnHTML = FALSE) {
248 if (is_array($rows)) {
249 $firstEl = reset($rows);
250 if (is_array($firstEl)) {
251 $headerColumns = array_keys($firstEl);
255 $tRows[] = '<tr><td colspan="' . count($headerColumns) .
256 '" style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;"><strong>' .
257 htmlspecialchars($header) . '</strong></td></tr>';
259 foreach ($headerColumns as $key) {
261 <td><font face="Verdana,Arial" size="1"><strong>' . htmlspecialchars($key) . '</strong></font></td>';
264 <tr>' . implode('', $tCells) . '
268 foreach ($rows as $singleRow) {
270 foreach ($headerColumns as $key) {
272 <td><font face="Verdana,Arial" size="1">' .
273 (is_array($singleRow[$key]) ? self
::debugRows($singleRow[$key], '', TRUE) : htmlspecialchars($singleRow[$key])) .
277 <tr>' . implode('', $tCells) . '
282 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">' . implode('', $tRows) . '
293 debug('Empty array of rows', $header);
296 debug('No array of rows', $header);
301 * Returns a string with a list of ascii-values for the first $characters characters in $string
303 * @param string String to show ASCII value for
304 * @param integer Number of characters to show
305 * @return string The string with ASCII values in separated by a space char.
307 public static function ordinalValue($string, $characters = 100) {
308 if (strlen($string) < $characters) {
309 $characters = strlen($string);
311 for ($i = 0; $i < $characters; $i++
) {
312 $valuestring .= ' ' . ord(substr($string, $i, 1));
314 return trim($valuestring);
318 * Returns HTML-code, which is a visual representation of a multidimensional array
319 * use t3lib_div::print_array() in order to print an array
320 * Returns FALSE if $array_in is not an array
322 * @param mixed Array to view
323 * @return string HTML output
325 public static function viewArray($array_in) {
326 if (is_array($array_in)) {
328 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">';
329 if (count($array_in) == 0) {
330 $result .= '<tr><td><font face="Verdana,Arial" size="1"><strong>EMPTY!</strong></font></td></tr>';
332 foreach ($array_in as $key => $val) {
334 <td valign="top"><font face="Verdana,Arial" size="1">' . htmlspecialchars((string) $key) . '</font></td>
336 if (is_array($val)) {
337 $result .= self
::viewArray($val);
338 } elseif (is_object($val)) {
340 if (method_exists($val, '__toString')) {
341 $string .= get_class($val) . ': ' . (string) $val;
343 $string .= print_r($val, TRUE);
345 $result .= '<font face="Verdana,Arial" size="1" color="red">' .
346 nl2br(htmlspecialchars($string)) .
349 if (gettype($val) == 'object') {
350 $string = 'Unknown object';
352 $string = (string) $val;
354 $result .= '<font face="Verdana,Arial" size="1" color="red">' .
355 nl2br(htmlspecialchars($string)) .
362 $result .= '</table>';
364 $result = '<table border="1" cellpadding="1" cellspacing="0" bgcolor="white">
366 <td><font face="Verdana,Arial" size="1" color="red">' .
367 nl2br(htmlspecialchars((string) $array_in)) .
370 </table>'; // Output it as a string.
378 * @param mixed Array to print visually (in a table).
382 public static function printArray($array_in) {
383 echo self
::viewArray($array_in);