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