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