<?php
//******************************************************************************
// Debug Tracer.
//
// Traces:
// code execution and allows to see result
// from application web-page in browser by hot keys:
// Ctrl+Shift+e
// or from stand-alone landing to show-deb.php
// Method:
// stores debug information ins $_SESSION[ 'deb-btb' ] variable.
//
// PHP-version Dependency:
// possibly only versions where PHP-namespace has been introduced.
//
// No Library Dependencies:
// on PHP and JavaScript out-of-language-core-libraries.
//
// Author: Konstantin Kirillov
// Date: February 4, 2015.
// License: MIT
//
//*******************************************************************************
//
// Standalone use:
// do land like: http://.......deb-show.php?show-deb=yes
// ?show-deb=yes shows debug trace,
// ?show-sess=yes shows an entire $_SESSION,
// ?purge-deb=yes does clean up accomulated debugt info, $_SESSION.
// In-application-use like:
// require_once __DIR__ . '/deb-show.php';
//
// Display in standalone and in application
// When app or standalone are landed, click Ctr+Shif+e and
// it will show the contents.
// To accomulate debug data from application:
//
// in PHP-source-code, call like:
// if( function_exists( '\BTBT\capture_deb' ) ) \BTBT\capture_deb( $deb );
// or \BTBT\capture_deb( variable_to_debug );
// where variable_to_debug can be of any type.
namespace BTBT;
error_reporting( E_ALL );
ini_set( 'display_errors' , '1' );
const DLOG = 'tr'; // Debug Log Array Index
const R_PURGE = 'deb-purge'; // Request parameter to purge Debug Log
const R_SHOW = 'deb-show';
const R_SESS = 'deb-sess';
const APP_SESS_NAME = 'Elgg';
//const APP_SESS_NAME = '';
function establish_session ()
{
//http://php.net/manual/en/ref.session.php
if( !session_id() )
{
if( APP_SESS_NAME ) session_name( APP_SESS_NAME );
session_start ();
}
if( empty( $_SESSION[ DLOG ] ) ) $_SESSION[ DLOG ] = "\n";
}
/// captures debug in application
function capture_deb ( $obj )
{
establish_session ();
if( empty( $_SESSION[ DLOG ] ) ) $_SESSION[ DLOG ] = "\n";
//http://php.net/manual/en/function.gettype.php
$type = gettype ( $obj );
$output = $type === 'string' || $type === 'double' || $type === 'integer' ? "'" . $obj . "'" : print_r( $obj, true );
$_SESSION[ DLOG ] .= time() . " " . $output . "\n";
}
/// manages and outputs debug to web-browser
function manage_debug_btb ()
{
establish_session ();
if( array_key_exists( R_PURGE, $_REQUEST ) ) $_SESSION[ DLOG ] = '';
$in_standalone_mode = array_key_exists( R_SHOW, $_REQUEST );
$in_standalone_mode_display = $in_standalone_mode ? 'block' : 'none';
//. tests JS-app-show-toggler
// $in_standalone_mode_display = $in_standalone_mode ? 'none' : 'none';
$show_all = array_key_exists( R_SESS, $_REQUEST );
$show_this = $show_all ? $_SESSION : $_SESSION[ DLOG ];
$inner = htmlspecialchars( print_r( $show_this, true ), ENT_QUOTES );
$inner .= 'session_cookie_params=' . print_r( session_get_cookie_params(), true ) . "\n";
$inner .= 'session_id ( value visible in web-browser )=' . session_id() . "\n";
$inner .= 'session_name ( cookie name visible in web-browser )=' . session_name () . "\n";
$inner = str_replace( "\n", '\n', $inner );
$inner = str_replace( "\r", '\r', $inner );
$inner = '"<pre>' . $inner . '</pre>";';
$inner = "\t\t\t\t" . $inner;
$inner = <<<_DEBUG
<script>
( function () {
function check () {
// c onsole.log( 'still ticking ...' + ( new Date() ).getTime() );
if( document.readyState !== 'complete' ) { setTimeout( check, 18 ); return; }
doInitDebug ();
};
check ();
function addEvent ( evnt, elem, func )
{
if( elem.addEventListener ) // W3C DOM
{
elem.addEventListener( evnt, func, false );
}else if( elem.attachEvent ) { // IE DOM
elem.attachEvent( "on" + evnt, funct );
}
}
function doInitDebug ()
{
var deb = document.createElement( 'div' );
deb.style.display = '$in_standalone_mode_display';
deb.style.position = 'absolute';
deb.style.zIndex = '111111111';
document.body.appendChild( deb );
deb.innerHTML = $inner
var keys = 'abcdefghijklmnopqrstuvxyz';
var key = 'e';
addEvent( 'keydown', document, function ( event )
{
if( event.ctrlKey && event.shiftKey && keys.charAt( event.keyCode - 65 ) === key )
{
deb.style.display = deb.style.display === 'none' ? 'block' : 'none';
var preventDefault = function() { event.returnValue = false };
var preventDefaultOriginal = function() { event.preventDefault() };
var stopPropagation = function() { event.cancelBubble = true };
var stopPropagationOriginal = function() { event.stopPropagation() };
var preventDefault = event.preventDefault ? preventDefaultOriginal : preventDefault;
var stopPropagation = event.stopPropagation ? stopPropagationOriginal : stopPropagation;
preventDefault();
stopPropagation();
return false;
}
return true;
} );
}
}) ();
</script>
_DEBUG;
$html = <<<_HTML_DEB
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
$inner
</body>
</html>
_HTML_DEB;
echo $in_standalone_mode ? $html : $inner;
}
manage_debug_btb ();
//\BTBT\capture_deb( 'test-capture' );