Location: PHPKode > projects > Aukyla Document Management System > base/Config.php
<?php
/*
     Config.php, gives access to configuration files
     Copyright (C) 2003-2005 Arend van Beelen, Auton Rijnsburg

     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

     For any questions, comments or whatever, you may mail me at: hide@address.com
*/

require_once('Constants.php');
require_once('Locale.php');
require_once('String.php');

/**
 * @internal
 */
class ConfigVariable
{
	public function __construct($name, $value, $changed = false)
	{
		$this->name = $name;
		$this->value = $value;
		$this->changed = $changed;
	}

	public $name;
	public $value;
	public $changed;
}

/**
 * @brief Class for managing global, application and user configurations.
 *
 * One instance of this class is always created automatically for
 * accessing global variables. Global variables are the variables stored
 * in the global.conf file and are neither module- nor
 * user-specific. You can access the global variables using the static
 * globals() function.
 *
 * If you do need access to module- or user-specific variables, you
 * should create your own instance of this class. You can then access
 * these variables using the variable() function.
 *
 * Finally, you can also use this class to access GET and POST
 * variables. You should use the static function request() for this.
 *
 * @sa globals(), variable(), request()
 */
class Config
{
	/**
	 * Constructor.
	 *
	 * You only need to create your own instance of this class if you need to
	 * access module- or user-specific variables. You can access these variables
	 * using the variable() function.
	 *
	 * @param modulename Name of the module whose configuration file should be
	 *                   opened.
	 * @param username   Name of the user whose configuration file(s) should be
	 *                   opened. If @p modulename is not empty, the user's
	 *                   configuration file for the given module will be opened as
	 *                   well.
	 *
	 * @note Anonymous users don't have personal configuration and you are advised
	 *       against creating a Config class instance for anonymous users. The
	 *       default 'anonymous' username is used for reading non-user-specific
	 *       configuration.
	 */
	public function __construct($modulename = '', $username = 'anonymous')
	{
		if(String::startsWith($username, 'anonymous'))
		{
			$username = 'anonymous';
		}

		$this->modulename = $modulename;
		$this->username = $username;

		$this->readConfiguration();

		// register all request variables for the global configuration
		if($modulename == '' && $username == 'anonymous')
		{
			$request = array_merge($_GET, $_POST);
			foreach($request as $var => $val)
			{
				if($val != '')
				{
					if(get_magic_quotes_gpc() == true)
					{
						$val = stripslashes($val);
					}

					$this->requests[$var] = $val;
				}
			}
		}
	}

