-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
343 lines (309 loc) · 11.5 KB
/
index.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?
//configs
require_once('config.php');
function exec_curl_request($handle) {
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error $errno: $error\n");
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code >= 500) {
// do not wat to DDOS server if something goes wrong
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, true);
error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
if ($http_code == 401) {
throw new Exception('Invalid access token provided');
}
return false;
} else {
$response = json_decode($response, true);
if (isset($response['description'])) {
error_log("Request was successfull: {$response['description']}\n");
}
$response = $response['result'];
}
return $response;
}
function apiRequestWebhook($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
$parameters["method"] = $method;
header("Content-Type: application/json");
echo json_encode($parameters);
return true;
}
function apiRequest($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
foreach ($parameters as $key => &$val) {
// encoding to JSON array parameters, for example reply_markup
if (!is_numeric($val) && !is_string($val)) {
$val = json_encode($val);
}
}
$url = API_URL.$method.'?'.http_build_query($parameters);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
return exec_curl_request($handle);
}
function apiRequestJson($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
$parameters["method"] = $method;
$handle = curl_init(API_URL);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
return exec_curl_request($handle);
}
$gram=json_decode(file_get_contents('php://input'),true);
if ($gram['message']['reply_to_message']!=null) die();
if($gram!=null){
$str = trim($gram['message']['text']);
$str = stripslashes($str);
$str = htmlspecialchars($str);
$input=array(
'chat_id' =>$gram['message']['chat']['id'],
'user_id' =>$gram['message']['from']['id'],
'username' =>$gram['message']['from']['username'],
'message' =>$str,
'message_id' =>$gram['message']['message_id']
);
}else die();
if (isset($gram['message']['new_chat_participant'])) {
$a=array("Привет", "Здравствуй", "Приветище", "Здарова", "Шалом", "Не стой на месте, проходи", "ну и хули припёрся?", "Ну и что ты тут забыл?", "ПХП ГАВНО!!!");
$output=$a[array_rand($a)];
apiRequestJson("sendMessage", array('chat_id' => $input['chat_id'], "text" => $output, 'reply_to_message_id'=>$input['message_id']));
}
if (file_exists("promise.".$input['user_id'])){
$command=null;
if(FindText($input['message'],'отмен')){
$a=array("Хорошо", "Хозяин-барин", "Ну как хочешь", "Как скажешь, {m-name}", "Ок", "Ну ладно(((");
$output=$a[array_rand($a)];
} else $command=file_get_contents("promise.".$input['user_id']);
switch($command){
case 'Password4DB':
$query="INSERT INTO passwords( p_datetime, pname, pass ) VALUES (
'".date("Y-m-d H:i:s")."','". $input['message'] ."','".file_get_contents("tmp_pass")."')";
Query2DB($query);
$output="Паролю ".file_get_contents("tmp_pass")." присвоено имя \"". $input['message'] ."\".\r\n";
break;
case 'SetCalendar':
$output=ToDoSomething('Scheldure',$input['message']);
break;
}
unlink("promise.".$input['user_id']);
}else{
$output.=AnalizeMessage($input['message']);
}
if($output!=null) AddInJournal($input);
$a=array(
'chat_id'=>$input['chat_id'],
'text'=>$output,
'parse_mode'=>'Markdown',
'reply_to_message_id'=>$input['message_id'],
'disable_web_page_preview'=>null
);
apiRequestJson("sendMessage", $a);
function AnalizeMessage($text){
$output=null;
$keywords=array(
'Greeterings'=>array('доброе','добрый','приве','здравств','дрям','здоров','здаров'),
'WorkWithJournal'=>array('журнал'),
'Passwords'=>array('пароль'),
'MarkKing'=>array('#узбек'),
'Amen'=>array('гавн', 'говн', 'govn'),
'IntuitLink'=>array('курсы', 'изуч'),
'Scheldure'=>array('напомни','напоминай')
);
foreach ($keywords as $key => $value)
foreach ($value as $word)
if(mb_stripos($text,$word,0,"utf-8")!==false)
$output .= ToDoSomething($key,$text);
return $output;
}
function ToDoSomething($keyword,$text){
global $input;
$output=null;
switch($keyword){
case 'Greeterings':
$a=array("Привет", "Здравствуй", "Приветище", "Здарова");
if(rand(1,2)==1){
$curhour=date("G");
switch ($curhour){
case ($curhour<6):
case ($curhour>=23):
$output="Доброй ночи";
break;
case ($curhour>=6 && $curhour<12):
$output="Доброе утро";
break;
case ($curhour>=12 && $curhour<18):
$output="Добрый день";
break;
case ($curhour>=18 && $curhour<23):
$output="Добрый вечер";
break;
}
}else{
$output=$a[array_rand($a)];
}
break;
case 'WorkWithJournal':
$mysqli = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
if ($mysqli->connect_errno) {
$output="Не удалось подключиться к MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if(FindText($text,array('очисти','удали','снеси'))){
$query="truncate journal";
if ($result = $mysqli->query($query)) {
$output= "Журнал очищен, {m-name}.\r\n";
}
}else{
$output="Последние записи из журнала:\r\n";
if (!$mysqli->query("SET NAMES 'utf8'")){
$output="Не удалось подключиться: (" . $mysqli->errno . ") " . $mysqli->error;
}
$mysqli->query("SET SESSION collation_connection = 'utf8_general_ci'");
$query="select * from journal ORDER BY id DESC LIMIT 10";
if ($result = $mysqli->query($query)) {
while ($row = mysqli_fetch_assoc($result)) $output.= "{$row['username']} ({$row['chat_id']}) в {$row['m_datetime']}: {$row['message']} \r\n";
}
}
$mysqli->close();
break;
case 'Passwords':
if(FindText($text,array('генер','дела','придум'))){
$chars="qazxswedcvfrtgbnhyujmkiolp1234567890QAZXSWEDCVFRTGBNHYUJMKIOLP";
if(mb_stripos($text,"сложн",0,"utf-8")!==false) $chars.="~!@#$%^&*_+/.,<>[]{};:-=";
$max=8;
$size=StrLen($chars)-1;
$output=null;
while($max--) $output.=$chars[rand(0,$size)];
file_put_contents("tmp_pass", $output);
}elseif(FindText($text,array('запомни','сохрани'))){
file_put_contents("promise.".$input['user_id'], "Password4DB");
$a=array("Как обозвать?", "Как назвать?", "Какой ярлык присвоить?", "Какое имя ему дать?", "Как назовём?", "Какой псевдоним ему присвоить?");
$output=$a[array_rand($a)];
}elseif(FindText($text,array('покажи','выведи','закешь','показ'))){
$mysqli = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET SESSION collation_connection = 'utf8_general_ci'");
if(FindText($text,'парол')){
$output="Список паролей:\r\n";
$query="select * from passwords";
}elseif(preg_match('/пароль ([a-zA-Z а-яА-Я0-9ёЁ]+)/ui', $text, $matches)){
$query="select * from passwords where pname='".$matches[1]."'";
}
$flag=false;
if ($result = $mysqli->query($query)) while ($row = mysqli_fetch_assoc($result)){
$output.= "{$row['pname']} ({$row['p_datetime']}): {$row['pass']} \r\n";
$flag=true;
}
if (!$flag) $output="Список пуст.\r\n";
$mysqli->close();
}elseif(FindText($text,array('очисти','удали','снеси'))){
if(FindText($text,'пароли')){
$query="truncate passwords";
$tmp="Пароли удалены";
}elseif(preg_match('/пароль ([a-zA-Z а-яА-Я0-9ёЁ]+)/ui', $text, $matches)){
$query="delete from passwords where pname='".$matches[1]."'";
$tmp="Пароль удалён";
echo $query;
}
Query2DB($query);
$a=array("Готово", "Задание выполнено", "Сделано", $tmp.", {m-name}", "Сделала", "Выполнено");
$output=$a[array_rand($a)];
}
break;
case 'IntuitLink':
if(FindText($text,array('пхп','php','пехепе'))){
$output="http://www.intuit.ru/studies/courses/42/42/info или http://www.specialist.ru/dictionary/definition/php";
}
break;
case 'Amen':
if(FindText($text,array('пхп','php','пехепе'))){
$a=array("Аминь!", "Воистину гавно!", "Базаришь");
$output=$a[array_rand($a)];
}
break;
case 'MarkKing':
if(preg_match('/(@[A-Za-z0-9_]{5,}) #узбек/ui', $text, $matches)){
$query="insert into kings(username) values ('".$matches[1]."')";
$output="Отметила";
}
break;
}
return $output;
}
function AddInJournal($input){
$query="INSERT INTO journal( m_datetime, chat_id, user_id, username, message ) VALUES (
'".date("Y-m-d H:i:s")."',
'".$input['chat_id']."',
'".$input['user_id']."',
'".$input['username']."',
'".$input['message']."'
)";
return Query2DB($query);
}
function FindText($text,$a){
$flag=false;
if(gettype($a)=='array')
foreach($a as $item){
if(mb_stripos($text,$item,0,"utf-8")!==false) $flag=true;
} else if(mb_stripos($text,$a,0,"utf-8")!==false) $flag=true;
return $flag;
}
function Query2DB($query){
$mysqli = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
if ($mysqli->connect_errno) {
$output="Не удалось подключиться к MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$mysqli->query("SET SESSION collation_connection = 'utf8_general_ci'");
if (!$mysqli->query("SET NAMES 'utf8'")){
$output="Не удалось подключиться: (" . $mysqli->errno . ") " . $mysqli->error;
}
$a=array();
if ($result = $mysqli->query($query)) {
while ($row = mysqli_fetch_assoc($result)) foreach($row as $value) array_push($a,$value);
}
$mysqli->close();
return $a;
}
?>