forked from kestasjk/webDiplomacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.php
executable file
·86 lines (64 loc) · 1.94 KB
/
cache.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
<?php
class libCache
{
public static function dirName($name)
{
static $cache;
if( !isset($cache) ) $cache=array();
if( !isset($cache[$name]) ) $cache[$name]=self::dir('cache', array($name) );
return $cache[$name];
}
public static function wipeDir($dir, $glob='*.*')
{
if( $files = glob($dir.'/'.$glob) )
foreach($files as $file)
unlink($file);
}
public static function privateFilename($dir, $filename)
{
if( !file_exists($dir.'/index.html') )
file_put_contents($dir.'/index.html', '');
$hash=md5($dir.$filename.Config::$secret);
return $filename.'-'.$hash;
}
static public function getCacheID($base, $id, $filename, $private=false)
{
$dir = self::dirID($base, $id);
if( $private )
$filename = self::privateFilename($dir, $filename);
if( file_exists($dir.'/'.$filename) )
return file_get_contents($dir.'/'.$filename);
else
return false;
}
static public function putCacheID($base, $id, $filename, $data, $private=false)
{
$dir = self::dirID($base, $id);
if( $private )
$filename = self::privateFilename($dir, $filename);
file_put_contents($dir.'/'.$filename, $data);
}
public static function dirID($base, $id, $absolute=false)
{
static $cache;
if( !isset($cache) ) $cache=array();
if( !isset($cache[$base]) ) $cache[$base]=array();
if( !isset($cache[$base][$id]) )
{
if( $absolute )
$cache[$base][$id]=self::dir($base, array($id, floor($id/100)) );
else
$cache[$base][$id]=self::dir('cache', array($id, floor($id/100), $base) );
}
return $cache[$base][$id];
}
public static function dir($dir, array $dirParts)
{
$name=array_pop($dirParts);
if( is_null($name) ) return $dir;
if( !is_dir($dir.'/'.$name) && !mkdir($dir.'/'.$name, 0775, true) )
throw new Exception(l_t("Couldn't make cache directory '%s'.",$dir.'/'.$name));
return self::dir($dir.'/'.$name, $dirParts);
}
}
?>