| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; |
| 3 |
|
| 4 |
if (!class_exists('BVIPStoreCallback')) : |
| 5 |
require_once dirname( __FILE__ ) . '/../../protect/ipstore.php'; |
| 6 |
|
| 7 |
class BVIPStoreCallback extends BVCallbackBase { |
| 8 |
public $db; |
| 9 |
|
| 10 |
const IPSTORE_WING_VERSION = 1.3; |
| 11 |
|
| 12 |
public function __construct($callback_handler) { |
| 13 |
$this->db = $callback_handler->db; |
| 14 |
} |
| 15 |
|
| 16 |
public function updateBVTableContent($table, $value, $filter) { |
| 17 |
$this->db->query("UPDATE $table SET $value $filter;"); |
| 18 |
} |
| 19 |
|
| 20 |
public function insertBVTableContent($table, $fields, $value) { |
| 21 |
$this->db->query("INSERT INTO $table $fields values $value;"); |
| 22 |
} |
| 23 |
|
| 24 |
public function deleteIPs($table, $rmfilters) { |
| 25 |
if (is_array($rmfilters)) { |
| 26 |
foreach ($rmfilters as $rmfilter) { |
| 27 |
$rmfilter = base64_decode($rmfilter); |
| 28 |
$this->db->deleteBVTableContent($table, $rmfilter); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
public function insertIPs($table, $fields, $values) { |
| 34 |
if (is_array($values)) { |
| 35 |
foreach ($values as $value) { |
| 36 |
$value = base64_decode($value); |
| 37 |
$this->insertBVTableContent($table, $fields, $value); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public function updateIPs($table, $value, $filters) { |
| 43 |
if (is_array($filters)) { |
| 44 |
foreach ($filters as $filter) { |
| 45 |
$filter = base64_decode($filter); |
| 46 |
$this->updateBVTableContent($table, $value, $filter); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
public function getIPs($table, $auto_increment_offset, $type, $category) { |
| 52 |
$query = "SELECT `start_ip_range` FROM $table WHERE id < $auto_increment_offset AND `type` in (" . implode(',', $type) . ") AND "; |
| 53 |
$query .= ($category == MCProtectIpstoreDB::CATEGORY_FW) ? "`is_fw` = true;" : "`is_lp` = true;"; |
| 54 |
return $this->db->getCol($query); |
| 55 |
} |
| 56 |
|
| 57 |
public function getIPStoreOffset($table, $auto_increment_offset) { |
| 58 |
$db = $this->db; |
| 59 |
return intval($db->getVar("SELECT MAX(id) FROM $table WHERE id < $auto_increment_offset")); |
| 60 |
} |
| 61 |
|
| 62 |
public function getIPStoreInfo($table, $auto_increment_offset) { |
| 63 |
$db = $this->db; |
| 64 |
$info = array(); |
| 65 |
|
| 66 |
$info['fw_blacklisted_ips'] = $this->getIPs($table, $auto_increment_offset, |
| 67 |
MCProtectIpstoreDB::blacklistedTypes(), MCProtectIpstoreDB::CATEGORY_FW); |
| 68 |
$info['lp_blacklisted_ips'] = $this->getIPs($table, $auto_increment_offset, |
| 69 |
MCProtectIpstoreDB::blacklistedTypes(), MCProtectIpstoreDB::CATEGORY_LP); |
| 70 |
$info['fw_whitelisted_ips'] = $this->getIPs($table, $auto_increment_offset, |
| 71 |
MCProtectIpstoreDB::whitelistedTypes(), MCProtectIpstoreDB::CATEGORY_FW); |
| 72 |
$info['lp_whitelisted_ips'] = $this->getIPs($table, $auto_increment_offset, |
| 73 |
MCProtectIpstoreDB::whitelistedTypes(), MCProtectIpstoreDB::CATEGORY_LP); |
| 74 |
$info['ip_store_offset'] = $this->getIPStoreOffset($table, $auto_increment_offset); |
| 75 |
$info['country_ips_size'] = intval($db->getVar("SELECT COUNT(id) FROM $table WHERE id >= $auto_increment_offset")); |
| 76 |
|
| 77 |
return $info; |
| 78 |
} |
| 79 |
|
| 80 |
public function process($request) { |
| 81 |
$db = $this->db; |
| 82 |
$params = $request->params; |
| 83 |
$table = $params['table']; |
| 84 |
$bvTable = $db->getBVTable($table); |
| 85 |
$auto_increment_offset = $params['auto_increment_offset']; |
| 86 |
if (!$db->isTablePresent($bvTable)) { |
| 87 |
$resp = array("info" => false); |
| 88 |
} else { |
| 89 |
switch ($request->method) { |
| 90 |
case "ipstrinfo": |
| 91 |
$info = $this->getIPStoreInfo($bvTable, $auto_increment_offset); |
| 92 |
$resp = array("info" => $info); |
| 93 |
break; |
| 94 |
case "insrtips": |
| 95 |
$values = $params['values']; |
| 96 |
$fields = $params['fields']; |
| 97 |
if (array_key_exists('rmfilter', $params)) { |
| 98 |
$db->deleteBVTableContent($table, $params['rmfilter']); |
| 99 |
} |
| 100 |
$this->insertIPs($bvTable, $fields, $values); |
| 101 |
$resp = array("offset" => $this->getIPStoreOffset($bvTable, $auto_increment_offset)); |
| 102 |
break; |
| 103 |
case "dltips": |
| 104 |
$rmfilters = $params['rmfilters']; |
| 105 |
$this->deleteIPs($table, $rmfilters); |
| 106 |
$resp = array("offset" => $this->getIPStoreOffset($bvTable, $auto_increment_offset)); |
| 107 |
break; |
| 108 |
case "updtips": |
| 109 |
$value = $params['value']; |
| 110 |
$filters = $params['filters']; |
| 111 |
$this->updateIPs($bvTable, $value, $filters); |
| 112 |
$resp = array("offset" => $this->getIPStoreOffset($bvTable, $auto_increment_offset)); |
| 113 |
break; |
| 114 |
default: |
| 115 |
$resp = false; |
| 116 |
} |
| 117 |
return $resp; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
endif; |