action.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. class action {
  3. public function __construct() {
  4. $this->db = new db();
  5. }
  6. public function block($ip) {
  7. $dbAction = $this->db->prepare("SELECT count FROM blocklist WHERE ip = ?");
  8. $dbAction->bind_param('i',$ip);
  9. $dbAction->execute();
  10. $dbAction->store_result();
  11. $dbAction->bind_result($count);
  12. $dbAction->fetch();
  13. if($count > 3) {
  14. $this->list($ip);
  15. return true;
  16. };
  17. $dbAction = $this->db->prepare("INSERT INTO blocklist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1");
  18. $dbAction->bind_param('i',$ip);
  19. $dbAction->execute();
  20. return true;
  21. }
  22. public function adminBlock($ip) {
  23. $dbAction = $this->db->prepare("INSERT IGNORE INTO blocklist (ip, count) VALUES (?, 4)");
  24. $dbAction->bind_param('i',$ip);
  25. $dbAction->execute();
  26. return true;
  27. }
  28. public function deBlockList($ip) {
  29. $dbAction = $this->db->prepare("DELETE FROM blocklist WHERE ip = ?");
  30. $dbAction->bind_param('i',$ip);
  31. return $dbAction->execute();
  32. $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
  33. $dbAction->bind_param('i',$ip);
  34. return $dbAction->execute();
  35. }
  36. public function getBlockedClients() {
  37. $list = array();
  38. $dbAction = $this->db->prepare("SELECT ip FROM blocklist WHERE count > 3 ORDER BY ip ASC");
  39. $dbAction->execute();
  40. $dbAction->store_result();
  41. $dbAction->bind_result($ip);
  42. while($dbAction->fetch()) {
  43. array_push($list,$ip);
  44. };
  45. return $list;
  46. }
  47. public function getUser() {
  48. $list = array();
  49. $dbAction = $this->db->prepare("SELECT token, role, description FROM clients ORDER BY role ASC");
  50. $dbAction->execute();
  51. $dbAction->store_result();
  52. $dbAction->bind_result($token,$role,$description);
  53. while($dbAction->fetch()) {
  54. array_push($list,[$token,$role,$description]);
  55. };
  56. return $list;
  57. }
  58. public function addUser($userid,$role,$description) {
  59. $dbAction = $this->db->prepare("INSERT IGNORE INTO clients VALUES (?,?,?)");
  60. $dbAction->bind_param('sss',$userid,$role,$description);
  61. return $dbAction->execute();
  62. }
  63. public function delUser($userid) {
  64. $dbAction = $this->db->prepare("DELETE FROM clients WHERE token = ?");
  65. $dbAction->bind_param('s',$userid);
  66. return $dbAction->execute();
  67. }
  68. public function getWhiteList() {
  69. $list = array();
  70. $dbAction = $this->db->prepare("SELECT ip FROM whitelist ORDER BY ip ASC");
  71. $dbAction->execute();
  72. $dbAction->store_result();
  73. $dbAction->bind_result($ip);
  74. while($dbAction->fetch()) {
  75. array_push($list,$ip);
  76. };
  77. return $list;
  78. }
  79. public function getBlackList() {
  80. $list = array();
  81. $dbAction = $this->db->prepare("SELECT ip FROM blacklist ORDER BY ip ASC");
  82. $dbAction->execute();
  83. $dbAction->store_result();
  84. $dbAction->bind_result($ip);
  85. while($dbAction->fetch()) {
  86. array_push($list,$ip);
  87. };
  88. return $list;
  89. }
  90. public function getList() {
  91. $list = array();
  92. $dbAction = $this->db->prepare("SELECT ip FROM list ORDER BY ip ASC");
  93. $dbAction->execute();
  94. $dbAction->store_result();
  95. $dbAction->bind_result($ip);
  96. while($dbAction->fetch()) {
  97. array_push($list,$ip);
  98. };
  99. return $list;
  100. }
  101. public function getBlockList() {
  102. $list = array();
  103. $dbAction = $this->db->prepare("SELECT ip FROM list UNION SELECT ip FROM blacklist ORDER BY ip ASC");
  104. $dbAction->execute();
  105. $dbAction->store_result();
  106. $dbAction->bind_result($ip);
  107. while($dbAction->fetch()) {
  108. array_push($list,$ip);
  109. };
  110. return $list;
  111. }
  112. private function isListed($ip) {
  113. $dbAction = $this->db->prepare("SELECT ip FROM list WHERE ip = ?");
  114. $dbAction->bind_param('i',$ip);
  115. $dbAction->execute();
  116. $dbAction->store_result();
  117. if($dbAction->num_rows() == 0) {
  118. return false;
  119. }
  120. return true;
  121. }
  122. private function isBlackListed($ip) {
  123. $dbAction = $this->db->prepare("SELECT ip FROM blacklist WHERE ip = ?");
  124. $dbAction->bind_param('i',$ip);
  125. $dbAction->execute();
  126. $dbAction->store_result();
  127. if($dbAction->num_rows() == 0) {
  128. return false;
  129. }
  130. return true;
  131. }
  132. public function isBlocked($ip) {
  133. if($this->isListed($ip)||$this->isBlackListed($ip)) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. private function isWhiteListed($ip) {
  139. $dbAction = $this->db->prepare("SELECT ip FROM whitelist WHERE ip = ?");
  140. $dbAction->bind_param('i',$ip);
  141. $dbAction->execute();
  142. $dbAction->store_result();
  143. if($dbAction->num_rows() == 0) {
  144. return false;
  145. }
  146. return true;
  147. }
  148. private function deListCount($ip) {
  149. $dbAction = $this->db->prepare("SELECT count FROM delist WHERE ip = ?");
  150. $dbAction->bind_param('i',$ip);
  151. $dbAction->execute();
  152. $dbAction->store_result();
  153. $dbAction->bind_result($count);
  154. $dbAction->fetch();
  155. return $count;
  156. }
  157. public function list($ip) {
  158. if($this->isWhiteListed($ip)){
  159. return false;
  160. }
  161. $dbAction = $this->db->prepare("INSERT IGNORE INTO list VALUES (?)");
  162. $dbAction->bind_param('i',$ip);
  163. $dbAction->execute();
  164. return true;
  165. }
  166. public function adminDeList($ip) {
  167. //delist ip
  168. $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
  169. $dbAction->bind_param('i',$ip);
  170. $dbAction->execute();
  171. //reset delist count
  172. $dbAction = $this->db->prepare("DELETE FROM delist WHERE ip = ?");
  173. $dbAction->bind_param('i',$ip);
  174. $dbAction->execute();
  175. //remove from blacklist
  176. $dbAction = $this->db->prepare("DELETE FROM blacklist WHERE ip = ?");
  177. $dbAction->bind_param('i',$ip);
  178. $dbAction->execute();
  179. return true;
  180. }
  181. public function deList($ip) {
  182. if(!$this->isListed($ip)) { //set true to avoid discovering
  183. return true;
  184. }
  185. //check delisting count
  186. if ($this->delistCount($ip) > 3){
  187. echo "Fatal: ". long2ip($ip) . " delisted to often!\n";
  188. return false;
  189. }
  190. //delist ip
  191. $dbAction = $this->db->prepare("DELETE FROM list WHERE ip = ?");
  192. $dbAction->bind_param('i',$ip);
  193. $dbAction->execute();
  194. //update delist count
  195. $dbAction = $this->db->prepare("INSERT INTO delist (ip) VALUES (?) ON DUPLICATE KEY UPDATE count = count + 1");
  196. $dbAction->bind_param('i',$ip);
  197. $dbAction->execute();
  198. return true;
  199. }
  200. public function blackList($ip) {
  201. $this->deWhiteList($ip);
  202. $this->deList($ip);
  203. $dbAction = $this->db->prepare("INSERT IGNORE INTO blacklist VALUES (?)");
  204. $dbAction->bind_param('i',$ip);
  205. if($dbAction->execute()) {
  206. $this->deWhiteList($ip);
  207. $this->deList($ip);
  208. return true;
  209. };
  210. return false;
  211. }
  212. public function deBlackList($ip) {
  213. $dbAction = $this->db->prepare("DELETE FROM blacklist WHERE ip = ?");
  214. $dbAction->bind_param('i',$ip);
  215. return $dbAction->execute();
  216. }
  217. public function whiteList($ip) {
  218. $this->adminDeList($ip);
  219. $dbAction = $this->db->prepare("INSERT IGNORE INTO whitelist VALUES (?)");
  220. $dbAction->bind_param('i',$ip);
  221. return $dbAction->execute();
  222. }
  223. public function deWhiteList($ip) {
  224. $dbAction = $this->db->prepare("DELETE FROM whitelist WHERE ip = ?");
  225. $dbAction->bind_param('i',$ip);
  226. return $dbAction->execute();
  227. }
  228. }