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")) {
65 // Yes, we can do. So let's remove it
66 @unlink($path."dummy.tmp");
68 // Is there a .htaccess file?
69 if (file_exists($path.".htaccess")) {
70 // Update database that we have tested it
71 $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_config SET cache_tested='1' WHERE config=0 LIMIT 1", __FILE__, __LINE__);
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)
96 // Construct FQFN (full qualified file name)
97 $inc = $this->cache_path.$file.".cache";
99 // Rember it + filename in class
100 $this->cache_inc = $inc;
102 // Check if file exists
103 $status = (file_exists($inc) && (is_readable($inc)) && (is_writeable($inc)));
106 // Yes, it does. So let's get it's last changed date/time
107 $ctime = filectime($inc);
111 // No, it doesn't. Zero date/time
115 // Remember change date/time in class
116 $this->cache_ctime = $ctime;
118 // Is the cache file outdated?
119 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime))
121 // Ok, we need an update!
127 function cache_init($array)
129 // This will destory an existing cache file!
130 if ($this->ret == "done")
133 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
134 $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
136 // Begin of cache file
137 fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
139 // Remember file pointer
140 $this->cache_pointer = $fp;
144 // Cannot create file
145 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
149 function add_row($data) {
150 if (is_resource($this->cache_pointer)) {
151 // Write every array element to cache file
152 foreach ($data as $k=>$v) {
153 @fwrite($this->cache_pointer, "\$data['".$k."'][] = \"".$v."\";\n");
156 // Cannot create file
157 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
161 function cache_close()
163 // Quit function when no pointer is set
164 if (empty($this->cache_pointer)) return;
165 if ($this->cache_pointer)
167 // Close file add destroy handler
168 @fclose($this->cache_pointer);
171 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
174 unset($this->cache_pointer);
178 // Cannot create file
179 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
183 function cache_load()
185 if ((file_exists($this->cache_inc)) && (is_readable($this->cache_inc)))
187 // Prepare temporary array
191 $cache = implode("", file($this->cache_inc));
193 // Execute cache file
195 if (is_array($data)) {
199 // Cache problem detected!
200 $this->cache_destroy();
205 // Cache file not found or not readable
206 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
210 function cache_destroy()
212 if (file_exists($this->cache_inc))
214 // Remove cache file from system
215 @unlink($this->cache_inc);
216 if (!file_exists($this->cache_inc))
218 // Close cache automatically (we don't need it anymore!)
219 $this->cache_close();
224 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
230 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
234 function cache_remove($search, $data, $array)
237 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
239 // Load cache into dummy array
240 $dummy = $this->cache_load();
242 // Search for key in array
243 $key = array_search($data, $dummy[$search]);
246 // Key (hopefully) found?
247 foreach ($array as $a)
249 // So we can remove all elements as requested
250 unset($dummy[$a][$key]);
253 // Flush array to cache file
254 $fp = fopen($this->cache_inc, 'w');
255 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
256 foreach ($dummy as $k=>$v)
260 // Multi line(s) found
262 foreach($v as $k2=>$v2)
264 // Put every array element in a row...
265 $LINE .= "\$cacheInstance['".$k."'][] = \"".$v2."\";\n";
271 $LINE = "\$cacheInstance['".$k."'] = \"".$v."\";\n";
284 // Cannot write to cache!
285 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
289 function cache_replace($search, $replace, $search_key, $array)
292 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
294 // Load cache into dummy array
295 $dummy = $this->cache_load();
297 // Check if $dummy is valid (prevents some errors)
298 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search])))
300 // Search for key in array
301 $key_found = array_key_exists($search_key, $dummy[$search]);
302 if ($key_found == true)
305 // Key (hopefully) found?
306 foreach ($dummy as $a=>$v)
308 // So we can update all entries
312 $dummy[$a][$search_key] = $replace;
316 // Flush array to cache file
317 $fp = fopen($this->cache_inc, 'w');
318 fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
319 foreach ($dummy as $k=>$v)
323 // Multi line(s) found
325 foreach($v as $k2=>$v2)
327 // Put every array element in a row...
328 $LINE .= "\$cacheInstance['".$k."'][] = \"".$v2."\";\n";
334 $LINE = "\$cacheInstance['".$k."'] = \"".$v."\";\n";
348 // Cannot write to cache!
349 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);