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