move to Renderer class
[friendica-addons.git/.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\Core\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\Util\Network;
15
16 function geocoordinates_install()
17 {
18         Addon::registerHook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
19         Addon::registerHook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
20 }
21
22
23 function geocoordinates_uninstall()
24 {
25         Addon::unregisterHook('post_local',    'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
26         Addon::unregisterHook('post_remote',    'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
27 }
28
29 function geocoordinates_resolve_item(&$item)
30 {
31         if((!$item["coord"]) || ($item["location"]))
32                 return;
33
34         $key = Config::get("geocoordinates", "api_key");
35         if ($key == "")
36                 return;
37
38         $language = Config::get("geocoordinates", "language");
39         if ($language == "")
40                 $language = "de";
41
42         $coords = explode(' ',$item["coord"]);
43
44         if (count($coords) < 2)
45                 return;
46
47         $coords[0] = round($coords[0], 5);
48         $coords[1] = round($coords[1], 5);
49
50         $result = Cache::get("geocoordinates:".$language.":".$coords[0]."-".$coords[1]);
51         if (!is_null($result)) {
52                 $item["location"] = $result;
53                 return;
54         }
55
56         $s = Network::fetchUrl("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language);
57
58         if (!$s) {
59                 Logger::log("API could not be queried", Logger::DEBUG);
60                 return;
61         }
62
63         $data = json_decode($s);
64
65         if ($data->status->code != "200") {
66                 Logger::log("API returned error ".$data->status->code." ".$data->status->message, Logger::DEBUG);
67                 return;
68         }
69
70         if (($data->total_results == 0) || (count($data->results) == 0)) {
71                 Logger::log("No results found for coordinates ".$item["coord"], Logger::DEBUG);
72                 return;
73         }
74
75         $item["location"] = $data->results[0]->formatted;
76
77         Logger::log("Got location for coordinates ".$coords[0]."-".$coords[1].": ".$item["location"], Logger::DEBUG);
78
79         if ($item["location"] != "")
80                 Cache::set("geocoordinates:".$language.":".$coords[0]."-".$coords[1], $item["location"]);
81 }
82
83 function geocoordinates_post_hook($a, &$item)
84 {
85         geocoordinates_resolve_item($item);
86 }
87
88 function geocoordinates_addon_admin(&$a, &$o)
89 {
90
91         $t = Renderer::getMarkupTemplate("admin.tpl", "addon/geocoordinates/");
92
93         $o = Renderer::replaceMacros($t, [
94                 '$submit' => L10n::t('Save Settings'),
95                 '$api_key' => ['api_key', L10n::t('API Key'), Config::get('geocoordinates', 'api_key'), ''],
96                 '$language' => ['language', L10n::t('Language code (IETF format)'), Config::get('geocoordinates', 'language'), ''],
97         ]);
98 }
99
100 function geocoordinates_addon_admin_post(&$a)
101 {
102         $api_key  = ((x($_POST, 'api_key')) ? notags(trim($_POST['api_key']))   : '');
103         Config::set('geocoordinates', 'api_key', $api_key);
104
105         $language  = ((x($_POST, 'language')) ? notags(trim($_POST['language']))   : '');
106         Config::set('geocoordinates', 'language', $language);
107         info(L10n::t('Settings updated.'). EOL);
108 }