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\Config;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
15 use Friendica\Util\ConfigFileLoader;
16 use Friendica\Util\Network;
17 use Friendica\Util\XML;
19 function geonames_install()
21 Hook::register('load_config', __FILE__, 'geonames_load_config');
23 /* Our addon will attach in three places.
24 * The first is just prior to storing a local post.
27 Hook::register('post_local', __FILE__, 'geonames_post_hook');
29 /* Then we'll attach into the addon settings page, and also the
30 * settings post hook so that we can create and update
34 Hook::register('addon_settings', __FILE__, 'geonames_addon_settings');
35 Hook::register('addon_settings_post', __FILE__, 'geonames_addon_settings_post');
38 function geonames_load_config(App $a, ConfigFileLoader $loader)
40 $a->getConfigCache()->load($loader->loadAddonConfig('geonames'));
43 function geonames_post_hook(App $a, array &$item)
45 /* An item was posted on the local system.
46 * We are going to look for specific items:
47 * - A status post by a profile owner
48 * - The profile owner must have allowed our addon
51 Logger::log('geonames invoked');
53 if (!local_user()) { /* non-zero if this is a logged in user of this system */
57 if (local_user() != $item['uid']) { /* Does this person own the post? */
61 if ($item['parent']) { /* If the item has a parent, this is a comment or something else, not a status post. */
65 /* Retrieve our personal config setting */
67 $geo_account = Config::get('geonames', 'username');
68 $active = DI::pConfig()->get(local_user(), 'geonames', 'enable');
70 if (!$geo_account || !$active) {
74 if (!$item['coord'] || $item['location']) {
78 $coords = explode(' ', $item['coord']);
80 /* OK, we're allowed to do our stuff. */
82 $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
88 $xml = XML::parseString($s);
90 if ($xml->geoname->name && $xml->geoname->countryName) {
91 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
98 * Callback from the settings post function.
99 * We will make sure we've got a valid user account
100 * and if so set our configuration setting for this person.
103 * @param array $post The $_POST array
105 function geonames_addon_settings_post(App $a, array $post)
107 if (!local_user() || empty($_POST['geonames-submit'])) {
111 DI::pConfig()->set(local_user(), 'geonames', 'enable', intval($_POST['geonames-enable']));
113 info(DI::l10n()->t('Geonames settings updated.'));
117 * Called from the Addon Setting form.
118 * Add our own settings info to the page.
124 function geonames_addon_settings(App $a, &$s)
130 $geo_account = Config::get('geonames', 'username');
136 /* Add our stylesheet to the page so we can make our settings look nice */
137 $stylesheetPath = __DIR__ . '/geonames.css';
138 DI::page()->registerStylesheet($stylesheetPath);
140 /* Get the current state of our config variable */
141 $enabled = intval(DI::pConfig()->get(local_user(), 'geonames', 'enable'));
143 $t = Renderer::getMarkupTemplate('settings.tpl', __DIR__);
144 $s .= Renderer::replaceMacros($t, [
145 '$title' => DI::l10n()->t('Geonames Settings'),
146 '$description' => DI::l10n()->t('Replace numerical coordinates by the nearest populated location name in your posts.'),
147 '$enable' => ['geonames-enable', DI::l10n()->t('Enable Geonames Addon'), $enabled],
148 '$submit' => DI::l10n()->t('Save Settings')