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