2 /************************************************************************
3 * MXChange v0.2.1 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 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
44 // Init the config array
45 function initConfig () {
46 // Init not if already found
47 if (isConfigurationLoaded()) {
48 // Already initialized
49 debug_report_bug(sprintf("[%s:%s] Configuration is already initialized.", __FUNCTION__, __LINE__));
52 // Set a minimum of configuration, required to by-pass some error triggers in getConfig()
53 $GLOBALS['config'] = array(
57 // 'DEFAULT_SALT_LENGTH' => 40,
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 // 'verbose_sql' => 'Y',
65 // For installation phase:
66 'SMTP_HOSTNAME' => '',
68 'SMTP_PASSWORD' => '',
69 'MT_WORD' => '{--DEFAULT_MT_WORD--}',
73 // Getter for $GLOBALS['config'] entries
74 function getConfig ($configEntry) {
78 // Is the entry there?
79 if (!isset($GLOBALS['config'][$configEntry])) {
80 // Raise an error of missing entries
81 debug_report_bug(sprintf("[%s:%s] Configuration entry <em>%s</em> is missing.",
89 return $GLOBALS['config'][$configEntry];
92 // Setter for $GLOBALS['config'] entries
93 function setConfigEntry ($configEntry, $value) {
94 // Just set it (unsecured won't hurt?)
95 $GLOBALS['config'][$configEntry] = $value;
98 // Checks wether the given config entry is set
99 function isConfigEntrySet ($configEntry) {
100 //* DEBUG: */ print __FUNCTION__.':'.$configEntry.'='.intval(isset($GLOBALS['config'][$configEntry])).'<br />';
101 return (isset($GLOBALS['config'][$configEntry]));
104 // Merges $GLOBALS['config'] with data in given array
105 function mergeConfig ($newConfig) {
106 $GLOBALS['config'] = merge_array(getConfigArray(), $newConfig);
109 // Increment or init with given value or 1 as default the given config entry
110 function incrementConfigEntry ($configEntry, $value=1) {
111 // Increment it if set or init it with 1
112 if (isConfigEntrySet($configEntry)) {
113 $GLOBALS['config'][$configEntry] += $value;
115 $GLOBALS['config'][$configEntry] = $value;
119 // Checks wether the configuration array is set so the config is loaded
120 function isConfigurationLoaded () {
122 return (isset($GLOBALS['config']['config']));
125 // Getter for whole $GLOBALS['config'] array
126 function getConfigArray () {
130 // Is the config set?
131 if (isset($GLOBALS['config'])) {
133 $return = $GLOBALS['config'];
140 // Updates an old inc/config.php to a inc/cache/config-local.php file
141 function updateOldConfigFile () {
142 // Watch out for these lines and execute them as single command
143 // @TODO Make this all better... :-/
145 'SITE_KEY' => 'SITE_KEY',
146 'DEFAULT_LANG' => 'DEFAULT_LANG',
147 'warn_no_pass' => 'WARN_NO_PASS',
148 'WRITE_FOOTER' => 'WRITE_FOOTER',
149 'OUTPUT_MODE' => 'OUTPUT_MODE',
150 'MAIN_TITLE' => 'MAIN_TITLE',
151 'SLOGAN' => 'SLOGAN',
152 'WEBMASTER' => 'WEBMASTER',
153 'mxchange_installed' => 'MXCHANGE_INSTALLED',
154 'admin_registered' => 'ADMIN_REGISTERED',
155 '_MYSQL_PREFIX' => '_MYSQL_PREFIX',
156 '_TABLE_TYPE' => '_TABLE_TYPE',
157 '_DB_TYPE' => '_DB_TYPE',
158 'SMTP_HOSTNAME' => 'SMTP_HOSTNAME',
159 'SMTP_USER' => 'SMTP_USER',
160 'SMTP_PASSWORD' => 'SMTP_PASSWORD',
161 'ENABLE_BACKLINK' => 'ENABLE_BACKLINK',
162 'MAIN_TITLE' => 'MAIN_TITLE',
163 'SLOGAN' => 'SLOGAN',
164 'WEBMASTER' => 'WEBMASTER',
169 // Make these lower-case! (damn stupid code...)
170 $lowerCase = array('WARN_NO_PASS', 'MXCHANGE_INSTALLED', 'ADMIN_REGISTERED');
172 // Special comments...
174 'WARN_NO_PASS' => 'NULLPASS-WARNING',
175 'MXCHANGE_INSTALLED' => 'INSTALLED',
176 'ADMIN_REGISTERED' => 'ADMIN-SETUP',
177 '_MYSQL_PREFIX' => 'MYSQL-PREFIX',
178 '_TABLE_TYPE' => 'TABLE-TYPE',
179 '_DB_TYPE' => 'DATABASE-TYPE',
180 'ENABLE_BACKLINK' => 'BACKLINK',
181 'host' => 'MYSQL-HOST',
182 'dbase' => 'MYSQL-DBASE',
183 'login' => 'MYSQL-LOGIN',
184 'password' => 'MYSQL-PASSWORD'
187 // Copy template to new file destionation
188 copyFileVerified(getConfig('PATH') . 'inc/config-local.php.dist', getConfig('CACHE_PATH') . 'config-local.php', 0644);
190 // First of all, load the old one!
191 $oldConfig = explode("\n", readFromFile(getConfig('PATH') . 'inc/config.php'));
193 // Now, analyze every entry
195 foreach ($oldConfig as $line) {
196 // Check all watch lines
197 foreach ($watchLines as $old => $new) {
198 // Add define() command around old one
199 $old = "define('" . $old . "',";
201 // Is the line found?
202 if ((substr($line, 0, strlen($old)) == $old) && (!isset($done[$old]))) {
204 //* DEBUG: */ outputHtml(htmlentities($line) . " - FOUND!<br />");
209 // Setting config entry is new default behaviour!
210 $function = 'setConfigEntry';
213 $comment = str_replace('_', '-', $new);
215 // Do we have a special comment?
216 if (isset($comments[$new])) {
218 $comment = $comments[$new];
221 // Do we need to make $new lowercase?
223 if (in_array($new, $lowerCase)) {
225 $new = strtolower($new);
228 /// ... and write it to the new config
229 //* DEBUG: */ outputHtml('function=' . $function . ',new=' . $new . ',comment=' . $comment . "<br />");
230 changeDataInFile(getConfig('CACHE_PATH') . 'config-local.php', $comment, $function . "('" . $oldNew . "', \"", "\");", constant($new), 0);
231 //* DEBUG: */ outputHtml("CHANGED!<br />");
239 // By default the old array $MySQL was not found
242 // Analyze every entry again for the MySQL configuration
243 foreach ($oldConfig as $line) {
247 // Is the $MySQL found?
248 if (substr($line, 0, 6) == "\$MySQL") {
251 } elseif ($found === true) {
252 // Now check this row
253 if (substr($line, 0, 2) == ');') {
254 // MySQL array is closed so stop looking for it
256 } elseif (substr($line, 0, 2) == '//') {
262 //* DEBUG: */ outputHtml(htmlentities($line) . " - MySQL!<br />");
264 // Split parts so we can check them and prepare them
265 $parts = explode('=>', $line);
266 $key = substr(trim($parts[0]), 1, -1); $value = substr(trim($parts[1]), 1, -2);
268 // We can now save the right part in new config file
269 changeDataInFile(getConfig('CACHE_PATH') . 'config-local.php', $comments[$key], " '".$key."' => \"", "\",", $value, 0);
273 // Finally remove old config file
274 removeFile(getConfig('PATH') . 'inc/config.php');
276 // Redirect to same URL to reload our new config
277 redirectToUrl(getRequestUri());
280 // Update config entries
281 function updateConfiguration ($entries, $values, $updateMode='', $config = '0') {
282 // Do not update config in CSS mode
283 if ((getOutputMode() == 1) || (getOutputMode() == -1) || (isInstallationPhase())) {
287 // Do we have multiple entries?
288 if (is_array($entries)) {
291 foreach ($entries as $idx => $entry) {
293 if (!empty($updateMode)) {
295 $all .= sprintf("`%s`=`%s`%s%s,", $entry, $entry, $updateMode, (float)$values[$idx]);
297 // Check if string or number
298 if (($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: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):entries={$entries},affectedRows={$affectedRows}<br />");
352 rebuildCacheFile('config', 'config');
355 // Filter for loading configuration
356 function FILTER_LOAD_CONFIGURATION ($no = '0') {
357 // Is the value null, fix it :(
358 if (is_null($no)) $no = '0';
360 // Check for cache extension, cache-array and if the requested configuration is in cache
361 if ((isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
362 // Load config from cache
363 mergeConfig($GLOBALS['cache_array']['config'][$no]);
365 // Count cache hits if exists
366 if ((isStatsEntrySet('cache_hits')) && (isExtensionActive('cache'))) {
367 incrementStatsEntry('cache_hits');
369 } elseif ((!isExtensionActive('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
370 // Load config from DB
371 $result_config = SQL_QUERY_ESC("SELECT * FROM `{?_MYSQL_PREFIX?}_config` WHERE `config`='%s' LIMIT 1",
372 array(bigintval($no)), __FUNCTION__, __LINE__);
374 // Is the config there?
375 if (SQL_NUMROWS($result_config) == 1) {
376 // Get config from database
377 mergeConfig(SQL_FETCHARRAY($result_config));
381 SQL_FREERESULT($result_config);
383 // Remember this config in the array
384 $GLOBALS['cache_array']['config'][$no] = getConfigArray();