]> git.mxchange.org Git - friendica-addons.git/blob - geonames/geonames.php
819d9eca11952d4b20739d071afc5aecbc97aaaf
[friendica-addons.git] / geonames / geonames.php
1 <?php
2 /**
3  * Name: Geonames
4  * Description: Use Geonames service to resolve nearest populated location for given latitude, longitude
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  */
8
9 use Friendica\App;
10 use Friendica\Core\Hook;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14 use Friendica\Core\Config\Util\ConfigFileLoader;
15 use Friendica\Core\Session;
16 use Friendica\Util\XML;
17
18 function geonames_install()
19 {
20         Hook::register('load_config', __FILE__, 'geonames_load_config');
21
22         /* Our addon will attach in three places.
23          * The first is just prior to storing a local post.
24          */
25
26         Hook::register('post_local', __FILE__, 'geonames_post_hook');
27
28         /* Then we'll attach into the addon settings page, and also the
29          * settings post hook so that we can create and update
30          * user preferences.
31          */
32
33         Hook::register('addon_settings', __FILE__, 'geonames_addon_settings');
34         Hook::register('addon_settings_post', __FILE__, 'geonames_addon_settings_post');
35 }
36
37 function geonames_load_config(App $a, ConfigFileLoader $loader)
38 {
39         $a->getConfigCache()->load($loader->loadAddonConfig('geonames'));
40 }
41
42 function geonames_post_hook(App $a, array &$item)
43 {
44         /* An item was posted on the local system.
45          * We are going to look for specific items:
46          *      - A status post by a profile owner
47          *      - The profile owner must have allowed our addon
48          */
49
50         Logger::notice('geonames invoked');
51
52         if (!Session::getLocalUser()) {   /* non-zero if this is a logged in user of this system */
53                 return;
54         }
55
56         if (Session::getLocalUser() != $item['uid']) {   /* Does this person own the post? */
57                 return;
58         }
59
60         if ($item['parent']) {   /* If the item has a parent, this is a comment or something else, not a status post. */
61                 return;
62         }
63
64         /* Retrieve our personal config setting */
65
66         $geo_account = DI::config()->get('geonames', 'username');
67         $active = DI::pConfig()->get(Session::getLocalUser(), 'geonames', 'enable');
68
69         if (!$geo_account || !$active) {
70                 return;
71         }
72
73         if (!$item['coord'] || $item['location']) {
74                 return;
75         }
76
77         $coords = explode(' ', $item['coord']);
78
79         /* OK, we're allowed to do our stuff. */
80
81         $s = DI::httpClient()->fetch('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
82
83         if (!$s) {
84                 return;
85         }
86
87         $xml = XML::parseString($s);
88
89         if ($xml->geoname->name && $xml->geoname->countryName) {
90                 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
91         }
92
93         return;
94 }
95
96 /**
97  * Callback from the settings post function.
98  * We will make sure we've got a valid user account
99  * and if so set our configuration setting for this person.
100  *
101  * @param App   $a
102  * @param array $post The $_POST array
103  */
104 function geonames_addon_settings_post(App $a, array $post)
105 {
106         if (!Session::getLocalUser() || empty($_POST['geonames-submit'])) {
107                 return;
108         }
109
110         DI::pConfig()->set(Session::getLocalUser(), 'geonames', 'enable', intval($_POST['geonames-enable']));
111 }
112
113 /**
114  * Called from the Addon Setting form.
115  * Add our own settings info to the page.
116  *
117  * @param App   $a
118  * @param array $data
119  * @throws Exception
120  */
121 function geonames_addon_settings(App $a, array &$data)
122 {
123         if (!Session::getLocalUser()) {
124                 return;
125         }
126
127         $geo_account = DI::config()->get('geonames', 'username');
128         if (!$geo_account) {
129                 return;
130         }
131
132         $enabled = intval(DI::pConfig()->get(Session::getLocalUser(), 'geonames', 'enable'));
133
134         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/geonames/');
135         $html = Renderer::replaceMacros($t, [
136                 '$info'   => DI::l10n()->t('Replace numerical coordinates by the nearest populated location name in your posts.'),
137                 '$enable' => ['geonames-enable', DI::l10n()->t('Enable Geonames Addon'), $enabled],
138         ]);
139
140         $data = [
141                 'addon' => 'geonames',
142                 'title' => DI::l10n()->t('Geonames Settings'),
143                 'html'  => $html,
144         ];
145 }