|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Distilleries\Messenger\Http\Controllers\Base; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Created by PhpStorm. |
|
7
|
|
|
* User: mfrancois |
|
8
|
|
|
* Date: 12/08/2016 |
|
9
|
|
|
* Time: 15:10 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
use Distilleries\Messenger\Contracts\MessengerReceiverContract; |
|
13
|
|
|
use \Log; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
|
|
16
|
|
|
trait WebHookBaseTrait |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
16 |
|
public function getValidHook(Request $request) |
|
20
|
|
|
{ |
|
21
|
16 |
|
$hub = $request->input('hub_mode'); |
|
22
|
16 |
|
$verify_token = $request->input('hub_verify_token'); |
|
23
|
|
|
|
|
24
|
16 |
|
if ($hub === 'subscribe' && urldecode($verify_token) === config('messenger.validation_token')) { |
|
25
|
4 |
|
return response($request->get('hub_challenge')); |
|
26
|
|
|
} else { |
|
27
|
12 |
|
return abort(403); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
36 |
|
public function postMessage(Request $request, MessengerReceiverContract $messenger) |
|
33
|
|
|
{ |
|
34
|
36 |
|
$object = $request->input('object'); |
|
35
|
|
|
|
|
36
|
36 |
|
if (empty($object)) { |
|
37
|
4 |
|
return abort(403); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
32 |
|
if ($object == 'page') { |
|
41
|
32 |
|
$entry = $request->input('entry'); |
|
42
|
32 |
|
$entry = json_decode(json_encode($entry), false); |
|
43
|
|
|
|
|
44
|
32 |
|
if (empty($entry) || !is_array($entry)) { |
|
45
|
8 |
|
return abort(403); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
24 |
|
$result = ""; |
|
49
|
|
|
|
|
50
|
24 |
|
foreach ($entry as $pageEntry) { |
|
51
|
|
|
|
|
52
|
24 |
|
if (!is_array($pageEntry->messaging)) { |
|
53
|
4 |
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
20 |
|
foreach ($pageEntry->messaging as $messagingEvent) { |
|
57
|
|
|
|
|
58
|
20 |
|
if (!empty($messagingEvent->optin)) { |
|
59
|
4 |
|
$result .= $messenger->receivedAuthentication($messagingEvent); |
|
60
|
3 |
|
} else { |
|
61
|
16 |
|
if (!empty($messagingEvent->message)) { |
|
62
|
4 |
|
$result .= $messenger->receivedMessage($messagingEvent); |
|
63
|
3 |
|
} else { |
|
64
|
12 |
|
if (!empty($messagingEvent->delivery)) { |
|
65
|
4 |
|
$result .= $messenger->receivedDeliveryConfirmation($messagingEvent); |
|
66
|
3 |
|
} else { |
|
67
|
8 |
|
if (!empty($messagingEvent->postback)) { |
|
68
|
4 |
|
$result .= $messenger->receivedPostback($messagingEvent); |
|
69
|
3 |
|
} else { |
|
70
|
9 |
|
$result .= $messenger->defaultHookUndefinedAction($messagingEvent); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
15 |
|
} |
|
76
|
18 |
|
} |
|
77
|
18 |
|
} |
|
78
|
|
|
|
|
79
|
24 |
|
return response($result); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: