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