Merge pull request #1097 from annando/acvlang
[friendica-addons.git/.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\Core\Hook;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Renderer;
12 use Friendica\DI;
13
14 function nominatim_install()
15 {
16         Hook::register('post_local', 'addon/nominatim/nominatim.php', 'nominatim_post_hook');
17         Hook::register('post_remote', 'addon/nominatim/nominatim.php', 'nominatim_post_hook');
18 }
19
20 function nominatim_resolve_item(&$item)
21 {
22         if(empty($item['coord']) || !empty($item['location'])) {
23                 return;
24         }
25
26         $language = DI::config()->get('nominatim', 'language');
27         if (empty($language)) {
28                 $language = 'en';
29         }
30
31         $coords = explode(' ',$item['coord']);
32
33         if (count($coords) < 2) {
34                 return;
35         }
36
37         $coords[0] = round($coords[0], 5);
38         $coords[1] = round($coords[1], 5);
39
40         $result = DI::cache()->get('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1]);
41         if (!is_null($result)) {
42                 $item['location'] = $result;
43                 return;
44         }
45
46         $s = DI::httpRequest()->fetch('https://nominatim.openstreetmap.org/reverse?lat=' . $coords[0] . '&lon=' . $coords[1] . '&format=json&addressdetails=0&accept-language=' . $language);
47         if (empty($s)) {
48                 Logger::info('API could not be queried');
49                 return;
50         }
51
52         $data = json_decode($s, true);
53         if (empty($data['display_name'])) {
54                 Logger::info('No results found for coordinates', ['coordinates' => $item['coord'], 'data' => $data]);
55                 return;
56         }
57
58         $item['location'] = $data['display_name'];
59
60         Logger::info('Got location', ['lat' => $coords[0], 'long' => $coords[1], 'location' => $item['location']]);
61
62         if (!empty($item['location'])) {
63                 DI::cache()->set('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1], $item['location']);
64         }
65 }
66
67 function nominatim_post_hook($a, &$item)
68 {
69         nominatim_resolve_item($item);
70 }
71
72 function nominatim_addon_admin(&$a, &$o)
73 {
74
75         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/nominatim/');
76
77         $o = Renderer::replaceMacros($t, [
78                 '$submit' => DI::l10n()->t('Save Settings'),
79                 '$language' => ['language', DI::l10n()->t('Language code (IETF format)'), DI::config()->get('nominatim', 'language'), ''],
80         ]);
81 }
82
83 function nominatim_addon_admin_post(&$a)
84 {
85         $language  = !empty($_POST['language']) ? trim($_POST['language']) : '';
86         DI::config()->set('nominatim', 'language', $language);
87 }