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;
52 function mxchange_cache($interval, $path, $tested) {
53 // Remember interval in class
54 $this->update_interval=$interval;
57 $this->cache_path=$path;
59 // Check if path exists
60 if ((file_exists($path)) && (is_dir($path)) && (!$tested))
62 // Check if we can create a file inside the path
63 @touch($path."dummy.tmp", 'w');
64 if (file_exists($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_exists($path.".htaccess"))
72 // Update database that we have tested it
73 $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_config SET cache_tested='1' WHERE config='0' LIMIT 1", __FILE__, __LINE__);
81 // Stop! Set a .htaccess file first
82 $this->ret="htaccess";
89 // System already tested
94 // Something goes wrong here!
99 function cache_file($file, $ignore_ctime=false)
102 // Construct FQFN (full qualified file name)
103 $inc = $this->cache_path.$file.".cache";
105 // Rember it + filename in class
106 $this->cache_inc = $inc;
108 // Check if file exists
109 $status = (file_exists($inc) && (is_readable($inc)) && (is_writeable($inc)));
112 // Yes, it does. So let's get it's last changed date/time
113 $ctime = filectime($inc);
117 // No, it doesn't. Zero date/time
121 // Remember change date/time in class
122 $this->cache_ctime = $ctime;
124 // Is the cache file outdated?
125 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime))
127 // Ok, we need an update!
133 function cache_init($array)
135 // This will destory an existing cache file!
136 if ($this->ret == "done")
139 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
140 $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
142 // Begin of cache file
143 fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
145 // Remember file pointer
146 $this->cache_pointer = $fp;
150 // Cannot create file
151 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
155 function add_row($data)
157 if ($this->cache_pointer)
159 // Write every array element to cache file
160 foreach ($data as $k=>$v)
162 @fwrite($this->cache_pointer, "\$CACHE['".$k."'][] = \"".$v."\";\n");
167 // Cannot create file
168 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
172 function cache_close()
174 // Quit function when no pointer is set
175 if (empty($this->cache_pointer)) return;
176 if ($this->cache_pointer)
178 // Close file add destroy handler
179 @fclose($this->cache_pointer);
182 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
185 unset($this->cache_pointer);
189 // Cannot create file
190 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
194 function cache_load()
196 if ((file_exists($this->cache_inc)) && (is_readable($this->cache_inc)))
198 // Prepare temporay array
201 $cache = implode("", file($this->cache_inc));
202 // Execute cache file
204 if (is_array($CACHE))
210 // Cache problem detected!
211 $this->cache_destroy();
216 // Cache file not found or not readable
217 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
221 function cache_destroy()
223 if (file_exists($this->cache_inc))
225 // Remove cache file from system
226 @unlink($this->cache_inc);
227 if (!file_exists($this->cache_inc))
229 // Close cache automatically (we don't need it anymore!)
230 $this->cache_close();
235 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
241 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
245 function cache_remove($search, $data, $array)
248 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
250 // Load cache into dummy array
251 $DUMMY = $this->cache_load();
253 // Search for key in array
254 $key = array_search($data, $DUMMY[$search]);
257 // Key (hopefully) found?
258 foreach ($array as $a)
260 // So we can remove all elements as requested
261 unset($DUMMY[$a][$key]);
264 // Flush array to cache file
265 $fp = fopen($this->cache_inc, 'w');
266 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
267 foreach ($DUMMY as $k=>$v)
271 // Multi line(s) found
273 foreach($v as $k2=>$v2)
275 // Put every array element in a row...
276 $LINE .= "\$CACHE['".$k."'][] = \"".$v2."\";\n";
282 $LINE = "\$CACHE['".$k."'] = \"".$v."\";\n";
295 // Cannot write to cache!
296 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
300 function cache_replace($search, $replace, $search_key, $array)
303 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
305 // Load cache into dummy array
306 $DUMMY = $this->cache_load();
308 // Check if $DUMMY is valid (prevents some errors)
309 if ((is_array($DUMMY)) && (is_array($DUMMY[$search])))
311 // Search for key in array
312 $key_found = array_key_exists($search_key, $DUMMY[$search]);
313 if ($key_found == true)
316 // Key (hopefully) found?
317 foreach ($DUMMY as $a=>$v)
319 // So we can update all entries
323 $DUMMY[$a][$search_key] = $replace;
327 // Flush array to cache file
328 $fp = fopen($this->cache_inc, 'w');
329 fwrite($fp, "\$DUMMY = \"".$ARRAY."\";\n");
330 foreach ($DUMMY as $k=>$v)
334 // Multi line(s) found
336 foreach($v as $k2=>$v2)
338 // Put every array element in a row...
339 $LINE .= "\$CACHE['".$k."'][] = \"".$v2."\";\n";
345 $LINE = "\$CACHE['".$k."'] = \"".$v."\";\n";
359 // Cannot write to cache!
360 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);