1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Tgallice\FBMessenger\Callback\CallbackEvent; |
8
|
|
|
use Tgallice\FBMessenger\Model\Callback\Entry; |
9
|
|
|
|
10
|
|
|
class WebhookRequestHandler |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ServerRequestInterface |
14
|
|
|
*/ |
15
|
|
|
private $request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* App secret used to verify the request sha1 |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $secret; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $decodedBody; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Entry[] |
31
|
|
|
*/ |
32
|
|
|
private $hydratedEntries; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $body; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $verifyToken; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $secret |
46
|
|
|
* @param string $verifyToken |
47
|
|
|
* @param ServerRequestInterface|null $request |
48
|
|
|
*/ |
49
|
10 |
|
public function __construct($secret, $verifyToken, ServerRequestInterface $request = null) |
50
|
|
|
{ |
51
|
10 |
|
$this->secret = $secret; |
52
|
10 |
|
$this->verifyToken = $verifyToken; |
53
|
10 |
|
$this->request = null === $request ? ServerRequest::fromGlobals() : $request; |
54
|
10 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Check if the token match with the given verify token. |
58
|
|
|
* This is useful in the webhook setup process. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
1 |
|
public function isValidVerifyTokenRequest() |
63
|
|
|
{ |
64
|
1 |
|
if ($this->request->getMethod() !== 'GET') { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$params = $this->request->getQueryParams(); |
69
|
|
|
|
70
|
1 |
|
if (!isset($params['hub.verify_token'])) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return $params['hub.verify_token'] === $this->verifyToken; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return null|string |
79
|
|
|
*/ |
80
|
1 |
|
public function getChallenge() |
81
|
|
|
{ |
82
|
1 |
|
$params = $this->request->getQueryParams(); |
83
|
|
|
|
84
|
1 |
|
return isset($params['hub.challenge']) ? $params['hub.challenge'] : null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Check if the request is a valid webhook request |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
3 |
|
public function isValidCallbackRequest() |
93
|
|
|
{ |
94
|
3 |
|
if (!$this->isValidHeader()) { |
95
|
2 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$decoded = $this->getDecodedBody(); |
99
|
|
|
|
100
|
1 |
|
$object = isset($decoded['object']) ? $decoded['object'] : null; |
101
|
1 |
|
$entry = isset($decoded['entry']) ? $decoded['entry'] : null; |
102
|
|
|
|
103
|
1 |
|
return $object === 'page' && null !== $entry; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if the request is a valid webhook request |
108
|
|
|
* |
109
|
|
|
* @deprecated use WebhookRequestHandler::isValidCallbackRequest() instead |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function isValid() |
114
|
|
|
{ |
115
|
|
|
return $this->isValidCallbackRequest(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return CallbackEvent[] |
120
|
|
|
*/ |
121
|
1 |
|
public function getAllCallbackEvents() |
122
|
|
|
{ |
123
|
1 |
|
$events = []; |
124
|
|
|
|
125
|
1 |
|
foreach ($this->getHydratedEntries() as $hydratedEntry) { |
126
|
1 |
|
$events = array_merge($events, $hydratedEntry->getCallbackEvents()); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
1 |
|
return $events; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return Entry[] |
134
|
|
|
*/ |
135
|
1 |
|
public function getEntries() |
136
|
|
|
{ |
137
|
1 |
|
return $this->getHydratedEntries(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return ServerRequestInterface |
142
|
|
|
*/ |
143
|
1 |
|
public function getRequest() |
144
|
|
|
{ |
145
|
1 |
|
return $this->request; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
4 |
|
public function getDecodedBody() |
152
|
|
|
{ |
153
|
4 |
|
if (isset($this->decodedBody)) { |
154
|
|
|
return $this->decodedBody; |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
$decoded = @json_decode($this->getBody(), true); |
158
|
|
|
|
159
|
4 |
|
return $this->decodedBody = null === $decoded ? [] : $decoded; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return Entry[] |
164
|
|
|
*/ |
165
|
2 |
|
private function getHydratedEntries() |
166
|
|
|
{ |
167
|
2 |
|
if (isset($this->hydratedEntries)) { |
168
|
|
|
return $this->hydratedEntries; |
169
|
|
|
} |
170
|
|
|
|
171
|
2 |
|
$decodedBody = $this->getDecodedBody(); |
172
|
2 |
|
$entries = $decodedBody['entry']; |
173
|
|
|
|
174
|
2 |
|
$hydrated = []; |
175
|
|
|
|
176
|
2 |
|
foreach ($entries as $entry) { |
177
|
2 |
|
$hydrated[] = Entry::create($entry); |
178
|
2 |
|
} |
179
|
|
|
|
180
|
2 |
|
return $this->hydratedEntries = $hydrated; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
6 |
|
private function getBody() |
187
|
|
|
{ |
188
|
6 |
|
if (isset($this->body)) { |
189
|
1 |
|
return $this->body; |
190
|
|
|
} |
191
|
|
|
|
192
|
6 |
|
$this->body = (string) $this->request->getBody(); |
193
|
|
|
|
194
|
6 |
|
return $this->body; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
3 |
|
private function isValidHeader() |
201
|
|
|
{ |
202
|
3 |
|
$headers = $this->request->getHeader('X-Hub-Signature'); |
203
|
|
|
|
204
|
3 |
|
if (empty($headers)) { |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
$signature = XHubSignature::parseHeader($headers[0]); |
209
|
|
|
|
210
|
3 |
|
return XHubSignature::validate($this->getBody(), $this->secret, $signature); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|