Browse Source

split classes to files

andre 4 years ago
parent
commit
3603047484
3 changed files with 130 additions and 0 deletions
  1. 80 0
      action.php
  2. 15 0
      db.php
  3. 35 0
      token.php

+ 80 - 0
action.php

@@ -0,0 +1,80 @@
+<?php
+class action {
+
+    public function __construct() {
+        $this->db = new db();
+    }
+
+    private function isListed($ip) {
+        $dbAction = $this->db->prepare("SELECT ip FROM list WHERE ip = ?");
+        $dbAction->bind_param('i',$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',$ip);
+        $dbAction->execute();
+        $dbAction->store_result();
+        $dbAction->bind_result($count);
+        $dbAction->fetch();
+        return $count;
+    }
+
+    public function list($ip) {
+        $dbAction = $this->db->prepare("INSERT IGNORE INTO list VALUES (?)");
+        $dbAction->bind_param('i',$ip);
+        return $dbAction->execute();
+    }
+
+    public function deList($ip) {
+        if(!$this->isListed($ip)) {         //set true to avoid discovering
+            return true;
+        }
+        //check delisting count
+        if ($this->delistCount($ip) > 3){
+            echo "Fatal: ". long2ip($ip) . " delisted to often!\n";
+            return false;
+        }
+        //delist ip
+        $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
+        $dbAction->bind_param('i',$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',$ip);
+        $dbAction->execute();
+        return true;
+    }
+
+    public function blackList($ip) {
+        $this->deWhiteList($ip);
+        $dbAction = $this->db->prepare("INSERT IGNORE INTO blacklist VALUES (?)");
+        $dbAction->bind_param('i',$ip);
+        return $dbAction->execute();
+    }
+
+    public function deBlackList($ip) {
+        $dbAction = $this->db->prepare("DELETE FROM blacklist WHERE ip = (?)");
+        $dbAction->bind_param('i',$ip);
+        return $dbAction->execute();
+    }
+
+    public function whiteList($ip) {
+        $this->deBlackList($ip);
+        $dbAction = $this->db->prepare("INSERT IGNORE INTO whitelist VALUES (?)");
+        $dbAction->bind_param('i',$ip);
+        return $dbAction->execute();
+    }
+
+    public function deWhiteList($ip) {
+        $dbAction = $this->db->prepare("DELETE FROM whitelist WHERE ip = (?)");
+        $dbAction->bind_param('i',$ip);
+        return $dbAction->execute();
+    }
+}

+ 15 - 0
db.php

@@ -0,0 +1,15 @@
+<?php
+class db extends mysqli {
+    private $host = 'localhost';
+    private $user = 'ban';
+    private $pass = 'Blubb123-';
+    private $db   = 'ban';
+
+    public function __construct() {
+        @parent::__construct($this->host, $this->user, $this->pass, $this->db);
+        if($this->connect_errno){
+            die($this->connect_error);
+        }
+        $this->set_charset('utf8');
+    }
+}

+ 35 - 0
token.php

@@ -0,0 +1,35 @@
+<?php
+class token {
+    private $db = null;
+
+    public function __construct() {
+        $this->db = new db();
+    }
+
+    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;
+    }
+
+    public function isAdmin($token) {
+        if($this->getRole($token) == 'admin') {
+            return true;
+        }
+        return false;
+    }
+
+    public function isClient($token) {
+        if($this->getRole($token) == 'client') {
+            return true;
+        }
+        return false;
+    }
+}