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