/** * @return bool */ public function isBlocked() { include_once ROOT . '/dataProvider/GeoIpLocation.php'; $ip = $_SERVER['REMOTE_ADDR']; if ($ip == '::1' || $ip == '127.0.0.1') { return false; } $geo_data = GeoIpLocation::getGeoLocation($ip); if ($geo_data === false) { $sql = 'SELECT * FROM `ip_access_rules` WHERE active = 1 AND ip = :ip1 OR ip = :ip2 ORDER BY weight DESC LIMIT 1'; $where = []; $where[':ip1'] = '*'; $where[':ip2'] = $ip; } else { $sql = 'SELECT * FROM `ip_access_rules` WHERE active = 1 AND ip = :ip1 OR ip = :ip2 OR country_code = :country_code ORDER BY weight DESC LIMIT 1'; $where = []; $where[':ip1'] = '*'; $where[':ip2'] = $ip; $where[':country_code'] = $geo_data['country_code']; } $conn = Matcha::getConn(); $sth = $conn->prepare($sql); $sth->execute($where); $result = $sth->fetch(PDO::FETCH_ASSOC); if ($result !== false) { $blocked = $result['rule'] == 'BLK'; } else { // if no rule found blocked the IP if not inside local network $blocked = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) !== false; } if ($blocked) { $record = new stdClass(); $record->ip = $ip; $record->country_code = $geo_data !== false ? $geo_data['country_code'] : ''; $record->event = 'Blocked'; $record->create_date = date('Y-m-d H:i:s'); $this->l->save($record); } return $blocked; }