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 * $Date:: 2009-03-06 20:24:32 +0100 (Fr, 06. March 2009) $ *
15 * $Tag:: 0.2.1-FINAL $ *
16 * $Author:: stelzi $ *
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();
59 function CacheSystem ($interval, $path, $tested) {
60 // Failed is the default
61 $this->ret = "failed";
66 // Check if path exists
67 if ((is_dir($path)) && (!$tested)) {
68 // Check if we can create a file inside the path
69 touch($path."dummy.tmp", 'w');
70 if (FILE_READABLE($path."dummy.tmp")) {
71 // Yes, we can do. So let's remove it
72 unlink($path."dummy.tmp");
74 // Is there a .htaccess file?
75 if (FILE_READABLE($path.".htaccess")) {
76 // Update database that we have tested it
77 UPDATE_CONFIG("cache_tested", 1);
82 // Stop! Set a .htaccess file first
83 $this->ret = "htaccess";
87 // System already tested
92 // Checks validity of cache file and if content is given
93 function loadCacheFile ($cacheName, $forceContent = false) {
94 // Remember cache file
95 $this->name = $cacheName;
97 // Construct include filename for LOAD_INC_ONCE() call
98 $this->inc = $this->path . $cacheName . ".cache";
100 // Construct FQFN (full qualified file name)
101 $this->fqfn = constant('PATH') . $this->inc;
103 // Check if file exists and if version matches
104 $status = ($this->isCacheReadable() && (is_writeable($this->fqfn)) && ($this->extensionVersionMatches("cache")));
110 // Initializes the cache file
112 // This will destory an existing cache file!
113 if ($this->ret == "done") {
115 if ($this->isCacheReadable()) chmod($this->fqfn, 0666);
116 $this->pointer = fopen($this->fqfn, 'w') or mxchange_die("Cannot write to cache ".$this->fqfn." !");
119 fwrite($this->pointer, "<?php\n");
121 // Add default depency
122 $this->storeExtensionVersion("cache");
124 // Cannot create file
125 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
129 function addRow ($data) {
130 // Is the pointe rvalid?
131 if (is_resource($this->pointer)) {
132 // Write every array element to cache file
133 foreach ($data as $k => $v) {
134 // Write global cache array for immediate access
135 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
136 if ($k != "ext_name") {
137 $GLOBALS['cache_array']['extensions'][$k][$data['ext_name']] = $v;
139 $GLOBALS['cache_array']['extensions'][$k][$data['ext_id']] = $v;
141 if (($k == "ext_keep") && ($v == "Y")) {
142 $GLOBALS['cache_array']['active_extensions'][$data['ext_name']] = $v;
144 } elseif (is_array($v)) {
145 // Serialize and BASE64-encode the array
146 $v = base64_encode(serialize($v));
149 // Write cache line to file
150 fwrite($this->pointer, $this->rewriteEntry($k, $v));
153 // Cannot create file
154 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
158 function finalize () {
159 // Quit function when no pointer is set
160 if (is_resource($this->pointer)) {
162 fwrite($this->pointer, "?>\n");
164 // Close file add destroy handler
165 fclose($this->pointer);
168 if ($this->isCacheReadable()) chmod($this->fqfn, 0666);
171 $this->pointer = false;
172 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - FINALIZED!<br />\n";
176 function getArrayFromCache () {
177 // Is the cache already loaded?
178 if (isset($this->data[$this->name])) {
179 // Return it's content!
180 return $this->data[$this->name];
183 // Is the cache file there?
184 if ($this->isCacheReadable()) {
185 // Prepare temporary array
187 $cache_version = null;
192 // Is there an array?
193 if (is_array($data)) {
195 $this->data[$this->name] = $data;
197 // Cache version found?
198 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
199 // Remember it as well...
200 $this->version[$this->name] = $cache_version;
202 // Invalid cache so destroy it
203 $this->destroyCacheFile();
206 $this->data[$this->name] = array();
210 return $this->data[$this->name];
212 // Cache problem detected!
213 $this->destroyCacheFile();
216 // Cache file not found or not readable
217 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->fqfn.CACHE_CANNOT_LOAD_2);
221 // Destroy an existing cache file
222 function destroyCacheFile () {
223 // Is the cache file there?
224 if ((!isset($this->rebuilt[$this->name])) && ($this->isCacheReadable())) {
228 // Remove cache file from system
229 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - DESTROYED!<br />\n";
231 // @TODO remove from $GLOBALS['cache_array']!!!
233 // Is the file there?
234 if (!$this->isCacheReadable()) {
235 // The cache does no longer exist so kill the content
236 unset($this->data[$this->name]);
237 unset($this->version[$this->name]);
238 $this->rebuilt[$this->name] = true;
241 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->fqfn.CACHE_CANNOT_UNLINK_2);
247 function removeEntry ($search, $data, $array) {
248 if (($this->isCacheReadable()) && (is_writeable($this->fqfn))) {
249 // Load cache into dummy array
250 $dummy = $this->getArrayFromCache();
252 // Search for key in array
253 $key = array_search($data, $dummy[$search]);
255 // Key (hopefully) found?
256 foreach ($array as $a) {
257 // So we can remove all elements as requested
258 unset($dummy[$a][$key]);
261 // Flush array to cache file
265 $this->writeArray($dummy);
271 // Cannot write to cache!
272 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
276 function writeArray ($array) {
277 if (is_resource($this->pointer)) {
278 foreach ($array as $k => $v) {
280 // Multi line(s) found
282 foreach($v as $k2 => $v2) {
283 // Put every array element in a row...
284 $LINE .= $this->rewriteEntry($k, $v2);
288 $LINE = $this->rewriteEntry($k, $v);
292 fwrite($this->pointer, $LINE);
295 // Cannot write array!
296 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
301 function replaceEntry ($search, $replace, $search_key, $array) {
302 if (($this->isCacheReadable()) && (is_writeable($this->fqfn))) {
303 // Load cache into dummy array
304 $dummy = $this->getArrayFromCache();
306 // Check if $dummy is valid (prevents some errors)
307 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
308 // Search for key in array
309 $key_found = array_key_exists($search_key, $dummy[$search]);
310 if ($key_found == true) {
312 // Key (hopefully) found?
313 foreach ($dummy as $a => $v) {
314 // So we can update all entries
317 $dummy[$a][$search_key] = $replace;
321 // Flush array to cache file
325 $this->writeArray($dummy);
332 // Cannot write to cache!
333 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
337 // Writes the version of given extension to the cache file
338 function storeExtensionVersion ($ext_name) {
339 // Valid cache pointer?
340 if (is_resource($this->pointer)) {
341 // Get extension version
342 $ext_ver = GET_EXT_VERSION($ext_name);
344 // Write cache line to file
345 fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
347 // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
348 $this->version[$this->name][$ext_name] = $ext_ver;
349 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />\n";
351 // Cannot create file
352 addFatalMessage(__METHOD__, __LINE__, "(<font color=\"#0000aa\">".__LINE__."</font>): ".getMessage('CACHE_PROBLEMS_DETECTED'));
356 // Checks wether versions from cache and extension matches
357 function extensionVersionMatches ($ext_name) {
358 // Load cache (dummy)
359 $this->getArrayFromCache();
361 // Get extension version
362 $ext_ver = GET_EXT_VERSION($ext_name);
365 if (isset($this->version[$this->name][$ext_name])) {
366 //* 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";
368 // No cache version found!
369 DEBUG_LOG(__METHOD__, __LINE__, "Cache {$this->name} has missing version entry for extension {$ext_name}!");
373 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
376 // Rewrit the entry so it can be stored in cache file
377 // @TODO Add support for more types which break in last else-block
378 function rewriteEntry ($key, $value) {
382 // String or non-string? ;-)
383 if (is_string($value)) {
385 $line = "\$data['".$key."'][] = \"".$value."\";\n";
386 } elseif (is_null($value)) {
388 $line = "\$data['".$key."'][] = null;\n";
389 } elseif (is_bool($value)) {
391 if ($value === true) {
393 $line = "\$data['".$key."'][] = true;\n";
396 $line = "\$data['".$key."'][] = false;\n";
400 $line = "\$data['".$key."'][] = ".$value.";\n";
407 // Getter for cache status
408 function getStatus () {
412 // Checks wether the current cache file is readable
413 function isCacheReadable () {
414 return INCLUDE_READABLE($this->inc);
419 // Destroy the cache on extension changes
420 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE ($data) {
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();
430 // Destroy the cache on changing admin
431 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
433 if ($GLOBALS['cache_instance']->loadCacheFile("admins")) $GLOBALS['cache_instance']->destroyCacheFile();
436 // Destroy all cache files
437 function FILTER_CACHE_DESTROY_ALL () {
438 // Remove cache files
439 if ($GLOBALS['cache_instance']->loadCacheFile("admins")) $GLOBALS['cache_instance']->destroyCacheFile();
440 if ($GLOBALS['cache_instance']->loadCacheFile("admins_acls")) $GLOBALS['cache_instance']->destroyCacheFile();
441 if ($GLOBALS['cache_instance']->loadCacheFile("config")) $GLOBALS['cache_instance']->destroyCacheFile();
442 if ($GLOBALS['cache_instance']->loadCacheFile("extensions")) $GLOBALS['cache_instance']->destroyCacheFile();
443 if ($GLOBALS['cache_instance']->loadCacheFile("modreg")) $GLOBALS['cache_instance']->destroyCacheFile();
444 if ($GLOBALS['cache_instance']->loadCacheFile("refdepths")) $GLOBALS['cache_instance']->destroyCacheFile();
445 if ($GLOBALS['cache_instance']->loadCacheFile("refsystem")) $GLOBALS['cache_instance']->destroyCacheFile();
446 if ($GLOBALS['cache_instance']->loadCacheFile("themes")) $GLOBALS['cache_instance']->destroyCacheFile();
447 if ($GLOBALS['cache_instance']->loadCacheFile("revision")) $GLOBALS['cache_instance']->destroyCacheFile();
450 // Filter for purging entire admin menu cache
451 function FILTER_CACHE_PURGE_ADMIN_MENU () {
452 // Just call the function
453 CACHE_PURGE_ADMIN_MENU();