3 * A class for the configuration stuff implemented in a singleton design paddern
5 * NOTE: We cannot put this in inc/classes/ because it would be loaded (again)
6 * in the class loader. See inc/loader/class_ClassLoader.php for instance
9 * @author Roland Haeder <webmaster@ship-simu.org>
11 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
12 * @license GNU GPL 3.0 or any newer version
13 * @link http://www.ship-simu.org
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 class FrameworkConfiguration implements Registerable {
30 * The framework's main configuration array which will be initialized with
31 * hard-coded configuration data and might be overwritten/extended by
32 * config data from the database.
34 private $config = array();
37 * The configuration instance itself
39 private static $cfgInstance = null;
41 // Some constants for the configuration system
42 const EXCEPTION_CONFIG_ENTRY_IS_EMPTY = 0x130;
43 const EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND = 0x131;
46 * Protected constructor
50 protected function __construct () {
55 * "Create" a configuration instance
57 * @param $enableDebug Wether enable debug mode (default: off)
58 * @return $cfgInstance An instance of this configuration class
60 public final static function createFrameworkConfiguration ($enableDebug = false) {
62 * For singleton design pattern because we only need a one-time-run
63 * through the initial configuration.
65 if (is_null(self::$cfgInstance)) {
66 // CFG: ERROR-REPORTING
67 @error_reporting(E_ALL | E_STRICT);
70 * Shall we enable the debug mode?
73 define('DEBUG_MODE', true);
77 * Crate a config instance
79 self::$cfgInstance = new FrameworkConfiguration();
85 return self::$cfgInstance;
89 * Getter for an instance of this class
91 * @return $cfgInstance An instance of this class
93 public final static function getInstance () {
94 return self::$cfgInstance;
98 * Setter for default time zone (must be correct!)
100 * @param $zone The time-zone string (e.g. Europe/Berlin)
103 public final function setDefaultTimezone ($zone) {
104 // At least 5.1.0 is required for this!
105 if (version_compare(phpversion(), "5.1.0")) {
106 @date_default_timezone_set($zone);
111 * Setter for runtime magic quotes
113 public final function setMagicQuotesRuntime ($enableQuotes) {
114 // Cast it to boolean
115 $enableQuotes = (boolean) $enableQuotes;
118 @set_magic_quotes_runtime($enableQuotes);
122 * A private include loader
124 * @param $arrayObject The array object with all include files
127 private function loadIncludes (ArrayObject $arrayObject) {
128 // Load only if there are includes defined
129 if (!is_null($arrayObject)) {
130 for ($idx = $arrayObject->getIterator(); $idx->valid(); $idx->next()) {
132 $inc = $idx->current();
134 // Is the file name really set?
136 // Base path added? (Uni* / Windows)
137 if ((substr($inc, 0, 1) != "/") && (substr($inc, 1, 1) != ":")) {
139 $fqfn = sprintf("%s/inc/extra/%s", PATH, $inc);
141 // Base path is already added
146 // Include them all here
153 * Define the database type which must be valid and will not be verified.
155 * @param $type The database type. See path inc/database/.
158 public function defineDatabaseType ($type) {
159 // Is it defined or not?
160 if (!defined('_DB_TYPE')) {
162 $type = (string) $type;
165 define('_DB_TYPE', $type);
167 // Already defined! But we cannot throw an exception here... :(
168 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the database type only once in your application!",
175 * Define the local file path
177 * @param $path Local file path for include files.
180 public function definePath ($path) {
182 $path = (string) $path;
184 // Replace backslashes with slashes
185 $path = str_replace("\\", "/", $path);
187 // Is it defined or not?
188 if ((!is_dir($path)) || (!is_readable($path))) {
189 // Is not a valid path
190 ApplicationEntryPoint::app_die(sprintf("[%s:] Invalid path (not found) specified. Please make sure it is created.",
193 } elseif (!defined('PATH')) {
195 define('PATH', $path);
197 // Already defined! But we cannot throw an exception here... :(
198 ApplicationEntryPoint::app_die(sprintf("[%s:] Please define the local file path only once in your application.",
205 * Read a configuration element.
207 * @param $cfgEntry The configuration element
208 * @return $cfgValue The fetched configuration value
209 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
210 * @throws ConfigEntryNotFoundException If a configuration element
213 public function readConfig ($cfgEntry) {
215 $cfgEntry = (string) $cfgEntry;
217 // Is a valid configuration entry provided?
218 if (empty($cfgEntry)) {
220 throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
221 } elseif (!isset($this->config[$cfgEntry])) {
222 // Entry was not found!
223 throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
227 if ((defined('DEBUG_CONFIG')) || (defined('DEBUG_ALL'))) {
228 echo "[".__METHOD__."] Configuration entry ".$cfgEntry." requested.<br />\n";
231 // Return the requested value
232 return $this->config[$cfgEntry];
236 * Set a configuration entry.
238 * @param $cfgEntry The configuration entry we want to add/change
239 * @param $cfgValue The configuration value we want to set
241 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
243 public final function setConfigEntry ($cfgEntry, $cfgValue) {
245 $cfgEntry = (string) $cfgEntry;
246 $cfgValue = (string) $cfgValue;
248 // Is a valid configuration entry provided?
249 if (empty($cfgEntry)) {
251 throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
254 // Set the configuration value
255 $this->config[$cfgEntry] = $cfgValue;
258 ksort($this->config);
262 * Compatiblity method to return this class' name
264 * @return __CLASS__ This class' name
266 public function __toString () {
267 return get_class($this);
271 * Dectect and return the base URL for all URLs and forms
273 * @return $baseUrl Detected base URL
275 public function detectBaseUrl() {
276 // Initialize the URL
280 if (isset($_SERVER['HTTPS'])) {
281 // Add the >s< for HTTPS
285 // Construct the full URL now and secure it against CSRF attacks
286 $baseUrl = $baseUrl . "://" . $this->detectDomain() . dirname($_SERVER['SCRIPT_NAME']);
293 * Detect safely and return the full domain where this script is installed
295 * @return $fullDomain The detected full domain
297 public function detectDomain () {
298 // Detect the full domain
299 $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES);