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 - 2013 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 reportBug(__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_HOURLY' => 'N',
59 // 'DEBUG_DAILY' => 'N',
60 // 'DEBUG_MONTHLY' => 'N',
61 // 'DEBUG_YEARLY' => 'N',
62 // 'DEBUG_WEEKLY' => 'N',
63 // 'DEBUG_REGEX' => 'N',
64 // 'ADMIN_REGISTERED' => 'N',
65 // 'verbose_sql' => 'Y',
66 // For installation phase:
67 'SMTP_HOSTNAME' => '',
69 'SMTP_PASSWORD' => '',
70 'MT_WORD' => '{--DEFAULT_MT_WORD--}',
74 // Getter for $GLOBALS['config'] entries
75 function getConfig ($configEntry) {
76 // Is the entry there?
77 if (!isConfigEntrySet($configEntry)) {
78 // Raise an error of missing entries
79 reportBug(__FUNCTION__, __LINE__, sprintf('Configuration entry <span class="data">%s</span> is missing.', $configEntry));
83 //* DEBUG: */ error_log(__FUNCTION__.'['.__LINE__.':] '.$configEntry.'='.$GLOBALS['config'][$configEntry]);
84 return $GLOBALS['config'][$configEntry];
87 // Setter for $GLOBALS['config'] entries
88 function setConfigEntry ($configEntry, $value) {
89 // Just set it (unsecured won't hurt?)
90 $GLOBALS['config'][$configEntry] = $value;
93 removeGlobalCache($configEntry);
96 // Removes entry in $GLOBALS
97 function removeGlobalCache ($configEntry) {
99 $key = 'get' . capitalizeUnderscoreString($configEntry);
102 unset($GLOBALS[$key]);
105 // Checks whether the given config entry is set
106 function isConfigEntrySet ($configEntry) {
107 //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . $configEntry . '=' . intval(isset($GLOBALS['config'][$configEntry])));
108 return ((isset($GLOBALS['config'][$configEntry])) || (array_key_exists($configEntry, $GLOBALS['config'])));
111 // Merges $GLOBALS['config'] with data in given array
112 function mergeConfig ($newConfig) {
113 // Merge current configuration with new one
114 $GLOBALS['config'] = merge_array(getConfigArray(), $newConfig);
117 // Increment or init with given value or 1 as default the given config entry
118 function incrementConfigEntry ($configEntry, $value=1) {
119 // Increment it if set or init it with $value
120 if (isConfigEntrySet($configEntry)) {
121 $GLOBALS['config'][$configEntry] += $value;
123 $GLOBALS['config'][$configEntry] = $value;
127 // Checks whether the configuration array is set so the config is loaded
128 function isConfigurationLoaded () {
130 return (isset($GLOBALS['config']['config']));
133 // Getter for whole $GLOBALS['config'] array
134 function getConfigArray () {
138 // Is the config set?
139 if (isset($GLOBALS['config'])) {
141 $return = $GLOBALS['config'];
148 // Updates an old inc/config.php to a inc/cache/config-local.php file
149 function updateOldConfigFile () {
150 // Watch out for these lines and execute them as single command
151 // @TODO Make this all better... :-/
153 'SITE_KEY' => 'SITE_KEY',
154 'DEFAULT_LANG' => 'DEFAULT_LANG',
155 'warn_no_pass' => 'WARN_NO_PASS',
156 'WRITE_FOOTER' => 'WRITE_FOOTER',
157 'OUTPUT_MODE' => 'OUTPUT_MODE',
158 'MAIN_TITLE' => 'MAIN_TITLE',
159 'SLOGAN' => 'SLOGAN',
160 'WEBMASTER' => 'WEBMASTER',
161 'mailer_installed' => 'MAILER_INSTALLED',
162 'admin_registered' => 'ADMIN_REGISTERED',
163 '_MYSQL_PREFIX' => '_MYSQL_PREFIX',
164 '_TABLE_TYPE' => '_TABLE_TYPE',
165 '_DB_TYPE' => '_DB_TYPE',
166 'SMTP_HOSTNAME' => 'SMTP_HOSTNAME',
167 'SMTP_USER' => 'SMTP_USER',
168 'SMTP_PASSWORD' => 'SMTP_PASSWORD',
169 'ENABLE_BACKLINK' => 'ENABLE_BACKLINK',
170 'MAIN_TITLE' => 'MAIN_TITLE',
171 'SLOGAN' => 'SLOGAN',
172 'WEBMASTER' => 'WEBMASTER',
177 // Make these lower-case! (damn stupid code...)
178 $lowerCase = array('WARN_NO_PASS', 'MAILER_INSTALLED', 'ADMIN_REGISTERED');
180 // Special comments...
182 'WARN_NO_PASS' => 'NULLPASS-WARNING',
183 'MAILER_INSTALLED ' => 'INSTALLED',
184 'ADMIN_REGISTERED' => 'ADMIN-SETUP',
185 '_MYSQL_PREFIX' => 'MYSQL-PREFIX',
186 '_TABLE_TYPE' => 'TABLE-TYPE',
187 '_DB_TYPE' => 'DATABASE-TYPE',
188 'ENABLE_BACKLINK' => 'BACKLINK',
189 'host' => 'MYSQL-HOST',
190 'dbase' => 'MYSQL-DBASE',
191 'login' => 'MYSQL-LOGIN',
192 'password' => 'MYSQL-PASSWORD'
195 // Copy template to new file destionation
196 copyFileVerified(getPath() . 'inc/config-local.php.dist', getCachePath() . 'config-local.php', 0644);
198 // First of all, load the old one!
199 $oldConfig = explode(PHP_EOL, readFromFile(getPath() . 'inc/config.php'));
201 // Now, analyze every entry
203 foreach ($oldConfig as $line) {
204 // Check all watch lines
205 foreach ($watchLines as $old => $new) {
206 // Add define() command around old one
207 $old = "define('" . $old . "',";
209 // Is the line found?
210 if ((substr($line, 0, strlen($old)) == $old) && (!isset($done[$old]))) {
212 //* DEBUG: */ debugOutput(secureString($line) . ' - FOUND!');
217 // Setting config entry is new default behaviour!
218 $function = 'setConfigEntry';
221 $comment = str_replace('_', '-', $new);
223 // Is there a special comment?
224 if (isset($comments[$new])) {
226 $comment = $comments[$new];
229 // Does $new needs to be lower-case?
231 if (in_array($new, $lowerCase)) {
233 $new = strtolower($new);
236 // ... and write it to the new config
237 //* DEBUG: */ debugOutput('function=' . $function . ',new=' . $new . ',comment=' . $comment);
238 changeDataInInclude(getCachePath() . 'config-local.php', $comment, $function . "('" . $oldNew . "', \"", '");', constant($new), 0);
239 //* DEBUG: */ debugOutput('CHANGED!');
247 // By default the old array $MySQL was not found
250 // Analyze every entry again for the MySQL configuration
251 foreach ($oldConfig as $line) {
255 // Is the $MySQL found?
256 if (substr($line, 0, 6) == '$MySQL') {
259 } elseif ($found === TRUE) {
260 // Now check this row
261 if (substr($line, 0, 2) == ');') {
262 // MySQL array is closed so stop looking for it
265 } elseif (substr($line, 0, 2) == '//') {
271 //* DEBUG: */ debugOutput(secureString($line) . ' - MySQL!');
273 // Split parts so we can check them and prepare them
274 $parts = explode('=>', $line);
275 $key = substr(trim($parts[0]), 1, -1); $value = substr(trim($parts[1]), 1, -2);
277 // We can now save the right part in new config file
278 changeDataInInclude(getCachePath() . 'config-local.php', $comments[$key], " '".$key."' => \"", '",', $value, 0);
282 // Finally remove old config file
283 removeFile(getPath() . 'inc/config.php');
285 // Redirect to same URL to reload our new config
286 redirectToUrl(getRequestUri());
289 // Update config entries
290 function updateConfiguration ($entries, $values, $updateMode = '', $config = '0', $force = FALSE) {
291 // Do not update config in CSS mode
292 if (($force === FALSE) && ((isCssOutputMode()) || (isRawOutputMode()) || (isInstaller()))) {
293 // This logger line may be very noisy
294 /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not updating configuration. entries[]=' . gettype($entries) . ',values[]=' . gettype($values) . ',updateMode=' . $updateMode . ',config=' . $config . ',isCssOutputMode()=' . intval(isCssOutputMode()) . ',isRawOutputMode()=' . intval(isRawOutputMode()) . ',isInstaller()=' . intval(isInstaller()));
298 // Default is empty SQL
301 // Is there multiple entries?
302 if (is_array($entries)) {
304 foreach ($entries as $idx => $entry) {
306 if (!empty($updateMode)) {
308 $SQL .= sprintf("`%s`=`%s`%s%01.5f,", $entry, $entry, $updateMode, (float) $values[$idx]);
310 // Check if string or number but no array
311 if (is_array($values[$idx])) {
312 // Arrays must be fixed...
313 reportBug(__FUNCTION__, __LINE__, 'values[' . $idx . '] should not be an array! Content=<pre>'.print_r($values[$idx], TRUE).'</pre>');
314 } elseif ($values[$idx] == 'UNIX_TIMESTAMP()') {
315 // Function UNIX_TIMESTAMP() detected
316 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Function UNIX_TIMESAMP() detected: entry=' . $entry);
317 $SQL .= sprintf('`%s`=UNIX_TIMESTAMP(),', $entry);
319 // Set timestamp in array as well
320 setConfigEntry($entry, time());
321 } elseif (!empty($updateMode)) {
322 // Is the value zero?
323 if ($values[$idx] == '0') {
329 // @TODO Call setConfigEntry() somehow
330 $SQL .= $entries = sprintf('`%s`=`%s`%s%01.5f', $entry, $entry, $updateMode, (float) $values[$idx]);
331 } elseif (($values[$idx] + 0) === $values[$idx]) {
333 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Number detected: entry=' . $entry . ',values[' . $idx . '/' . gettype($values[$idx]) . ']=' . $values[$idx]);
334 $SQL .= sprintf('`%s`=%01.5f,', $entry, (float) $values[$idx]);
336 // Set it in config as well
337 setConfigEntry($entry, $values[$idx]);
340 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Regular value detected: entry=' . $entry . ',values[' . $idx . '/' . gettype($values[$idx]) . ']=' . $values[$idx]);
341 $SQL .= sprintf("`%s`='%s',", $entry, sqlEscapeString($values[$idx]));
343 // Set it in config as well
344 setConfigEntry($entry, $values[$idx]);
350 $SQL = substr($SQL, 0, -1);
351 } elseif (!empty($updateMode)) {
352 // Is the value zero?
353 if ($values == '0') {
359 // @TODO Call setConfigEntry() somehow
360 $SQL = sprintf('`%s`=`%s`%s%01.5f', $entries, $entries, $updateMode, (float) $values);
361 } elseif ($values == 'UNIX_TIMESTAMP()') {
362 // Function UNIX_TIMESTAMP() detected
363 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Function UNIX_TIMESAMP() detected: entries=' . $entries);
364 $SQL = sprintf('`%s`=UNIX_TIMESTAMP()', $entries);
366 // Set timestamp in array as well
367 setConfigEntry($entries, time());
368 } elseif (($values + 0) === $values) {
370 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Number detected: entries=' . $entries . ',values[' . gettype($values) . ']=' . $values);
371 $SQL = sprintf('`%s`=%01.5f', $entries, (float) $values);
373 // Set it in config first
374 setConfigEntry($entries, (float) $values);
376 // Regular entry to update
377 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Regular value detected: entries=' . $entries . ',values[' . gettype($values) . ']=' . $values);
378 $SQL = sprintf("`%s`='%s'", $entries, sqlEscapeString($values));
380 // Set it in config as well
381 setConfigEntry($entries, sqlEscapeString($values));
384 // Run database update
385 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQL=' . $SQL);
386 sqlQueryEscaped('UPDATE `{?_MYSQL_PREFIX?}_config` SET ' . $SQL . ' WHERE `config`=%s LIMIT 1',
387 array(bigintval($config)), __FUNCTION__, __LINE__);
388 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQL=' . $SQL . ',affectedRows=' . sqlAffectedRows());
391 rebuildCache('config', 'config');
394 // Filter for loading configuration
395 function FILTER_LOAD_CONFIGURATION ($no = '0') {
396 // Is installation phase?
398 // Then don't load any configuration
402 // Is the value null, it comes from the 'init' filter chain
407 // Check for cache extension, cache-array and if the requested configuration is in cache
408 if ((isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
409 // Load config from cache
410 mergeConfig($GLOBALS['cache_array']['config'][$no]);
413 incrementStatsEntry('cache_hits');
414 } elseif ((!isExtensionActive('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
415 // Load config from DB
416 $result_config = sqlQueryEscaped('SELECT * FROM `{?_MYSQL_PREFIX?}_config` WHERE `config`=%s LIMIT 1',
417 array(bigintval($no)), __FUNCTION__, __LINE__);
419 // Is the config there?
420 if (sqlNumRows($result_config) == 1) {
421 // Get config from database
422 mergeConfig(sqlFetchArray($result_config));
426 sqlFreeResult($result_config);
428 // Remember this config in the array
429 $GLOBALS['cache_array']['config'][$no] = getConfigArray();
433 // "Getter" for "checked" configuration entries
434 function getCheckedConfig ($configEntries) {
436 $content = getConfigEntries($configEntries, ' checked="checked"');
442 // "Getter" for "selected" configuration entries
443 function getSelectedConfig ($configEntries) {
445 $content = getConfigEntries($configEntries, ' selected="selected"');
451 // "Getter" for configuration entries
452 function getConfigEntries ($configEntries, $value) {
456 // "Walk" through all entries
457 foreach ($configEntries as $entry) {
458 $content[$entry . '_' . strtolower(getConfig($entry))] = $value;