<?php // ۞// text { encoding:utf-8; bom:no; linebreaks:unix; tabs:4; }
$du_version = '0.4.8.1';
/*
corz.org debug unit (aka "du")
I gave the unit a wee update, small but mighty significant..
The debug unit can now act as a stand-alone "variable collector".
If the usefulness of this isn't immediately apparent, think about it! better
yet, send this baby a FORM!
<form method="post" name="myform" action="/path/to/debug.php">
etc ...
This ability was hacked in to find out exactly what the Winamp "Now Playing"
plug-in <http://www.cc.jyu.fi/~ltnevala/nowplaying/index.php> was up to. I
send its HTTP Post function directly to the debug unit's URL, and BINGO! saves
having to go all the way with degubbin lines when all you need's a quick read-out
of your $_POST, $_GET, $_SERVER, $_SESSION, $_COOKIE, or whatever variables.
This facility has proven useful since, and you might find it so, too*, especially
if you tinker with mod_rewrite. The output-only "report.php" is very useful for that.
Hey, it's still free!
*grin*
* the session and cookie tests, in particular, have been truly helpful in
tracking down those browser "variations".
(c) corz.org 2003 ->
notes and instructions:
if call the script directly and add "?output=true" (no quotes!) to your request,
you'll get the output spewed back into your browser. handy.
in output mode, you will get a SESSION and COOKIE counter in your titlebar.
if all is well, the view counter will be 1 higher than the cookie counter.
hit refresh a few times to see if everything's working as expected.
I always keep my debug lines hard left, ending each debug line with "//:debug:" which
makes them a) easy to find, and b) easy to remove, here's some regex to kill 'em..
^(.*):debug:$
the "$GLOBALS['do_debug']" preference (below) is best kept in your script or site preferences,
so you can switch debugging on or off globally. this makes life easier.
this is what you might expect to find in the calling script (if you are debugging all
pages on a site, pop it into your header or init script and have it always available)..
// start debugging..// :debug:
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/inc/debug.php')) {// :debug:
include ($_SERVER['DOCUMENT_ROOT'].'/inc/debug.php');// :debug:
}
$GLOBALS['do_debug'] = 1;// probably called earlier from your site prefs, whatever.//:debug:
if (!empty($GLOBALS['do_debug'])) {// :debug:
debug("\n".'begin debug output for ampsig.com init.php..'."\n\n");// :debug:
while (list($key, $val) = each($_SERVER)) {//:debug:
debug($key.chr(9).$val."\n"); }//:debug:
debug('$_GET: '."\t".print_r($_GET, true)."\n");//:debug:
debug('$_POST: '."\t".print_r($_POST, true)."\n"); //:debug:
debug('$_REQUEST: '."\t".print_r($_REQUEST, true)."\n"); //:debug:
debug("\n\n"); //:debug:
}
which will produce a useful read-out of some common global arrays.
now, if at any point in your script you need to capture a value, drop in a debug line..
if (!empty($GLOBALS['do_debug'])) { debug('$variable: '. $variable ."\n\n"); }//:debug:
or..
if (!empty($GLOBALS['do_debug'])) { debug('$some_array: '."\t".print_r($some_array, true)."\n\n"); }//:debug:
you can put multiple lines inside one "if ($GLOBALS['do_debug'] > 0)" conditional block, of course.
lastly, at the end of the script, maybe in your footer, put..
if (!empty($GLOBALS['do_debug'])) { debug('out'); }//:debug:
TADA!
*/
// enable debugging?
// set to 0 to disable debugging
// if you set it previously (in a calling script), this line will be ignored, but we
// leave it here for running in output mode: http://mydomain/path/to/debug.php?output=true
if (!isset($GLOBALS['do_debug'])) { $GLOBALS['do_debug'] = 1; }
// where to send the output..
// $debug_file = './debug.out'; // to use the same folder as this script
if (!isset($debug_file)) $debug_file = realpath($_SERVER['DOCUMENT_ROOT']).'/inc/log/.ht_debug';
// we'll put stuff here.
$errors = array();
$degug_write_mode = 'new'; // 'new' or 'append' (use append with caution!)
/*
function:debug()
*/
if (isset($GLOBALS['do_debug']) and ($GLOBALS['do_debug'] > 0)) {
if (!function_exists('debug')) {
function debug($data) {
global $debug_file, $degug_write_mode, $debug_string;
if ($GLOBALS['do_debug'] == 0) { return; }
if ($data == 'out') {
$stamp = date("Y-m-d H:i:s (T)");
$debug_string .= "\n\ndebug.php called @ $stamp\n\n";
$debug_string .= path_info_report();
// it's not there. try to create..
if (!file_exists($debug_file)) { $fp = fopen($debug_file, 'wb'); }
if (is_writeable($debug_file)) {
if (($degug_write_mode) == 'append') {
$d_file = fopen($debug_file, 'ab');
} else {
$d_file = fopen($debug_file, 'wb');
}
fwrite($d_file, $debug_string);
fclose($d_file);
} else {
echo '<span class="warning" id="debug-warning-writable">',$debug_file,' not writable!</span>';
}
// trigger a javascript pop-up with script-specific errors.
// could make it first check if you are logged-in admin.
if (!empty($GLOBALS['errors'])) {
//make_js_error_pop($GLOBALS['errors']);
}
// in a function I might set..
// $GLOBALS['errors']['get_file_into_string'] = 'The word list is missing!';
} else {
$debug_string .= $data;
}
}/* end function debug()
*/
}
// we are collecting variables ("output mode") ..
if (basename($_SERVER['SCRIPT_NAME']) == 'debug.php') { //oops! lol
// check session capabilities..
// multiple refreshes of page will increase the counter..
ini_set('session.name', 'debug');
@session_start();
// check the title bar when ?output=true
if (!isset($_SESSION['debug'])) {
$_SESSION['debug'] = '0';
} else {
$_SESSION['debug']++;
}
// basic cookie test (should run 1 behind the view counter)
if (isset($_COOKIE['debug_cookie'])) {
$foo = (integer) $_COOKIE['debug_cookie'];
$foo++;
setcookie('debug_cookie', $foo, time() + (60*60*24)); // 1 day
} else {
setcookie('debug_cookie', '0', time() + (60*60*24));
}
if (isset($_REQUEST['output'])) {
debug("\n".'$debug_file::->'.$debug_file."<-\n\n"); // :debug:
/*
some example debugging lines..
there's some dupliaction, for demonstration purposes.
*/
// keeping the debug lines hard left makes them easy to find..//:debug:
$prot_add = '';
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { $prot_add = 's';}
debug('::: begin DIRECT ACCESS debug output for http'.$prot_add.'://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].":..\n\n");
debug(path_info_report()."\n\n"); // :debug:
// $HTTP_SERVER_VARS..
debug("\n".'$_SERVER variables..'."\n\n");//:debug:
while (list($key, $val) = each($_SERVER)) {//:debug:
debug($key.chr(9).htmlspecialchars($val)."\n"); }//:debug:
if (is_array($HTTP_ENV_VARS)) {
debug("\n".'environment variables..'."\n\n");//:debug:
while (list($key, $val) = each($HTTP_ENV_VARS)) {//:debug:
debug($key.chr(9).$val."\n"); }//:debug:
}
if (is_array($HTTP_COOKIE_VARS)) {
debug("\n".'cookie variables..'."\n\n");//:debug:
while (list($key, $val) = each($HTTP_COOKIE_VARS)) {//:debug:
debug($key.chr(9).$val."\n"); }//:debug:
}
if (isset($_SESSION)) {//:debug:
debug("\n".'$__SESSION Variables..'."\n\n");//:debug:
while (list($key, $val) = each($_SESSION)) {//:debug:
debug($key.chr(9).$val."\n"); }//:debug:
}//:debug:
debug("\n".'request variables...'."\n\n");//:debug:
debug('$_REQUEST: '."\t".print_r($_REQUEST, true)."\n\n");//:debug:
debug('$_GET: '."\t".print_r($_GET, true)."\n\n");//:debug:
debug('$_POST: '."\t".print_r($_POST, true)."\n\n\n");//:debug:
//debug('$GLOBALS: '."\t".print_r($GLOBALS, true)."\n\n\n");// gobs of info //:debug:
// final output
debug('out');//:debug:
echo '<!DOCTYPE HTML>
<html lang="en">
<head><title>corz debug unit v',
$du_version,' .. viewed ',@$_SESSION['debug'],' times [cookie increment: ',@$_COOKIE['debug_cookie'],
']</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
<!--
body {
color: #000000;
margin: 3% 0 0 5%;
font-family: sans-serif;
}
pre {
margin: 0px;
font-family: monospace;
}
//-->
/*]]>*/
</style></head><body><a href="">LINK TO THIS PAGE</a>
<pre><br />',$debug_string,'</pre></body></html>';
}
}
}
// paths..
// this is way more useful than was originally intended!
//
function path_info_report() {
$prot_add = '';
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { $prot_add = 's';}
$path_string = '::: PATHS report for http'.$prot_add.'://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].":..\n\n";
$path_string .= 'realpath("")::-> '.realpath('')."\n\n";
$path_string .= '__FILE__::-> '.__FILE__."\n";
$path_string .= 'realpath (__FILE__)::-> '.realpath(__FILE__)."\n";
$path_string .= 'parent of (__FILE__)::-> '.dirname(__FILE__)."\n\n"; // :debug:
$path_string .= 'DOCUMENT_ROOT::-> '.$_SERVER['DOCUMENT_ROOT']."\n";
$path_string .= 'realpath(DOCUMENT_ROOT)::-> '.realpath($_SERVER['DOCUMENT_ROOT'])."\n";
$path_string .= 'parent of realpath(DOCUMENT_ROOT)::-> '.dirname(realpath($_SERVER['DOCUMENT_ROOT']))."\n\n"; // :debug:
$path_string .= 'SCRIPT_NAME::-> '.$_SERVER['SCRIPT_NAME']."\n";
$path_string .= 'PHP_SELF::-> '.$_SERVER['PHP_SELF']."\n\n";
if (isset($_SERVER['PATH_TRANSLATED'])) {
$path_string .= 'PATH_TRANSLATED::-> '.$_SERVER['PATH_TRANSLATED']."\n";
$path_string .= 'realpath (PATH_TRANSLATED)::-> '.realpath($_SERVER['PATH_TRANSLATED'])."\n";
$path_string .= 'parent of realpath($_SERVER[PATH_TRANSLATED))::-> ' .dirname(realpath($_SERVER['PATH_TRANSLATED']))."\n\n"; // :debug:
}
$path_string .= 'SCRIPT_FILENAME::-> '.$_SERVER['SCRIPT_FILENAME']."\n";
$path_string .= 'realpath (SCRIPT_FILENAME)::-> '.realpath($_SERVER['SCRIPT_FILENAME'])."\n";
$path_string .= 'parent of realpath($_SERVER[SCRIPT_FILENAME))::-> '.dirname(realpath($_SERVER['SCRIPT_FILENAME']))."\n\n"; // :debug:
$path_string .= 'PATH from site root::-> '.substr($_SERVER['SCRIPT_NAME'],0,(strrpos($_SERVER['SCRIPT_NAME'],'/')+1))."\n";
$path_string .= 'THIS file path (fixed)::-> '.str_replace( "\\\\", "/", dirname( __FILE__ ) . "/" )."\n";
$path_string .= 'Main (calling) page path (fixed)::-> '.substr($_SERVER['SCRIPT_FILENAME'], 0, (strrpos($_SERVER['SCRIPT_FILENAME'],'/') + 1))."\n";
return $path_string;
}
/*
pop-up serious errors
handy javascript pop-up for certain errors. it's for arrays, but you
can send it strings, too.
if you are logged in as addmin, you see the pop-ups, otherwise you see
nothing. I'm using pajamas authentication, but you could easily change
($auth->auth_user()) to whatever you need, or use pajamas!
https://corz.org/serv/security/pajamas.php
*/
if (!function_exists('make_js_error_pop')) {
function make_js_error_pop($array) {
global $auth;
$err_str = '';
if ($auth->auth_user()) {
if (is_array($array)) {
foreach($array as $function => $tring) {
$err_str .= $function.': '.$tring."\n";
}
}
// as you can see, I'm still fiddling with this!
// addslashes seems fairly ineffective, here.
$err_str = strip_tags($err_str);
$err_str = str_replace("\n", '\n', $err_str);
$err_str = str_replace("'", "\'", $err_str);
$err_str = str_replace('"', "'+String.fromCharCode(34)+'", $err_str);
echo '
<script type="text/javascript">
//<![CDATA[
alert("',$err_str,'");
//-->//]]>
</script>';
} //!if $auth->auth_user()
}
}
/*
BONUS EXTRAS!
Some things I use onsite,
which you may find useful in your own debugging..
*/
/*
// stop_watch()
//
// A good place to start the page generation timer, and more.
// Send it 'start' (or any positive value), to start it up,
// which returns the current timestamp, which you can use.
//
// Call the bare function to get back a float of the time
// difference since the stop_watch was started. e.g..
//
// stop_watch('start'); // <- Simple start, handy for page generation timer. Or for pure debugging..
// if (!empty($GLOBALS['do_debug'])) { debug("\n\n".'Started @ '.date('l dS \of F Y h:i:s A', stop_watch('start'))."\n"); }// :debug:
// if (!empty($GLOBALS['do_debug'])) { debug("\n".'time so far: '.substr(stop_watch(),0,5)."\n\n"); }// :debug:
//
function stop_watch() {
if (func_num_args() > 0) { $press = func_get_arg(0); }
else { $press = false; }
static $start_time;
$time = array_sum(explode(' ', microtime(true)));
if (!empty($press)) {
$start_time = $time;
return ($start_time);
} else {
return ($time - $start_time);
}
}
*/
?>
|