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