1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mfrancois |
5
|
|
|
* Date: 31/07/2016 |
6
|
|
|
* Time: 19:09 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Distilleries\Messenger\Http\Controllers; |
10
|
|
|
|
11
|
|
|
use Distilleries\Messenger\Contracts\MessengerReceiverContract; |
12
|
|
|
use Illuminate\Routing\Controller; |
13
|
|
|
use Log; |
14
|
|
|
use Illuminate\Http\Request; |
15
|
|
|
|
16
|
|
|
class WebHookController extends Controller |
17
|
|
|
{ |
18
|
16 |
|
public function getValidHook(Request $request) |
19
|
|
|
{ |
20
|
16 |
|
$hub = $request->input('hub_mode'); |
21
|
16 |
|
$verify_token = $request->input('hub_verify_token'); |
22
|
|
|
|
23
|
16 |
|
if ($hub === 'subscribe' && urldecode($verify_token) === config('messenger.validation_token')) { |
24
|
4 |
|
return response($request->get('hub_challenge')); |
25
|
|
|
} else { |
26
|
12 |
|
return abort(403); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
36 |
|
public function postMessage(Request $request, MessengerReceiverContract $messenger) |
32
|
|
|
{ |
33
|
36 |
|
$object = $request->input('object'); |
34
|
|
|
|
35
|
36 |
|
if (empty($object)) { |
36
|
4 |
|
return abort(403); |
37
|
|
|
} |
38
|
|
|
|
39
|
32 |
|
if ($object == 'page') { |
40
|
32 |
|
$entry = $request->input('entry'); |
41
|
32 |
|
$entry = json_decode(json_encode($entry), false); |
42
|
|
|
|
43
|
32 |
|
if (empty($entry) || !is_array($entry)) { |
44
|
8 |
|
return abort(403); |
45
|
|
|
} |
46
|
|
|
|
47
|
24 |
|
$result = ""; |
48
|
|
|
|
49
|
24 |
|
foreach ($entry as $pageEntry) { |
50
|
|
|
|
51
|
24 |
|
if (!is_array($pageEntry->messaging)) { |
52
|
4 |
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
20 |
|
foreach ($pageEntry->messaging as $messagingEvent) { |
56
|
|
|
|
57
|
20 |
|
if (!empty($messagingEvent->optin)) { |
58
|
4 |
|
$result .= $messenger->receivedAuthentication($messagingEvent); |
59
|
3 |
|
} else { |
60
|
16 |
|
if (!empty($messagingEvent->message)) { |
61
|
4 |
|
$result .= $messenger->receivedMessage($messagingEvent); |
62
|
3 |
|
} else { |
63
|
12 |
|
if (!empty($messagingEvent->delivery)) { |
64
|
4 |
|
$result .= $messenger->receivedDeliveryConfirmation($messagingEvent); |
65
|
3 |
|
} else { |
66
|
8 |
|
if (!empty($messagingEvent->postback)) { |
67
|
4 |
|
$result .= $messenger->receivedPostback($messagingEvent); |
68
|
3 |
|
} else { |
69
|
9 |
|
$result .= $messenger->defaultHookUndefinedAction($messagingEvent); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
15 |
|
} |
75
|
18 |
|
} |
76
|
18 |
|
} |
77
|
|
|
|
78
|
24 |
|
return response($result); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
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: