Default language set to 'de'
[mailer.git] / inc / config-functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 02/28/2009 *
4  * ===============                              Last change: 02/28/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : config-functions.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Many non-MySQL functions (also file access)      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Viele Nicht-MySQL-Funktionen (auch Dateizugriff) *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 }
44
45 // Init the config array
46 function initConfig () {
47         // Init not if already found
48         if ((isset($GLOBALS['config'])) && (count($GLOBALS['config']) >= 3)) {
49                 // Already initialized
50                 trigger_error(sprintf("[%s:%s] Configuration is already initialized.", __FUNCTION__, __LINE__));
51         } // END - if
52
53         // Set a minimum of configuration, required to by-pass some errors triggers in getConfig()
54         $GLOBALS['config'] = array(
55                 'code_length'      => 0,
56                 'patch_level'      => 0,
57                 'last_update'      => time(),
58                 'DEFAULT_LANG'     => 'de',
59                 'DEBUG_MODE'       => 'N',
60                 'DEBUG_RESET'      => 'N',
61                 'DEBUG_MONTHLY'    => 'N',
62                 'DEBUG_WEEKLY'     => 'N',
63                 'DEBUG_REGEX'      => 'N',
64                 'ADMIN_REGISTERED' => 'N',
65                 'sql_count'        => 0,
66                 'num_templates'    => 0,
67                 'default_theme'    => 'default',
68         );
69 }
70
71 // Getter for $GLOBALS['config'] entries
72 function getConfig ($configEntry) {
73         // Default value
74         $value = null;
75
76         // Is the entry there?
77         if (!isConfigEntrySet($configEntry)) {
78                 // Raise an error of missing entries
79                 trigger_error(sprintf("[%s:%s] Configuration entry <em>%s</em> is missing.", __FUNCTION__, __LINE__, $configEntry));
80         } // END - if
81
82         // Return it
83         return $GLOBALS['config'][$configEntry];
84 }
85
86 // Setter for $GLOBALS['config'] entries
87 function setConfigEntry ($configEntry, $value) {
88         // Secure the entry name
89         if (function_exists('SQL_ESCAPE')) {
90                 // Use our secure function
91                 $configEntry = SQL_ESCAPE($configEntry);
92         } else {
93                 // Use maybe insecure function
94                 $configEntry = smartAddSlashes($configEntry);
95         }
96
97         // And set it
98         $GLOBALS['config'][$configEntry] = $value;
99 }
100
101 // Checks wether the given config entry is set
102 function isConfigEntrySet ($configEntry) {
103         return (isset($GLOBALS['config'][$configEntry]));
104 }
105
106 // Merges $GLOBALS['config'] with data in given array
107 function mergeConfig ($newConfig) {
108         $GLOBALS['config'] = merge_array(getConfigArray(), $newConfig);
109 }
110
111 // Increment or init with given value or 1 as default the given config entry
112 function incrementConfigEntry ($configEntry, $value=1) {
113         // Increment it if set or init it with 1
114         if (getConfig($configEntry) > 0) {
115                 $GLOBALS['config'][$configEntry] += $value;
116         } else {
117                 $GLOBALS['config'][$configEntry] = $value;
118         }
119 }
120
121 // Checks wether the configuration array is set so the config is loaded
122 function isConfigLoaded () {
123         // Check all
124         return ((isset($GLOBALS['config'])) && (is_array($GLOBALS['config'])) && (count($GLOBALS['config']) > 0));
125 }
126
127 // Load configuration and return it as an arry
128 function loadConfiguration ($no = '0') {
129         // Check for cache extension, cache-array and if the requested configuration is in cache
130         if ((isset($GLOBALS['cache_array'])) && (is_array($GLOBALS['cache_array'])) && (isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
131                 // Load config from cache
132                 //* DEBUG: */ OUTPUT_HTML(gettype($GLOBALS['cache_array']['config'][$no])."<br />");
133                 foreach ($GLOBALS['cache_array']['config'][$no] as $key => $value) {
134                         setConfigEntry($key, $value);
135                 } // END - foreach
136
137                 // Count cache hits if exists
138                 if ((isConfigEntrySet('cache_hits')) && (EXT_IS_ACTIVE('cache'))) {
139                         incrementConfigEntry('cache_hits');
140                 } // END - if
141         } elseif ((!EXT_IS_ACTIVE('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
142                 // Load config from DB
143                 $result_config = SQL_QUERY_ESC("SELECT * FROM `{!_MYSQL_PREFIX!}_config` WHERE config=%d LIMIT 1",
144                         array(bigintval($no)), __FUNCTION__, __LINE__);
145
146                 // Is the config there?
147                 if (SQL_NUMROWS($result_config) == 1) {
148                         // Get config from database
149                         mergeConfig(SQL_FETCHARRAY($result_config));
150                 } // END - if
151
152                 // Free result
153                 SQL_FREERESULT($result_config);
154
155                 // Remember this config in the array
156                 $GLOBALS['cache_array']['config'][$no] = getConfigArray();
157         }
158 }
159
160 // Getter for whole $GLOBALS['config'] array
161 function getConfigArray () {
162         // Default is null
163         $return = null;
164
165         // Is the config set?
166         if (isConfigLoaded()) {
167                 // Then use it
168                 $return = $GLOBALS['config'];
169         } // END - if
170
171         // Return result
172         return $return;
173 }
174
175 // Updates an old inc/config.php to a inc/cache/config-local.php file
176 function updateOldConfigFile () {
177         // Watch out for these lines and execute them as single command
178         // @TODO Make this all better... :-/
179         $watchLines = array(
180                 "define('SITE_KEY',"           => 'SITE_KEY',
181                 "define('DEFAULT_LANG',"       => 'DEFAULT_LANG',
182                 "define('warn_no_pass',"       => 'WARN_NO_PASS',
183                 "define('WRITE_FOOTER',"       => 'WRITE_FOOTER',
184                 "define('OUTPUT_MODE',"        => 'OUTPUT_MODE',
185                 "define('MAIN_TITLE',"         => 'MAIN_TITLE',
186                 "define('SLOGAN',"             => 'SLOGAN',
187                 "define('WEBMASTER',"          => 'WEBMASTER',
188                 "define('mxchange_installed'," => 'MXCHANGE_INSTALLED',
189                 "define('admin_registered',"   => 'ADMIN_REGISTERED',
190                 "define('_MYSQL_PREFIX',"      => '_MYSQL_PREFIX',
191                 "define('_TABLE_TYPE',"        => '_TABLE_TYPE',
192                 "define('_DB_TYPE',"           => '_DB_TYPE',
193                 "define('SMTP_HOSTNAME',"      => 'SMTP_HOSTNAME',
194                 "define('SMTP_USER'"           => 'SMTP_USER',
195                 "define('SMTP_PASSWORD',"      => 'SMTP_PASSWORD',
196                 "define('ENABLE_BACKLINK',"    => 'ENABLE_BACKLINK',
197         );
198
199         // Make these lower-case! (damn stupid code...)
200         $lowerCase = array('WARN_NO_PASS', 'MXCHANGE_INSTALLED', 'ADMIN_REGISTERED');
201
202         // These are still constants...
203         // @TODO Rewrite these all to config entries, if somehow possible
204         $constants = array('MAIN_TITLE', 'SLOGAN', 'WEBMASTER');
205
206         // Special comments...
207         $comments = array(
208                 'WARN_NO_PASS'       => 'NULLPASS-WARNING',
209                 'MXCHANGE_INSTALLED' => 'INSTALLED',
210                 'ADMIN_REGISTERED'   => 'ADMIN-SETUP',
211                 '_MYSQL_PREFIX'      => 'MYSQL-PREFIX',
212                 '_TABLE_TYPE'        => 'TABLE-TYPE',
213                 '_DB_TYPE'           => 'DATABASE-TYPE',
214                 'ENABLE_BACKLINK'    => 'BACKLINK',
215                 'host'               => 'MYSQL-HOST',
216                 'dbase'              => 'MYSQL-DBASE',
217                 'login'              => 'MYSQL-LOGIN',
218                 'password'           => 'MYSQL-PASSWORD'
219         );
220
221         // Copy template to new file destionation
222         copyFileVerified(constant('PATH') . 'inc/config-local.php.dist', constant('PATH') . 'inc/cache/config-local.php', 0644);
223
224         // First of all, load the old one!
225         $oldConfig = explode("\n", readFromFile(constant('PATH') . 'inc/config.php'));
226
227         // Now, analyze every entry
228         $done = array();
229         foreach ($oldConfig as $line) {
230                 // Check all watch lines
231                 foreach ($watchLines as $old => $new) {
232                         // Is the line found?
233                         if ((substr($line, 0, strlen($old)) == $old) && (!isset($done[$old]))) {
234                                 // Entry found!
235                                 //* DEBUG: */ OUTPUT_HTML(htmlentities($line) . " - FOUND!<br />");
236
237                                 // Eval the line...
238                                 eval($line);
239
240                                 // Setting config entry is new default behaviour!
241                                 $function = 'setConfigEntry';
242
243                                 // Still some old constants left?
244                                 if (in_array($new, $constants)) {
245                                         // Then switch over...
246                                         $function = 'define';
247                                 } // END - if
248
249                                 // Default comment
250                                 $comment = str_replace('_', '-', $new);
251
252                                 // Do we have a special comment?
253                                 if (isset($comments[$new])) {
254                                         // Then use it
255                                         $comment = $comments[$new];
256                                 } // END - if
257
258                                 // Do we need to make $new lowercase?
259                                 $oldNew = $new;
260                                 if (in_array($new, $lowerCase)) {
261                                         // Then do so... :)
262                                         $new = strtolower($new);
263                                 } // END - if
264
265                                 /// ... and write it to the new config
266                                 //* DEBUG: */ OUTPUT_HTML('function=' . $function . ',new=' . $new . ',comment=' . $comment . "<br />");
267                                 changeDataInFile(constant('PATH') . 'inc/cache/config-local.php', $comment, $function . "('" . $oldNew . "', \"", "\");", constant($new), 0);
268                                 //* DEBUG: */ OUTPUT_HTML("CHANGED!<br />");
269
270                                 // Mark it as done
271                                 $done[$old] = 1;
272                         } // END - if
273                 } // END - foreach
274         } // END - foreach
275
276         // By default the old array $MySQL was not found
277         $found = false;
278
279         // Analyze every entry again for the MySQL configuration
280         foreach ($oldConfig as $line) {
281                 // Trim spaces
282                 $line = trim($line);
283
284                 // Is the $MySQL found?
285                 if (substr($line, 0, 6) == "\$MySQL") {
286                         // Okay found!
287                         $found = true;
288                 } elseif ($found === true) {
289                         // Now check this row
290                         if (substr($line, 0, 2) == ');') {
291                                 // MySQL array is closed so stop looking for it
292                                 break;
293                         } elseif (substr($line, 0, 2) == '//') {
294                                 // Skip this line
295                                 continue;
296                         }
297
298                         // Debug output only
299                         //* DEBUG: */ OUTPUT_HTML(htmlentities($line) . " - MySQL!<br />");
300
301                         // Split parts so we can check them and prepare them
302                         $parts = explode('=>', $line);
303                         $key = substr(trim($parts[0]), 1, -1); $value = substr(trim($parts[1]), 1, -2);
304
305                         // We can now save the right part in new config file
306                         changeDataInFile(constant('PATH') . 'inc/cache/config-local.php', $comments[$key], "    '".$key."'     => \"", "\",", $value, 0);
307                 }
308         } // END - foreach
309
310         // Finally remove old config file
311         removeFile(constant('PATH') . 'inc/config.php');
312
313         // Redirect to same URL to reload our new config
314         redirectToUrl($_SERVER['REQUEST_URI']);
315 }
316
317 // [EOF]
318 ?>