1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* ****************************************************************************** |
5
|
|
|
* Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 |
6
|
|
|
* and GN4-2 consortia |
7
|
|
|
* |
8
|
|
|
* License: see the web/copyright.php file in the file structure |
9
|
|
|
* ****************************************************************************** |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/* |
13
|
|
|
* This product includes GeoLite data created by MaxMind, available from |
14
|
|
|
* http://www.maxmind.com |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace core; |
18
|
|
|
|
19
|
|
|
use GeoIp2\Database\Reader; |
20
|
|
|
use \Exception; |
21
|
|
|
|
22
|
|
|
class UserLocation { |
23
|
|
|
/** |
24
|
|
|
* find out where the user is currently located |
25
|
|
|
* @return array |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public function __construct() { |
28
|
|
|
$geoipVersion = CONFIG['GEOIP']['version'] ?? 0; |
29
|
|
|
switch ($geoipVersion) { |
30
|
|
|
case 0: |
31
|
|
|
$this->location = ['status' => 'error', 'error' => 'Geolocation not supported']; |
32
|
|
|
break; |
33
|
|
|
case 1: |
34
|
|
|
$this->location = $this->locateUser1(); |
35
|
|
|
break; |
36
|
|
|
case 2: |
37
|
|
|
$this->location = $this->locateUser2(); |
38
|
|
|
break; |
39
|
|
|
default: |
40
|
|
|
throw new Exception("This version of GeoIP is not known!"); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
private function locateUser1() { |
46
|
|
View Code Duplication |
if (CONFIG['GEOIP']['version'] != 1) { |
|
|
|
|
47
|
|
|
return ['status' => 'error', 'error' => 'Function for GEOIPv1 called, but config says this is not the version to use!']; |
48
|
|
|
} |
49
|
|
|
//$host = $_SERVER['REMOTE_ADDR']; |
|
|
|
|
50
|
|
|
$host = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP); |
51
|
|
|
$record = geoip_record_by_name($host); |
52
|
|
|
if ($record === FALSE) { |
53
|
|
|
return ['status' => 'error', 'error' => 'Problem getting the address']; |
54
|
|
|
} |
55
|
|
|
$result = ['status' => 'ok']; |
56
|
|
|
$result['country'] = $record['country_code']; |
57
|
|
|
// the two lines below are a dirty hack to take of the error in naming the UK federation |
58
|
|
|
if ($result['country'] == 'GB') { |
59
|
|
|
$result['country'] = 'UK'; |
60
|
|
|
} |
61
|
|
|
$result['region'] = $record['region']; |
62
|
|
|
$result['geo'] = ['lat' => (float) $record['latitude'], 'lon' => (float) $record['longitude']]; |
63
|
|
|
return($result); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* find out where the user is currently located, using GeoIP2 |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
private function locateUser2() { |
71
|
|
View Code Duplication |
if (CONFIG['GEOIP']['version'] != 2) { |
|
|
|
|
72
|
|
|
return ['status' => 'error', 'error' => 'Function for GEOIPv2 called, but config says this is not the version to use!']; |
73
|
|
|
} |
74
|
|
|
require_once CONFIG['GEOIP']['geoip2-path-to-autoloader']; |
75
|
|
|
$reader = new Reader(CONFIG['GEOIP']['geoip2-path-to-db']); |
76
|
|
|
$host = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP); |
77
|
|
|
try { |
78
|
|
|
$record = $reader->city($host); |
79
|
|
|
} catch (\Exception $e) { |
80
|
|
|
$result = ['status' => 'error', 'error' => 'Problem getting the address']; |
81
|
|
|
return($result); |
82
|
|
|
} |
83
|
|
|
$result = ['status' => 'ok']; |
84
|
|
|
$result['country'] = $record->country->isoCode; |
85
|
|
|
// the two lines below are a dirty hack to take of the error in naming the UK federation |
86
|
|
|
if ($result['country'] == 'GB') { |
87
|
|
|
$result['country'] = 'UK'; |
88
|
|
|
} |
89
|
|
|
$result['region'] = $record->continent->name; |
90
|
|
|
|
91
|
|
|
$result['geo'] = ['lat' => (float) $record->location->latitude, 'lon' => (float) $record->location->longitude]; |
92
|
|
|
return($result); |
93
|
|
|
} |
94
|
|
|
public $location; |
95
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.