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;
16 use Friendica\Util\Config\ConfigFileLoader;
17 use Friendica\Util\Network;
18 use Friendica\Util\XML;
20 function geonames_install()
22 Hook::register('load_config', __FILE__, 'geonames_load_config');
24 /* Our addon will attach in three places.
25 * The first is just prior to storing a local post.
28 Hook::register('post_local', __FILE__, 'geonames_post_hook');
30 /* Then we'll attach into the addon settings page, and also the
31 * settings post hook so that we can create and update
35 Hook::register('addon_settings', __FILE__, 'geonames_addon_settings');
36 Hook::register('addon_settings_post', __FILE__, 'geonames_addon_settings_post');
39 function geonames_load_config(App $a, ConfigFileLoader $loader)
41 $a->getConfigCache()->load($loader->loadAddonConfig('geonames'));
44 function geonames_post_hook(App $a, array &$item)
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
52 Logger::log('geonames invoked');
54 if (!local_user()) { /* non-zero if this is a logged in user of this system */
58 if (local_user() != $item['uid']) { /* Does this person own the post? */
62 if ($item['parent']) { /* If the item has a parent, this is a comment or something else, not a status post. */
66 /* Retrieve our personal config setting */
68 $geo_account = Config::get('geonames', 'username');
69 $active = PConfig::get(local_user(), 'geonames', 'enable');
71 if (!$geo_account || !$active) {
75 if (!$item['coord'] || $item['location']) {
79 $coords = explode(' ', $item['coord']);
81 /* OK, we're allowed to do our stuff. */
83 $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
89 $xml = XML::parseString($s);
91 if ($xml->geoname->name && $xml->geoname->countryName) {
92 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
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.
104 * @param array $post The $_POST array
106 function geonames_addon_settings_post(App $a, array $post)
108 if (!local_user() || empty($_POST['geonames-submit'])) {
112 PConfig::set(local_user(), 'geonames', 'enable', intval($_POST['geonames-enable']));
114 info(L10n::t('Geonames settings updated.'));
118 * Called from the Addon Setting form.
119 * Add our own settings info to the page.
125 function geonames_addon_settings(App $a, &$s)
131 $geo_account = Config::get('geonames', 'username');
137 /* Add our stylesheet to the page so we can make our settings look nice */
138 $stylesheetPath = __DIR__ . '/geonames.css';
139 $a->registerStylesheet($stylesheetPath);
141 /* Get the current state of our config variable */
142 $enabled = intval(PConfig::get(local_user(), 'geonames', 'enable'));
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')