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