action.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. print_r($list);
  38. return $list;*/
  39. return $dbAction->fetch_all();
  40. }
  41. private function isListed($ip) {
  42. $dbAction = $this->db->prepare("SELECT ip FROM list WHERE ip = ?");
  43. $dbAction->bind_param('i',$ip);
  44. $dbAction->execute();
  45. $dbAction->store_result();
  46. if($dbAction->num_rows() == 0) {
  47. return false;
  48. }
  49. return true;
  50. }
  51. private function deListCount($ip) {
  52. $dbAction = $this->db->prepare("SELECT count FROM delist WHERE ip = ?");
  53. $dbAction->bind_param('i',$ip);
  54. $dbAction->execute();
  55. $dbAction->store_result();
  56. $dbAction->bind_result($count);
  57. $dbAction->fetch();
  58. return $count;
  59. }
  60. public function list($ip) {
  61. $dbAction = $this->db->prepare("INSERT IGNORE INTO list VALUES (?)");
  62. $dbAction->bind_param('i',$ip);
  63. return $dbAction->execute();
  64. }
  65. public function deList($ip) {
  66. if(!$this->isListed($ip)) { //set true to avoid discovering
  67. return true;
  68. }
  69. //check delisting count
  70. if ($this->delistCount($ip) > 3){
  71. echo "Fatal: ". long2ip($ip) . " delisted to often!\n";
  72. return false;
  73. }
  74. //delist ip
  75. $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
  76. $dbAction->bind_param('i',$ip);
  77. $dbAction->execute();
  78. //update delist count
  79. $dbAction = $this->db->prepare("INSERT INTO delist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1");
  80. $dbAction->bind_param('i',$ip);
  81. $dbAction->execute();
  82. return true;
  83. }
  84. public function blackList($ip) {
  85. $this->deWhiteList($ip);
  86. $dbAction = $this->db->prepare("INSERT IGNORE INTO blacklist VALUES (?)");
  87. $dbAction->bind_param('i',$ip);
  88. return $dbAction->execute();
  89. }
  90. public function deBlackList($ip) {
  91. $dbAction = $this->db->prepare("DELETE FROM blacklist WHERE ip = (?)");
  92. $dbAction->bind_param('i',$ip);
  93. return $dbAction->execute();
  94. }
  95. public function whiteList($ip) {
  96. $this->deBlackList($ip);
  97. $dbAction = $this->db->prepare("INSERT IGNORE INTO whitelist VALUES (?)");
  98. $dbAction->bind_param('i',$ip);
  99. return $dbAction->execute();
  100. }
  101. public function deWhiteList($ip) {
  102. $dbAction = $this->db->prepare("DELETE FROM whitelist WHERE ip = (?)");
  103. $dbAction->bind_param('i',$ip);
  104. return $dbAction->execute();
  105. }
  106. }