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";
48 var $version = array();
50 var $rebuilt = array();
53 function CacheSystem ($interval, $path, $tested) {
54 // Failed is the default
55 $this->ret = "failed";
60 // Check if path exists
61 if ((is_dir($path)) && (!$tested)) {
62 // Check if we can create a file inside the path
63 touch($path."dummy.tmp", 'w');
64 if (FILE_READABLE($path."dummy.tmp")) {
65 // Yes, we can do. So let's remove it
66 unlink($path."dummy.tmp");
68 // Is there a .htaccess file?
69 if (FILE_READABLE($path.".htaccess")) {
70 // Update database that we have tested it
71 UPDATE_CONFIG("cache_tested", 1);
76 // Stop! Set a .htaccess file first
77 $this->ret = "htaccess";
81 // System already tested
86 // Checks validity of cache file and if content is given
87 function loadCacheFile ($cacheName, $forceContent = false) {
88 // Remember cache file
89 $this->name = $cacheName;
91 // Construct FQFN (full qualified file name)
92 $this->inc = $this->path . $cacheName . ".cache";
94 // Check if file exists and if version matches
95 $status = (FILE_READABLE($this->inc) && (is_writeable($this->inc)) && ($this->extensionVersionMatches("cache")));
102 // This will destory an existing cache file!
103 if ($this->ret == "done") {
105 if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
106 $this->pointer = fopen($this->inc, 'w') or mxchange_die("Cannot write to cache ".$this->inc." !");
109 fwrite($this->pointer, "<?php\n");
111 // Add default depency
112 $this->storeExtensionVersion("cache");
114 // Cannot create file
115 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
119 function addRow ($data) {
120 // Is the pointe rvalid?
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 $GLOBALS['cache_array']['extensions'][$k][$data['ext_name']] = $v;
129 $GLOBALS['cache_array']['extensions'][$k][$data['ext_id']] = $v;
131 if (($k == "ext_keep") && ($v == "Y")) {
132 $GLOBALS['cache_array']['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 addFatalMessage(__METHOD__, __LINE__, "(<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;
162 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - FINALIZED!<br />\n";
166 function getArrayFromCache () {
167 // Is the cache already loaded?
168 if (isset($this->data[$this->name])) {
169 // Return it's content!
170 return $this->data[$this->name];
173 // Is the cache file there?
174 if (FILE_READABLE($this->inc)) {
175 // Prepare temporary array
177 $cache_version = null;
180 LOAD_INC_ONCE($this->inc);
182 // Is there an array?
183 if (is_array($data)) {
185 $this->data[$this->name] = $data;
187 // Cache version found?
188 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
189 // Remember it as well...
190 $this->version[$this->name] = $cache_version;
192 // Invalid cache so destroy it
193 $this->destroyCacheFile();
196 $this->data[$this->name] = array();
200 return $this->data[$this->name];
202 // Cache problem detected!
203 $this->destroyCacheFile();
206 // Cache file not found or not readable
207 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->inc.CACHE_CANNOT_LOAD_2);
211 // Destroy an existing cache file
212 function destroyCacheFile () {
213 // Is the cache file there?
214 if ((!isset($this->rebuilt[$this->name])) && (FILE_READABLE($this->inc))) {
218 // Remove cache file from system
219 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - DESTROYED!<br />\n";
222 // Is the file there?
223 if (!FILE_READABLE($this->inc)) {
224 // The cache does no longer exist so kill the content
225 unset($this->data[$this->name]);
226 unset($this->version[$this->name]);
227 $this->rebuilt[$this->name] = true;
230 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->inc.CACHE_CANNOT_UNLINK_2);
236 function removeEntry ($search, $data, $array) {
237 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
238 // Load cache into dummy array
239 $dummy = $this->getArrayFromCache();
241 // Search for key in array
242 $key = array_search($data, $dummy[$search]);
244 // Key (hopefully) found?
245 foreach ($array as $a) {
246 // So we can remove all elements as requested
247 unset($dummy[$a][$key]);
250 // Flush array to cache file
254 $this->writeArray($dummy);
260 // Cannot write to cache!
261 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
265 function writeArray ($array) {
266 if (is_resource($this->pointer)) {
267 foreach ($array as $k => $v) {
269 // Multi line(s) found
271 foreach($v as $k2 => $v2) {
272 // Put every array element in a row...
273 $LINE .= $this->rewriteEntry($k, $v2);
277 $LINE = $this->rewriteEntry($k, $v);
281 fwrite($this->pointer, $LINE);
284 // Cannot write array!
285 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
290 function replaceEntry ($search, $replace, $search_key, $array) {
291 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
292 // Load cache into dummy array
293 $dummy = $this->getArrayFromCache();
295 // Check if $dummy is valid (prevents some errors)
296 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
297 // Search for key in array
298 $key_found = array_key_exists($search_key, $dummy[$search]);
299 if ($key_found == true) {
301 // Key (hopefully) found?
302 foreach ($dummy as $a => $v) {
303 // So we can update all entries
306 $dummy[$a][$search_key] = $replace;
310 // Flush array to cache file
314 $this->writeArray($dummy);
321 // Cannot write to cache!
322 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
326 function storeExtensionVersion ($ext_name) {
327 // Valid cache pointer?
328 if (is_resource($this->pointer)) {
329 // Get extension version
330 $ext_ver = GET_EXT_VERSION($ext_name);
332 // Write cache line to file
333 fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
335 // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
336 $this->version[$this->name][$ext_name] = $ext_ver;
337 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />\n";
339 // Cannot create file
340 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
344 function extensionVersionMatches ($ext_name) {
345 // Load cache (dummy)
346 $this->getArrayFromCache();
348 // Get extension version
349 $ext_ver = GET_EXT_VERSION($ext_name);
352 if (isset($this->version[$this->name][$ext_name])) {
353 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): cache={$this->name},ext_name={$ext_name} - {$ext_ver}/{$this->version[$this->name][$ext_name]}<br />\n";
355 // No cache version found!
356 DEBUG_LOG(__METHOD__, __LINE__, "Cache {$this->name} has missing entry for extension {$ext_name}!");
360 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
363 function rewriteEntry ($key, $value) {
367 // String or non-string? ;-)
368 if (is_string($value)) {
370 $line = "\$data['".$key."'][] = \"".$value."\";\n";
371 } elseif (is_null($value)) {
373 $line = "\$data['".$key."'][] = null;\n";
374 } elseif (is_bool($value)) {
376 if ($value === true) {
377 $line = "\$data['".$key."'][] = true;\n";
379 $line = "\$data['".$key."'][] = false;\n";
383 $line = "\$data['".$key."'][] = ".$value.";\n";
390 function getStatus () {
395 // Destroy the cache on extension changes
396 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE ($data) {
398 if (EXT_IS_ACTIVE("cache")) {
399 if ($GLOBALS['cache_instance']->loadCacheFile("config")) $GLOBALS['cache_instance']->destroyCacheFile();
400 if ($GLOBALS['cache_instance']->loadCacheFile("extensions")) $GLOBALS['cache_instance']->destroyCacheFile();
401 if ($GLOBALS['cache_instance']->loadCacheFile("modreg")) $GLOBALS['cache_instance']->destroyCacheFile();
408 // Destroy the cache on changing admin
409 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
411 if (EXT_IS_ACTIVE("cache")) {
412 if ($GLOBALS['cache_instance']->loadCacheFile("admins")) $GLOBALS['cache_instance']->destroyCacheFile();
416 // Destroy all cache files
417 function FILTER_CACHE_DESTROY_ALL () {
419 if (EXT_IS_ACTIVE("cache")) {
420 if ($GLOBALS['cache_instance']->loadCacheFile("admins")) $GLOBALS['cache_instance']->destroyCacheFile();
421 if ($GLOBALS['cache_instance']->loadCacheFile("admins_acls")) $GLOBALS['cache_instance']->destroyCacheFile();
422 if ($GLOBALS['cache_instance']->loadCacheFile("config")) $GLOBALS['cache_instance']->destroyCacheFile();
423 if ($GLOBALS['cache_instance']->loadCacheFile("extensions")) $GLOBALS['cache_instance']->destroyCacheFile();
424 if ($GLOBALS['cache_instance']->loadCacheFile("modreg")) $GLOBALS['cache_instance']->destroyCacheFile();
425 if ($GLOBALS['cache_instance']->loadCacheFile("refdepths")) $GLOBALS['cache_instance']->destroyCacheFile();
426 if ($GLOBALS['cache_instance']->loadCacheFile("refsystem")) $GLOBALS['cache_instance']->destroyCacheFile();
427 if ($GLOBALS['cache_instance']->loadCacheFile("themes")) $GLOBALS['cache_instance']->destroyCacheFile();
431 // Filter for purging entire admin menu cache
432 function FILTER_CACHE_PURGE_ADMIN_MENU () {
433 // Just call the function
434 CACHE_PURGE_ADMIN_MENU();