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