c2b92decb910d1aae6054724d6bc358b5636fdcb
[mailer.git] / inc / libs / cache_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/11/2003 *
4  * ===============                              Last change: 10/11/2003 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : admins_functions.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for the admins extension               *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer die admins-Erweiterung           *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
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.                                  *
22  *                                                                      *
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.                         *
27  *                                                                      *
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,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Caching class
41 class mxchange_cache
42 {
43         // Define variables
44         var $update_interval = 0;
45         var $ret = "init";
46         var $cache_path = "";
47         var $cache_inc = "";
48         var $cache_ctime = 0;
49         var $cache_pointer = false;
50         var $cache_data = "";
51         var $cache_version = "";
52         var $cache_file = "";
53
54         // Constructor
55         function mxchange_cache($interval, $path, $tested) {
56                 // Failed is the default
57                 $this->ret = "failed";
58
59                 // Remember interval in class
60                 $this->update_interval = $interval;
61
62                 // Remeber path
63                 $this->cache_path = $path;
64
65                 // Check if path exists
66                 if ((is_dir($path)) && (!$tested)) {
67                         // Check if we can create a file inside the path
68                         touch($path."dummy.tmp", 'w');
69                         if (FILE_READABLE($path."dummy.tmp")) {
70                                 // Yes, we can do. So let's remove it
71                                 unlink($path."dummy.tmp");
72
73                                 // Is there a .htaccess file?
74                                 if (FILE_READABLE($path.".htaccess")) {
75                                         // Update database that we have tested it
76                                         UPDATE_CONFIG("cache_tested", 1);
77
78                                         // All done!
79                                         $this->ret = "done";
80                                 } else {
81                                         // Stop! Set a .htaccess file first
82                                         $this->ret = "htaccess";
83                                 }
84                         }
85                 } elseif ($tested) {
86                         // System already tested
87                         $this->ret = "done";
88                 }
89         }
90
91         // Checks validity of cache file and if content is given
92         function cache_file ($file, $forceContent = false) {
93                 // Remember cache file
94                 $this->cache_file = $file;
95
96                 // Construct FQFN (full qualified file name)
97                 $inc = $this->cache_path.$file.".cache";
98
99                 // Rember it + filename in class
100                 $this->cache_inc = $inc;
101
102                 // Check if file exists
103                 $status = (FILE_READABLE($inc) && (is_writeable($inc)));
104                 if ($status) {
105                         // Yes, it does. So let's get it's last changed date/time
106                         $ctime = filectime($inc);
107                 } else {
108                         // No, it doesn't. Zero date/time
109                         $ctime = 0;
110                 }
111
112                 // Remember change date/time in class
113                 $this->cache_ctime = $ctime;
114
115                 // Is the cache file outdated?
116                 if (($status) && ((time() - $ctime) >= $this->update_interval)) {
117                         // Ok, we need an update!
118                         $status = false;
119                 } elseif ($status) {
120                         // Check on 'cache' extension
121                         $status = $this->ext_version_matches("cache");
122                 }
123
124                 // Return status
125                 return $status;
126         }
127
128         function cache_init($array) {
129                 // This will destory an existing cache file!
130                 if ($this->ret == "done") {
131                         // Create file
132                         if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);
133                         $this->cache_pointer = fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
134
135                         // Begin of cache file
136                         fwrite($this->cache_pointer, "<?php\n\$ARRAY = \"".$array."\";\n\n");
137
138                         // Add default depency
139                         $this->store_extension_version("cache");
140                 } else {
141                         // Cannot create file
142                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
143                 }
144         }
145
146         function add_row ($data) {
147                 global $cacheArray;
148
149                 if (is_resource($this->cache_pointer)) {
150                         // Write every array element to cache file
151                         foreach ($data as $k => $v) {
152                                 // Write global cache array for immediate access
153                                 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
154                                         if ($k != "ext_name") {
155                                                 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
156                                         } else {
157                                                 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
158                                         }
159                                         if (($k == "ext_keep") && ($v == "Y")) {
160                                                 $cacheArray['active_extensions'][$data['ext_name']] = $v;
161                                         } // END - if
162                                 } elseif (is_array($v)) {
163                                         // Serialize and BASE64-encode the array
164                                         $v = base64_encode(serialize($v));
165                                 }
166
167                                 // Write cache line to file
168                                 fwrite($this->cache_pointer, $this->add_raw_row($k, $v));
169                         }
170                 } else {
171                         // Cannot create file
172                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
173                 }
174         }
175
176         function cache_close() {
177                 // Quit function when no pointer is set
178                 if (empty($this->cache_pointer)) return;
179                 if (is_resource($this->cache_pointer)) {
180                         // Write footer
181                         fwrite($this->cache_pointer, "?>\n");
182
183                         // Close file add destroy handler
184                         fclose($this->cache_pointer);
185
186                         // Set rights
187                         if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);
188
189                         // Remove pointer
190                         unset($this->cache_pointer);
191                 } else {
192                         // Cannot create file
193                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
194                 }
195         }
196
197         function cache_load() {
198                 // Is the cache already loaded?
199                 if (isset($this->cache_data[$this->cache_file])) {
200                         // Return it's content!
201                         return $this->cache_data[$this->cache_file];
202                 } // END - if
203
204                 // Is the cache file there?
205                 if (FILE_READABLE($this->cache_inc)) {
206                         // Prepare temporary array
207                         $data = array();
208                         $cache_version = null;
209
210                         // Load cache file
211                         require_once($this->cache_inc);
212
213                         // Is there an array?
214                         if (is_array($data)) {
215                                 // Cache data
216                                 $this->cache_data[$this->cache_file] = $data;
217
218                                 // Cache version found?
219                                 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
220                                         // Remember it as well...
221                                         $this->cache_version[$this->cache_file] = $cache_version;
222                                 } else {
223                                         // Invalid cache so destroy it
224                                         $this->cache_destroy();
225
226                                         // Clear cached data
227                                         $this->cache_data[$this->cache_file] = array();
228                                 }
229
230                                 // Return cache
231                                 return $this->cache_data[$this->cache_file];
232                         } else {
233                                 // Cache problem detected!
234                                 $this->cache_destroy();
235                         }
236                 } else {
237                         // Cache file not found or not readable
238                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
239                 }
240         }
241
242         // Destroy an existing cache file
243         function cache_destroy() {
244                 // Is the cache file there?
245                 if (FILE_READABLE($this->cache_inc)) {
246                         // Close cache
247                         $this->close_cache();
248
249                         // Remove cache file from system
250                         unlink($this->cache_inc);
251
252                         // Is the file there?
253                         if (!FILE_READABLE($this->cache_inc)) {
254                                 // The cache does no longer exist so kill the content
255                                 unset($this->cache_data[$this->cache_file]);
256                         } else {
257                                 // Not removed!
258                                 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
259                         }
260                 } else {
261                         // Does not exist!
262                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
263                 }
264         }
265
266         function cache_remove($search, $data, $array) {
267                 global $ARRAY;
268                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
269                         // Load cache into dummy array
270                         $dummy = $this->cache_load();
271
272                         // Search for key in array
273                         $key = array_search($data, $dummy[$search]);
274                         if (!empty($key)) {
275                                 // Key (hopefully) found?
276                                 foreach ($array as $a) {
277                                         // So we can remove all elements as requested
278                                         unset($dummy[$a][$key]);
279                                 }
280
281                                 // Flush array to cache file
282                                 $this->cache_init($ARRAY);
283
284                                 // Write array out
285                                 $this->cache_write_array($dummy);
286
287                                 // Close cache file
288                                 $this->cache_close();
289                         }
290                 } else {
291                         // Cannot write to cache!
292                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
293                 }
294         }
295
296         function cache_write_array ($array) {
297                 if (is_resource($this->cache_pointer)) {
298                         foreach ($array as $k => $v) {
299                                 if (is_array($v)) {
300                                         // Multi line(s) found
301                                         $LINE = "";
302                                         foreach($v as $k2 => $v2) {
303                                                 // Put every array element in a row...
304                                                 $LINE .= $this->add_raw_row($k, $v2);
305                                         }
306                                 } else {
307                                         // Single line found
308                                         $LINE = $this->add_raw_row($k, $v);
309                                 }
310
311                                 // Write line(s)
312                                 fwrite($this->cache_pointer, $LINE);
313                         } // END - foreach
314                 } else {
315                         // Cannot write array!
316                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
317                 }
318         }
319
320         function cache_replace($search, $replace, $search_key, $array) {
321                 global $ARRAY;
322                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
323                         // Load cache into dummy array
324                         $dummy = $this->cache_load();
325
326                         // Check if $dummy is valid (prevents some errors)
327                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
328                                 // Search for key in array
329                                 $key_found = array_key_exists($search_key, $dummy[$search]);
330                                 if ($key_found == true) {
331                                         $key = $search_key;
332                                         // Key (hopefully) found?
333                                         foreach ($dummy as $a => $v) {
334                                                 // So we can update all entries
335                                                 if ($a == $search) {
336                                                         // Update now...
337                                                         $dummy[$a][$search_key] = $replace;
338                                                 }
339                                         }
340
341                                         // Flush array to cache file
342                                         $this->cache_init($ARRAY);
343
344                                         // Write array out
345                                         $this->cache_write_array($dummy);
346
347                                         // Close cache file
348                                         $this->cache_close();
349                                 }
350                         }
351                 } else {
352                         // Cannot write to cache!
353                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
354                 }
355         }
356
357         function store_extension_version ($ext_name) {
358                 // Valid cache pointer?
359                 if (is_resource($this->cache_pointer)) {
360                         // Get extension version
361                         $ext_ver = GET_EXT_VERSION($ext_name);
362
363                         // Write cache line to file
364                         fwrite($this->cache_pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
365                 } else {
366                         // Cannot create file
367                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
368                 }
369         }
370
371         function ext_version_matches ($ext_name) {
372                 // Load cache (dummy)
373                 $this->cache_load();
374
375                 // Get extension version
376                 $ext_ver = GET_EXT_VERSION($ext_name);
377
378                 // Compare both
379                 return ((isset($this->cache_version[$this->cache_file][$ext_name])) && ($this->cache_version[$this->cache_file][$ext_name] == $ext_ver));
380         }
381
382         function add_raw_row ($key, $value) {
383                 // Init line
384                 $line = "";
385
386                 // String or non-string? ;-)
387                 if ((is_string($value)) || (is_null($value))) {
388                         // String...
389                         $line = "\$data['".$key."'][] = \"".$value."\";\n";
390                 } else {
391                         // Non-string
392                         $line = "\$data['".$key."'][] = ".$value.";\n";
393                 }
394
395                 // Return line
396                 return $line;
397         }
398
399         function getStatus () {
400                 return $this->ret;
401         }
402 }
403 //
404 ?>