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 = "";
55 function mxchange_cache($interval, $path, $tested) {
56 // Failed is the default
57 $this->ret = "failed";
59 // Remember interval in class
60 $this->update_interval = $interval;
63 $this->cache_path = $path;
65 // Check if path exists
66 if ((is_dir($path)) && (!$tested)) {
67 // Check if we can create a file inside the path
68 touch($path."dummy.tmp", 'w');
69 if (FILE_READABLE($path."dummy.tmp")) {
70 // Yes, we can do. So let's remove it
71 unlink($path."dummy.tmp");
73 // Is there a .htaccess file?
74 if (FILE_READABLE($path.".htaccess")) {
75 // Update database that we have tested it
76 UPDATE_CONFIG("cache_tested", 1);
81 // Stop! Set a .htaccess file first
82 $this->ret = "htaccess";
86 // System already tested
91 // Checks validity of cache file and if content is given
92 function cache_file ($file, $forceContent = false) {
93 // Remember cache file
94 $this->cache_file = $file;
96 // Construct FQFN (full qualified file name)
97 $inc = $this->cache_path.$file.".cache";
99 // Rember it + filename in class
100 $this->cache_inc = $inc;
102 // Check if file exists
103 $status = (FILE_READABLE($inc) && (is_writeable($inc)));
105 // Yes, it does. So let's get it's last changed date/time
106 $ctime = filectime($inc);
108 // No, it doesn't. Zero date/time
112 // Remember change date/time in class
113 $this->cache_ctime = $ctime;
115 // Is the cache file outdated?
116 if (($status) && ((time() - $ctime) >= $this->update_interval)) {
117 // Ok, we need an update!
120 // Check on 'cache' extension
121 $status = $this->ext_version_matches("cache");
128 function cache_init($array) {
129 // This will destory an existing cache file!
130 if ($this->ret == "done") {
132 if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);
133 $this->cache_pointer = fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
135 // Begin of cache file
136 fwrite($this->cache_pointer, "<?php\n\$ARRAY = \"".$array."\";\n\n");
138 // Add default depency
139 $this->store_extension_version("cache");
141 // Cannot create file
142 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
146 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 // Write global cache array for immediate access
153 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
154 if ($k != "ext_name") {
155 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
157 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
159 if (($k == "ext_keep") && ($v == "Y")) {
160 $cacheArray['active_extensions'][$data['ext_name']] = $v;
162 } elseif (is_array($v)) {
163 // Serialize and BASE64-encode the array
164 $v = base64_encode(serialize($v));
167 // Write cache line to file
168 fwrite($this->cache_pointer, $this->add_raw_row($k, $v));
171 // Cannot create file
172 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
176 function cache_close() {
177 // Quit function when no pointer is set
178 if (empty($this->cache_pointer)) return;
179 if (is_resource($this->cache_pointer)) {
181 fwrite($this->cache_pointer, "?>\n");
183 // Close file add destroy handler
184 fclose($this->cache_pointer);
187 if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);
190 unset($this->cache_pointer);
192 // Cannot create file
193 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
197 function cache_load() {
198 // Is the cache already loaded?
199 if (isset($this->cache_data[$this->cache_file])) {
200 // Return it's content!
201 return $this->cache_data[$this->cache_file];
204 // Is the cache file there?
205 if (FILE_READABLE($this->cache_inc)) {
206 // Prepare temporary array
208 $cache_version = null;
211 require_once($this->cache_inc);
213 // Is there an array?
214 if (is_array($data)) {
216 $this->cache_data[$this->cache_file] = $data;
218 // Cache version found?
219 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
220 // Remember it as well...
221 $this->cache_version[$this->cache_file] = $cache_version;
223 // Invalid cache so destroy it
224 $this->cache_destroy();
227 $this->cache_data[$this->cache_file] = array();
231 return $this->cache_data[$this->cache_file];
233 // Cache problem detected!
234 $this->cache_destroy();
237 // Cache file not found or not readable
238 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
242 // Destroy an existing cache file
243 function cache_destroy() {
244 // Is the cache file there?
245 if (FILE_READABLE($this->cache_inc)) {
247 $this->cache_close();
249 // Remove cache file from system
250 unlink($this->cache_inc);
252 // Is the file there?
253 if (!FILE_READABLE($this->cache_inc)) {
254 // The cache does no longer exist so kill the content
255 unset($this->cache_data[$this->cache_file]);
258 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
262 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
266 function cache_remove($search, $data, $array) {
268 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
269 // Load cache into dummy array
270 $dummy = $this->cache_load();
272 // Search for key in array
273 $key = array_search($data, $dummy[$search]);
275 // Key (hopefully) found?
276 foreach ($array as $a) {
277 // So we can remove all elements as requested
278 unset($dummy[$a][$key]);
281 // Flush array to cache file
282 $this->cache_init($ARRAY);
285 $this->cache_write_array($dummy);
288 $this->cache_close();
291 // Cannot write to cache!
292 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
296 function cache_write_array ($array) {
297 if (is_resource($this->cache_pointer)) {
298 foreach ($array as $k => $v) {
300 // Multi line(s) found
302 foreach($v as $k2 => $v2) {
303 // Put every array element in a row...
304 $LINE .= $this->add_raw_row($k, $v2);
308 $LINE = $this->add_raw_row($k, $v);
312 fwrite($this->cache_pointer, $LINE);
315 // Cannot write array!
316 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
320 function cache_replace($search, $replace, $search_key, $array) {
322 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
323 // Load cache into dummy array
324 $dummy = $this->cache_load();
326 // Check if $dummy is valid (prevents some errors)
327 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
328 // Search for key in array
329 $key_found = array_key_exists($search_key, $dummy[$search]);
330 if ($key_found == true) {
332 // Key (hopefully) found?
333 foreach ($dummy as $a => $v) {
334 // So we can update all entries
337 $dummy[$a][$search_key] = $replace;
341 // Flush array to cache file
342 $this->cache_init($ARRAY);
345 $this->cache_write_array($dummy);
348 $this->cache_close();
352 // Cannot write to cache!
353 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
357 function store_extension_version ($ext_name) {
358 // Valid cache pointer?
359 if (is_resource($this->cache_pointer)) {
360 // Get extension version
361 $ext_ver = GET_EXT_VERSION($ext_name);
363 // Write cache line to file
364 fwrite($this->cache_pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
366 // Cannot create file
367 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
371 function ext_version_matches ($ext_name) {
372 // Load cache (dummy)
375 // Get extension version
376 $ext_ver = GET_EXT_VERSION($ext_name);
379 return ((isset($this->cache_version[$this->cache_file][$ext_name])) && ($this->cache_version[$this->cache_file][$ext_name] == $ext_ver));
382 function add_raw_row ($key, $value) {
386 // String or non-string? ;-)
387 if ((is_string($value)) || (is_null($value))) {
389 $line = "\$data['".$key."'][] = \"".$value."\";\n";
392 $line = "\$data['".$key."'][] = ".$value.";\n";
399 function getStatus () {