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\Network;
31 use Friendica\Util\XML;
33 function geonames_install() {
35 Hook::register('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
39 * Our addon will attach in three places.
40 * The first is just prior to storing a local post.
44 Hook::register('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
48 * Then we'll attach into the addon settings page, and also the
49 * settings post hook so that we can create and update
54 Hook::register('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
55 Hook::register('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
57 Logger::log("installed geonames");
61 function geonames_uninstall() {
65 * uninstall unregisters any hooks created with register_hook
66 * during install. It may also delete configuration settings
67 * and any other cleanup.
71 Hook::unregister('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
72 Hook::unregister('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
73 Hook::unregister('addon_settings', 'addon/geonames/geonames.php', 'geonames_addon_admin');
74 Hook::unregister('addon_settings_post', 'addon/geonames/geonames.php', 'geonames_addon_admin_post');
77 Logger::log("removed geonames");
80 function geonames_load_config(\Friendica\App $a, Config\ConfigCacheLoader $loader)
82 $a->getConfig()->loadConfigArray($loader->loadAddonConfig('geonames'));
85 function geonames_post_hook($a, &$item) {
89 * An item was posted on the local system.
90 * We are going to look for specific items:
91 * - A status post by a profile owner
92 * - The profile owner must have allowed our addon
96 Logger::log('geonames invoked');
98 if(! local_user()) /* non-zero if this is a logged in user of this system */
101 if(local_user() != $item['uid']) /* Does this person own the post? */
104 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
107 /* Retrieve our personal config setting */
109 $geo_account = Config::get('geonames', 'username');
110 $active = PConfig::get(local_user(), 'geonames', 'enable');
112 if((! $geo_account) || (! $active))
115 if((! $item['coord']) || ($item['location']))
118 $coords = explode(' ',$item['coord']);
122 * OK, we're allowed to do our stuff.
126 $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
131 $xml = XML::parseString($s);
133 if($xml->geoname->name && $xml->geoname->countryName)
134 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
137 // Logger::log('geonames : ' . print_r($xml,true), Logger::DATA);
146 * Callback from the settings post function.
147 * $post contains the $_POST array.
148 * We will make sure we've got a valid user account
149 * and if so set our configuration setting for this person.
153 function geonames_addon_admin_post($a,$post) {
154 if(! local_user() || empty($_POST['geonames-submit']))
156 PConfig::set(local_user(),'geonames','enable',intval($_POST['geonames']));
158 info(L10n::t('Geonames settings updated.') . EOL);
164 * Called from the Addon Setting form.
165 * Add our own settings info to the page.
171 function geonames_addon_admin(&$a,&$s) {
176 $geo_account = Config::get('geonames', 'username');
181 /* Add our stylesheet to the page so we can make our settings look nice */
183 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/geonames/geonames.css' . '" media="all" />' . "\r\n";
185 /* Get the current state of our config variable */
187 $enabled = PConfig::get(local_user(),'geonames','enable');
189 $checked = (($enabled) ? ' checked="checked" ' : '');
191 /* Add some HTML to the existing form */
193 $s .= '<div class="settings-block">';
194 $s .= '<h3>' . L10n::t('Geonames Settings') . '</h3>';
195 $s .= '<div id="geonames-enable-wrapper">';
196 $s .= '<label id="geonames-enable-label" for="geonames-checkbox">' . L10n::t('Enable Geonames Addon') . '</label>';
197 $s .= '<input id="geonames-checkbox" type="checkbox" name="geonames" value="1" ' . $checked . '/>';
198 $s .= '</div><div class="clear"></div>';
200 /* provide a submit button */
202 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="geonames-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';