Security line in all includes changed
[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                 // Remember interval in class
56                 $this->update_interval=$interval;
57
58                 // Remeber path
59                 $this->cache_path=$path;
60
61                 // Check if path exists
62                 if ((is_dir($path)) && (!$tested)) {
63                         // Check if we can create a file inside the path
64                         @touch($path."dummy.tmp", 'w');
65                         if (FILE_READABLE($path."dummy.tmp")) {
66                                 // Yes, we can do. So let's remove it
67                                 @unlink($path."dummy.tmp");
68
69                                 // Is there a .htaccess file?
70                                 if (FILE_READABLE($path.".htaccess")) {
71                                         // Update database that we have tested it
72                                         UPDATE_CONFIG("cache_tested", 1);
73
74                                         // All done!
75                                         return "done";
76                                 } else {
77                                         // Stop! Set a .htaccess file first
78                                         $this->ret="htaccess";
79                                         return "htaccess";
80                                 }
81                         }
82                 } elseif ($tested) {
83                         // System already tested
84                         $this->ret="done";
85                         return "done";
86                 }
87
88                 // Something goes wrong here!
89                 $this->ret="failed";
90                 return "failed";
91         }
92
93         function cache_file($file, $ignore_ctime=false) {
94                 global $INC;
95                 // Construct FQFN (full qualified file name)
96                 $inc = $this->cache_path.$file.".cache";
97
98                 // Rember it + filename in class
99                 $this->cache_inc = $inc;
100
101                 // Check if file exists
102                 $status = (FILE_READABLE($inc) && (is_writeable($inc)));
103                 if ($status) {
104                         // Yes, it does. So let's get it's last changed date/time
105                         $ctime = filectime($inc);
106                 } else {
107                         // No, it doesn't. Zero date/time
108                         $ctime = 0;
109                 }
110
111                 // Remember change date/time in class
112                 $this->cache_ctime = $ctime;
113
114                 // Is the cache file outdated?
115                 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime)) {
116                         // Ok, we need an update!
117                         $status = false;
118                 }
119
120                 // Return status
121                 return $status;
122         }
123
124         function cache_init($array) {
125                 // This will destory an existing cache file!
126                 if ($this->ret == "done") {
127                         // Create file
128                         if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
129                         $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
130
131                         // Begin of cache file
132                         fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
133
134                         // Remember file pointer
135                         $this->cache_pointer = $fp;
136                 } else {
137                         // Cannot create file
138                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
139                 }
140         }
141
142         function add_row ($data) {
143                 global $cacheArray;
144
145                 if (is_resource($this->cache_pointer)) {
146                         // Write every array element to cache file
147                         foreach ($data as $k => $v) {
148                                 // Write global cache array for immediate access
149                                 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
150                                         if ($k != "ext_name") {
151                                                 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
152                                         } else {
153                                                 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
154                                         }
155                                         if (($k == "ext_keep") && ($v == "Y")) {
156                                                 $cacheArray['active_extensions'][$data['ext_name']] = $v;
157                                         } // END - if
158                                 } elseif (is_array($v)) {
159                                         // Serialize and BASE64-encode the array
160                                         $v = base64_encode(serialize($v));
161                                 }
162
163                                 // Write cache line to file
164                                 @fwrite($this->cache_pointer, $this->add_raw_row($k, $v));
165                         }
166                 } else {
167                         // Cannot create file
168                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
169                 }
170         }
171
172         function cache_close()
173         {
174                 // Quit function when no pointer is set
175                 if (empty($this->cache_pointer)) return;
176                 if ($this->cache_pointer)
177                 {
178                         // Close file add destroy handler
179                         @fclose($this->cache_pointer);
180
181                         // Set rights
182                         if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
183
184                         // Remove pointer
185                         unset($this->cache_pointer);
186                 }
187                 else
188                 {
189                         // Cannot create file
190                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
191                 }
192         }
193
194         function cache_load() {
195                 // Is the cache file there?
196                 if (FILE_READABLE($this->cache_inc)) {
197                         // Prepare temporary array
198                         $data = array();
199                         $cache_version = null;
200
201                         // Load cache file
202                         $this->cache_data = implode("", file($this->cache_inc));
203
204                         // Execute cache file
205                         eval($this->cache_data);
206
207                         if (is_array($data)) {
208                                 // Cache data
209                                 $this->cache_data = $data;
210
211                                 // Cache version found?
212                                 if ((isset($cache_version)) && (is_array($cache_version))) {
213                                         // Remember it as well...
214                                         $this->cache_version = $cache_version;
215                                 } // END - if
216
217                                 // Return cache
218                                 return $this->cache_data;
219                         } else {
220                                 // Cache problem detected!
221                                 $this->cache_destroy();
222                         }
223                 } else {
224                         // Cache file not found or not readable
225                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
226                 }
227         }
228
229         function cache_destroy()
230         {
231                 if (FILE_READABLE($this->cache_inc))
232                 {
233                         // Remove cache file from system
234                         @unlink($this->cache_inc);
235                         if (!FILE_READABLE($this->cache_inc))
236                         {
237                                 // Close cache automatically (we don't need it anymore!)
238                                 $this->cache_close();
239                         }
240                         else
241                         {
242                                 // Not removed!
243                                 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
244                         }
245                 }
246                 else
247                 {
248                         // Does not exist!
249                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
250                 }
251         }
252
253         function cache_remove($search, $data, $array)
254         {
255                 global $ARRAY;
256                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc))) {
257                         // Load cache into dummy array
258                         $dummy = $this->cache_load();
259
260                         // Search for key in array
261                         $key = array_search($data, $dummy[$search]);
262                         if (!empty($key)) {
263                                 // Key (hopefully) found?
264                                 foreach ($array as $a) {
265                                         // So we can remove all elements as requested
266                                         unset($dummy[$a][$key]);
267                                 }
268
269                                 // Flush array to cache file
270                                 $fp = fopen($this->cache_inc, 'w');
271                                 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
272                                 foreach ($dummy as $k => $v) {
273                                         if (is_array($v)) {
274                                                 // Multi line(s) found
275                                                 $LINE = "";
276                                                 foreach($v as $k2 => $v2) {
277                                                         // Put every array element in a row...
278                                                         $LINE .= $this->add_raw_row($k, $v2);
279                                                 }
280                                         } else {
281                                                 // Single line found
282                                                 $LINE = $this->add_raw_row($k, $v);
283                                         }
284
285                                         // Write line(s)
286                                         fwrite($fp, $LINE);
287                                 }
288
289                                 // Close cache file
290                                 fclose($fp);
291                         }
292                 } else {
293                         // Cannot write to cache!
294                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
295                 }
296         }
297
298         function cache_replace($search, $replace, $search_key, $array)
299         {
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__."(".__LINE__."): ".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__."(".__LINE__."): ".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                 //* DEBUG: */ echo __METHOD__.": ext_name={$ext_name},ext_ver={$ext_ver},cache_version={$this->cache_version}<br />\n";
373                 // Compare both
374                 return ((isset($this->cache_version[$ext_name])) && ($this->cache_version[$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)) {
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 //
395 ?>