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 - 2008 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')) {
41 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
45 // Merges $_CONFIG with data in given array
46 function mergeConfig ($newConfig) {
48 $_CONFIG = merge_array(getConfigArray(), $newConfig);
51 // Getter for $_CONFIG entries
52 function getConfig ($configEntry) {
58 // Is the entry there?
59 if (isConfigEntrySet($configEntry)) {
61 $value = $_CONFIG[$configEntry];
68 // Setter for $_CONFIG entries
69 function setConfigEntry ($configEntry, $value) {
72 // Secure the entry name
73 if (function_exists('SQL_ESCAPE')) {
74 // Use our secure function
75 $configEntry = SQL_ESCAPE($configEntry);
77 // Use maybe insecure function
78 $configEntry = smartAddSlashes($configEntry);
81 // If config array isn't set, set it
82 if (!isConfigLoaded()) {
83 // Init configuration array
88 $_CONFIG[$configEntry] = $value;
91 // Checks wether the given config entry is set
92 function isConfigEntrySet ($configEntry) {
94 return (isset($_CONFIG[$configEntry]));
97 // Increment or init with given value or 1 as default the given config entry
98 function incrementConfigEntry ($configEntry, $value=1) {
101 // Increment it if set or init it with 1
102 if (getConfig($configEntry) > 0) {
103 $_CONFIG[$configEntry] += $value;
105 $_CONFIG[$configEntry] = $value;
109 // Checks wether the configuration array is set so the config is loaded
110 function isConfigLoaded () {
114 return ((isset($_CONFIG)) && (is_array($_CONFIG)) && (count($_CONFIG) > 0));
117 // Init the config array
118 function initConfig () {
121 // Set a minimum dummy configuration
125 'last_update' => time()
129 // Load configuration and return it as an arry
130 function loadConfiguration ($no = '0') {
131 // Check for cache extension, cache-array and if the requested configuration is in cache
132 if ((isset($GLOBALS['cache_array'])) && (is_array($GLOBALS['cache_array'])) && (isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
133 // Load config from cache
134 //* DEBUG: */ echo gettype($GLOBALS['cache_array']['config'][$no])."<br />\n";
135 foreach ($GLOBALS['cache_array']['config'][$no] as $key => $value) {
136 setConfigEntry($key, $value);
139 // Count cache hits if exists
140 if ((isConfigEntrySet('cache_hits')) && (EXT_IS_ACTIVE('cache'))) {
141 incrementConfigEntry('cache_hits');
143 } elseif ((!EXT_IS_ACTIVE('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
144 // Load config from DB
145 $result_config = SQL_QUERY_ESC("SELECT * FROM `{!_MYSQL_PREFIX!}_config` WHERE config=%d LIMIT 1",
146 array(bigintval($no)), __FUNCTION__, __LINE__);
148 // Is the config there?
149 if (SQL_NUMROWS($result_config) == 1) {
150 // Get config from database
151 mergeConfig(SQL_FETCHARRAY($result_config));
155 SQL_FREERESULT($result_config);
157 // Remember this config in the array
158 $GLOBALS['cache_array']['config'][$no] = getConfigArray();
162 // Getter for whole $_CONFIG array
163 function getConfigArray () {
169 // Is the config set?
170 if (isConfigLoaded()) {
179 // Updates an old inc/config.php to a inc/cache/config-local.php file
180 function updateOldConfigFile () {
181 // Watch out for these lines and execute them as single command
182 // @TODO Make this all better... :-/
184 "define('SITE_KEY'," => 'SITE_KEY',
185 "define('DEFAULT_LANG'," => 'DEFAULT_LANG',
186 "define('warn_no_pass'," => 'WARN_NO_PASS',
187 "define('WRITE_FOOTER'," => 'WRITE_FOOTER',
188 "define('OUTPUT_MODE'," => 'OUTPUT_MODE',
189 "define('MAIN_TITLE'," => 'MAIN_TITLE',
190 "define('SLOGAN'," => 'SLOGAN',
191 "define('WEBMASTER'," => 'WEBMASTER',
192 "define('mxchange_installed'," => 'MXCHANGE_INSTALLED',
193 "define('admin_registered'," => 'ADMIN_REGISTERED',
194 "define('_MYSQL_PREFIX'," => '_MYSQL_PREFIX',
195 "define('_TABLE_TYPE'," => '_TABLE_TYPE',
196 "define('_DB_TYPE'," => '_DB_TYPE',
197 "define('SMTP_HOSTNAME'," => 'SMTP_HOSTNAME',
198 "define('SMTP_USER'" => 'SMTP_USER',
199 "define('SMTP_PASSWORD'," => 'SMTP_PASSWORD',
200 "define('ENABLE_BACKLINK'," => 'ENABLE_BACKLINK',
203 // Make these lower-case! (damn stupid code...)
204 $lowerCase = array('WARN_NO_PASS', 'MXCHANGE_INSTALLED', 'ADMIN_REGISTERED');
206 // These are still constants...
207 // @TODO Rewrite these all to config entries, if somehow possible
208 $constants = array('MAIN_TITLE', 'SLOGAN', 'WEBMASTER');
210 // Special comments...
212 'WARN_NO_PASS' => 'NULLPASS-WARNING',
213 'MXCHANGE_INSTALLED' => 'INSTALLED',
214 'ADMIN_REGISTERED' => 'ADMIN-SETUP',
215 '_MYSQL_PREFIX' => 'MYSQL-PREFIX',
216 '_TABLE_TYPE' => 'TABLE-TYPE',
217 '_DB_TYPE' => 'DATABASE-TYPE',
218 'ENABLE_BACKLINK' => 'BACKLINK',
219 'host' => 'MYSQL-HOST',
220 'dbase' => 'MYSQL-DBASE',
221 'login' => 'MYSQL-LOGIN',
222 'password' => 'MYSQL-PASSWORD'
225 // Copy template to new file destionation
226 copyFileVerified(constant('PATH') . 'inc/config-local.php.dist', constant('PATH') . 'inc/cache/config-local.php', 0644);
228 // First of all, load the old one!
229 $oldConfig = explode("\n", readFromFile(constant('PATH') . 'inc/config.php'));
231 // Now, analyze every entry
233 foreach ($oldConfig as $line) {
234 // Check all watch lines
235 foreach ($watchLines as $old => $new) {
236 // Is the line found?
237 if ((substr($line, 0, strlen($old)) == $old) && (!isset($done[$old]))) {
239 //* DEBUG: */ print htmlentities($line) . " - FOUND!<br />\n";
244 // Setting config entry is new default behaviour!
245 $function = 'setConfigEntry';
247 // Still some old constants left?
248 if (in_array($new, $constants)) {
249 // Then switch over...
250 $function = 'define';
254 $comment = str_replace('_', '-', $new);
256 // Do we have a special comment?
257 if (isset($comments[$new])) {
259 $comment = $comments[$new];
262 // Do we need to make $new lowercase?
264 if (in_array($new, $lowerCase)) {
266 $new = strtolower($new);
269 /// ... and write it to the new config
270 //* DEBUG: */ print 'function=' . $function . ',new=' . $new . ',comment=' . $comment . "<br />\n";
271 changeDataInFile(constant('PATH') . 'inc/cache/config-local.php', $comment, $function . "('" . $oldNew . "', \"", "\");", constant($new), 0);
272 //* DEBUG: */ print "CHANGED!<br />\n";
280 // By default the old array $MySQL was not found
283 // Analyze every entry again for the MySQL configuration
284 foreach ($oldConfig as $line) {
288 // Is the $MySQL found?
289 if (substr($line, 0, 6) == "\$MySQL") {
292 } elseif ($found === true) {
293 // Now check this row
294 if (substr($line, 0, 2) == ');') {
295 // MySQL array is closed so stop looking for it
297 } elseif (substr($line, 0, 2) == '//') {
303 //* DEBUG: */ print htmlentities($line) . " - MySQL!<br />\n";
305 // Split parts so we can check them and prepare them
306 $parts = explode('=>', $line);
307 $key = substr(trim($parts[0]), 1, -1); $value = substr(trim($parts[1]), 1, -2);
309 // We can now save the right part in new config file
310 changeDataInFile(constant('PATH') . 'inc/cache/config-local.php', $comments[$key], " '".$key."' => \"", "\",", $value, 0);
314 // Finally remove old config file
315 removeFile(constant('PATH') . 'inc/config.php');
317 // Redirect to same URL to reload our new config
318 redirectToUrl($_SERVER['REQUEST_URI']);