Merge pull request #719 from annando/issue-4475
[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.ini.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 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;
30
31 function geonames_install() {
32
33         Addon::registerHook('load_config', 'addon/geonames/geonames.php', 'geonames_load_config');
34
35         /**
36          *
37          * Our addon will attach in three places.
38          * The first is just prior to storing a local post.
39          *
40          */
41
42         Addon::registerHook('post_local', 'addon/geonames/geonames.php', 'geonames_post_hook');
43
44         /**
45          *
46          * Then we'll attach into the addon settings page, and also the
47          * settings post hook so that we can create and update
48          * user preferences.
49          *
50          */
51
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');
54
55         logger("installed geonames");
56 }
57
58
59 function geonames_uninstall() {
60
61         /**
62          *
63          * uninstall unregisters any hooks created with register_hook
64          * during install. It may also delete configuration settings
65          * and any other cleanup.
66          *
67          */
68
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');
73
74
75         logger("removed geonames");
76 }
77
78 function geonames_load_config(\Friendica\App $a)
79 {
80         $a->loadConfigFile(__DIR__. '/config/geonames.ini.php');
81 }
82
83 function geonames_post_hook($a, &$item) {
84
85         /**
86          *
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
91          *
92          */
93
94         logger('geonames invoked');
95
96         if(! local_user())   /* non-zero if this is a logged in user of this system */
97                 return;
98
99         if(local_user() != $item['uid'])    /* Does this person own the post? */
100                 return;
101
102         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
103                 return;
104
105         /* Retrieve our personal config setting */
106
107         $geo_account = Config::get('geonames', 'username');
108         $active = PConfig::get(local_user(), 'geonames', 'enable');
109
110         if((! $geo_account) || (! $active))
111                 return;
112
113         if((! $item['coord']) || ($item['location']))
114                 return;
115
116         $coords = explode(' ',$item['coord']);
117
118         /**
119          *
120          * OK, we're allowed to do our stuff.
121          *
122          */
123
124         $s = Network::fetchUrl('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
125
126         if(! $s)
127                 return;
128
129         $xml = XML::parseString($s);
130
131         if($xml->geoname->name && $xml->geoname->countryName)
132                 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
133
134
135 //      logger('geonames : ' . print_r($xml,true), LOGGER_DATA);
136         return;
137 }
138
139
140
141
142 /**
143  *
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.
148  *
149  */
150
151 function geonames_addon_admin_post($a,$post) {
152         if(! local_user() || (! x($_POST,'geonames-submit')))
153                 return;
154         PConfig::set(local_user(),'geonames','enable',intval($_POST['geonames']));
155
156         info(L10n::t('Geonames settings updated.') . EOL);
157 }
158
159
160 /**
161  *
162  * Called from the Addon Setting form.
163  * Add our own settings info to the page.
164  *
165  */
166
167
168
169 function geonames_addon_admin(&$a,&$s) {
170
171         if(! local_user())
172                 return;
173
174         $geo_account = Config::get('geonames', 'username');
175
176         if(! $geo_account)
177                 return;
178
179         /* Add our stylesheet to the page so we can make our settings look nice */
180
181         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/geonames/geonames.css' . '" media="all" />' . "\r\n";
182
183         /* Get the current state of our config variable */
184
185         $enabled = PConfig::get(local_user(),'geonames','enable');
186
187         $checked = (($enabled) ? ' checked="checked" ' : '');
188
189         /* Add some HTML to the existing form */
190
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>';
197
198         /* provide a submit button */
199
200         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="geonames-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
201
202 }