Skip to content

Instantly share code, notes, and snippets.

@ddur
Last active July 26, 2018 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddur/c350c864aa720fd2d57ac393acf6266c to your computer and use it in GitHub Desktop.
Save ddur/c350c864aa720fd2d57ac393acf6266c to your computer and use it in GitHub Desktop.
IP Geo API provider class for ip-geo-block
<?php
if (class_exists ('IP_Geo_Block_API', FALSE)) {
/** Class for Nginx with Http GeoIP Module
* @see http://nginx.org/en/docs/http/ngx_http_geoip_module.html
* @see https://gist.github.com/gjuric/fc418ab81a56b63a633e
*/
class IP_Geo_Block_API_NginxGeoIP extends IP_Geo_Block_API {
public function get_location ($ip, $args = array()) {
if ($ip !== $_SERVER['REMOTE_ADDR']) {
return array (
'errorMessage' => 'Service not available. Please select another provider.',
);
}
return array(
'countryCode' => isset ($_SERVER['GEOIP_COUNTRY_CODE']) ? $_SERVER['GEOIP_COUNTRY_CODE'] : 'ZZ',
'countryName' => isset ($_SERVER['GEOIP_COUNTRY_NAME']) ? $_SERVER['GEOIP_COUNTRY_NAME'] : '',
'regionName' => isset ($_SERVER['GEOIP_REGION_NAME']) ? $_SERVER['GEOIP_REGION_NAME'] : '',
'cityName' => isset ($_SERVER['GEOIP_CITY']) ? $_SERVER['GEOIP_CITY'] : '',
'latitude' => isset ($_SERVER['GEOIP_LATITUDE']) ? $_SERVER['GEOIP_LATITUDE'] : '',
'longitude' => isset ($_SERVER['GEOIP_LONGITUDE']) ? $_SERVER['GEOIP_LONGITUDE'] : '',
);
}
public function get_attribution () { return NULL; }
public function download (&$db, $args) { return FALSE; }
public function add_settings_field ($field, $section, $option_slug, $option_name, $options, $callback, $str_path, $str_last) {}
}
/** Register API */
IP_Geo_Block_Provider::register_addon (
array (
'Nginx+GeoIP' => array (
'key' => NULL,
'type' => 'IPv4, IPv6',
'link' => '<a href="http://nginx.org/en/docs/http/ngx_http_geoip_module.html" title="Nginx ngx_http_geoip_module (Maxmind)" rel=noreferrer target=_blank>Nginx ngx_http_geoip_module (Maxmind)</a>&nbsp;(IPv4, IPv6)',
),
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment