2015-06-20 18:40:05 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: Geocoordinates
|
|
|
|
* Description: Use the OpenCage Geocoder http://geocoder.opencagedata.com to resolve nearest populated location for given latitude, longitude. Derived from "geonames"
|
|
|
|
* Version: 0.1
|
|
|
|
* Author: Michael Vogel <https://pirati.ca/profile/heluecht>
|
|
|
|
*/
|
2018-01-17 01:51:12 +01:00
|
|
|
use Friendica\Core\Addon;
|
2017-11-09 17:08:32 +01:00
|
|
|
use Friendica\Core\Cache;
|
2017-11-07 00:55:24 +01:00
|
|
|
use Friendica\Core\Config;
|
|
|
|
|
2017-11-09 17:08:32 +01:00
|
|
|
function geocoordinates_install()
|
|
|
|
{
|
2018-01-17 01:51:12 +01:00
|
|
|
Addon::registerHook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
|
|
|
|
Addon::registerHook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
|
2015-06-20 18:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-09 17:08:32 +01:00
|
|
|
function geocoordinates_uninstall()
|
|
|
|
{
|
2018-01-17 01:51:12 +01:00
|
|
|
Addon::unregisterHook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
|
|
|
|
Addon::unregisterHook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
|
2015-06-20 18:40:05 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 17:08:32 +01:00
|
|
|
function geocoordinates_resolve_item(&$item)
|
|
|
|
{
|
2015-06-20 18:40:05 +02:00
|
|
|
if((!$item["coord"]) || ($item["location"]))
|
|
|
|
return;
|
|
|
|
|
2017-11-07 00:55:24 +01:00
|
|
|
$key = Config::get("geocoordinates", "api_key");
|
2015-06-20 18:40:05 +02:00
|
|
|
if ($key == "")
|
|
|
|
return;
|
|
|
|
|
2017-11-07 00:55:24 +01:00
|
|
|
$language = Config::get("geocoordinates", "language");
|
2015-06-20 18:40:05 +02:00
|
|
|
if ($language == "")
|
|
|
|
$language = "de";
|
|
|
|
|
2015-06-21 01:58:19 +02:00
|
|
|
$coords = explode(' ',$item["coord"]);
|
|
|
|
|
|
|
|
if (count($coords) < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
$coords[0] = round($coords[0], 5);
|
|
|
|
$coords[1] = round($coords[1], 5);
|
|
|
|
|
|
|
|
$result = Cache::get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]);
|
2015-06-20 18:40:05 +02:00
|
|
|
if (!is_null($result)) {
|
|
|
|
$item["location"] = $result;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$s = fetch_url("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language);
|
|
|
|
|
|
|
|
if (!$s) {
|
|
|
|
logger("API could not be queried", LOGGER_DEBUG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = json_decode($s);
|
|
|
|
|
|
|
|
if ($data->status->code != "200") {
|
|
|
|
logger("API returned error ".$data->status->code." ".$data->status->message, LOGGER_DEBUG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 03:20:27 +02:00
|
|
|
if (($data->total_results == 0) || (count($data->results) == 0)) {
|
2015-06-20 22:29:01 +02:00
|
|
|
logger("No results found for coordinates ".$item["coord"], LOGGER_DEBUG);
|
2015-06-20 18:40:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item["location"] = $data->results[0]->formatted;
|
|
|
|
|
2015-06-21 01:58:19 +02:00
|
|
|
logger("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], LOGGER_DEBUG);
|
2015-06-20 18:40:05 +02:00
|
|
|
|
|
|
|
if ($item["location"] != "")
|
2015-06-21 01:58:19 +02:00
|
|
|
Cache::set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
|
2015-06-20 18:40:05 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 17:08:32 +01:00
|
|
|
function geocoordinates_post_hook($a, &$item)
|
|
|
|
{
|
2015-06-20 18:40:05 +02:00
|
|
|
geocoordinates_resolve_item($item);
|
|
|
|
}
|
|
|
|
|
2017-11-09 17:08:32 +01:00
|
|
|
function geocoordinates_plugin_admin(&$a, &$o)
|
|
|
|
{
|
2015-06-20 18:40:05 +02:00
|
|
|
|
|
|
|
$t = get_markup_template("admin.tpl", "addon/geocoordinates/");
|
|
|
|
|
2018-01-15 14:15:33 +01:00
|
|
|
$o = replace_macros($t, [
|
2015-06-20 18:40:05 +02:00
|
|
|
'$submit' => t('Save Settings'),
|
2018-01-15 14:15:33 +01:00
|
|
|
'$api_key' => ['api_key', t('API Key'), Config::get('geocoordinates', 'api_key' ), ''],
|
|
|
|
'$language' => ['language', t('Language code (IETF format)'), Config::get('geocoordinates', 'language' ), ''],
|
|
|
|
]);
|
2015-06-20 18:40:05 +02:00
|
|
|
}
|
|
|
|
|
2017-11-09 17:08:32 +01:00
|
|
|
function geocoordinates_plugin_admin_post(&$a)
|
|
|
|
{
|
2015-06-20 18:40:05 +02:00
|
|
|
$api_key = ((x($_POST,'api_key')) ? notags(trim($_POST['api_key'])) : '');
|
2017-11-07 00:55:24 +01:00
|
|
|
Config::set('geocoordinates','api_key',$api_key);
|
2015-06-20 18:40:05 +02:00
|
|
|
|
|
|
|
$language = ((x($_POST,'language')) ? notags(trim($_POST['language'])) : '');
|
2017-11-07 00:55:24 +01:00
|
|
|
Config::set('geocoordinates','language',$language);
|
2015-06-20 18:40:05 +02:00
|
|
|
info(t('Settings updated.'). EOL);
|
|
|
|
}
|