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 (isConfigLoaded()) {
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 // 'ENABLE_BACKLINK' => 'Y',
66 // 'display_debug_sqls' => 'N',
67 // Keep session_save_path to fall-back to php.ini setting
68 // 'session_save_path' => '',
69 // For installation phase:
70 'SMTP_HOSTNAME' => '',
72 'SMTP_PASSWORD' => '',
73 'MT_WORD' => '{--DEFAULT_MT_WORD--}',
77 // Getter for $GLOBALS['config'] entries
78 function getConfig ($configEntry) {
82 // Is the entry there?
83 if (!isset($GLOBALS['config'][$configEntry])) {
84 // Raise an error of missing entries
85 debug_report_bug(sprintf("[%s:%s] Configuration entry <em>%s</em> is missing.",
93 return $GLOBALS['config'][$configEntry];
96 // Setter for $GLOBALS['config'] entries
97 function setConfigEntry ($configEntry, $value) {
98 // Just set it (unsecured won't hurt?)
99 $GLOBALS['config'][$configEntry] = $value;
102 // Checks wether the given config entry is set
103 function isConfigEntrySet ($configEntry) {
104 return (isset($GLOBALS['config'][$configEntry]));
107 // Merges $GLOBALS['config'] with data in given array
108 function mergeConfig ($newConfig) {
109 $GLOBALS['config'] = merge_array(getConfigArray(), $newConfig);
112 // Increment or init with given value or 1 as default the given config entry
113 function incrementConfigEntry ($configEntry, $value=1) {
114 // Increment it if set or init it with 1
115 if (getConfig($configEntry) > 0) {
116 $GLOBALS['config'][$configEntry] += $value;
118 $GLOBALS['config'][$configEntry] = $value;
122 // Checks wether the configuration array is set so the config is loaded
123 function isConfigLoaded () {
125 return ((isset($GLOBALS['config'])) && (is_array($GLOBALS['config'])) && (count($GLOBALS['config']) > 0));
128 // Getter for whole $GLOBALS['config'] array
129 function getConfigArray () {
133 // Is the config set?
134 if (isConfigLoaded()) {
136 $return = $GLOBALS['config'];
143 // Updates an old inc/config.php to a inc/cache/config-local.php file
144 function updateOldConfigFile () {
145 // Watch out for these lines and execute them as single command
146 // @TODO Make this all better... :-/
148 'SITE_KEY' => 'SITE_KEY',
149 'DEFAULT_LANG' => 'DEFAULT_LANG',
150 'warn_no_pass' => 'WARN_NO_PASS',
151 'WRITE_FOOTER' => 'WRITE_FOOTER',
152 'OUTPUT_MODE' => 'OUTPUT_MODE',
153 'MAIN_TITLE' => 'MAIN_TITLE',
154 'SLOGAN' => 'SLOGAN',
155 'WEBMASTER' => 'WEBMASTER',
156 'mxchange_installed' => 'MXCHANGE_INSTALLED',
157 'admin_registered' => 'ADMIN_REGISTERED',
158 '_MYSQL_PREFIX' => '_MYSQL_PREFIX',
159 '_TABLE_TYPE' => '_TABLE_TYPE',
160 '_DB_TYPE' => '_DB_TYPE',
161 'SMTP_HOSTNAME' => 'SMTP_HOSTNAME',
162 'SMTP_USER' => 'SMTP_USER',
163 'SMTP_PASSWORD' => 'SMTP_PASSWORD',
164 'ENABLE_BACKLINK' => 'ENABLE_BACKLINK',
165 'MAIN_TITLE' => 'MAIN_TITLE',
166 'SLOGAN' => 'SLOGAN',
167 'WEBMASTER' => 'WEBMASTER',
172 // Make these lower-case! (damn stupid code...)
173 $lowerCase = array('WARN_NO_PASS', 'MXCHANGE_INSTALLED', 'ADMIN_REGISTERED');
175 // Special comments...
177 'WARN_NO_PASS' => 'NULLPASS-WARNING',
178 'MXCHANGE_INSTALLED' => 'INSTALLED',
179 'ADMIN_REGISTERED' => 'ADMIN-SETUP',
180 '_MYSQL_PREFIX' => 'MYSQL-PREFIX',
181 '_TABLE_TYPE' => 'TABLE-TYPE',
182 '_DB_TYPE' => 'DATABASE-TYPE',
183 'ENABLE_BACKLINK' => 'BACKLINK',
184 'host' => 'MYSQL-HOST',
185 'dbase' => 'MYSQL-DBASE',
186 'login' => 'MYSQL-LOGIN',
187 'password' => 'MYSQL-PASSWORD'
190 // Copy template to new file destionation
191 copyFileVerified(getConfig('PATH') . 'inc/config-local.php.dist', getConfig('CACHE_PATH') . 'config-local.php', 0644);
193 // First of all, load the old one!
194 $oldConfig = explode("\n", readFromFile(getConfig('PATH') . 'inc/config.php'));
196 // Now, analyze every entry
198 foreach ($oldConfig as $line) {
199 // Check all watch lines
200 foreach ($watchLines as $old => $new) {
201 // Add define() command around old one
202 $old = "define('" . $old . "',";
204 // Is the line found?
205 if ((substr($line, 0, strlen($old)) == $old) && (!isset($done[$old]))) {
207 //* DEBUG: */ outputHtml(htmlentities($line) . " - FOUND!<br />");
212 // Setting config entry is new default behaviour!
213 $function = 'setConfigEntry';
216 $comment = str_replace('_', '-', $new);
218 // Do we have a special comment?
219 if (isset($comments[$new])) {
221 $comment = $comments[$new];
224 // Do we need to make $new lowercase?
226 if (in_array($new, $lowerCase)) {
228 $new = strtolower($new);
231 /// ... and write it to the new config
232 //* DEBUG: */ outputHtml('function=' . $function . ',new=' . $new . ',comment=' . $comment . "<br />");
233 changeDataInFile(getConfig('CACHE_PATH') . 'config-local.php', $comment, $function . "('" . $oldNew . "', \"", "\");", constant($new), 0);
234 //* DEBUG: */ outputHtml("CHANGED!<br />");
242 // By default the old array $MySQL was not found
245 // Analyze every entry again for the MySQL configuration
246 foreach ($oldConfig as $line) {
250 // Is the $MySQL found?
251 if (substr($line, 0, 6) == "\$MySQL") {
254 } elseif ($found === true) {
255 // Now check this row
256 if (substr($line, 0, 2) == ');') {
257 // MySQL array is closed so stop looking for it
259 } elseif (substr($line, 0, 2) == '//') {
265 //* DEBUG: */ outputHtml(htmlentities($line) . " - MySQL!<br />");
267 // Split parts so we can check them and prepare them
268 $parts = explode('=>', $line);
269 $key = substr(trim($parts[0]), 1, -1); $value = substr(trim($parts[1]), 1, -2);
271 // We can now save the right part in new config file
272 changeDataInFile(getConfig('CACHE_PATH') . 'config-local.php', $comments[$key], " '".$key."' => \"", "\",", $value, 0);
276 // Finally remove old config file
277 removeFile(getConfig('PATH') . 'inc/config.php');
279 // Redirect to same URL to reload our new config
280 redirectToUrl(getRequestUri());
283 // Update config entries
284 function updateConfiguration ($entries, $values, $updateMode='') {
285 // Do not update config in CSS mode
286 if ((getOutputMode() == 1) || (getOutputMode() == -1) || (isInstallationPhase())) {
290 // Do we have multiple entries?
291 if (is_array($entries)) {
294 foreach ($entries as $idx => $entry) {
296 if (!empty($updateMode)) {
298 $all .= sprintf("`%s`=`%s`%s%s,", $entry, $entry, $updateMode, (float)$values[$idx]);
300 // Check if string or number
301 if (($values[$idx] + 0) === $values[$idx]) {
303 $all .= sprintf("`%s`=%s,", $entry, (float)$values[$idx]);
304 } elseif ($values[$idx] == 'UNIX_TIMESTAMP()') {
305 // Function UNIX_TIMESTAMP() detected
306 $all .= sprintf("`%s`=%s,", $entry, $values[$idx]);
309 $all .= sprintf("`%s`='%s',", $entry, SQL_ESCAPE($values[$idx]));
312 // Set it in config as well
313 setConfigEntry($entry, $values[$idx]);
318 $entries = substr($all, 0, -1);
319 } elseif (!empty($updateMode)) {
321 // @TODO Find a way for updating configuration here
322 $entries .= sprintf("=%s%s%s", $entries, $updateMode, (float)$values);
324 // Set it in config first
325 setConfigEntry($entries, $values);
327 // Regular entry to update
328 $entries .= sprintf("='%s'", SQL_ESCAPE($values));
331 // Run database update
332 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "entries={$entries}");
333 SQL_QUERY("UPDATE `{?_MYSQL_PREFIX?}_config` SET ".$entries." WHERE `config`=0 LIMIT 1", __FUNCTION__, __LINE__);
334 //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):entries={$entries},affectedRows={$affectedRows}<br />");
337 rebuildCacheFile('config', 'config');
340 // Filter for loading configuration
341 function FILTER_LOAD_CONFIGURATION ($no = 0) {
342 // Check for cache extension, cache-array and if the requested configuration is in cache
343 if ((isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
344 // Load config from cache
345 //* DEBUG: */ outputHtml(gettype($GLOBALS['cache_array']['config'][$no])."<br />");
346 mergeConfig($GLOBALS['cache_array']['config'][$no]);
347 //foreach ($GLOBALS['cache_array']['config'][$no] as $key => $value) {
348 // setConfigEntry($key, $value);
351 // Count cache hits if exists
352 if ((isStatsEntrySet('cache_hits')) && (isExtensionActive('cache'))) {
353 incrementStatsEntry('cache_hits');
355 } elseif ((!isExtensionActive('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
356 // Load config from DB
357 $result_config = SQL_QUERY_ESC("SELECT * FROM `{?_MYSQL_PREFIX?}_config` WHERE config=%d LIMIT 1",
358 array(bigintval($no)), __FUNCTION__, __LINE__);
360 // Is the config there?
361 if (SQL_NUMROWS($result_config) == 1) {
362 // Get config from database
363 mergeConfig(SQL_FETCHARRAY($result_config));
367 SQL_FREERESULT($result_config);
369 // Remember this config in the array
370 $GLOBALS['cache_array']['config'][$no] = getConfigArray();