2 /************************************************************************
3 * MXChange v0.2.1 Start: 10/11/2003 *
4 * =============== Last change: 10/11/2003 *
6 * -------------------------------------------------------------------- *
7 * File : admins_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for the admins extension *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer die admins-Erweiterung *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program; if not, write to the Free Software *
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
32 ************************************************************************/
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
37 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
44 var $update_interval = 0;
49 var $cache_pointer = false;
51 var $cache_version = "";
54 function mxchange_cache($interval, $path, $tested) {
55 // Remember interval in class
56 $this->update_interval=$interval;
59 $this->cache_path=$path;
61 // Check if path exists
62 if ((is_dir($path)) && (!$tested)) {
63 // Check if we can create a file inside the path
64 @touch($path."dummy.tmp", 'w');
65 if (FILE_READABLE($path."dummy.tmp")) {
66 // Yes, we can do. So let's remove it
67 @unlink($path."dummy.tmp");
69 // Is there a .htaccess file?
70 if (FILE_READABLE($path.".htaccess")) {
71 // Update database that we have tested it
72 UPDATE_CONFIG("cache_tested", 1);
77 // Stop! Set a .htaccess file first
78 $this->ret="htaccess";
83 // System already tested
88 // Something goes wrong here!
93 function cache_file($file, $ignore_ctime=false) {
95 // Construct FQFN (full qualified file name)
96 $inc = $this->cache_path.$file.".cache";
98 // Rember it + filename in class
99 $this->cache_inc = $inc;
101 // Check if file exists
102 $status = (FILE_READABLE($inc) && (is_writeable($inc)));
104 // Yes, it does. So let's get it's last changed date/time
105 $ctime = filectime($inc);
107 // No, it doesn't. Zero date/time
111 // Remember change date/time in class
112 $this->cache_ctime = $ctime;
114 // Is the cache file outdated?
115 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime)) {
116 // Ok, we need an update!
124 function cache_init($array) {
125 // This will destory an existing cache file!
126 if ($this->ret == "done") {
128 if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
129 $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
131 // Begin of cache file
132 fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
134 // Remember file pointer
135 $this->cache_pointer = $fp;
137 // Cannot create file
138 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
142 function add_row ($data) {
145 if (is_resource($this->cache_pointer)) {
146 // Write every array element to cache file
147 foreach ($data as $k => $v) {
148 // Write global cache array for immediate access
149 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
150 if ($k != "ext_name") {
151 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
153 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
155 if (($k == "ext_keep") && ($v == "Y")) {
156 $cacheArray['active_extensions'][$data['ext_name']] = $v;
160 // Write cache line to file
161 @fwrite($this->cache_pointer, $this->add_raw_row($k, $v));
164 // Cannot create file
165 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
169 function cache_close()
171 // Quit function when no pointer is set
172 if (empty($this->cache_pointer)) return;
173 if ($this->cache_pointer)
175 // Close file add destroy handler
176 @fclose($this->cache_pointer);
179 if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
182 unset($this->cache_pointer);
186 // Cannot create file
187 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
191 function cache_load() {
192 // Is the cache file there?
193 if (FILE_READABLE($this->cache_inc)) {
194 // Prepare temporary array
198 $this->cache_data = implode("", file($this->cache_inc));
200 // Execute cache file
201 eval($this->cache_data);
203 if (is_array($data)) {
205 $this->cache_data = $data;
207 // Cache version found?
208 if (isset($cache_version)) {
209 // Remember it as well...
210 $this->cache_version = $cache_version;
214 return $this->cache_data;
216 // Cache problem detected!
217 $this->cache_destroy();
220 // Cache file not found or not readable
221 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
225 function cache_destroy()
227 if (FILE_READABLE($this->cache_inc))
229 // Remove cache file from system
230 @unlink($this->cache_inc);
231 if (!FILE_READABLE($this->cache_inc))
233 // Close cache automatically (we don't need it anymore!)
234 $this->cache_close();
239 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
245 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
249 function cache_remove($search, $data, $array)
252 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
253 // Load cache into dummy array
254 $dummy = $this->cache_load();
256 // Search for key in array
257 $key = array_search($data, $dummy[$search]);
259 // Key (hopefully) found?
260 foreach ($array as $a) {
261 // So we can remove all elements as requested
262 unset($dummy[$a][$key]);
265 // Flush array to cache file
266 $fp = fopen($this->cache_inc, 'w');
267 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
268 foreach ($dummy as $k => $v) {
270 // Multi line(s) found
272 foreach($v as $k2 => $v2) {
273 // Put every array element in a row...
274 $LINE .= $this->add_raw_row($k, $v2);
278 $LINE = $this->add_raw_row($k, $v);
289 // Cannot write to cache!
290 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
294 function cache_replace($search, $replace, $search_key, $array)
297 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc)))
299 // Load cache into dummy array
300 $dummy = $this->cache_load();
302 // Check if $dummy is valid (prevents some errors)
303 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
304 // Search for key in array
305 $key_found = array_key_exists($search_key, $dummy[$search]);
306 if ($key_found == true) {
308 // Key (hopefully) found?
309 foreach ($dummy as $a => $v) {
310 // So we can update all entries
313 $dummy[$a][$search_key] = $replace;
317 // Flush array to cache file
318 $fp = fopen($this->cache_inc, 'w');
319 fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
320 foreach ($dummy as $k => $v) {
322 // Multi line(s) found
324 foreach($v as $k2 => $v2) {
325 // Put every array element in a row...
326 $LINE .= $this->add_raw_row($k, $v2);
330 $LINE = $this->add_raw_row($k, $v);
342 // Cannot write to cache!
343 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
347 function store_extension_version ($ext_name) {
348 // Valid cache pointer?
349 if (is_resource($this->cache_pointer)) {
350 // Get extension version
351 $ext_ver = GET_EXT_VERSION($ext_name);
353 // Write cache line to file
354 @fwrite($this->cache_pointer, "\$cache_version = \"".$ext_ver."\";\n");
356 // Cannot create file
357 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
361 function ext_version_matches ($ext_name) {
362 // Load cache (dummy)
365 // Get extension version
366 $ext_ver = GET_EXT_VERSION($ext_name);
368 //* DEBUG: */ echo __METHOD__.": ext_name={$ext_name},ext_ver={$ext_ver},cache_version={$this->cache_version}<br />\n";
370 return ($ext_ver == $this->cache_version);
373 function add_raw_row ($key, $value) {
377 // String or non-string? ;-)
378 if (is_string($value)) {
380 $line = "\$data['".$key."'][] = \"".$value."\";\n";
383 $line = "\$data['".$key."'][] = ".$value.";\n";