2020-04-22 11:05:09 +02:00
|
|
|
<?php
|
|
|
|
|
2020-05-13 11:53:58 +02:00
|
|
|
define('BOT_TOKEN', '<token>');
|
2020-04-22 11:05:09 +02:00
|
|
|
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
|
|
|
|
|
2020-05-13 11:53:58 +02:00
|
|
|
$redis = new Redis();
|
|
|
|
$redis->connect('127.0.0.1', 6379);
|
|
|
|
|
|
|
|
$mysql_user = 'mysql_user';
|
|
|
|
$mysql_pass = 'mysql_password';
|
2020-04-22 11:05:09 +02:00
|
|
|
|
|
|
|
$db = new PDO('mysql:host=127.0.0.1;dbname=certalertbot;charset=utf8mb4', $mysql_user, $mysql_pass);
|
|
|
|
|
|
|
|
$help = "
|
|
|
|
<strong>CertAlert</strong> bot
|
|
|
|
This bot sends an alert when a certificate matching a certain rule is logged in the Certificate Trasparency.
|
|
|
|
|
|
|
|
|
|
|
|
<pre>/list
|
|
|
|
</pre>
|
|
|
|
To list the current rules.
|
|
|
|
|
|
|
|
<pre>/delete <id>
|
|
|
|
</pre>
|
|
|
|
To delete a rule.
|
|
|
|
|
|
|
|
<pre>/add <in/start/end> <string>
|
|
|
|
</pre>
|
|
|
|
To add a rule.
|
2020-05-13 11:53:58 +02:00
|
|
|
<i>in</i> matches the given substring in any postition, <i>start</i> at the beginning and <i>end</i> at the end.
|
2020-04-22 11:05:09 +02:00
|
|
|
|
|
|
|
For special characters use the IDNA encoding.
|
|
|
|
";
|
|
|
|
|
|
|
|
function reply($chatid, $reply) {
|
|
|
|
return file_get_contents(API_URL."sendmessage?chat_id=".$chatid."&text=".urlencode($reply)."&parse_mode=HTML");
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = file_get_contents("php://input");
|
|
|
|
//error_log($content);
|
|
|
|
$update = json_decode($content, true);
|
|
|
|
$chatid = $update["message"]["chat"]["id"];
|
|
|
|
|
|
|
|
$fromid = $update["message"]["from"]["id"];
|
|
|
|
$fromusername = $update["message"]["from"]["username"];
|
|
|
|
$command = explode(" ", $update['message']['text'])[0];
|
|
|
|
|
|
|
|
switch($command) {
|
|
|
|
case '/start':
|
|
|
|
$reply = $help;
|
|
|
|
break;
|
|
|
|
case '/list':
|
|
|
|
$stmt = $db->prepare("SELECT id, type, value FROM rules where userid = ? ORDER BY timestamp ASC");
|
|
|
|
$stmt->execute(array($fromid));
|
|
|
|
$rules = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (!empty($rules)) {
|
|
|
|
$reply = "<strong>ID\tType\tValue</strong>\n";
|
|
|
|
foreach ($rules as $rule) {
|
|
|
|
switch($rule['type']) {
|
|
|
|
case 0:
|
|
|
|
$type = "in";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
$type = "start";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
$type = "end";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$reply .= $rule['id']."\t<i>".$type."</i>\t".htmlentities($rule['value'])."\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$reply = "There are no rules yet";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case '/add':
|
|
|
|
$exp = explode(" ", $update['message']['text']);
|
|
|
|
$type = $exp[1];
|
|
|
|
$value = $exp[2];
|
2020-05-13 11:53:58 +02:00
|
|
|
if (strlen($value) < 5) {
|
|
|
|
$reply = "The filter must be at least 5 chars.";
|
|
|
|
break;
|
|
|
|
}
|
2020-04-22 11:05:09 +02:00
|
|
|
switch($type) {
|
|
|
|
case 'in':
|
|
|
|
$type = 0;
|
|
|
|
break;
|
|
|
|
case 'start':
|
|
|
|
$type = 1;
|
|
|
|
break;
|
|
|
|
case 'end':
|
|
|
|
$type = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$type = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($type > -1) {
|
|
|
|
$stmt = $db->prepare("INSERT INTO rules (userid, chatid, type, value, timestamp) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP())");
|
|
|
|
$stmt->execute(array($fromid, $chatid, $type, $value));
|
2020-05-13 11:53:58 +02:00
|
|
|
$id = $db->lastInsertId();
|
|
|
|
$toadd["id"] = $id;
|
|
|
|
$toadd["value"] = array("t" => $type, "v" => $value, "c" => $chatid);
|
|
|
|
$toadd = json_encode($toadd, JSON_NUMERIC_CHECK);
|
|
|
|
$redis->rPush('toadd', $toadd);
|
2020-04-22 11:05:09 +02:00
|
|
|
$reply = "Rule added, check with /list";
|
|
|
|
} else {
|
|
|
|
$reply = "Invalid rule type.";
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case '/delete':
|
|
|
|
$exp = explode(" ", $update['message']['text']);
|
|
|
|
$id = $exp[1];
|
|
|
|
$stmt = $db->prepare("DELETE FROM rules WHERE id = ? AND userid = ?");
|
|
|
|
$stmt->execute(array($id, $fromid));
|
2020-05-13 11:53:58 +02:00
|
|
|
$redis->rPush('todel', $id);
|
2020-04-22 11:05:09 +02:00
|
|
|
$reply = "Rule ".$id." deleted";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$reply = "Unknown command";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
reply($chatid, $reply);
|
|
|
|
|
|
|
|
?>
|