forked from shangril/crero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crero-lib.php
87 lines (67 loc) · 2.02 KB
/
crero-lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
class creroHtmlCache {
static $htmlcacheexpires=7;
static $cachedat=Array();
public function __construct($htmlcache_expires){
$this::$htmlcacheexpires=floatval($htmlcache_expires);
if (file_exists('./htmlcache/cached.dat')){
$this::$cachedat=unserialize(file_get_contents('./htmlcache/cached.dat'));
}
//cleanup old pages
$cachedkeys=array_keys($this::$cachedat);
foreach ($cachedkeys as $cachedkey){
$cachedpage=$this::$cachedat[$cachedkey];
if ((floatval($cachedpage['expires'])
+floatval($this::$htmlcacheexpires*3600))
<floatval(microtime(true))){
unlink('./htmlcache/cached/'.$cachedpage['expires'].'.html');
unset($this::$cachedat[$cachedkey]);
}
}
$this->saveCacheDatToDisk();
}
public function hasPageExpired($cachedkey){
if (array_key_exists($cachedkey, $this::$cachedat))
{
$cachedpage=$this::$cachedat[$cachedkey];
if (
(floatval($cachedpage['expires'])
+floatval($this::$htmlcacheexpires*3600))
<floatval(microtime(true))
){
return true;
}
else {
return false;
}
}
else {
return true;
}
}
public function cachePage($cachedkey, $htmlCode){
$willexpire=microtime(true);
if (file_put_contents('./htmlcache/cached/'.floatval($willexpire).'.html', $htmlCode)){
$pagedat=Array();
$pagedat['expires']=''.floatval($willexpire).'';
$this::$cachedat[$cachedkey]=$pagedat;
return $this->saveCacheDatToDisk();
}
}
public function getCachedPage($cachedkey){
$page=$this::$cachedat[$cachedkey];
return file_get_contents('./htmlcache/cached/'.$page['expires'].'.html');
}
private function saveCacheDatToDisk(){
return file_put_contents('./htmlcache/cached.dat', serialize ($this::$cachedat));
}
public function purgeCache(){
unlink ('./htmlcache/cached.dat');
$cachedfilez=array_diff(scandir('./htmlcache/cached'), array('..', '.'));
foreach ($cachedfilez as $cachedfile){
unlink ('./htmlcache/cached/'.$cachedfile);
}
unlink ('./htmlcache/cached.dat');
}
}
?>