| 1 |
<?php |
| 2 |
/** |
| 3 |
* Author: Hoang Ngo |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Defender\Extra; |
| 7 |
|
| 8 |
use MaxMind\Db\Reader; |
| 9 |
|
| 10 |
class GeoIp { |
| 11 |
/** |
| 12 |
* @var \GeoIp2\Database\Reader |
| 13 |
*/ |
| 14 |
protected $provider; |
| 15 |
|
| 16 |
public function __construct( $dbPath, $type = 'maxmind' ) { |
| 17 |
$this->provider = new Reader( $dbPath ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* @param $ip |
| 22 |
* |
| 23 |
* @return array|bool |
| 24 |
* @throws Reader\InvalidDatabaseException |
| 25 |
*/ |
| 26 |
public function ip_to_country( $ip ) { |
| 27 |
$info = $this->provider->get( $ip ); |
| 28 |
if ( empty( $info['country'] ) ) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
$country = array( |
| 33 |
'iso' => $info['country']['iso_code'], |
| 34 |
'name' => $info['country']['names']['en'] |
| 35 |
); |
| 36 |
|
| 37 |
return $country; |
| 38 |
} |
| 39 |
} |
| 40 |
|