]> git.mxchange.org Git - friendica-addons.git/blob - nominatim/nominatim.php
audon/C/messages.po gelöscht
[friendica-addons.git] / nominatim / nominatim.php
1 <?php
2 /**
3  * Name: Nominatim
4  * Description: Use Nominatim from OpenStreetMap to resolve the location for the given latitude and longitude. Derived from "geocoordinates"
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 nominatim_install()
16 {
17         Hook::register('post_local', 'addon/nominatim/nominatim.php', 'nominatim_post_hook');
18         Hook::register('post_remote', 'addon/nominatim/nominatim.php', 'nominatim_post_hook');
19 }
20
21 function nominatim_resolve_item(array &$item)
22 {
23         if(empty($item['coord']) || !empty($item['location'])) {
24                 return;
25         }
26
27         $language = DI::config()->get('nominatim', 'language');
28         if (empty($language)) {
29                 $language = 'en';
30         }
31
32         $coords = explode(' ',$item['coord']);
33
34         if (count($coords) < 2) {
35                 return;
36         }
37
38         $coords[0] = round($coords[0], 5);
39         $coords[1] = round($coords[1], 5);
40
41         $result = DI::cache()->get('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1]);
42         if (!is_null($result)) {
43                 $item['location'] = $result;
44                 return;
45         }
46
47         $s = DI::httpClient()->fetch('https://nominatim.openstreetmap.org/reverse?lat=' . $coords[0] . '&lon=' . $coords[1] . '&format=json&addressdetails=0&accept-language=' . $language);
48         if (empty($s)) {
49                 Logger::info('API could not be queried');
50                 return;
51         }
52
53         $data = json_decode($s, true);
54         if (empty($data['display_name'])) {
55                 Logger::info('No results found for coordinates', ['coordinates' => $item['coord'], 'data' => $data]);
56                 return;
57         }
58
59         $item['location'] = $data['display_name'];
60
61         Logger::info('Got location', ['lat' => $coords[0], 'long' => $coords[1], 'location' => $item['location']]);
62
63         if (!empty($item['location'])) {
64                 DI::cache()->set('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1], $item['location']);
65         }
66 }
67
68 function nominatim_post_hook(array &$item)
69 {
70         nominatim_resolve_item($item);
71 }
72
73 function nominatim_addon_admin(string &$o)
74 {
75
76         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/nominatim/');
77
78         $o = Renderer::replaceMacros($t, [
79                 '$submit' => DI::l10n()->t('Save Settings'),
80                 '$language' => ['language', DI::l10n()->t('Language code (IETF format)'), DI::config()->get('nominatim', 'language'), ''],
81         ]);
82 }
83
84 function nominatim_addon_admin_post()
85 {
86         DI::config()->set('nominatim', 'language', (!empty($_POST['language']) ? trim($_POST['language']) : ''));
87 }