| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; |
| 3 |
if (!class_exists('BVIPStore')) : |
| 4 |
|
| 5 |
class BVIPStore { |
| 6 |
|
| 7 |
public $db; |
| 8 |
public static $name = 'ip_store'; |
| 9 |
|
| 10 |
#TYPE |
| 11 |
const BLACKLISTED = 1; |
| 12 |
const WHITELISTED = 2; |
| 13 |
|
| 14 |
#CATEGORY |
| 15 |
const FW = 3; |
| 16 |
const LP = 4; |
| 17 |
|
| 18 |
function __construct($db) { |
| 19 |
$this->db = $db; |
| 20 |
} |
| 21 |
|
| 22 |
function init() { |
| 23 |
add_action('clear_ip_store', array($this, 'clearConfig')); |
| 24 |
} |
| 25 |
|
| 26 |
public function clearConfig() { |
| 27 |
$this->db->dropBVTable(BVIPStore::$name); |
| 28 |
} |
| 29 |
|
| 30 |
public function isLPIPBlacklisted($ip) { |
| 31 |
return $this->checkIPPresent($ip, BVIPStore::BLACKLISTED, BVIPStore::LP); |
| 32 |
} |
| 33 |
|
| 34 |
public function isLPIPWhitelisted($ip) { |
| 35 |
return $this->checkIPPresent($ip, BVIPStore::WHITELISTED, BVIPStore::LP); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
public function isFWIPBlacklisted($ip) { |
| 40 |
return $this->checkIPPresent($ip, BVIPStore::BLACKLISTED, BVIPStore::FW); |
| 41 |
} |
| 42 |
|
| 43 |
public function isFWIPWhitelisted($ip) { |
| 44 |
return $this->checkIPPresent($ip, BVIPStore::WHITELISTED, BVIPStore::FW); |
| 45 |
} |
| 46 |
|
| 47 |
public function checkIPPresent($ip, $type, $category) { |
| 48 |
$db = $this->db; |
| 49 |
$table = $db->getBVTable(BVIPStore::$name); |
| 50 |
if ($db->isTablePresent($table)) { |
| 51 |
$binIP = BVProtectBase::bvInetPton($ip); |
| 52 |
if ($binIP !== false) { |
| 53 |
$category_str = ($category == BVIPStore::FW) ? "`is_fw` = true" : "`is_lp` = true"; |
| 54 |
$query_str = "SELECT * FROM $table WHERE %s >= `start_ip_range` && %s <= `end_ip_range` && " . $category_str . " && `type` = %d LIMIT 1;"; |
| 55 |
$query = $db->prepare($query_str, array($binIP, $binIP, $type)); |
| 56 |
if ($db->getVar($query) > 0) |
| 57 |
return true; |
| 58 |
} |
| 59 |
return false; |
| 60 |
} |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
endif; |