From 85f45f37ce7375dfdd061221f84adb6231290d79 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 6 Jan 2014 20:26:41 -0500 Subject: [PATCH] ipinfodb info --- ip2locationlite.class.php | 59 +++++++++++++++++++++++++++++++++++++++ options.php | 2 +- post-form.php | 9 ++++++ simple-location.php | 36 +++++++++++++++++++++++- 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 ip2locationlite.class.php create mode 100644 post-form.php diff --git a/ip2locationlite.class.php b/ip2locationlite.class.php new file mode 100644 index 0000000..7ea2f2f --- /dev/null +++ b/ip2locationlite.class.php @@ -0,0 +1,59 @@ +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; + } +} +?> diff --git a/options.php b/options.php index ad6b491..c5cf5d5 100644 --- a/options.php +++ b/options.php @@ -8,7 +8,7 @@ settings_fields( 'simple-location-options' ); ?> -Google API Key: +IPInfoDB API Key: diff --git a/post-form.php b/post-form.php new file mode 100644 index 0000000..af3b7c5 --- /dev/null +++ b/post-form.php @@ -0,0 +1,9 @@ +ID, '_simple_location_city', true); +$country = get_post_meta($post->ID, '_simple_location_country', true); +?> +

If left blank, location detection will be automatically attempted from your IP with Google.

+City: , County: + +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() { include( plugin_dir_path(__FILE__) . 'options.php'); } function reg_settings() { - register_setting( 'simple-location-options', 'gapi-key' ); + register_setting( 'simple-location-options', 'simple-location-api-key' ); } +