This repository was archived by the owner on Jul 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCFBlockCountry.php
More file actions
275 lines (246 loc) · 8.58 KB
/
Copy pathCFBlockCountry.php
File metadata and controls
275 lines (246 loc) · 8.58 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* @version cfBlockCountry.php 1.4-mod
* @package Joomla
* @copyright Copyright (C) 2018
* The code has been written by www.CodeFire.in in case of any questions please contact joomla@codefire.in
* This a a modified version with added whitelist and new a option to allow or deny country codes which
* can be selected in plugin options in backend. Also "external" geoip is disabled since url is offline.
* @license GNU/GPL, see LICENSE.php
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.plugin.plugin' );
$geoip2_phar = JPATH_LIBRARIES . '/geoip/geoip2.phar';
if(file_exists($geoip2_phar)) {
require_once($geoip2_phar);
}
use GeoIp2\Database\Reader;
/**
* @package Joomla
* @subpackage System
*/
class plgSystemCFBlockCountry extends JPlugin {
function onAfterDispatch() {
//global $mainframe;
//$params = new JParameter( $plugin->params );
//if ( !$app->isSite() ) return;
$plugin =& JPluginHelper::getPlugin( 'system', 'CFBlockCountry' );
$app =& JFactory::getApplication();
$layer = $this->params->get( 'layer', '' );
if ($app->isSite()) { $scope = 'site'; } elseif ($app->isAdmin()) { $scope = 'admin'; }
if ( $layer == 'frontend' && $scope != 'site' ) return;
if ( $layer == 'backend' && $scope != 'admin' ) return;
global $geoip_version;
$countryCodes = $this->params->get( 'country', '' );
$action = $this->params->get( 'action', '' );
$wblist = $this->params->get( 'wblist', 0 );
$ipfile = $this->params->get( 'ipfile', '');
// external does not work anymore
//$external = $this->params->get( 'external', 0 );
$textMsgForBlocked = $this->params->get( 'blockedText', '' );
$option = $this->params->get( 'show_msg', 0 );
$site = $this->params->get( 'site', '' );
$log = $this->params->get( 'log', '' );
$geoip_version = $this->params->get( 'geoip_version', 1 );
global $debug;
$debug = 0;
global $valid_ip;
$valid_ip = $this->get_ip_address();
$country = '';
$blocked = function($reason, $match) use ($scope, $valid_ip, $option, $textMsgForBlocked, $site, $log) {
if (isset($log) && $log != '') {
$data = @date('Y-m-d H:i:s') . " " . str_pad($scope, 6) . str_pad($reason . ":", 11) . $valid_ip . " ($match)";
file_put_contents(JPATH_ROOT . $log, $data . PHP_EOL, FILE_APPEND);
}
if ($option == 0) {
echo $textMsgForBlocked;
exit;
} else {
if (isset($site) && $site != '') {
header("Location: $site");
exit;
}
}
};
// BEGIN: whitelist blacklist mod, v0.1 2014-06-16
if ($wblist == 1) {
if (strpos($valid_ip, ":") === false) {
function check_ip($low_ip, $high_ip) {
global $valid_ip;
if ((!isset($valid_ip)) || (empty($valid_ip)) || (is_null($valid_ip))) {
$valid_ip = $this->get_ip_address();
}
$ip = ip2long($valid_ip);
$lo_ip = ip2long("$low_ip");
$hi_ip = ip2long("$high_ip");
return ($ip <= $hi_ip && $lo_ip <= $ip);
}
$iplist = require($ipfile);
foreach($iplist['whitelist'] as $key=>$val) {
if(isset($val)) {
list($low_ip, $high_ip) = explode(" ", $val);
if (check_ip($low_ip, $high_ip)) {
//if (isset($log) && $log != '') {
// $data = @date('Y-m-d H:i:s'). " whitelist: " . $_SERVER['REMOTE_ADDR'] . " ($low_ip $high_ip)";
// file_put_contents(JPATH_ROOT . $log, $data . PHP_EOL, FILE_APPEND);
//}
return;
}
}
}
foreach($iplist['blacklist'] as $key=>$val) {
if(isset($val)) {
list($low_ip, $high_ip) = explode(" ", $val);
if (check_ip($low_ip, $high_ip)) {
$blocked("blacklist", "$low_ip $high_ip");
}
}
}
}
}
// END: whitelist blacklist mod
/*
* Disabled: external
*
if(!$external){
$country = $this->getCountryCodeByIP();
$country = $country['code'];
}
else {
$country = $this->externalCall();
}
*/
/*
* Replaced: see $blocked above and allow/deny code below
*
$block = explode(',', $blockedCountryCodes);
foreach($block as $key => $val){
if(isset($val) && trim($val) != '' && $country == trim($val) ){
if($option == 0)
{
echo $textMsgForBlocked;
exit;
}else{
if(isset($site) && $site != '')
header("Location: $site");
exit;
}
}
}
*/
$country = $this->getCountryCodeByIP();
$country = $country['code'];
$ccodes = explode(',', $countryCodes);
if($action == 'allow') {
foreach($ccodes as $key => $val) {
if(isset($val) && trim($val) != '' && $country != trim($val) ) {
$blocked("cc_allow", "$country");
}
}
} else {
foreach($ccodes as $key => $val) {
if(isset($val) && trim($val) != '' && $country == trim($val) ) {
$blocked("cc_deny", "$country");
}
}
}
}
function getCountryCodeByIP() {
global $geoip_version;
global $valid_ip;
if ((!isset($valid_ip)) || (empty($valid_ip)) || (is_null($valid_ip))) {
$valid_ip = $this->get_ip_address();
}
$country = array();
// GeoIP legacy
if ($geoip_version == 1) {
require(JPATH_LIBRARIES.'/geoip/geoip.inc');
/*
* Replaced by ipv6 aware code block below which also uses GeoIPv6.dat
*
$gi = geoip_open(JPATH_LIBRARIES."/geoip/GeoIP.dat",GEOIP_STANDARD);
$country = array();
$country['code'] = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
$country['name'] = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);
// test: $db = (strpos($_SERVER['REMOTE_ADDR'], ":") === false) ? "/geoip/GeoIP.dat" : "/geoip/GeoIPv6.dat";
*/
if (strpos($valid_ip, ":") === false) {
$gi = geoip_open(JPATH_LIBRARIES."/geoip/GeoIP.dat",GEOIP_STANDARD);
$country['code'] = geoip_country_code_by_addr($gi, $valid_ip);
$country['name'] = geoip_country_name_by_addr($gi, $valid_ip);
} else {
$gi = geoip_open(JPATH_LIBRARIES."/geoip/GeoIPv6.dat",GEOIP_STANDARD);
$country['code'] = geoip_country_code_by_addr_v6($gi, $valid_ip);
$country['name'] = geoip_country_name_by_addr_v6($gi, $valid_ip);
}
geoip_close($gi);
// GeoIP2
} elseif ($geoip_version == 2) {
$reader = new Reader(JPATH_LIBRARIES . '/geoip/GeoLite2-Country.mmdb');
$record = $reader->country($valid_ip);
$country = array();
$country['code'] = $record->country->isoCode;
$country['name'] = $record->country->name;
}
global $debug;
if($debug == 1) {
$log = $this->params->get( 'log', '' );
if (isset($log) && $log != '') {
$data = @date('Y-m-d H:i:s') . " DEBUG: " . $valid_ip . " " . $country['code'] . " " . $country['name'];
file_put_contents(JPATH_ROOT . $log, $data . PHP_EOL, FILE_APPEND);
}
}
return $country;
}
/*
* External does not work anymore, url is inreachable
* The code block below is no longer used
*
function externalCall() {
define('POSTURL', 'http://joomla.codefire.in/apps/geoip/index.php');
define('POSTVARS', 'ip=');
$ip = $_SERVER['REMOTE_ADDR'];
$ch = curl_init(POSTURL);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,POSTVARS.$ip);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
*/
/*
* Get valid IP address
* https://gist.github.com/cballou/2201933
*/
function get_ip_address() {
$ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR', 'HTTP_X_REAL_IP');
foreach ($ip_keys as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
// trim for safety measures
$ip = trim($ip);
// attempt to validate IP
if ($this->validate_ip($ip)) {
return $ip;
}
}
}
}
return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
}
/*
* Ensures an ip address is both a valid IP and does not fall within
* a private network range.
*/
function validate_ip($ip)
{
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {
return false;
}
return true;
}
}
/* vim: set noai tabstop=4 shiftwidth=4 softtabstop=4 noexpandtab: */