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