2015-06-20 12:40:05 -04: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-16 19:51:12 -05:00
|
|
|
use Friendica\Core\Addon;
|
2017-11-09 11:08:32 -05:00
|
|
|
use Friendica\Core\Cache;
|
2017-11-06 18:55:24 -05:00
|
|
|
use Friendica\Core\Config;
|
2018-01-22 14:03:11 -05:00
|
|
|
use Friendica\Core\L10n;
|
2018-10-29 19:40:18 -04:00
|
|
|
use Friendica\Core\Logger;
|
2018-10-31 10:55:15 -04:00
|
|
|
use Friendica\Core\Renderer;
|
2018-01-27 08:52:02 -05:00
|
|
|
use Friendica\Util\Network;
|
2018-11-08 11:45:19 -05:00
|
|
|
use Friendica\Util\Strings;
|
2017-11-06 18:55:24 -05:00
|
|
|
|
2017-11-09 11:08:32 -05:00
|
|
|
function geocoordinates_install()
|
|
|
|
{
|
2018-01-16 19:51:12 -05: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 12:40:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-09 11:08:32 -05:00
|
|
|
function geocoordinates_uninstall()
|
|
|
|
{
|
2018-01-16 19:51:12 -05: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 12:40:05 -04:00
|
|
|
}
|
|
|
|
|
2017-11-09 11:08:32 -05:00
|
|
|
function geocoordinates_resolve_item(&$item)
|
|
|
|
{
|
2015-06-20 12:40:05 -04:00
|
|
|
if((!$item["coord"]) || ($item["location"]))
|
|
|
|
return;
|
|
|
|
|
2017-11-06 18:55:24 -05:00
|
|
|
$key = Config::get("geocoordinates", "api_key");
|
2015-06-20 12:40:05 -04:00
|
|
|
if ($key == "")
|
|
|
|
return;
|
|
|
|
|
2017-11-06 18:55:24 -05:00
|
|
|
$language = Config::get("geocoordinates", "language");
|
2015-06-20 12:40:05 -04:00
|
|
|
if ($language == "")
|
|
|
|
$language = "de";
|
|
|
|
|
2015-06-20 19:58:19 -04: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 12:40:05 -04:00
|
|
|
if (!is_null($result)) {
|
|
|
|
$item["location"] = $result;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-27 11:23:04 -05:00
|
|
|
$s = Network::fetchUrl("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language);
|
2015-06-20 12:40:05 -04:00
|
|
|
|
|
|
|
if (!$s) {
|
2018-10-30 09:48:09 -04:00
|
|
|
Logger::log("API could not be queried", Logger::DEBUG);
|
2015-06-20 12:40:05 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = json_decode($s);
|
|
|
|
|
|
|
|
if ($data->status->code != "200") {
|
2018-10-30 09:48:09 -04:00
|
|
|
Logger::log("API returned error ".$data->status->code." ".$data->status->message, Logger::DEBUG);
|
2015-06-20 12:40:05 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:20:27 -04:00
|
|
|
if (($data->total_results == 0) || (count($data->results) == 0)) {
|
2018-10-30 09:48:09 -04:00
|
|
|
Logger::log("No results found for coordinates ".$item["coord"], Logger::DEBUG);
|
2015-06-20 12:40:05 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item["location"] = $data->results[0]->formatted;
|
|
|
|
|
2018-10-30 09:48:09 -04:00
|
|
|
Logger::log("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], Logger::DEBUG);
|
2015-06-20 12:40:05 -04:00
|
|
|
|
|
|
|
if ($item["location"] != "")
|
2015-06-20 19:58:19 -04:00
|
|
|
Cache::set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
|
2015-06-20 12:40:05 -04:00
|
|
|
}
|
|
|
|
|
2017-11-09 11:08:32 -05:00
|
|
|
function geocoordinates_post_hook($a, &$item)
|
|
|
|
{
|
2015-06-20 12:40:05 -04:00
|
|
|
geocoordinates_resolve_item($item);
|
|
|
|
}
|
|
|
|
|
2018-01-20 08:57:41 -05:00
|
|
|
function geocoordinates_addon_admin(&$a, &$o)
|
2017-11-09 11:08:32 -05:00
|
|
|
{
|
2015-06-20 12:40:05 -04:00
|
|
|
|
2018-10-31 10:55:15 -04:00
|
|
|
$t = Renderer::getMarkupTemplate("admin.tpl", "addon/geocoordinates/");
|
2015-06-20 12:40:05 -04:00
|
|
|
|
2018-10-31 10:55:15 -04:00
|
|
|
$o = Renderer::replaceMacros($t, [
|
2018-01-22 14:03:11 -05:00
|
|
|
'$submit' => L10n::t('Save Settings'),
|
|
|
|
'$api_key' => ['api_key', L10n::t('API Key'), Config::get('geocoordinates', 'api_key'), ''],
|
|
|
|
'$language' => ['language', L10n::t('Language code (IETF format)'), Config::get('geocoordinates', 'language'), ''],
|
2018-01-15 08:15:33 -05:00
|
|
|
]);
|
2015-06-20 12:40:05 -04:00
|
|
|
}
|
|
|
|
|
2018-01-20 08:57:41 -05:00
|
|
|
function geocoordinates_addon_admin_post(&$a)
|
2017-11-09 11:08:32 -05:00
|
|
|
{
|
2018-11-30 09:11:56 -05:00
|
|
|
$api_key = (!empty($_POST['api_key']) ? Strings::escapeTags(trim($_POST['api_key'])) : '');
|
2018-01-20 08:57:41 -05:00
|
|
|
Config::set('geocoordinates', 'api_key', $api_key);
|
2015-06-20 12:40:05 -04:00
|
|
|
|
2018-11-30 09:11:56 -05:00
|
|
|
$language = (!empty($_POST['language']) ? Strings::escapeTags(trim($_POST['language'])) : '');
|
2018-01-20 08:57:41 -05:00
|
|
|
Config::set('geocoordinates', 'language', $language);
|
2018-01-22 14:03:11 -05:00
|
|
|
info(L10n::t('Settings updated.'). EOL);
|
2015-06-20 12:40:05 -04:00
|
|
|
}
|