ipinfodb info

This commit is contained in:
Dan Ballard 2014-01-06 20:26:41 -05:00
parent b2f65fa12d
commit 85f45f37ce
4 changed files with 104 additions and 2 deletions

59
ip2locationlite.class.php Normal file
View File

@ -0,0 +1,59 @@
<?php
final class ip2location_lite{
protected $errors = array();
protected $service = 'api.ipinfodb.com';
protected $version = 'v3';
protected $apiKey = '';
public function __construct(){}
public function __destruct(){}
public function setKey($key){
if(!empty($key)) $this->apiKey = $key;
}
public function getError(){
return implode("\n", $this->errors);
}
public function getCountry($host){
return $this->getResult($host, 'ip-country');
}
public function getCity($host){
return $this->getResult($host, 'ip-city');
}
private function getResult($host, $name){
$ip = @gethostbyname($host);
// if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){
if(filter_var($ip, FILTER_VALIDATE_IP)){
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');
if (get_magic_quotes_runtime()){
$xml = stripslashes($xml);
}
try{
$response = @new SimpleXMLElement($xml);
foreach($response as $field=>$value){
$result[(string)$field] = (string)$value;
}
return $result;
}
catch(Exception $e){
$this->errors[] = $e->getMessage();
return;
}
}
$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
return;
}
}
?>

View File

@ -8,7 +8,7 @@
settings_fields( 'simple-location-options' ); settings_fields( 'simple-location-options' );
?> ?>
Google API Key: <input id="gapi-key" name="gapi-key" type="text" value="<?php print get_option('gapi-key'); ?>" /> <a href="http://ipinfodb.com">IPInfoDB</a> API Key: <input id="simple-location-api-key" name="simple-location-api-key" type="text" value="<?php print get_option('simple-location-api-key'); ?>" size="70" />
<?php submit_button(); ?> <?php submit_button(); ?>

9
post-form.php Normal file
View File

@ -0,0 +1,9 @@
<?php
global $post;
$city = get_post_meta($post->ID, '_simple_location_city', true);
$country = get_post_meta($post->ID, '_simple_location_country', true);
?>
<p>If left blank, location detection will be automatically attempted from your IP with Google.</p>
City: <input name="city" type="text" value="<?php print $city;?>" />, County: <input name="country" type="text" value="<?php print $country;?>" />
<?php

View File

@ -14,16 +14,50 @@ Tested up to: 3.8
if ( is_admin() ) { if ( is_admin() ) {
add_action( 'admin_menu', 'add_options_menu' ); add_action( 'admin_menu', 'add_options_menu' );
add_action( 'admin_init', 'reg_settings' ); add_action( 'admin_init', 'reg_settings' );
add_action( 'admin_menu', 'add_meta_to_edit' );
add_action( 'edit_attachment', 'simple_location_post_save' );
add_action( 'save_post', 'simple_location_post_save' );
} }
function add_options_menu() { function add_options_menu() {
add_options_page('Simple Location Options', 'Simple Location', 'manage_options', 'simple-location', 'options_page'); add_options_page('Simple Location Options', 'Simple Location', 'manage_options', 'simple-location', 'options_page');
} }
function add_meta_to_edit() {
add_meta_box('simple-location-meta', 'Simple Location', 'simple_location_meta', 'post', 'normal');
}
function simple_location_post_save($post_id) {
if ($_POST['city'] == '' || $_POST['country'] == '') {
$key = get_option('simple-location-api-key');
if ($key != '') {
include('ip2locationlite.class.php');
$ipLite = new ip2location_lite;
$ipLite->setKey($key);
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
print 'locations: ';
print_r($locations);
}
if ($_POST['city'] == '') {
$_POST['city'] = $locations['cityName'];
}
update_post_meta($post_id, '_simple_location_city', sanitize_text_field($_POST['city']));
if ($_POST['country'] == '') {
$_POST['country'] = $locations['countryName'];
}
update_post_meta($post_id, '_simple_location_country', sanitize_text_field($_POST['country']));
}
function simple_location_meta() {
include( plugin_dir_path(__FILE__) . 'post-form.php');
}
function options_page() { function options_page() {
include( plugin_dir_path(__FILE__) . 'options.php'); include( plugin_dir_path(__FILE__) . 'options.php');
} }
function reg_settings() { function reg_settings() {
register_setting( 'simple-location-options', 'gapi-key' ); register_setting( 'simple-location-options', 'simple-location-api-key' );
} }