action.php 3.6 KB

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