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)
54 // Remember interval in class
55 $this->update_interval=$interval;
58 $this->cache_path=$path;
60 // Check if path exists
61 if ((file_exists($path)) && (is_dir($path)) && (!$tested))
63 // Check if we can create a file inside the path
64 @touch($path."dummy.tmp", 'w');
65 if (file_exists($path."dummy.tmp"))
67 // Yes, we can do. So let's remove it
68 unlink($path."dummy.tmp");
70 // Is there a .htaccess file?
71 if (file_exists($path.".htaccess"))
73 // Update database that we have tested it
74 $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_config SET cache_tested='1' WHERE config='0' LIMIT 1", __FILE__, __LINE__);
82 // Stop! Set a .htaccess file first
83 $this->ret="htaccess";
90 // System already tested
95 // Something goes wrong here!
100 function cache_file($file, $ignore_ctime=false)
103 // Construct FQFN (full qualified file name)
104 $inc = $this->cache_path.$file.".cache";
106 // Rember it + filename in class
107 $this->cache_inc = $inc;
109 // Check if file exists
110 $status = (file_exists($inc) && (is_readable($inc)) && (is_writeable($inc)));
113 // Yes, it does. So let's get it's last changed date/time
114 $ctime = filectime($inc);
118 // No, it doesn't. Zero date/time
122 // Remember change date/time in class
123 $this->cache_ctime = $ctime;
125 // Is the cache file outdated?
126 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime))
128 // Ok, we need an update!
134 function cache_init($array)
136 // This will destory an existing cache file!
137 if ($this->ret == "done")
140 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
141 $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
143 // Begin of cache file
144 fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
146 // Remember file pointer
147 $this->cache_pointer = $fp;
151 // Cannot create file
152 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
156 function add_row($data)
158 if ($this->cache_pointer)
160 // Write every array element to cache file
161 foreach ($data as $k=>$v)
163 @fwrite($this->cache_pointer, "\$CACHE['".$k."'][] = \"".$v."\";\n");
168 // Cannot create file
169 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
173 function cache_close()
175 // Quit function when no pointer is set
176 if (empty($this->cache_pointer)) return;
177 if ($this->cache_pointer)
179 // Close file add destroy handler
180 @fclose($this->cache_pointer);
183 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
186 unset($this->cache_pointer);
190 // Cannot create file
191 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
195 function cache_load()
197 if ((file_exists($this->cache_inc)) && (is_readable($this->cache_inc)))
199 // Prepare temporay array
202 $cache = implode("", file($this->cache_inc));
203 // Execute cache file
205 if (is_array($CACHE))
211 // Cache problem detected!
212 $this->cache_destroy();
217 // Cache file not found or not readable
218 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
222 function cache_destroy()
224 if (file_exists($this->cache_inc))
226 // Remove cache file from system
227 @unlink($this->cache_inc);
228 if (!file_exists($this->cache_inc))
230 // Close cache automatically (we don't need it anymore!)
231 $this->cache_close();
236 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
242 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
246 function cache_remove($search, $data, $array)
249 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
251 // Load cache into dummy array
252 $DUMMY = $this->cache_load();
254 // Search for key in array
255 $key = array_search($data, $DUMMY[$search]);
258 // Key (hopefully) found?
259 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)
272 // Multi line(s) found
274 foreach($v as $k2=>$v2)
276 // Put every array element in a row...
277 $LINE .= "\$CACHE['".$k."'][] = \"".$v2."\";\n";
283 $LINE = "\$CACHE['".$k."'] = \"".$v."\";\n";
296 // Cannot write to cache!
297 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
301 function cache_replace($search, $replace, $search_key, $array)
304 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
306 // Load cache into dummy array
307 $DUMMY = $this->cache_load();
309 // Check if $DUMMY is valid (prevents some errors)
310 if ((is_array($DUMMY)) && (is_array($DUMMY[$search])))
312 // Search for key in array
313 $key_found = array_key_exists($search_key, $DUMMY[$search]);
314 if ($key_found == true)
317 // Key (hopefully) found?
318 foreach ($DUMMY as $a=>$v)
320 // So we can update all entries
324 $DUMMY[$a][$search_key] = $replace;
328 // Flush array to cache file
329 $fp = fopen($this->cache_inc, 'w');
330 fwrite($fp, "\$DUMMY = \"".$ARRAY."\";\n");
331 foreach ($DUMMY as $k=>$v)
335 // Multi line(s) found
337 foreach($v as $k2=>$v2)
339 // Put every array element in a row...
340 $LINE .= "\$CACHE['".$k."'][] = \"".$v2."\";\n";
346 $LINE = "\$CACHE['".$k."'] = \"".$v."\";\n";
360 // Cannot write to cache!
361 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);