action.php 7.8 KB

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