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