action.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. class action {
  3. public function __construct() {
  4. $this->db = new db();
  5. }
  6. public function getUser() {
  7. $list = array();
  8. $dbAction = $this->db->prepare("SELECT token, role, description FROM clients ORDER BY role ASC");
  9. $dbAction->execute();
  10. $dbAction->store_result();
  11. $dbAction->bind_result($token,$role,$description);
  12. while($dbAction->fetch()) {
  13. array_push($list,[$token,$role,$description]);
  14. };
  15. return $list;
  16. }
  17. public function addUser($userid,$role,$description) {
  18. $dbAction = $this->db->prepare("INSERT IGNORE INTO clients VALUES (?,?,?)");
  19. $dbAction->bind_param('sss',$userid,$role,$description);
  20. return $dbAction->execute();
  21. }
  22. public function delUser($userid) {
  23. $dbAction = $this->db->prepare("DELETE FROM clients WHERE token = ?");
  24. $dbAction->bind_param('s',$userid);
  25. return $dbAction->execute();
  26. }
  27. public function getWhiteList() {
  28. $list = array();
  29. $dbAction = $this->db->prepare("SELECT ip FROM whitelist ORDER BY ip ASC");
  30. $dbAction->execute();
  31. $dbAction->store_result();
  32. $dbAction->bind_result($ip);
  33. while($dbAction->fetch()) {
  34. array_push($list,$ip);
  35. };
  36. return $list;
  37. }
  38. public function getBlackList() {
  39. $list = array();
  40. $dbAction = $this->db->prepare("SELECT ip FROM blacklist ORDER BY ip ASC");
  41. $dbAction->execute();
  42. $dbAction->store_result();
  43. $dbAction->bind_result($ip);
  44. while($dbAction->fetch()) {
  45. array_push($list,$ip);
  46. };
  47. return $list;
  48. }
  49. public function getList() {
  50. $list = array();
  51. $dbAction = $this->db->prepare("SELECT ip FROM list UNION SELECT ip FROM blacklist ORDER BY ip ASC");
  52. $dbAction->execute();
  53. $dbAction->store_result();
  54. $dbAction->bind_result($ip);
  55. while($dbAction->fetch()) {
  56. array_push($list,$ip);
  57. };
  58. return $list;
  59. }
  60. private function isListed($ip) {
  61. $dbAction = $this->db->prepare("SELECT ip FROM list WHERE ip = ?");
  62. $dbAction->bind_param('i',$ip);
  63. $dbAction->execute();
  64. $dbAction->store_result();
  65. if($dbAction->num_rows() == 0) {
  66. return false;
  67. }
  68. return true;
  69. }
  70. private function isWhiteListed($ip) {
  71. $dbAction = $this->db->prepare("SELECT ip FROM whitelist WHERE ip = ?");
  72. $dbAction->bind_param('i',$ip);
  73. $dbAction->execute();
  74. $dbAction->store_result();
  75. if($dbAction->num_rows() == 0) {
  76. return false;
  77. }
  78. return true;
  79. }
  80. private function deListCount($ip) {
  81. $dbAction = $this->db->prepare("SELECT count FROM delist WHERE ip = ?");
  82. $dbAction->bind_param('i',$ip);
  83. $dbAction->execute();
  84. $dbAction->store_result();
  85. $dbAction->bind_result($count);
  86. $dbAction->fetch();
  87. return $count;
  88. }
  89. public function list($ip) {
  90. if($this->isWhiteListed($ip)){
  91. return false;
  92. }
  93. $dbAction = $this->db->prepare("INSERT IGNORE INTO list VALUES (?)");
  94. $dbAction->bind_param('i',$ip);
  95. $dbAction->execute();
  96. return true;
  97. }
  98. public function adminDeList($ip) {
  99. //delist ip
  100. $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
  101. $dbAction->bind_param('i',$ip);
  102. $dbAction->execute();
  103. //reset delist count
  104. $dbAction = $this->db->prepare("DELETE FROM delist WHERE ip = ?");
  105. $dbAction->bind_param('i',$ip);
  106. $dbAction->execute();
  107. //remove from blacklist
  108. $dbAction = $this->db->prepare("DELETE FROM blacklist WHERE ip = ?");
  109. $dbAction->bind_param('i',$ip);
  110. $dbAction->execute();
  111. return true;
  112. }
  113. public function deList($ip) {
  114. if(!$this->isListed($ip)) { //set true to avoid discovering
  115. return true;
  116. }
  117. //check delisting count
  118. if ($this->delistCount($ip) > 3){
  119. echo "Fatal: ". long2ip($ip) . " delisted to often!\n";
  120. return false;
  121. }
  122. //delist ip
  123. $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
  124. $dbAction->bind_param('i',$ip);
  125. $dbAction->execute();
  126. //update delist count
  127. $dbAction = $this->db->prepare("INSERT INTO delist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1");
  128. $dbAction->bind_param('i',$ip);
  129. $dbAction->execute();
  130. return true;
  131. }
  132. public function blackList($ip) {
  133. $this->deWhiteList($ip);
  134. $dbAction = $this->db->prepare("INSERT IGNORE INTO blacklist VALUES (?)");
  135. $dbAction->bind_param('i',$ip);
  136. return $dbAction->execute();
  137. }
  138. public function deBlackList($ip) {
  139. $dbAction = $this->db->prepare("DELETE FROM blacklist WHERE ip = ?");
  140. $dbAction->bind_param('i',$ip);
  141. return $dbAction->execute();
  142. }
  143. public function whiteList($ip) {
  144. $this->adminDeList($ip);
  145. $dbAction = $this->db->prepare("INSERT IGNORE INTO whitelist VALUES (?)");
  146. $dbAction->bind_param('i',$ip);
  147. return $dbAction->execute();
  148. }
  149. public function deWhiteList($ip) {
  150. $dbAction = $this->db->prepare("DELETE FROM whitelist WHERE ip = ?");
  151. $dbAction->bind_param('i',$ip);
  152. return $dbAction->execute();
  153. }
  154. }