-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcountry-block.inc.php
146 lines (127 loc) · 4.26 KB
/
country-block.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
*
* @author Matt Gross <[email protected]>
* @link https://github.com/MatthewGross/PHP-Country-Block
* @license MIT - http://mattgross.mit-license.org/
*
* A basic PHP IP Address blocker for certain countries provided by the user.
* Uses IP Info DB <http://ipinfodb.com/>
* Uses IP Info DB PHP Wrapper <http://github.com/beingtomgreen/IP-User-Location>
*
*/
class countryBlock
{
// Store Country
private $countries = null;
// Store Visitors IP Address
private $ip_address = null;
// Store ipInfo object
private $ipInfo = null;
// Returning line. Sets to true if user is blocked.
public $isBlocked = false;
/**
* __construct
*
* @param array $countries - All countries provided in an array
* @param string $apiKey - Your API key
* @param string $path_to_script - Directory path before the Uses IP Info DB PHP Wrapper
*
* @return true/false - true if ip has been blocked, false if ip has not been blocked and is permitted.
*
*/
public function __construct($countries, $api_key, $path_to_script = '')
{
// Include ipInfo.inc.php with or without path
include($path_to_script . 'ipInfo.inc.php');
// new ipInfo class with api_key from parameters
$this->ipInfo = new ipInfo($api_key);
// Save Countries to $countries
$this->countries = $countries;
// Save IP Address $ip_address
$this->ip_address = $this->ipInfo->getIPAddress();
// Check if cookie exists
if ($this->cookieCheck()) {
// returned true... cookie does not exist
foreach ($this->countries as $country) {
// return true or false
$blockable = $this->countryCheck($country);
// check
if ($blockable) {
// block
$this->setCookie("true");
// Change the return line.
$this->isBlocked = true;
// Break the loop if already blocked.
break;
}
}
} else {
// Block again. If the cookie is set, it means the user has been blocked before due to country and should be blocked again.
$this->isBlocked = true;
}
}
/**
* getCountry()
*
* Returns true or false based off if the country from the ip is in the $countries array
*
* @param string $country - country code of one country!
*
* @return true/false - true if same, false if not
*
*/
protected function countryCheck($country)
{
// Get Country from ipInfo API
$userCountry = $this->ipInfo->getCountry($this->ip_address);
// Using regex, take specific info out of returned string at $userCountry
$regex = ".*?((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\\d]).*?((?:[a-z][a-z]+)).*?((?:[a-z][a-z]+))";
if ($c = preg_match_all("/" . $regex . "/is", $userCountry, $matches)) {
$ipAddress = $matches[1][0];
$countryCode = $matches[2][0];
$countryName = $matches[3][0];
// Compare...
if ($country == $countryCode) {
// Should block
return true;
} else {
// Shouldn't block
return false;
}
}
return false;
}
/**
* cookieCheck()
*
* Returns true or false depending if the cookie ('ip_not_allowed') is set or not
*
* @return true/false - true if not set, false if is set
*
*/
protected function cookieCheck()
{
if (!isset($_COOKIE['ip_not_allowed'])) {
return true;
} else {
return false;
}
}
/**
* setCookie()
*
* Sets cookie and returns true
*
* @param string $value - Value to set the cookie. "true/false"
*
* @return true
*
*/
protected function setCookie($value)
{
setcookie('ip_not_allowed', $value);
$_COOKIE['ip_not_allowed'] = $value;
return true;
}
}