d5b3639477adb24ff08b65b35794b764c33a3bc3
[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                         // Remove cache file from system
247                         unlink($this->cache_inc);
248                         if (!FILE_READABLE($this->cache_inc)) {
249                                 // Close cache automatically (we don't need it anymore!)
250                                 $this->cache_close();
251                         } else {
252                                 // Not removed!
253                                 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
254                         }
255                 } else {
256                         // Does not exist!
257                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
258                 }
259         }
260
261         function cache_remove($search, $data, $array) {
262                 global $ARRAY;
263                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
264                         // Load cache into dummy array
265                         $dummy = $this->cache_load();
266
267                         // Search for key in array
268                         $key = array_search($data, $dummy[$search]);
269                         if (!empty($key)) {
270                                 // Key (hopefully) found?
271                                 foreach ($array as $a) {
272                                         // So we can remove all elements as requested
273                                         unset($dummy[$a][$key]);
274                                 }
275
276                                 // Flush array to cache file
277                                 $this->cache_init($ARRAY);
278
279                                 // Write array out
280                                 $this->cache_write_array($dummy);
281
282                                 // Close cache file
283                                 $this->cache_close();
284                         }
285                 } else {
286                         // Cannot write to cache!
287                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
288                 }
289         }
290
291         function cache_write_array ($array) {
292                 if (is_resource($this->cache_pointer)) {
293                         foreach ($array as $k => $v) {
294                                 if (is_array($v)) {
295                                         // Multi line(s) found
296                                         $LINE = "";
297                                         foreach($v as $k2 => $v2) {
298                                                 // Put every array element in a row...
299                                                 $LINE .= $this->add_raw_row($k, $v2);
300                                         }
301                                 } else {
302                                         // Single line found
303                                         $LINE = $this->add_raw_row($k, $v);
304                                 }
305
306                                 // Write line(s)
307                                 fwrite($this->cache_pointer, $LINE);
308                         } // END - foreach
309                 } else {
310                         // Cannot write array!
311                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
312                 }
313         }
314
315         function cache_replace($search, $replace, $search_key, $array) {
316                 global $ARRAY;
317                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
318                         // Load cache into dummy array
319                         $dummy = $this->cache_load();
320
321                         // Check if $dummy is valid (prevents some errors)
322                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
323                                 // Search for key in array
324                                 $key_found = array_key_exists($search_key, $dummy[$search]);
325                                 if ($key_found == true) {
326                                         $key = $search_key;
327                                         // Key (hopefully) found?
328                                         foreach ($dummy as $a => $v) {
329                                                 // So we can update all entries
330                                                 if ($a == $search) {
331                                                         // Update now...
332                                                         $dummy[$a][$search_key] = $replace;
333                                                 }
334                                         }
335
336                                         // Flush array to cache file
337                                         $this->cache_init($ARRAY);
338
339                                         // Write array out
340                                         $this->cache_write_array($dummy);
341
342                                         // Close cache file
343                                         $this->cache_close();
344                                 }
345                         }
346                 } else {
347                         // Cannot write to cache!
348                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
349                 }
350         }
351
352         function store_extension_version ($ext_name) {
353                 // Valid cache pointer?
354                 if (is_resource($this->cache_pointer)) {
355                         // Get extension version
356                         $ext_ver = GET_EXT_VERSION($ext_name);
357
358                         // Write cache line to file
359                         fwrite($this->cache_pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
360                 } else {
361                         // Cannot create file
362                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
363                 }
364         }
365
366         function ext_version_matches ($ext_name) {
367                 // Load cache (dummy)
368                 $this->cache_load();
369
370                 // Get extension version
371                 $ext_ver = GET_EXT_VERSION($ext_name);
372
373                 // Compare both
374                 return ((isset($this->cache_version[$this->cache_file][$ext_name])) && ($this->cache_version[$this->cache_file][$ext_name] == $ext_ver));
375         }
376
377         function add_raw_row ($key, $value) {
378                 // Init line
379                 $line = "";
380
381                 // String or non-string? ;-)
382                 if ((is_string($value)) || (is_null($value))) {
383                         // String...
384                         $line = "\$data['".$key."'][] = \"".$value."\";\n";
385                 } else {
386                         // Non-string
387                         $line = "\$data['".$key."'][] = ".$value.";\n";
388                 }
389
390                 // Return line
391                 return $line;
392         }
393
394         function getStatus () {
395                 return $this->ret;
396         }
397 }
398 //
399 ?>