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 .htconfig.php
12 * $a->config['geonames']['username'] = 'your_username';
13 * Also visit http://geonames.org/manageaccount and enable access to the free web services
15 * When plugin is installed, the system calls the plugin
16 * name_install() function, located in 'addon/name/name.php',
17 * where 'name' is the name of the addon.
18 * If the addon is removed from the configuration list, the
19 * system will call the name_uninstall() function.
23 use Friendica\Core\Config;
24 use Friendica\Core\PConfig;
26 function geonames_install() {
30 * Our plugin will attach in three places.
31 * The first is just prior to storing a local post.
35 register_hook('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
39 * Then we'll attach into the plugin settings page, and also the
40 * settings post hook so that we can create and update
45 register_hook('plugin_settings', 'addon/geonames/geonames.php', 'geonames_plugin_admin');
46 register_hook('plugin_settings_post', 'addon/geonames/geonames.php', 'geonames_plugin_admin_post');
48 logger("installed geonames");
52 function geonames_uninstall() {
56 * uninstall unregisters any hooks created with register_hook
57 * during install. It may also delete configuration settings
58 * and any other cleanup.
62 unregister_hook('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
63 unregister_hook('plugin_settings', 'addon/geonames/geonames.php', 'geonames_plugin_admin');
64 unregister_hook('plugin_settings_post', 'addon/geonames/geonames.php', 'geonames_plugin_admin_post');
67 logger("removed geonames");
72 function geonames_post_hook($a, &$item) {
76 * An item was posted on the local system.
77 * We are going to look for specific items:
78 * - A status post by a profile owner
79 * - The profile owner must have allowed our plugin
83 logger('geonames invoked');
85 if(! local_user()) /* non-zero if this is a logged in user of this system */
88 if(local_user() != $item['uid']) /* Does this person own the post? */
91 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
94 /* Retrieve our personal config setting */
96 $geo_account = Config::get('geonames', 'username');
97 $active = PConfig::get(local_user(), 'geonames', 'enable');
99 if((! $geo_account) || (! $active))
102 if((! $item['coord']) || ($item['location']))
105 $coords = explode(' ',$item['coord']);
109 * OK, we're allowed to do our stuff.
113 $s = fetch_url('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
118 $xml = parse_xml_string($s);
120 if($xml->geoname->name && $xml->geoname->countryName)
121 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
124 // logger('geonames : ' . print_r($xml,true), LOGGER_DATA);
133 * Callback from the settings post function.
134 * $post contains the $_POST array.
135 * We will make sure we've got a valid user account
136 * and if so set our configuration setting for this person.
140 function geonames_plugin_admin_post($a,$post) {
141 if(! local_user() || (! x($_POST,'geonames-submit')))
143 PConfig::set(local_user(),'geonames','enable',intval($_POST['geonames']));
145 info( t('Geonames settings updated.') . EOL);
151 * Called from the Plugin Setting form.
152 * Add our own settings info to the page.
158 function geonames_plugin_admin(&$a,&$s) {
163 $geo_account = Config::get('geonames', 'username');
168 /* Add our stylesheet to the page so we can make our settings look nice */
170 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/geonames/geonames.css' . '" media="all" />' . "\r\n";
172 /* Get the current state of our config variable */
174 $enabled = PConfig::get(local_user(),'geonames','enable');
176 $checked = (($enabled) ? ' checked="checked" ' : '');
178 /* Add some HTML to the existing form */
180 $s .= '<div class="settings-block">';
181 $s .= '<h3>' . t('Geonames Settings') . '</h3>';
182 $s .= '<div id="geonames-enable-wrapper">';
183 $s .= '<label id="geonames-enable-label" for="geonames-checkbox">' . t('Enable Geonames Plugin') . '</label>';
184 $s .= '<input id="geonames-checkbox" type="checkbox" name="geonames" value="1" ' . $checked . '/>';
185 $s .= '</div><div class="clear"></div>';
187 /* provide a submit button */
189 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="geonames-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';