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.ini.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\Addon;
25 use Friendica\Core\Config;
26 use Friendica\Core\L10n;
27 use Friendica\Core\PConfig;
28 use Friendica\Util\Network;
29 use Friendica\Util\XML;
31 function geonames_install() {
33 Addon::registerHook('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
37 * Our addon will attach in three places.
38 * The first is just prior to storing a local post.
42 Addon::registerHook('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
46 * Then we'll attach into the addon settings page, and also the
47 * settings post hook so that we can create and update
52 Addon::registerHook('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
53 Addon::registerHook('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
55 logger("installed geonames");
59 function geonames_uninstall() {
63 * uninstall unregisters any hooks created with register_hook
64 * during install. It may also delete configuration settings
65 * and any other cleanup.
69 Addon::unregisterHook('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
70 Addon::unregisterHook('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
71 Addon::unregisterHook('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
72 Addon::unregisterHook('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
75 logger("removed geonames");
78 function geonames_load_config(\Friendica\App $a)
80 $a->loadConfigFile(__DIR__. '/config/geonames.ini.php');
83 function geonames_post_hook($a, &$item) {
87 * An item was posted on the local system.
88 * We are going to look for specific items:
89 * - A status post by a profile owner
90 * - The profile owner must have allowed our addon
94 logger('geonames invoked');
96 if(! local_user()) /* non-zero if this is a logged in user of this system */
99 if(local_user() != $item['uid']) /* Does this person own the post? */
102 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
105 /* Retrieve our personal config setting */
107 $geo_account = Config::get('geonames', 'username');
108 $active = PConfig::get(local_user(), 'geonames', 'enable');
110 if((! $geo_account) || (! $active))
113 if((! $item['coord']) || ($item['location']))
116 $coords = explode(' ',$item['coord']);
120 * OK, we're allowed to do our stuff.
124 $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
129 $xml = XML::parseString($s);
131 if($xml->geoname->name && $xml->geoname->countryName)
132 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
135 // logger('geonames : ' . print_r($xml,true), LOGGER_DATA);
144 * Callback from the settings post function.
145 * $post contains the $_POST array.
146 * We will make sure we've got a valid user account
147 * and if so set our configuration setting for this person.
151 function geonames_addon_admin_post($a,$post) {
152 if(! local_user() || (! x($_POST,'geonames-submit')))
154 PConfig::set(local_user(),'geonames','enable',intval($_POST['geonames']));
156 info(L10n::t('Geonames settings updated.') . EOL);
162 * Called from the Addon Setting form.
163 * Add our own settings info to the page.
169 function geonames_addon_admin(&$a,&$s) {
174 $geo_account = Config::get('geonames', 'username');
179 /* Add our stylesheet to the page so we can make our settings look nice */
181 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/geonames/geonames.css' . '" media="all" />' . "\r\n";
183 /* Get the current state of our config variable */
185 $enabled = PConfig::get(local_user(),'geonames','enable');
187 $checked = (($enabled) ? ' checked="checked" ' : '');
189 /* Add some HTML to the existing form */
191 $s .= '<div class="settings-block">';
192 $s .= '<h3>' . L10n::t('Geonames Settings') . '</h3>';
193 $s .= '<div id="geonames-enable-wrapper">';
194 $s .= '<label id="geonames-enable-label" for="geonames-checkbox">' . L10n::t('Enable Geonames Addon') . '</label>';
195 $s .= '<input id="geonames-checkbox" type="checkbox" name="geonames" value="1" ' . $checked . '/>';
196 $s .= '</div><div class="clear"></div>';
198 /* provide a submit button */
200 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="geonames-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';