2 /************************************************************************
\r
3 * MXChange v0.2.1 Start: 10/11/2003 *
\r
4 * =============== Last change: 10/11/2003 *
\r
6 * -------------------------------------------------------------------- *
\r
7 * File : admins_functions.php *
\r
8 * -------------------------------------------------------------------- *
\r
9 * Short description : Functions for the admins extension *
\r
10 * -------------------------------------------------------------------- *
\r
11 * Kurzbeschreibung : Funktionen fuer die admins-Erweiterung *
\r
12 * -------------------------------------------------------------------- *
\r
14 * -------------------------------------------------------------------- *
\r
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
\r
16 * For more information visit: http://www.mxchange.org *
\r
18 * This program is free software; you can redistribute it and/or modify *
\r
19 * it under the terms of the GNU General Public License as published by *
\r
20 * the Free Software Foundation; either version 2 of the License, or *
\r
21 * (at your option) any later version. *
\r
23 * This program is distributed in the hope that it will be useful, *
\r
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
26 * GNU General Public License for more details. *
\r
28 * You should have received a copy of the GNU General Public License *
\r
29 * along with this program; if not, write to the Free Software *
\r
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
\r
31 * MA 02110-1301 USA *
\r
32 ************************************************************************/
\r
34 // Some security stuff...
\r
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
\r
37 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";
\r
41 class mxchange_cache
\r
44 var $update_interval = 0;
\r
46 var $cache_path = "";
\r
47 var $cache_inc = "";
\r
48 var $cache_ctime = 0;
\r
49 var $cache_pointer = false;
\r
52 function mxchange_cache($interval, $path, $tested)
\r
54 // Remember interval in class
\r
55 $this->update_interval=$interval;
\r
58 $this->cache_path=$path;
\r
60 // Check if path exists
\r
61 if ((file_exists($path)) && (is_dir($path)) && (!$tested))
\r
63 // Check if we can create a file inside the path
\r
64 @touch($path."dummy.tmp", 'w');
\r
65 if (file_exists($path."dummy.tmp"))
\r
67 // Yes, we can do. So let's remove it
\r
68 unlink($path."dummy.tmp");
\r
70 // Is there a .htaccess file?
\r
71 if (file_exists($path.".htaccess"))
\r
73 // Update database that we have tested it
\r
74 $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_config SET cache_tested='1' WHERE config='0' LIMIT 1", __FILE__, __LINE__);
\r
82 // Stop! Set a .htaccess file first
\r
83 $this->ret="htaccess";
\r
90 // System already tested
\r
95 // Something goes wrong here!
\r
96 $this->ret="failed";
\r
100 function cache_file($file, $ignore_ctime=false)
\r
103 // Construct FQFN (full qualified file name)
\r
104 $inc = $this->cache_path.$file.".cache";
\r
106 // Rember it + filename in class
\r
107 $this->cache_inc = $inc;
\r
109 // Check if file exists
\r
110 $status = (file_exists($inc) && (is_readable($inc)) && (is_writeable($inc)));
\r
113 // Yes, it does. So let's get it's last changed date/time
\r
114 $ctime = filectime($inc);
\r
118 // No, it doesn't. Zero date/time
\r
122 // Remember change date/time in class
\r
123 $this->cache_ctime = $ctime;
\r
125 // Is the cache file outdated?
\r
126 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime))
\r
128 // Ok, we need an update!
\r
134 function cache_init($array)
\r
136 // This will destory an existing cache file!
\r
137 if ($this->ret == "done")
\r
140 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
\r
141 $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
\r
143 // Begin of cache file
\r
144 fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
\r
146 // Remember file pointer
\r
147 $this->cache_pointer = $fp;
\r
151 // Cannot create file
\r
152 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
\r
156 function add_row($data)
\r
158 if ($this->cache_pointer)
\r
160 // Write every array element to cache file
\r
161 foreach ($data as $k=>$v)
\r
163 @fwrite($this->cache_pointer, "\$CACHE['".$k."'][] = \"".$v."\";\n");
\r
168 // Cannot create file
\r
169 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
\r
173 function cache_close()
\r
175 // Quit function when no pointer is set
\r
176 if (empty($this->cache_pointer)) return;
\r
177 if ($this->cache_pointer)
\r
179 // Close file add destroy handler
\r
180 @fclose($this->cache_pointer);
\r
183 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
\r
186 unset($this->cache_pointer);
\r
190 // Cannot create file
\r
191 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
\r
195 function cache_load()
\r
197 if ((file_exists($this->cache_inc)) && (is_readable($this->cache_inc)))
\r
199 // Prepare temporay array
\r
202 $cache = implode("", file($this->cache_inc));
\r
203 // Execute cache file
\r
205 if (is_array($CACHE))
\r
211 // Cache problem detected!
\r
212 $this->cache_destroy();
\r
217 // Cache file not found or not readable
\r
218 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
\r
222 function cache_destroy()
\r
224 if (file_exists($this->cache_inc))
\r
226 // Remove cache file from system
\r
227 @unlink($this->cache_inc);
\r
228 if (!file_exists($this->cache_inc))
\r
230 // Close cache automatically (we don't need it anymore!)
\r
231 $this->cache_close();
\r
236 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
\r
242 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
\r
246 function cache_remove($search, $data, $array)
\r
249 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
\r
251 // Load cache into dummy array
\r
252 $DUMMY = $this->cache_load();
\r
254 // Search for key in array
\r
255 $key = array_search($data, $DUMMY[$search]);
\r
258 // Key (hopefully) found?
\r
259 foreach ($array as $a)
\r
261 // So we can remove all elements as requested
\r
262 unset($DUMMY[$a][$key]);
\r
265 // Flush array to cache file
\r
266 $fp = fopen($this->cache_inc, 'w');
\r
267 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
\r
268 foreach ($DUMMY as $k=>$v)
\r
272 // Multi line(s) found
\r
274 foreach($v as $k2=>$v2)
\r
276 // Put every array element in a row...
\r
277 $LINE .= "\$CACHE['".$k."'][] = \"".$v2."\";\n";
\r
282 // Single line found
\r
283 $LINE = "\$CACHE['".$k."'] = \"".$v."\";\n";
\r
287 fwrite($fp, $LINE);
\r
290 // Close cache file
\r
296 // Cannot write to cache!
\r
297 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
\r
301 function cache_replace($search, $replace, $search_key, $array)
\r
304 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
\r
306 // Load cache into dummy array
\r
307 $DUMMY = $this->cache_load();
\r
309 // Check if $DUMMY is valid (prevents some errors)
\r
310 if ((is_array($DUMMY)) && (is_array($DUMMY[$search])))
\r
312 // Search for key in array
\r
313 $key_found = array_key_exists($search_key, $DUMMY[$search]);
\r
314 if ($key_found == true)
\r
316 $key = $search_key;
\r
317 // Key (hopefully) found?
\r
318 foreach ($DUMMY as $a=>$v)
\r
320 // So we can update all entries
\r
324 $DUMMY[$a][$search_key] = $replace;
\r
328 // Flush array to cache file
\r
329 $fp = fopen($this->cache_inc, 'w');
\r
330 fwrite($fp, "\$DUMMY = \"".$ARRAY."\";\n");
\r
331 foreach ($DUMMY as $k=>$v)
\r
335 // Multi line(s) found
\r
337 foreach($v as $k2=>$v2)
\r
339 // Put every array element in a row...
\r
340 $LINE .= "\$CACHE['".$k."'][] = \"".$v2."\";\n";
\r
345 // Single line found
\r
346 $LINE = "\$CACHE['".$k."'] = \"".$v."\";\n";
\r
350 fwrite($fp, $LINE);
\r
353 // Close cache file
\r
360 // Cannot write to cache!
\r
361 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
\r