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