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 : Many non-MySQL functions (also file access) *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Viele Nicht-MySQL-Funktionen (auch Dateizugriff) *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 - 2009 by Roland Haeder *
21 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Init the config array
46 function initConfig () {
47 // Init not if already found
48 if (isConfigurationLoaded()) {
49 // Already initialized
50 debug_report_bug(__FUNCTION__, __LINE__, 'Configuration is already initialized.');
53 // Set a minimum of configuration, required to by-pass some error triggers in getConfig()
54 $GLOBALS['config'] = array(
58 // 'DEFAULT_SALT_LENGTH' => 40,
59 // 'DEBUG_MODE' => 'N',
60 // 'DEBUG_RESET' => 'N',
61 // 'DEBUG_MONTHLY' => '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) {
79 // Is the entry there?
80 if (!isConfigEntrySet($configEntry)) {
81 // Raise an error of missing entries
82 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Configuration entry <em>%s</em> is missing.", $configEntry));
86 //* DEBUG: */ error_log(__FUNCTION__.'['.__LINE__.':] '.$configEntry.'='.$GLOBALS['config'][$configEntry]);
87 return $GLOBALS['config'][$configEntry];
90 // Setter for $GLOBALS['config'] entries
91 function setConfigEntry ($configEntry, $value) {
92 // Just set it (unsecured won't hurt?)
93 $GLOBALS['config'][$configEntry] = $value;
96 // Checks wether the given config entry is set
97 function isConfigEntrySet ($configEntry) {
98 //* DEBUG: */ debugOutput(__FUNCTION__.':'.$configEntry.'='.intval(isset($GLOBALS['config'][$configEntry])));
99 return (isset($GLOBALS['config'][$configEntry]));
102 // Merges $GLOBALS['config'] with data in given array
103 function mergeConfig ($newConfig) {
104 $GLOBALS['config'] = merge_array(getConfigArray(), $newConfig);
107 // Increment or init with given value or 1 as default the given config entry
108 function incrementConfigEntry ($configEntry, $value=1) {
109 // Increment it if set or init it with 1
110 if (isConfigEntrySet($configEntry)) {
111 $GLOBALS['config'][$configEntry] += $value;
113 $GLOBALS['config'][$configEntry] = $value;
117 // Checks wether the configuration array is set so the config is loaded
118 function isConfigurationLoaded () {
120 return (isset($GLOBALS['config']['config']));
123 // Getter for whole $GLOBALS['config'] array
124 function getConfigArray () {
128 // Is the config set?
129 if (isset($GLOBALS['config'])) {
131 $return = $GLOBALS['config'];
138 // Updates an old inc/config.php to a inc/cache/config-local.php file
139 function updateOldConfigFile () {
140 // Watch out for these lines and execute them as single command
141 // @TODO Make this all better... :-/
143 'SITE_KEY' => 'SITE_KEY',
144 'DEFAULT_LANG' => 'DEFAULT_LANG',
145 'warn_no_pass' => 'WARN_NO_PASS',
146 'WRITE_FOOTER' => 'WRITE_FOOTER',
147 'OUTPUT_MODE' => 'OUTPUT_MODE',
148 'MAIN_TITLE' => 'MAIN_TITLE',
149 'SLOGAN' => 'SLOGAN',
150 'WEBMASTER' => 'WEBMASTER',
151 'mxchange_installed' => 'MXCHANGE_INSTALLED',
152 'admin_registered' => 'ADMIN_REGISTERED',
153 '_MYSQL_PREFIX' => '_MYSQL_PREFIX',
154 '_TABLE_TYPE' => '_TABLE_TYPE',
155 '_DB_TYPE' => '_DB_TYPE',
156 'SMTP_HOSTNAME' => 'SMTP_HOSTNAME',
157 'SMTP_USER' => 'SMTP_USER',
158 'SMTP_PASSWORD' => 'SMTP_PASSWORD',
159 'ENABLE_BACKLINK' => 'ENABLE_BACKLINK',
160 'MAIN_TITLE' => 'MAIN_TITLE',
161 'SLOGAN' => 'SLOGAN',
162 'WEBMASTER' => 'WEBMASTER',
167 // Make these lower-case! (damn stupid code...)
168 $lowerCase = array('WARN_NO_PASS', 'MXCHANGE_INSTALLED', 'ADMIN_REGISTERED');
170 // Special comments...
172 'WARN_NO_PASS' => 'NULLPASS-WARNING',
173 'MXCHANGE_INSTALLED' => 'INSTALLED',
174 'ADMIN_REGISTERED' => 'ADMIN-SETUP',
175 '_MYSQL_PREFIX' => 'MYSQL-PREFIX',
176 '_TABLE_TYPE' => 'TABLE-TYPE',
177 '_DB_TYPE' => 'DATABASE-TYPE',
178 'ENABLE_BACKLINK' => 'BACKLINK',
179 'host' => 'MYSQL-HOST',
180 'dbase' => 'MYSQL-DBASE',
181 'login' => 'MYSQL-LOGIN',
182 'password' => 'MYSQL-PASSWORD'
185 // Copy template to new file destionation
186 copyFileVerified(getPath() . 'inc/config-local.php.dist', getCachePath() . 'config-local.php', 0644);
188 // First of all, load the old one!
189 $oldConfig = explode("\n", readFromFile(getPath() . 'inc/config.php'));
191 // Now, analyze every entry
193 foreach ($oldConfig as $line) {
194 // Check all watch lines
195 foreach ($watchLines as $old => $new) {
196 // Add define() command around old one
197 $old = "define('" . $old . "',";
199 // Is the line found?
200 if ((substr($line, 0, strlen($old)) == $old) && (!isset($done[$old]))) {
202 //* DEBUG: */ debugOutput(secureString($line) . ' - FOUND!');
207 // Setting config entry is new default behaviour!
208 $function = 'setConfigEntry';
211 $comment = str_replace('_', '-', $new);
213 // Do we have a special comment?
214 if (isset($comments[$new])) {
216 $comment = $comments[$new];
219 // Do we need to make $new lowercase?
221 if (in_array($new, $lowerCase)) {
223 $new = strtolower($new);
226 /// ... and write it to the new config
227 //* DEBUG: */ debugOutput('function=' . $function . ',new=' . $new . ',comment=' . $comment);
228 changeDataInFile(getCachePath() . 'config-local.php', $comment, $function . "('" . $oldNew . "', \"", '");', constant($new), 0);
229 //* DEBUG: */ debugOutput('CHANGED!');
237 // By default the old array $MySQL was not found
240 // Analyze every entry again for the MySQL configuration
241 foreach ($oldConfig as $line) {
245 // Is the $MySQL found?
246 if (substr($line, 0, 6) == "\$MySQL") {
249 } elseif ($found === true) {
250 // Now check this row
251 if (substr($line, 0, 2) == ');') {
252 // MySQL array is closed so stop looking for it
254 } elseif (substr($line, 0, 2) == '//') {
260 //* DEBUG: */ debugOutput(secureString($line) . ' - MySQL!');
262 // Split parts so we can check them and prepare them
263 $parts = explode('=>', $line);
264 $key = substr(trim($parts[0]), 1, -1); $value = substr(trim($parts[1]), 1, -2);
266 // We can now save the right part in new config file
267 changeDataInFile(getCachePath() . 'config-local.php', $comments[$key], " '".$key."' => \"", '",', $value, 0);
271 // Finally remove old config file
272 removeFile(getPath() . 'inc/config.php');
274 // Redirect to same URL to reload our new config
275 redirectToUrl(getRequestUri());
278 // Update config entries
279 function updateConfiguration ($entries, $values, $updateMode='', $config = '0') {
280 // Do not update config in CSS mode
281 if ((getScriptOutputMode() == 1) || (getScriptOutputMode() == -1) || (isInstallationPhase())) {
285 // Do we have multiple entries?
286 if (is_array($entries)) {
289 foreach ($entries as $idx => $entry) {
291 if (!empty($updateMode)) {
293 $all .= sprintf("`%s`=`%s`%s%s,", $entry, $entry, $updateMode, (float)$values[$idx]);
295 // Check if string or number but no array
296 if (is_array($values[$idx])) {
297 // Arrays must be fixed...
298 debug_report_bug(__FUNCTION__, __LINE__, 'values[' . $idx . '] should not be an array! Content=<pre>'.print_r($values[$idx], true).'</pre>');
299 } elseif (($values[$idx] + 0) === $values[$idx]) {
301 $all .= sprintf("`%s`=%s,", $entry, (float)$values[$idx]);
303 // Set it in config as well
304 setConfigEntry($entry, $values[$idx]);
305 } elseif ($values[$idx] == 'UNIX_TIMESTAMP()') {
306 // Function UNIX_TIMESTAMP() detected
307 $all .= sprintf("`%s`=UNIX_TIMESTAMP(),", $entry);
309 // Set timestamp in array as well
310 setConfigEntry($entry, time());
313 $all .= sprintf("`%s`='%s',", $entry, SQL_ESCAPE($values[$idx]));
315 // Set it in config as well
316 setConfigEntry($entry, $values[$idx]);
322 $entries = substr($all, 0, -1);
323 } elseif (!empty($updateMode)) {
325 $entries = sprintf("`%s`=`%s`%s%s", $entries, $entries, $updateMode, (float)$values);
326 } elseif (($values + 0) === $values) {
328 $entries = sprintf("`%s`=%s", $entries, (float)$values);
330 // Set it in config first
331 setConfigEntry($entries, (float)$values);
332 } elseif ($values == 'UNIX_TIMESTAMP()') {
333 // Function UNIX_TIMESTAMP() detected
334 $entries = sprintf("`%s`=UNIX_TIMESTAMP()", $entries);
336 // Set timestamp in array as well
337 setConfigEntry($entries, time());
339 // Regular entry to update
340 $entries = sprintf("`%s`='%s'", $entries, SQL_ESCAPE($values));
342 // Set it in config as well
343 setConfigEntry($entries, SQL_ESCAPE($values));
346 // Run database update
347 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "entries={$entries}");
348 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_config` SET ".$entries." WHERE `config`=%s LIMIT 1",
349 array(bigintval($config)), __FUNCTION__, __LINE__);
350 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "entries={$entries},affectedRows={$affectedRows}<br />");
353 rebuildCache('config', 'config');
356 // Filter for loading configuration
357 function FILTER_LOAD_CONFIGURATION ($no = '0') {
358 // Is the value null, fix it :(
359 if (is_null($no)) $no = '0';
361 // Check for cache extension, cache-array and if the requested configuration is in cache
362 if ((isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
363 // Load config from cache
364 mergeConfig($GLOBALS['cache_array']['config'][$no]);
366 // Count cache hits if exists
367 if ((isStatsEntrySet('cache_hits')) && (isExtensionActive('cache'))) {
368 incrementStatsEntry('cache_hits');
370 } elseif ((!isExtensionActive('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
371 // Load config from DB
372 $result_config = SQL_QUERY_ESC("SELECT * FROM `{?_MYSQL_PREFIX?}_config` WHERE `config`='%s' LIMIT 1",
373 array(bigintval($no)), __FUNCTION__, __LINE__);
375 // Is the config there?
376 if (SQL_NUMROWS($result_config) == 1) {
377 // Get config from database
378 mergeConfig(SQL_FETCHARRAY($result_config));
382 SQL_FREERESULT($result_config);
384 // Remember this config in the array
385 $GLOBALS['cache_array']['config'][$no] = getConfigArray();