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\Util\Network;
15 function geocoordinates_install()
17 Addon::registerHook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
18 Addon::registerHook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
22 function geocoordinates_uninstall()
24 Addon::unregisterHook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
25 Addon::unregisterHook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
28 function geocoordinates_resolve_item(&$item)
30 if((!$item["coord"]) || ($item["location"]))
33 $key = Config::get("geocoordinates", "api_key");
37 $language = Config::get("geocoordinates", "language");
41 $coords = explode(' ',$item["coord"]);
43 if (count($coords) < 2)
46 $coords[0] = round($coords[0], 5);
47 $coords[1] = round($coords[1], 5);
49 $result = Cache::get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]);
50 if (!is_null($result)) {
51 $item["location"] = $result;
55 $s = Network::fetchUrl("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language);
58 Logger::log("API could not be queried", Logger::DEBUG);
62 $data = json_decode($s);
64 if ($data->status->code != "200") {
65 Logger::log("API returned error ".$data->status->code." ".$data->status->message, Logger::DEBUG);
69 if (($data->total_results == 0) || (count($data->results) == 0)) {
70 Logger::log("No results found for coordinates ".$item["coord"], Logger::DEBUG);
74 $item["location"] = $data->results[0]->formatted;
76 Logger::log("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], Logger::DEBUG);
78 if ($item["location"] != "")
79 Cache::set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
82 function geocoordinates_post_hook($a, &$item)
84 geocoordinates_resolve_item($item);
87 function geocoordinates_addon_admin(&$a, &$o)
90 $t = get_markup_template("admin.tpl", "addon/geocoordinates/");
92 $o = replace_macros($t, [
93 '$submit' => L10n::t('Save Settings'),
94 '$api_key' => ['api_key', L10n::t('API Key'), Config::get('geocoordinates', 'api_key'), ''],
95 '$language' => ['language', L10n::t('Language code (IETF format)'), Config::get('geocoordinates', 'language'), ''],
99 function geocoordinates_addon_admin_post(&$a)
101 $api_key = ((x($_POST, 'api_key')) ? notags(trim($_POST['api_key'])) : '');
102 Config::set('geocoordinates', 'api_key', $api_key);
104 $language = ((x($_POST, 'language')) ? notags(trim($_POST['language'])) : '');
105 Config::set('geocoordinates', 'language', $language);
106 info(L10n::t('Settings updated.'). EOL);