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\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Renderer;
17 use Friendica\Util\ConfigFileLoader;
18 use Friendica\Util\Network;
19 use Friendica\Util\XML;
21 function geonames_install()
23 Hook::register('load_config', __FILE__, 'geonames_load_config');
25 /* Our addon will attach in three places.
26 * The first is just prior to storing a local post.
29 Hook::register('post_local', __FILE__, 'geonames_post_hook');
31 /* Then we'll attach into the addon settings page, and also the
32 * settings post hook so that we can create and update
36 Hook::register('addon_settings', __FILE__, 'geonames_addon_settings');
37 Hook::register('addon_settings_post', __FILE__, 'geonames_addon_settings_post');
40 function geonames_load_config(App $a, ConfigFileLoader $loader)
42 $a->getConfigCache()->load($loader->loadAddonConfig('geonames'));
45 function geonames_post_hook(App $a, array &$item)
47 /* An item was posted on the local system.
48 * We are going to look for specific items:
49 * - A status post by a profile owner
50 * - The profile owner must have allowed our addon
53 Logger::log('geonames invoked');
55 if (!local_user()) { /* non-zero if this is a logged in user of this system */
59 if (local_user() != $item['uid']) { /* Does this person own the post? */
63 if ($item['parent']) { /* If the item has a parent, this is a comment or something else, not a status post. */
67 /* Retrieve our personal config setting */
69 $geo_account = Config::get('geonames', 'username');
70 $active = PConfig::get(local_user(), 'geonames', 'enable');
72 if (!$geo_account || !$active) {
76 if (!$item['coord'] || $item['location']) {
80 $coords = explode(' ', $item['coord']);
82 /* OK, we're allowed to do our stuff. */
84 $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
90 $xml = XML::parseString($s);
92 if ($xml->geoname->name && $xml->geoname->countryName) {
93 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
100 * Callback from the settings post function.
101 * We will make sure we've got a valid user account
102 * and if so set our configuration setting for this person.
105 * @param array $post The $_POST array
107 function geonames_addon_settings_post(App $a, array $post)
109 if (!local_user() || empty($_POST['geonames-submit'])) {
113 PConfig::set(local_user(), 'geonames', 'enable', intval($_POST['geonames-enable']));
115 info(L10n::t('Geonames settings updated.'));
119 * Called from the Addon Setting form.
120 * Add our own settings info to the page.
126 function geonames_addon_settings(App $a, &$s)
132 $geo_account = Config::get('geonames', 'username');
138 /* Add our stylesheet to the page so we can make our settings look nice */
139 $stylesheetPath = __DIR__ . '/geonames.css';
140 DI::page()->registerStylesheet($stylesheetPath);
142 /* Get the current state of our config variable */
143 $enabled = intval(PConfig::get(local_user(), 'geonames', 'enable'));
145 $t = Renderer::getMarkupTemplate('settings.tpl', __DIR__);
146 $s .= Renderer::replaceMacros($t, [
147 '$title' => L10n::t('Geonames Settings'),
148 '$description' => L10n::t('Replace numerical coordinates by the nearest populated location name in your posts.'),
149 '$enable' => ['geonames-enable', L10n::t('Enable Geonames Addon'), $enabled],
150 '$submit' => L10n::t('Save Settings')