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.
24 use Friendica\Core\Config;
25 use Friendica\Core\Hook;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Logger;
28 use Friendica\Core\PConfig;
29 use Friendica\Util\Network;
30 use Friendica\Util\XML;
32 function geonames_install() {
34 Hook::register('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
38 * Our addon will attach in three places.
39 * The first is just prior to storing a local post.
43 Hook::register('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
47 * Then we'll attach into the addon settings page, and also the
48 * settings post hook so that we can create and update
53 Hook::register('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
54 Hook::register('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
56 Logger::log("installed geonames");
60 function geonames_uninstall() {
64 * uninstall unregisters any hooks created with register_hook
65 * during install. It may also delete configuration settings
66 * and any other cleanup.
70 Hook::unregister('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
71 Hook::unregister('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
72 Hook::unregister('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
73 Hook::unregister('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
76 Logger::log("removed geonames");
79 function geonames_load_config(\Friendica\App $a)
81 $a->loadConfigFile(__DIR__. '/config/geonames.config.php');
84 function geonames_post_hook($a, &$item) {
88 * An item was posted on the local system.
89 * We are going to look for specific items:
90 * - A status post by a profile owner
91 * - The profile owner must have allowed our addon
95 Logger::log('geonames invoked');
97 if(! local_user()) /* non-zero if this is a logged in user of this system */
100 if(local_user() != $item['uid']) /* Does this person own the post? */
103 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
106 /* Retrieve our personal config setting */
108 $geo_account = Config::get('geonames', 'username');
109 $active = PConfig::get(local_user(), 'geonames', 'enable');
111 if((! $geo_account) || (! $active))
114 if((! $item['coord']) || ($item['location']))
117 $coords = explode(' ',$item['coord']);
121 * OK, we're allowed to do our stuff.
125 $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
130 $xml = XML::parseString($s);
132 if($xml->geoname->name && $xml->geoname->countryName)
133 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
136 // Logger::log('geonames : ' . print_r($xml,true), Logger::DATA);
145 * Callback from the settings post function.
146 * $post contains the $_POST array.
147 * We will make sure we've got a valid user account
148 * and if so set our configuration setting for this person.
152 function geonames_addon_admin_post($a,$post) {
153 if(! local_user() || empty($_POST['geonames-submit']))
155 PConfig::set(local_user(),'geonames','enable',intval($_POST['geonames']));
157 info(L10n::t('Geonames settings updated.') . EOL);
163 * Called from the Addon Setting form.
164 * Add our own settings info to the page.
170 function geonames_addon_admin(&$a,&$s) {
175 $geo_account = Config::get('geonames', 'username');
180 /* Add our stylesheet to the page so we can make our settings look nice */
182 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/geonames/geonames.css' . '" media="all" />' . "\r\n";
184 /* Get the current state of our config variable */
186 $enabled = PConfig::get(local_user(),'geonames','enable');
188 $checked = (($enabled) ? ' checked="checked" ' : '');
190 /* Add some HTML to the existing form */
192 $s .= '<div class="settings-block">';
193 $s .= '<h3>' . L10n::t('Geonames Settings') . '</h3>';
194 $s .= '<div id="geonames-enable-wrapper">';
195 $s .= '<label id="geonames-enable-label" for="geonames-checkbox">' . L10n::t('Enable Geonames Addon') . '</label>';
196 $s .= '<input id="geonames-checkbox" type="checkbox" name="geonames" value="1" ' . $checked . '/>';
197 $s .= '</div><div class="clear"></div>';
199 /* provide a submit button */
201 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="geonames-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';