1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\ProxyClient; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\ClearCapable; |
15
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable; |
16
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable; |
17
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* LiteSpeed Web Server (LSWS) invalidator. |
21
|
|
|
* |
22
|
|
|
* @author Yanick Witschi <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class LiteSpeed extends HttpProxyClient implements PurgeCapable, TagCapable, ClearCapable |
25
|
|
|
{ |
26
|
|
|
private $headerLines = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function clear() |
32
|
|
|
{ |
33
|
|
|
$this->headerLines[] = 'X-LiteSpeed-Purge: *'; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function purge($url, array $headers = []) |
42
|
|
|
{ |
43
|
|
|
$this->headerLines[] = 'X-LiteSpeed-Purge: ' . $url; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
protected function configureOptions() |
52
|
|
|
{ |
53
|
|
|
$resolver = parent::configureOptions(); |
54
|
|
|
|
55
|
|
|
$resolver->setRequired(['target_dir']); |
56
|
|
|
$resolver->setAllowedTypes('target_dir', 'string'); |
57
|
|
|
|
58
|
|
|
return $resolver; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function invalidateTags(array $tags) |
65
|
|
|
{ |
66
|
|
|
$this->headerLines[] = 'X-LiteSpeed-Purge: ' . implode(', ', preg_filter('/^/', 'tag=', $tags)); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function flush() |
76
|
|
|
{ |
77
|
|
|
$filename = $this->createFile(); |
78
|
|
|
|
79
|
|
|
$url = '/' . $filename; |
80
|
|
|
|
81
|
|
|
$this->queueRequest('GET', $url, []); |
82
|
|
|
|
83
|
|
|
$result = parent::flush(); |
84
|
|
|
|
85
|
|
|
// Reset |
86
|
|
|
$this->headerLines = []; |
87
|
|
|
unlink($this->options['target_dir'] . '/' . $filename); |
88
|
|
|
|
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Creates the file and returns the file name. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
private function createFile() |
98
|
|
|
{ |
99
|
|
|
$content = '<?php' . "\n\n"; |
100
|
|
|
|
101
|
|
|
foreach ($this->headerLines as $header) { |
102
|
|
|
$content .= sprintf('header(\'%s\');', $header) . "\n"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$filename = $this->generateUrlSafeRandomFileName(); |
106
|
|
|
|
107
|
|
|
file_put_contents($this->options['target_dir'] . '/' . $filename, $content); |
108
|
|
|
|
109
|
|
|
return $filename; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function generateUrlSafeRandomFileName() |
113
|
|
|
{ |
114
|
|
|
$filename = 'fos_cache_litespeed_purger_'; |
115
|
|
|
|
116
|
|
|
if (function_exists('random_bytes')) { |
117
|
|
|
$filename .= bin2hex(random_bytes(20)); |
118
|
|
|
} else { |
119
|
|
|
$filename .= sha1(mt_rand() . mt_rand() . mt_rand()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$filename .= '.php'; |
123
|
|
|
|
124
|
|
|
return $filename; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|