<?php

/*
** DGS Search
** $Id: debug.php,v 1.2 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2000-2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

function verifyPHPVersion($major, $minor, $sub) {
	global $config;

	if ($config["debug"])
		printf("->Debug: verifyPHPVersion() - Running PHP '%s' (Required version is %d.%d.%d)<BR>\n", phpversion(), $major, $minor, $sub);

	$phpVersion = explode(".", phpversion());
	/* Convert any non-integer values to -1, such as 4.0b3 */
	$phpVersion[0] = (strlen($phpVersion[0]) > 0) ? intval($phpVersion[0]) : -1;
	$phpVersion[1] = (strlen($phpVersion[1]) > 0) ? intval($phpVersion[1]) : -1;
	$phpVersion[2] = (strlen($phpVersion[2]) > 0) ? intval($phpVersion[2]) : -1;

	if ($phpVersion[0] < $major || ($phpVersion[0] == $major && $phpVersion[1] < $minor) || ($phpVersion[0] == $major && $phpVersion[1] == $minor && $phpVersion[2] < $sub)) {
		return sprintf('%s %s requires at least PHP v%d.%d.%d. You are currently running PHP %s.', $config['program'], $config['version'], $major, $minor, $sub, phpversion());
	}

	return true;
}

function remoteDebug($password, $option) {
	global $config;
		
	if (!$config['remoteDebug'] || (is_string($config['remoteDebug']) && strcmp($config['remoteDebug'], $password))) {
		printf("<!-- Remote Debug is not available. -->\n");
		flush();
		return true;
	}

	switch (strtolower($option)) {
		case 'all':
			remoteDebug($password, 'config');
			remoteDebug($password, 'phpInfo');
			printf("</CENTER>");
			$config['debug'] = true;
			return true;
		case 'phpinfo':
			phpInfo();
			break;
		case 'config':
			$content = file('config/config.php');
			$content = implode(' ', $content);

			/* Cleanup a bit - Generic */
			$content = ereg_replace("/\*[^\*]+\*/", "", $content);

			/* Hide usernames and passwords. */
			$content = eregi_replace("(config\['remoteDebug'\])[^;]+", "\\1 = [Hidden]", $content);
			$content = eregi_replace("(database\[[0-9]+\]\[['\"]username['\"]\])[^;]+", "\\1 = [Hidden]", $content);
			$content = eregi_replace("(database\[[0-9]+\]\[['\"]password['\"]\])[^;]+", "\\1 = [Hidden]", $content);

			/* Final touches */
			if (function_exists('highlight_string')) {
				$func = 'highlight_string'; /* I know this looks stupid. */
				$content = $func($content);
			} else {
				/* Cleanup a bit - Specific */
				$content = ereg_replace("[\n\r]", "<BR>\n", $content);
				$content = ereg_replace("[[:space:]]*<BR>\n", "<BR>\n", $content);
				$content = ereg_replace("(<BR>\n){2,}", "<BR>\n<BR>\n", $content);
				$content = str_replace('<?', '<STRONG>', $content);
				$content = str_replace('?>', '</STRONG>', $content);
			}

			/* Display config.php */
			printf("%s", $content);
			break;
		case 'ash':
			srand ((double) microtime() * 1000000);
			switch (rand() % 5) {
				case 0:
					printf("Its a trick, get an axe.<BR>\n");
					break;
				case 1:
					printf("Lady, I'm afraid I'm going to have to ask you to leave the store.<BR>\n");
					break;
				case 2:
					printf("Good. Bad. I'm the guy with the gun.<BR>\n");
					break;
				case 3:
					printf("Come get some.<BR>\n");
					break;
				case 4:
					printf("Swallow this.<BR>\n");
					break;
			}
			printf("<P>\n-Bruce 'Ash' Campbell<BR>\n");
			break;
		default:
			$config['debug'] = true;
			return true;
	}

	return false;
}

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3
*/

?>
