-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathLINEBotTiny.php
124 lines (106 loc) · 3.68 KB
/
LINEBotTiny.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* This polyfill of hash_equals() is a modified edition of https://github.com/indigophp/hash-compat/tree/43a19f42093a0cd2d11874dff9d891027fc42214
*
* Copyright (c) 2015 Indigo Development Team
* Released under the MIT license
* https://github.com/indigophp/hash-compat/blob/43a19f42093a0cd2d11874dff9d891027fc42214/LICENSE
*/
if (!function_exists('hash_equals')) {
defined('USE_MB_STRING') or define('USE_MB_STRING', function_exists('mb_strlen'));
function hash_equals($knownString, $userString)
{
$strlen = function ($string) {
if (USE_MB_STRING) {
return mb_strlen($string, '8bit');
}
return strlen($string);
};
// Compare string lengths
if (($length = $strlen($knownString)) !== $strlen($userString)) {
return false;
}
$diff = 0;
// Calculate differences
for ($i = 0; $i < $length; $i++) {
$diff |= ord($knownString[$i]) ^ ord($userString[$i]);
}
return $diff === 0;
}
}
class LINEBotTiny
{
private $channelAccessToken;
private $channelSecret;
public function __construct($channelAccessToken, $channelSecret)
{
$this->channelAccessToken = $channelAccessToken;
$this->channelSecret = $channelSecret;
}
public function parseEvents()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
error_log('Method not allowed');
exit();
}
$entityBody = file_get_contents('php://input');
if (strlen($entityBody) === 0) {
http_response_code(400);
error_log('Missing request body');
exit();
}
if (!hash_equals($this->sign($entityBody), $_SERVER['HTTP_X_LINE_SIGNATURE'])) {
http_response_code(400);
error_log('Invalid signature value');
exit();
}
$data = json_decode($entityBody, true);
if (!isset($data['events'])) {
http_response_code(400);
error_log('Invalid request body: missing events property');
exit();
}
return $data['events'];
}
public function replyMessage($message)
{
$header = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $this->channelAccessToken,
);
$context = stream_context_create([
'http' => [
'ignore_errors' => true,
'method' => 'POST',
'header' => implode("\r\n", $header),
'content' => json_encode($message),
],
]);
$response = file_get_contents('https://api.line.me/v2/bot/message/reply', false, $context);
if (strpos($http_response_header[0], '200') === false) {
error_log('Request failed: ' . $response);
}
}
private function sign($body)
{
$hash = hash_hmac('sha256', $body, $this->channelSecret, true);
$signature = base64_encode($hash);
return $signature;
}
}