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