Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * CountryBlock PHP implementation class.
- * Version 1.0
- *
- * @link http://countryblock.io
- * @author Kristoffer Alfheim
- * @copyright CountryBlock.io 2014
- * @license https://creativecommons.org/licenses/by-sa/4.0/ Attribution-ShareAlike 4.0 International
- */
- /**
- * CountryBlock configuration.
- * @static
- * @access public
- */
- class __countryblock_config
- {
- /**
- * REQUIRED!!
- * Website ID.
- * @var int
- */
- const WS_ID = x;
- /**
- * REQUIRED!!
- * Website hash.
- * @var string
- */
- const WS_HASH = 'xx';
- /**
- * Enable if website is using CloudFlare.
- * @var boolean
- */
- const CLOUDFLARE = false;
- /**
- * Connection timeout in seconds.
- * @var int
- */
- const TIMEOUT_SECS = 3;
- /**
- * What happens if connection times out.
- * 'allow' or 'deny'
- * @var string
- */
- const TIMEOUT_ACTION = 'allow';
- /**
- * What happens if an unexpected error occurs.
- * 'allow' or 'deny'
- * @var string
- */
- const ERROR_ACTION = 'allow';
- /**
- * Default redirection URL.
- * In case CountryBlock is unavailable.
- * @var string
- */
- const DEFAULT_REDIR_URL = 'http://countryblock.io/blocked.php';
- /**
- * CountryBlock API URL.
- * @var string. Include http(s). No trailing slash.
- */
- const API_URL = 'http://countryblock.io/api';
- }
- /**
- * Main CountryBlock class.
- *
- * Do NOT edit anything below this line!
- * (unless you know what you're doing)
- *
- * @static
- */
- class __countryblock_main
- {
- /**
- * Version number.
- * @var string
- */
- const VERSION = '1.0';
- /**
- * Access codes
- * @var int
- */
- const ACCESS_YES = 1;
- const ACCESS_NO = 2;
- const ACCESS_ERR = 3;
- const ACCESS_TIMEOUT = 4;
- /**
- * Redirect URL gets stored here.
- * @var string
- * @access private
- */
- private static $redir_url;
- /**
- * Initialize.
- * @return void
- * @access public
- */
- public static function init()
- {
- $ac = self::get_code();
- if ($ac == self::ACCESS_NO || ($ac == self::ACCESS_ERR && __countryblock_config::ERROR_ACTION == 'deny'))
- {
- header('Location: '.(empty(self::$redir_url) ? __countryblock_config::DEFAULT_REDIR_URL : self::$redir_url));
- exit('Access denied.');
- }
- }
- /**
- * Get access code from CountryBlock servers.
- * @return int
- * @access private
- */
- private static function get_code()
- {
- if (!self::ip())
- return self::ACCESS_ERR;
- $result = function_exists('curl_init') ? self::req_curl() : self::req_file();
- if (empty($result) || mb_substr_count($result, "\n") != 1)
- return self::ACCESS_ERR;
- $result = explode("\n", $result);
- $code = intval($result[0]);
- self::$redir_url = $result[1];
- switch ($code)
- {
- case self::ACCESS_YES:
- case self::ACCESS_NO:
- return $code;
- case self::ACCESS_TIMEOUT;
- return __countryblock_config::TIMEOUT_ACTION != 'deny' ? self::ACCESS_YES : self::ACCESS_NO;
- }
- return self::ACCESS_ERR;
- }
- /**
- * Use cURL to perform HTTP request.
- * @return mixed
- * @access private
- */
- private static function req_curl()
- {
- $ch = curl_init(self::req_check_url());
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, __countryblock_config::TIMEOUT_SECS);
- curl_setopt($ch, CURLOPT_TIMEOUT, __countryblock_config::TIMEOUT_SECS);
- curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_USERAGENT, 'class.countryblock.php '.self::VERSION.' cURL');
- $result = curl_exec($ch);
- $errno = curl_errno($ch);
- curl_close($ch);
- if ($errno != 0)
- return false;
- return $result;
- }
- /**
- * Use file_get_contents() to perform HTTP request.
- * @return string
- * @access private
- */
- private static function req_file()
- {
- $result = file_get_contents(self::req_check_url(), stream_context_create(array
- (
- 'http' => array
- (
- 'method' => 'GET',
- 'user_agent' => 'class.countryblock.php '.self::VERSION.' file',
- 'timeout' => __countryblock_config::TIMEOUT_SECS
- )
- )));
- if (!$result)
- return false;
- return $result;
- }
- /**
- * Parse check request URL.
- * @return string
- * @access private
- */
- private static function req_check_url()
- {
- return __countryblock_config::API_URL.'/check.php?i='.__countryblock_config::WS_ID.'&h='.__countryblock_config::WS_HASH.'&ip='.self::ip();
- }
- /**
- * Get the users IP address.
- * @return string, false on error
- * @access private
- */
- private static function ip()
- {
- if (__countryblock_config::CLOUDFLARE)
- $s = 'HTTP_CF_CONNECTING_IP';
- else
- $s = 'REMOTE_ADDR';
- return isset($_SERVER[$s]) ? $_SERVER[$s] : false;
- }
- }
- __countryblock_main::init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement