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) {
151 if (is_resource($this->cache_pointer)) {
152 // Write every array element to cache file
153 foreach ($data as $k => $v) {
154 // Write global cache array for immediate access
155 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
156 if ($k != "ext_name") {
157 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
159 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
161 if (($k == "ext_keep") && ($v == "Y")) {
162 $cacheArray['active_extensions'][$data['ext_name']] = $v;
166 // Write cache line to file
167 @fwrite($this->cache_pointer, "\$data['".$k."'][] = \"".$v."\";\n");
170 // Cannot create file
171 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
175 function cache_close()
177 // Quit function when no pointer is set
178 if (empty($this->cache_pointer)) return;
179 if ($this->cache_pointer)
181 // Close file add destroy handler
182 @fclose($this->cache_pointer);
185 if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
188 unset($this->cache_pointer);
192 // Cannot create file
193 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
197 function cache_load()
199 if ((file_exists($this->cache_inc)) && (is_readable($this->cache_inc)))
201 // Prepare temporary array
205 $cache = implode("", file($this->cache_inc));
207 // Execute cache file
209 if (is_array($data)) {
213 // Cache problem detected!
214 $this->cache_destroy();
219 // Cache file not found or not readable
220 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
224 function cache_destroy()
226 if (file_exists($this->cache_inc))
228 // Remove cache file from system
229 @unlink($this->cache_inc);
230 if (!file_exists($this->cache_inc))
232 // Close cache automatically (we don't need it anymore!)
233 $this->cache_close();
238 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
244 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
248 function cache_remove($search, $data, $array)
251 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
253 // Load cache into dummy array
254 $dummy = $this->cache_load();
256 // Search for key in array
257 $key = array_search($data, $dummy[$search]);
260 // Key (hopefully) found?
261 foreach ($array as $a)
263 // So we can remove all elements as requested
264 unset($dummy[$a][$key]);
267 // Flush array to cache file
268 $fp = fopen($this->cache_inc, 'w');
269 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
270 foreach ($dummy as $k=>$v)
274 // Multi line(s) found
276 foreach($v as $k2=>$v2)
278 // Put every array element in a row...
279 $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
285 $LINE = "\$data['".$k."'] = \"".$v."\";\n";
298 // Cannot write to cache!
299 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
303 function cache_replace($search, $replace, $search_key, $array)
306 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
308 // Load cache into dummy array
309 $dummy = $this->cache_load();
311 // Check if $dummy is valid (prevents some errors)
312 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search])))
314 // Search for key in array
315 $key_found = array_key_exists($search_key, $dummy[$search]);
316 if ($key_found == true)
319 // Key (hopefully) found?
320 foreach ($dummy as $a=>$v)
322 // So we can update all entries
326 $dummy[$a][$search_key] = $replace;
330 // Flush array to cache file
331 $fp = fopen($this->cache_inc, 'w');
332 fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
333 foreach ($dummy as $k=>$v)
337 // Multi line(s) found
339 foreach($v as $k2=>$v2)
341 // Put every array element in a row...
342 $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
348 $LINE = "\$data['".$k."'] = \"".$v."\";\n";
362 // Cannot write to cache!
363 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);