|
|
@@ -0,0 +1,253 @@
|
|
|
+<?php
|
|
|
+class action {
|
|
|
+
|
|
|
+ public function __construct() {
|
|
|
+ $this->db = new db();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function block($ip) {
|
|
|
+ $dbAction = $this->db->prepare("SELECT count FROM blocklist WHERE ip = ?");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ $dbAction->bind_result($count);
|
|
|
+ $dbAction->fetch();
|
|
|
+ if($count > 3) {
|
|
|
+ $this->list($ip);
|
|
|
+ return true;
|
|
|
+ };
|
|
|
+ $dbAction = $this->db->prepare("INSERT INTO blocklist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function adminBlock($ip) {
|
|
|
+ $dbAction = $this->db->prepare("INSERT IGNORE INTO blocklist (ip, count) VALUES (?, 4)");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function deBlockList($ip) {
|
|
|
+ $dbAction = $this->db->prepare("DELETE FROM blocklist WHERE ip = ?");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getBlockedClients() {
|
|
|
+ $list = array();
|
|
|
+ $dbAction = $this->db->prepare("SELECT ip FROM blocklist WHERE count > 3 ORDER BY ip ASC");
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ $dbAction->bind_result($ip);
|
|
|
+ while($dbAction->fetch()) {
|
|
|
+ array_push($list,$ip);
|
|
|
+ };
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getUser() {
|
|
|
+ $list = array();
|
|
|
+ $dbAction = $this->db->prepare("SELECT token, role, description FROM clients ORDER BY role ASC");
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ $dbAction->bind_result($token,$role,$description);
|
|
|
+ while($dbAction->fetch()) {
|
|
|
+ array_push($list,[$token,$role,$description]);
|
|
|
+ };
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function addUser($userid,$role,$description) {
|
|
|
+ $dbAction = $this->db->prepare("INSERT IGNORE INTO clients VALUES (?,?,?)");
|
|
|
+ $dbAction->bind_param('sss',$userid,$role,$description);
|
|
|
+ return $dbAction->execute();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function delUser($userid) {
|
|
|
+ $dbAction = $this->db->prepare("DELETE FROM clients WHERE token = ?");
|
|
|
+ $dbAction->bind_param('s',$userid);
|
|
|
+ return $dbAction->execute();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getWhiteList() {
|
|
|
+ $list = array();
|
|
|
+ $dbAction = $this->db->prepare("SELECT ip FROM whitelist ORDER BY ip ASC");
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ $dbAction->bind_result($ip);
|
|
|
+ while($dbAction->fetch()) {
|
|
|
+ array_push($list,$ip);
|
|
|
+ };
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getBlackList() {
|
|
|
+ $list = array();
|
|
|
+ $dbAction = $this->db->prepare("SELECT ip FROM blacklist ORDER BY ip ASC");
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ $dbAction->bind_result($ip);
|
|
|
+ while($dbAction->fetch()) {
|
|
|
+ array_push($list,$ip);
|
|
|
+ };
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getList() {
|
|
|
+ $list = array();
|
|
|
+ $dbAction = $this->db->prepare("SELECT ip FROM list ORDER BY ip ASC");
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ $dbAction->bind_result($ip);
|
|
|
+ while($dbAction->fetch()) {
|
|
|
+ array_push($list,$ip);
|
|
|
+ };
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getBlockList() {
|
|
|
+ $list = array();
|
|
|
+ $dbAction = $this->db->prepare("SELECT ip FROM list UNION SELECT ip FROM blacklist ORDER BY ip ASC");
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ $dbAction->bind_result($ip);
|
|
|
+ while($dbAction->fetch()) {
|
|
|
+ array_push($list,$ip);
|
|
|
+ };
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 isBlackListed($ip) {
|
|
|
+ $dbAction = $this->db->prepare("SELECT ip FROM blacklist WHERE ip = ?");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ $dbAction->store_result();
|
|
|
+ if($dbAction->num_rows() == 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function isBlocked($ip) {
|
|
|
+ if($this->isListed($ip)||$this->isBlackListed($ip)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function isWhiteListed($ip) {
|
|
|
+ $dbAction = $this->db->prepare("SELECT ip FROM whitelist 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) {
|
|
|
+ if($this->isWhiteListed($ip)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $dbAction = $this->db->prepare("INSERT IGNORE INTO list VALUES (?)");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function adminDeList($ip) {
|
|
|
+ //delist ip
|
|
|
+ $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ //reset delist count
|
|
|
+ $dbAction = $this->db->prepare("DELETE FROM delist WHERE ip = ?");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ //remove from blacklist
|
|
|
+ $dbAction = $this->db->prepare("DELETE FROM blacklist WHERE ip = ?");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ $dbAction->execute();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ $this->deList($ip);
|
|
|
+ $dbAction = $this->db->prepare("INSERT IGNORE INTO blacklist VALUES (?)");
|
|
|
+ $dbAction->bind_param('i',$ip);
|
|
|
+ if($dbAction->execute()) {
|
|
|
+ $this->deWhiteList($ip);
|
|
|
+ $this->deList($ip);
|
|
|
+ return true;
|
|
|
+ };
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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->adminDeList($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();
|
|
|
+ }
|
|
|
+}
|