1 | <?php
|
---|
2 | if(!defined('METAL_LIBRARY_CACHE_FILE_CACHE_CLASS'))
|
---|
3 | {
|
---|
4 | define('METAL_LIBRARY_CACHE_FILE_CACHE_CLASS',1);
|
---|
5 |
|
---|
6 | /*
|
---|
7 | *
|
---|
8 | * Copyright © (C) Manuel Lemos 2001-2003
|
---|
9 | *
|
---|
10 | * @(#) $Id: filecacheclass.class,v 1.32 2008/02/03 21:32:47 mlemos Exp $
|
---|
11 | *
|
---|
12 | */
|
---|
13 |
|
---|
14 | class file_cache_class
|
---|
15 | {
|
---|
16 | /*
|
---|
17 | * Protected variables
|
---|
18 | *
|
---|
19 | */
|
---|
20 | var $read_cache_file;
|
---|
21 | var $read_cache_file_opened=0;
|
---|
22 | var $write_cache_file;
|
---|
23 | var $write_cache_file_opened=0;
|
---|
24 | var $writtenheaders=0;
|
---|
25 |
|
---|
26 | /*
|
---|
27 | * Public variables
|
---|
28 | *
|
---|
29 | */
|
---|
30 | var $path='';
|
---|
31 | var $flush_cache_file=0;
|
---|
32 | var $error='';
|
---|
33 | var $reopenupdatedcache=1;
|
---|
34 | var $headers=array();
|
---|
35 | var $cache_buffer_length=100000;
|
---|
36 | var $automatic_headers=1;
|
---|
37 | var $verify_headers=array();
|
---|
38 |
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * Protected functions
|
---|
42 | *
|
---|
43 | */
|
---|
44 | Function writecachedata($data)
|
---|
45 | {
|
---|
46 | if((fwrite($this->write_cache_file,$data,strlen($data))!=0))
|
---|
47 | return 1;
|
---|
48 | $this->error='could not write to the cache file';
|
---|
49 | return 0;
|
---|
50 | }
|
---|
51 |
|
---|
52 | Function readcachedata(&$data,$length)
|
---|
53 | {
|
---|
54 | if($length<=0)
|
---|
55 | {
|
---|
56 | $this->error='it was specified an invalid cache data read length';
|
---|
57 | return 0;
|
---|
58 | }
|
---|
59 | if(GetType($readdata=fread($this->read_cache_file,$length))=='string')
|
---|
60 | {
|
---|
61 | $data=$readdata;
|
---|
62 | return 1;
|
---|
63 | }
|
---|
64 | $this->error='could not read from the cache file';
|
---|
65 | return 0;
|
---|
66 | }
|
---|
67 |
|
---|
68 | Function readcacheline(&$line)
|
---|
69 | {
|
---|
70 | if(GetType($readline=fgets($this->read_cache_file))=='string')
|
---|
71 | {
|
---|
72 | $line=((GetType($endofline=strpos($readline,"\r"))=='integer') ? substr($readline,0,$endofline) : $readline);
|
---|
73 | return 1;
|
---|
74 | }
|
---|
75 | $this->error='could not read from the cache file';
|
---|
76 | return 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | Function endofcache(&$endofcache)
|
---|
80 | {
|
---|
81 | if(!($this->read_cache_file_opened))
|
---|
82 | {
|
---|
83 | $this->error='endofcache function is not implemented';
|
---|
84 | return 0;
|
---|
85 | }
|
---|
86 | $endofcache=feof($this->read_cache_file);
|
---|
87 | return 1;
|
---|
88 | }
|
---|
89 |
|
---|
90 | Function cachelength(&$length)
|
---|
91 | {
|
---|
92 | if((GetType($filelength=@filesize($this->path))=='integer'))
|
---|
93 | {
|
---|
94 | $length=$filelength;
|
---|
95 | return 1;
|
---|
96 | }
|
---|
97 | $this->error='could not determing cache file length';
|
---|
98 | return 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 | Function cachebufferlength(&$length)
|
---|
102 | {
|
---|
103 | $length=$this->cache_buffer_length;
|
---|
104 | if($length!=0 || $this->cachelength($length))
|
---|
105 | return 1;
|
---|
106 | $this->error=('could not get cache buffer length: '.$this->error);
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | Function readcacheheaders(&$read)
|
---|
111 | {
|
---|
112 | $this->headers=array();
|
---|
113 | for(;;)
|
---|
114 | {
|
---|
115 | if(!($this->readcacheline($line)))
|
---|
116 | return 0;
|
---|
117 | if(!(strlen($line)!=0))
|
---|
118 | break;
|
---|
119 | if(!($this->endofcache($endofcache)))
|
---|
120 | return 0;
|
---|
121 | if(GetType($endofline=strpos($line,"\r"))=='integer')
|
---|
122 | $line=substr($line,0,$endofline);
|
---|
123 | if($endofcache || !GetType($space=strpos($line,' '))=='integer')
|
---|
124 | {
|
---|
125 | $read=0;
|
---|
126 | return 1;
|
---|
127 | }
|
---|
128 | $this->headers[substr($line,0,$space)]=substr($line,($space+1),(strlen($line)-$space-1));
|
---|
129 | }
|
---|
130 | $read=1;
|
---|
131 | return 1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | Function updatedcache(&$updated)
|
---|
135 | {
|
---|
136 | if(!($this->readcacheheaders($updated)))
|
---|
137 | return 0;
|
---|
138 | if($updated)
|
---|
139 | {
|
---|
140 | Reset($this->verify_headers);
|
---|
141 | $end=(GetType($headername=Key($this->verify_headers))!='string');
|
---|
142 | for(;!$end;)
|
---|
143 | {
|
---|
144 | if(!IsSet($this->headers[$headername]) || (strcmp($this->headers[$headername],$this->verify_headers[$headername])))
|
---|
145 | {
|
---|
146 | $updated=0;
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | Next($this->verify_headers);
|
---|
150 | $end=(GetType($headername=Key($this->verify_headers))!='string');
|
---|
151 | }
|
---|
152 | }
|
---|
153 | if($updated && IsSet($this->headers['x-expires:']))
|
---|
154 | $updated=(strcmp($this->headers['x-expires:'],strftime('%Y-%m-%d %H:%M:%S'))>0);
|
---|
155 | if(!($updated))
|
---|
156 | $this->headers=array();
|
---|
157 | return 1;
|
---|
158 | }
|
---|
159 |
|
---|
160 | Function writecache(&$data,$endofcache)
|
---|
161 | {
|
---|
162 | if(!$this->writtenheaders && (strlen($data)>0 || $endofcache))
|
---|
163 | {
|
---|
164 | $rfc822now=gmstrftime('%a, %d %b %Y %H:%M:%S GMT');
|
---|
165 | srand(time());
|
---|
166 | if($this->automatic_headers && (!$this->setheader('date:',$rfc822now) || !$this->setheader('etag:','"'.md5(strval(rand(0,9999)).$rfc822now).'"') || ($endofcache && !$this->setheader('content-length:',strval(strlen($data))))))
|
---|
167 | return 0;
|
---|
168 | Reset($this->headers);
|
---|
169 | $end=(GetType($headername=Key($this->headers))!='string');
|
---|
170 | for(;!$end;)
|
---|
171 | {
|
---|
172 | $headerdata=($headername.' '.$this->headers[$headername]."\r\n");
|
---|
173 | if(!($this->writecachedata($headerdata)))
|
---|
174 | {
|
---|
175 | $this->error=('could not write the cache headers: '.$this->error);
|
---|
176 | return 0;
|
---|
177 | }
|
---|
178 | Next($this->headers);
|
---|
179 | $end=(GetType($headername=Key($this->headers))!='string');
|
---|
180 | }
|
---|
181 | Reset($this->verify_headers);
|
---|
182 | $end=(GetType($headername=Key($this->verify_headers))!='string');
|
---|
183 | for(;!$end;)
|
---|
184 | {
|
---|
185 | $headerdata=($headername.' '.$this->verify_headers[$headername]."\r\n");
|
---|
186 | if(!($this->writecachedata($headerdata)))
|
---|
187 | {
|
---|
188 | $this->error=('could not write the cache headers: '.$this->error);
|
---|
189 | return 0;
|
---|
190 | }
|
---|
191 | Next($this->verify_headers);
|
---|
192 | $end=(GetType($headername=Key($this->verify_headers))!='string');
|
---|
193 | }
|
---|
194 | $headerdata="\r\n";
|
---|
195 | if(!($this->writecachedata($headerdata)))
|
---|
196 | {
|
---|
197 | $this->error=('could not write to the cache header separator: '.$this->error);
|
---|
198 | return 0;
|
---|
199 | }
|
---|
200 | $this->writtenheaders=1;
|
---|
201 | }
|
---|
202 | if(strlen($data)==0)
|
---|
203 | {
|
---|
204 | if(!($endofcache))
|
---|
205 | {
|
---|
206 | $this->error='it was specified an empty data block to store in the cache';
|
---|
207 | return 0;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | if(!($this->writecachedata($data)))
|
---|
213 | {
|
---|
214 | $this->error=('could not write to the cache data: '.$this->error);
|
---|
215 | return 0;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | return 1;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Public functions
|
---|
223 | *
|
---|
224 | */
|
---|
225 | Function verifycache(&$updated)
|
---|
226 | {
|
---|
227 | if($this->read_cache_file_opened)
|
---|
228 | {
|
---|
229 | $this->error='the cache file is already opened for reading';
|
---|
230 | return 0;
|
---|
231 | }
|
---|
232 | if(!strcmp($this->path,''))
|
---|
233 | {
|
---|
234 | $this->error='it was not specified the cache file path';
|
---|
235 | return 0;
|
---|
236 | }
|
---|
237 | $updated=0;
|
---|
238 | if(file_exists($this->path))
|
---|
239 | {
|
---|
240 | if(!((($this->read_cache_file=@fopen($this->path,'rb'))!=0)))
|
---|
241 | {
|
---|
242 | $this->error='could not open cache file for reading';
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 | if(!(@flock($this->read_cache_file,1)))
|
---|
246 | {
|
---|
247 | fclose($this->read_cache_file);
|
---|
248 | $this->error='could not lock shared cache file';
|
---|
249 | return 0;
|
---|
250 | }
|
---|
251 | $this->read_cache_file_opened=1;
|
---|
252 | $success=$this->cachelength($length);
|
---|
253 | if($success)
|
---|
254 | {
|
---|
255 | if($length>0)
|
---|
256 | $success=$this->updatedcache($updated);
|
---|
257 | }
|
---|
258 | if(!($success))
|
---|
259 | {
|
---|
260 | @flock($this->read_cache_file,3);
|
---|
261 | fclose($this->read_cache_file);
|
---|
262 | $this->read_cache_file_opened=0;
|
---|
263 | return 0;
|
---|
264 | }
|
---|
265 | if($updated)
|
---|
266 | return 1;
|
---|
267 | $success=@flock($this->read_cache_file,3);
|
---|
268 | fclose($this->read_cache_file);
|
---|
269 | $this->read_cache_file_opened=0;
|
---|
270 | if(!($success))
|
---|
271 | {
|
---|
272 | $this->error='could not unlock the cache file';
|
---|
273 | return 0;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | if(!((($this->read_cache_file=@fopen($this->path,'ab'))!=0)))
|
---|
277 | {
|
---|
278 | $this->error='could not open cache file for appending';
|
---|
279 | return 0;
|
---|
280 | }
|
---|
281 | $this->read_cache_file_opened=1;
|
---|
282 | if(!(@flock($this->read_cache_file,2)))
|
---|
283 | {
|
---|
284 | $this->error='could not lock exclusive cache file';
|
---|
285 | @flock($this->read_cache_file,3);
|
---|
286 | fclose($this->read_cache_file);
|
---|
287 | $this->read_cache_file_opened=0;
|
---|
288 | return 0;
|
---|
289 | }
|
---|
290 | if(!((($this->write_cache_file=@fopen($this->path,'wb'))!=0)))
|
---|
291 | {
|
---|
292 | @flock($this->read_cache_file,3);
|
---|
293 | fclose($this->read_cache_file);
|
---|
294 | $this->read_cache_file_opened=0;
|
---|
295 | $this->error='could not open cache file for writing';
|
---|
296 | return 0;
|
---|
297 | }
|
---|
298 | $this->write_cache_file_opened=1;
|
---|
299 | $this->writtenheaders=0;
|
---|
300 | $this->headers=array();
|
---|
301 | return 1;
|
---|
302 | }
|
---|
303 |
|
---|
304 | Function storedata(&$data,$endofcache)
|
---|
305 | {
|
---|
306 | if(!($this->write_cache_file_opened))
|
---|
307 | {
|
---|
308 | $this->error='cache file is not set for storing data';
|
---|
309 | return 0;
|
---|
310 | }
|
---|
311 | $success=$this->writecache($data,$endofcache);
|
---|
312 | if($success)
|
---|
313 | {
|
---|
314 | $success=(!$endofcache || !$this->flush_cache_file || fflush($this->write_cache_file));
|
---|
315 | if(!($success))
|
---|
316 | $this->error='could not flush the cache file';
|
---|
317 | }
|
---|
318 | if(!($success))
|
---|
319 | {
|
---|
320 | fclose($this->write_cache_file);
|
---|
321 | $this->write_cache_file_opened=0;
|
---|
322 | @flock($this->read_cache_file,3);
|
---|
323 | fclose($this->read_cache_file);
|
---|
324 | unlink($this->path);
|
---|
325 | $this->read_cache_file_opened=0;
|
---|
326 | return 0;
|
---|
327 | }
|
---|
328 | if(!($endofcache))
|
---|
329 | return 1;
|
---|
330 | fclose($this->write_cache_file);
|
---|
331 | $this->write_cache_file_opened=0;
|
---|
332 | @flock($this->read_cache_file,3);
|
---|
333 | fclose($this->read_cache_file);
|
---|
334 | $this->read_cache_file_opened=0;
|
---|
335 | if(!($this->reopenupdatedcache))
|
---|
336 | return 1;
|
---|
337 | if(!((($this->read_cache_file=@fopen($this->path,'rb'))!=0)))
|
---|
338 | {
|
---|
339 | $this->error='could not reopen cache file for reading';
|
---|
340 | return 0;
|
---|
341 | }
|
---|
342 | if(!(@flock($this->read_cache_file,1)))
|
---|
343 | {
|
---|
344 | fclose($this->read_cache_file);
|
---|
345 | $this->error='could not lock shared cache file';
|
---|
346 | return 0;
|
---|
347 | }
|
---|
348 | $this->read_cache_file_opened=1;
|
---|
349 | if(!($this->readcacheheaders($read)))
|
---|
350 | return 0;
|
---|
351 | if(!($read))
|
---|
352 | {
|
---|
353 | @flock($this->read_cache_file,3);
|
---|
354 | fclose($this->read_cache_file);
|
---|
355 | $this->read_cache_file_opened=0;
|
---|
356 | $this->error='could not read the cache file headers';
|
---|
357 | return 0;
|
---|
358 | }
|
---|
359 | return 1;
|
---|
360 | }
|
---|
361 |
|
---|
362 | Function retrievefromcache(&$data,&$endofcache)
|
---|
363 | {
|
---|
364 | if(!($this->read_cache_file_opened))
|
---|
365 | {
|
---|
366 | $this->error='cache file is not set for retrieving cached data';
|
---|
367 | return 0;
|
---|
368 | }
|
---|
369 | if($this->write_cache_file_opened)
|
---|
370 | {
|
---|
371 | $this->error='cache file is still opened for writing';
|
---|
372 | return 0;
|
---|
373 | }
|
---|
374 | $success=$this->cachebufferlength($length);
|
---|
375 | if($success)
|
---|
376 | $success=($length==0 || $this->readcachedata($data,$length));
|
---|
377 | if(!($success))
|
---|
378 | {
|
---|
379 | @flock($this->read_cache_file,3);
|
---|
380 | fclose($this->read_cache_file);
|
---|
381 | $this->read_cache_file_opened=0;
|
---|
382 | return 0;
|
---|
383 | }
|
---|
384 | if($length==0)
|
---|
385 | $endofcache=1;
|
---|
386 | else
|
---|
387 | {
|
---|
388 | if(!($this->endofcache($endofcache)))
|
---|
389 | {
|
---|
390 | @flock($this->read_cache_file,3);
|
---|
391 | fclose($this->read_cache_file);
|
---|
392 | $this->read_cache_file_opened=0;
|
---|
393 | return 0;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | if(!($endofcache))
|
---|
397 | return 1;
|
---|
398 | @flock($this->read_cache_file,3);
|
---|
399 | fclose($this->read_cache_file);
|
---|
400 | $this->read_cache_file_opened=0;
|
---|
401 | return 1;
|
---|
402 | }
|
---|
403 |
|
---|
404 | Function closecache()
|
---|
405 | {
|
---|
406 | if($this->read_cache_file_opened || $this->write_cache_file_opened)
|
---|
407 | {
|
---|
408 | @flock($this->read_cache_file,3);
|
---|
409 | $this->read_cache_file_opened=0;
|
---|
410 | }
|
---|
411 | if($this->read_cache_file_opened)
|
---|
412 | {
|
---|
413 | fclose($this->read_cache_file);
|
---|
414 | $this->read_cache_file_opened=0;
|
---|
415 | }
|
---|
416 | if($this->write_cache_file_opened)
|
---|
417 | {
|
---|
418 | fclose($this->write_cache_file);
|
---|
419 | $this->write_cache_file_opened=0;
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | Function voidcache()
|
---|
424 | {
|
---|
425 | if($this->read_cache_file_opened)
|
---|
426 | {
|
---|
427 | $this->error='can not void cache with the file open for reading';
|
---|
428 | return 0;
|
---|
429 | }
|
---|
430 | if(!strcmp($this->path,''))
|
---|
431 | {
|
---|
432 | $this->error='it was not specified the cache file path';
|
---|
433 | return 0;
|
---|
434 | }
|
---|
435 | if(!(file_exists($this->path)))
|
---|
436 | return 1;
|
---|
437 | if(!((($this->read_cache_file=@fopen($this->path,'ab'))!=0)))
|
---|
438 | {
|
---|
439 | $this->error='could not open the cache file for reading';
|
---|
440 | return 0;
|
---|
441 | }
|
---|
442 | if(!(@flock($this->read_cache_file,2)))
|
---|
443 | {
|
---|
444 | fclose($this->read_cache_file);
|
---|
445 | $this->error='could not lock exclusive cache file';
|
---|
446 | return 0;
|
---|
447 | }
|
---|
448 | $success=(($this->write_cache_file=@fopen($this->path,'wb'))!=0);
|
---|
449 | if($success)
|
---|
450 | fclose($this->write_cache_file);
|
---|
451 | @flock($this->read_cache_file,3);
|
---|
452 | fclose($this->read_cache_file);
|
---|
453 | $this->error='could not open cache file for writing';
|
---|
454 | unlink($this->path);
|
---|
455 | return $success;
|
---|
456 | }
|
---|
457 |
|
---|
458 | Function updating(&$updating)
|
---|
459 | {
|
---|
460 | if($this->read_cache_file_opened)
|
---|
461 | {
|
---|
462 | $this->error='the cache file is already opened for reading';
|
---|
463 | return 0;
|
---|
464 | }
|
---|
465 | if(!strcmp($this->path,''))
|
---|
466 | {
|
---|
467 | $this->error='it was not specified the cache file path';
|
---|
468 | return 0;
|
---|
469 | }
|
---|
470 | $updating=0;
|
---|
471 | if(!(file_exists($this->path)))
|
---|
472 | return 1;
|
---|
473 | if(!((($this->read_cache_file=@fopen($this->path,'rb'))!=0)))
|
---|
474 | {
|
---|
475 | $this->error='could not open cache file for reading';
|
---|
476 | return 0;
|
---|
477 | }
|
---|
478 | if(@flock($this->read_cache_file,5))
|
---|
479 | @flock($this->read_cache_file,3);
|
---|
480 | else
|
---|
481 | $updating=1;
|
---|
482 | fclose($this->read_cache_file);
|
---|
483 | return 1;
|
---|
484 | }
|
---|
485 |
|
---|
486 | Function setheader($header,$value)
|
---|
487 | {
|
---|
488 | if(GetType(strpos($header,' '))=='integer')
|
---|
489 | {
|
---|
490 | $this->error='it was specified a header name with a space in it';
|
---|
491 | return 0;
|
---|
492 | }
|
---|
493 | $lowerheader=strtolower($header);
|
---|
494 | if(IsSet($this->headers[$lowerheader]))
|
---|
495 | {
|
---|
496 | $this->error='it was specified a header already defined';
|
---|
497 | return 0;
|
---|
498 | }
|
---|
499 | $this->headers[$lowerheader]=$value;
|
---|
500 | return 1;
|
---|
501 | }
|
---|
502 |
|
---|
503 | Function setexpirydate($date)
|
---|
504 | {
|
---|
505 | if(!($this->setheader('x-expires:',$date)))
|
---|
506 | return 0;
|
---|
507 | if($this->automatic_headers)
|
---|
508 | return $this->setheader('expires:',gmstrftime('%a, %d %b %Y %H:%M:%S GMT',mktime(substr($date,11,2),substr($date,14,2),substr($date,17,2),substr($date,5,2),substr($date,8,2),substr($date,0,4))));
|
---|
509 | return 1;
|
---|
510 | }
|
---|
511 |
|
---|
512 | Function setexpirytime($time)
|
---|
513 | {
|
---|
514 | if($time<=0)
|
---|
515 | {
|
---|
516 | $this->error='it was not specified a valid expiry time period';
|
---|
517 | return 0;
|
---|
518 | }
|
---|
519 | return $this->setexpirydate(strftime('%Y-%m-%d %H:%M:%S',time()+$time));
|
---|
520 | }
|
---|
521 | };
|
---|
522 |
|
---|
523 | }
|
---|
524 | ?>
|
---|