index.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. class db {
  3. private $host = 'localhost'; //Host
  4. private $user = 'ban'; //Username
  5. private $pw = 'Blubb123'; //DBpasswort
  6. private $dbname = 'ban'; //Datenbankname
  7. static public $db_obj = null;
  8. private function __construct(){
  9. try
  10. {
  11. self::$db_obj = new mysqli($this->host, $this->user, $this->pw, $this->dbname);
  12. }
  13. catch(Exception $e)
  14. {
  15. printf('Fehler beim &Ouml;ffnen der Datenbank.<br><br>%s',
  16. $e->getMessage);
  17. exit();
  18. }
  19. }
  20. public static function getInstance(){
  21. if(self::$db_obj === null)
  22. self::$objekt = new db_sql;
  23. return self::$db_obj;
  24. }
  25. private final function __clone(){ }
  26. }
  27. error_reporting(E_ALL);
  28. $ip = trim($_GET['ip']);
  29. $token = trim($_GET['token']);
  30. $action = trim($_GET['action']);
  31. $db = new db;
  32. if ($db->connect_errno){
  33. die($db->connect_error);
  34. }
  35. $db->set_charset('utf8');
  36. function checkToken($token,$db) {
  37. $dbAction = $db->prepare("SELECT description FROM clients WHERE token = ?");
  38. $dbAction->bind_param('s',$token);
  39. $dbAction->execute();
  40. $dbAction->store_result();
  41. $dbAction->bind_result($desc);
  42. $dbAction->fetch();
  43. if ($dbAction->num_rows() == 1){
  44. echo "Client " . $desc;
  45. return true;
  46. }
  47. return false;
  48. }
  49. function checkAdmin($token,$db) {
  50. $dbAction = $db->prepare("SELECT description FROM clients WHERE token = ?");
  51. $dbAction->bind_param('s',$token);
  52. $dbAction->execute();
  53. $dbAction->store_result();
  54. $dbAction->bind_result($desc);
  55. $dbAction->fetch();
  56. if ($dbAction->num_rows() == 1){
  57. if($desc == 'admin') {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. function set($ip,$db) {
  64. $dbAction = $db->prepare("INSERT IGNORE INTO list VALUES (?)");
  65. $dbAction->bind_param('i',ip2long($ip));
  66. return $dbAction->execute();
  67. }
  68. function islisted($ip,$db) {
  69. $dbAction = $db->prepare("SELECT ip FROM list WHERE ip = ?");
  70. $dbAction->bind_param('i',ip2long($ip));
  71. $dbAction->execute();
  72. $dbAction->store_result();
  73. if($dbAction->num_rows() == 0) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. function delistCount($ip,$db) {
  79. $dbAction = $db->prepare("SELECT count FROM delist WHERE ip = ?");
  80. $dbAction->bind_param('i',ip2long($ip));
  81. $dbAction->execute();
  82. $dbAction->store_result();
  83. $dbAction->bind_result($count);
  84. $dbAction->fetch();
  85. return $count;
  86. }
  87. function delist($ip,$db) {
  88. if(!islisted($ip,$db)) {
  89. return false;
  90. }
  91. //check delisting count
  92. if (delistCount($ip,$db) > 3){
  93. echo "Fatal: ". $ip . " delisted to often!\n";
  94. return false;
  95. }
  96. //delist ip
  97. $dbAction = $db->prepare("DELETE FROM list WHERE ip = ?");
  98. $dbAction->bind_param('i',ip2long($ip));
  99. $dbAction->execute();
  100. //update delist count
  101. $dbAction = $db->prepare("INSERT INTO delist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1");
  102. $dbAction->bind_param('i',ip2long($ip));
  103. $dbAction->execute();
  104. return true;
  105. }
  106. switch($action) {
  107. case 'delist':
  108. if(delist($ip,$db)){
  109. echo "$ip delisted\n";
  110. } else {
  111. echo "$ip not delisted\n";
  112. };
  113. break;
  114. case 'blacklist':
  115. break;
  116. default:
  117. if (checkToken($token,$db)){
  118. if(set($ip,$db)){
  119. echo " inserted " . $ip ."\n";
  120. } else {
  121. echo " fehler\n";
  122. };
  123. } else {
  124. echo "Client token " . $token . " not registered\n";
  125. };
  126. }