| 1 |
<?php |
| 2 |
if (!defined('MCDATAPATH')) exit; |
| 3 |
|
| 4 |
if (!class_exists('BVPrependProtect')) : |
| 5 |
|
| 6 |
require_once dirname( __FILE__ ) . '/../base.php'; |
| 7 |
require_once dirname( __FILE__ ) . '/../fw/fw.php'; |
| 8 |
require_once dirname( __FILE__ ) . '/../fw/request.php'; |
| 9 |
require_once dirname( __FILE__ ) . '/../fw/config.php'; |
| 10 |
require_once dirname( __FILE__ ) . '/info.php'; |
| 11 |
require_once dirname( __FILE__ ) . '/ipstore.php'; |
| 12 |
require_once dirname( __FILE__ ) . '/logger.php'; |
| 13 |
|
| 14 |
class BVPrependProtect { |
| 15 |
public $mcConfFile; |
| 16 |
public $mcIPsFile; |
| 17 |
public $mcRulesFile; |
| 18 |
|
| 19 |
function __construct() { |
| 20 |
$this->mcConfFile = MCDATAPATH . MCCONFKEY . '-' . 'mc.conf'; |
| 21 |
$this->mcIPsFile = MCDATAPATH . MCCONFKEY . '-' . 'mc_ips.conf'; |
| 22 |
$this->mcRulesFile = MCDATAPATH . MCCONFKEY . '-' . 'mc_rules.json'; |
| 23 |
} |
| 24 |
|
| 25 |
public function parseFile($fname) { |
| 26 |
$result = array(); |
| 27 |
|
| 28 |
if (file_exists($fname)) { |
| 29 |
$content = file_get_contents($fname); |
| 30 |
if (($content !== false) && is_string($content)) { |
| 31 |
$result = json_decode($content, true); |
| 32 |
|
| 33 |
if (!is_array($result)) { |
| 34 |
$result = array(); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return $result; |
| 40 |
} |
| 41 |
|
| 42 |
public function run() { |
| 43 |
$mcConf = $this->parseFile($this->mcConfFile); |
| 44 |
$mcIPsConf = $this->parseFile($this->mcIPsFile); |
| 45 |
$mcRuleSet = $this->parseFile($this->mcRulesFile); |
| 46 |
|
| 47 |
if (!array_key_exists('time', $mcConf) || !isset($mcConf['time']) || !($mcConf['time'] > time() - (48*3600))) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
$brand = array_key_exists('brandname', $mcConf) ? $mcConf['brandname'] : "Protect"; |
| 52 |
$bvinfo = new BVPrependInfo($brand); |
| 53 |
$bvipstore = new BVPrependIPStore($mcIPsConf); |
| 54 |
|
| 55 |
$ipHeader = array_key_exists('ipheader', $mcConf) ? $mcConf['ipheader'] : false; |
| 56 |
$ip = BVProtectBase::getIP($ipHeader); |
| 57 |
|
| 58 |
$fwlogger = new BVPrependLogger(); |
| 59 |
|
| 60 |
$fwConfHash = array_key_exists('fw', $mcConf) ? $mcConf['fw'] : array(); |
| 61 |
$fw = BVFW::getInstance($fwlogger, $fwConfHash, $ip, $bvinfo, $bvipstore, $mcRuleSet); |
| 62 |
|
| 63 |
if ($fw->isActive()) { |
| 64 |
|
| 65 |
if ($fw->canSetIPCookie()) { |
| 66 |
$fw->setIPCookie(); |
| 67 |
} |
| 68 |
|
| 69 |
register_shutdown_function(array($fw, 'log')); |
| 70 |
|
| 71 |
$fw->execute(); |
| 72 |
$fw->executeRules(); |
| 73 |
define('MCWAFLOADED', true); |
| 74 |
} |
| 75 |
|
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
endif; |