4 * Description: Use Geonames service to resolve nearest populated location for given latitude, longitude
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
9 * Pre-requisite: Register a username at geonames.org
10 * and set in config/addon.config.php
13 * username = your_username
15 * Also visit http://geonames.org/manageaccount and enable access to the free web services
17 * When addon is installed, the system calls the addon
18 * name_install() function, located in 'addon/name/name.php',
19 * where 'name' is the name of the addon.
20 * If the addon is removed from the configuration list, the
21 * system will call the name_uninstall() function.
25 use Friendica\Core\Config;
26 use Friendica\Core\Hook;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Logger;
29 use Friendica\Core\PConfig;
30 use Friendica\Util\Config\ConfigFileLoader;
31 use Friendica\Util\Network;
32 use Friendica\Util\XML;
34 function geonames_install() {
36 Hook::register('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
40 * Our addon will attach in three places.
41 * The first is just prior to storing a local post.
45 Hook::register('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
49 * Then we'll attach into the addon settings page, and also the
50 * settings post hook so that we can create and update
55 Hook::register('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
56 Hook::register('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
58 Logger::log("installed geonames");
62 function geonames_uninstall() {
66 * uninstall unregisters any hooks created with register_hook
67 * during install. It may also delete configuration settings
68 * and any other cleanup.
72 Hook::unregister('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
73 Hook::unregister('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
74 Hook::unregister('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
75 Hook::unregister('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
78 Logger::log("removed geonames");
81 function geonames_load_config(\Friendica\App $a, ConfigFileLoader $loader)
83 $a->getConfigCache()->load($loader->loadAddonConfig('geonames'));
86 function geonames_post_hook($a, &$item) {
90 * An item was posted on the local system.
91 * We are going to look for specific items:
92 * - A status post by a profile owner
93 * - The profile owner must have allowed our addon
97 Logger::log('geonames invoked');
99 if(! local_user()) /* non-zero if this is a logged in user of this system */
102 if(local_user() != $item['uid']) /* Does this person own the post? */
105 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
108 /* Retrieve our personal config setting */
110 $geo_account = Config::get('geonames', 'username');
111 $active = PConfig::get(local_user(), 'geonames', 'enable');
113 if((! $geo_account) || (! $active))
116 if((! $item['coord']) || ($item['location']))
119 $coords = explode(' ',$item['coord']);
123 * OK, we're allowed to do our stuff.
127 $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
132 $xml = XML::parseString($s);
134 if($xml->geoname->name && $xml->geoname->countryName)
135 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
138 // Logger::log('geonames : ' . print_r($xml,true), Logger::DATA);
147 * Callback from the settings post function.
148 * $post contains the $_POST array.
149 * We will make sure we've got a valid user account
150 * and if so set our configuration setting for this person.
154 function geonames_addon_admin_post($a,$post) {
155 if(! local_user() || empty($_POST['geonames-submit']))
157 PConfig::set(local_user(),'geonames','enable',intval($_POST['geonames']));
159 info(L10n::t('Geonames settings updated.') . EOL);
165 * Called from the Addon Setting form.
166 * Add our own settings info to the page.
172 function geonames_addon_admin(&$a,&$s) {
177 $geo_account = Config::get('geonames', 'username');
182 /* Add our stylesheet to the page so we can make our settings look nice */
184 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/geonames/geonames.css' . '" media="all" />' . "\r\n";
186 /* Get the current state of our config variable */
188 $enabled = PConfig::get(local_user(),'geonames','enable');
190 $checked = (($enabled) ? ' checked="checked" ' : '');
192 /* Add some HTML to the existing form */
194 $s .= '<div class="settings-block">';
195 $s .= '<h3>' . L10n::t('Geonames Settings') . '</h3>';
196 $s .= '<div id="geonames-enable-wrapper">';
197 $s .= '<label id="geonames-enable-label" for="geonames-checkbox">' . L10n::t('Enable Geonames Addon') . '</label>';
198 $s .= '<input id="geonames-checkbox" type="checkbox" name="geonames" value="1" ' . $checked . '/>';
199 $s .= '</div><div class="clear"></div>';
201 /* provide a submit button */
203 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="geonames-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';