<?php
// $Header: /cvsroot/phptsmadmin/phpTSMadmin/lib/config_default.php,v 1.16 2009/04/19 05:44:55 wurley Exp $

/**
 * Configuration processing and defaults.
 *
 * @author The phpTSMadmin development team
 * @package phpTSMadmin
 *
 * @todo Add validation of set variables to enforce limits or particular values.
 */

/** The minimum version of PHP required to run this application. */
define('REQUIRED_PHP_VERSION','5.0.0');

/**
 * The config class contains all our configuration settings for a session.
 *
 * An instance of this class should be stored in $_SESSION to maintain state, and to avoid
 * rebuilding/rereading it at the state of each page output.
 *
 * @package phpTSMadmin
 *
 * @author The phpTSMadmin development team
 * @author Deon George
 */
class Config {
	public $custom;
	protected $default;
	public $servers = array();
	public $hooks = array();

	public function __construct() {
		$this->custom = new stdClass;
		$this->default = new stdClass;

		$this->default->app['name'] = array(
			'desc'=>'Application Name',
			'default'=>'phpTSMadmin');

		$this->default->appearance['compress'] = array(
			'desc'=>'Compress Output',
			'default'=>false);

		$this->default->appearance['control_icons'] = array(
			'desc'=>'Show the control as icons or text',
			'default'=>false);

		$this->default->appearance['menu'] = array(
			'desc'=>'Class name which inherits from Menu class and implements the draw() method',
			'default'=>'menu_html');

		/** Language
		 * The language setting. If you set this to 'auto', This application will
		 * attempt to determine your language automatically. Otherwise, set
		 * this to your applicable language in xx_XX format.
		 */
		$this->default->appearance['language'] = array(
			'desc'=>'Language',
			'default'=>'auto');

		$this->default->appearance['page_title'] = array(
			'desc'=>'Change the page title to this text',
			'default'=>'');

		$this->default->appearance['stylesheet'] = array(
			'desc'=>'Style sheet to use',
			'default'=>'style.css');

		$this->default->appearance['timezone'] = array(
			'desc'=>'Define our timezone, if not defined in php.ini',
			'default'=>null);

		$this->default->appearance['logged_in_chars'] = array(
			'desc'=>'Number of chars of loggin name to show',
			'default'=>15);

		/** Caching
		 */
		$this->default->cache['menu'] = array(
			'desc'=>'Cache the Menu',
			'default'=>false);

		$this->default->cache['time'] = array(
			'desc'=>'How long to cache our TSM queries',
			'default'=>600);

		/** Commands
		 * Define which high level commands are available.
		 */
		$this->default->commands['all'] = array(
			'desc'=>'Define command availability',
			'default'=> array(
				'home' => true,
				'login' => true,
				'logout' => true,
				'purge' => true,
				'showcache' => true,
				'register' => true
			));

		/** Debug Attributes
		 * Attributes that control debugging
		 */
		$this->default->debug['level'] = array(
			'desc'=>'Debug level verbosity',
			'default'=>0);

		$this->default->debug['syslog'] = array(
			'desc'=>'Whether to send debug messages to syslog',
			'default'=>false);

		$this->default->debug['file'] = array(
			'desc'=>'Name of file to send debug output to',
			'default'=>null);

		$this->default->debug['addr'] = array(
			'desc'=>'IP address of client to provide debugging info.',
			'default'=>null);

		$this->default->debug['append'] = array(
			'desc'=>'Whether to append to the debug file, or create it fresh each time',
			'default'=>true);

		$this->default->debug['show_cache'] = array(
			'desc'=>'Show the tool that shows debugging information.',
			'default'=>false);

		/** Session Attributes
		 * Session related attributes.
		 */
		$this->default->session['blowfish'] = array(
			'desc'=>'Blowfish key to encrypt cookie details',
			'default'=>null);

		$this->default->session['memorylimit'] = array(
			'desc'=>'Set the PHP memorylimit warning threshold.',
			'default'=>24);

		$this->default->session['timelimit'] = array(
			'desc'=>'Set the PHP timelimit.',
			'default'=>30);

		/** Cookie Time
		 * If you used auth_type 'form' in the servers list, you can adjust how long the cookie will last
		 * (default is 0 seconds, which expires when you close the browser)
		 */
		$this->default->session['cookie_time'] = array(
			'desc'=>'Time in seconds for the life of cookies',
			'default'=>0);

		/** Session Menu
		 */
		$this->default->menu['session'] = array(
			'desc'=>'Menu items when logged in.',
			'default'=>array(
				'info'=>array('php'=>'info.php','icon'=>'info.png','text'=>'Server Info')
			));

		/** Available Commands
		 */
	}

