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