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 (!defined('__SECURITY')) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
44 var $update_interval = 0;
49 var $cache_pointer = false;
51 var $cache_version = "";
54 function mxchange_cache($interval, $path, $tested) {
55 // Failed is the default
56 $this->ret = "failed";
58 // Remember interval in class
59 $this->update_interval = $interval;
62 $this->cache_path = $path;
64 // Check if path exists
65 if ((is_dir($path)) && (!$tested)) {
66 // Check if we can create a file inside the path
67 @touch($path."dummy.tmp", 'w');
68 if (FILE_READABLE($path."dummy.tmp")) {
69 // Yes, we can do. So let's remove it
70 @unlink($path."dummy.tmp");
72 // Is there a .htaccess file?
73 if (FILE_READABLE($path.".htaccess")) {
74 // Update database that we have tested it
75 UPDATE_CONFIG("cache_tested", 1);
80 // Stop! Set a .htaccess file first
81 $this->ret = "htaccess";
85 // System already tested
90 function cache_file($file, $ignore_ctime=false) {
92 // Construct FQFN (full qualified file name)
93 $inc = $this->cache_path.$file.".cache";
95 // Rember it + filename in class
96 $this->cache_inc = $inc;
98 // Check if file exists
99 $status = (FILE_READABLE($inc) && (is_writeable($inc)));
101 // Yes, it does. So let's get it's last changed date/time
102 $ctime = filectime($inc);
104 // No, it doesn't. Zero date/time
108 // Remember change date/time in class
109 $this->cache_ctime = $ctime;
111 // Is the cache file outdated?
112 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime)) {
113 // Ok, we need an update!
121 function cache_init($array) {
122 // This will destory an existing cache file!
123 if ($this->ret == "done") {
125 if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
126 $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
128 // Begin of cache file
129 fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
131 // Remember file pointer
132 $this->cache_pointer = $fp;
134 // Cannot create file
135 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
139 function add_row ($data) {
142 if (is_resource($this->cache_pointer)) {
143 // Write every array element to cache file
144 foreach ($data as $k => $v) {
145 // Write global cache array for immediate access
146 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
147 if ($k != "ext_name") {
148 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
150 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
152 if (($k == "ext_keep") && ($v == "Y")) {
153 $cacheArray['active_extensions'][$data['ext_name']] = $v;
155 } elseif (is_array($v)) {
156 // Serialize and BASE64-encode the array
157 $v = base64_encode(serialize($v));
160 // Write cache line to file
161 @fwrite($this->cache_pointer, $this->add_raw_row($k, $v));
164 // Cannot create file
165 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
169 function cache_close()
171 // Quit function when no pointer is set
172 if (empty($this->cache_pointer)) return;
173 if ($this->cache_pointer)
175 // Close file add destroy handler
176 @fclose($this->cache_pointer);
179 if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
182 unset($this->cache_pointer);
186 // Cannot create file
187 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
191 function cache_load() {
192 // Is the cache file there?
193 if (FILE_READABLE($this->cache_inc)) {
194 // Prepare temporary array
196 $cache_version = null;
199 $this->cache_data = implode("", file($this->cache_inc));
201 // Execute cache file
202 eval($this->cache_data);
204 if (is_array($data)) {
206 $this->cache_data = $data;
208 // Cache version found?
209 if ((isset($cache_version)) && (is_array($cache_version))) {
210 // Remember it as well...
211 $this->cache_version = $cache_version;
215 return $this->cache_data;
217 // Cache problem detected!
218 $this->cache_destroy();
221 // Cache file not found or not readable
222 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
226 function cache_destroy()
228 if (FILE_READABLE($this->cache_inc))
230 // Remove cache file from system
231 @unlink($this->cache_inc);
232 if (!FILE_READABLE($this->cache_inc))
234 // Close cache automatically (we don't need it anymore!)
235 $this->cache_close();
240 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
246 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
250 function cache_remove($search, $data, $array)
253 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
254 // Load cache into dummy array
255 $dummy = $this->cache_load();
257 // Search for key in array
258 $key = array_search($data, $dummy[$search]);
260 // Key (hopefully) found?
261 foreach ($array as $a) {
262 // So we can remove all elements as requested
263 unset($dummy[$a][$key]);
266 // Flush array to cache file
267 $fp = fopen($this->cache_inc, 'w');
268 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
269 foreach ($dummy as $k => $v) {
271 // Multi line(s) found
273 foreach($v as $k2 => $v2) {
274 // Put every array element in a row...
275 $LINE .= $this->add_raw_row($k, $v2);
279 $LINE = $this->add_raw_row($k, $v);
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_READABLE($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]))) {
305 // Search for key in array
306 $key_found = array_key_exists($search_key, $dummy[$search]);
307 if ($key_found == true) {
309 // Key (hopefully) found?
310 foreach ($dummy as $a => $v) {
311 // So we can update all entries
314 $dummy[$a][$search_key] = $replace;
318 // Flush array to cache file
319 $fp = fopen($this->cache_inc, 'w');
320 fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
321 foreach ($dummy as $k => $v) {
323 // Multi line(s) found
325 foreach($v as $k2 => $v2) {
326 // Put every array element in a row...
327 $LINE .= $this->add_raw_row($k, $v2);
331 $LINE = $this->add_raw_row($k, $v);
343 // Cannot write to cache!
344 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
348 function store_extension_version ($ext_name) {
349 // Valid cache pointer?
350 if (is_resource($this->cache_pointer)) {
351 // Get extension version
352 $ext_ver = GET_EXT_VERSION($ext_name);
354 // Write cache line to file
355 @fwrite($this->cache_pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
357 // Cannot create file
358 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
362 function ext_version_matches ($ext_name) {
363 // Load cache (dummy)
366 // Get extension version
367 $ext_ver = GET_EXT_VERSION($ext_name);
369 //* DEBUG: */ echo __METHOD__.": ext_name={$ext_name},ext_ver={$ext_ver},cache_version={$this->cache_version}<br />\n";
371 return ((isset($this->cache_version[$ext_name])) && ($this->cache_version[$ext_name] == $ext_ver));
374 function add_raw_row ($key, $value) {
378 // String or non-string? ;-)
379 if (is_string($value)) {
381 $line = "\$data['".$key."'][] = \"".$value."\";\n";
384 $line = "\$data['".$key."'][] = ".$value.";\n";
391 function getStatus () {