|
1
|
|
|
<?php |
|
2
|
|
|
namespace Aoe\Varnish\System; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2018 AOE GmbH <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* All rights reserved |
|
10
|
|
|
* |
|
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
12
|
|
|
* free software; you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU General Public License as published by |
|
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
15
|
|
|
* (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* The GNU General Public License can be found at |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
19
|
|
|
* |
|
20
|
|
|
* This script is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
26
|
|
|
***************************************************************/ |
|
27
|
|
|
|
|
28
|
|
|
use Aoe\Varnish\Domain\Model\TagInterface; |
|
29
|
|
|
use Aoe\Varnish\TYPO3\Configuration\ExtensionConfiguration; |
|
30
|
|
|
use TYPO3\CMS\Core\Log\LogManager; |
|
31
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
32
|
|
|
|
|
33
|
|
|
class Varnish implements SingletonInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var Http |
|
37
|
|
|
*/ |
|
38
|
|
|
private $http; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ExtensionConfiguration |
|
42
|
|
|
*/ |
|
43
|
|
|
private $extensionConfiguration; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var LogManager |
|
47
|
|
|
*/ |
|
48
|
|
|
private $logManager; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Http $http |
|
52
|
|
|
* @param ExtensionConfiguration $extensionConfiguration |
|
53
|
|
|
* @param LogManager $logManager |
|
54
|
|
|
*/ |
|
55
|
5 |
|
public function __construct(Http $http, ExtensionConfiguration $extensionConfiguration, LogManager $logManager) |
|
56
|
|
|
{ |
|
57
|
5 |
|
$this->http = $http; |
|
58
|
5 |
|
$this->extensionConfiguration = $extensionConfiguration; |
|
59
|
5 |
|
$this->logManager = $logManager; |
|
60
|
|
|
|
|
61
|
5 |
|
register_shutdown_function([$this, 'shutdown']); |
|
62
|
5 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function shutdown() |
|
68
|
|
|
{ |
|
69
|
1 |
|
$phrases = $this->http->wait(); |
|
70
|
1 |
|
if (is_array($phrases)) { |
|
71
|
1 |
|
foreach ($phrases as $phrase) { |
|
72
|
1 |
|
if ($phrase['success']) { |
|
73
|
1 |
|
$this->logManager->getLogger(__CLASS__)->info($phrase['reason']); |
|
74
|
|
|
} else { |
|
75
|
1 |
|
$this->logManager->getLogger(__CLASS__)->alert($phrase['reason']); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
1 |
|
return $phrases; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param TagInterface $tag |
|
84
|
|
|
* @return Varnish |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function banByTag(TagInterface $tag) |
|
87
|
|
|
{ |
|
88
|
2 |
|
if (false === $tag->isValid()) { |
|
89
|
1 |
|
throw new \RuntimeException('Tag is not valid', 1435159558); |
|
90
|
|
|
} |
|
91
|
1 |
|
$this->request('BAN', ['X-Ban-Tags' => $tag->getIdentifier()], $this->extensionConfiguration->getBanTimeout()); |
|
92
|
1 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return Varnish |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function banAll() |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->request('BAN', ['X-Ban-All' => '1'], $this->extensionConfiguration->getBanTimeout()); |
|
101
|
1 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $regex |
|
106
|
|
|
* @return Varnish |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function banByRegex($regex) |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->request('BAN', ['X-Ban-Regex' => $regex], $this->extensionConfiguration->getBanTimeout()); |
|
111
|
1 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $method |
|
116
|
|
|
* @param array $headers |
|
117
|
|
|
* @param integer $timeout |
|
118
|
|
|
*/ |
|
119
|
3 |
|
private function request($method, $headers = [], $timeout = null) |
|
120
|
|
|
{ |
|
121
|
3 |
|
if ($timeout === null) { |
|
122
|
|
|
$timeout = $this->extensionConfiguration->getDefaultTimeout(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
3 |
|
foreach ($this->extensionConfiguration->getHosts() as $host) { |
|
126
|
3 |
|
$this->http->request($method, $host, $headers, $timeout); |
|
127
|
|
|
} |
|
128
|
3 |
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|