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>
|
|
|
|
*/
|
2020-03-04 22:07:04 +01:00
|
|
|
|
2022-06-23 07:16:22 +02:00
|
|
|
use Friendica\App;
|
2018-12-26 08:28:16 +01:00
|
|
|
use Friendica\Core\Hook;
|
2018-10-30 00:40:18 +01:00
|
|
|
use Friendica\Core\Logger;
|
2018-10-31 15:55:15 +01:00
|
|
|
use Friendica\Core\Renderer;
|
2020-01-18 22:07:06 +01:00
|
|
|
use Friendica\DI;
|
2017-11-07 00:55:24 +01:00
|
|
|
|
2017-11-09 17:08:32 +01:00
|
|
|
function geocoordinates_install()
|
|
|
|
{
|
2018-12-26 08:28:16 +01:00
|
|
|
Hook::register('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
|
|
|
|
Hook::register('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
|
2015-06-20 18:40:05 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
function geocoordinates_resolve_item(array &$item)
|
2017-11-09 17:08:32 +01:00
|
|
|
{
|
2022-06-30 13:32:13 +02:00
|
|
|
if ((!$item['coord']) || ($item['location'])) {
|
2015-06-20 18:40:05 +02:00
|
|
|
return;
|
2022-06-30 13:32:13 +02:00
|
|
|
}
|
2015-06-20 18:40:05 +02:00
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
$key = DI::config()->get('geocoordinates', 'api_key');
|
|
|
|
if ($key == '') {
|
2015-06-20 18:40:05 +02:00
|
|
|
return;
|
2022-06-30 13:32:13 +02:00
|
|
|
}
|
2015-06-20 18:40:05 +02:00
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
$language = DI::config()->get('geocoordinates', 'language');
|
|
|
|
if ($language == '') {
|
|
|
|
$language = 'de';
|
|
|
|
}
|
2015-06-20 18:40:05 +02:00
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
$coords = explode(' ', $item['coord']);
|
2015-06-21 01:58:19 +02:00
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
if (count($coords) < 2) {
|
2015-06-21 01:58:19 +02:00
|
|
|
return;
|
2022-06-30 13:32:13 +02:00
|
|
|
}
|
2015-06-21 01:58:19 +02:00
|
|
|
|
|
|
|
$coords[0] = round($coords[0], 5);
|
|
|
|
$coords[1] = round($coords[1], 5);
|
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
$result = DI::cache()->get('geocoordinates:' . $language . ':' . $coords[0] . '-' . $coords[1]);
|
2015-06-20 18:40:05 +02:00
|
|
|
if (!is_null($result)) {
|
2022-06-30 13:32:13 +02:00
|
|
|
$item['location'] = $result;
|
2015-06-20 18:40:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
$s = DI::httpClient()->fetch('https://api.opencagedata.com/geocode/v1/json?q=' . $coords[0] . ',' . $coords[1] . '&key=' . $key . '&language=' . $language);
|
2015-06-20 18:40:05 +02:00
|
|
|
|
|
|
|
if (!$s) {
|
2022-06-30 13:32:13 +02:00
|
|
|
Logger::info('API could not be queried');
|
2015-06-20 18:40:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = json_decode($s);
|
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
if ($data->status->code != '200') {
|
|
|
|
Logger::info('API returned error ' . $data->status->code . ' ' . $data->status->message);
|
2015-06-20 18:40:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 03:20:27 +02:00
|
|
|
if (($data->total_results == 0) || (count($data->results) == 0)) {
|
2022-06-30 13:32:13 +02:00
|
|
|
Logger::info('No results found for coordinates ' . $item['coord']);
|
2015-06-20 18:40:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
$item['location'] = $data->results[0]->formatted;
|
2015-06-20 18:40:05 +02:00
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
Logger::info('Got location for coordinates ' . $coords[0] . '-' . $coords[1] . ': ' . $item['location']);
|
2015-06-20 18:40:05 +02:00
|
|
|
|
2022-06-30 13:32:13 +02:00
|
|
|
if ($item['location'] != '') {
|
|
|
|
DI::cache()->set('geocoordinates:' . $language.':' . $coords[0] . '-' . $coords[1], $item['location']);
|
|
|
|
}
|
2015-06-20 18:40:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-14 03:16:09 +01:00
|
|
|
function geocoordinates_post_hook(&$item)
|
2017-11-09 17:08:32 +01:00
|
|
|
{
|
2015-06-20 18:40:05 +02:00
|
|
|
geocoordinates_resolve_item($item);
|
|
|
|
}
|
|
|
|
|
2023-01-14 03:16:09 +01:00
|
|
|
function geocoordinates_addon_admin(string &$o)
|
2017-11-09 17:08:32 +01:00
|
|
|
{
|
2022-06-30 13:32:13 +02:00
|
|
|
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/geocoordinates/');
|
2015-06-20 18:40:05 +02:00
|
|
|
|
2018-10-31 15:55:15 +01:00
|
|
|
$o = Renderer::replaceMacros($t, [
|
2020-01-18 20:52:33 +01:00
|
|
|
'$submit' => DI::l10n()->t('Save Settings'),
|
2020-01-19 21:21:12 +01:00
|
|
|
'$api_key' => ['api_key', DI::l10n()->t('API Key'), DI::config()->get('geocoordinates', 'api_key'), ''],
|
|
|
|
'$language' => ['language', DI::l10n()->t('Language code (IETF format)'), DI::config()->get('geocoordinates', 'language'), ''],
|
2018-01-15 14:15:33 +01:00
|
|
|
]);
|
2015-06-20 18:40:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-14 03:16:09 +01:00
|
|
|
function geocoordinates_addon_admin_post()
|
2017-11-09 17:08:32 +01:00
|
|
|
{
|
2022-06-30 13:32:13 +02:00
|
|
|
DI::config()->set('geocoordinates', 'api_key', trim($_POST['api_key'] ?? ''));
|
|
|
|
DI::config()->set('geocoordinates', 'language', trim($_POST['language'] ?? ''));
|
2015-06-20 18:40:05 +02:00
|
|
|
}
|