4 * Description: Use the OpenCage Geocoder http://geocoder.opencagedata.com to resolve nearest populated location for given latitude, longitude. Derived from "geonames"
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
9 use Friendica\Core\Hook;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Renderer;
13 use Friendica\Network\HTTPRequest;
14 use Friendica\Util\Strings;
16 function geocoordinates_install()
18 Hook::register('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
19 Hook::register('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
23 function geocoordinates_uninstall()
25 Hook::unregister('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
26 Hook::unregister('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
29 function geocoordinates_resolve_item(&$item)
31 if((!$item["coord"]) || ($item["location"]))
34 $key = DI::config()->get("geocoordinates", "api_key");
38 $language = DI::config()->get("geocoordinates", "language");
42 $coords = explode(' ',$item["coord"]);
44 if (count($coords) < 2)
47 $coords[0] = round($coords[0], 5);
48 $coords[1] = round($coords[1], 5);
50 $result = DI::cache()->get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]);
51 if (!is_null($result)) {
52 $item["location"] = $result;
56 $s = HTTPRequest::fetchUrl("https://api.opencagedata.com/geocode/v1/json?q=" . $coords[0] . "," . $coords[1] . "&key=" . $key . "&language=" . $language);
59 Logger::log("API could not be queried", Logger::DEBUG);
63 $data = json_decode($s);
65 if ($data->status->code != "200") {
66 Logger::log("API returned error ".$data->status->code." ".$data->status->message, Logger::DEBUG);
70 if (($data->total_results == 0) || (count($data->results) == 0)) {
71 Logger::log("No results found for coordinates ".$item["coord"], Logger::DEBUG);
75 $item["location"] = $data->results[0]->formatted;
77 Logger::log("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], Logger::DEBUG);
79 if ($item["location"] != "")
80 DI::cache()->set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
83 function geocoordinates_post_hook($a, &$item)
85 geocoordinates_resolve_item($item);
88 function geocoordinates_addon_admin(&$a, &$o)
91 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/geocoordinates/");
93 $o = Renderer::replaceMacros($t, [
94 '$submit' => DI::l10n()->t('Save Settings'),
95 '$api_key' => ['api_key', DI::l10n()->t('API Key'), DI::config()->get('geocoordinates', 'api_key'), ''],
96 '$language' => ['language', DI::l10n()->t('Language code (IETF format)'), DI::config()->get('geocoordinates', 'language'), ''],
100 function geocoordinates_addon_admin_post(&$a)
102 $api_key = (!empty($_POST['api_key']) ? Strings::escapeTags(trim($_POST['api_key'])) : '');
103 DI::config()->set('geocoordinates', 'api_key', $api_key);
105 $language = (!empty($_POST['language']) ? Strings::escapeTags(trim($_POST['language'])) : '');
106 DI::config()->set('geocoordinates', 'language', $language);
107 info(DI::l10n()->t('Settings updated.') . EOL);