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