connect_errno){ die($db->connect_error); } $db->set_charset('utf8'); function checkToken($token,$db) { $dbAction = $db->prepare("SELECT description FROM clients WHERE token = ?"); $dbAction->bind_param('s',$token); $dbAction->execute(); $dbAction->store_result(); $dbAction->bind_result($desc); $dbAction->fetch(); if ($dbAction->num_rows() == 1){ echo "Client $desc"; return true; } return false; } function checkAdmin($token,$db) { $dbAction = $db->prepare("SELECT description FROM clients WHERE token = ?"); $dbAction->bind_param('s',$token); $dbAction->execute(); $dbAction->store_result(); $dbAction->bind_result($desc); $dbAction->fetch(); if ($dbAction->num_rows() == 1){ if($desc == 'admin') { return true; } } return false; } function set($ip,$db) { $dbAction = $db->prepare("INSERT IGNORE INTO list VALUES (?)"); $dbAction->bind_param('i',ip2long($ip)); return $dbAction->execute(); } function islisted($ip,$db) { $dbAction = $db->prepare("SELECT ip FROM list WHERE ip = ?"); $dbAction->bind_param('i',ip2long($ip)); $dbAction->execute(); if($dbAction->num_rows() == 0) { return false; } return true; } function delist($ip,$db) { if(!islisted($ip,$db)) { echo $ip . " not listed"; return false; } $dbAction = $db->prepare("INSERT INTO delist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1"); $dbAction->bind_param('i',ip2long($ip)); $dbAction->execute(); $dbAction = $db->prepare("SELECT count FROM delist WHERE ip = ?"); $dbAction->bind_param('i',ip2long($ip)); $dbAction->execute(); $dbAction->store_result(); $dbAction->bind_result($count); $dbAction->fetch(); if ($count > 3){ echo "Fatal: ". $ip . " delisted to often!" . PHP_EOL; return false; } $dbAction = $db->prepare("DELETE FROM list WHERE ip = ?"); $dbAction->bind_param('i',ip2long($ip)); $dbAction->execute(); return true; } switch($action) { case 'delist': if(delist($ip,$db)){ echo "$ip delisted" . PHP_EOL; } else { echo "$ip not delisted" . PHP_EOL; }; break; case 'blacklist': break; default: if (checkToken($token,$db)){ if(set($ip,$db)){ echo " inserted $ip" . PHP_EOL; } else { echo " fehler" . PHP_EOL; }; } else { echo "Client token $token not registered" . PHP_EOL; }; }