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