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