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 ($file, $forceContent = false) {
88 // Remember cache file
91 // Construct FQFN (full qualified file name)
92 $this->inc = $this->path.$file.".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 ADD_FATAL(__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 ADD_FATAL(__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 require_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 ADD_FATAL(__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 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->inc.CACHE_CANNOT_UNLINK_2);
235 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
240 function removeEntry ($search, $data, $array) {
241 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
242 // Load cache into dummy array
243 $dummy = $this->getArrayFromCache();
245 // Search for key in array
246 $key = array_search($data, $dummy[$search]);
248 // Key (hopefully) found?
249 foreach ($array as $a) {
250 // So we can remove all elements as requested
251 unset($dummy[$a][$key]);
254 // Flush array to cache file
258 $this->writeArray($dummy);
264 // Cannot write to cache!
265 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
269 function writeArray ($array) {
270 if (is_resource($this->pointer)) {
271 foreach ($array as $k => $v) {
273 // Multi line(s) found
275 foreach($v as $k2 => $v2) {
276 // Put every array element in a row...
277 $LINE .= $this->rewriteEntry($k, $v2);
281 $LINE = $this->rewriteEntry($k, $v);
285 fwrite($this->pointer, $LINE);
288 // Cannot write array!
289 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
294 function replaceEntry ($search, $replace, $search_key, $array) {
295 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
296 // Load cache into dummy array
297 $dummy = $this->getArrayFromCache();
299 // Check if $dummy is valid (prevents some errors)
300 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
301 // Search for key in array
302 $key_found = array_key_exists($search_key, $dummy[$search]);
303 if ($key_found == true) {
305 // Key (hopefully) found?
306 foreach ($dummy as $a => $v) {
307 // So we can update all entries
310 $dummy[$a][$search_key] = $replace;
314 // Flush array to cache file
318 $this->writeArray($dummy);
325 // Cannot write to cache!
326 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
330 function storeExtensionVersion ($ext_name) {
331 // Valid cache pointer?
332 if (is_resource($this->pointer)) {
333 // Get extension version
334 $ext_ver = GET_EXT_VERSION($ext_name);
336 // Write cache line to file
337 fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
339 // Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
340 $this->version[$this->name][$ext_name] = $ext_ver;
341 //* DEBUG: */ print __METHOD__."(<font color=\"#0000aa\">".__LINE__."</font>): {$this->name} - {$ext_name}={$ext_ver}<br />\n";
343 // Cannot create file
344 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
348 function extensionVersionMatches ($ext_name) {
349 // Load cache (dummy)
350 $this->getArrayFromCache();
352 // Get extension version
353 $ext_ver = GET_EXT_VERSION($ext_name);
356 if (isset($this->version[$this->name][$ext_name])) {
357 //* 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";
359 // No cache version found!
360 DEBUG_LOG(__METHOD__, __LINE__, "Cache {$this->name} has missing entry for extension {$ext_name}!");
364 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
367 function rewriteEntry ($key, $value) {
371 // String or non-string? ;-)
372 if (is_string($value)) {
374 $line = "\$data['".$key."'][] = \"".$value."\";\n";
375 } elseif (is_null($value)) {
377 $line = "\$data['".$key."'][] = null;\n";
378 } elseif (is_bool($value)) {
380 if ($value === true) {
381 $line = "\$data['".$key."'][] = true;\n";
383 $line = "\$data['".$key."'][] = false;\n";
387 $line = "\$data['".$key."'][] = ".$value.";\n";
394 function getStatus () {
399 // Destroy the cache on extension changes
400 function FILTER_CACHE_DESTROY_ON_EXT_CHANGE ($data) {
401 global $cacheInstance;
404 if (EXT_IS_ACTIVE("cache")) {
405 if ($cacheInstance->loadCacheFile("config")) $cacheInstance->destroyCacheFile();
406 if ($cacheInstance->loadCacheFile("extensions")) $cacheInstance->destroyCacheFile();
407 if ($cacheInstance->loadCacheFile("mod_reg")) $cacheInstance->destroyCacheFile();
414 // Destroy the cache on changing admin
415 function FILTER_CACHE_DESTROY_ON_ADMIN_CHANGE () {
416 global $cacheInstance;
419 if (EXT_IS_ACTIVE("cache")) {
420 if ($cacheInstance->loadCacheFile("admins")) $cacheInstance->destroyCacheFile();
424 // Destroy all cache files
425 function FILTER_CACHE_DESTROY_ALL () {
426 global $cacheInstance;
429 if (EXT_IS_ACTIVE("cache")) {
430 if ($cacheInstance->loadCacheFile("admins")) $cacheInstance->destroyCacheFile();
431 if ($cacheInstance->loadCacheFile("config")) $cacheInstance->destroyCacheFile();
432 if ($cacheInstance->loadCacheFile("extensions")) $cacheInstance->destroyCacheFile();
433 if ($cacheInstance->loadCacheFile("mod_reg")) $cacheInstance->destroyCacheFile();
434 if ($cacheInstance->loadCacheFile("refdepths")) $cacheInstance->destroyCacheFile();
435 if ($cacheInstance->loadCacheFile("refsystem")) $cacheInstance->destroyCacheFile();
436 if ($cacheInstance->loadCacheFile("themes")) $cacheInstance->destroyCacheFile();
440 // Filter for purging entire admin menu cache
441 function FILTER_CACHE_PURGE_ADMIN_MENU () {
442 // Just call the function
443 CACHE_PURGE_ADMIN_MENU();