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