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