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@shipsimu.org>
11 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
12 * @license GNU GPL 3.0 or any newer version
13 * @link http://www.shipsimu.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 * Compatiblity method to return this class' name
57 * @return __CLASS__ This class' name
59 public function __toString () {
60 return get_class($this);
64 * Getter for a singleton instance of this class
66 * @return $configInstance A singleton instance of this class
68 public static final function getSelfInstance () {
69 // is the instance there?
70 if (is_null(self::$configInstance)) {
71 // Create a config instance
72 self::$configInstance = new FrameworkConfiguration();
75 // Return singleton instance
76 return self::$configInstance;
80 * Converts dashes to underscores, e.g. useable for configuration entries
82 * @param $str The string with maybe dashes inside
83 * @return $str The converted string with no dashed, but underscores
85 private final function convertDashesToUnderscores ($str) {
87 $str = str_replace('-', '_', $str);
89 // Return converted string
94 * Setter for default time zone (must be correct!)
96 * @param $zone The time-zone string (e.g. Europe/Berlin)
99 public final function setDefaultTimezone ($zone) {
100 // Is PHP version 5.1.0 or higher? Older versions are being ignored
101 if (version_compare(phpversion(), '5.1.0', '>=')) {
103 * Set desired time zone to prevent date() and related functions to
106 date_default_timezone_set($zone);
111 * Setter for runtime magic quotes
113 * @param $enableQuotes Whether enable magic runtime quotes (should be disabled for security reasons)
115 * @todo This method encapsulates a deprecated PHP function and should be deprecated, too.
117 public final function setMagicQuotesRuntime ($enableQuotes) {
118 // Is the PHP version < 5.4?
119 if (version_compare(phpversion(), '5.4', '>=')) {
120 // Then silently skip this part as set_magic_quotes_runtime() is deprecated
124 // Cast it to boolean
125 $enableQuotes = (boolean) $enableQuotes;
128 set_magic_quotes_runtime($enableQuotes);
132 * Checks whether the given configuration entry is set
134 * @param $configEntry The configuration entry we shall check
135 * @return $isset Whether the given configuration entry is set
137 public function isConfigurationEntrySet ($configEntry) {
139 $isset = isset($this->config[$configEntry]);
146 * Read a configuration element.
148 * @param $configEntry The configuration element
149 * @return $configValue The fetched configuration value
150 * @throws ConfigEntryIsEmptyException If $configEntry is empty
151 * @throws NoConfigEntryException If a configuration element was not found
153 public function getConfigEntry ($configEntry) {
154 // Convert dashes to underscore
155 $configEntry = $this->convertDashesToUnderscores($configEntry);
157 // Is a valid configuration entry provided?
158 if (empty($configEntry)) {
160 throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
161 } elseif (!$this->isConfigurationEntrySet($configEntry)) {
162 // Entry was not found!
163 throw new NoConfigEntryException(array(__CLASS__, $configEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
166 // Return the requested value
167 return $this->config[$configEntry];
171 * Set a configuration entry
173 * @param $configEntry The configuration entry we want to add/change
174 * @param $configValue The configuration value we want to set
176 * @throws ConfigEntryIsEmptyException If $configEntry is empty
178 public final function setConfigEntry ($configEntry, $configValue) {
180 $configEntry = $this->convertDashesToUnderscores($configEntry);
181 $configValue = (string) $configValue;
183 // Is a valid configuration entry provided?
184 if (empty($configEntry)) {
186 throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
189 // Set the configuration value
190 //* NOISY-DEBUG: */ print(__METHOD__ . ':configEntry=' . $configEntry . ',configValue=' . $configValue . PHP_EOL);
191 $this->config[$configEntry] = $configValue;
194 ksort($this->config);
198 * Unset a configuration entry, the entry must be there or else an
199 * exception is thrown.
201 * @param $configEntry Configuration entry to unset
203 * @throws NoConfigEntryException If a configuration element was not found
205 public final function unsetConfigEntry ($configEntry) {
206 // Convert dashes to underscore
207 $configEntry = $this->convertDashesToUnderscores($configEntry);
209 // Is the configuration entry there?
210 if (!$this->isConfigurationEntrySet($configEntry)) {
211 // Entry was not found!
212 throw new NoConfigEntryException(array(__CLASS__, $configEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
216 unset($this->config[$configEntry]);
220 * Detects the server address (SERVER_ADDR) and set it in configuration
222 * @return $serverAddress The detected server address
223 * @todo We have to add some more entries from $_SERVER here
225 public function detectServerAddress () {
227 if (!$this->isConfigurationEntrySet('server_addr')) {
228 // Is it set in $_SERVER?
229 if (isset($_SERVER['SERVER_ADDR'])) {
230 // Set it from $_SERVER
231 $this->setServerAddress($_SERVER['SERVER_ADDR']);
232 } elseif (class_exists('ConsoleTools')) {
233 // Run auto-detecting through console tools lib
234 ConsoleTools::acquireSelfIPAddress();
238 // Now get it from configuration
239 $serverAddress = $this->getServerAddress();
242 return $serverAddress;
246 * Setter for SERVER_ADDR
248 * @param $serverAddress New SERVER_ADDR value to set
251 public function setServerAddress ($serverAddress) {
252 $this->setConfigEntry('server_addr', (string) $serverAddress);
256 * Getter for SERVER_ADDR
258 * @return $serverAddress New SERVER_ADDR value to set
260 public function getServerAddress () {
261 return $this->getConfigEntry('server_addr');
265 * Detects the HTTPS flag
267 * @return $https The detected HTTPS flag or null if failed
269 public function detectHttpSecured () {
274 if ($this->isHttpSecured()) {
276 $https = $_SERVER['HTTPS'];
284 * Checks whether HTTPS is set in $_SERVER
286 * @return $isset Whether HTTPS is set
288 public function isHttpSecured () {
289 return (isset($_SERVER['HTTPS']));
293 * Dectect and return the base URL for all URLs and forms
295 * @return $baseUrl Detected base URL
297 public function detectBaseUrl () {
298 // Initialize the URL
302 if ($this->isHttpSecured()) {
303 // Add the >s< for HTTPS
307 // Construct the full URL and secure it against CSRF attacks
308 $baseUrl = $baseUrl . '://' . $this->detectDomain() . $this->detectScriptPath();
315 * Detect safely and return the full domain where this script is installed
317 * @return $fullDomain The detected full domain
319 public function detectDomain () {
320 // Full domain is localnet.invalid by default
321 $fullDomain = 'localnet.invalid';
323 // Is the server name there?
324 if (isset($_SERVER['SERVER_NAME'])) {
325 // Detect the full domain
326 $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES);
334 * Detect safely the script path without trailing slash which is the glue
335 * between "http://your-domain.invalid/" and "script-name.php"
337 * @return $scriptPath The script path extracted from $_SERVER['SCRIPT_NAME']
339 public function detectScriptPath () {
343 // Is the scriptname set?
344 if (isset($_SERVER['SCRIPT_NAME'])) {
345 // Get dirname from it and replace back-slashes with slashes for lame OSes...
346 $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME']));
354 * Getter for field name
356 * @param $fieldName Field name which we shall get
357 * @return $fieldValue Field value from the user
358 * @throws NullPointerException If the result instance is null
360 public final function getField ($fieldName) {
361 // Our super interface "FrameworkInterface" requires this
365 * Generates a code for hashes from this class
367 * @return $hashCode The hash code respresenting this class
369 public function hashCode () {
370 return crc32($this->__toString());
374 * Checks whether an object equals this object. You should overwrite this
375 * method to implement own equality checks
377 * @param $objectInstance An instance of a FrameworkInterface object
378 * @return $equals Whether both objects equals
380 public function equals (FrameworkInterface $objectInstance) {
383 $this->__toString() == $objectInstance->__toString()
385 $this->hashCode() == $objectInstance->hashCode()