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