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