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";
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(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
119 function addRow ($data) {
122 if (is_resource($this->pointer)) {
123 // Write every array element to cache file
124 foreach ($data as $k => $v) {
125 // Write global cache array for immediate access
126 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
127 if ($k != "ext_name") {
128 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
130 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
132 if (($k == "ext_keep") && ($v == "Y")) {
133 $cacheArray['active_extensions'][$data['ext_name']] = $v;
135 } elseif (is_array($v)) {
136 // Serialize and BASE64-encode the array
137 $v = base64_encode(serialize($v));
140 // Write cache line to file
141 fwrite($this->pointer, $this->rewriteEntry($k, $v));
144 // Cannot create file
145 addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
149 function finalize () {
150 // Quit function when no pointer is set
151 if (is_resource($this->pointer)) {
153 fwrite($this->pointer, "?>\n");
155 // Close file add destroy handler
156 fclose($this->pointer);
159 if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
162 $this->pointer = false;
163 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - FINALIZED!<br />\n";
167 function getArrayFromCache () {
168 // Is the cache already loaded?
169 if (isset($this->data[$this->name])) {
170 // Return it's content!
171 return $this->data[$this->name];
174 // Is the cache file there?
175 if (FILE_READABLE($this->inc)) {
176 // Prepare temporary array
178 $cache_version = null;
181 LOAD_INC_ONCE($this->inc);
183 // Is there an array?
184 if (is_array($data)) {
186 $this->data[$this->name] = $data;
188 // Cache version found?
189 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
190 // Remember it as well...
191 $this->version[$this->name] = $cache_version;
193 // Invalid cache so destroy it
194 $this->destroyCacheFile();
197 $this->data[$this->name] = array();
201 return $this->data[$this->name];
203 // Cache problem detected!
204 $this->destroyCacheFile();
207 // Cache file not found or not readable
208 addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->inc.CACHE_CANNOT_LOAD_2);
212 // Destroy an existing cache file
213 function destroyCacheFile () {
214 // Is the cache file there?
215 if ((!isset($this->rebuilt[$this->name])) && (FILE_READABLE($this->inc))) {
219 // Remove cache file from system
220 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - DESTROYED!<br />\n";
223 // Is the file there?
224 if (!FILE_READABLE($this->inc)) {
225 // The cache does no longer exist so kill the content
226 unset($this->data[$this->name]);
227 unset($this->version[$this->name]);
228 $this->rebuilt[$this->name] = true;
231 addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->inc.CACHE_CANNOT_UNLINK_2);
237 function removeEntry ($search, $data, $array) {
238 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
239 // Load cache into dummy array
240 $dummy = $this->getArrayFromCache();
242 // Search for key in array
243 $key = array_search($data, $dummy[$search]);
245 // Key (hopefully) found?
246 foreach ($array as $a) {
247 // So we can remove all elements as requested
248 unset($dummy[$a][$key]);
251 // Flush array to cache file
255 $this->writeArray($dummy);
261 // Cannot write to cache!
262 addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
266 function writeArray ($array) {
267 if (is_resource($this->pointer)) {
268 foreach ($array as $k => $v) {
270 // Multi line(s) found
272 foreach($v as $k2 => $v2) {
273 // Put every array element in a row...
274 $LINE .= $this->rewriteEntry($k, $v2);
278 $LINE = $this->rewriteEntry($k, $v);
282 fwrite($this->pointer, $LINE);
285 // Cannot write array!
286 addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
291 function replaceEntry ($search, $replace, $search_key, $array) {
292 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
293 // Load cache into dummy array
294 $dummy = $this->getArrayFromCache();
296 // Check if $dummy is valid (prevents some errors)
297 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
298 // Search for key in array
299 $key_found = array_key_exists($search_key, $dummy[$search]);
300 if ($key_found == true) {
302 // Key (hopefully) found?
303 foreach ($dummy as $a => $v) {
304 // So we can update all entries
307 $dummy[$a][$search_key] = $replace;
311 // Flush array to cache file
315 $this->writeArray($dummy);
322 // Cannot write to cache!
323 addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
327 function storeExtensionVersion ($ext_name) {
328 // Valid cache pointer?
329 if (is_resource($this->pointer)) {
330 // Get extension version
331 $ext_ver = GET_EXT_VERSION($ext_name);
333 // Write cache line to file
334 fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
336 // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
337 $this->version[$this->name][$ext_name] = $ext_ver;
338 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />\n";
340 // Cannot create file
341 addFatalMessage(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
345 function extensionVersionMatches ($ext_name) {
346 // Load cache (dummy)
347 $this->getArrayFromCache();
349 // Get extension version
350 $ext_ver = GET_EXT_VERSION($ext_name);
353 if (isset($this->version[$this->name][$ext_name])) {
354 //* 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";
356 // No cache version found!
357 DEBUG_LOG(__METHOD__, __LINE__, "Cache {$this->name} has missing entry for extension {$ext_name}!");
361 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
364 function rewriteEntry ($key, $value) {
368 // String or non-string? ;-)
369 if (is_string($value)) {
371 $line = "\$data['".$key."'][] = \"".$value."\";\n";
372 } elseif (is_null($value)) {
374 $line = "\$data['".$key."'][] = null;\n";
375 } elseif (is_bool($value)) {
377 if ($value === true) {
378 $line = "\$data['".$key."'][] = true;\n";
380 $line = "\$data['".$key."'][] = false;\n";
384 $line = "\$data['".$key."'][] = ".$value.";\n";
391 function getStatus () {
396 // Destroy the cache on extension changes
397 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE ($data) {
398 global $cacheInstance;
401 if (EXT_IS_ACTIVE("cache")) {
402 if ($cacheInstance->loadCacheFile("config")) $cacheInstance->destroyCacheFile();
403 if ($cacheInstance->loadCacheFile("extensions")) $cacheInstance->destroyCacheFile();
404 if ($cacheInstance->loadCacheFile("mod_reg")) $cacheInstance->destroyCacheFile();
411 // Destroy the cache on changing admin
412 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
413 global $cacheInstance;
416 if (EXT_IS_ACTIVE("cache")) {
417 if ($cacheInstance->loadCacheFile("admins")) $cacheInstance->destroyCacheFile();
421 // Destroy all cache files
422 function FILTER_CACHE_DESTROY_ALL () {
423 global $cacheInstance;
426 if (EXT_IS_ACTIVE("cache")) {
427 if ($cacheInstance->loadCacheFile("admins")) $cacheInstance->destroyCacheFile();
428 if ($cacheInstance->loadCacheFile("admins_acls")) $cacheInstance->destroyCacheFile();
429 if ($cacheInstance->loadCacheFile("config")) $cacheInstance->destroyCacheFile();
430 if ($cacheInstance->loadCacheFile("extensions")) $cacheInstance->destroyCacheFile();
431 if ($cacheInstance->loadCacheFile("mod_reg")) $cacheInstance->destroyCacheFile();
432 if ($cacheInstance->loadCacheFile("refdepths")) $cacheInstance->destroyCacheFile();
433 if ($cacheInstance->loadCacheFile("refsystem")) $cacheInstance->destroyCacheFile();
434 if ($cacheInstance->loadCacheFile("themes")) $cacheInstance->destroyCacheFile();
438 // Filter for purging entire admin menu cache
439 function FILTER_CACHE_PURGE_ADMIN_MENU () {
440 // Just call the function
441 CACHE_PURGE_ADMIN_MENU();