|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BramR\TorExits\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use BramR\TorExits\IpList; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class IpListJsonCache |
|
9
|
|
|
*/ |
|
10
|
|
|
class IpListJsonCache implements IpListCacheInterface |
|
11
|
|
|
{ |
|
12
|
|
|
const DEFAULT_LOCATION = '/tmp/tor-exits.cache.json'; |
|
13
|
|
|
const DEFAULT_TTL = 7200; |
|
14
|
|
|
|
|
15
|
|
|
protected $location; |
|
16
|
|
|
protected $ttl; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Setter for location |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $location |
|
22
|
|
|
* @return IpListJsonCache |
|
23
|
|
|
*/ |
|
24
|
6 |
|
public function setLocation($location) |
|
25
|
|
|
{ |
|
26
|
6 |
|
$this->location = $location; |
|
27
|
|
|
|
|
28
|
6 |
|
return $this; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Setter for ttl |
|
33
|
|
|
* |
|
34
|
|
|
* @param int $ttl |
|
35
|
|
|
* @return IpListJsonCache |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function setTtl($ttl) |
|
38
|
|
|
{ |
|
39
|
2 |
|
if (intval($ttl) < 1) { |
|
40
|
1 |
|
throw new \InvalidArgumentException('Invalid ttl.'); |
|
41
|
|
|
} |
|
42
|
1 |
|
$this->ttl = $ttl; |
|
43
|
|
|
|
|
44
|
1 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Getter for location |
|
49
|
|
|
* |
|
50
|
|
|
* return string |
|
51
|
|
|
*/ |
|
52
|
7 |
|
public function getLocation() |
|
53
|
|
|
{ |
|
54
|
7 |
|
return $this->location ? $this->location : self::DEFAULT_LOCATION; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Getter for location |
|
59
|
|
|
* |
|
60
|
|
|
* return int |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function getTtl() |
|
63
|
|
|
{ |
|
64
|
3 |
|
return $this->ttl ? $this->ttl : self::DEFAULT_TTL; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
3 |
|
public function store(IpList $ipList, $ttl = null) |
|
71
|
|
|
{ |
|
72
|
3 |
|
if (is_null($ttl)) { |
|
73
|
2 |
|
$ttl = $this->getTtl(); |
|
74
|
2 |
|
} |
|
75
|
3 |
|
$store = new \stdClass(); |
|
76
|
3 |
|
$store->iplist = $ipList; |
|
77
|
3 |
|
$store->expires = $this->calculateExpiration($ttl)->format('c'); |
|
78
|
|
|
|
|
79
|
2 |
|
if (!is_writable(dirname($this->getLocation())) || |
|
80
|
1 |
|
(file_exists($this->getLocation()) && !is_writable($this->getLocation())) |
|
81
|
2 |
|
) { |
|
82
|
1 |
|
return false; |
|
83
|
|
|
} |
|
84
|
1 |
|
return (bool) file_put_contents($this->getLocation(), json_encode($store)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public function fetch() |
|
91
|
|
|
{ |
|
92
|
4 |
|
if (!is_readable($this->getLocation())) { |
|
93
|
1 |
|
return null; |
|
94
|
|
|
} |
|
95
|
3 |
|
$data = json_decode(file_get_contents($this->getLocation())); |
|
96
|
3 |
|
if (!isset($data->iplist) || !isset($data->expires)) { |
|
97
|
1 |
|
return null; |
|
98
|
|
|
} |
|
99
|
2 |
|
if (new \DateTime($data->expires) < new \DateTime()) { |
|
100
|
1 |
|
return null; |
|
101
|
|
|
} |
|
102
|
1 |
|
return new IpList($data->iplist); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* calculateExpiration, calculate expiration DateTime based on ttl |
|
107
|
|
|
* |
|
108
|
|
|
* @param int|null $ttl |
|
109
|
|
|
* @return DateTime |
|
110
|
|
|
*/ |
|
111
|
3 |
|
protected function calculateExpiration($ttl = null) |
|
112
|
|
|
{ |
|
113
|
3 |
|
if (!is_int($ttl) && $ttl < 1) { |
|
114
|
1 |
|
throw new \InvalidArgumentException('Invalid ttl.'); |
|
115
|
|
|
} |
|
116
|
2 |
|
return new \DateTime(sprintf('+%d seconds', $ttl)); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|