2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 02/28/2009 *
4 * =================== Last change: 02/28/2009 *
6 * -------------------------------------------------------------------- *
7 * File : config-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Configuration functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Konfigurationsfunktionen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2011 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Init the config array
44 function initConfig () {
45 // Init not if already found
46 if (isConfigurationLoaded()) {
47 // Already initialized
48 debug_report_bug(__FUNCTION__, __LINE__, 'Configuration is already initialized.');
51 // Set a minimum of configuration, required to by-pass some error triggers in getConfig()
52 $GLOBALS['config'] = array(
56 // 'DEFAULT_SALT_LENGTH' => 40,
57 // 'DEBUG_MODE' => 'N',
58 // 'DEBUG_RESET' => 'N',
59 // 'DEBUG_MONTHLY' => 'N',
60 // 'DEBUG_WEEKLY' => 'N',
61 // 'DEBUG_REGEX' => 'N',
62 // 'ADMIN_REGISTERED' => 'N',
63 // 'verbose_sql' => 'Y',
64 // For installation phase:
65 'SMTP_HOSTNAME' => '',
67 'SMTP_PASSWORD' => '',
68 'MT_WORD' => '{--DEFAULT_MT_WORD--}',
72 // Getter for $GLOBALS['config'] entries
73 function getConfig ($configEntry) {
74 // Is the entry there?
75 if (!isConfigEntrySet($configEntry)) {
76 // Raise an error of missing entries
77 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Configuration entry <span class=\"data\">%s</span> is missing.", $configEntry));
81 //* DEBUG: */ error_log(__FUNCTION__.'['.__LINE__.':] '.$configEntry.'='.$GLOBALS['config'][$configEntry]);
82 return $GLOBALS['config'][$configEntry];
85 // Setter for $GLOBALS['config'] entries
86 function setConfigEntry ($configEntry, $value) {
87 // Just set it (unsecured won't hurt?)
88 $GLOBALS['config'][$configEntry] = $value;
91 // Checks wether the given config entry is set
92 function isConfigEntrySet ($configEntry) {
93 //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . $configEntry . '=' . intval(isset($GLOBALS['config'][$configEntry])));
94 return (isset($GLOBALS['config'][$configEntry]));
97 // Merges $GLOBALS['config'] with data in given array
98 function mergeConfig ($newConfig) {
99 $GLOBALS['config'] = merge_array(getConfigArray(), $newConfig);
101 // Remove all cached entries
104 // Increment or init with given value or 1 as default the given config entry
105 function incrementConfigEntry ($configEntry, $value=1) {
106 // Increment it if set or init it with $value
107 if (isConfigEntrySet($configEntry)) {
108 $GLOBALS['config'][$configEntry] += $value;
110 $GLOBALS['config'][$configEntry] = $value;
114 // Checks wether the configuration array is set so the config is loaded
115 function isConfigurationLoaded () {
117 return (isset($GLOBALS['config']['config']));
120 // Getter for whole $GLOBALS['config'] array
121 function getConfigArray () {
125 // Is the config set?
126 if (isset($GLOBALS['config'])) {
128 $return = $GLOBALS['config'];
135 // Updates an old inc/config.php to a inc/cache/config-local.php file
136 function updateOldConfigFile () {
137 // Watch out for these lines and execute them as single command
138 // @TODO Make this all better... :-/
140 'SITE_KEY' => 'SITE_KEY',
141 'DEFAULT_LANG' => 'DEFAULT_LANG',
142 'warn_no_pass' => 'WARN_NO_PASS',
143 'WRITE_FOOTER' => 'WRITE_FOOTER',
144 'OUTPUT_MODE' => 'OUTPUT_MODE',
145 'MAIN_TITLE' => 'MAIN_TITLE',
146 'SLOGAN' => 'SLOGAN',
147 'WEBMASTER' => 'WEBMASTER',
148 'mxchange_installed' => 'MXCHANGE_INSTALLED',
149 'admin_registered' => 'ADMIN_REGISTERED',
150 '_MYSQL_PREFIX' => '_MYSQL_PREFIX',
151 '_TABLE_TYPE' => '_TABLE_TYPE',
152 '_DB_TYPE' => '_DB_TYPE',
153 'SMTP_HOSTNAME' => 'SMTP_HOSTNAME',
154 'SMTP_USER' => 'SMTP_USER',
155 'SMTP_PASSWORD' => 'SMTP_PASSWORD',
156 'ENABLE_BACKLINK' => 'ENABLE_BACKLINK',
157 'MAIN_TITLE' => 'MAIN_TITLE',
158 'SLOGAN' => 'SLOGAN',
159 'WEBMASTER' => 'WEBMASTER',
164 // Make these lower-case! (damn stupid code...)
165 $lowerCase = array('WARN_NO_PASS', 'MXCHANGE_INSTALLED', 'ADMIN_REGISTERED');
167 // Special comments...
169 'WARN_NO_PASS' => 'NULLPASS-WARNING',
170 'MXCHANGE_INSTALLED' => 'INSTALLED',
171 'ADMIN_REGISTERED' => 'ADMIN-SETUP',
172 '_MYSQL_PREFIX' => 'MYSQL-PREFIX',
173 '_TABLE_TYPE' => 'TABLE-TYPE',
174 '_DB_TYPE' => 'DATABASE-TYPE',
175 'ENABLE_BACKLINK' => 'BACKLINK',
176 'host' => 'MYSQL-HOST',
177 'dbase' => 'MYSQL-DBASE',
178 'login' => 'MYSQL-LOGIN',
179 'password' => 'MYSQL-PASSWORD'
182 // Copy template to new file destionation
183 copyFileVerified(getPath() . 'inc/config-local.php.dist', getCachePath() . 'config-local.php', 0644);
185 // First of all, load the old one!
186 $oldConfig = explode("\n", readFromFile(getPath() . 'inc/config.php'));
188 // Now, analyze every entry
190 foreach ($oldConfig as $line) {
191 // Check all watch lines
192 foreach ($watchLines as $old => $new) {
193 // Add define() command around old one
194 $old = "define('" . $old . "',";
196 // Is the line found?
197 if ((substr($line, 0, strlen($old)) == $old) && (!isset($done[$old]))) {
199 //* DEBUG: */ debugOutput(secureString($line) . ' - FOUND!');
204 // Setting config entry is new default behaviour!
205 $function = 'setConfigEntry';
208 $comment = str_replace('_', '-', $new);
210 // Do we have a special comment?
211 if (isset($comments[$new])) {
213 $comment = $comments[$new];
216 // Do we need to make $new lowercase?
218 if (in_array($new, $lowerCase)) {
220 $new = strtolower($new);
223 // ... and write it to the new config
224 //* DEBUG: */ debugOutput('function=' . $function . ',new=' . $new . ',comment=' . $comment);
225 changeDataInInclude(getCachePath() . 'config-local.php', $comment, $function . "('" . $oldNew . "', \"", '");', constant($new), 0);
226 //* DEBUG: */ debugOutput('CHANGED!');
234 // By default the old array $MySQL was not found
237 // Analyze every entry again for the MySQL configuration
238 foreach ($oldConfig as $line) {
242 // Is the $MySQL found?
243 if (substr($line, 0, 6) == '$MySQL') {
246 } elseif ($found === true) {
247 // Now check this row
248 if (substr($line, 0, 2) == ');') {
249 // MySQL array is closed so stop looking for it
251 } elseif (substr($line, 0, 2) == '//') {
257 //* DEBUG: */ debugOutput(secureString($line) . ' - MySQL!');
259 // Split parts so we can check them and prepare them
260 $parts = explode('=>', $line);
261 $key = substr(trim($parts[0]), 1, -1); $value = substr(trim($parts[1]), 1, -2);
263 // We can now save the right part in new config file
264 changeDataInInclude(getCachePath() . 'config-local.php', $comments[$key], " '".$key."' => \"", '",', $value, 0);
268 // Finally remove old config file
269 removeFile(getPath() . 'inc/config.php');
271 // Redirect to same URL to reload our new config
272 redirectToUrl(getRequestUri());
275 // Update config entries
276 function updateConfiguration ($entries, $values, $updateMode = '', $config = '0') {
277 // Do not update config in CSS mode
278 if ((isCssOutputMode()) || (isRawOutputMode()) || (isInstallationPhase())) {
279 // This logger line may be very noisy
280 /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not updating configuration. entries[]=' . gettype($entries) . ',values[]=' . gettype($values) . ',updateMode=' . $updateMode . ',config=' . $config . ',isCssOutputMode()=' . intval(isCssOutputMode()) . ',isRawOutputMode()=' . intval(isRawOutputMode()) . ',isInstallationPhase()=' . intval(isInstallationPhase()));
284 // Do we have multiple entries?
285 if (is_array($entries)) {
288 foreach ($entries as $idx => $entry) {
290 if (!empty($updateMode)) {
292 $all .= sprintf("`%s`=`%s`%s%s,", $entry, $entry, $updateMode, (float) $values[$idx]);
294 // Check if string or number but no array
295 if (is_array($values[$idx])) {
296 // Arrays must be fixed...
297 debug_report_bug(__FUNCTION__, __LINE__, 'values[' . $idx . '] should not be an array! Content=<pre>'.print_r($values[$idx], true).'</pre>');
298 } elseif (($values[$idx] + 0) === $values[$idx]) {
300 $all .= sprintf("`%s`=%s,", $entry, (float) $values[$idx]);
302 // Set it in config as well
303 setConfigEntry($entry, $values[$idx]);
304 } elseif ($values[$idx] == 'UNIX_TIMESTAMP()') {
305 // Function UNIX_TIMESTAMP() detected
306 $all .= sprintf("`%s`=UNIX_TIMESTAMP(),", $entry);
308 // Set timestamp in array as well
309 setConfigEntry($entry, time());
312 $all .= sprintf("`%s`='%s',", $entry, SQL_ESCAPE($values[$idx]));
314 // Set it in config as well
315 setConfigEntry($entry, $values[$idx]);
321 $entries = substr($all, 0, -1);
322 } elseif (!empty($updateMode)) {
324 $entries = sprintf("`%s`=`%s`%s%s", $entries, $entries, $updateMode, (float) $values);
325 } elseif (($values + 0) === $values) {
327 $entries = sprintf("`%s`=%s", $entries, (float) $values);
329 // Set it in config first
330 setConfigEntry($entries, (float) $values);
331 } elseif ($values == 'UNIX_TIMESTAMP()') {
332 // Function UNIX_TIMESTAMP() detected
333 $entries = sprintf("`%s`=UNIX_TIMESTAMP()", $entries);
335 // Set timestamp in array as well
336 setConfigEntry($entries, time());
338 // Regular entry to update
339 $entries = sprintf("`%s`='%s'", $entries, SQL_ESCAPE($values));
341 // Set it in config as well
342 setConfigEntry($entries, SQL_ESCAPE($values));
345 // Run database update
346 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'entries=' . $entries);
347 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_config` SET ".$entries." WHERE `config`=%s LIMIT 1",
348 array(bigintval($config)), __FUNCTION__, __LINE__);
349 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'entries=' . $entries . ',affectedRows=' . SQL_AFFECTEDROWS());
352 rebuildCache('config', 'config');
355 // Filter for loading configuration
356 function FILTER_LOAD_CONFIGURATION ($no = '0') {
357 // Is the value null, it comes from the 'init' filter chain
362 // Check for cache extension, cache-array and if the requested configuration is in cache
363 if ((isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
364 // Load config from cache
365 mergeConfig($GLOBALS['cache_array']['config'][$no]);
367 // Count cache hits if exists
368 if ((isStatsEntrySet('cache_hits')) && (isExtensionActive('cache'))) {
369 incrementStatsEntry('cache_hits');
371 } elseif ((!isExtensionActive('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
372 // Load config from DB
373 $result_config = SQL_QUERY_ESC("SELECT * FROM `{?_MYSQL_PREFIX?}_config` WHERE `config`='%s' LIMIT 1",
374 array(bigintval($no)), __FUNCTION__, __LINE__);
376 // Is the config there?
377 if (SQL_NUMROWS($result_config) == 1) {
378 // Get config from database
379 mergeConfig(SQL_FETCHARRAY($result_config));
383 SQL_FREERESULT($result_config);
385 // Remember this config in the array
386 $GLOBALS['cache_array']['config'][$no] = getConfigArray();