More debug output added
[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 // Merges $_CONFIG with data in given array
46 function mergeConfig ($newConfig) {
47         global $_CONFIG;
48         $_CONFIG = merge_array(getConfigArray(), $newConfig);
49 }
50
51 // Getter for $_CONFIG entries
52 function getConfig ($configEntry) {
53         global $_CONFIG;
54
55         // Default value
56         $value = null;
57
58         // Is the entry there?
59         if (isConfigEntrySet($configEntry)) {
60                 // Then use it
61                 $value = $_CONFIG[$configEntry];
62         } // END - if
63
64         // Return it
65         return $value;
66 }
67
68 // Setter for $_CONFIG entries
69 function setConfigEntry ($configEntry, $value) {
70         global $_CONFIG;
71
72         // Secure the entry name
73         if (function_exists('SQL_ESCAPE')) {
74                 // Use our secure function
75                 $configEntry = SQL_ESCAPE($configEntry);
76         } else {
77                 // Use maybe insecure function
78                 $configEntry = smartAddSlashes($configEntry);
79         }
80
81         // If config array isn't set, set it
82         if (!isConfigLoaded()) {
83                 // Init configuration array
84                 initConfig();
85         } // END - if
86
87         // And set it
88         $_CONFIG[$configEntry] = $value;
89 }
90
91 // Checks wether the given config entry is set
92 function isConfigEntrySet ($configEntry) {
93         global $_CONFIG;
94         return (isset($_CONFIG[$configEntry]));
95 }
96
97 // Increment or init with given value or 1 as default the given config entry
98 function incrementConfigEntry ($configEntry, $value=1) {
99         global $_CONFIG;
100
101         // Increment it if set or init it with 1
102         if (getConfig($configEntry) > 0) {
103                 $_CONFIG[$configEntry] += $value;
104         } else {
105                 $_CONFIG[$configEntry] = $value;
106         }
107 }
108
109 // Checks wether the configuration array is set so the config is loaded
110 function isConfigLoaded () {
111         global $_CONFIG;
112
113         // Check all
114         return ((isset($_CONFIG)) && (is_array($_CONFIG)) && (count($_CONFIG) > 0));
115 }
116
117 // Init the config array
118 function initConfig () {
119         global $_CONFIG;
120
121         // Set a minimum dummy configuration
122         $_CONFIG = array(
123                 'code_length' => 0,
124                 'patch_level' => 0,
125                 'last_update' => time()
126         );
127 }
128
129 // Load configuration and return it as an arry
130 function loadConfiguration ($no = '0') {
131         // Check for cache extension, cache-array and if the requested configuration is in cache
132         if ((isset($GLOBALS['cache_array'])) && (is_array($GLOBALS['cache_array'])) && (isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
133                 // Load config from cache
134                 //* DEBUG: */ echo gettype($GLOBALS['cache_array']['config'][$no])."<br />\n";
135                 foreach ($GLOBALS['cache_array']['config'][$no] as $key => $value) {
136                         setConfigEntry($key, $value);
137                 } // END - foreach
138
139                 // Count cache hits if exists
140                 if ((isConfigEntrySet('cache_hits')) && (EXT_IS_ACTIVE('cache'))) {
141                         incrementConfigEntry('cache_hits');
142                 } // END - if
143         } elseif ((!EXT_IS_ACTIVE('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
144                 // Load config from DB
145                 $result_config = SQL_QUERY_ESC("SELECT * FROM `{!_MYSQL_PREFIX!}_config` WHERE config=%d LIMIT 1",
146                         array(bigintval($no)), __FUNCTION__, __LINE__);
147
148                 // Is the config there?
149                 if (SQL_NUMROWS($result_config) == 1) {
150                         // Get config from database
151                         mergeConfig(SQL_FETCHARRAY($result_config));
152                 } // END - if
153
154                 // Free result
155                 SQL_FREERESULT($result_config);
156
157                 // Remember this config in the array
158                 $GLOBALS['cache_array']['config'][$no] = getConfigArray();
159         }
160 }
161
162 // Getter for whole $_CONFIG array
163 function getConfigArray () {
164         global $_CONFIG;
165
166         // Default is null
167         $return = null;
168
169         // Is the config set?
170         if (isConfigLoaded()) {
171                 // Then use it
172                 $return = $_CONFIG;
173         } // END - if
174
175         // Return result
176         return $return;
177 }
178
179 // Updates an old inc/config.php to a inc/cache/config-local.php file
180 function updateOldConfigFile () {
181         // Watch out for these lines and execute them as single command
182         // @TODO Make this all better... :-/
183         $watchLines = array(
184                 "define('SITE_KEY',"           => 'SITE_KEY',
185                 "define('DEFAULT_LANG',"       => 'DEFAULT_LANG',
186                 "define('warn_no_pass',"       => 'WARN_NO_PASS',
187                 "define('WRITE_FOOTER',"       => 'WRITE_FOOTER',
188                 "define('OUTPUT_MODE',"        => 'OUTPUT_MODE',
189                 "define('MAIN_TITLE',"         => 'MAIN_TITLE',
190                 "define('SLOGAN',"             => 'SLOGAN',
191                 "define('WEBMASTER',"          => 'WEBMASTER',
192                 "define('mxchange_installed'," => 'MXCHANGE_INSTALLED',
193                 "define('admin_registered',"   => 'ADMIN_REGISTERED',
194                 "define('_MYSQL_PREFIX',"      => '_MYSQL_PREFIX',
195                 "define('_TABLE_TYPE',"        => '_TABLE_TYPE',
196                 "define('_DB_TYPE',"           => '_DB_TYPE',
197                 "define('SMTP_HOSTNAME',"      => 'SMTP_HOSTNAME',
198                 "define('SMTP_USER'"           => 'SMTP_USER',
199                 "define('SMTP_PASSWORD',"      => 'SMTP_PASSWORD',
200                 "define('ENABLE_BACKLINK',"    => 'ENABLE_BACKLINK',
201         );
202
203         // Make these lower-case! (damn stupid code...)
204         $lowerCase = array('WARN_NO_PASS', 'MXCHANGE_INSTALLED', 'ADMIN_REGISTERED');
205
206         // These are still constants...
207         // @TODO Rewrite these all to config entries, if somehow possible
208         $constants = array('MAIN_TITLE', 'SLOGAN', 'WEBMASTER');
209
210         // Special comments...
211         $comments = array(
212                 'WARN_NO_PASS'       => 'NULLPASS-WARNING',
213                 'MXCHANGE_INSTALLED' => 'INSTALLED',
214                 'ADMIN_REGISTERED'   => 'ADMIN-SETUP',
215                 '_MYSQL_PREFIX'      => 'MYSQL-PREFIX',
216                 '_TABLE_TYPE'        => 'TABLE-TYPE',
217                 '_DB_TYPE'           => 'DATABASE-TYPE',
218                 'ENABLE_BACKLINK'    => 'BACKLINK',
219                 'host'               => 'MYSQL-HOST',
220                 'dbase'              => 'MYSQL-DBASE',
221                 'login'              => 'MYSQL-LOGIN',
222                 'password'           => 'MYSQL-PASSWORD'
223         );
224
225         // Copy template to new file destionation
226         copyFileVerified(constant('PATH') . 'inc/config-local.php.dist', constant('PATH') . 'inc/cache/config-local.php', 0644);
227
228         // First of all, load the old one!
229         $oldConfig = explode("\n", readFromFile(constant('PATH') . 'inc/config.php'));
230
231         // Now, analyze every entry
232         $done = array();
233         foreach ($oldConfig as $line) {
234                 // Check all watch lines
235                 foreach ($watchLines as $old => $new) {
236                         // Is the line found?
237                         if ((substr($line, 0, strlen($old)) == $old) && (!isset($done[$old]))) {
238                                 // Entry found!
239                                 //* DEBUG: */ print htmlentities($line) . " - FOUND!<br />\n";
240
241                                 // Eval the line...
242                                 eval($line);
243
244                                 // Setting config entry is new default behaviour!
245                                 $function = 'setConfigEntry';
246
247                                 // Still some old constants left?
248                                 if (in_array($new, $constants)) {
249                                         // Then switch over...
250                                         $function = 'define';
251                                 } // END - if
252
253                                 // Default comment
254                                 $comment = str_replace('_', '-', $new);
255
256                                 // Do we have a special comment?
257                                 if (isset($comments[$new])) {
258                                         // Then use it
259                                         $comment = $comments[$new];
260                                 } // END - if
261
262                                 // Do we need to make $new lowercase?
263                                 $oldNew = $new;
264                                 if (in_array($new, $lowerCase)) {
265                                         // Then do so... :)
266                                         $new = strtolower($new);
267                                 } // END - if
268
269                                 /// ... and write it to the new config
270                                 //* DEBUG: */ print 'function=' . $function . ',new=' . $new . ',comment=' . $comment . "<br />\n";
271                                 changeDataInFile(constant('PATH') . 'inc/cache/config-local.php', $comment, $function . "('" . $oldNew . "', \"", "\");", constant($new), 0);
272                                 //* DEBUG: */ print "CHANGED!<br />\n";
273
274                                 // Mark it as done
275                                 $done[$old] = 1;
276                         } // END - if
277                 } // END - foreach
278         } // END - foreach
279
280         // By default the old array $MySQL was not found
281         $found = false;
282
283         // Analyze every entry again for the MySQL configuration
284         foreach ($oldConfig as $line) {
285                 // Trim spaces
286                 $line = trim($line);
287
288                 // Is the $MySQL found?
289                 if (substr($line, 0, 6) == "\$MySQL") {
290                         // Okay found!
291                         $found = true;
292                 } elseif ($found === true) {
293                         // Now check this row
294                         if (substr($line, 0, 2) == ');') {
295                                 // MySQL array is closed so stop looking for it
296                                 break;
297                         } elseif (substr($line, 0, 2) == '//') {
298                                 // Skip this line
299                                 continue;
300                         }
301
302                         // Debug output only
303                         //* DEBUG: */ print htmlentities($line) . " - MySQL!<br />\n";
304
305                         // Split parts so we can check them and prepare them
306                         $parts = explode('=>', $line);
307                         $key = substr(trim($parts[0]), 1, -1); $value = substr(trim($parts[1]), 1, -2);
308
309                         // We can now save the right part in new config file
310                         changeDataInFile(constant('PATH') . 'inc/cache/config-local.php', $comments[$key], "    '".$key."'     => \"", "\",", $value, 0);
311                 }
312         } // END - foreach
313
314         // Finally remove old config file
315         removeFile(constant('PATH') . 'inc/config.php');
316
317         // Redirect to same URL to reload our new config
318         redirectToUrl($_SERVER['REQUEST_URI']);
319 }
320
321 // [EOF]
322 ?>