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