c96e851961f113ab1b420f4f95a94635f08b1077
[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 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 final static function getInstance () {
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         public final function setMagicQuotesRuntime ($enableQuotes) {
95                 // Cast it to boolean
96                 $enableQuotes = (boolean) $enableQuotes;
97
98                 // Set it
99                 set_magic_quotes_runtime($enableQuotes);
100         }
101
102         /**
103          * A private include loader
104          *
105          * @param       $arrayObject    The array object with all include files
106          * @return      void
107          * @deprecated
108          * @see         ClassLoader
109          */
110         private function loadIncludes (ArrayObject $arrayObject) {
111                 // Load only if there are includes defined
112                 if (!is_null($arrayObject)) {
113                         for ($idx = $arrayObject->getIterator(); $idx->valid(); $idx->next()) {
114                                 // Get include file
115                                 $inc = $idx->current();
116
117                                 // Is the file name really set?
118                                 if (!empty($inc)) {
119                                         // Base path is by default added
120                                         $fqfn = $inc;
121
122                                         // Base path added? (Uni* / Windows)
123                                         if ((substr($inc, 0, 1) != '/') && (substr($inc, 1, 1) != ':')) {
124                                                 // Generate FQFN
125                                                 $fqfn = $this->getConfigEntry('base_path') . '/inc/extra/' . $inc;
126                                         } // END - if
127                                 } // END - if
128
129                                 // Include them all here
130                                 require($fqfn);
131                         }
132                 } // END - if
133         }
134
135         /**
136          * Checks wether the given configuration entry is set
137          *
138          * @param       $configEntry    The configuration entry we shall check
139          * @return      $isset                  Wether the given configuration entry is set
140          */
141         protected function isConfigurationEntrySet ($configEntry) {
142                 // Is it set?
143                 $isset = isset($this->config[$configEntry]);
144
145                 // Return the result
146                 return $isset;
147         }
148
149         /**
150          * Read a configuration element.
151          *
152          * @param       $cfgEntry       The configuration element
153          * @return      $cfgValue       The fetched configuration value
154          * @throws      ConfigEntryIsEmptyException             If $cfgEntry is empty
155          * @throws      ConfigEntryNotFoundException    If a configuration element
156          *                                                                                      was not found
157          */
158         public function getConfigEntry ($cfgEntry) {
159                 // Cast to string
160                 $cfgEntry = (string) $cfgEntry;
161
162                 // Is a valid configuration entry provided?
163                 if (empty($cfgEntry)) {
164                         // Entry is empty
165                         throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
166                 } elseif (!$this->isConfigurationEntrySet($cfgEntry)) {
167                         // Entry was not found!
168                         throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
169                 }
170
171                 // Return the requested value
172                 return $this->config[$cfgEntry];
173         }
174
175         /**
176          * Set a configuration entry.
177          *
178          * @param       $cfgEntry       The configuration entry we want to add/change
179          * @param       $cfgValue       The configuration value we want to set
180          * @return      void
181          * @throws      ConfigEntryIsEmptyException     If $cfgEntry is empty
182          */
183         public final function setConfigEntry ($cfgEntry, $cfgValue) {
184                 // Cast to string
185                 $cfgEntry = (string) $cfgEntry;
186                 $cfgValue = (string) $cfgValue;
187
188                 // Is a valid configuration entry provided?
189                 if (empty($cfgEntry)) {
190                         // Entry is empty
191                         throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
192                 } // END - if
193
194                 // Set the configuration value
195                 $this->config[$cfgEntry] = $cfgValue;
196
197                 // Resort the array
198                 ksort($this->config);
199         }
200
201         /**
202          * Detects the server address (SERVER_ADDR) and set it in configuration
203          *
204          * @return      $serverAddress  The detected server address
205          * @todo        We have to add some more entries from $_SERVER here
206          */
207         public function detectServerAddress () {
208                 // Is the entry set?
209                 if (!$this->isConfigurationEntrySet('server_addr')) {
210                         // Is it set in $_SERVER?
211                         if (isset($_SERVER['SERVER_ADDR'])) {
212                                 // Set it from $_SERVER
213                                 $this->setServerAddress($_SERVER['SERVER_ADDR']);
214                         } elseif (class_exists('ConsoleTools')) {
215                                 // Run auto-detecting through console tools lib
216                                 ConsoleTools::acquireSelfIPAddress();
217                         }
218                 } // END - if
219
220                 // Now get it from configuration
221                 $serverAddress = $this->getServerAddress();
222
223                 // Return it
224                 return $serverAddress;
225         }
226
227         /**
228          * Setter for SERVER_ADDR
229          *
230          * @param       $serverAddress  New SERVER_ADDR value to set
231          * @return      void
232          */
233         public function setServerAddress ($serverAddress) {
234                 $this->setConfigEntry('server_addr', (string) $serverAddress);
235         }
236
237         /**
238          * Getter for SERVER_ADDR
239          *
240          * @return      $serverAddress  New SERVER_ADDR value to set
241          */
242         public function getServerAddress () {
243                 return $this->getConfigEntry('server_addr');
244         }
245
246         /**
247          * Detects the HTTPS flag
248          *
249          * @return      $https  The detected HTTPS flag or null if failed
250          */
251         public function detectHttpSecured () {
252                 // Default is null
253                 $https = null;
254
255                 // Is HTTPS set?
256                 if ($this->isHttpSecured()) {
257                         // Then use it
258                         $https = $_SERVER['HTTPS'];
259                 } // END - if
260
261                 // Return it
262                 return $https;
263         }
264
265         /**
266          * Checks wether HTTPS is set in $_SERVER
267          *
268          * @return $isset       Wether HTTPS is set
269          */
270         public function isHttpSecured () {
271                 return (isset($_SERVER['HTTPS']));
272         }
273
274         /**
275          * Dectect and return the base URL for all URLs and forms
276          *
277          * @return      $baseUrl        Detected base URL
278          */
279         public function detectBaseUrl () {
280                 // Initialize the URL
281                 $baseUrl = 'http';
282
283                 // Do we have HTTPS?
284                 if ($this->isHttpSecured()) {
285                         // Add the >s< for HTTPS
286                         $baseUrl .= 's';
287                 } // END - if
288
289                 // Construct the full URL and secure it against CSRF attacks
290                 $baseUrl = $baseUrl . '://' . $this->detectDomain() . $this->detectScriptPath();
291
292                 // Return the URL
293                 return $baseUrl;
294         }
295
296         /**
297          * Detect safely and return the full domain where this script is installed
298          *
299          * @return      $fullDomain             The detected full domain
300          */
301         public function detectDomain () {
302                 // Full domain is localnet.invalid by default
303                 $fullDomain = 'localnet.invalid';
304
305                 // Is the server name there?
306                 if (isset($_SERVER['SERVER_NAME'])) {
307                         // Detect the full domain
308                         $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES);
309                 } // END - if
310
311                 // Return it
312                 return $fullDomain;
313         }
314
315         /**
316          * Detect safely the script path without trailing slash which is the glue
317          * between "http://your-domain.invalid/" and "script-name.php"
318          *
319          * @return      $scriptPath             The script path extracted from $_SERVER['SCRIPT_NAME']
320          */
321         public function detectScriptPath () {
322                 // Default is empty
323                 $scriptPath = '';
324
325                 // Is the scriptname set?
326                 if (isset($_SERVER['SCRIPT_NAME'])) {
327                         // Get dirname from it and replace back-slashes with slashes for lame OSes...
328                         $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME']));
329                 } // END - if
330
331                 // Return it
332                 return $scriptPath;
333         }
334
335         /**
336          * Getter for field name
337          *
338          * @param       $fieldName              Field name which we shall get
339          * @return      $fieldValue             Field value from the user
340          * @throws      NullPointerException    If the result instance is null
341          */
342         public final function getField ($fieldName) {
343                 // Our super interface "FrameworkInterface" requires this
344         }
345
346         /**
347          * Generates a code for hashes from this class
348          *
349          * @return      $hashCode       The hash code respresenting this class
350          */
351         public function hashCode () {
352                 return crc32($this->__toString());
353         }
354 }
355
356 //
357 ?>