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
|
|
|
* @param string $secret |
36
|
|
|
* @param RequestInterface|null $request |
37
|
|
|
*/ |
38
|
|
|
public function __construct($secret, RequestInterface $request = null) |
39
|
|
|
{ |
40
|
|
|
$this->secret = $secret; |
41
|
|
|
$this->request = null === $request ? ServerRequest::fromGlobals() : $request; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Check if the request is a valid webhook request |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function isValid() |
50
|
|
|
{ |
51
|
|
|
$payload = (string) $this->request->getBody(); |
52
|
|
|
$header = $this->request->getHeader('X-Hub-Signature'); |
53
|
|
|
$signature = XHubSignature::parseHeader($header); |
|
|
|
|
54
|
|
|
|
55
|
|
|
if (!XHubSignature::validate($payload, $this->secret, $signature)) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$decoded = $this->getDecodedBody(); |
60
|
|
|
|
61
|
|
|
$object = isset($decoded['object']) ? $decoded['object'] : null; |
62
|
|
|
$entry = isset($decoded['entry']) ? $decoded['entry'] : null; |
63
|
|
|
|
64
|
|
|
return $object === 'page' && null !== $entry; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return CallbackEvent[] |
69
|
|
|
*/ |
70
|
|
|
public function getAllCallbackEvents() |
71
|
|
|
{ |
72
|
|
|
$events = []; |
73
|
|
|
|
74
|
|
|
foreach ($this->getHydratedEntries() as $hydratedEntry) { |
75
|
|
|
$events = array_merge($events, $hydratedEntry->getCallbackEvents()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $events; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Entry[] |
83
|
|
|
*/ |
84
|
|
|
public function getEntries() |
85
|
|
|
{ |
86
|
|
|
return $this->getHydratedEntries(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return RequestInterface |
91
|
|
|
*/ |
92
|
|
|
public function getRequest() |
93
|
|
|
{ |
94
|
|
|
return $this->request; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function getDecodedBody() |
101
|
|
|
{ |
102
|
|
|
if (isset($this->decodedBody)) { |
103
|
|
|
return $this->decodedBody; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$body = (string) $this->request->getBody(); |
107
|
|
|
$decoded = @json_decode($body, true); |
108
|
|
|
|
109
|
|
|
return $this->decodedBody = null === $decoded ? [] : $decoded; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Entry[] |
114
|
|
|
*/ |
115
|
|
|
private function getHydratedEntries() |
116
|
|
|
{ |
117
|
|
|
if (isset($this->hydratedEntries)) { |
118
|
|
|
return $this->hydratedEntries; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$decodedBody = $this->getDecodedBody(); |
122
|
|
|
$entries = $decodedBody['entry']; |
123
|
|
|
|
124
|
|
|
$hydrated = []; |
125
|
|
|
|
126
|
|
|
foreach ($entries as $entry) { |
127
|
|
|
$hydrated[] = Entry::create($entry); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->hydratedEntries = $hydrated; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: