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 UPDATE_CONFIG("cache_tested", 1);
76 // Stop! Set a .htaccess file first
77 $this->ret="htaccess";
82 // System already tested
87 // Something goes wrong here!
92 function cache_file($file, $ignore_ctime=false)
95 // Construct FQFN (full qualified file name)
96 $inc = $this->cache_path.$file.".cache";
98 // Rember it + filename in class
99 $this->cache_inc = $inc;
101 // Check if file exists
102 $status = (file_exists($inc) && (is_readable($inc)) && (is_writeable($inc)));
105 // Yes, it does. So let's get it's last changed date/time
106 $ctime = filectime($inc);
110 // No, it doesn't. Zero date/time
114 // Remember change date/time in class
115 $this->cache_ctime = $ctime;
117 // Is the cache file outdated?
118 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime))
120 // Ok, we need an update!
126 function cache_init($array)
128 // This will destory an existing cache file!
129 if ($this->ret == "done")
132 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
133 $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
135 // Begin of cache file
136 fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
138 // Remember file pointer
139 $this->cache_pointer = $fp;
143 // Cannot create file
144 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
148 function add_row($data) {
149 if (is_resource($this->cache_pointer)) {
150 // Write every array element to cache file
151 foreach ($data as $k=>$v) {
152 @fwrite($this->cache_pointer, "\$data['".$k."'][] = \"".$v."\";\n");
155 // Cannot create file
156 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
160 function cache_close()
162 // Quit function when no pointer is set
163 if (empty($this->cache_pointer)) return;
164 if ($this->cache_pointer)
166 // Close file add destroy handler
167 @fclose($this->cache_pointer);
170 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
173 unset($this->cache_pointer);
177 // Cannot create file
178 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
182 function cache_load()
184 if ((file_exists($this->cache_inc)) && (is_readable($this->cache_inc)))
186 // Prepare temporary array
190 $cache = implode("", file($this->cache_inc));
192 // Execute cache file
194 if (is_array($data)) {
198 // Cache problem detected!
199 $this->cache_destroy();
204 // Cache file not found or not readable
205 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
209 function cache_destroy()
211 if (file_exists($this->cache_inc))
213 // Remove cache file from system
214 @unlink($this->cache_inc);
215 if (!file_exists($this->cache_inc))
217 // Close cache automatically (we don't need it anymore!)
218 $this->cache_close();
223 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
229 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
233 function cache_remove($search, $data, $array)
236 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
238 // Load cache into dummy array
239 $dummy = $this->cache_load();
241 // Search for key in array
242 $key = array_search($data, $dummy[$search]);
245 // Key (hopefully) found?
246 foreach ($array as $a)
248 // So we can remove all elements as requested
249 unset($dummy[$a][$key]);
252 // Flush array to cache file
253 $fp = fopen($this->cache_inc, 'w');
254 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
255 foreach ($dummy as $k=>$v)
259 // Multi line(s) found
261 foreach($v as $k2=>$v2)
263 // Put every array element in a row...
264 $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
270 $LINE = "\$data['".$k."'] = \"".$v."\";\n";
283 // Cannot write to cache!
284 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
288 function cache_replace($search, $replace, $search_key, $array)
291 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
293 // Load cache into dummy array
294 $dummy = $this->cache_load();
296 // Check if $dummy is valid (prevents some errors)
297 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search])))
299 // Search for key in array
300 $key_found = array_key_exists($search_key, $dummy[$search]);
301 if ($key_found == true)
304 // Key (hopefully) found?
305 foreach ($dummy as $a=>$v)
307 // So we can update all entries
311 $dummy[$a][$search_key] = $replace;
315 // Flush array to cache file
316 $fp = fopen($this->cache_inc, 'w');
317 fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
318 foreach ($dummy as $k=>$v)
322 // Multi line(s) found
324 foreach($v as $k2=>$v2)
326 // Put every array element in a row...
327 $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
333 $LINE = "\$data['".$k."'] = \"".$v."\";\n";
347 // Cannot write to cache!
348 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);