-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoIPContent.php
64 lines (55 loc) · 1.74 KB
/
GeoIPContent.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
<?php
/**
* ContentGeoIP plugin
* Inspired by DirectPHP plugin
*/
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );
jimport( 'joomla.event.plugin' );
class plgContentGeoIPContent extends JPlugin {
//PHP4 Compatibility
function plgContentGeoIP( &$subject, $params ) {
parent::__construct( $subject, $params );
}
function onContentPrepare( $context, &$article, &$params, $limitstart=0 ) {
$pluginParams = $this->params;
require_once('geoip-api-php/geoip.inc');
$maxmind_geoip_file = dirname(__FILE__).'/maxmind-data/GeoIP.dat';
$gi = geoip_open($maxmind_geoip_file, GEOIP_STANDARD);
$client_ip = $this->getClientIp();
$client_ip_code = geoip_country_code_by_addr($gi, $client_ip);
geoip_close($gi);
$contents = $article->text;
$output = '';
$regexp = '/(.*?)'.'\{GEO ([\!A-Za-z]{2,4})\}'.'\s*(.*?)'.'\{\/GEO\}'.'(.*)/s';
$found = preg_match($regexp, $contents, $matches);
while ($found) {
$output .= $matches[1];
$country = $matches[2];
if ($country[0] == '!') {
if (strtoupper(substr($country,1)) != $client_ip_code) {
$output .= $matches[3];
}
} else {
if (strtoupper($country) == $client_ip_code) {
$output .= $matches[3];
}
}
$contents = $matches[4];
$found = preg_match($regexp, $contents, $matches);
}
$output .= $contents;
$article->text = $output;
return true;
}
function getClientIp() {
//FIXME handling for proxy chain needed
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}