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";
52 function CacheSystem ($interval, $path, $tested) {
53 // Failed is the default
54 $this->ret = "failed";
59 // Check if path exists
60 if ((is_dir($path)) && (!$tested)) {
61 // Check if we can create a file inside the path
62 touch($path."dummy.tmp", 'w');
63 if (FILE_READABLE($path."dummy.tmp")) {
64 // Yes, we can do. So let's remove it
65 unlink($path."dummy.tmp");
67 // Is there a .htaccess file?
68 if (FILE_READABLE($path.".htaccess")) {
69 // Update database that we have tested it
70 UPDATE_CONFIG("cache_tested", 1);
75 // Stop! Set a .htaccess file first
76 $this->ret = "htaccess";
80 // System already tested
85 // Checks validity of cache file and if content is given
86 function loadCacheFile ($file, $forceContent = false) {
87 // Remember cache file
90 // Construct FQFN (full qualified file name)
91 $this->inc = $this->path.$file.".cache";
93 // Check if file exists and if version matches
94 $status = (FILE_READABLE($this->inc) && (is_writeable($this->inc)) && ($this->extensionVersionMatches("cache")));
101 // This will destory an existing cache file!
102 if ($this->ret == "done") {
104 if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
105 $this->pointer = fopen($this->inc, 'w') or mxchange_die("Cannot write to cache ".$this->inc." !");
108 fwrite($this->pointer, "<?php\n");
110 // Add default depency
111 $this->storeExtensionVersion("cache");
113 // Cannot create file
114 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
118 function addRow ($data) {
121 if (is_resource($this->pointer)) {
122 // Write every array element to cache file
123 foreach ($data as $k => $v) {
124 // Write global cache array for immediate access
125 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
126 if ($k != "ext_name") {
127 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
129 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
131 if (($k == "ext_keep") && ($v == "Y")) {
132 $cacheArray['active_extensions'][$data['ext_name']] = $v;
134 } elseif (is_array($v)) {
135 // Serialize and BASE64-encode the array
136 $v = base64_encode(serialize($v));
139 // Write cache line to file
140 fwrite($this->pointer, $this->rewriteEntry($k, $v));
143 // Cannot create file
144 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
148 function finalize () {
149 // Quit function when no pointer is set
150 if (is_resource($this->pointer)) {
152 fwrite($this->pointer, "?>\n");
154 // Close file add destroy handler
155 fclose($this->pointer);
158 if (FILE_READABLE($this->inc)) chmod($this->inc, 0666);
161 $this->pointer = false;
163 // Cannot create file
164 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
168 function getArrayFromCache () {
169 // Is the cache already loaded?
170 if (isset($this->data[$this->name])) {
171 // Return it's content!
172 return $this->data[$this->name];
175 // Is the cache file there?
176 if (FILE_READABLE($this->inc)) {
177 // Prepare temporary array
179 $cache_version = null;
182 require_once($this->inc);
184 // Is there an array?
185 if (is_array($data)) {
187 $this->data[$this->name] = $data;
189 // Cache version found?
190 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
191 // Remember it as well...
192 $this->version[$this->name] = $cache_version;
194 // Invalid cache so destroy it
195 $this->destroyCacheFile();
198 $this->data[$this->name] = array();
202 return $this->data[$this->name];
204 // Cache problem detected!
205 $this->destroyCacheFile();
208 // Cache file not found or not readable
209 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->inc.CACHE_CANNOT_LOAD_2);
213 // Destroy an existing cache file
214 function destroyCacheFile () {
215 // Is the cache file there?
216 if (FILE_READABLE($this->inc)) {
220 // Remove cache file from system
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]);
229 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->inc.CACHE_CANNOT_UNLINK_2);
233 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
238 function removeEntry ($search, $data, $array) {
239 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
240 // Load cache into dummy array
241 $dummy = $this->getArrayFromCache();
243 // Search for key in array
244 $key = array_search($data, $dummy[$search]);
246 // Key (hopefully) found?
247 foreach ($array as $a) {
248 // So we can remove all elements as requested
249 unset($dummy[$a][$key]);
252 // Flush array to cache file
256 $this->writeArray($dummy);
262 // Cannot write to cache!
263 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
267 function writeArray ($array) {
268 if (is_resource($this->pointer)) {
269 foreach ($array as $k => $v) {
271 // Multi line(s) found
273 foreach($v as $k2 => $v2) {
274 // Put every array element in a row...
275 $LINE .= $this->rewriteEntry($k, $v2);
279 $LINE = $this->rewriteEntry($k, $v);
283 fwrite($this->pointer, $LINE);
286 // Cannot write array!
287 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
292 function replaceEntry ($search, $replace, $search_key, $array) {
293 if ((FILE_READABLE($this->inc)) && (is_writeable($this->inc))) {
294 // Load cache into dummy array
295 $dummy = $this->getArrayFromCache();
297 // Check if $dummy is valid (prevents some errors)
298 if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
299 // Search for key in array
300 $key_found = array_key_exists($search_key, $dummy[$search]);
301 if ($key_found == true) {
303 // Key (hopefully) found?
304 foreach ($dummy as $a => $v) {
305 // So we can update all entries
308 $dummy[$a][$search_key] = $replace;
312 // Flush array to cache file
316 $this->writeArray($dummy);
323 // Cannot write to cache!
324 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
328 function storeExtensionVersion ($ext_name) {
329 // Valid cache pointer?
330 if (is_resource($this->pointer)) {
331 // Get extension version
332 $ext_ver = GET_EXT_VERSION($ext_name);
334 // Write cache line to file
335 fwrite($this->pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
337 // Cannot create file
338 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
342 function extensionVersionMatches ($ext_name) {
343 // Load cache (dummy)
344 $this->getArrayFromCache();
346 // Get extension version
347 $ext_ver = GET_EXT_VERSION($ext_name);
350 return ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
353 function rewriteEntry ($key, $value) {
357 // String or non-string? ;-)
358 if (is_string($value)) {
360 $line = "\$data['".$key."'][] = \"".$value."\";\n";
361 } elseif (is_null($value)) {
363 $line = "\$data['".$key."'][] = null;\n";
364 } elseif (is_bool($value)) {
366 if ($value === true) {
367 $line = "\$data['".$key."'][] = true;\n";
369 $line = "\$data['".$key."'][] = false;\n";
373 $line = "\$data['".$key."'][] = ".$value.";\n";
380 function getStatus () {