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