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