* @package TYPO3
* @subpackage t3lib
*/
final class t3lib_utility_Debug {
/**
* Template for debug output
*
* @var string
*/
const DEBUG_TABLE_TEMPLATE = '
';
public static function debug($var = '', $header = '', $group = 'Debug') {
// buffer the output of debug if no buffering started before
if (ob_get_level() == 0) {
ob_start();
}
$debug = self::convertVariableToString($var);
if ($header) {
$debug = sprintf(self::DEBUG_TABLE_TEMPLATE, htmlspecialchars((string) $header), $debug);
}
if (TYPO3_MODE === 'BE') {
$debugString = self::prepareVariableForJavascript($debug, is_object($var));
$group = htmlspecialchars($group);
if ($header !== '') {
$tabHeader = htmlspecialchars($header);
} else {
$tabHeader = 'Debug';
}
$script = '
(function debug() {
var debugMessage = "' . $debugString . '";
var header = "' . $tabHeader . '";
var group = "' . $group . '";
if (typeof Ext !== "object" && (top && typeof top.Ext !== "object")) {
document.write(debugMessage);
return;
}
if (top && typeof Ext !== "object") {
Ext = top.Ext;
}
Ext.onReady(function() {
var TYPO3ViewportInstance = null;
if (top && top.TYPO3 && typeof top.TYPO3.Backend === "object") {
TYPO3ViewportInstance = top.TYPO3.Backend;
} else if (typeof TYPO3 === "object" && typeof TYPO3.Backend === "object") {
TYPO3ViewportInstance = TYPO3.Backend;
}
if (TYPO3ViewportInstance !== null) {
TYPO3ViewportInstance.DebugConsole.addTab(debugMessage, header, group);
} else {
document.write(debugMessage);
}
});
})();
';
echo t3lib_div::wrapJS($script);
} else {
echo $debug;
}
}
/**
* Replaces special characters for the usage inside javascript
*
* @param string $string
* @param boolean $asObject
* @return string
*/
public static function prepareVariableForJavascript($string, $asObject) {
if ($asObject) {
$string = str_replace(array(
'"', '/', '<', "\n", "\r"
), array(
'\"', '\/', '\<', '
', ''
), $string);
} else {
$string = str_replace(array(
'"', '/', '<', "\n", "\r"
), array(
'\"', '\/', '\<', '', ''
), $string);
}
return $string;
}
/**
* Converts a variable to a string
*
* @param mixed $variable
* @return string
*/
public static function convertVariableToString($variable) {
$string = '';
if (is_array($variable)) {
$string = self::viewArray($variable);
} elseif (is_object($variable)) {
$string = '|Object:';
$string .= print_r($variable, TRUE);
$string .= '
|';
} elseif ((string) $variable !== '') {
$string = '|' . htmlspecialchars((string) $variable) . '|';
} else {
$string = '| debug |';
}
return $string;
}
/**
* Opens a debug message inside a popup window
*
* @param mixed $debugVariable
* @param string $header
* @param string $group
*/
public static function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug') {
$debugString = self::prepareVariableForJavascript(
self::convertVariableToString($debugVariable),
is_object($debugVariable)
);
$script = '
(function debug() {
var debugMessage = "' . $debugString . '",
header = "' . htmlspecialchars($header) . '",
group = "' . htmlspecialchars($group) . '",
browserWindow = function(debug, header, group) {
var newWindow = window.open("", "TYPO3DebugWindow_" + group,
"width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
);
if (newWindow.document.body.innerHTML) {
newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
"
" + debugMessage;
} else {
newWindow.document.writeln(
"Debug: " + header + "(" + group + ")"
+ ""
+ debugMessage
+ ""
);
}
}
if (!top.Ext) {
browserWindow(debugMessage, header, group);
} else {
top.Ext.onReady(function() {
if (top && top.TYPO3 && top.TYPO3.Backend) {
top.TYPO3.Backend.DebugConsole.openBrowserWindow(header, debugMessage, group);
} else {
browserWindow(debugMessage, header, group);
}
});
}
})();
';
echo t3lib_div::wrapJS($script);
}
/**
* Displays the "path" of the function call stack in a string, using debug_backtrace
*
* @return string
*/
public static function debugTrail() {
$trail = debug_backtrace();
$trail = array_reverse($trail);
array_pop($trail);
$path = array();
foreach ($trail as $dat) {
$pathFragment = $dat['class'] . $dat['type'] . $dat['function'];
// add the path of the included file
if (in_array($dat['function'], array('require', 'include', 'require_once', 'include_once'))) {
$pathFragment .= '(' . substr($dat['args'][0], strlen(PATH_site)) . '),' . substr($dat['file'], strlen(PATH_site));
}
$path[] = $pathFragment . '#' . $dat['line'];
}
return implode(' // ', $path);
}
/**
* Displays an array as rows in a table. Useful to debug output like an array of database records.
*
* @param mixed Array of arrays with similar keys
* @param string Table header
* @param boolean If TRUE, will return content instead of echo'ing out.
* @return void Outputs to browser.
*/
public static function debugRows($rows, $header = '', $returnHTML = FALSE) {
if (is_array($rows)) {
$firstEl = reset($rows);
if (is_array($firstEl)) {
$headerColumns = array_keys($firstEl);
$tRows = array();
// Header:
$tRows[] = '' .
htmlspecialchars($header) . ' |
';
$tCells = array();
foreach ($headerColumns as $key) {
$tCells[] = '
' . htmlspecialchars($key) . ' | ';
}
$tRows[] = '
' . implode('', $tCells) . '
';
// Rows:
foreach ($rows as $singleRow) {
$tCells = array();
foreach ($headerColumns as $key) {
$tCells[] = '
' .
(is_array($singleRow[$key]) ? self::debugRows($singleRow[$key], '', TRUE) : htmlspecialchars($singleRow[$key])) .
' | ';
}
$tRows[] = '
' . implode('', $tCells) . '
';
}
$table = '
' . implode('', $tRows) . '
';
if ($returnHTML) {
return $table;
}
else
{
echo $table;
}
} else
{
debug('Empty array of rows', $header);
}
} else {
debug('No array of rows', $header);
}
}
/**
* Returns a string with a list of ascii-values for the first $characters characters in $string
*
* @param string String to show ASCII value for
* @param integer Number of characters to show
* @return string The string with ASCII values in separated by a space char.
*/
public static function ordinalValue($string, $characters = 100) {
if (strlen($string) < $characters) {
$characters = strlen($string);
}
for ($i = 0; $i < $characters; $i++) {
$valuestring .= ' ' . ord(substr($string, $i, 1));
}
return trim($valuestring);
}
/**
* Returns HTML-code, which is a visual representation of a multidimensional array
* use t3lib_div::print_array() in order to print an array
* Returns FALSE if $array_in is not an array
*
* @param mixed Array to view
* @return string HTML output
*/
public static function viewArray($array_in) {
if (is_array($array_in)) {
$result = '
';
if (count($array_in) == 0) {
$result .= 'EMPTY! |
';
} else {
foreach ($array_in as $key => $val) {
$result .= '
' . htmlspecialchars((string) $key) . ' |
';
if (is_array($val)) {
$result .= self::viewArray($val);
} elseif (is_object($val)) {
$string = '';
if (method_exists($val, '__toString')) {
$string .= get_class($val) . ': ' . (string) $val;
} else {
$string .= print_r($val, TRUE);
}
$result .= '' .
nl2br(htmlspecialchars($string)) .
' ';
} else {
if (gettype($val) == 'object') {
$string = 'Unknown object';
} else {
$string = (string) $val;
}
$result .= '' .
nl2br(htmlspecialchars($string)) .
' ';
}
$result .= ' |
';
}
}
$result .= '
';
} else {
$result = '
' .
nl2br(htmlspecialchars((string) $array_in)) .
'
|
'; // Output it as a string.
}
return $result;
}
/**
* Prints an array
*
* @param mixed Array to print visually (in a table).
* @return void
* @see viewArray()
*/
public static function printArray($array_in) {
echo self::viewArray($array_in);
}
}
?>