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