-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.php
More file actions
54 lines (51 loc) · 1.37 KB
/
cache.php
File metadata and controls
54 lines (51 loc) · 1.37 KB
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
<?php
require_once('config.php');
function createCache($url,$content) {
global $cache_folder;
$filename = md5($url);
$filepath = $cache_folder . $filename;
$handle = fopen($filepath, "w");
$success = fwrite($handle, $content);
fclose($handle);
if (is_writable($filepath) && $success > 0) {
if (!$handle = fopen($filepath, 'w')) {
// echo "<p class='warning'>Cannot open file ($filename).</p>";
exit;
}
if (fwrite($handle, $content) === FALSE) {
// echo "<p class='warning'>Cannot write to file ($filename).</p>";
exit;
}
// echo "<p class='warning'>Saved to file ($filename).</p>";
fclose($handle);
} else {
// echo "<p class='warning'>The file ($filename) is not writable.</p>";
}
}
function checkCache($url,$delay=NULL) {
global $cache_folder;
if (!$delay) { $delay = 1 * 24 * 60 * 60; }
$filename = md5($url);
$filepath = $cache_folder . $filename;
if (!file_exists($filepath)) {
return false;
} else {
$timestamp = filemtime($filepath);
if ($timestamp + $delay > time()) {
$content = getCache($url);
return $content;
} else {
return false;
}
}
}
function getCache($url) {
global $cache_folder;
$filename = md5($url);
$filepath = $cache_folder . $filename;
$handle = fopen($filepath, "r");
$content = fread($handle, filesize($filepath));
fclose($handle);
return $content;
}
?>