	/**
	 * (Re-)reads the configuration files, thereby resetting all variables to
	 * their initial value and discarting new variables.
	 *
	 * This function is called initially by the constructor.
	 *
	 * @sa __construct()
	 */
	public function readConfiguration()
	{
		$this->variables = array();

		$files[] = AUKYLA_DIR.'/config/global.conf';
		if($this->username != 'anonymous')
		{
			$files[] = AUKYLA_DIR."/config/{$this->username}/global.conf";
		}
		if($this->modulename != '')
		{
			$files[] = AUKYLA_DIR."/config/{$this->modulename}.conf";
			if($this->username != 'anonymous')
			{
				$files[] = AUKYLA_DIR."/config/{$this->username}/{$this->modulename}.conf";
			}
		}

		foreach($files as $file)
		{
			if(file_exists($file) && $lines = file($file))
			{
				foreach($lines as $line)
				{
					$line = trim($line);

					if(substr($line, 0, 1) == "#" || strlen($line) == 0)
					{
						continue;
					}

					list($var, $val) = explode('=', $line, 2);
					$var = rtrim($var);
					$val = ltrim($val);
					// special tags...
					switch($var)
					{
						case 'baseURL':
						case 'secureBaseURL':
						case 'downloadURL':
						case 'secureDownloadURL':
						case 'editURL':
						case 'secureEditURL':
							if(isset($_SERVER['HTTP_HOST']))
							{
								$val = str_replace('%HOST%', $_SERVER['HTTP_HOST'], $val);
							}
							break;

						case 'language':
							if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) == false)
							{
								$preferredLanguage = 'en';
							}
							else if(strstr($_SERVER['HTTP_ACCEPT_LANGUAGE'], ',') == false)
							{
								$preferredLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
							}
							else
							{
								list($preferredLanguage, $otherLanguages) = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'], 2);
							}
							$val = str_replace('%ACCEPT_LANGUAGE%', $preferredLanguage, $val);
							break;

						default:
					}
					$this->variables[$var] = new ConfigVariable($var, $val);
				}
			}
		}
	}

	/**
	 * Reads a global variable.
	 *
	 * @param variable The global variable to read.
	 * @param default  The default value to return in case the variable is not
	 *                 set.
	 * @return The value of the global variable, or @p default if it was not set.
	 *
	 * @sa setGlobal()
	 */
	public static function globals($variable, $default = '')
	{
		global $Config;

		return $Config->variable($variable, $default);
	}

	/**
	 * Sets a global variable.
	 *
	 * @note The set variable is not saved to disk, you should create your own
	 *       instance of this class and then use setVariable() to be able to save
	 *       your configuration.
	 *
	 * @param variable The global variable to change.
	 * @param value    The new value for the variable.
	 *
	 * @sa globals(), __construct()
	 */
	public static function setGlobal($variable, $value)
	{
		global $Config;

		$Config->setVariable($variable, $value);
	}

	/**
	 * Returns a GET or POST request variable. If a variable has been set
	 * through both the GET and POST methods, POST will take precedence.
	 *
	 * @param variable The request variable to read.
	 * @param default  The default value to return in case the variable is not
	 *                 set.
	 * @return The value of the GET or POST request variable, or @p default if it
	 *         was not set.
	 *
	 * @sa requests(), setRequest()
	 */
	public static function request($variable, $default = '')
	{
		global $Config;

		return (isset($Config->requests[$variable]) ? $Config->requests[$variable] : $default);
	}

	/**
	 * Returns all request variables in an array.
	 *
	 * @return An array containing all GET and POST request variables. The keys in
	 *         the array are the variable names and the values represent the
	 *         values of the request variables.
	 *
	 * @sa request()
	 */
	public static function requests()
	{
		global $Config;

		return $Config->requests;
	}

	/**
	 * Sets a new value for a request variable.
	 *
	 * @param variable The variable to set.
	 * @param value    The new value for the variable. If the value is @p false
	 *                 the variable is unset.
	 *
	 * @sa request(), unsetRequest(), unsetRequests()
	 */
	public static function setRequest($variable, $value = false)
	{
		global $Config;

		if($value === false)
		{
			$Config->unsetRequest($variable);
		}
		else
		{
			$Config->requests[$variable] = $value;
		}
	}

	/**
	 * Unsets a request variable.
	 *
	 * @param variable The variable to unset.
	 *
	 * @sa unsetRequests(), request(), setRequest()
	 *
	 * @since 1.1
	 */
	public static function unsetRequest($variable)
	{
		global $Config;

		unset($Config->requests[$variable]);
	}

	/**
	 * Unsets all request variables.
	 *
	 * @sa setRequest()
	 */
	public static function unsetRequests()
	{
		global $Config;

		$Config->requests = array();
	}

	/**
	 * Reads a variable from the config file.
	 *
	 * @param variable The variable to read.
	 * @param default  The default value to return in case the variable is
	 *                 not set.
	 * @return The value of the configuration variable, or @p default if the
	 *         variable was not set.
	 *
	 * @sa setVariable()
	 */
	public function variable($variable, $default = '')
	{
		return (isset($this->variables[$variable]) ? $this->variables[$variable]->value : $default);
	}

	/**
	 * Sets a variable in the configuration file.
	 *
	 * @param variable The variable to set.
	 * @param value    The new value of the variable.
	 *
	 * @note The changes are not automatically saved to disk, use
	 *       saveConfiguration() to save any changes permanently.
	 *
	 * @sa variable(), saveConfiguration()
	 */
	public function setVariable($variable, $value)
	{
		if(isset($this->variables[$variable]))
		{
			$this->variables[$variable]->value = $value;
			$this->variables[$variable]->changed = true;
		}
		else
		{
			$this->variables[$variable] = new ConfigVariable($variable, $value, true);
		}
	}

	/**
	 * Saves all set variables to disk. The changes are saved in the lowest
	 * order file. This means if a user has changed a variable that came
	 * from a global configuration file, the new value is saved in his
	 * personal configuration, not in the global configuration.
	 *
	 * @warning Because anonymous users don't have any user-specifc configuration,
	 *          they only use the global configuration directly. Saving their
	 *          configuration would result in overwriting the global
	 *          configuration.
	 *
	 * @sa setVariable()
	 */
	public function saveConfiguration()
	{
		$dir = AUKYLA_DIR.'/config/'.($this->username != 'anonymous' ?
		                              "{$this->username}/" : '');
		$file = $dir.($this->modulename != '' ? $this->modulename : 'global').'.conf';
		if(!file_exists($dir))
		{
			mkdir($dir, 0700);
		}

		$lines = (file_exists($file) ? file($file) : array(0 => ''));
		if($fp = fopen($file, 'w'))
		{
			foreach($this->variables as $var)
			{
				if($var->changed == false)
				{
					continue;
				}

				$setval = false;
				foreach($lines as $linenum => $line)
				{
					$line = trim($line);

					if(substr($line, 0, 1) != "#" && strlen($line) != 0)
					{
						list($lineVar, $lineVal) = explode('=', $line, 2);
						$lineVar = rtrim($lineVar);
						if($lineVar == $var->name)
						{
							$lines[$linenum] = "{$var->name} = {$var->value}\n";
							$setval = true;
						}
					}
				}
				if($setval == false)
				{
					$lines[] = "{$var->name} = {$var->value}\n";
				}
			}

			foreach($lines as $line)
			{
				fwrite($fp, $line);
			}

			fclose($fp);

			return true;
		}
		return false;
	}

	private $modulename;
	private $username;
	private $variables;
	private $requests;
}

// create one global instance of the class and initialize localization according
// to the global settings
global $Config;
$Config = new Config();
Locale::init('base', Config::globals('language', 'en'));

?>
Return current item: Aukyla Document Management System