4 * Description: Use Geonames service to resolve nearest populated location for given latitude, longitude
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
10 use Friendica\Core\Hook;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Renderer;
14 use Friendica\Util\ConfigFileLoader;
15 use Friendica\Util\XML;
17 function geonames_install()
19 Hook::register('load_config', __FILE__, 'geonames_load_config');
21 /* Our addon will attach in three places.
22 * The first is just prior to storing a local post.
25 Hook::register('post_local', __FILE__, 'geonames_post_hook');
27 /* Then we'll attach into the addon settings page, and also the
28 * settings post hook so that we can create and update
32 Hook::register('addon_settings', __FILE__, 'geonames_addon_settings');
33 Hook::register('addon_settings_post', __FILE__, 'geonames_addon_settings_post');
36 function geonames_load_config(App $a, ConfigFileLoader $loader)
38 $a->getConfigCache()->load($loader->loadAddonConfig('geonames'));
41 function geonames_post_hook(App $a, array &$item)
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
49 Logger::log('geonames invoked');
51 if (!local_user()) { /* non-zero if this is a logged in user of this system */
55 if (local_user() != $item['uid']) { /* Does this person own the post? */
59 if ($item['parent']) { /* If the item has a parent, this is a comment or something else, not a status post. */
63 /* Retrieve our personal config setting */
65 $geo_account = DI::config()->get('geonames', 'username');
66 $active = DI::pConfig()->get(local_user(), 'geonames', 'enable');
68 if (!$geo_account || !$active) {
72 if (!$item['coord'] || $item['location']) {
76 $coords = explode(' ', $item['coord']);
78 /* OK, we're allowed to do our stuff. */
80 $s = DI::httpRequest()->fetch('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
86 $xml = XML::parseString($s);
88 if ($xml->geoname->name && $xml->geoname->countryName) {
89 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
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.
101 * @param array $post The $_POST array
103 function geonames_addon_settings_post(App $a, array $post)
105 if (!local_user() || empty($_POST['geonames-submit'])) {
109 DI::pConfig()->set(local_user(), 'geonames', 'enable', intval($_POST['geonames-enable']));
113 * Called from the Addon Setting form.
114 * Add our own settings info to the page.
120 function geonames_addon_settings(App $a, &$s)
126 $geo_account = DI::config()->get('geonames', 'username');
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);
136 /* Get the current state of our config variable */
137 $enabled = intval(DI::pConfig()->get(local_user(), 'geonames', 'enable'));
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')