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