action.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. class action {
  3. public function __construct() {
  4. $this->db = new db();
  5. }
  6. public function getList() {
  7. $list = array();
  8. $dbAction = $this->db->prepare("SELECT ip FROM list UNION SELECT ip FROM blacklist ORDER BY ip ASC");
  9. $dbAction->execute();
  10. $dbAction->store_result();
  11. $dbAction->bind_result($ip);
  12. while($dbAction->fetch()) {
  13. array_push($list,long2ip($ip));
  14. };
  15. return $list;
  16. }
  17. private function isListed($ip) {
  18. $dbAction = $this->db->prepare("SELECT ip FROM list WHERE ip = ?");
  19. $dbAction->bind_param('i',$ip);
  20. $dbAction->execute();
  21. $dbAction->store_result();
  22. if($dbAction->num_rows() == 0) {
  23. return false;
  24. }
  25. return true;
  26. }
  27. private function deListCount($ip) {
  28. $dbAction = $this->db->prepare("SELECT count FROM delist WHERE ip = ?");
  29. $dbAction->bind_param('i',$ip);
  30. $dbAction->execute();
  31. $dbAction->store_result();
  32. $dbAction->bind_result($count);
  33. $dbAction->fetch();
  34. return $count;
  35. }
  36. public function list($ip) {
  37. $dbAction = $this->db->prepare("INSERT IGNORE INTO list VALUES (?)");
  38. $dbAction->bind_param('i',$ip);
  39. return $dbAction->execute();
  40. }
  41. public function deList($ip) {
  42. if(!$this->isListed($ip)) { //set true to avoid discovering
  43. return true;
  44. }
  45. //check delisting count
  46. if ($this->delistCount($ip) > 3){
  47. echo "Fatal: ". long2ip($ip) . " delisted to often!\n";
  48. return false;
  49. }
  50. //delist ip
  51. $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
  52. $dbAction->bind_param('i',$ip);
  53. $dbAction->execute();
  54. //update delist count
  55. $dbAction = $this->db->prepare("INSERT INTO delist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1");
  56. $dbAction->bind_param('i',$ip);
  57. $dbAction->execute();
  58. return true;
  59. }
  60. public function blackList($ip) {
  61. $this->deWhiteList($ip);
  62. $dbAction = $this->db->prepare("INSERT IGNORE INTO blacklist VALUES (?)");
  63. $dbAction->bind_param('i',$ip);
  64. return $dbAction->execute();
  65. }
  66. public function deBlackList($ip) {
  67. $dbAction = $this->db->prepare("DELETE FROM blacklist WHERE ip = (?)");
  68. $dbAction->bind_param('i',$ip);
  69. return $dbAction->execute();
  70. }
  71. public function whiteList($ip) {
  72. $this->deBlackList($ip);
  73. $dbAction = $this->db->prepare("INSERT IGNORE INTO whitelist VALUES (?)");
  74. $dbAction->bind_param('i',$ip);
  75. return $dbAction->execute();
  76. }
  77. public function deWhiteList($ip) {
  78. $dbAction = $this->db->prepare("DELETE FROM whitelist WHERE ip = (?)");
  79. $dbAction->bind_param('i',$ip);
  80. return $dbAction->execute();
  81. }
  82. }