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 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2008 by Roland Haeder *
21 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
54 var $version = array();
56 var $rebuilt = array();
57 var $extension = '.cache';
58 var $statusDone = 'done';
61 function CacheSystem ($interval, $path, $tested) {
62 // Failed is the default
63 $this->ret = 'failed';
68 // Check if path exists
69 if ((isDirectory($path)) && ($tested === false)) {
70 // Make FQFN for dummy file
71 $fqfn = $path . 'dummy.tmp';
73 // Check if we can create a file inside the path
77 if (isFileReadable($fqfn)) {
78 // Yes, we can do. So let's remove it
81 // Is there a .htaccess file?
82 if (isFileReadable($path . '.htaccess')) {
83 // Update database that we have tested it
84 updateConfiguration('cache_tested', 1);
87 $this->ret = $this->statusDone;
89 // Stop! Set a .htaccess file first
90 $this->ret = 'htaccess';
94 // System already tested
95 $this->ret = $this->statusDone;
99 // Checks validity of cache file and if content is given
100 function loadCacheFile ($cacheName, $forceContent = false) {
101 // Remember cache file
102 $this->name = $cacheName;
104 // Construct include filename for loadIncludeOnce() call
105 $this->inc = $this->path . $cacheName . $this->extension;
107 // Construct FQFN (full qualified file name)
108 $this->fqfn = constant('PATH') . $this->inc;
110 // Check if file exists and if version matches
111 $status = ($this->isCacheReadable() && (is_writeable($this->fqfn)) && ($this->extensionVersionMatches('cache')));
117 // Initializes the cache file
119 // This will destory an existing cache file!
120 if ($this->ret == $this->statusDone) {
122 if ($this->isCacheReadable()) changeMode($this->fqfn, 0666);
123 $this->pointer = fopen($this->fqfn, 'w') or app_die(__METHOD__, __LINE__, "Cannot write to cache ".$this->fqfn." !");
126 fwrite($this->pointer, "<?php\n");
128 // Add default depency
129 $this->storeExtensionVersion('cache');
131 // Cannot create file
132 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
136 function addRow ($data) {
137 // Is the pointe rvalid?
138 if (is_resource($this->pointer)) {
139 // Write every array element to cache file
140 foreach ($data as $k => $v) {
141 // Write global cache array for immediate access
142 if ((substr($k, 0, 4) == 'ext_') && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
143 if ($k != 'ext_name') {
144 $GLOBALS['cache_array']['extensions'][$k][$data['ext_name']] = $v;
146 $GLOBALS['cache_array']['extensions'][$k][$data['ext_id']] = $v;
148 if (($k == 'ext_keep') && ($v == 'Y')) {
149 $GLOBALS['cache_array']['active_extensions'][$data['ext_name']] = $v;
151 } elseif (is_array($v)) {
152 // Serialize and BASE64-encode the array
153 $v = base64_encode(serialize($v));
156 // Write cache line to file
157 fwrite($this->pointer, $this->rewriteEntry($k, $v));
160 // Cannot create file
161 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
165 function finalize () {
166 // Quit function when no pointer is set
167 if (is_resource($this->pointer)) {
169 fwrite($this->pointer, "?>\n");
171 // Close file add destroy handler
172 fclose($this->pointer);
175 if ($this->isCacheReadable()) changeMode($this->fqfn, 0666);
178 $this->pointer = false;
179 //* DEBUG: */ OUTPUT_HTML(__METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - FINALIZED!<br />");
183 function getArrayFromCache () {
184 // Is the cache already loaded?
185 if (isset($this->data[$this->name])) {
186 // Return it's content!
187 return $this->data[$this->name];
190 // Is the cache file there?
191 if ($this->isCacheReadable()) {
192 // Prepare temporary array
194 $cache_version = null;
199 // Is there an array?
200 if (is_array($data)) {
202 $this->data[$this->name] = $data;
204 // Cache version found?
205 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
206 // Remember it as well...
207 $this->version[$this->name] = $cache_version;
209 // Invalid cache so destroy it
210 $this->destroyCacheFile();
213 $this->data[$this->name] = array();
217 return $this->data[$this->name];
219 // Cache problem detected!
220 $this->destroyCacheFile();
223 // Cache file not found or not readable
224 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->fqfn.CACHE_CANNOT_LOAD_2);
228 // Destroy an existing cache file
229 function destroyCacheFile ($removeArray = false, $force = false) {
230 // Only run in regular output mode
231 if ((getOutputMode() != 0) && ($force === false)) {
232 // Debug message if allowed
233 if (isDebugModeEnabled()) {
235 debug_report_bug('Not removing cache ' . $this->name . ' in output_mode=' . getOutputMode());
242 // Is the cache file there?
243 if ((!isset($this->rebuilt[$this->name])) && ($this->isCacheReadable())) {
247 // Debug-mode enabled?
248 if (isDebugModeEnabled()) {
249 // Log removal of cache
250 DEBUG_LOG(__METHOD__, __LINE__, 'removing cache: ' . $this->name);
253 // Remove cache file from system
254 removeFile($this->fqfn);
256 // Shall we remove the array from memory?
257 if ($removeArray === true) {
258 // Debug message if allowed
259 if (isDebugModeEnabled()) {
261 DEBUG_LOG(__METHOD__, __LINE__, 'removing array!');
264 // Remove it from memory
265 unset($GLOBALS['cache_array'][$this->name]);
268 // Is the file there?
269 if (!$this->isCacheReadable()) {
270 // The cache does no longer exist so kill the content
271 unset($this->data[$this->name]);
272 unset($this->version[$this->name]);
273 $this->rebuilt[$this->name] = true;
276 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->fqfn.CACHE_CANNOT_UNLINK_2);
282 function removeEntry ($search, $data, $array) {
283 if (($this->isCacheReadable()) && (is_writeable($this->fqfn))) {
284 // Load cache into dummy array
285 $dummy = $this->getArrayFromCache();
287 // Search for key in array
288 $key = array_search($data, $dummy[$search]);
290 // Key (hopefully) found?
291 foreach ($array as $a) {
292 // So we can remove all elements as requested
293 unset($dummy[$a][$key]);
296 // Flush array to cache file
300 $this->writeArray($dummy);
306 // Cannot write to cache!
307 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
311 function writeArray ($array) {
312 if (is_resource($this->pointer)) {
313 foreach ($array as $k => $v) {
315 // Multi line(s) found
317 foreach($v as $k2 => $v2) {
318 // Put every array element in a row...
319 $LINE .= $this->rewriteEntry($k, $v2);
323 $LINE = $this->rewriteEntry($k, $v);
327 fwrite($this->pointer, $LINE);
330 // Cannot write array!
331 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
336 function replaceEntry ($search, $replace, $search_key, $array) {
337 if (($this->isCacheReadable()) && (is_writeable($this->fqfn))) {
338 // Load cache into dummy array
339 $dummy = $this->getArrayFromCache();
341 // Check if $dummy is valid (prevents some errors)
342 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
343 // Search for key in array
344 $key_found = array_key_exists($search_key, $dummy[$search]);
345 if ($key_found == true) {
347 // Key (hopefully) found?
348 foreach ($dummy as $a => $v) {
349 // So we can update all entries
352 $dummy[$a][$search_key] = $replace;
356 // Flush array to cache file
360 $this->writeArray($dummy);
367 // Cannot write to cache!
368 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
372 // Writes the version of given extension to the cache file
373 function storeExtensionVersion ($ext_name) {
374 // Valid cache pointer?
375 if (is_resource($this->pointer)) {
376 // Get extension version
377 $ext_ver = GET_EXT_VERSION($ext_name);
379 // Write cache line to file
380 fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
382 // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
383 $this->version[$this->name][$ext_name] = $ext_ver;
384 //* DEBUG: */ OUTPUT_HTML(__METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />");
386 // Cannot create file
387 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
391 // Checks wether versions from cache and extension matches
392 function extensionVersionMatches ($ext_name) {
393 // Load cache (dummy)
394 $this->getArrayFromCache();
396 // Get extension version
397 $ext_ver = GET_EXT_VERSION($ext_name);
400 if (isset($this->version[$this->name][$ext_name])) {
401 //* DEBUG: */ OUTPUT_HTML(__METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): cache={$this->name},ext_name={$ext_name} - {$ext_ver}/{$this->version[$this->name][$ext_name]}<br />");
403 // No cache version found!
404 DEBUG_LOG(__METHOD__, __LINE__, "Cache {$this->name} has missing version entry for extension {$ext_name}!");
408 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
411 // Rewrit the entry so it can be stored in cache file
412 // @TODO Add support for more types which break in last else-block
413 function rewriteEntry ($key, $value) {
417 // String or non-string? ;-)
418 if (is_string($value)) {
420 $line = "\$data['".$key."'][] = \"".$value."\";\n";
421 } elseif (is_null($value)) {
423 $line = "\$data['".$key."'][] = null;\n";
424 } elseif (is_bool($value)) {
426 if ($value === true) {
428 $line = "\$data['".$key."'][] = true;\n";
431 $line = "\$data['".$key."'][] = false;\n";
435 $line = "\$data['".$key."'][] = ".$value.";\n";
442 // Getter for cache status
443 function getStatus () {
447 // Checks wether the current cache file is readable
448 function isCacheReadable () {
449 return isIncludeReadable($this->inc);
454 // Destroy the cache on extension changes
455 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE ($data) {
456 // Return the data anyway if there is no cache extension
457 if (!isCacheInstanceValid()) return $data;
460 if ($GLOBALS['cache_instance']->loadCacheFile('config')) $GLOBALS['cache_instance']->destroyCacheFile(false);
461 if ($GLOBALS['cache_instance']->loadCacheFile('extensions')) $GLOBALS['cache_instance']->destroyCacheFile(false);
462 if ($GLOBALS['cache_instance']->loadCacheFile('modreg')) $GLOBALS['cache_instance']->destroyCacheFile(false);
468 // Destroy the cache on changing admin
469 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
470 // Skip this step if the cache instance is not there
471 if (!isCacheInstanceValid()) return false;
474 if ($GLOBALS['cache_instance']->loadCacheFile('admins')) $GLOBALS['cache_instance']->destroyCacheFile(false);
477 // Destroy all cache files
478 function FILTER_CACHE_DESTROY_ALL () {
479 // Skip this step if the cache instance is not there
480 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, 'Called!');
481 if (!isCacheInstanceValid()) return false;
483 // Remove cache files
484 if ($GLOBALS['cache_instance']->loadCacheFile('admins')) $GLOBALS['cache_instance']->destroyCacheFile(false);
485 if ($GLOBALS['cache_instance']->loadCacheFile('admins_acls')) $GLOBALS['cache_instance']->destroyCacheFile(false);
486 if ($GLOBALS['cache_instance']->loadCacheFile('config')) $GLOBALS['cache_instance']->destroyCacheFile(false);
487 if ($GLOBALS['cache_instance']->loadCacheFile('extensions')) $GLOBALS['cache_instance']->destroyCacheFile(false);
488 if ($GLOBALS['cache_instance']->loadCacheFile('modreg')) $GLOBALS['cache_instance']->destroyCacheFile(false);
489 if ($GLOBALS['cache_instance']->loadCacheFile('refdepths')) $GLOBALS['cache_instance']->destroyCacheFile(false);
490 if ($GLOBALS['cache_instance']->loadCacheFile('refsystem')) $GLOBALS['cache_instance']->destroyCacheFile(false);
491 if ($GLOBALS['cache_instance']->loadCacheFile('themes')) $GLOBALS['cache_instance']->destroyCacheFile(false);
492 if ($GLOBALS['cache_instance']->loadCacheFile('revision')) $GLOBALS['cache_instance']->destroyCacheFile(false);
493 if ($GLOBALS['cache_instance']->loadCacheFile('filter')) $GLOBALS['cache_instance']->destroyCacheFile(false);
494 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, 'Done!');
497 // Filter for purging 'filter' cache
498 function FILTER_CACHE_DESTROY_FILTER () {
499 // Skip this step if the cache instance is not there
500 if ((!isCacheInstanceValid()) || (getConfig('update_filter_usage') == 'N')) return false;
502 // Remove cache files
503 if ($GLOBALS['cache_instance']->loadCacheFile('filter')) $GLOBALS['cache_instance']->destroyCacheFile(false);
506 // Filter for purging entire admin menu cache
507 function FILTER_CACHE_PURGE_ADMIN_MENU () {
508 // Just call the function
509 cachePurgeAdminMenu();