Fixed a typo
[core.git] / inc / config / class_FrameworkConfiguration.php
1 <?php
2 /**
3  * A class for the configuration stuff implemented in a singleton design paddern
4  *
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
7  *
8  * @see                 ClassLoader
9  * @author              Roland Haeder <webmaster@ship-simu.org>
10  * @version             0.0.0
11  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
12  * @license             GNU GPL 3.0 or any newer version
13  * @link                http://www.ship-simu.org
14  *
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.
19  *
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.
24  *
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/>.
27  */
28 class FrameworkConfiguration implements Registerable {
29         /**
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.
33          */
34         private $config = array();
35
36         /**
37          * The configuration instance itself
38          */
39         private static $configInstance = NULL;
40
41         // Some constants for the configuration system
42         const EXCEPTION_CONFIG_ENTRY_IS_EMPTY      = 0x130;
43         const EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND = 0x131;
44
45         /**
46          * Protected constructor
47          *
48          * @return      void
49          */
50         protected function __construct () {
51                 // Empty for now
52         }
53
54         /**
55          * Compatiblity method to return this class' name
56          *
57          * @return      __CLASS__               This class' name
58          */
59         public function __toString () {
60                 return get_class($this);
61         }
62
63         /**
64          * Getter for an instance of this class
65          *
66          * @return      $configInstance An instance of this class
67          */
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();
73                 } // END - if
74
75                 return self::$configInstance;
76         }
77
78         /**
79          * Setter for default time zone (must be correct!)
80          *
81          * @param       $zone   The time-zone string (e.g. Europe/Berlin)
82          * @return      void
83          */
84         public final function setDefaultTimezone ($zone) {
85                 // At least 5.1.0 is required for this!
86                 if (version_compare(phpversion(), '5.1.0')) {
87                         date_default_timezone_set($zone);
88                 } // END - if
89         }
90
91         /**
92          * Setter for runtime magic quotes
93          *
94          * @param       $enableQuotes   Whether enable magic runtime quotes (should be disabled for security reasons)
95          * @return      void
96          */
97         public final function setMagicQuotesRuntime ($enableQuotes) {
98                 // Cast it to boolean
99                 $enableQuotes = (boolean) $enableQuotes;
100
101                 // Set it
102                 set_magic_quotes_runtime($enableQuotes);
103         }
104
105         /**
106          * Checks whether the given configuration entry is set
107          *
108          * @param       $configEntry    The configuration entry we shall check
109          * @return      $isset                  Whether the given configuration entry is set
110          */
111         protected function isConfigurationEntrySet ($configEntry) {
112                 // Is it set?
113                 $isset = isset($this->config[$configEntry]);
114
115                 // Return the result
116                 return $isset;
117         }
118
119         /**
120          * Read a configuration element.
121          *
122          * @param       $cfgEntry       The configuration element
123          * @return      $cfgValue       The fetched configuration value
124          * @throws      ConfigEntryIsEmptyException             If $cfgEntry is empty
125          * @throws      NoConfigEntryException  If a configuration element
126          *                                                                                      was not found
127          */
128         public function getConfigEntry ($cfgEntry) {
129                 // Cast to string
130                 $cfgEntry = (string) $cfgEntry;
131
132                 // Is a valid configuration entry provided?
133                 if (empty($cfgEntry)) {
134                         // Entry is empty
135                         throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
136                 } elseif (!$this->isConfigurationEntrySet($cfgEntry)) {
137                         // Entry was not found!
138                         throw new NoConfigEntryException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
139                 }
140
141                 // Return the requested value
142                 return $this->config[$cfgEntry];
143         }
144
145         /**
146          * Set a configuration entry.
147          *
148          * @param       $cfgEntry       The configuration entry we want to add/change
149          * @param       $cfgValue       The configuration value we want to set
150          * @return      void
151          * @throws      ConfigEntryIsEmptyException     If $cfgEntry is empty
152          */
153         public final function setConfigEntry ($cfgEntry, $cfgValue) {
154                 // Cast to string
155                 $cfgEntry = (string) $cfgEntry;
156                 $cfgValue = (string) $cfgValue;
157
158                 // Is a valid configuration entry provided?
159                 if (empty($cfgEntry)) {
160                         // Entry is empty
161                         throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
162                 } // END - if
163
164                 // Set the configuration value
165                 $this->config[$cfgEntry] = $cfgValue;
166
167                 // Resort the array
168                 ksort($this->config);
169         }
170
171         /**
172          * Detects the server address (SERVER_ADDR) and set it in configuration
173          *
174          * @return      $serverAddress  The detected server address
175          * @todo        We have to add some more entries from $_SERVER here
176          */
177         public function detectServerAddress () {
178                 // Is the entry set?
179                 if (!$this->isConfigurationEntrySet('server_addr')) {
180                         // Is it set in $_SERVER?
181                         if (isset($_SERVER['SERVER_ADDR'])) {
182                                 // Set it from $_SERVER
183                                 $this->setServerAddress($_SERVER['SERVER_ADDR']);
184                         } elseif (class_exists('ConsoleTools')) {
185                                 // Run auto-detecting through console tools lib
186                                 ConsoleTools::acquireSelfIPAddress();
187                         }
188                 } // END - if
189
190                 // Now get it from configuration
191                 $serverAddress = $this->getServerAddress();
192
193                 // Return it
194                 return $serverAddress;
195         }
196
197         /**
198          * Setter for SERVER_ADDR
199          *
200          * @param       $serverAddress  New SERVER_ADDR value to set
201          * @return      void
202          */
203         public function setServerAddress ($serverAddress) {
204                 $this->setConfigEntry('server_addr', (string) $serverAddress);
205         }
206
207         /**
208          * Getter for SERVER_ADDR
209          *
210          * @return      $serverAddress  New SERVER_ADDR value to set
211          */
212         public function getServerAddress () {
213                 return $this->getConfigEntry('server_addr');
214         }
215
216         /**
217          * Detects the HTTPS flag
218          *
219          * @return      $https  The detected HTTPS flag or null if failed
220          */
221         public function detectHttpSecured () {
222                 // Default is null
223                 $https = NULL;
224
225                 // Is HTTPS set?
226                 if ($this->isHttpSecured()) {
227                         // Then use it
228                         $https = $_SERVER['HTTPS'];
229                 } // END - if
230
231                 // Return it
232                 return $https;
233         }
234
235         /**
236          * Checks whether HTTPS is set in $_SERVER
237          *
238          * @return $isset       Whether HTTPS is set
239          */
240         public function isHttpSecured () {
241                 return (isset($_SERVER['HTTPS']));
242         }
243
244         /**
245          * Dectect and return the base URL for all URLs and forms
246          *
247          * @return      $baseUrl        Detected base URL
248          */
249         public function detectBaseUrl () {
250                 // Initialize the URL
251                 $baseUrl = 'http';
252
253                 // Do we have HTTPS?
254                 if ($this->isHttpSecured()) {
255                         // Add the >s< for HTTPS
256                         $baseUrl .= 's';
257                 } // END - if
258
259                 // Construct the full URL and secure it against CSRF attacks
260                 $baseUrl = $baseUrl . '://' . $this->detectDomain() . $this->detectScriptPath();
261
262                 // Return the URL
263                 return $baseUrl;
264         }
265
266         /**
267          * Detect safely and return the full domain where this script is installed
268          *
269          * @return      $fullDomain             The detected full domain
270          */
271         public function detectDomain () {
272                 // Full domain is localnet.invalid by default
273                 $fullDomain = 'localnet.invalid';
274
275                 // Is the server name there?
276                 if (isset($_SERVER['SERVER_NAME'])) {
277                         // Detect the full domain
278                         $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES);
279                 } // END - if
280
281                 // Return it
282                 return $fullDomain;
283         }
284
285         /**
286          * Detect safely the script path without trailing slash which is the glue
287          * between "http://your-domain.invalid/" and "script-name.php"
288          *
289          * @return      $scriptPath             The script path extracted from $_SERVER['SCRIPT_NAME']
290          */
291         public function detectScriptPath () {
292                 // Default is empty
293                 $scriptPath = '';
294
295                 // Is the scriptname set?
296                 if (isset($_SERVER['SCRIPT_NAME'])) {
297                         // Get dirname from it and replace back-slashes with slashes for lame OSes...
298                         $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME']));
299                 } // END - if
300
301                 // Return it
302                 return $scriptPath;
303         }
304
305         /**
306          * Getter for field name
307          *
308          * @param       $fieldName              Field name which we shall get
309          * @return      $fieldValue             Field value from the user
310          * @throws      NullPointerException    If the result instance is null
311          */
312         public final function getField ($fieldName) {
313                 // Our super interface "FrameworkInterface" requires this
314         }
315
316         /**
317          * Generates a code for hashes from this class
318          *
319          * @return      $hashCode       The hash code respresenting this class
320          */
321         public function hashCode () {
322                 return crc32($this->__toString());
323         }
324
325         /**
326          * Checks whether an object equals this object. You should overwrite this
327          * method to implement own equality checks
328          *
329          * @param       $objectInstance         An instance of a FrameworkInterface object
330          * @return      $equals                         Whether both objects equals
331          */
332         public function equals (FrameworkInterface $objectInstance) {
333                 // Now test it
334                 $equals = ((
335                         $this->__toString() == $objectInstance->__toString()
336                 ) && (
337                         $this->hashCode() == $objectInstance->hashCode()
338                 ));
339
340                 // Return the result
341                 return $equals;
342         }
343 }
344
345 // [EOF]
346 ?>