1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
use Tgallice\FBMessenger\Callback\CallbackEvent; |
8
|
|
|
use Tgallice\FBMessenger\Model\Callback\Entry; |
9
|
|
|
|
10
|
|
|
class WebhookRequestHandler |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var RequestInterface |
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
|
|
|
* @param string $secret |
41
|
|
|
* @param RequestInterface|null $request |
42
|
|
|
*/ |
43
|
8 |
|
public function __construct($secret, RequestInterface $request = null) |
44
|
|
|
{ |
45
|
8 |
|
$this->secret = $secret; |
46
|
8 |
|
$this->request = null === $request ? ServerRequest::fromGlobals() : $request; |
47
|
8 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check if the request is a valid webhook request |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
3 |
|
public function isValid() |
55
|
|
|
{ |
56
|
3 |
|
if (!$this->isValidHeader()) { |
57
|
2 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$decoded = $this->getDecodedBody(); |
61
|
|
|
|
62
|
1 |
|
$object = isset($decoded['object']) ? $decoded['object'] : null; |
63
|
1 |
|
$entry = isset($decoded['entry']) ? $decoded['entry'] : null; |
64
|
|
|
|
65
|
1 |
|
return $object === 'page' && null !== $entry; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return CallbackEvent[] |
70
|
|
|
*/ |
71
|
1 |
|
public function getAllCallbackEvents() |
72
|
|
|
{ |
73
|
1 |
|
$events = []; |
74
|
|
|
|
75
|
1 |
|
foreach ($this->getHydratedEntries() as $hydratedEntry) { |
76
|
1 |
|
$events = array_merge($events, $hydratedEntry->getCallbackEvents()); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
return $events; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Entry[] |
84
|
|
|
*/ |
85
|
1 |
|
public function getEntries() |
86
|
|
|
{ |
87
|
1 |
|
return $this->getHydratedEntries(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return RequestInterface |
92
|
|
|
*/ |
93
|
1 |
|
public function getRequest() |
94
|
|
|
{ |
95
|
1 |
|
return $this->request; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
4 |
|
public function getDecodedBody() |
102
|
|
|
{ |
103
|
4 |
|
if (isset($this->decodedBody)) { |
104
|
|
|
return $this->decodedBody; |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
$decoded = @json_decode($this->getBody(), true); |
108
|
|
|
|
109
|
4 |
|
return $this->decodedBody = null === $decoded ? [] : $decoded; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Entry[] |
114
|
|
|
*/ |
115
|
2 |
|
private function getHydratedEntries() |
116
|
|
|
{ |
117
|
2 |
|
if (isset($this->hydratedEntries)) { |
118
|
|
|
return $this->hydratedEntries; |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
$decodedBody = $this->getDecodedBody(); |
122
|
2 |
|
$entries = $decodedBody['entry']; |
123
|
|
|
|
124
|
2 |
|
$hydrated = []; |
125
|
|
|
|
126
|
2 |
|
foreach ($entries as $entry) { |
127
|
2 |
|
$hydrated[] = Entry::create($entry); |
128
|
2 |
|
} |
129
|
|
|
|
130
|
2 |
|
return $this->hydratedEntries = $hydrated; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
6 |
|
private function getBody() |
137
|
|
|
{ |
138
|
6 |
|
if (isset($this->body )) { |
139
|
1 |
|
return $this->body; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Reset pointer to the beginning |
143
|
6 |
|
$this->request->getBody()->rewind(); |
144
|
6 |
|
$this->body = $this->request->getBody()->getContents(); |
145
|
6 |
|
$this->request->getBody()->rewind(); |
146
|
|
|
|
147
|
6 |
|
return $this->body; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
3 |
|
private function isValidHeader() |
154
|
|
|
{ |
155
|
3 |
|
$headers = $this->request->getHeader('X-Hub-Signature'); |
156
|
|
|
|
157
|
3 |
|
if (empty($headers)) { |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
3 |
|
$signature = XHubSignature::parseHeader($headers[0]); |
162
|
|
|
|
163
|
3 |
|
return XHubSignature::validate($this->getBody(), $this->secret, $signature); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|