c9dda2a6bd922c86d7abead7a9c66745aee6fa6c
[mailer.git] / 0.2.1 / inc / libs / cache_functions.php
1 <?php\r
2 /************************************************************************\r
3  * MXChange v0.2.1                                    Start: 10/11/2003 *\r
4  * ===============                              Last change: 10/11/2003 *\r
5  *                                                                      *\r
6  * -------------------------------------------------------------------- *\r
7  * File              : admins_functions.php                             *\r
8  * -------------------------------------------------------------------- *\r
9  * Short description : Functions for the admins extension               *\r
10  * -------------------------------------------------------------------- *\r
11  * Kurzbeschreibung  : Funktionen fuer die admins-Erweiterung           *\r
12  * -------------------------------------------------------------------- *\r
13  *                                                                      *\r
14  * -------------------------------------------------------------------- *\r
15  * Copyright (c) 2003 - 2007 by Roland Haeder                           *\r
16  * For more information visit: http://www.mxchange.org                  *\r
17  *                                                                      *\r
18  * This program is free software; you can redistribute it and/or modify *\r
19  * it under the terms of the GNU General Public License as published by *\r
20  * the Free Software Foundation; either version 2 of the License, or    *\r
21  * (at your option) any later version.                                  *\r
22  *                                                                      *\r
23  * This program is distributed in the hope that it will be useful,      *\r
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *\r
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *\r
26  * GNU General Public License for more details.                         *\r
27  *                                                                      *\r
28  * You should have received a copy of the GNU General Public License    *\r
29  * along with this program; if not, write to the Free Software          *\r
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *\r
31  * MA  02110-1301  USA                                                  *\r
32  ************************************************************************/\r
33 \r
34 // Some security stuff...\r
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))\r
36 {\r
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";\r
38         require($INC);\r
39 }\r
40 // Caching class\r
41 class mxchange_cache\r
42 {\r
43         // Define variables\r
44         var $update_interval = 0;\r
45         var $ret = "init";\r
46         var $cache_path = "";\r
47         var $cache_inc = "";\r
48         var $cache_ctime = 0;\r
49         var $cache_pointer = false;\r
50 \r
51         // Constructor\r
52         function mxchange_cache($interval, $path, $tested)\r
53         {\r
54                 // Remember interval in class\r
55                 $this->update_interval=$interval;\r
56 \r
57                 // Remeber path\r
58                 $this->cache_path=$path;\r
59 \r
60                 // Check if path exists\r
61                 if ((file_exists($path)) && (is_dir($path)) && (!$tested))\r
62                 {\r
63                         // Check if we can create a file inside the path\r
64                         @touch($path."dummy.tmp", 'w');\r
65                         if (file_exists($path."dummy.tmp"))\r
66                         {\r
67                                 // Yes, we can do. So let's remove it\r
68                                 unlink($path."dummy.tmp");\r
69 \r
70                                 // Is there a .htaccess file?\r
71                                 if (file_exists($path.".htaccess"))\r
72                                 {\r
73                                         // Update database that we have tested it\r
74                                         $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_config SET cache_tested='1' WHERE config='0' LIMIT 1", __FILE__, __LINE__);\r
75                                         $this->ret="done";\r
76 \r
77                                         // All done!\r
78                                         return "done";\r
79                                 }\r
80                                  else\r
81                                 {\r
82                                         // Stop! Set a .htaccess file first\r
83                                         $this->ret="htaccess";\r
84                                         return "htaccess";\r
85                                 }\r
86                         }\r
87                 }\r
88                  elseif ($tested)\r
89                 {\r
90                         // System already tested\r
91                         $this->ret="done";\r
92                         return "done";\r
93                 }\r
94 \r
95                 // Something goes wrong here!\r
96                 $this->ret="failed";\r
97                 return "failed";\r
98         }\r
99 \r
100         function cache_file($file, $ignore_ctime=false)\r
101         {\r
102                 global $INC;\r
103                 // Construct FQFN (full qualified file name)\r
104                 $inc = $this->cache_path.$file.".cache";\r
105 \r
106                 // Rember it + filename in class\r
107                 $this->cache_inc = $inc;\r
108 \r
109                 // Check if file exists\r
110                 $status = (file_exists($inc) && (is_readable($inc)) && (is_writeable($inc)));\r
111                 if ($status)\r
112                 {\r
113                         // Yes, it does. So let's get it's last changed date/time\r
114                         $ctime = filectime($inc);\r
115                 }\r
116                  else\r
117                 {\r
118                         // No, it doesn't. Zero date/time\r
119                         $ctime = "0";\r
120                 }\r
121 \r
122                 // Remember change date/time in class\r
123                 $this->cache_ctime = $ctime;\r
124 \r
125                 // Is the cache file outdated?\r
126                 if (((time() - $ctime) >= $this->update_interval) && (!$ignore_ctime))\r
127                 {\r
128                         // Ok, we need an update!\r
129                         $status = false;\r
130                 }\r
131                 return $status;\r
132         }\r
133 \r
134         function cache_init($array)\r
135         {\r
136                 // This will destory an existing cache file!\r
137                 if ($this->ret == "done")\r
138                 {\r
139                         // Create file\r
140                         if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);\r
141                         $fp = @fopen($this->cache_inc, 'w') or mxchange_die("Cannot write to cache ".$this->cache_inc." !");\r
142 \r
143                         // Begin of cache file\r
144                         fwrite($fp, "\$ARRAY = \"".$array."\";\n\n");\r
145 \r
146                         // Remember file pointer\r
147                         $this->cache_pointer = $fp;\r
148                 }\r
149                  else\r
150                 {\r
151                         // Cannot create file\r
152                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);\r
153                 }\r
154         }\r
155 \r
156         function add_row($data)\r
157         {\r
158                 if ($this->cache_pointer)\r
159                 {\r
160                         // Write every array element to cache file\r
161                         foreach ($data as $k=>$v)\r
162                         {\r
163                                 @fwrite($this->cache_pointer, "\$CACHE['".$k."'][] = \"".$v."\";\n");\r
164                         }\r
165                 }\r
166                  else\r
167                 {\r
168                         // Cannot create file\r
169                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);\r
170                 }\r
171         }\r
172 \r
173         function cache_close()\r
174         {\r
175                 // Quit function when no pointer is set\r
176                 if (empty($this->cache_pointer)) return;\r
177                 if ($this->cache_pointer)\r
178                 {\r
179                         // Close file add destroy handler\r
180                         @fclose($this->cache_pointer);\r
181 \r
182                         // Set rights\r
183                         if (file_exists($this->cache_inc)) @chmod($this->cache_inc, 0666);\r
184 \r
185                         // Remove pointer\r
186                         unset($this->cache_pointer);\r
187                 }\r
188                  else\r
189                 {\r
190                         // Cannot create file\r
191                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);\r
192                 }\r
193         }\r
194 \r
195         function cache_load()\r
196         {\r
197                 if ((file_exists($this->cache_inc)) && (is_readable($this->cache_inc)))\r
198                 {\r
199                         // Prepare temporay array\r
200                         $CACHE = array();\r
201                         // Load cache file\r
202                         $cache = implode("", file($this->cache_inc));\r
203                         // Execute cache file\r
204                         eval($cache);\r
205                         if (is_array($CACHE))\r
206                         {\r
207                                 return $CACHE;\r
208                         }\r
209                          else\r
210                         {\r
211                                 // Cache problem detected!\r
212                                 $this->cache_destroy();\r
213                         }\r
214                 }\r
215                  else\r
216                 {\r
217                         // Cache file not found or not readable\r
218                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_LOAD_1.$this->cache_inc.CACHE_CANNOT_LOAD_2);\r
219                 }\r
220         }\r
221 \r
222         function cache_destroy()\r
223         {\r
224                 if (file_exists($this->cache_inc))\r
225                 {\r
226                         // Remove cache file from system\r
227                         @unlink($this->cache_inc);\r
228                         if (!file_exists($this->cache_inc))\r
229                         {\r
230                                 // Close cache automatically (we don't need it anymore!)\r
231                                 $this->cache_close();\r
232                         }\r
233                          else\r
234                         {\r
235                                 // Not removed!\r
236                                 ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);\r
237                         }\r
238                 }\r
239                  else\r
240                 {\r
241                         // Does not exist!\r
242                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_CANNOT_UNLINK_1.$this->cache_inc.CACHE_CANNOT_UNLINK_2);\r
243                 }\r
244         }\r
245 \r
246         function cache_remove($search, $data, $array)\r
247         {\r
248                 global $ARRAY;\r
249                 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))\r
250                 {\r
251                         // Load cache into dummy array\r
252                         $DUMMY = $this->cache_load();\r
253 \r
254                         // Search for key in array\r
255                         $key = array_search($data, $DUMMY[$search]);\r
256                         if (!empty($key))\r
257                         {\r
258                                 // Key (hopefully) found?\r
259                                 foreach ($array as $a)\r
260                                 {\r
261                                         // So we can remove all elements as requested\r
262                                         unset($DUMMY[$a][$key]);\r
263                                 }\r
264 \r
265                                 // Flush array to cache file\r
266                                 $fp = fopen($this->cache_inc, 'w');\r
267                                 fwrite($fp, "\$ARRAY = \"".$ARRAY."\";\n");\r
268                                 foreach ($DUMMY as $k=>$v)\r
269                                 {\r
270                                         if (is_array($v))\r
271                                         {\r
272                                                 // Multi line(s) found\r
273                                                 $LINE = "";\r
274                                                 foreach($v as $k2=>$v2)\r
275                                                 {\r
276                                                         // Put every array element in a row...\r
277                                                         $LINE .= "\$CACHE['".$k."'][] = \"".$v2."\";\n";\r
278                                                 }\r
279                                         }\r
280                                          else\r
281                                         {\r
282                                                 // Single line found\r
283                                                 $LINE = "\$CACHE['".$k."'] = \"".$v."\";\n";\r
284                                         }\r
285 \r
286                                         // Write line(s)\r
287                                         fwrite($fp, $LINE);\r
288                                 }\r
289 \r
290                                 // Close cache file\r
291                                 fclose($fp);\r
292                         }\r
293                 }\r
294                  else\r
295                 {\r
296                         // Cannot write to cache!\r
297                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);\r
298                 }\r
299         }\r
300 \r
301         function cache_replace($search, $replace, $search_key, $array)\r
302         {\r
303                 global $ARRAY;\r
304                 if ((file_exists($this->cache_inc)) && (is_writeable($this->cache_inc)))\r
305                 {\r
306                         // Load cache into dummy array\r
307                         $DUMMY = $this->cache_load();\r
308 \r
309                         // Check if $DUMMY is valid (prevents some errors)\r
310                         if ((is_array($DUMMY)) && (is_array($DUMMY[$search])))\r
311                         {\r
312                                 // Search for key in array\r
313                                 $key_found = array_key_exists($search_key, $DUMMY[$search]);\r
314                                 if ($key_found == true)\r
315                                 {\r
316                                         $key = $search_key;\r
317                                         // Key (hopefully) found?\r
318                                         foreach ($DUMMY as $a=>$v)\r
319                                         {\r
320                                                 // So we can update all entries\r
321                                                 if ($a == $search)\r
322                                                 {\r
323                                                         // Update now...\r
324                                                         $DUMMY[$a][$search_key] = $replace;\r
325                                                 }\r
326                                         }\r
327 \r
328                                         // Flush array to cache file\r
329                                         $fp = fopen($this->cache_inc, 'w');\r
330                                         fwrite($fp, "\$DUMMY = \"".$ARRAY."\";\n");\r
331                                         foreach ($DUMMY as $k=>$v)\r
332                                         {\r
333                                                 if (is_array($v))\r
334                                                 {\r
335                                                         // Multi line(s) found\r
336                                                         $LINE = "";\r
337                                                         foreach($v as $k2=>$v2)\r
338                                                         {\r
339                                                                 // Put every array element in a row...\r
340                                                                 $LINE .= "\$CACHE['".$k."'][] = \"".$v2."\";\n";\r
341                                                         }\r
342                                                 }\r
343                                                  else\r
344                                                 {\r
345                                                         // Single line found\r
346                                                         $LINE = "\$CACHE['".$k."'] = \"".$v."\";\n";\r
347                                                 }\r
348 \r
349                                                 // Write line(s)\r
350                                                 fwrite($fp, $LINE);\r
351                                         }\r
352 \r
353                                         // Close cache file\r
354                                         fclose($fp);\r
355                                 }\r
356                         }\r
357                 }\r
358                  else\r
359                 {\r
360                         // Cannot write to cache!\r
361                         ADD_FATAL(__FILE__."(".__LINE__."): ".CACHE_PROBLEMS_DETECTED);\r
362                 }\r
363         }\r
364 }\r
365 //\r
366 ?>\r