Merge branch '3.6-rc'
[friendica-addons.git] / geocoordinates / geocoordinates.php
1 <?php
2 /**
3  * Name: Geocoordinates
4  * Description: Use the OpenCage Geocoder http://geocoder.opencagedata.com to resolve nearest populated location for given latitude, longitude. Derived from "geonames"
5  * Version: 0.1
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  */
8 use Friendica\Core\Addon;
9 use Friendica\Core\Cache;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Util\Network;
13
14 function geocoordinates_install()
15 {
16         Addon::registerHook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
17         Addon::registerHook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
18 }
19
20
21 function geocoordinates_uninstall()
22 {
23         Addon::unregisterHook('post_local',    'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
24         Addon::unregisterHook('post_remote',    'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
25 }
26
27 function geocoordinates_resolve_item(&$item) {
28         if((!$item["coord"]) || ($item["location"]))
29                 return;
30
31         $key = get_config("geocoordinates", "api_key");
32         if ($key == "")
33                 return;
34
35         $language = get_config("geocoordinates", "language");
36         if ($language == "")
37                 $language = "de";
38
39         $coords = explode(' ',$item["coord"]);
40
41         if (count($coords) < 2)
42                 return;
43
44         $coords[0] = round($coords[0], 5);
45         $coords[1] = round($coords[1], 5);
46
47         $result = Cache::get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]);
48         if (!is_null($result)) {
49                 $item["location"] = $result;
50                 return;
51         }
52
53         $s = Network::fetchUrl("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language);
54
55         if (!$s) {
56                 logger("API could not be queried", LOGGER_DEBUG);
57                 return;
58         }
59
60         $data = json_decode($s);
61
62         if ($data->status->code != "200") {
63                 logger("API returned error ".$data->status->code." ".$data->status->message, LOGGER_DEBUG);
64                 return;
65         }
66
67         if (($data->total_results == 0) || (count($data->results) == 0)) {
68                 logger("No results found for coordinates ".$item["coord"], LOGGER_DEBUG);
69                 return;
70         }
71
72         $item["location"] = $data->results[0]->formatted;
73
74         logger("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], LOGGER_DEBUG);
75
76         if ($item["location"] != "")
77                 Cache::set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
78 }
79
80 function geocoordinates_post_hook($a, &$item) {
81         geocoordinates_resolve_item($item);
82 }
83
84 function geocoordinates_addon_admin(&$a, &$o)
85 {
86
87         $t = get_markup_template("admin.tpl", "addon/geocoordinates/");
88
89         $o = replace_macros($t, [
90                 '$submit' => L10n::t('Save Settings'),
91                 '$api_key' => ['api_key', L10n::t('API Key'), Config::get('geocoordinates', 'api_key'), ''],
92                 '$language' => ['language', L10n::t('Language code (IETF format)'), Config::get('geocoordinates', 'language'), ''],
93         ]);
94 }
95
96 function geocoordinates_addon_admin_post(&$a)
97 {
98         $api_key  = ((x($_POST, 'api_key')) ? notags(trim($_POST['api_key']))   : '');
99         Config::set('geocoordinates', 'api_key', $api_key);
100
101         $language  = ((x($_POST, 'language')) ? notags(trim($_POST['language']))   : '');
102         Config::set('geocoordinates', 'language', $language);
103         info(L10n::t('Settings updated.'). EOL);
104 }