<?php

//	define('DBG_IP', 'xxx.yyy.zzz.ddd');		// Define IP addreses, that are allowed to user debugger, semicolon separated. All allowed if none specified. 
//	define('SILENT_LOG', 1);					// Log all php errors on site to separate file (/silent_log.txt)
//	define('DBG_REQUREST_LOG', '/path/to/file');// Log all user requests to site into filename specified
//	define('DBG_SITE_PATH', '/relative_path/');	// set alternative BASE_PATH for old in-portal parts (where no K4 included)
	
	InitDebugger();
	
	/**
	 * Allows to determine if debug is allowed by user, who is viewing site
	 *
	 * @return bool
	 */
	function DebugAllowed()
	{
		$ip_addresses = defined('DBG_IP') && DBG_IP ? explode(';', DBG_IP) : Array($_SERVER['REMOTE_ADDR']);
		
		return 	in_array($_SERVER['REMOTE_ADDR'], $ip_addresses) &&
				!(defined('IS_INSTALL') && IS_INSTALL);
	}
	
	/**
	 * Performs initial debugger configuration by defining control constants
	 *
	 */
	function InitDebugger()
	{
		if (!DebugAllowed()) {
			// debug is not allowed
			return false;
		}
		define('DEBUG_MODE', 1);				// Debug mode is allowed
		
		define('DBG_LOCAL_BASE_PATH', 'w:');	// Folder name on mapped drive, where site resides
//		define('DBG_TOOLBAR_BUTTONS', 1);		// Show "Show Debugger" & "Refresh Frame" buttons (on front)
		
//		define('DBG_USE_HIGHLIGHT', 0);			// Use "highlight_string" php function for debugger output formatting
//		define('DBG_RAISE_ON_WARNINGS',1);		// Show debugger output in case of any non-fatal error
		define('DBG_SQL_PROFILE',1);			// Profile SQL queries
		define('DBG_SQL_FAILURE', defined('IS_INSTALL') && IS_INSTALL ? 0 : 1);	// treat sql errors as fatal errors except for installation process
		
		define('DBG_SHOW_HTTPQUERY', 1);		// Show http query content (parsed user submit, GPC)
		define('DBG_SHOW_SESSIONDATA', 1);		// Show session data (at script finish)
		
		define('DBG_EDIT_HELP', 1);				// Show help filename on help screen
//		define('DBG_HELP', 1);		// Show FCK editor when viewing help screen
		
//		define('DBG_FORCE_THEME', 1);			// Use this theme_id instead of one in url
		define('DBG_PHRASES', 1);				// Add ability to translate phrases on the fly (K4 templates only)
		define('DBG_WINDOW_WIDTH', 700);// Set custom debugger layer width (in pixels)
		
//		define('DBG_REDIRECT', 1);				// Show links with redirect url instead of performing it (useful in events debugging)
//		define('DBG_ZEND_PRESENT',0);			// Set to 0 to debug debugger (because debugger automatically got disabled during zend debug sessions)
//		define('DBG_VALIDATE_CONFIGS',1);		// Check that config fields match ones from database
//		define('DBG_SHOW_TAGS', 1);				// Show tags beeing processed
		
		// for ADODB to work: begin
		define('ADODB_OUTP', 'dbg_SQLLog');
		function dbg_SQLLog($msg, $new_line = false) { }
		// for ADODB to work: end
		
		function isSkipTable($sql)
		{
			// don't show sqls that use one or more tables from list below
			static $skipTables = Array(	'PermissionConfig','SessionData','Permissions',
										'Phrase','Cache','Modules','PermCache','Events',
										'PersistantSessionData','EmailQueue','UserSession',
										'ThemeFiles', 'Language','ConfigurationValues');
			
			// make exception for scripts listed below
			$filename = basename($_SERVER['PHP_SELF']);
			switch ($filename) {
				case 'session_list.php':
					$allow_tables = Array('UserSession', 'SessionData');
					break;
			
				default:
					$allow_tables = Array();
					break;
			}
			
			foreach ($allow_tables as $allowed_table) {
				unset($skipTables[ array_search($allowed_table, $skipTables)]);
			}
			
			foreach ($skipTables as $table) {
				if( tableMatch($table,$sql) ) {
					return true;
				}
			}
			return false;
		}

		function tableMatch($table_name,$sql)
		{
			static $prefix = '';
			$prefix = defined('TABLE_PREFIX')?TABLE_PREFIX:GetTablePrefix();
			return strpos($sql,$prefix.$table_name)!==false;
		}
	}
?>