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) {
156 if (is_resource($this->cache_pointer)) {
157 // Write every array element to cache file
158 foreach ($data as $k=>$v) {
159 @fwrite($this->cache_pointer, "\$data['".$k."'][] = \"".$v."\";\n");
162 // Cannot create file
163 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
167 function cache_close()
169 // Quit function when no pointer is set
170 if (empty($this->cache_pointer)) return;
171 if ($this->cache_pointer)
173 // Close file add destroy handler
174 @fclose($this->cache_pointer);
177 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
180 unset($this->cache_pointer);
184 // Cannot create file
185 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
189 function cache_load()
191 if ((file_exists($this->cache_inc)) && (is_readable($this->cache_inc)))
193 // Prepare temporary array
197 $cache = implode("", file($this->cache_inc));
199 // Execute cache file
201 if (is_array($data)) {
205 // Cache problem detected!
206 $this->cache_destroy();
211 // Cache file not found or not readable
212 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
216 function cache_destroy()
218 if (file_exists($this->cache_inc))
220 // Remove cache file from system
221 @unlink($this->cache_inc);
222 if (!file_exists($this->cache_inc))
224 // Close cache automatically (we don't need it anymore!)
225 $this->cache_close();
230 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
236 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
240 function cache_remove($search, $data, $array)
243 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
245 // Load cache into dummy array
246 $dummy = $this->cache_load();
248 // Search for key in array
249 $key = array_search($data, $dummy[$search]);
252 // Key (hopefully) found?
253 foreach ($array as $a)
255 // So we can remove all elements as requested
256 unset($dummy[$a][$key]);
259 // Flush array to cache file
260 $fp = fopen($this->cache_inc, 'w');
261 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
262 foreach ($dummy as $k=>$v)
266 // Multi line(s) found
268 foreach($v as $k2=>$v2)
270 // Put every array element in a row...
271 $LINE .= "\$cacheInstance['".$k."'][] = \"".$v2."\";\n";
277 $LINE = "\$cacheInstance['".$k."'] = \"".$v."\";\n";
290 // Cannot write to cache!
291 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
295 function cache_replace($search, $replace, $search_key, $array)
298 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
300 // Load cache into dummy array
301 $dummy = $this->cache_load();
303 // Check if $dummy is valid (prevents some errors)
304 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search])))
306 // Search for key in array
307 $key_found = array_key_exists($search_key, $dummy[$search]);
308 if ($key_found == true)
311 // Key (hopefully) found?
312 foreach ($dummy as $a=>$v)
314 // So we can update all entries
318 $dummy[$a][$search_key] = $replace;
322 // Flush array to cache file
323 $fp = fopen($this->cache_inc, 'w');
324 fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
325 foreach ($dummy as $k=>$v)
329 // Multi line(s) found
331 foreach($v as $k2=>$v2)
333 // Put every array element in a row...
334 $LINE .= "\$cacheInstance['".$k."'][] = \"".$v2."\";\n";
340 $LINE = "\$cacheInstance['".$k."'] = \"".$v."\";\n";
354 // Cannot write to cache!
355 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);