Advertisement
Guest User

class.countryblock.php

a guest
May 24th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.68 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * CountryBlock PHP implementation class.
  5.  * Version 1.0
  6.  *
  7.  * @link http://countryblock.io
  8.  * @author Kristoffer Alfheim
  9.  * @copyright CountryBlock.io 2014
  10.  * @license https://creativecommons.org/licenses/by-sa/4.0/ Attribution-ShareAlike 4.0 International
  11.  */
  12.  
  13. /**
  14.  * CountryBlock configuration.
  15.  * @static
  16.  * @access public
  17.  */
  18. class __countryblock_config
  19. {
  20.     /**
  21.      * REQUIRED!!
  22.      * Website ID.
  23.      * @var int
  24.      */
  25.     const WS_ID = x;
  26.  
  27.     /**
  28.      * REQUIRED!!
  29.      * Website hash.
  30.      * @var string
  31.      */
  32.     const WS_HASH = 'xx';
  33.  
  34.     /**
  35.      * Enable if website is using CloudFlare.
  36.      * @var boolean
  37.      */
  38.     const CLOUDFLARE = false;
  39.  
  40.     /**
  41.      * Connection timeout in seconds.
  42.      * @var int
  43.      */
  44.     const TIMEOUT_SECS = 3;
  45.  
  46.     /**
  47.      * What happens if connection times out.
  48.      * 'allow' or 'deny'
  49.      * @var string
  50.      */
  51.     const TIMEOUT_ACTION = 'allow';
  52.  
  53.     /**
  54.      * What happens if an unexpected error occurs.
  55.      * 'allow' or 'deny'
  56.      * @var string
  57.      */
  58.     const ERROR_ACTION = 'allow';
  59.  
  60.     /**
  61.      * Default redirection URL.
  62.      * In case CountryBlock is unavailable.
  63.      * @var string
  64.      */
  65.     const DEFAULT_REDIR_URL = 'http://countryblock.io/blocked.php';
  66.  
  67.     /**
  68.      * CountryBlock API URL.
  69.      * @var string. Include http(s). No trailing slash.
  70.      */
  71.     const API_URL = 'http://countryblock.io/api';
  72. }
  73.  
  74. /**
  75.  * Main CountryBlock class.
  76.  *
  77.  * Do NOT edit anything below this line!
  78.  * (unless you know what you're doing)
  79.  *
  80.  * @static
  81.  */
  82. class __countryblock_main
  83. {
  84.     /**
  85.      * Version number.
  86.      * @var string
  87.      */
  88.     const VERSION = '1.0';
  89.  
  90.     /**
  91.      * Access codes
  92.      * @var int
  93.      */
  94.     const ACCESS_YES = 1;
  95.     const ACCESS_NO = 2;
  96.     const ACCESS_ERR = 3;
  97.     const ACCESS_TIMEOUT = 4;
  98.  
  99.     /**
  100.      * Redirect URL gets stored here.
  101.      * @var string
  102.      * @access private
  103.      */
  104.     private static $redir_url;
  105.  
  106.     /**
  107.      * Initialize.
  108.      * @return void
  109.      * @access public
  110.      */
  111.     public static function init()
  112.     {
  113.         $ac = self::get_code();
  114.         if ($ac == self::ACCESS_NO || ($ac == self::ACCESS_ERR && __countryblock_config::ERROR_ACTION == 'deny'))
  115.         {
  116.             header('Location: '.(empty(self::$redir_url) ? __countryblock_config::DEFAULT_REDIR_URL : self::$redir_url));
  117.             exit('Access denied.');
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * Get access code from CountryBlock servers.
  123.      * @return int
  124.      * @access private
  125.      */
  126.     private static function get_code()
  127.     {
  128.         if (!self::ip())
  129.             return self::ACCESS_ERR;
  130.  
  131.         $result = function_exists('curl_init') ? self::req_curl() : self::req_file();
  132.  
  133.         if (empty($result) || mb_substr_count($result, "\n") != 1)
  134.             return self::ACCESS_ERR;
  135.  
  136.         $result = explode("\n", $result);
  137.         $code = intval($result[0]);
  138.         self::$redir_url = $result[1];
  139.  
  140.         switch ($code)
  141.         {
  142.             case self::ACCESS_YES:
  143.             case self::ACCESS_NO:
  144.                 return $code;
  145.  
  146.             case self::ACCESS_TIMEOUT;
  147.                 return __countryblock_config::TIMEOUT_ACTION != 'deny' ? self::ACCESS_YES : self::ACCESS_NO;
  148.         }
  149.  
  150.         return self::ACCESS_ERR;
  151.     }
  152.  
  153.     /**
  154.      * Use cURL to perform HTTP request.
  155.      * @return mixed
  156.      * @access private
  157.      */
  158.     private static function req_curl()
  159.     {
  160.         $ch = curl_init(self::req_check_url());
  161.  
  162.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, __countryblock_config::TIMEOUT_SECS);
  163.         curl_setopt($ch, CURLOPT_TIMEOUT, __countryblock_config::TIMEOUT_SECS);
  164.         curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
  165.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  166.         @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  167.         curl_setopt($ch, CURLOPT_USERAGENT, 'class.countryblock.php '.self::VERSION.' cURL');
  168.  
  169.         $result = curl_exec($ch);
  170.         $errno = curl_errno($ch);
  171.         curl_close($ch);
  172.  
  173.         if ($errno != 0)
  174.             return false;
  175.  
  176.         return $result;
  177.     }
  178.  
  179.     /**
  180.      * Use file_get_contents() to perform HTTP request.
  181.      * @return string
  182.      * @access private
  183.      */
  184.     private static function req_file()
  185.     {
  186.         $result = file_get_contents(self::req_check_url(), stream_context_create(array
  187.         (
  188.             'http' => array
  189.             (
  190.                 'method' => 'GET',
  191.                 'user_agent' => 'class.countryblock.php '.self::VERSION.' file',
  192.                 'timeout' => __countryblock_config::TIMEOUT_SECS
  193.             )
  194.         )));
  195.  
  196.         if (!$result)
  197.             return false;
  198.  
  199.         return $result;
  200.     }
  201.  
  202.     /**
  203.      * Parse check request URL.
  204.      * @return string
  205.      * @access private
  206.      */
  207.     private static function req_check_url()
  208.     {
  209.         return __countryblock_config::API_URL.'/check.php?i='.__countryblock_config::WS_ID.'&h='.__countryblock_config::WS_HASH.'&ip='.self::ip();
  210.     }
  211.  
  212.     /**
  213.      * Get the users IP address.
  214.      * @return string, false on error
  215.      * @access private
  216.      */
  217.     private static function ip()
  218.     {
  219.         if (__countryblock_config::CLOUDFLARE)
  220.             $s = 'HTTP_CF_CONNECTING_IP';
  221.         else
  222.             $s = 'REMOTE_ADDR';
  223.  
  224.         return isset($_SERVER[$s]) ? $_SERVER[$s] : false;
  225.     }
  226. }
  227.  
  228. __countryblock_main::init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement