|
|
@@ -11,92 +11,107 @@ class db extends mysqli {
|
|
|
if($this->connect_errno){
|
|
|
die($this->connect_error);
|
|
|
}
|
|
|
+ $this->set_charset('utf8');
|
|
|
}
|
|
|
}
|
|
|
-$ip = trim($_GET['ip']);
|
|
|
-$token = trim($_GET['token']);
|
|
|
-$action = trim($_GET['action']);
|
|
|
-$db = new db();
|
|
|
-$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;
|
|
|
+class checkToken {
|
|
|
+ use db;
|
|
|
+ private $db = null;
|
|
|
+
|
|
|
+ public function __construct() {
|
|
|
+ $this->db = new db();
|
|
|
}
|
|
|
- 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;
|
|
|
+ private function getRole($token) {
|
|
|
+ $dbAction = $this->db->prepare("SELECT role FROM clients WHERE token = ?");
|
|
|
+ $dbAction->bind_param('s',$token);
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ $dbAction->bind_result($role);
|
|
|
+ $dbAction->fetch();
|
|
|
+ if ($dbAction->num_rows() == 1){
|
|
|
+ return $role;
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
- return false;
|
|
|
-}
|
|
|
|
|
|
-function set($ip,$db) {
|
|
|
- $dbAction = $db->prepare("INSERT IGNORE INTO list VALUES (?)");
|
|
|
- $dbAction->bind_param('i',ip2long($ip));
|
|
|
- return $dbAction->execute();
|
|
|
-}
|
|
|
+ public function isAdmin($token) {
|
|
|
+ if($this->getRole($token) == 'admin') {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
-function islisted($ip,$db) {
|
|
|
- $dbAction = $db->prepare("SELECT ip FROM list WHERE ip = ?");
|
|
|
- $dbAction->bind_param('i',ip2long($ip));
|
|
|
- $dbAction->execute();
|
|
|
- $dbAction->store_result();
|
|
|
- if($dbAction->num_rows() == 0) {
|
|
|
+ public function isClient($token) {
|
|
|
+ if($this->getRole($token) == 'client') {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
-function delistCount($ip,$db) {
|
|
|
- $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();
|
|
|
- return $count;
|
|
|
-}
|
|
|
+class action {
|
|
|
+ use db;
|
|
|
|
|
|
-function delist($ip,$db) {
|
|
|
- if(!islisted($ip,$db)) {
|
|
|
- return false;
|
|
|
+ public function __construct() {
|
|
|
+ $this->db = new db();
|
|
|
}
|
|
|
- //check delisting count
|
|
|
- if (delistCount($ip,$db) > 3){
|
|
|
- echo "Fatal: ". $ip . " delisted to often!\n";
|
|
|
- return false;
|
|
|
+
|
|
|
+ private function isListed($ip) {
|
|
|
+ $dbAction = $this->db->prepare("SELECT ip FROM list WHERE ip = ?");
|
|
|
+ $dbAction->bind_param('i',ip2long($ip));
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ if($dbAction->num_rows() == 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function deListCount($ip) {
|
|
|
+ $dbAction = $this->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();
|
|
|
+ return $count;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function set($ip) {
|
|
|
+ $dbAction = $this->db->prepare("INSERT IGNORE INTO list VALUES (?)");
|
|
|
+ $dbAction->bind_param('i',ip2long($ip));
|
|
|
+ return $dbAction->execute();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function delist($ip) {
|
|
|
+ if(!islisted($ip)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //check delisting count
|
|
|
+ if (delistCount($ip) > 3){
|
|
|
+ echo "Fatal: ". $ip . " delisted to often!\n";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //delist ip
|
|
|
+ $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
|
|
|
+ $dbAction->bind_param('i',ip2long($ip));
|
|
|
+ $dbAction->execute();
|
|
|
+ //update delist count
|
|
|
+ $dbAction = $this->db->prepare("INSERT INTO delist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1");
|
|
|
+ $dbAction->bind_param('i',ip2long($ip));
|
|
|
+ $dbAction->execute();
|
|
|
+ return true;
|
|
|
}
|
|
|
- //delist ip
|
|
|
- $dbAction = $db->prepare("DELETE FROM list WHERE ip = ?");
|
|
|
- $dbAction->bind_param('i',ip2long($ip));
|
|
|
- $dbAction->execute();
|
|
|
- //update delist count
|
|
|
- $dbAction = $db->prepare("INSERT INTO delist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1");
|
|
|
- $dbAction->bind_param('i',ip2long($ip));
|
|
|
- $dbAction->execute();
|
|
|
- return true;
|
|
|
}
|
|
|
+$ip = trim($_GET['ip']);
|
|
|
+$token = trim($_GET['token']);
|
|
|
+$action = trim($_GET['action']);
|
|
|
+
|
|
|
switch($action) {
|
|
|
case 'delist':
|
|
|
- if(delist($ip,$db)){
|
|
|
+ if((new action)->delist($ip)){
|
|
|
echo "$ip delisted\n";
|
|
|
} else {
|
|
|
echo "$ip not delisted\n";
|
|
|
@@ -107,8 +122,8 @@ switch($action) {
|
|
|
case 'whitelist':
|
|
|
break;
|
|
|
default:
|
|
|
- if (checkToken($token,$db)){
|
|
|
- if(set($ip,$db)){
|
|
|
+ if ((new checkToken)->isClient($token)){
|
|
|
+ if((new action)->set($ip)){
|
|
|
echo " inserted " . $ip ."\n";
|
|
|
} else {
|
|
|
echo " fehler\n";
|