88b506223d9f6fdd1ecf162e97de9a05ddf664ad
[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                 } // END - if
120
121                 // Return status
122                 return $status;
123         }
124
125         function cache_init($array) {
126                 // This will destory an existing cache file!
127                 if ($this->ret == "done") {
128                         // Create file
129                         if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
130                         $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
131
132                         // Begin of cache file
133                         fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
134
135                         // Remember file pointer
136                         $this->cache_pointer = $fp;
137                 } else {
138                         // Cannot create file
139                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
140                 }
141         }
142
143         function add_row ($data) {
144                 global $cacheArray;
145
146                 if (is_resource($this->cache_pointer)) {
147                         // Write every array element to cache file
148                         foreach ($data as $k => $v) {
149                                 // Write global cache array for immediate access
150                                 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
151                                         if ($k != "ext_name") {
152                                                 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
153                                         } else {
154                                                 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
155                                         }
156                                         if (($k == "ext_keep") && ($v == "Y")) {
157                                                 $cacheArray['active_extensions'][$data['ext_name']] = $v;
158                                         } // END - if
159                                 } elseif (is_array($v)) {
160                                         // Serialize and BASE64-encode the array
161                                         $v = base64_encode(serialize($v));
162                                 }
163
164                                 // Write cache line to file
165                                 @fwrite($this->cache_pointer, $this->add_raw_row($k, $v));
166                         }
167                 } else {
168                         // Cannot create file
169                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
170                 }
171         }
172
173         function cache_close()
174         {
175                 // Quit function when no pointer is set
176                 if (empty($this->cache_pointer)) return;
177                 if ($this->cache_pointer)
178                 {
179                         // Close file add destroy handler
180                         @fclose($this->cache_pointer);
181
182                         // Set rights
183                         if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
184
185                         // Remove pointer
186                         unset($this->cache_pointer);
187                 }
188                 else
189                 {
190                         // Cannot create file
191                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
192                 }
193         }
194
195         function cache_load() {
196                 // Is the cache file there?
197                 if (FILE_READABLE($this->cache_inc)) {
198                         // Prepare temporary array
199                         $data = array();
200                         $cache_version = null;
201
202                         // Load cache file
203                         $this->cache_data[$this->cache_file] = implode("", file($this->cache_inc));
204
205                         // Execute cache file
206                         eval($this->cache_data[$this->cache_file]);
207
208                         if (is_array($data)) {
209                                 // Cache data
210                                 $this->cache_data[$this->cache_file] = $data;
211
212                                 // Cache version found?
213                                 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
214                                         // Remember it as well...
215                                         $this->cache_version[$this->cache_file] = $cache_version;
216                                 } else {
217                                         // Invalid cache so destroy it
218                                         $this->cache_destroy();
219
220                                         // Clear cached data
221                                         $this->cache_data[$this->cache_file] = array();
222                                 }
223
224                                 // Return cache
225                                 return $this->cache_data[$this->cache_file];
226                         } else {
227                                 // Cache problem detected!
228                                 $this->cache_destroy();
229                         }
230                 } else {
231                         // Cache file not found or not readable
232                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
233                 }
234         }
235
236         // Destroy an existing cache file
237         function cache_destroy() {
238                 // Is the cache file there?
239                 if (FILE_READABLE($this->cache_inc)) {
240                         // Remove cache file from system
241                         @unlink($this->cache_inc);
242                         if (!FILE_READABLE($this->cache_inc)) {
243                                 // Close cache automatically (we don't need it anymore!)
244                                 $this->cache_close();
245                         } else {
246                                 // Not removed!
247                                 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
248                         }
249                 } else {
250                         // Does not exist!
251                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
252                 }
253         }
254
255         function cache_remove($search, $data, $array) {
256                 global $ARRAY;
257                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
258                         // Load cache into dummy array
259                         $dummy = $this->cache_load();
260
261                         // Search for key in array
262                         $key = array_search($data, $dummy[$search]);
263                         if (!empty($key)) {
264                                 // Key (hopefully) found?
265                                 foreach ($array as $a) {
266                                         // So we can remove all elements as requested
267                                         unset($dummy[$a][$key]);
268                                 }
269
270                                 // Flush array to cache file
271                                 $fp = fopen($this->cache_inc, 'w');
272                                 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
273                                 foreach ($dummy as $k => $v) {
274                                         if (is_array($v)) {
275                                                 // Multi line(s) found
276                                                 $LINE = "";
277                                                 foreach($v as $k2 => $v2) {
278                                                         // Put every array element in a row...
279                                                         $LINE .= $this->add_raw_row($k, $v2);
280                                                 }
281                                         } else {
282                                                 // Single line found
283                                                 $LINE = $this->add_raw_row($k, $v);
284                                         }
285
286                                         // Write line(s)
287                                         fwrite($fp, $LINE);
288                                 }
289
290                                 // Close cache file
291                                 fclose($fp);
292                         }
293                 } else {
294                         // Cannot write to cache!
295                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
296                 }
297         }
298
299         function cache_replace($search, $replace, $search_key, $array) {
300                 global $ARRAY;
301                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc)))
302                 {
303                         // Load cache into dummy array
304                         $dummy = $this->cache_load();
305
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) {
311                                         $key = $search_key;
312                                         // Key (hopefully) found?
313                                         foreach ($dummy as $a => $v) {
314                                                 // So we can update all entries
315                                                 if ($a == $search) {
316                                                         // Update now...
317                                                         $dummy[$a][$search_key] = $replace;
318                                                 }
319                                         }
320
321                                         // Flush array to cache file
322                                         $fp = fopen($this->cache_inc, 'w');
323                                         fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
324                                         foreach ($dummy as $k => $v) {
325                                                 if (is_array($v)) {
326                                                         // Multi line(s) found
327                                                         $LINE = "";
328                                                         foreach($v as $k2 => $v2) {
329                                                                 // Put every array element in a row...
330                                                                 $LINE .= $this->add_raw_row($k, $v2);
331                                                         }
332                                                 } else {
333                                                         // Single line found
334                                                         $LINE = $this->add_raw_row($k, $v);
335                                                 }
336
337                                                 // Write line(s)
338                                                 fwrite($fp, $LINE);
339                                         }
340
341                                         // Close cache file
342                                         fclose($fp);
343                                 }
344                         }
345                 } else {
346                         // Cannot write to cache!
347                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
348                 }
349         }
350
351         function store_extension_version ($ext_name) {
352                 // Valid cache pointer?
353                 if (is_resource($this->cache_pointer)) {
354                         // Get extension version
355                         $ext_ver = GET_EXT_VERSION($ext_name);
356
357                         // Write cache line to file
358                         @fwrite($this->cache_pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
359                 } else {
360                         // Cannot create file
361                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
362                 }
363         }
364
365         function ext_version_matches ($ext_name) {
366                 // Load cache (dummy)
367                 $this->cache_load();
368
369                 // Get extension version
370                 $ext_ver = GET_EXT_VERSION($ext_name);
371
372                 // Compare both
373                 return ((isset($this->cache_version[$this->cache_file][$ext_name])) && ($this->cache_version[$this->cache_file][$ext_name] == $ext_ver));
374         }
375
376         function add_raw_row ($key, $value) {
377                 // Init line
378                 $line = "";
379
380                 // String or non-string? ;-)
381                 if ((is_string($value)) || (is_null($value))) {
382                         // String...
383                         $line = "\$data['".$key."'][] = \"".$value."\";\n";
384                 } else {
385                         // Non-string
386                         $line = "\$data['".$key."'][] = ".$value.";\n";
387                 }
388
389                 // Return line
390                 return $line;
391         }
392
393         function getStatus () {
394                 return $this->ret;
395         }
396 }
397 //
398 ?>