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) in
6 * 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, 2009 Core Developer Team
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 $configInstance = 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 * Getter for an instance of this class
57 * @return $configInstance An instance of this class
59 public final static function getInstance () {
60 // is the instance there?
61 if (is_null(self::$configInstance)) {
62 // Create a config instance
63 self::$configInstance = new FrameworkConfiguration();
66 return self::$configInstance;
70 * Setter for default time zone (must be correct!)
72 * @param $zone The time-zone string (e.g. Europe/Berlin)
75 public final function setDefaultTimezone ($zone) {
76 // At least 5.1.0 is required for this!
77 if (version_compare(phpversion(), "5.1.0")) {
78 @date_default_timezone_set($zone);
83 * Setter for runtime magic quotes
85 public final function setMagicQuotesRuntime ($enableQuotes) {
87 $enableQuotes = (boolean) $enableQuotes;
90 @set_magic_quotes_runtime($enableQuotes);
94 * A private include loader
96 * @param $arrayObject The array object with all include files
99 private function loadIncludes (ArrayObject $arrayObject) {
100 // Load only if there are includes defined
101 if (!is_null($arrayObject)) {
102 for ($idx = $arrayObject->getIterator(); $idx->valid(); $idx->next()) {
104 $inc = $idx->current();
106 // Is the file name really set?
108 // Base path is by default added
111 // Base path added? (Uni* / Windows)
112 if ((substr($inc, 0, 1) != '/') && (substr($inc, 1, 1) != ':')) {
114 $fqfn = $this->getConfigEntry('base_path') . '/inc/extra/' . $inc;
118 // Include them all here
125 * Read a configuration element.
127 * @param $cfgEntry The configuration element
128 * @return $cfgValue The fetched configuration value
129 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
130 * @throws ConfigEntryNotFoundException If a configuration element
133 public function getConfigEntry ($cfgEntry) {
135 $cfgEntry = (string) $cfgEntry;
137 // Is a valid configuration entry provided?
138 if (empty($cfgEntry)) {
140 throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
141 } elseif (!isset($this->config[$cfgEntry])) {
142 // Entry was not found!
143 throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
146 // Return the requested value
147 return $this->config[$cfgEntry];
151 * Set a configuration entry.
153 * @param $cfgEntry The configuration entry we want to add/change
154 * @param $cfgValue The configuration value we want to set
156 * @throws ConfigEntryIsEmptyException If $cfgEntry is empty
158 public final function setConfigEntry ($cfgEntry, $cfgValue) {
160 $cfgEntry = (string) $cfgEntry;
161 $cfgValue = (string) $cfgValue;
163 // Is a valid configuration entry provided?
164 if (empty($cfgEntry)) {
166 throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
169 // Set the configuration value
170 $this->config[$cfgEntry] = $cfgValue;
173 ksort($this->config);
177 * Compatiblity method to return this class' name
179 * @return __CLASS__ This class' name
181 public function __toString () {
182 return get_class($this);
186 * Dectect and return the base URL for all URLs and forms
188 * @return $baseUrl Detected base URL
190 public function detectBaseUrl() {
191 // Initialize the URL
195 if (isset($_SERVER['HTTPS'])) {
196 // Add the >s< for HTTPS
200 // Construct the full URL and secure it against CSRF attacks
201 $baseUrl = $baseUrl . '://' . $this->detectDomain() . $this->detectScriptPath();
208 * Detect safely and return the full domain where this script is installed
210 * @return $fullDomain The detected full domain
212 public function detectDomain () {
213 // Full domain is localnet.invalid by default
214 $fullDomain = 'localnet.invalid';
216 // Is the server name there?
217 if (isset($_SERVER['SERVER_NAME'])) {
218 // Detect the full domain
219 $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES);
227 * Detect safely the script path without trailing slash which is the glue
228 * between "http://your-domain.invalid/" and "script-name.php"
230 * @return $scriptPath The script path extracted from $_SERVER['SCRIPT_NAME']
232 public function detectScriptPath () {
236 // Is the scriptname set?
237 if (isset($_SERVER['SCRIPT_NAME'])) {
238 // Get dirname from it and replace back-slashes with slashes for lame OSes...
239 $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME']));
247 * Getter for field name
249 * @param $fieldName Field name which we shall get
250 * @return $fieldValue Field value from the user
251 * @throws NullPointerException If the result instance is null
253 public final function getField ($fieldName) {
254 // Our super interface "FrameworkInterface" requires this