- Daily/weekly/monthly reset completely rewritten
[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                 if (is_resource($this->cache_pointer)) {
150                         // Write every array element to cache file
151                         foreach ($data as $k=>$v) {
152                                 @fwrite($this->cache_pointer, "\$data['".$k."'][] = \"".$v."\";\n");
153                         }
154                 } else {
155                         // Cannot create file
156                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
157                 }
158         }
159
160         function cache_close()
161         {
162                 // Quit function when no pointer is set
163                 if (empty($this->cache_pointer)) return;
164                 if ($this->cache_pointer)
165                 {
166                         // Close file add destroy handler
167                         @fclose($this->cache_pointer);
168
169                         // Set rights
170                         if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
171
172                         // Remove pointer
173                         unset($this->cache_pointer);
174                 }
175                 else
176                 {
177                         // Cannot create file
178                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
179                 }
180         }
181
182         function cache_load()
183         {
184                 if ((file_exists($this->cache_inc)) && (is_readable($this->cache_inc)))
185                 {
186                         // Prepare temporary array
187                         $data = array();
188
189                         // Load cache file
190                         $cache = implode("", file($this->cache_inc));
191
192                         // Execute cache file
193                         eval($cache);
194                         if (is_array($data)) {
195                                 // Return cache
196                                 return $data;
197                         } else {
198                                 // Cache problem detected!
199                                 $this->cache_destroy();
200                         }
201                 }
202                 else
203                 {
204                         // Cache file not found or not readable
205                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
206                 }
207         }
208
209         function cache_destroy()
210         {
211                 if (file_exists($this->cache_inc))
212                 {
213                         // Remove cache file from system
214                         @unlink($this->cache_inc);
215                         if (!file_exists($this->cache_inc))
216                         {
217                                 // Close cache automatically (we don't need it anymore!)
218                                 $this->cache_close();
219                         }
220                         else
221                         {
222                                 // Not removed!
223                                 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
224                         }
225                 }
226                 else
227                 {
228                         // Does not exist!
229                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
230                 }
231         }
232
233         function cache_remove($search, $data, $array)
234         {
235                 global $ARRAY;
236                 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
237                 {
238                         // Load cache into dummy array
239                         $dummy = $this->cache_load();
240
241                         // Search for key in array
242                         $key = array_search($data, $dummy[$search]);
243                         if (!empty($key))
244                         {
245                                 // Key (hopefully) found?
246                                 foreach ($array as $a)
247                                 {
248                                         // So we can remove all elements as requested
249                                         unset($dummy[$a][$key]);
250                                 }
251
252                                 // Flush array to cache file
253                                 $fp = fopen($this->cache_inc, 'w');
254                                 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
255                                 foreach ($dummy as $k=>$v)
256                                 {
257                                         if (is_array($v))
258                                         {
259                                                 // Multi line(s) found
260                                                 $LINE = "";
261                                                 foreach($v as $k2=>$v2)
262                                                 {
263                                                         // Put every array element in a row...
264                                                         $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
265                                                 }
266                                         }
267                                         else
268                                         {
269                                                 // Single line found
270                                                 $LINE = "\$data['".$k."'] = \"".$v."\";\n";
271                                         }
272
273                                         // Write line(s)
274                                         fwrite($fp, $LINE);
275                                 }
276
277                                 // Close cache file
278                                 fclose($fp);
279                         }
280                 }
281                 else
282                 {
283                         // Cannot write to cache!
284                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
285                 }
286         }
287
288         function cache_replace($search, $replace, $search_key, $array)
289         {
290                 global $ARRAY;
291                 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
292                 {
293                         // Load cache into dummy array
294                         $dummy = $this->cache_load();
295
296                         // Check if $dummy is valid (prevents some errors)
297                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search])))
298                         {
299                                 // Search for key in array
300                                 $key_found = array_key_exists($search_key, $dummy[$search]);
301                                 if ($key_found == true)
302                                 {
303                                         $key = $search_key;
304                                         // Key (hopefully) found?
305                                         foreach ($dummy as $a=>$v)
306                                         {
307                                                 // So we can update all entries
308                                                 if ($a == $search)
309                                                 {
310                                                         // Update now...
311                                                         $dummy[$a][$search_key] = $replace;
312                                                 }
313                                         }
314
315                                         // Flush array to cache file
316                                         $fp = fopen($this->cache_inc, 'w');
317                                         fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
318                                         foreach ($dummy as $k=>$v)
319                                         {
320                                                 if (is_array($v))
321                                                 {
322                                                         // Multi line(s) found
323                                                         $LINE = "";
324                                                         foreach($v as $k2=>$v2)
325                                                         {
326                                                                 // Put every array element in a row...
327                                                                 $LINE .= "\$data['".$k."'][] = \"".$v2."\";\n";
328                                                         }
329                                                 }
330                                                 else
331                                                 {
332                                                         // Single line found
333                                                         $LINE = "\$data['".$k."'] = \"".$v."\";\n";
334                                                 }
335
336                                                 // Write line(s)
337                                                 fwrite($fp, $LINE);
338                                         }
339
340                                         // Close cache file
341                                         fclose($fp);
342                                 }
343                         }
344                 }
345                 else
346                 {
347                         // Cannot write to cache!
348                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
349                 }
350         }
351 }
352 //
353 ?>