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 // Init the config array
46 function initConfig () {
47 // Init not if already found
48 if ((isset($GLOBALS['config'])) && (count($GLOBALS['config']) >= 3)) {
49 // Already initialized
50 trigger_error(sprintf("[%s:%s] Configuration is already initialized.", __FUNCTION__, __LINE__));
53 // Set a minimum dummy configuration
54 $GLOBALS['config'] = array(
57 'last_update' => time(),
61 'default_theme' => 'default',
65 // Getter for $GLOBALS['config'] entries
66 function getConfig ($configEntry) {
70 // Is the entry there?
71 if (!isConfigEntrySet($configEntry)) {
72 // Raise an error of missing entries
73 trigger_error(sprintf("[%s:%s] Configuration entry <em>%s</em> is missing.", __FUNCTION__, __LINE__, $configEntry));
77 return $GLOBALS['config'][$configEntry];
80 // Setter for $GLOBALS['config'] entries
81 function setConfigEntry ($configEntry, $value) {
82 // Secure the entry name
83 if (function_exists('SQL_ESCAPE')) {
84 // Use our secure function
85 $configEntry = SQL_ESCAPE($configEntry);
87 // Use maybe insecure function
88 $configEntry = smartAddSlashes($configEntry);
92 $GLOBALS['config'][$configEntry] = $value;
95 // Checks wether the given config entry is set
96 function isConfigEntrySet ($configEntry) {
97 return (isset($GLOBALS['config'][$configEntry]));
100 // Merges $GLOBALS['config'] with data in given array
101 function mergeConfig ($newConfig) {
102 $GLOBALS['config'] = merge_array(getConfigArray(), $newConfig);
105 // Increment or init with given value or 1 as default the given config entry
106 function incrementConfigEntry ($configEntry, $value=1) {
107 // Increment it if set or init it with 1
108 if (getConfig($configEntry) > 0) {
109 $GLOBALS['config'][$configEntry] += $value;
111 $GLOBALS['config'][$configEntry] = $value;
115 // Checks wether the configuration array is set so the config is loaded
116 function isConfigLoaded () {
118 return ((isset($GLOBALS['config'])) && (is_array($GLOBALS['config'])) && (count($GLOBALS['config']) > 0));
121 // Load configuration and return it as an arry
122 function loadConfiguration ($no = '0') {
123 // Check for cache extension, cache-array and if the requested configuration is in cache
124 if ((isset($GLOBALS['cache_array'])) && (is_array($GLOBALS['cache_array'])) && (isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
125 // Load config from cache
126 //* DEBUG: */ OUTPUT_HTML(gettype($GLOBALS['cache_array']['config'][$no])."<br />");
127 foreach ($GLOBALS['cache_array']['config'][$no] as $key => $value) {
128 setConfigEntry($key, $value);
131 // Count cache hits if exists
132 if ((isConfigEntrySet('cache_hits')) && (EXT_IS_ACTIVE('cache'))) {
133 incrementConfigEntry('cache_hits');
135 } elseif ((!EXT_IS_ACTIVE('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
136 // Load config from DB
137 $result_config = SQL_QUERY_ESC("SELECT * FROM `{!_MYSQL_PREFIX!}_config` WHERE config=%d LIMIT 1",
138 array(bigintval($no)), __FUNCTION__, __LINE__);
140 // Is the config there?
141 if (SQL_NUMROWS($result_config) == 1) {
142 // Get config from database
143 mergeConfig(SQL_FETCHARRAY($result_config));
147 SQL_FREERESULT($result_config);
149 // Remember this config in the array
150 $GLOBALS['cache_array']['config'][$no] = getConfigArray();
154 // Getter for whole $GLOBALS['config'] array
155 function getConfigArray () {
159 // Is the config set?
160 if (isConfigLoaded()) {
162 $return = $GLOBALS['config'];
169 // Updates an old inc/config.php to a inc/cache/config-local.php file
170 function updateOldConfigFile () {
171 // Watch out for these lines and execute them as single command
172 // @TODO Make this all better... :-/
174 "define('SITE_KEY'," => 'SITE_KEY',
175 "define('DEFAULT_LANG'," => 'DEFAULT_LANG',
176 "define('warn_no_pass'," => 'WARN_NO_PASS',
177 "define('WRITE_FOOTER'," => 'WRITE_FOOTER',
178 "define('OUTPUT_MODE'," => 'OUTPUT_MODE',
179 "define('MAIN_TITLE'," => 'MAIN_TITLE',
180 "define('SLOGAN'," => 'SLOGAN',
181 "define('WEBMASTER'," => 'WEBMASTER',
182 "define('mxchange_installed'," => 'MXCHANGE_INSTALLED',
183 "define('admin_registered'," => 'ADMIN_REGISTERED',
184 "define('_MYSQL_PREFIX'," => '_MYSQL_PREFIX',
185 "define('_TABLE_TYPE'," => '_TABLE_TYPE',
186 "define('_DB_TYPE'," => '_DB_TYPE',
187 "define('SMTP_HOSTNAME'," => 'SMTP_HOSTNAME',
188 "define('SMTP_USER'" => 'SMTP_USER',
189 "define('SMTP_PASSWORD'," => 'SMTP_PASSWORD',
190 "define('ENABLE_BACKLINK'," => 'ENABLE_BACKLINK',
193 // Make these lower-case! (damn stupid code...)
194 $lowerCase = array('WARN_NO_PASS', 'MXCHANGE_INSTALLED', 'ADMIN_REGISTERED');
196 // These are still constants...
197 // @TODO Rewrite these all to config entries, if somehow possible
198 $constants = array('MAIN_TITLE', 'SLOGAN', 'WEBMASTER');
200 // Special comments...
202 'WARN_NO_PASS' => 'NULLPASS-WARNING',
203 'MXCHANGE_INSTALLED' => 'INSTALLED',
204 'ADMIN_REGISTERED' => 'ADMIN-SETUP',
205 '_MYSQL_PREFIX' => 'MYSQL-PREFIX',
206 '_TABLE_TYPE' => 'TABLE-TYPE',
207 '_DB_TYPE' => 'DATABASE-TYPE',
208 'ENABLE_BACKLINK' => 'BACKLINK',
209 'host' => 'MYSQL-HOST',
210 'dbase' => 'MYSQL-DBASE',
211 'login' => 'MYSQL-LOGIN',
212 'password' => 'MYSQL-PASSWORD'
215 // Copy template to new file destionation
216 copyFileVerified(constant('PATH') . 'inc/config-local.php.dist', constant('PATH') . 'inc/cache/config-local.php', 0644);
218 // First of all, load the old one!
219 $oldConfig = explode("\n", readFromFile(constant('PATH') . 'inc/config.php'));
221 // Now, analyze every entry
223 foreach ($oldConfig as $line) {
224 // Check all watch lines
225 foreach ($watchLines as $old => $new) {
226 // Is the line found?
227 if ((substr($line, 0, strlen($old)) == $old) && (!isset($done[$old]))) {
229 //* DEBUG: */ OUTPUT_HTML(htmlentities($line) . " - FOUND!<br />");
234 // Setting config entry is new default behaviour!
235 $function = 'setConfigEntry';
237 // Still some old constants left?
238 if (in_array($new, $constants)) {
239 // Then switch over...
240 $function = 'define';
244 $comment = str_replace('_', '-', $new);
246 // Do we have a special comment?
247 if (isset($comments[$new])) {
249 $comment = $comments[$new];
252 // Do we need to make $new lowercase?
254 if (in_array($new, $lowerCase)) {
256 $new = strtolower($new);
259 /// ... and write it to the new config
260 //* DEBUG: */ OUTPUT_HTML('function=' . $function . ',new=' . $new . ',comment=' . $comment . "<br />");
261 changeDataInFile(constant('PATH') . 'inc/cache/config-local.php', $comment, $function . "('" . $oldNew . "', \"", "\");", constant($new), 0);
262 //* DEBUG: */ OUTPUT_HTML("CHANGED!<br />");
270 // By default the old array $MySQL was not found
273 // Analyze every entry again for the MySQL configuration
274 foreach ($oldConfig as $line) {
278 // Is the $MySQL found?
279 if (substr($line, 0, 6) == "\$MySQL") {
282 } elseif ($found === true) {
283 // Now check this row
284 if (substr($line, 0, 2) == ');') {
285 // MySQL array is closed so stop looking for it
287 } elseif (substr($line, 0, 2) == '//') {
293 //* DEBUG: */ OUTPUT_HTML(htmlentities($line) . " - MySQL!<br />");
295 // Split parts so we can check them and prepare them
296 $parts = explode('=>', $line);
297 $key = substr(trim($parts[0]), 1, -1); $value = substr(trim($parts[1]), 1, -2);
299 // We can now save the right part in new config file
300 changeDataInFile(constant('PATH') . 'inc/cache/config-local.php', $comments[$key], " '".$key."' => \"", "\",", $value, 0);
304 // Finally remove old config file
305 removeFile(constant('PATH') . 'inc/config.php');
307 // Redirect to same URL to reload our new config
308 redirectToUrl($_SERVER['REQUEST_URI']);