certalert.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. define('BOT_TOKEN', '<token>');
  3. define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
  4. $redis = new Redis();
  5. $redis->connect('127.0.0.1', 6379);
  6. $mysql_user = 'mysql_user';
  7. $mysql_pass = 'mysql_password';
  8. $db = new PDO('mysql:host=127.0.0.1;dbname=certalertbot;charset=utf8mb4', $mysql_user, $mysql_pass);
  9. $help = "
  10. <strong>CertAlert</strong> bot
  11. This bot sends an alert when a certificate matching a certain rule is logged in the Certificate Trasparency.
  12. <pre>/list
  13. </pre>
  14. To list the current rules.
  15. <pre>/delete &lt;id&gt;
  16. </pre>
  17. To delete a rule.
  18. <pre>/add &lt;in/start/end&gt; &lt;string&gt;
  19. </pre>
  20. To add a rule.
  21. <i>in</i> matches the given substring in any postition, <i>start</i> at the beginning and <i>end</i> at the end.
  22. For special characters use the IDNA encoding.
  23. ";
  24. function reply($chatid, $reply) {
  25. return file_get_contents(API_URL."sendmessage?chat_id=".$chatid."&text=".urlencode($reply)."&parse_mode=HTML");
  26. }
  27. $content = file_get_contents("php://input");
  28. //error_log($content);
  29. $update = json_decode($content, true);
  30. $chatid = $update["message"]["chat"]["id"];
  31. $fromid = $update["message"]["from"]["id"];
  32. $fromusername = $update["message"]["from"]["username"];
  33. $command = explode(" ", $update['message']['text'])[0];
  34. switch($command) {
  35. case '/start':
  36. $reply = $help;
  37. break;
  38. case '/list':
  39. $stmt = $db->prepare("SELECT id, type, value FROM rules where userid = ? ORDER BY timestamp ASC");
  40. $stmt->execute(array($fromid));
  41. $rules = $stmt->fetchAll(PDO::FETCH_ASSOC);
  42. if (!empty($rules)) {
  43. $reply = "<strong>ID\tType\tValue</strong>\n";
  44. foreach ($rules as $rule) {
  45. switch($rule['type']) {
  46. case 0:
  47. $type = "in";
  48. break;
  49. case 1:
  50. $type = "start";
  51. break;
  52. case 2:
  53. $type = "end";
  54. break;
  55. }
  56. $reply .= $rule['id']."\t<i>".$type."</i>\t".htmlentities($rule['value'])."\n";
  57. }
  58. } else {
  59. $reply = "There are no rules yet";
  60. }
  61. break;
  62. break;
  63. case '/add':
  64. $exp = explode(" ", $update['message']['text']);
  65. $type = $exp[1];
  66. $value = $exp[2];
  67. if (strlen($value) < 5) {
  68. $reply = "The filter must be at least 5 chars.";
  69. break;
  70. }
  71. switch($type) {
  72. case 'in':
  73. $type = 0;
  74. break;
  75. case 'start':
  76. $type = 1;
  77. break;
  78. case 'end':
  79. $type = 2;
  80. break;
  81. default:
  82. $type = -1;
  83. break;
  84. }
  85. if ($type > -1) {
  86. $stmt = $db->prepare("INSERT INTO rules (userid, chatid, type, value, timestamp) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP())");
  87. $stmt->execute(array($fromid, $chatid, $type, $value));
  88. $id = $db->lastInsertId();
  89. $toadd["id"] = $id;
  90. $toadd["value"] = array("t" => $type, "v" => $value, "c" => $chatid);
  91. $toadd = json_encode($toadd, JSON_NUMERIC_CHECK);
  92. $redis->rPush('toadd', $toadd);
  93. $reply = "Rule added, check with /list";
  94. } else {
  95. $reply = "Invalid rule type.";
  96. }
  97. break;
  98. case '/delete':
  99. $exp = explode(" ", $update['message']['text']);
  100. $id = $exp[1];
  101. $stmt = $db->prepare("DELETE FROM rules WHERE id = ? AND userid = ?");
  102. $stmt->execute(array($id, $fromid));
  103. $redis->rPush('todel', $id);
  104. $reply = "Rule ".$id." deleted";
  105. break;
  106. default:
  107. $reply = "Unknown command";
  108. break;
  109. }
  110. reply($chatid, $reply);
  111. ?>