more in naming convention applied, you should better kill inc/cache/*.cache files...
[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                         {
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_exists($path.".htaccess"))
71                                 {
72                                         // Update database that we have tested it
73                                         $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_config SET cache_tested='1' WHERE config=0 LIMIT 1", __FILE__, __LINE__);
74                                         $this->ret="done";
75
76                                         // All done!
77                                         return "done";
78                                 }
79                                 else
80                                 {
81                                         // Stop! Set a .htaccess file first
82                                         $this->ret="htaccess";
83                                         return "htaccess";
84                                 }
85                         }
86                 }
87                 elseif ($tested)
88                 {
89                         // System already tested
90                         $this->ret="done";
91                         return "done";
92                 }
93
94                 // Something goes wrong here!
95                 $this->ret="failed";
96                 return "failed";
97         }
98
99         function cache_file($file, $ignore_ctime=false)
100         {
101                 global $INC;
102                 // Construct FQFN (full qualified file name)
103                 $inc = $this->cache_path.$file.".cache";
104
105                 // Rember it + filename in class
106                 $this->cache_inc = $inc;
107
108                 // Check if file exists
109                 $status = (file_exists($inc) && (is_readable($inc)) && (is_writeable($inc)));
110                 if ($status)
111                 {
112                         // Yes, it does. So let's get it's last changed date/time
113                         $ctime = filectime($inc);
114                 }
115                 else
116                 {
117                         // No, it doesn't. Zero date/time
118                         $ctime = "0";
119                 }
120
121                 // Remember change date/time in class
122                 $this->cache_ctime = $ctime;
123
124                 // Is the cache file outdated?
125                 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime))
126                 {
127                         // Ok, we need an update!
128                         $status = false;
129                 }
130                 return $status;
131         }
132
133         function cache_init($array)
134         {
135                 // This will destory an existing cache file!
136                 if ($this->ret == "done")
137                 {
138                         // Create file
139                         if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);
140                         $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");
141
142                         // Begin of cache file
143                         fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");
144
145                         // Remember file pointer
146                         $this->cache_pointer = $fp;
147                 }
148                 else
149                 {
150                         // Cannot create file
151                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
152                 }
153         }
154
155         function add_row($data) {
156                 if (is_resource($this->cache_pointer)) {
157                         // Write every array element to cache file
158                         foreach ($data as $k=>$v) {
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_exists($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_exists($this->cache_inc)) && (is_readable($this->cache_inc)))
192                 {
193                         // Prepare temporary array
194                         $data = array();
195
196                         // Load cache file
197                         $cache = implode("", file($this->cache_inc));
198
199                         // Execute cache file
200                         eval($cache);
201                         if (is_array($data)) {
202                                 // Return cache
203                                 return $data;
204                         } else {
205                                 // Cache problem detected!
206                                 $this->cache_destroy();
207                         }
208                 }
209                 else
210                 {
211                         // Cache file not found or not readable
212                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);
213                 }
214         }
215
216         function cache_destroy()
217         {
218                 if (file_exists($this->cache_inc))
219                 {
220                         // Remove cache file from system
221                         @unlink($this->cache_inc);
222                         if (!file_exists($this->cache_inc))
223                         {
224                                 // Close cache automatically (we don't need it anymore!)
225                                 $this->cache_close();
226                         }
227                         else
228                         {
229                                 // Not removed!
230                                 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
231                         }
232                 }
233                 else
234                 {
235                         // Does not exist!
236                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);
237                 }
238         }
239
240         function cache_remove($search, $data, $array)
241         {
242                 global $ARRAY;
243                 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
244                 {
245                         // Load cache into dummy array
246                         $dummy = $this->cache_load();
247
248                         // Search for key in array
249                         $key = array_search($data, $dummy[$search]);
250                         if (!empty($key))
251                         {
252                                 // Key (hopefully) found?
253                                 foreach ($array as $a)
254                                 {
255                                         // So we can remove all elements as requested
256                                         unset($dummy[$a][$key]);
257                                 }
258
259                                 // Flush array to cache file
260                                 $fp = fopen($this->cache_inc, 'w');
261                                 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");
262                                 foreach ($dummy as $k=>$v)
263                                 {
264                                         if (is_array($v))
265                                         {
266                                                 // Multi line(s) found
267                                                 $LINE = "";
268                                                 foreach($v as $k2=>$v2)
269                                                 {
270                                                         // Put every array element in a row...
271                                                         $LINE .= "\$cacheInstance['".$k."'][] = \"".$v2."\";\n";
272                                                 }
273                                         }
274                                         else
275                                         {
276                                                 // Single line found
277                                                 $LINE = "\$cacheInstance['".$k."'] = \"".$v."\";\n";
278                                         }
279
280                                         // Write line(s)
281                                         fwrite($fp, $LINE);
282                                 }
283
284                                 // Close cache file
285                                 fclose($fp);
286                         }
287                 }
288                 else
289                 {
290                         // Cannot write to cache!
291                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
292                 }
293         }
294
295         function cache_replace($search, $replace, $search_key, $array)
296         {
297                 global $ARRAY;
298                 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))
299                 {
300                         // Load cache into dummy array
301                         $dummy = $this->cache_load();
302
303                         // Check if $dummy is valid (prevents some errors)
304                         if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search])))
305                         {
306                                 // Search for key in array
307                                 $key_found = array_key_exists($search_key, $dummy[$search]);
308                                 if ($key_found == true)
309                                 {
310                                         $key = $search_key;
311                                         // Key (hopefully) found?
312                                         foreach ($dummy as $a=>$v)
313                                         {
314                                                 // So we can update all entries
315                                                 if ($a == $search)
316                                                 {
317                                                         // Update now...
318                                                         $dummy[$a][$search_key] = $replace;
319                                                 }
320                                         }
321
322                                         // Flush array to cache file
323                                         $fp = fopen($this->cache_inc, 'w');
324                                         fwrite($fp, "\$dummy = \"".$ARRAY."\";\n");
325                                         foreach ($dummy as $k=>$v)
326                                         {
327                                                 if (is_array($v))
328                                                 {
329                                                         // Multi line(s) found
330                                                         $LINE = "";
331                                                         foreach($v as $k2=>$v2)
332                                                         {
333                                                                 // Put every array element in a row...
334                                                                 $LINE .= "\$cacheInstance['".$k."'][] = \"".$v2."\";\n";
335                                                         }
336                                                 }
337                                                 else
338                                                 {
339                                                         // Single line found
340                                                         $LINE = "\$cacheInstance['".$k."'] = \"".$v."\";\n";
341                                                 }
342
343                                                 // Write line(s)
344                                                 fwrite($fp, $LINE);
345                                         }
346
347                                         // Close cache file
348                                         fclose($fp);
349                                 }
350                         }
351                 }
352                 else
353                 {
354                         // Cannot write to cache!
355                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);
356                 }
357         }
358 }
359 //
360 ?>