	/**
	 * Access the configuration, taking into account the defaults and the customisations
	 */
	private function getConfigArray($usecache=true) {
		static $CACHE = array();

		if ($usecache && count($CACHE))
			return $CACHE;

		foreach ($this->default as $key => $vals)
			$CACHE[$key] = $vals;

		foreach ($this->custom as $key => $vals)
			foreach ($vals as $index => $val)
				$CACHE[$key][$index]['value'] = $val;

		return $CACHE;
	}

	/**
	 * Get a configuration value.
	 */
	public function getValue($key,$index,$fatal=true) {
		$config = $this->getConfigArray();

		if (! isset($config[$key]))
			if ($fatal)
				error(sprintf('A call was made in [%s] to getValue requesting [%s] that isnt predefined.',
					basename($_SERVER['PHP_SELF']),$key),'error',null,true);
			else
				return null;

		if (! isset($config[$key][$index]))
			if ($fatal)
				error(sprintf('Requesting an index [%s] in key [%s] that isnt predefined.',$index,$key),'error',null,true);
			else
				return null;

		return isset($config[$key][$index]['value']) ? $config[$key][$index]['value'] : $config[$key][$index]['default'];
	}

	/**
	 * Return the untested config items
	 */
	public function untested() {
		$result = array();

		foreach ($this->default as $option => $details)
			foreach ($details as $param => $values)
				if (isset($values['untested']) && $values['untested'])
					array_push($result,sprintf('%s.%s',$option,$param));

		return $result;
	}

	/**
	 * Function to check and warn about any unusual defined variables.
	 */
	public function CheckCustom() {
		if (isset($this->custom)) {
			foreach ($this->custom as $masterkey => $masterdetails) {

				if (isset($this->default->$masterkey)) {

					if (! is_array($masterdetails))
						error(sprintf('Error in configuration file, [%s] should be an ARRAY.',$masterdetails),'error',null,true);

					foreach ($masterdetails as $key => $value) {
						# Test that the key is correct.
						if (! in_array($key,array_keys($this->default->$masterkey)))
							error(sprintf('Error in configuration file, [%s] has not been defined as a configurable variable.',$key),'error',null,true);

						# Test if its should be an array or not.
						if (is_array($this->default->{$masterkey}[$key]['default']) && ! is_array($value))
							error(sprintf('Error in configuration file, %s[\'%s\'] SHOULD be an array of values.',$masterkey,$key),'error',null,true);

						if (! is_array($this->default->{$masterkey}[$key]['default']) && is_array($value))
							error(sprintf('Error in configuration file, %s[\'%s\'] should NOT be an array of values.',$masterkey,$key),'error',null,true);
					}

				} else {
					error(sprintf('Error in configuration file, [%s] has not been defined as a MASTER configurable variable.',$masterkey),'error',null,true);
				}
			}
		}
	}

	/**
	 * Get a list of available commands.
	 */
	public function getCommandList() {
		$config = $this->getConfigArray(false);

		masort($config['command'],'summary');

		if (isset($config['command']) && is_array($config['command']))
			return $config['command'];
		else
			return array();
	}

	/**
	 * The parameter number is variable.
	 * For example : isCommandAvailable('search','simple_search')
	 */
	public function isCommandAvailable($index='all') {
		$a = func_get_args();

		if (! in_array($index,array('all','script')))
			$index = 'all';
		else
			array_shift($a);

		if (count($a) == 1 && is_array($a[0]))
			$a = $a[0];
		$i = 0;

		# Command availability list
		$cmd = $this->getValue('commands',$index);

		# Search for the command
		while ($i < count($a)) {
			if (! is_array($cmd))
				return $cmd;

			if (! isset($cmd[$a[$i]]))
				return false;

			$cmd = $cmd[$a[$i]];
			$i++;
		}

		# If this is a leaf command, return its availability
		if (! is_array($cmd))
			return $cmd;

		# Else the command is available, if one of its sub-command is available
		$a[] = '';
		foreach ($cmd as $c => $v) {
			$a[$i] = $c;
			if ($this->isCommandAvailable($a))
				return true;
		}
		return false;
	}

	public function configDefinition($key,$index,$config) {
		if (! is_array($config) || ! array_key_exists('desc',$config) || ! array_key_exists('default',$config))
			return;

		if (isset($this->default->$key))
			$definition = $this->default->$key;

		$definition[$index] = $config;
		$this->default->$key = $definition;
	}

	public function setServers($servers) {
		$this->servers = $servers;
	}

	public function getServer($index=null) {
		return $this->servers->Instance($index);
	}

	/**
	 * Return a list of our servers
	 * @param boolean $visible - Only return visible servers
	 */
	public function getServerList($visible=true) {
		return $this->servers->getServerList($visible);
	}
}
?>
