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 ((is_dir($path)) && (!$tested)) {
61 // Check if we can create a file inside the path
62 @touch($path."dummy.tmp", 'w');
63 if (FILE_READABLE($path."dummy.tmp")) {
64 // Yes, we can do. So let's remove it
65 @unlink($path."dummy.tmp");
67 // Is there a .htaccess file?
68 if (FILE_READABLE($path.".htaccess")) {
69 // Update database that we have tested it
70 UPDATE_CONFIG("cache_tested", 1);
75 // Stop! Set a .htaccess file first
76 $this->ret="htaccess";
81 // System already tested
86 // Something goes wrong here!
91 function cache_file($file, $ignore_ctime=false) {
93 // Construct FQFN (full qualified file name)
94 $inc = $this->cache_path.$file.".cache";
96 // Rember it + filename in class
97 $this->cache_inc = $inc;
99 // Check if file exists
100 $status = (FILE_READABLE($inc) && (is_writeable($inc)));
102 // Yes, it does. So let's get it's last changed date/time
103 $ctime = filectime($inc);
105 // No, it doesn't. Zero date/time
109 // Remember change date/time in class
110 $this->cache_ctime = $ctime;
112 // Is the cache file outdated?
113 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime)) {
114 // Ok, we need an update!
122 function cache_init($array) {
123 // This will destory an existing cache file!
124 if ($this->ret == "done") {
126 if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
127 $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
129 // Begin of cache file
130 fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
132 // Remember file pointer
133 $this->cache_pointer = $fp;
135 // Cannot create file
136 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
140 function add_row ($data) {
143 if (is_resource($this->cache_pointer)) {
144 // Write every array element to cache file
145 foreach ($data as $k => $v) {
146 // Write global cache array for immediate access
147 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
148 if ($k != "ext_name") {
149 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
151 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
153 if (($k == "ext_keep") && ($v == "Y")) {
154 $cacheArray['active_extensions'][$data['ext_name']] = $v;
158 // Write cache line to file
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_READABLE($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_READABLE($this->cache_inc)) {
192 // Prepare temporary array
196 $cache = implode("", file($this->cache_inc));
198 // Execute cache file
200 if (is_array($data)) {
204 // Cache problem detected!
205 $this->cache_destroy();
210 // Cache file not found or not readable
211 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
215 function cache_destroy()
217 if (FILE_READABLE($this->cache_inc))
219 // Remove cache file from system
220 @unlink($this->cache_inc);
221 if (!FILE_READABLE($this->cache_inc))
223 // Close cache automatically (we don't need it anymore!)
224 $this->cache_close();
229 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
235 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
239 function cache_remove($search, $data, $array)
242 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc)))
244 // Load cache into dummy array
245 $dummy = $this->cache_load();
247 // Search for key in array
248 $key = array_search($data, $dummy[$search]);
251 // Key (hopefully) found?
252 foreach ($array as $a)
254 // So we can remove all elements as requested
255 unset($dummy[$a][$key]);
258 // Flush array to cache file
259 $fp = fopen($this->cache_inc, 'w');
260 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
261 foreach ($dummy as $k=>$v)
265 // Multi line(s) found
267 foreach($v as $k2=>$v2)
269 // Put every array element in a row...
270 $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
276 $LINE = "\$data['".$k."'] = \"".$v."\";\n";
289 // Cannot write to cache!
290 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
294 function cache_replace($search, $replace, $search_key, $array)
297 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc)))
299 // Load cache into dummy array
300 $dummy = $this->cache_load();
302 // Check if $dummy is valid (prevents some errors)
303 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search])))
305 // Search for key in array
306 $key_found = array_key_exists($search_key, $dummy[$search]);
307 if ($key_found == true)
310 // Key (hopefully) found?
311 foreach ($dummy as $a=>$v)
313 // So we can update all entries
317 $dummy[$a][$search_key] = $replace;
321 // Flush array to cache file
322 $fp = fopen($this->cache_inc, 'w');
323 fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
324 foreach ($dummy as $k=>$v)
328 // Multi line(s) found
330 foreach($v as $k2=>$v2)
332 // Put every array element in a row...
333 $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
339 $LINE = "\$data['".$k."'] = \"".$v."\";\n";
353 // Cannot write to cache!
354 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);