-
Notifications
You must be signed in to change notification settings - Fork 7
/
telegram.php
272 lines (235 loc) · 6.35 KB
/
telegram.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/* This file hard-linked from sean.taipei/telegram/tgpe.php to tg.pe/telegram.php */
if (!isset($TG))
exit;
require('/usr/share/nginx/tg.pe/config.php');
require('/usr/share/nginx/tg.pe/database.php');
$db = new MyDB();
/* Message Texts */
$msg_help = <<<EOF
Hello, just send me URL.
You can also specific your short code.
<b>Usage</b>
For instance, send me following text:
<pre>https://t.me/tgpebot bot</pre>
Note: You can use <b>a-z</b>, <b>A-Z</b> and <b>0-9</b>.
Minimum length is <b>3</b> characters.
<b>Commands</b>
/my - Show all your links.
/help - Show this message.
<b>About</b>
Developer: @SeanChannel
Source Code: tg.pe/repo
EOF;
if (strpos($TG->data['message']['from']['language_code'], 'zh') !== false)
$msg_help = <<<EOF
安安,請直接傳網址給我
<b>【使用方式】</b>
如果要自訂短網址,可參考以下範例:
<pre>https://t.me/tgpebot bot</pre>
注意:目前僅接受英數字(A-Z, a-z, 0-9)組合、最短 3 個字
<b>【指令列表】</b>
/my - 顯示您建立的連結清單
/help - 顯示此訊息
<b>【關於】</b>
開發者: @SeanChannel
原始碼: tg.pe/repo
EOF;
/* Allow Text in both message and photo caption */
$text = $TG->data['message']['text'] ?? $TG->data['message']['photo']['caption'] ?? '';
if (empty($text)) {
if ($TG->ChatID > 0) # Private Message
$TG->sendMsg([
'parse_mode' => 'HTML',
'text' => $msg_help
]);
exit;
}
/* Handle commands */
if (preg_match('#^[/!](?<cmd>\w+)(?:@' . $TG->botName . ')?(?:\s+(?<args>.+))?$#', $text, $matches)) {
$cmd = strtolower($matches['cmd']);
$args = $matches['args'] ?? '';
switch ($cmd) {
case 'my':
$author = "TG{$TG->FromID}";
$data = $db->findByAuthor($author);
if (count($data) == 0) {
$TG->sendMsg([
'parse_mode' => 'HTML',
'text' => $msg_help
]);
break;
}
$text = "You have <b>" . count($data) . "</b> shorten URLs.\n";
for ($i=0; $i<count($data) && strlen($text)<4000; $i++) {
if (mb_strlen($data[$i]['url']) > 40)
$url = mb_substr($data[$i]['url'], 0, 25) . '...' . mb_substr($data[$i]['url'], -5);
else
$url = $data[$i]['url'];
$url = $TG->enHTML($url);
if (!($i%5))
$text .= "\n";
$text .= ($i+1) . ". tg.pe/{$data[$i]['code']} ";
$text .= "(<code>$url</code>)\n";
}
$TG->sendMsg([
'text' => $text,
'parse_mode' => 'HTML',
'disable_web_page_preview' => true
]);
break;
case 'start':
case 'help':
default:
$TG->sendMsg([
'parse_mode' => 'HTML',
'text' => $msg_help
]);
break;
}
exit;
}
if (strpos($text, '.') !== false // Looks like URL
&& strtolower(substr($text, 0, 4)) !== 'http') // Not start with HTTP or HTTPS
$text = "https://$text"; // Prepend HTTPS scheme
/* Vaildate URL */
if (!preg_match('#^(?P<url>(?P<scheme>https?)://(?P<domain>[^\n\s@%/]+\.[^\n\s@%/]+)(?<path>/[^\n\s]*)?)(?:[\n\s]+(?P<code>[a-zA-Z0-9]+))?$#iu', $text, $matches)) {
if ($TG->ChatID > 0) # Private Message
$TG->sendMsg([
'parse_mode' => 'HTML',
'text' => $msg_help
]);
exit;
}
$scheme = $matches['scheme'];
$url = $matches['url'];
$domain = $matches['domain'];
$code = $matches['code'] ?? '';
$author = "TG{$TG->FromID}";
if (idn_to_ascii($domain) !== $domain) {
$domain = idn_to_ascii($domain);
$path = $matches['path'] ?? '/';
$url = "$scheme://$domain$path";
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
$TG->sendMsg([
'text' => 'Please Send a Vaild URL.'
]);
exit;
}
if (strtolower(substr($domain, -5)) == 'tg.pe') {
$TG->sendMsg([
'text' => 'This URL is short enough.'
]);
exit;
}
if (strlen($code) > 16) { /* Check Code Length */
$TG->sendMsg([
'text' => "Code too long"
]);
exit;
} else if (strlen($code) >= 3) { /* Check Code Existance */
if ($data = $db->findByCode($code)) {
$TG->sendMsg([
'text' => "Already Exist: https://tg.pe/$code\n\n" .
"Original URL: {$data['url']}"
]);
exit;
}
} else if (strlen($code) === 0) { /* Allocate 3-char not-exists code */
if ($code = $db->findCodeByUrl($url)) {
$TG->sendMsg([
'text' => "Success!\n\nhttps://tg.pe/$code",
'reply_markup' => [
'inline_keyboard' => [
[
[
'text' => 'Screen Message',
'url' => "https://www.sean.taipei/sm#t=tg.pe%2F$code"
]
],
[
[
'text' => 'QRCode',
'url' => "https://chart.googleapis.com/chart?chs=512x512&cht=qr&chl=https://tg.pe/$code&choe=UTF-8"
]
]
]
]
]);
exit;
} else
$code = $db->allocateCode();
} else { /* 1 or 2 char only allow admins */
if (!in_array($TG->FromID, TG_ADMINS)) {
$TG->sendMsg([
'text' => "ERROR: Code should be at least 3 chars"
]);
exit;
}
}
if (strpos($url, "fbclid=")) {
$TG->sendMsg([
'parse_mode' => 'HTML',
'text' => "Hey! Please remove <b>fbclid</b> before sharing URLs.\n\n" .
"fbclid is used for Facebook interaction tracking against user privacy.\n\n" .
"You can install this <a href='https://addons.mozilla.org/en-US/firefox/addon/facebook-tracking-removal/'>add-on</a> to <b>auto remove</b> Facebook tracking ID.",
'reply_markup' => [
'inline_keyboard' => [
[
[
'text' => 'Firefox',
'url' => 'https://addons.mozilla.org/en-US/firefox/addon/facebook-tracking-removal/'
],
[
'text' => 'Chrome',
'url' => 'https://chrome.google.com/webstore/detail/tracking-ad-removal-for-f/ldeofbdmhnnocclkaddcnamhbhanaiaj'
]
]
]
]
]);
exit;
}
/* Both $url and $code should be clean */
if (in_array($TG->FromID, $tg_blacklist)) {
$TG->sendMsg([
'parse_mode' => 'Markdown',
'text' => '*You have been banned.*',
]);
exit;
}
/* Create Record */
$error = $db->insert($code, $url, $author);
if ($error[0] === '00000') {
$TG->sendMsg([
'text' => "Success!\n\nhttps://tg.pe/$code",
'reply_markup' => [
'inline_keyboard' => [
[
[
'text' => 'Screen Message',
'url' => "https://www.sean.taipei/sm#t=tg.pe%2F$code"
]
]
]
]
]);
if (preg_match('/(' . implode('|', $domain_blacklist) . ')$/i', $domain))
$TG->sendMsg([
'chat_id' => TG_ADMINS[0],
'parse_mode' => 'HTML',
'text' => "Warning: blacklisted URL\n\n" .
"URL: $url\n" .
"Code: <code>$code</code>\n" .
"Author: #$author (@{$TG->data['message']['from']['username']})",
]);
} else
$TG->sendMsg([
'text' => "ERROR: Something went Wrong, please contact @S_ean\n\n" .
"Code: $code\n" .
"URL: $url\n" .
"Author: $author\n\n" .
"PDO Error Info:\n" .
json_encode($error, JSON_PRETTY_PRINT)
]);