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";
49 var $version = array();
51 var $rebuilt = array();
54 function CacheSystem ($interval, $path, $tested) {
55 // Failed is the default
56 $this->ret = "failed";
61 // Check if path exists
62 if ((is_dir($path)) && (!$tested)) {
63 // Check if we can create a file inside the path
64 touch($path."dummy.tmp", 'w');
65 if (FILE_READABLE($path."dummy.tmp")) {
66 // Yes, we can do. So let's remove it
67 unlink($path."dummy.tmp");
69 // Is there a .htaccess file?
70 if (FILE_READABLE($path.".htaccess")) {
71 // Update database that we have tested it
72 UPDATE_CONFIG("cache_tested", 1);
77 // Stop! Set a .htaccess file first
78 $this->ret = "htaccess";
82 // System already tested
87 // Checks validity of cache file and if content is given
88 function loadCacheFile ($cacheName, $forceContent = false) {
89 // Remember cache file
90 $this->name = $cacheName;
92 // Construct include filename for LOAD_INC_ONCE() call
93 $this->inc = $this->path . $cacheName . ".cache";
95 // Construct FQFN (full qualified file name)
96 $this->fqfn = constant('PATH') . $this->inc;
98 // Check if file exists and if version matches
99 $status = ($this->isCacheReadable() && (is_writeable($this->fqfn)) && ($this->extensionVersionMatches("cache")));
105 // Initializes the cache file
107 // This will destory an existing cache file!
108 if ($this->ret == "done") {
110 if ($this->isCacheReadable()) chmod($this->fqfn, 0666);
111 $this->pointer = fopen($this->fqfn, 'w') or mxchange_die("Cannot write to cache ".$this->fqfn." !");
114 fwrite($this->pointer, "<?php\n");
116 // Add default depency
117 $this->storeExtensionVersion("cache");
119 // Cannot create file
120 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
124 function addRow ($data) {
125 // Is the pointe rvalid?
126 if (is_resource($this->pointer)) {
127 // Write every array element to cache file
128 foreach ($data as $k => $v) {
129 // Write global cache array for immediate access
130 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
131 if ($k != "ext_name") {
132 $GLOBALS['cache_array']['extensions'][$k][$data['ext_name']] = $v;
134 $GLOBALS['cache_array']['extensions'][$k][$data['ext_id']] = $v;
136 if (($k == "ext_keep") && ($v == "Y")) {
137 $GLOBALS['cache_array']['active_extensions'][$data['ext_name']] = $v;
139 } elseif (is_array($v)) {
140 // Serialize and BASE64-encode the array
141 $v = base64_encode(serialize($v));
144 // Write cache line to file
145 fwrite($this->pointer, $this->rewriteEntry($k, $v));
148 // Cannot create file
149 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
153 function finalize () {
154 // Quit function when no pointer is set
155 if (is_resource($this->pointer)) {
157 fwrite($this->pointer, "?>\n");
159 // Close file add destroy handler
160 fclose($this->pointer);
163 if ($this->isCacheReadable()) chmod($this->fqfn, 0666);
166 $this->pointer = false;
167 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - FINALIZED!<br />\n";
171 function getArrayFromCache () {
172 // Is the cache already loaded?
173 if (isset($this->data[$this->name])) {
174 // Return it's content!
175 return $this->data[$this->name];
178 // Is the cache file there?
179 if ($this->isCacheReadable()) {
180 // Prepare temporary array
182 $cache_version = null;
187 // Is there an array?
188 if (is_array($data)) {
190 $this->data[$this->name] = $data;
192 // Cache version found?
193 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
194 // Remember it as well...
195 $this->version[$this->name] = $cache_version;
197 // Invalid cache so destroy it
198 $this->destroyCacheFile();
201 $this->data[$this->name] = array();
205 return $this->data[$this->name];
207 // Cache problem detected!
208 $this->destroyCacheFile();
211 // Cache file not found or not readable
212 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->fqfn.CACHE_CANNOT_LOAD_2);
216 // Destroy an existing cache file
217 function destroyCacheFile () {
218 // Is the cache file there?
219 if ((!isset($this->rebuilt[$this->name])) && ($this->isCacheReadable())) {
223 // Remove cache file from system
224 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - DESTROYED!<br />\n";
227 // Is the file there?
228 if (!$this->isCacheReadable()) {
229 // The cache does no longer exist so kill the content
230 unset($this->data[$this->name]);
231 unset($this->version[$this->name]);
232 $this->rebuilt[$this->name] = true;
235 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->fqfn.CACHE_CANNOT_UNLINK_2);
241 function removeEntry ($search, $data, $array) {
242 if (($this->isCacheReadable()) && (is_writeable($this->fqfn))) {
243 // Load cache into dummy array
244 $dummy = $this->getArrayFromCache();
246 // Search for key in array
247 $key = array_search($data, $dummy[$search]);
249 // Key (hopefully) found?
250 foreach ($array as $a) {
251 // So we can remove all elements as requested
252 unset($dummy[$a][$key]);
255 // Flush array to cache file
259 $this->writeArray($dummy);
265 // Cannot write to cache!
266 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
270 function writeArray ($array) {
271 if (is_resource($this->pointer)) {
272 foreach ($array as $k => $v) {
274 // Multi line(s) found
276 foreach($v as $k2 => $v2) {
277 // Put every array element in a row...
278 $LINE .= $this->rewriteEntry($k, $v2);
282 $LINE = $this->rewriteEntry($k, $v);
286 fwrite($this->pointer, $LINE);
289 // Cannot write array!
290 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
295 function replaceEntry ($search, $replace, $search_key, $array) {
296 if (($this->isCacheReadable()) && (is_writeable($this->fqfn))) {
297 // Load cache into dummy array
298 $dummy = $this->getArrayFromCache();
300 // Check if $dummy is valid (prevents some errors)
301 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
302 // Search for key in array
303 $key_found = array_key_exists($search_key, $dummy[$search]);
304 if ($key_found == true) {
306 // Key (hopefully) found?
307 foreach ($dummy as $a => $v) {
308 // So we can update all entries
311 $dummy[$a][$search_key] = $replace;
315 // Flush array to cache file
319 $this->writeArray($dummy);
326 // Cannot write to cache!
327 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
331 // Writes the version of given extension to the cache file
332 function storeExtensionVersion ($ext_name) {
333 // Valid cache pointer?
334 if (is_resource($this->pointer)) {
335 // Get extension version
336 $ext_ver = GET_EXT_VERSION($ext_name);
338 // Write cache line to file
339 fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
341 // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
342 $this->version[$this->name][$ext_name] = $ext_ver;
343 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />\n";
345 // Cannot create file
346 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
350 // Checks wether versions from cache and extension matches
351 function extensionVersionMatches ($ext_name) {
352 // Load cache (dummy)
353 $this->getArrayFromCache();
355 // Get extension version
356 $ext_ver = GET_EXT_VERSION($ext_name);
359 if (isset($this->version[$this->name][$ext_name])) {
360 //* 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";
362 // No cache version found!
363 DEBUG_LOG(__METHOD__, __LINE__, "Cache {$this->name} has missing version entry for extension {$ext_name}!");
367 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
370 // Rewrit the entry so it can be stored in cache file
371 // @TODO Add support for more types which break in last else-block
372 function rewriteEntry ($key, $value) {
376 // String or non-string? ;-)
377 if (is_string($value)) {
379 $line = "\$data['".$key."'][] = \"".$value."\";\n";
380 } elseif (is_null($value)) {
382 $line = "\$data['".$key."'][] = null;\n";
383 } elseif (is_bool($value)) {
385 if ($value === true) {
387 $line = "\$data['".$key."'][] = true;\n";
390 $line = "\$data['".$key."'][] = false;\n";
394 $line = "\$data['".$key."'][] = ".$value.";\n";
401 // Getter for cache status
402 function getStatus () {
406 // Checks wether the current cache file is readable
407 function isCacheReadable () {
408 return INCLUDE_READABLE($this->inc);
412 // Destroy the cache on extension changes
413 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE ($data) {
415 if (EXT_IS_ACTIVE("cache")) {
416 if ($GLOBALS['cache_instance']->loadCacheFile("config")) $GLOBALS['cache_instance']->destroyCacheFile();
417 if ($GLOBALS['cache_instance']->loadCacheFile("extensions")) $GLOBALS['cache_instance']->destroyCacheFile();
418 if ($GLOBALS['cache_instance']->loadCacheFile("modreg")) $GLOBALS['cache_instance']->destroyCacheFile();
425 // Destroy the cache on changing admin
426 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
428 if (EXT_IS_ACTIVE("cache")) {
429 if ($GLOBALS['cache_instance']->loadCacheFile("admins")) $GLOBALS['cache_instance']->destroyCacheFile();
433 // Destroy all cache files
434 function FILTER_CACHE_DESTROY_ALL () {
436 if (EXT_IS_ACTIVE("cache")) {
437 if ($GLOBALS['cache_instance']->loadCacheFile("admins")) $GLOBALS['cache_instance']->destroyCacheFile();
438 if ($GLOBALS['cache_instance']->loadCacheFile("admins_acls")) $GLOBALS['cache_instance']->destroyCacheFile();
439 if ($GLOBALS['cache_instance']->loadCacheFile("config")) $GLOBALS['cache_instance']->destroyCacheFile();
440 if ($GLOBALS['cache_instance']->loadCacheFile("extensions")) $GLOBALS['cache_instance']->destroyCacheFile();
441 if ($GLOBALS['cache_instance']->loadCacheFile("modreg")) $GLOBALS['cache_instance']->destroyCacheFile();
442 if ($GLOBALS['cache_instance']->loadCacheFile("refdepths")) $GLOBALS['cache_instance']->destroyCacheFile();
443 if ($GLOBALS['cache_instance']->loadCacheFile("refsystem")) $GLOBALS['cache_instance']->destroyCacheFile();
444 if ($GLOBALS['cache_instance']->loadCacheFile("themes")) $GLOBALS['cache_instance']->destroyCacheFile();
448 // Filter for purging entire admin menu cache
449 function FILTER_CACHE_PURGE_ADMIN_MENU () {
450 // Just call the function
451 CACHE_PURGE_ADMIN_MENU();