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";
41 class mxchange_cache {
46 var $cache_pointer = false;
48 var $cache_version = "";
52 function mxchange_cache($interval, $path, $tested) {
53 // Failed is the default
54 $this->ret = "failed";
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";
80 // System already tested
85 // Checks validity of cache file and if content is given
86 function cache_file ($file, $forceContent = false) {
87 // Remember cache file
88 $this->cache_file = $file;
90 // Construct FQFN (full qualified file name)
91 $inc = $this->cache_path.$file.".cache";
93 // Rember it + filename in class
94 $this->cache_inc = $inc;
96 // Check if file exists and if version matches
97 $status = (FILE_READABLE($inc) && (is_writeable($inc)) && ($this->ext_version_matches("cache")));
103 function cache_init($array) {
104 // This will destory an existing cache file!
105 if ($this->ret == "done") {
107 if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);
108 $this->cache_pointer = fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
110 // Begin of cache file
111 fwrite($this->cache_pointer, "<?php\n\$ARRAY = \"".$array."\";\n\n");
113 // Add default depency
114 $this->store_extension_version("cache");
116 // Cannot create file
117 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
121 function add_row ($data) {
124 if (is_resource($this->cache_pointer)) {
125 // Write every array element to cache file
126 foreach ($data as $k => $v) {
127 // Write global cache array for immediate access
128 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
129 if ($k != "ext_name") {
130 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
132 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
134 if (($k == "ext_keep") && ($v == "Y")) {
135 $cacheArray['active_extensions'][$data['ext_name']] = $v;
137 } elseif (is_array($v)) {
138 // Serialize and BASE64-encode the array
139 $v = base64_encode(serialize($v));
142 // Write cache line to file
143 fwrite($this->cache_pointer, $this->add_raw_row($k, $v));
146 // Cannot create file
147 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
151 function cache_close() {
152 // Quit function when no pointer is set
153 if (empty($this->cache_pointer)) return;
154 if (is_resource($this->cache_pointer)) {
156 fwrite($this->cache_pointer, "?>\n");
158 // Close file add destroy handler
159 fclose($this->cache_pointer);
162 if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);
165 unset($this->cache_pointer);
167 // Cannot create file
168 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
172 function cache_load() {
173 // Is the cache already loaded?
174 if (isset($this->cache_data[$this->cache_file])) {
175 // Return it's content!
176 return $this->cache_data[$this->cache_file];
179 // Is the cache file there?
180 if (FILE_READABLE($this->cache_inc)) {
181 // Prepare temporary array
183 $cache_version = null;
186 require_once($this->cache_inc);
188 // Is there an array?
189 if (is_array($data)) {
191 $this->cache_data[$this->cache_file] = $data;
193 // Cache version found?
194 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
195 // Remember it as well...
196 $this->cache_version[$this->cache_file] = $cache_version;
198 // Invalid cache so destroy it
199 $this->cache_destroy();
202 $this->cache_data[$this->cache_file] = array();
206 return $this->cache_data[$this->cache_file];
208 // Cache problem detected!
209 $this->cache_destroy();
212 // Cache file not found or not readable
213 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
217 // Destroy an existing cache file
218 function cache_destroy() {
219 // Is the cache file there?
220 if (FILE_READABLE($this->cache_inc)) {
222 $this->cache_close();
224 // Remove cache file from system
225 unlink($this->cache_inc);
227 // Is the file there?
228 if (!FILE_READABLE($this->cache_inc)) {
229 // The cache does no longer exist so kill the content
230 unset($this->cache_data[$this->cache_file]);
233 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
237 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
241 function cache_remove($search, $data, $array) {
243 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]);
250 // Key (hopefully) found?
251 foreach ($array as $a) {
252 // So we can remove all elements as requested
253 unset($dummy[$a][$key]);
256 // Flush array to cache file
257 $this->cache_init($ARRAY);
260 $this->cache_write_array($dummy);
263 $this->cache_close();
266 // Cannot write to cache!
267 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
271 function cache_write_array ($array) {
272 if (is_resource($this->cache_pointer)) {
273 foreach ($array as $k => $v) {
275 // Multi line(s) found
277 foreach($v as $k2 => $v2) {
278 // Put every array element in a row...
279 $LINE .= $this->add_raw_row($k, $v2);
283 $LINE = $this->add_raw_row($k, $v);
287 fwrite($this->cache_pointer, $LINE);
290 // Cannot write array!
291 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
295 function cache_replace($search, $replace, $search_key, $array) {
297 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
298 // Load cache into dummy array
299 $dummy = $this->cache_load();
301 // Check if $dummy is valid (prevents some errors)
302 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
303 // Search for key in array
304 $key_found = array_key_exists($search_key, $dummy[$search]);
305 if ($key_found == true) {
307 // Key (hopefully) found?
308 foreach ($dummy as $a => $v) {
309 // So we can update all entries
312 $dummy[$a][$search_key] = $replace;
316 // Flush array to cache file
317 $this->cache_init($ARRAY);
320 $this->cache_write_array($dummy);
323 $this->cache_close();
327 // Cannot write to cache!
328 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
332 function store_extension_version ($ext_name) {
333 // Valid cache pointer?
334 if (is_resource($this->cache_pointer)) {
335 // Get extension version
336 $ext_ver = GET_EXT_VERSION($ext_name);
338 // Write cache line to file
339 fwrite($this->cache_pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
341 // Cannot create file
342 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
346 function ext_version_matches ($ext_name) {
347 // Load cache (dummy)
350 // Get extension version
351 $ext_ver = GET_EXT_VERSION($ext_name);
354 return ((isset($this->cache_version[$this->cache_file][$ext_name])) && ($this->cache_version[$this->cache_file][$ext_name] == $ext_ver));
357 function add_raw_row ($key, $value) {
361 // String or non-string? ;-)
362 if ((is_string($value)) || (is_null($value))) {
364 $line = "\$data['".$key."'][] = \"".$value."\";\n";
367 $line = "\$data['".$key."'][] = ".$value.";\n";
374 function getStatus () {