Location: PHPKode > projects > Cynus > cynus/debug.php
<?php
/**
 * @file debug.php -- Provides debugging functions
 * @Id $Id: debug.php,v 1.6 2004/06/22 19:39:36 jason Exp $
 *
 * Cynus - a web-based content manager
 * Copyright (C) 2003 Brett and Jason Profitt
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
 

function cynus_debug($subject, $type=4, $direction=null) {
	# if $subject is an array, print it out parsed through cynus_debug_var...
	# else, assume it's a standard message
	
	#fool proofing.  
	if (!defined ('DEBUG_LEVEL')) {
		$override = 1;
	}
	
	# no use doing everything for messages that won't be seen...
	if (($override!=1) && !($type <= DEBUG_LEVEL)) { return; }
	
	if (is_array ($subject)) {
		_cynus_debug_var ($subject, $line);
	}
	else {
		# getting line numbers if we have debug_backtrace();
		if (function_exists (debug_backtrace)) {
			$trace = debug_backtrace();
			foreach ($trace as $temp) {
				if ($temp['function']=='cynus_debug') {
					$line = $temp['line'];
					$file = str_replace ($_SERVER['DOCUMENT_ROOT'], '', $temp['file']);
					$temp_name = split ('/', $file);
					$file = $temp_name[count($temp_name)-1];
					
				}
			}
			$subject = $subject . " [$file:$line]";
		}
		$subject = str_replace ("'", "\'", $subject);
		$subject = htmlspecialchars ($subject);
		_cynus_debug_msg ($subject, $type, $direction);
	}
}

function _cynus_debug_msg($msg, $type=4, $direction=null) {
	global $config;
	/* types:
	1 Error
	2 Warning
	3 Info
	4 Extra
	5 Variables
	*/
	
	if ($direction=="tx") {
		$dx = "<a class=\"tx\">C==&gt;</a> ";
	}
	elseif ($direction=="rx") {
		$dx = "<a class=\"rx\">C&lt;==</a> ";
	}
	
	switch ($type) {
		case 1:
			$type_print = "<a class=\"err\">[ERR]";
			break;
		case 2:
			$type_print = "<a class=\"wrn\">[WRN]";
			break;
		case 3:
			$type_print = "<a class=\"inf\">[INF]";
			break;
		case 4:
			$type_print = "<a class=\"ext\">[EXT]";
			# fading out debugging info direction colors
			if ($direction=="tx") {
					$dx = "<a class=\"tx_ext\">C==&gt;</a> ";
			}
			elseif ($direction=="rx") {
				$dx = "<a class=\"rx_ext\">C&lt;==</a> ";
			}
			break;
		case 5:
			$type_print = "<a class=\"var\">[VAR]";
			# fading out debugging info direction colors
			break;
	}
	$dbg_msg = "$dx$type_print &nbsp;$msg</a>";
	
	$dbg_print .= "<script language='JavaScript'>\n";
	$dbg_print .= "debugWindow.document.writeln('<tt>". trim(nl2br($dbg_msg))."</tt><br>');\n";
	#$dbg_print .= "self.focus();\n";
	$dbg_print .= "</script>\n";
	
	_debug_open_window();
	
	# if the header hasn't been sent, store all the info,
	# else, print it out to the debug window
	if (HEADER_SENT==1) {
		print $dbg_print;
	}
	else {
		$config['pre_header_dbg'] .= $dbg_print;
	}
}

function _cynus_debug_var($data) {
	#debug_open_window();
	#doing some initial checking to see if we're looking at an array returned by mysql_fetch_array,
	# which has duplicated entries.  removing the numerial keys.

	reset ($data);
	while (list ($key, $value)=each ($data)) {
		list ($key2, $value2) = each ($data);
		
		#checking that the values are equal, and the key is numeric
		if (($value==$value2) && (preg_match ("|[0-9]*|", $key))) {
			unset ($data[$key]);
			$altered = 1;
		}
	}

	# sending a notice that we've trimmed the array
	if ($altered==1) { _cynus_debug_msg ("Following looks like a MySQL returned MYSQL_BOTH array.  Trimming numerics.", 5); }
	
	$captured = explode("\n", _debug_capture_print_r($data));
	foreach($captured as $line)
	{
		$line = str_replace ("'", "\'", $line);
		$line = htmlspecialchars ($line);
		_cynus_debug_msg ("\t" . _debug_colorize_string ($line), 5);
	
	}

}


function _debug_open_window() {
	global $config;
	
	static $window_opened = FALSE;
	if(!$window_opened)
	{
		$win_js = <<<___eofh
		<script language="JavaScript">
		debugWindow = window.open("","debugWin","toolbar=no,scrollbars,width=600,height=400");
		debugWindow.document.writeln('<html>');
		debugWindow.document.writeln('<head>');
		debugWindow.document.writeln('<title>Cynus Debug Console</title>');
		debugWindow.document.writeln('<link rel="stylesheet" type="text/css" href="themes/debug.css" />');
		debugWindow.document.writeln('</head>');
		debugWindow.document.writeln('<body>');
		debugWindow.document.writeln('<br /><hr size="1" width="100%" /><br />');
		</script>

___eofh;
		#define (HEADER_SENT, 0);
		if (HEADER_SENT==1) {
			print $win_js;
		}
		else {
			$config['head_extra'] .= $win_js;
		}
		
		$window_opened = TRUE;
	}
}


/**
* catch the contents of a print_r into a string
*
* @access private
* @param $data unknown variable
* @return string print_r results
* @global
*/
function _debug_capture_print_r($data) {
	ob_start();
	print_r($data);

	$result = ob_get_contents();

	ob_end_clean();

	return $result;
}


/**
* colorize a string for pretty display
*
* @access private
* @param $string string info to colorize
* @return string HTML colorized
* @global
*/
function _debug_colorize_string($string) {
	/* turn array indexes to red */
	$string = str_replace('[','[<font color="red">',$string);
	$string = str_replace(']','</font>]',$string);
	/* turn the word Array blue */
	$string = str_replace('Array','<font color="blue">Array</font>',$string);
	/* turn arrows graygreen */
	$string = str_replace('=>','<font color="#556F55">=></font>',$string);
	return $string;
}

?>
Return current item: Cynus