1ce6dbfa547d0e5d1148cf4138edf5835e1e3331
[friendica-addons.git/.git] / geonames / geonames.php
1 <?php
2 /**
3  * Name: Geonames
4  * Description: Use Geonames service to resolve nearest populated location for given latitude, longitude
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  *
8  *
9  * Pre-requisite: Register a username at geonames.org
10  * and set in config/addon.config.php
11  *
12  * [geonames]
13  * username = your_username
14  *
15  * Also visit http://geonames.org/manageaccount and enable access to the free web services
16  *
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.
22  *
23  */
24
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;
33
34 function geonames_install() {
35
36         Hook::register('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
37
38         /**
39          *
40          * Our addon will attach in three places.
41          * The first is just prior to storing a local post.
42          *
43          */
44
45         Hook::register('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
46
47         /**
48          *
49          * Then we'll attach into the addon settings page, and also the
50          * settings post hook so that we can create and update
51          * user preferences.
52          *
53          */
54
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');
57
58         Logger::log("installed geonames");
59 }
60
61
62 function geonames_uninstall() {
63
64         /**
65          *
66          * uninstall unregisters any hooks created with register_hook
67          * during install. It may also delete configuration settings
68          * and any other cleanup.
69          *
70          */
71
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');
76
77
78         Logger::log("removed geonames");
79 }
80
81 function geonames_load_config(\Friendica\App $a, ConfigFileLoader $loader)
82 {
83         $a->getConfigCache()->load($loader->loadAddonConfig('geonames'));
84 }
85
86 function geonames_post_hook($a, &$item) {
87
88         /**
89          *
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
94          *
95          */
96
97         Logger::log('geonames invoked');
98
99         if(! local_user())   /* non-zero if this is a logged in user of this system */
100                 return;
101
102         if(local_user() != $item['uid'])    /* Does this person own the post? */
103                 return;
104
105         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
106                 return;
107
108         /* Retrieve our personal config setting */
109
110         $geo_account = Config::get('geonames', 'username');
111         $active = PConfig::get(local_user(), 'geonames', 'enable');
112
113         if((! $geo_account) || (! $active))
114                 return;
115
116         if((! $item['coord']) || ($item['location']))
117                 return;
118
119         $coords = explode(' ',$item['coord']);
120
121         /**
122          *
123          * OK, we're allowed to do our stuff.
124          *
125          */
126
127         $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
128
129         if(! $s)
130                 return;
131
132         $xml = XML::parseString($s);
133
134         if($xml->geoname->name && $xml->geoname->countryName)
135                 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
136
137
138 //      Logger::log('geonames : ' . print_r($xml,true), Logger::DATA);
139         return;
140 }
141
142
143
144
145 /**
146  *
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.
151  *
152  */
153
154 function geonames_addon_admin_post($a,$post) {
155         if(! local_user() || empty($_POST['geonames-submit']))
156                 return;
157         PConfig::set(local_user(),'geonames','enable',intval($_POST['geonames']));
158
159         info(L10n::t('Geonames settings updated.') . EOL);
160 }
161
162
163 /**
164  *
165  * Called from the Addon Setting form.
166  * Add our own settings info to the page.
167  *
168  */
169
170
171
172 function geonames_addon_admin(&$a,&$s) {
173
174         if(! local_user())
175                 return;
176
177         $geo_account = Config::get('geonames', 'username');
178
179         if(! $geo_account)
180                 return;
181
182         /* Add our stylesheet to the page so we can make our settings look nice */
183
184         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/geonames/geonames.css' . '" media="all" />' . "\r\n";
185
186         /* Get the current state of our config variable */
187
188         $enabled = PConfig::get(local_user(),'geonames','enable');
189
190         $checked = (($enabled) ? ' checked="checked" ' : '');
191
192         /* Add some HTML to the existing form */
193
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>';
200
201         /* provide a submit button */
202
203         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="geonames-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
204
205 }