-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
115 lines (104 loc) · 3.9 KB
/
index.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
<?php
require_once 'vendor/autoload.php';
use UAParser\Parser;
use GeoIp2\Database\Reader;
define('DOCROOT', __DIR__.DIRECTORY_SEPARATOR);
define('VENDORPATH', realpath(__DIR__.'/vendor/').DIRECTORY_SEPARATOR);
function downloadGeo()
{
set_time_limit(0);
//This input should be from somewhere else, hard-coded in this example
$file_name = './vendor/geoip2/geoip2/maxmind-db/GeoLite2-City.mmdb.gz';
//get GeoLite2 from HTTP
file_put_contents($file_name, fopen('http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz', 'r'));
// Raising this value may increase performance
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '', $file_name);
// Open our files (in binary mode)
$file = gzopen($file_name, 'rb');
$out_file = fopen($out_file_name, 'wb');
// Keep repeating until the end of the input file
while (!gzeof($file)) {
// Read buffer-size bytes
// Both fwrite and gzread and binary-safe
fwrite($out_file, gzread($file, $buffer_size));
}
// Files are done, close files
fclose($out_file);
gzclose($file);
//remove file
unlink($file_name);
}
function getGeo()
{
if(!file_exists(VENDORPATH.'/geoip2/geoip2/maxmind-db/GeoLite2-City.mmdb')){
downloadGeo();
}
$reader = new Reader(VENDORPATH.'/geoip2/geoip2/maxmind-db/GeoLite2-City.mmdb', array('ru'));
$data = array();
$data['ip'] = $_SERVER['REMOTE_ADDR'];
// $data['ip'] = "54.242.105.109";
try {
$resp = $reader->city($data['ip']);
$data['country'] = (($resp->country->isoCode != null) ? $resp->country->isoCode : "UN");
$city = null;
if($resp->city->name != null){
$city = $resp->city->name;
} elseif($resp->city->names['en'] != null) {
$city = $resp->city->names['en'];
} elseif($resp->mostSpecificSubdivision->name != null) {
$city = $resp->mostSpecificSubdivision->name;
} elseif($resp->mostSpecificSubdivision->names['en'] != null) {
$city = $resp->mostSpecificSubdivision->names['en'];
} else {
$city = "Unknown";
}
$data['city'] = $city;
} catch (GeoIp2\Exception\AddressNotFoundException $e) {
if((ip2long($data['ip']) >= 167772160 && ip2long($data['ip']) <= 184549375)
|| (ip2long($data['ip']) >= 2886729728 && ip2long($data['ip']) <= 2887778303)
|| (ip2long($data['ip']) >= 3232235520 && ip2long($data['ip']) <= 3232301055)) { //networks classes A,B,C
$data['country'] = 'LO';
$data['city'] = 'Local Network';
} elseif((ip2long($data['ip']) >= 2130706432 && ip2long($data['ip']) <= 2147483647)){
$data['country'] = 'LO';
$data['city'] = 'Loopback';
} else {
$data['country'] = 'UN';
$data['city'] = 'Unknown';
}
}
return $data;
}
function isMobile()
{
$notMobile = array(
'Other',
'Spider',
'WebTV',
'Nintendo Wii',
'Nintendo DS',
'PlayStation 3',
'PlayStation Portable'
);
$parser = Parser::create();
$result = $parser->parse($_SERVER['HTTP_USER_AGENT']);
$isMobile = !in_array($result->device->family, $notMobile);
return $isMobile;
}
$geo = getGeo();
$page = file_get_contents('./desktop.tmpl', true);
if(isMobile()) {
$page = file_get_contents((file_exists('./mobile.tmpl') ? './mobile.tmpl' : './desktop.tmpl'), true);
}
$locale = file_get_contents('locale.json');
$jsonLocale = json_decode($locale);
foreach ($jsonLocale as $key => $value) {
if(property_exists($jsonLocale->$key, $geo['country'])) {
$arrValue = get_object_vars($jsonLocale->$key);
$page = str_replace("{{%".$key."%}}", $arrValue[$geo['country']], $page);
} else {
$page = str_replace("{{%".$key."%}}", $jsonLocale->$key->RU, $page);
}
}
echo $page;