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>
8 use Friendica\Core\Addon;
9 use Friendica\Core\Cache;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\Util\Network;
15 use Friendica\Util\Strings;
17 function geocoordinates_install()
19 Addon::registerHook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
20 Addon::registerHook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
24 function geocoordinates_uninstall()
26 Addon::unregisterHook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
27 Addon::unregisterHook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
30 function geocoordinates_resolve_item(&$item)
32 if((!$item["coord"]) || ($item["location"]))
35 $key = Config::get("geocoordinates", "api_key");
39 $language = Config::get("geocoordinates", "language");
43 $coords = explode(' ',$item["coord"]);
45 if (count($coords) < 2)
48 $coords[0] = round($coords[0], 5);
49 $coords[1] = round($coords[1], 5);
51 $result = Cache::get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]);
52 if (!is_null($result)) {
53 $item["location"] = $result;
57 $s = Network::fetchUrl("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language);
60 Logger::log("API could not be queried", Logger::DEBUG);
64 $data = json_decode($s);
66 if ($data->status->code != "200") {
67 Logger::log("API returned error ".$data->status->code." ".$data->status->message, Logger::DEBUG);
71 if (($data->total_results == 0) || (count($data->results) == 0)) {
72 Logger::log("No results found for coordinates ".$item["coord"], Logger::DEBUG);
76 $item["location"] = $data->results[0]->formatted;
78 Logger::log("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], Logger::DEBUG);
80 if ($item["location"] != "")
81 Cache::set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
84 function geocoordinates_post_hook($a, &$item)
86 geocoordinates_resolve_item($item);
89 function geocoordinates_addon_admin(&$a, &$o)
92 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/geocoordinates/");
94 $o = Renderer::replaceMacros($t, [
95 '$submit' => L10n::t('Save Settings'),
96 '$api_key' => ['api_key', L10n::t('API Key'), Config::get('geocoordinates', 'api_key'), ''],
97 '$language' => ['language', L10n::t('Language code (IETF format)'), Config::get('geocoordinates', 'language'), ''],
101 function geocoordinates_addon_admin_post(&$a)
103 $api_key = ((x($_POST, 'api_key')) ? Strings::escapeTags(trim($_POST['api_key'])) : '');
104 Config::set('geocoordinates', 'api_key', $api_key);
106 $language = ((x($_POST, 'language')) ? Strings::escapeTags(trim($_POST['language'])) : '');
107 Config::set('geocoordinates', 'language', $language);
108 info(L10n::t('Settings updated.'). EOL);