Fixed bug #15589: Move debug functions to own utility class
[Packages/TYPO3.CMS.git] / t3lib / utility / class.t3lib_utility_debug.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 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 * $Id: $
32 *
33 *
34 * @author Steffen Kamper <steffen@typo3.org>
35 * @package TYPO3
36 * @subpackage t3lib
37 */
38 class t3lib_utility_Debug {
39
40 /**
41 * Template for debug output
42 *
43 * @var string
44 */
45 const DEBUG_TABLE_TEMPLATE = '
46 <table class="typo3-debug" border="0" cellpadding="0" cellspacing="0" bgcolor="white" style="border:0px; margin-top:3px; margin-bottom:3px;">
47 <tr>
48 <td style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;">%s</td>
49 </tr>
50 <tr>
51 <td>
52 %s
53 </td>
54 </tr>
55 </table>
56 ';
57
58
59 public function debug($var = '', $header = '', $group = 'Debug') {
60 // buffer the output of debug if no buffering started before
61 if (ob_get_level() == 0) {
62 ob_start();
63 }
64 $debug = '';
65
66 if (is_array($var)) {
67 $debug .= self::viewArray($var);
68 } elseif (is_object($var)) {
69 $debug .= '<strong>|Object:<pre>';
70 $debug .= print_r($var, TRUE);
71 $debug .= '</pre>|</strong>';
72 } elseif ((string) $var !== '') {
73 $debug .= '<strong>|' . htmlspecialchars((string) $var) . '|</strong>';
74 } else {
75 $debug .= '<strong>| debug |</strong>';
76 }
77
78 if ($header) {
79 $debug = sprintf(self::DEBUG_TABLE_TEMPLATE, htmlspecialchars((string) $header), $debug);
80 }
81
82 if (TYPO3_MODE === 'BE') {
83 $group = htmlspecialchars($group);
84
85 if ($header !== '') {
86 $tabHeader = htmlspecialchars($header);
87 } else {
88 $tabHeader = 'Debug';
89 }
90
91 if (is_object($var)) {
92 $debug = str_replace(array (
93 '"', '/', '<', "\n", "\r"
94 ), array (
95 '\"', '\/', '\<', '<br />', ''
96 ), $debug);
97 } else {
98 $debug = str_replace(array (
99 '"', '/', '<', "\n", "\r"
100 ), array (
101 '\"', '\/', '\<', '', ''
102 ), $debug);
103 }
104
105 $script = '
106 (function debug() {
107 var debugMessage = "' . $debug . '";
108 var header = "' . $tabHeader . '";
109 var group = "' . $group . '";
110
111 if (typeof Ext !== "object" && (top && typeof top.Ext !== "object")) {
112 document.write(debugMessage);
113 return;
114 }
115
116 if (top && typeof Ext !== "object") {
117 Ext = top.Ext;
118 }
119
120 Ext.onReady(function() {
121 var TYPO3ViewportInstance = null;
122
123 if (top && top.TYPO3 && typeof top.TYPO3.Backend === "object") {
124 TYPO3ViewportInstance = top.TYPO3.Backend;
125 } else if (typeof TYPO3 === "object" && typeof TYPO3.Backend === "object") {
126 TYPO3ViewportInstance = TYPO3.Backend;
127 }
128
129 if (TYPO3ViewportInstance !== null) {
130 TYPO3ViewportInstance.DebugConsole.addTab(debugMessage, header, group);
131 } else {
132 document.write(debugMessage);
133 }
134 });
135 })();
136 ';
137 echo t3lib_div::wrapJS($script);
138 } else {
139 echo $debug;
140 }
141 }
142
143 /**
144 * Displays the "path" of the function call stack in a string, using debug_backtrace
145 *
146 * @return string
147 */
148 public function debugTrail() {
149 $trail = debug_backtrace();
150 $trail = array_reverse($trail);
151 array_pop($trail);
152
153 $path = array ();
154 foreach ($trail as $dat) {
155 $path[] = $dat['class'] . $dat['type'] . $dat['function'] . '#' . $dat['line'];
156 }
157
158 return implode(' // ', $path);
159 }
160
161 /**
162 * Displays an array as rows in a table. Useful to debug output like an array of database records.
163 *
164 * @param mixed Array of arrays with similar keys
165 * @param string Table header
166 * @param boolean If TRUE, will return content instead of echo'ing out.
167 * @return void Outputs to browser.
168 */
169 public function debugRows($rows, $header = '', $returnHTML = FALSE) {
170 if (is_array($rows)) {
171 reset($rows);
172 $firstEl = current($rows);
173 if (is_array($firstEl)) {
174 $headerColumns = array_keys($firstEl);
175 $tRows = array ();
176
177 // Header:
178 $tRows[] = '<tr><td colspan="' . count($headerColumns) .
179 '" style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;"><strong>' .
180 htmlspecialchars($header) . '</strong></td></tr>';
181 $tCells = array ();
182 foreach ($headerColumns as $key) {
183 $tCells[] = '
184 <td><font face="Verdana,Arial" size="1"><strong>' . htmlspecialchars($key) . '</strong></font></td>';
185 }
186 $tRows[] = '
187 <tr>' . implode('', $tCells) . '
188 </tr>';
189
190 // Rows:
191 foreach ($rows as $singleRow) {
192 $tCells = array ();
193 foreach ($headerColumns as $key) {
194 $tCells[] = '
195 <td><font face="Verdana,Arial" size="1">' .
196 (is_array($singleRow[$key]) ? self::debugRows($singleRow[$key], '', TRUE) : htmlspecialchars($singleRow[$key])) .
197 '</font></td>';
198 }
199 $tRows[] = '
200 <tr>' . implode('', $tCells) . '
201 </tr>';
202 }
203
204 $table = '
205 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">' . implode('', $tRows) . '
206 </table>';
207 if ($returnHTML)
208 return $table;
209 else
210 echo $table;
211 } else
212 debug('Empty array of rows', $header);
213 } else {
214 debug('No array of rows', $header);
215 }
216 }
217
218 /**
219 * Returns a string with a list of ascii-values for the first $characters characters in $string
220 *
221 * @param string String to show ASCII value for
222 * @param integer Number of characters to show
223 * @return string The string with ASCII values in separated by a space char.
224 * @deprecated since TYPO3 4.5 - Use t3lib_utility_Debug::debug_ordvalue instead
225 */
226 public function ordinalValue($string, $characters = 100) {
227 if (strlen($string) < $characters)
228 $characters = strlen($string);
229 for ($i = 0; $i < $characters; $i++) {
230 $valuestring .= ' ' . ord(substr($string, $i, 1));
231 }
232 return trim($valuestring);
233 }
234
235 /**
236 * Returns HTML-code, which is a visual representation of a multidimensional array
237 * use t3lib_div::print_array() in order to print an array
238 * Returns false if $array_in is not an array
239 *
240 * @param mixed Array to view
241 * @return string HTML output
242 */
243 public function viewArray($array_in) {
244 if (is_array($array_in)) {
245 $result = '
246 <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">';
247 if (count($array_in) == 0) {
248 $result .= '<tr><td><font face="Verdana,Arial" size="1"><strong>EMPTY!</strong></font></td></tr>';
249 } else {
250 foreach ($array_in as $key => $val) {
251 $result .= '<tr>
252 <td valign="top"><font face="Verdana,Arial" size="1">' . htmlspecialchars((string) $key) . '</font></td>
253 <td>';
254 if (is_array($val)) {
255 $result .= self::viewArray($val);
256 } elseif (is_object($val)) {
257 $string = '';
258 if (method_exists($val, '__toString')) {
259 $string .= get_class($val) . ': ' . (string) $val;
260 } else {
261 $string .= print_r($val, TRUE);
262 }
263 $result .= '<font face="Verdana,Arial" size="1" color="red">' .
264 nl2br(htmlspecialchars($string)) .
265 '<br /></font>';
266 } else {
267 if (gettype($val) == 'object') {
268 $string = 'Unknown object';
269 } else {
270 $string = (string) $val;
271 }
272 $result .= '<font face="Verdana,Arial" size="1" color="red">' .
273 nl2br(htmlspecialchars($string)) .
274 '<br /></font>';
275 }
276 $result .= '</td>
277 </tr>';
278 }
279 }
280 $result .= '</table>';
281 } else {
282 $result = '<table border="1" cellpadding="1" cellspacing="0" bgcolor="white">
283 <tr>
284 <td><font face="Verdana,Arial" size="1" color="red">' .
285 nl2br(htmlspecialchars((string) $array_in)) .
286 '<br /></font></td>
287 </tr>
288 </table>'; // Output it as a string.
289 }
290 return $result;
291 }
292
293 /**
294 * Prints an array
295 *
296 * @param mixed Array to print visually (in a table).
297 * @return void
298 * @see view_array()
299 */
300 public function printArray($array_in) {
301 echo self::viewArray($array_in);
302 }
303 }
304
305
306 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/utility/class.t3lib_utility_debug.php']) {
307 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/utility/class.t3lib_utility_debug.php']);
308 }
309 ?>