e0c3fec20cd0b42090b05bc01ce33b78f94ffb07
[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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
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
51         // Constructor
52         function mxchange_cache($interval, $path, $tested) {
53                 // Remember interval in class
54                 $this->update_interval=$interval;
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                                         return "done";
74                                 } else {
75                                         // Stop! Set a .htaccess file first
76                                         $this->ret="htaccess";
77                                         return "htaccess";
78                                 }
79                         }
80                 } elseif ($tested) {
81                         // System already tested
82                         $this->ret="done";
83                         return "done";
84                 }
85
86                 // Something goes wrong here!
87                 $this->ret="failed";
88                 return "failed";
89         }
90
91         function cache_file($file, $ignore_ctime=false) {
92                 global $INC;
93                 // Construct FQFN (full qualified file name)
94                 $inc = $this->cache_path.$file.".cache";
95
96                 // Rember it + filename in class
97                 $this->cache_inc = $inc;
98
99                 // Check if file exists
100                 $status = (FILE_READABLE($inc) && (is_writeable($inc)));
101                 if ($status) {
102                         // Yes, it does. So let's get it's last changed date/time
103                         $ctime = filectime($inc);
104                 } else {
105                         // No, it doesn't. Zero date/time
106                         $ctime = "0";
107                 }
108
109                 // Remember change date/time in class
110                 $this->cache_ctime = $ctime;
111
112                 // Is the cache file outdated?
113                 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime)) {
114                         // Ok, we need an update!
115                         $status = false;
116                 }
117
118                 // Return status
119                 return $status;
120         }
121
122         function cache_init($array) {
123                 // This will destory an existing cache file!
124                 if ($this->ret == "done") {
125                         // Create file
126                         if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
127                         $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
128
129                         // Begin of cache file
130                         fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
131
132                         // Remember file pointer
133                         $this->cache_pointer = $fp;
134                 } else {
135                         // Cannot create file
136                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
137                 }
138         }
139
140         function add_row ($data) {
141                 global $cacheArray;
142
143                 if (is_resource($this->cache_pointer)) {
144                         // Write every array element to cache file
145                         foreach ($data as $k => $v) {
146                                 // Write global cache array for immediate access
147                                 if ((substr($k, 0, 4) == "ext_") && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
148                                         if ($k != "ext_name") {
149                                                 $cacheArray['extensions'][$k][$data['ext_name']] = $v;
150                                         } else {
151                                                 $cacheArray['extensions'][$k][$data['ext_id']] = $v;
152                                         }
153                                         if (($k == "ext_keep") && ($v == "Y")) {
154                                                 $cacheArray['active_extensions'][$data['ext_name']] = $v;
155                                         } // END - if
156                                 } // END - if
157
158                                 // Write cache line to file
159                                 @fwrite($this->cache_pointer, "\$data['".$k."'][] = \"".$v."\";\n");
160                         }
161                 } else {
162                         // Cannot create file
163                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
164                 }
165         }
166
167         function cache_close()
168         {
169                 // Quit function when no pointer is set
170                 if (empty($this->cache_pointer)) return;
171                 if ($this->cache_pointer)
172                 {
173                         // Close file add destroy handler
174                         @fclose($this->cache_pointer);
175
176                         // Set rights
177                         if (FILE_READABLE($this->cache_inc)) @chmod($this->cache_inc, 0666);
178
179                         // Remove pointer
180                         unset($this->cache_pointer);
181                 }
182                 else
183                 {
184                         // Cannot create file
185                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
186                 }
187         }
188
189         function cache_load()
190         {
191                 if (FILE_READABLE($this->cache_inc)) {
192                         // Prepare temporary array
193                         $data = array();
194
195                         // Load cache file
196                         $cache = implode("", file($this->cache_inc));
197
198                         // Execute cache file
199                         eval($cache);
200                         if (is_array($data)) {
201                                 // Return cache
202                                 return $data;
203                         } else {
204                                 // Cache problem detected!
205                                 $this->cache_destroy();
206                         }
207                 }
208                 else
209                 {
210                         // Cache file not found or not readable
211                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
212                 }
213         }
214
215         function cache_destroy()
216         {
217                 if (FILE_READABLE($this->cache_inc))
218                 {
219                         // Remove cache file from system
220                         @unlink($this->cache_inc);
221                         if (!FILE_READABLE($this->cache_inc))
222                         {
223                                 // Close cache automatically (we don't need it anymore!)
224                                 $this->cache_close();
225                         }
226                         else
227                         {
228                                 // Not removed!
229                                 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
230                         }
231                 }
232                 else
233                 {
234                         // Does not exist!
235                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
236                 }
237         }
238
239         function cache_remove($search, $data, $array)
240         {
241                 global $ARRAY;
242                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc)))
243                 {
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                         {
251                                 // Key (hopefully) found?
252                                 foreach ($array as $a)
253                                 {
254                                         // So we can remove all elements as requested
255                                         unset($dummy[$a][$key]);
256                                 }
257
258                                 // Flush array to cache file
259                                 $fp = fopen($this->cache_inc, 'w');
260                                 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
261                                 foreach ($dummy as $k => $v)
262                                 {
263                                         if (is_array($v))
264                                         {
265                                                 // Multi line(s) found
266                                                 $LINE = "";
267                                                 foreach($v as $k2 => $v2)
268                                                 {
269                                                         // Put every array element in a row...
270                                                         $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
271                                                 }
272                                         }
273                                         else
274                                         {
275                                                 // Single line found
276                                                 $LINE = "\$data['".$k."'] = \"".$v."\";\n";
277                                         }
278
279                                         // Write line(s)
280                                         fwrite($fp, $LINE);
281                                 }
282
283                                 // Close cache file
284                                 fclose($fp);
285                         }
286                 }
287                 else
288                 {
289                         // Cannot write to cache!
290                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
291                 }
292         }
293
294         function cache_replace($search, $replace, $search_key, $array)
295         {
296                 global $ARRAY;
297                 if ((FILE_READABLE($this->cache_inc)) && (is_writeable($this->cache_inc)))
298                 {
299                         // Load cache into dummy array
300                         $dummy = $this->cache_load();
301
302                         // Check if $dummy is valid (prevents some errors)
303                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search])))
304                         {
305                                 // Search for key in array
306                                 $key_found = array_key_exists($search_key, $dummy[$search]);
307                                 if ($key_found == true)
308                                 {
309                                         $key = $search_key;
310                                         // Key (hopefully) found?
311                                         foreach ($dummy as $a => $v)
312                                         {
313                                                 // So we can update all entries
314                                                 if ($a == $search)
315                                                 {
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                                         {
326                                                 if (is_array($v))
327                                                 {
328                                                         // Multi line(s) found
329                                                         $LINE = "";
330                                                         foreach($v as $k2 => $v2)
331                                                         {
332                                                                 // Put every array element in a row...
333                                                                 $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
334                                                         }
335                                                 }
336                                                 else
337                                                 {
338                                                         // Single line found
339                                                         $LINE = "\$data['".$k."'] = \"".$v."\";\n";
340                                                 }
341
342                                                 // Write line(s)
343                                                 fwrite($fp, $LINE);
344                                         }
345
346                                         // Close cache file
347                                         fclose($fp);
348                                 }
349                         }
350                 }
351                 else
352                 {
353                         // Cannot write to cache!
354                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
355                 }
356         }
357 }
358 //
359 ?>