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";
52 function CacheSystem ($interval, $path, $tested) {
53 // Failed is the default
54 $this->ret = "failed";
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 loadCacheFile ($file, $forceContent = false) {
87 // Remember cache file
90 // Construct FQFN (full qualified file name)
91 $this->inc = $this->path.$file.".cache";
93 // Check if file exists and if version matches
94 $status = (FILE_READABLE($this->inc) && (is_writeable($this->inc)) && ($this->extensionVersionMatches("cache")));
101 // This will destory an existing cache file!
102 if ($this->ret == "done") {
104 if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
105 $this->pointer = fopen($this->inc, 'w') or mxchange_die("Cannot write to cache ".$this->inc." !");
108 fwrite($this->pointer, "<?php\n");
110 // Add default depency
111 $this->storeExtensionVersion("cache");
113 // Cannot create file
114 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
118 function addRow ($data) {
121 if (is_resource($this->pointer)) {
122 // Write every array element to cache file
123 foreach ($data as $k => $v) {
124 // Write global cache array for immediate access
125 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
126 if ($k != "ext_name") {
127 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
129 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
131 if (($k == "ext_keep") && ($v == "Y")) {
132 $cacheArray['active_extensions'][$data['ext_name']] = $v;
134 } elseif (is_array($v)) {
135 // Serialize and BASE64-encode the array
136 $v = base64_encode(serialize($v));
139 // Write cache line to file
140 fwrite($this->pointer, $this->rewriteEntry($k, $v));
143 // Cannot create file
144 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
148 function finalize () {
149 // Quit function when no pointer is set
150 if (is_resource($this->pointer)) {
152 fwrite($this->pointer, "?>\n");
154 // Close file add destroy handler
155 fclose($this->pointer);
158 if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
161 $this->pointer = false;
165 function getArrayFromCache () {
166 // Is the cache already loaded?
167 if (isset($this->data[$this->name])) {
168 // Return it's content!
169 return $this->data[$this->name];
172 // Is the cache file there?
173 if (FILE_READABLE($this->inc)) {
174 // Prepare temporary array
176 $cache_version = null;
179 require_once($this->inc);
181 // Is there an array?
182 if (is_array($data)) {
184 $this->data[$this->name] = $data;
186 // Cache version found?
187 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
188 // Remember it as well...
189 $this->version[$this->name] = $cache_version;
191 // Invalid cache so destroy it
192 $this->destroyCacheFile();
195 $this->data[$this->name] = array();
199 return $this->data[$this->name];
201 // Cache problem detected!
202 $this->destroyCacheFile();
205 // Cache file not found or not readable
206 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->inc.CACHE_CANNOT_LOAD_2);
210 // Destroy an existing cache file
211 function destroyCacheFile () {
212 // Is the cache file there?
213 if (FILE_READABLE($this->inc)) {
217 // Remove cache file from system
220 // Is the file there?
221 if (!FILE_READABLE($this->inc)) {
222 // The cache does no longer exist so kill the content
223 unset($this->data[$this->name]);
226 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->inc.CACHE_CANNOT_UNLINK_2);
230 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
235 function removeEntry ($search, $data, $array) {
236 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
237 // Load cache into dummy array
238 $dummy = $this->getArrayFromCache();
240 // Search for key in array
241 $key = array_search($data, $dummy[$search]);
243 // Key (hopefully) found?
244 foreach ($array as $a) {
245 // So we can remove all elements as requested
246 unset($dummy[$a][$key]);
249 // Flush array to cache file
253 $this->writeArray($dummy);
259 // Cannot write to cache!
260 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
264 function writeArray ($array) {
265 if (is_resource($this->pointer)) {
266 foreach ($array as $k => $v) {
268 // Multi line(s) found
270 foreach($v as $k2 => $v2) {
271 // Put every array element in a row...
272 $LINE .= $this->rewriteEntry($k, $v2);
276 $LINE = $this->rewriteEntry($k, $v);
280 fwrite($this->pointer, $LINE);
283 // Cannot write array!
284 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
289 function replaceEntry ($search, $replace, $search_key, $array) {
290 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
291 // Load cache into dummy array
292 $dummy = $this->getArrayFromCache();
294 // Check if $dummy is valid (prevents some errors)
295 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
296 // Search for key in array
297 $key_found = array_key_exists($search_key, $dummy[$search]);
298 if ($key_found == true) {
300 // Key (hopefully) found?
301 foreach ($dummy as $a => $v) {
302 // So we can update all entries
305 $dummy[$a][$search_key] = $replace;
309 // Flush array to cache file
313 $this->writeArray($dummy);
320 // Cannot write to cache!
321 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
325 function storeExtensionVersion ($ext_name) {
326 // Valid cache pointer?
327 if (is_resource($this->pointer)) {
328 // Get extension version
329 $ext_ver = GET_EXT_VERSION($ext_name);
331 // Write cache line to file
332 fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
334 // Cannot create file
335 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
339 function extensionVersionMatches ($ext_name) {
340 // Load cache (dummy)
341 $this->getArrayFromCache();
343 // Get extension version
344 $ext_ver = GET_EXT_VERSION($ext_name);
347 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
350 function rewriteEntry ($key, $value) {
354 // String or non-string? ;-)
355 if (is_string($value)) {
357 $line = "\$data['".$key."'][] = \"".$value."\";\n";
358 } elseif (is_null($value)) {
360 $line = "\$data['".$key."'][] = null;\n";
361 } elseif (is_bool($value)) {
363 if ($value === true) {
364 $line = "\$data['".$key."'][] = true;\n";
366 $line = "\$data['".$key."'][] = false;\n";
370 $line = "\$data['".$key."'][] = ".$value.";\n";
377 function getStatus () {
382 // Destroy the cache on extension changes
383 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE () {
384 global $cacheInstance;
387 if (EXT_IS_ACTIVE("cache")) {
388 if ($cacheInstance->loadCacheFile("config")) $cacheInstance->destroyCacheFile();
389 if ($cacheInstance->loadCacheFile("extensions", true)) $cacheInstance->destroyCacheFile();
390 if ($cacheInstance->loadCacheFile("mod_reg")) $cacheInstance->destroyCacheFile();
394 // Destroy the cache on changing admin
395 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
396 global $cacheInstance;
399 if (EXT_IS_ACTIVE("cache")) {
400 if ($cacheInstance->loadCacheFile("admin")) $cacheInstance->destroyCacheFile();