7692132a1941dc9647206660909182360d510454
[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          * Getter for an instance of this class
56          *
57          * @return      $configInstance An instance of this class
58          */
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();
64                 } // END - if
65
66                 return self::$configInstance;
67         }
68
69         /**
70          * Setter for default time zone (must be correct!)
71          *
72          * @param               $zone   The time-zone string (e.g. Europe/Berlin)
73          * @return      void
74          */
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);
79                 } // END - if
80         }
81
82         /**
83          * Setter for runtime magic quotes
84          */
85         public final function setMagicQuotesRuntime ($enableQuotes) {
86                 // Cast it to boolean
87                 $enableQuotes = (boolean) $enableQuotes;
88
89                 // Set it
90                 set_magic_quotes_runtime($enableQuotes);
91         }
92
93         /**
94          * A private include loader
95          *
96          * @param       $arrayObject    The array object with all include files
97          * @return      void
98          * @deprecated
99          * @see         ClassLoader
100          */
101         private function loadIncludes (ArrayObject $arrayObject) {
102                 // Load only if there are includes defined
103                 if (!is_null($arrayObject)) {
104                         for ($idx = $arrayObject->getIterator(); $idx->valid(); $idx->next()) {
105                                 // Get include file
106                                 $inc = $idx->current();
107
108                                 // Is the file name really set?
109                                 if (!empty($inc)) {
110                                         // Base path is by default added
111                                         $fqfn = $inc;
112
113                                         // Base path added? (Uni* / Windows)
114                                         if ((substr($inc, 0, 1) != '/') && (substr($inc, 1, 1) != ':')) {
115                                                 // Generate FQFN
116                                                 $fqfn = $this->getConfigEntry('base_path') . '/inc/extra/' . $inc;
117                                         } // END - if
118                                 } // END - if
119
120                                 // Include them all here
121                                 require($fqfn);
122                         }
123                 } // END - if
124         }
125
126         /**
127          * Read a configuration element.
128          *
129          * @param       $cfgEntry       The configuration element
130          * @return      $cfgValue       The fetched configuration value
131          * @throws      ConfigEntryIsEmptyException             If $cfgEntry is empty
132          * @throws      ConfigEntryNotFoundException    If a configuration element
133          *                                                                                      was not found
134          */
135         public function getConfigEntry ($cfgEntry) {
136                 // Cast to string
137                 $cfgEntry = (string) $cfgEntry;
138
139                 // Is a valid configuration entry provided?
140                 if (empty($cfgEntry)) {
141                         // Entry is empty
142                         throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
143                 } elseif (!isset($this->config[$cfgEntry])) {
144                         // Entry was not found!
145                         throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
146                 }
147
148                 // Return the requested value
149                 return $this->config[$cfgEntry];
150         }
151
152         /**
153          * Set a configuration entry.
154          *
155          * @param       $cfgEntry       The configuration entry we want to add/change
156          * @param       $cfgValue       The configuration value we want to set
157          * @return      void
158          * @throws      ConfigEntryIsEmptyException     If $cfgEntry is empty
159          */
160         public final function setConfigEntry ($cfgEntry, $cfgValue) {
161                 // Cast to string
162                 $cfgEntry = (string) $cfgEntry;
163                 $cfgValue = (string) $cfgValue;
164
165                 // Is a valid configuration entry provided?
166                 if (empty($cfgEntry)) {
167                         // Entry is empty
168                         throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
169                 } // END - if
170
171                 // Set the configuration value
172                 $this->config[$cfgEntry] = $cfgValue;
173
174                 // Resort the array
175                 ksort($this->config);
176         }
177
178         /**
179          * Compatiblity method to return this class' name
180          *
181          * @return      __CLASS__               This class' name
182          */
183         public function __toString () {
184                 return get_class($this);
185         }
186
187         /**
188          * Setter for SERVER_ADDR
189          *
190          * @param       $serverAddress  New SERVER_ADDR value to set
191          * @return      void
192          */
193         public function setServerAddress ($serverAddress) {
194                 $this->setConfigEntry('server_addr', (string) $serverAddress);
195         }
196
197         /**
198          * Getter for SERVER_ADDR
199          *
200          * @return      $serverAddress  New SERVER_ADDR value to set
201          */
202         public function getServerAddress () {
203                 return $this->getConfigEntry('server_addr');
204         }
205
206         /**
207          * Detects the HTTPS flag
208          *
209          * @return      $https  The detected HTTPS flag or null if failed
210          */
211         public function detectHttpSecured () {
212                 // Default is null
213                 $https = null;
214
215                 // Is HTTPS set?
216                 if ($this->isHttpSecured()) {
217                         // Then use it
218                         $https = $_SERVER['HTTPS'];
219                 } // END - if
220
221                 // Return it
222                 return $https;
223         }
224
225         /**
226          * Checks wether HTTPS is set in $_SERVER
227          *
228          * @return $isset       Wether HTTPS is set
229          */
230         public function isHttpSecured () {
231                 return (isset($_SERVER['HTTPS']));
232         }
233
234         /**
235          * Dectect and return the base URL for all URLs and forms
236          *
237          * @return      $baseUrl        Detected base URL
238          */
239         public function detectBaseUrl () {
240                 // Initialize the URL
241                 $baseUrl = 'http';
242
243                 // Do we have HTTPS?
244                 if ($this->isHttpSecured()) {
245                         // Add the >s< for HTTPS
246                         $baseUrl .= 's';
247                 } // END - if
248
249                 // Construct the full URL and secure it against CSRF attacks
250                 $baseUrl = $baseUrl . '://' . $this->detectDomain() . $this->detectScriptPath();
251
252                 // Return the URL
253                 return $baseUrl;
254         }
255
256         /**
257          * Detect safely and return the full domain where this script is installed
258          *
259          * @return      $fullDomain             The detected full domain
260          */
261         public function detectDomain () {
262                 // Full domain is localnet.invalid by default
263                 $fullDomain = 'localnet.invalid';
264
265                 // Is the server name there?
266                 if (isset($_SERVER['SERVER_NAME'])) {
267                         // Detect the full domain
268                         $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES);
269                 } // END - if
270
271                 // Return it
272                 return $fullDomain;
273         }
274
275         /**
276          * Detect safely the script path without trailing slash which is the glue
277          * between "http://your-domain.invalid/" and "script-name.php"
278          *
279          * @return      $scriptPath             The script path extracted from $_SERVER['SCRIPT_NAME']
280          */
281         public function detectScriptPath () {
282                 // Default is empty
283                 $scriptPath = '';
284
285                 // Is the scriptname set?
286                 if (isset($_SERVER['SCRIPT_NAME'])) {
287                         // Get dirname from it and replace back-slashes with slashes for lame OSes...
288                         $scriptPath = str_replace("\\", '/', dirname($_SERVER['SCRIPT_NAME']));
289                 } // END - if
290
291                 // Return it
292                 return $scriptPath;
293         }
294
295         /**
296          * Getter for field name
297          *
298          * @param       $fieldName              Field name which we shall get
299          * @return      $fieldValue             Field value from the user
300          * @throws      NullPointerException    If the result instance is null
301          */
302         public final function getField ($fieldName) {
303                 // Our super interface "FrameworkInterface" requires this
304         }
305 }
306
307 // [EOF]
308 ?>