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\Core\Config\Util\ConfigFileManager;
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(ConfigFileManager $loader)
38 DI::app()->getConfigCache()->load($loader->loadAddonConfig('geonames'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
41 function geonames_post_hook(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::notice('geonames invoked');
51 if (!DI::userSession()->getLocalUserId()) { /* non-zero if this is a logged in user of this system */
55 if (DI::userSession()->getLocalUserId() != $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(DI::userSession()->getLocalUserId(), '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::httpClient()->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.
100 * @param array $post The $_POST array
102 function geonames_addon_settings_post(array $post)
104 if (!DI::userSession()->getLocalUserId() || empty($_POST['geonames-submit'])) {
108 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'geonames', 'enable', intval($_POST['geonames-enable']));
112 * Called from the Addon Setting form.
113 * Add our own settings info to the page.
118 function geonames_addon_settings(array &$data)
120 if (!DI::userSession()->getLocalUserId()) {
124 $geo_account = DI::config()->get('geonames', 'username');
129 $enabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'geonames', 'enable'));
131 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/geonames/');
132 $html = Renderer::replaceMacros($t, [
133 '$info' => DI::l10n()->t('Replace numerical coordinates by the nearest populated location name in your posts.'),
134 '$enable' => ['geonames-enable', DI::l10n()->t('Enable Geonames Addon'), $enabled],
138 'addon' => 'geonames',
139 'title' => DI::l10n()->t('Geonames Settings'),