Move post/curl/fetchUrl/fetchUrlFull to own class "Network\HTTPRequest"
[friendica-addons.git/.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\Network\HTTPRequest;
15 use Friendica\Util\ConfigFileLoader;
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::log('geonames invoked');
51
52         if (!local_user()) {   /* non-zero if this is a logged in user of this system */
53                 return;
54         }
55
56         if (local_user() != $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(local_user(), '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 = HTTPRequest::fetchUrl('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 (!local_user() || empty($_POST['geonames-submit'])) {
107                 return;
108         }
109
110         DI::pConfig()->set(local_user(), 'geonames', 'enable', intval($_POST['geonames-enable']));
111
112         info(DI::l10n()->t('Geonames settings updated.'));
113 }
114
115 /**
116  * Called from the Addon Setting form.
117  * Add our own settings info to the page.
118  *
119  * @param App    $a
120  * @param string $s
121  * @throws Exception
122  */
123 function geonames_addon_settings(App $a, &$s)
124 {
125         if (!local_user()) {
126                 return;
127         }
128
129         $geo_account = DI::config()->get('geonames', 'username');
130
131         if (!$geo_account) {
132                 return;
133         }
134
135         /* Add our stylesheet to the page so we can make our settings look nice */
136         $stylesheetPath = __DIR__ . '/geonames.css';
137         DI::page()->registerStylesheet($stylesheetPath);
138
139         /* Get the current state of our config variable */
140         $enabled = intval(DI::pConfig()->get(local_user(), 'geonames', 'enable'));
141
142         $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/geonames/');
143         $s .= Renderer::replaceMacros($t, [
144                 '$title' => DI::l10n()->t('Geonames Settings'),
145                 '$description' => DI::l10n()->t('Replace numerical coordinates by the nearest populated location name in your posts.'),
146                 '$enable' => ['geonames-enable', DI::l10n()->t('Enable Geonames Addon'), $enabled],
147                 '$submit' => DI::l10n()->t('Save Settings')
148         ]);
149 }