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