-
Notifications
You must be signed in to change notification settings - Fork 3
/
crero-lib.php
111 lines (91 loc) · 2.68 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?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'));
}
if ($this::$cachedat===false){
unlink('./htmlcache/cached.dat');
}
//cleanup old pages
if (is_array($this::$cachedat)){
$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');
}
public function getCachedPageDate($chachedkey){
$page=$this::$cachedat[$cachedkey];
if (file_exists('./htmlcache/cached/'.$page['expires'].'.html'))
{
return filemtime('./htmlcache/cached/'.$page['expires'].'.html');
}
else
{
return time();
}
}
private function saveCacheDatToDisk(){
return file_put_contents('./htmlcache/cached.dat', serialize ($this::$cachedat));
}
public function purgeCache(){
unlink ('./htmlcache/cached.dat');
if(scandir('./htmlcache/cached')!==false){
$cachedfilez=array_diff(scandir('./htmlcache/cached'), array('..', '.'));
foreach ($cachedfilez as $cachedfile){
unlink ('./htmlcache/cached/'.$cachedfile);
}
unlink ('./htmlcache/cached.dat');
}
}
public function purgePage($key){
$page=$this::$cachedat[$key];
unlink (file_get_contents('./htmlcache/cached/'.$page['expires'].'.html'));
unset($this::$cachedat[$key]);
return $this->saveCacheDatToDisk();
}
}
?>