.htaccess
7 years ago
bootstrap.php
6 years ago
wfWAFGeoIP2.php
7 years ago
wfWAFIPBlocksController.php
6 years ago
wfWAFUserIPRange.php
7 years ago
wfWAFGeoIP2.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | require_once(dirname(__FILE__) . '/../vendor/autoload.php'); |
| 4 | |
| 5 | use GeoIp2\Database\Reader; |
| 6 | |
| 7 | if (!defined('WFWAF_RUN_COMPLETE')) { |
| 8 | class wfWAFGeoIP2 { |
| 9 | private $_reader; |
| 10 | |
| 11 | /** |
| 12 | * Returns the singleton wfWAFGeoIP2. |
| 13 | * |
| 14 | * @return wfWAFGeoIP2 |
| 15 | */ |
| 16 | public static function shared() { |
| 17 | static $_geoip = null; |
| 18 | if ($_geoip === null) { |
| 19 | $_geoip = new wfWAFGeoIP2(); |
| 20 | } |
| 21 | return $_geoip; |
| 22 | } |
| 23 | |
| 24 | public function __construct() { |
| 25 | try { |
| 26 | if (file_exists(WFWAF_LOG_PATH . '/GeoLite2-Country.mmdb')) { |
| 27 | $this->_reader = new Reader(WFWAF_LOG_PATH . '/GeoLite2-Country.mmdb'); |
| 28 | return; |
| 29 | } |
| 30 | } |
| 31 | catch (Exception $e) { |
| 32 | //Fall through to bundled copy |
| 33 | } |
| 34 | |
| 35 | $this->_reader = new Reader(__DIR__ . '/../lib/GeoLite2-Country.mmdb'); //Can throw, but we don't catch it because it means the installation is likely corrupt and needs fixed anyway |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns the database version in use. This is the timestamp of when it was packaged. |
| 40 | * |
| 41 | * @return null|int |
| 42 | */ |
| 43 | public function version() { |
| 44 | try { |
| 45 | return $this->_reader->metadata()->buildEpoch; |
| 46 | } |
| 47 | catch (Exception $e) { |
| 48 | //Fall through |
| 49 | } |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the country code for the IP if known. |
| 55 | * |
| 56 | * @param string $ip |
| 57 | * @return null|string |
| 58 | */ |
| 59 | public function countryCode($ip) { |
| 60 | try { |
| 61 | $record = $this->_reader->country($ip); |
| 62 | return $record->country->isoCode; |
| 63 | } |
| 64 | catch (Exception $e) { |
| 65 | //Fall through |
| 66 | } |
| 67 | return null; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 |