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