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