Migration of stelzi's commit 1022 with some changes so we have a nicer code. See...
[mailer.git] / inc / config-functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 02/28/2009 *
4  * ===============                              Last change: 02/28/2009 *
5  *                                                                      *
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  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
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                  *
22  *                                                                      *
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.                                  *
27  *                                                                      *
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.                         *
32  *                                                                      *
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,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 }
44
45 // Merges $_CONFIG with data in given array
46 function mergeConfig ($newConfig) {
47         global $_CONFIG;
48         $_CONFIG = merge_array($_CONFIG, $newConfig);
49 }
50
51 // Getter for $_CONFIG entries
52 function getConfig ($configEntry) {
53         global $_CONFIG;
54
55         // Default value
56         $value = null;
57
58         // Is the entry there?
59         if (isConfigEntrySet($configEntry)) {
60                 // Then use it
61                 $value = $_CONFIG[$configEntry];
62         } // END - if
63
64         // Return it
65         return $value;
66 }
67
68 // Setter for $_CONFIG entries
69 function setConfigEntry ($configEntry, $value) {
70         global $_CONFIG;
71
72         // Secure the entry name
73         if (function_exists('SQL_ESCAPE')) {
74                 // Use our secure function
75                 $configEntry = SQL_ESCAPE($configEntry);
76         } else {
77                 // Use maybe insecure function
78                 $configEntry = smartAddSlashes($configEntry);
79         }
80
81         // And set it
82         $_CONFIG[$configEntry] = $value;
83 }
84
85 // Checks wether the given config entry is set
86 function isConfigEntrySet ($configEntry) {
87         global $_CONFIG;
88         return (isset($_CONFIG[$configEntry]));
89 }
90
91 // Increment or init with given value or 1 as default the given config entry
92 function incrementConfigEntry ($configEntry, $value=1) {
93         global $_CONFIG;
94
95         // Increment it if set or init it with 1
96         if (getConfig($configEntry) > 0) {
97                 $_CONFIG[$configEntry] += $value;
98         } else {
99                 $_CONFIG[$configEntry] = $value;
100         }
101 }
102
103 // Checks wether the configuration array is set so the config is loaded
104 function isConfigLoaded () {
105         global $_CONFIG;
106
107         // Check all
108         return ((isset($_CONFIG)) && (is_array($_CONFIG)) && (count($_CONFIG) > 0));
109 }
110
111 // Init the config array
112 function initConfig () {
113         global $_CONFIG;
114         debug_report_bug('Reached!');
115
116         // Set a minimum dummy configuration
117         $_CONFIG = array(
118                 'code_length' => 0,
119                 'patch_level' => 0,
120                 'last_update' => time()
121         );
122 }
123
124 // Load configuration and return it as an arry
125 function loadConfiguration ($no = '0') {
126         // Check for cache extension, cache-array and if the requested configuration is in cache
127         if ((isset($GLOBALS['cache_array'])) && (is_array($GLOBALS['cache_array'])) && (isset($GLOBALS['cache_array']['config'][$no])) && (is_array($GLOBALS['cache_array']['config'][$no]))) {
128                 // Load config from cache
129                 //* DEBUG: */ echo gettype($GLOBALS['cache_array']['config'][$no])."<br />\n";
130                 foreach ($GLOBALS['cache_array']['config'][$no] as $key => $value) {
131                         setConfigEntry($key, $value);
132                 } // END - foreach
133
134                 // Count cache hits if exists
135                 if ((isConfigEntrySet('cache_hits')) && (EXT_IS_ACTIVE('cache'))) {
136                         incrementConfigEntry('cache_hits');
137                 } // END - if
138         } elseif ((!EXT_IS_ACTIVE('cache')) || (!isset($GLOBALS['cache_array']['config'][$no]))) {
139                 // Load config from DB
140                 $result_config = SQL_QUERY_ESC("SELECT * FROM `{!_MYSQL_PREFIX!}_config` WHERE config=%d LIMIT 1",
141                         array(bigintval($no)), __FUNCTION__, __LINE__);
142
143                 // Is the config there?
144                 if (SQL_NUMROWS($result_config) == 1) {
145                         // Get config from database
146                         mergeConfig(SQL_FETCHARRAY($result_config));
147                 } // END - if
148
149                 // Free result
150                 SQL_FREERESULT($result_config);
151
152                 // Remember this config in the array
153                 $GLOBALS['cache_array']['config'][$no] = getConfigArray();
154         }
155 }
156
157 // Getter for whole $_CONFIG array
158 function getConfigArray () {
159         global $_CONFIG;
160
161         // Default is null
162         $return = null;
163
164         // Is the config set?
165         if (isConfigLoaded()) {
166                 // Then use it
167                 $return = $_CONFIG;
168         } // END - if
169
170         // Return result
171         return $return;
172 }
173
174 // [EOF]
175 ?>