Evn more quotes rewritten, old try-catch-blocks removed from old tests
[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 - 2009 Roland Haeder, this is free software
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          */
99         private function loadIncludes (ArrayObject $arrayObject) {
100                 // Load only if there are includes defined
101                 if (!is_null($arrayObject)) {
102                         for ($idx = $arrayObject->getIterator(); $idx->valid(); $idx->next()) {
103                                 // Get include file
104                                 $inc = $idx->current();
105
106                                 // Is the file name really set?
107                                 if (!empty($inc)) {
108                                         // Base path is by default added
109                                         $fqfn = $inc;
110
111                                         // Base path added? (Uni* / Windows)
112                                         if ((substr($inc, 0, 1) != "/") && (substr($inc, 1, 1) != ":")) {
113                                                 // Generate FQFN
114                                                 $fqfn = sprintf("%s/inc/extra/%s", $this->readConfig('base_path'), $inc);
115                                         } // END - if
116                                 } // END - if
117
118                                 // Include them all here
119                                 require($fqfn);
120                         }
121                 } // END - if
122         }
123
124         /**
125          * Read a configuration element.
126          *
127          * @param       $cfgEntry       The configuration element
128          * @return      $cfgValue       The fetched configuration value
129          * @throws      ConfigEntryIsEmptyException             If $cfgEntry is empty
130          * @throws      ConfigEntryNotFoundException    If a configuration element
131          *                                                                                      was not found
132          */
133         public function readConfig ($cfgEntry) {
134                 // Cast to string
135                 $cfgEntry = (string) $cfgEntry;
136
137                 // Is a valid configuration entry provided?
138                 if (empty($cfgEntry)) {
139                         // Entry is empty
140                         throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
141                 } elseif (!isset($this->config[$cfgEntry])) {
142                         // Entry was not found!
143                         throw new ConfigEntryNotFoundException(array(__CLASS__, $cfgEntry), self::EXCEPTION_CONFIG_ENTRY_WAS_NOT_FOUND);
144                 }
145
146                 // Debug message
147                 if ((defined('DEBUG_CONFIG')) || (defined('DEBUG_ALL'))) {
148                         echo "[".__METHOD__."] Configuration entry ".$cfgEntry." requested.<br />\n";
149                 } // END - if
150
151                 // Return the requested value
152                 return $this->config[$cfgEntry];
153         }
154
155         /**
156          * Set a configuration entry.
157          *
158          * @param       $cfgEntry       The configuration entry we want to add/change
159          * @param       $cfgValue       The configuration value we want to set
160          * @return      void
161          * @throws      ConfigEntryIsEmptyException     If $cfgEntry is empty
162          */
163         public final function setConfigEntry ($cfgEntry, $cfgValue) {
164                 // Cast to string
165                 $cfgEntry = (string) $cfgEntry;
166                 $cfgValue = (string) $cfgValue;
167
168                 // Is a valid configuration entry provided?
169                 if (empty($cfgEntry)) {
170                         // Entry is empty
171                         throw new ConfigEntryIsEmptyException($this, self::EXCEPTION_CONFIG_ENTRY_IS_EMPTY);
172                 } // END - if
173
174                 // Set the configuration value
175                 $this->config[$cfgEntry] = $cfgValue;
176
177                 // Resort the array
178                 ksort($this->config);
179         }
180
181         /**
182          * Compatiblity method to return this class' name
183          *
184          * @return      __CLASS__               This class' name
185          */
186         public function __toString () {
187                 return get_class($this);
188         }
189
190         /**
191          * Dectect and return the base URL for all URLs and forms
192          *
193          * @return      $baseUrl        Detected base URL
194          */
195         public function detectBaseUrl() {
196                 // Initialize the URL
197                 $baseUrl = "http";
198
199                 // Do we have HTTPS?
200                 if (isset($_SERVER['HTTPS'])) {
201                         // Add the >s< for HTTPS
202                         $baseUrl .= "s";
203                 } // END - if
204
205                 // Construct the full URL now and secure it against CSRF attacks
206                 $baseUrl = $baseUrl . "://" . $this->detectDomain() . $this->detectScriptPath();
207
208                 // Return the URL
209                 return $baseUrl;
210         }
211
212         /**
213          * Detect safely and return the full domain where this script is installed
214          *
215          * @return      $fullDomain             The detected full domain
216          */
217         public function detectDomain () {
218                 // Full domain is localnet.invalid by default
219                 $fullDomain = "localnet.invalid";
220
221                 // Is the server name there?
222                 if (isset($_SERVER['SERVER_NAME'])) {
223                         // Detect the full domain
224                         $fullDomain = htmlentities(strip_tags($_SERVER['SERVER_NAME']), ENT_QUOTES);
225                 } // END - if
226
227                 // Return it
228                 return $fullDomain;
229         }
230
231         /**
232          * Detect safely the script path without trailing slash which is the glue
233          * between "http://your-domain.invalid/" and "script-name.php"
234          *
235          * @return      $scriptPath             The script path extracted from $_SERVER['SCRIPT_NAME']
236          */
237         public function detectScriptPath () {
238                 // Default is empty
239                 $scriptPath = '';
240
241                 // Is the scriptname set?
242                 if (isset($_SERVER['SCRIPT_NAME'])) {
243                         // Get dirname from it and replace back-slashes with slashes for lame OSes...
244                         $scriptPath = str_replace("\\", "/", dirname($_SERVER['SCRIPT_NAME']));
245                 } // END - if
246
247                 // Return it
248                 return $scriptPath;
249         }
250
251         /**
252          * Getter for field name
253          *
254          * @param       $fieldName              Field name which we shall get
255          * @return      $fieldValue             Field value from the user
256          * @throws      NullPointerException    If the result instance is null
257          */
258         public final function getField ($fieldName) {
259                 // Our super interface "FrameworkInterface" requires this
260         }
261 }
262
263 // [EOF]
264 ?>