29cb7aea70fcfff6007293932af40f9fbc54c801
[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         // Define variables
43         var $ret = "init";
44         var $cache_path = "";
45         var $cache_inc = "";
46         var $cache_pointer = false;
47         var $cache_data = "";
48         var $cache_version = "";
49         var $cache_file = "";
50
51         // Constructor
52         function mxchange_cache($interval, $path, $tested) {
53                 // Failed is the default
54                 $this->ret = "failed";
55
56                 // Remeber path
57                 $this->cache_path = $path;
58
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");
66
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);
71
72                                         // All done!
73                                         $this->ret = "done";
74                                 } else {
75                                         // Stop! Set a .htaccess file first
76                                         $this->ret = "htaccess";
77                                 }
78                         }
79                 } elseif ($tested) {
80                         // System already tested
81                         $this->ret = "done";
82                 }
83         }
84
85         // Checks validity of cache file and if content is given
86         function cache_file ($file, $forceContent = false) {
87                 // Remember cache file
88                 $this->cache_file = $file;
89
90                 // Construct FQFN (full qualified file name)
91                 $inc = $this->cache_path.$file.".cache";
92
93                 // Rember it + filename in class
94                 $this->cache_inc = $inc;
95
96                 // Check if file exists and if version matches
97                 $status = (FILE_READABLE($inc) && (is_writeable($inc)) && ($this->ext_version_matches("cache")));
98
99                 // Return status
100                 return $status;
101         }
102
103         function cache_init($array) {
104                 // This will destory an existing cache file!
105                 if ($this->ret == "done") {
106                         // Create file
107                         if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);
108                         $this->cache_pointer = fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
109
110                         // Begin of cache file
111                         fwrite($this->cache_pointer, "<?php\n\$ARRAY = \"".$array."\";\n\n");
112
113                         // Add default depency
114                         $this->store_extension_version("cache");
115                 } else {
116                         // Cannot create file
117                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
118                 }
119         }
120
121         function add_row ($data) {
122                 global $cacheArray;
123
124                 if (is_resource($this->cache_pointer)) {
125                         // Write every array element to cache file
126                         foreach ($data as $k => $v) {
127                                 // Write global cache array for immediate access
128                                 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
129                                         if ($k != "ext_name") {
130                                                 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
131                                         } else {
132                                                 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
133                                         }
134                                         if (($k == "ext_keep") && ($v == "Y")) {
135                                                 $cacheArray['active_extensions'][$data['ext_name']] = $v;
136                                         } // END - if
137                                 } elseif (is_array($v)) {
138                                         // Serialize and BASE64-encode the array
139                                         $v = base64_encode(serialize($v));
140                                 }
141
142                                 // Write cache line to file
143                                 fwrite($this->cache_pointer, $this->add_raw_row($k, $v));
144                         }
145                 } else {
146                         // Cannot create file
147                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
148                 }
149         }
150
151         function cache_close() {
152                 // Quit function when no pointer is set
153                 if (empty($this->cache_pointer)) return;
154                 if (is_resource($this->cache_pointer)) {
155                         // Write footer
156                         fwrite($this->cache_pointer, "?>\n");
157
158                         // Close file add destroy handler
159                         fclose($this->cache_pointer);
160
161                         // Set rights
162                         if (FILE_READABLE($this->cache_inc)) chmod($this->cache_inc, 0666);
163
164                         // Remove pointer
165                         unset($this->cache_pointer);
166                 } else {
167                         // Cannot create file
168                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
169                 }
170         }
171
172         function cache_load() {
173                 // Is the cache already loaded?
174                 if (isset($this->cache_data[$this->cache_file])) {
175                         // Return it's content!
176                         return $this->cache_data[$this->cache_file];
177                 } // END - if
178
179                 // Is the cache file there?
180                 if (FILE_READABLE($this->cache_inc)) {
181                         // Prepare temporary array
182                         $data = array();
183                         $cache_version = null;
184
185                         // Load cache file
186                         require_once($this->cache_inc);
187
188                         // Is there an array?
189                         if (is_array($data)) {
190                                 // Cache data
191                                 $this->cache_data[$this->cache_file] = $data;
192
193                                 // Cache version found?
194                                 if ((is_array($cache_version)) && (count($cache_version) > 0)) {
195                                         // Remember it as well...
196                                         $this->cache_version[$this->cache_file] = $cache_version;
197                                 } else {
198                                         // Invalid cache so destroy it
199                                         $this->cache_destroy();
200
201                                         // Clear cached data
202                                         $this->cache_data[$this->cache_file] = array();
203                                 }
204
205                                 // Return cache
206                                 return $this->cache_data[$this->cache_file];
207                         } else {
208                                 // Cache problem detected!
209                                 $this->cache_destroy();
210                         }
211                 } else {
212                         // Cache file not found or not readable
213                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
214                 }
215         }
216
217         // Destroy an existing cache file
218         function cache_destroy() {
219                 // Is the cache file there?
220                 if (FILE_READABLE($this->cache_inc)) {
221                         // Close cache
222                         $this->cache_close();
223
224                         // Remove cache file from system
225                         unlink($this->cache_inc);
226
227                         // Is the file there?
228                         if (!FILE_READABLE($this->cache_inc)) {
229                                 // The cache does no longer exist so kill the content
230                                 unset($this->cache_data[$this->cache_file]);
231                         } else {
232                                 // Not removed!
233                                 ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
234                         }
235                 } else {
236                         // Does not exist!
237                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
238                 }
239         }
240
241         function cache_remove($search, $data, $array) {
242                 global $ARRAY;
243                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
244                         // Load cache into dummy array
245                         $dummy = $this->cache_load();
246
247                         // Search for key in array
248                         $key = array_search($data, $dummy[$search]);
249                         if (!empty($key)) {
250                                 // Key (hopefully) found?
251                                 foreach ($array as $a) {
252                                         // So we can remove all elements as requested
253                                         unset($dummy[$a][$key]);
254                                 }
255
256                                 // Flush array to cache file
257                                 $this->cache_init($ARRAY);
258
259                                 // Write array out
260                                 $this->cache_write_array($dummy);
261
262                                 // Close cache file
263                                 $this->cache_close();
264                         }
265                 } else {
266                         // Cannot write to cache!
267                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
268                 }
269         }
270
271         function cache_write_array ($array) {
272                 if (is_resource($this->cache_pointer)) {
273                         foreach ($array 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($this->cache_pointer, $LINE);
288                         } // END - foreach
289                 } else {
290                         // Cannot write array!
291                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
292                 }
293         }
294
295         function cache_replace($search, $replace, $search_key, $array) {
296                 global $ARRAY;
297                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
298                         // Load cache into dummy array
299                         $dummy = $this->cache_load();
300
301                         // Check if $dummy is valid (prevents some errors)
302                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
303                                 // Search for key in array
304                                 $key_found = array_key_exists($search_key, $dummy[$search]);
305                                 if ($key_found == true) {
306                                         $key = $search_key;
307                                         // Key (hopefully) found?
308                                         foreach ($dummy as $a => $v) {
309                                                 // So we can update all entries
310                                                 if ($a == $search) {
311                                                         // Update now...
312                                                         $dummy[$a][$search_key] = $replace;
313                                                 }
314                                         }
315
316                                         // Flush array to cache file
317                                         $this->cache_init($ARRAY);
318
319                                         // Write array out
320                                         $this->cache_write_array($dummy);
321
322                                         // Close cache file
323                                         $this->cache_close();
324                                 }
325                         }
326                 } else {
327                         // Cannot write to cache!
328                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
329                 }
330         }
331
332         function store_extension_version ($ext_name) {
333                 // Valid cache pointer?
334                 if (is_resource($this->cache_pointer)) {
335                         // Get extension version
336                         $ext_ver = GET_EXT_VERSION($ext_name);
337
338                         // Write cache line to file
339                         fwrite($this->cache_pointer, "\$cache_version['".$ext_name."'] = \"".$ext_ver."\";\n");
340                 } else {
341                         // Cannot create file
342                         ADD_FATAL(__FILE__."(<font color=\"#0000aa\">".__LINE__."</font>): ".CACHE_PROBLEMS_DETECTED);
343                 }
344         }
345
346         function ext_version_matches ($ext_name) {
347                 // Load cache (dummy)
348                 $this->cache_load();
349
350                 // Get extension version
351                 $ext_ver = GET_EXT_VERSION($ext_name);
352
353                 // Compare both
354                 return ((isset($this->cache_version[$this->cache_file][$ext_name])) && ($this->cache_version[$this->cache_file][$ext_name] == $ext_ver));
355         }
356
357         function add_raw_row ($key, $value) {
358                 // Init line
359                 $line = "";
360
361                 // String or non-string? ;-)
362                 if ((is_string($value)) || (is_null($value))) {
363                         // String...
364                         $line = "\$data['".$key."'][] = \"".$value."\";\n";
365                 } else {
366                         // Non-string
367                         $line = "\$data['".$key."'][] = ".$value.";\n";
368                 }
369
370                 // Return line
371                 return $line;
372         }
373
374         function getStatus () {
375                 return $this->ret;
376         }
377 }
378 //
379 ?>