]> git.mxchange.org Git - friendica-addons.git/blob - geonames/geonames.php
Changes:
[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 use Friendica\App;
10 use Friendica\Core\Hook;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14 use Friendica\Core\Config\Util\ConfigFileManager;
15 use Friendica\Util\XML;
16
17 function geonames_install()
18 {
19         Hook::register('load_config', __FILE__, 'geonames_load_config');
20
21         /* Our addon will attach in three places.
22          * The first is just prior to storing a local post.
23          */
24
25         Hook::register('post_local', __FILE__, 'geonames_post_hook');
26
27         /* Then we'll attach into the addon settings page, and also the
28          * settings post hook so that we can create and update
29          * user preferences.
30          */
31
32         Hook::register('addon_settings', __FILE__, 'geonames_addon_settings');
33         Hook::register('addon_settings_post', __FILE__, 'geonames_addon_settings_post');
34 }
35
36 function geonames_load_config(ConfigFileManager $loader)
37 {
38         DI::app()->getConfigCache()->load($loader->loadAddonConfig('geonames'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
39 }
40
41 function geonames_post_hook(array &$item)
42 {
43         /* An item was posted on the local system.
44          * We are going to look for specific items:
45          *      - A status post by a profile owner
46          *      - The profile owner must have allowed our addon
47          */
48
49         Logger::notice('geonames invoked');
50
51         if (!DI::userSession()->getLocalUserId()) {   /* non-zero if this is a logged in user of this system */
52                 return;
53         }
54
55         if (DI::userSession()->getLocalUserId() != $item['uid']) {   /* Does this person own the post? */
56                 return;
57         }
58
59         if ($item['parent']) {   /* If the item has a parent, this is a comment or something else, not a status post. */
60                 return;
61         }
62
63         /* Retrieve our personal config setting */
64
65         $geo_account = DI::config()->get('geonames', 'username');
66         $active = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'geonames', 'enable');
67
68         if (!$geo_account || !$active) {
69                 return;
70         }
71
72         if (!$item['coord'] || $item['location']) {
73                 return;
74         }
75
76         $coords = explode(' ', $item['coord']);
77
78         /* OK, we're allowed to do our stuff. */
79
80         $s = DI::httpClient()->fetch('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username=' . $geo_account);
81
82         if (!$s) {
83                 return;
84         }
85
86         $xml = XML::parseString($s);
87
88         if ($xml->geoname->name && $xml->geoname->countryName) {
89                 $item['location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
90         }
91
92         return;
93 }
94
95 /**
96  * Callback from the settings post function.
97  * We will make sure we've got a valid user account
98  * and if so set our configuration setting for this person.
99  *
100  * @param array $post The $_POST array
101  */
102 function geonames_addon_settings_post(array $post)
103 {
104         if (!DI::userSession()->getLocalUserId() || empty($_POST['geonames-submit'])) {
105                 return;
106         }
107
108         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'geonames', 'enable', intval($_POST['geonames-enable']));
109 }
110
111 /**
112  * Called from the Addon Setting form.
113  * Add our own settings info to the page.
114  *
115  * @param array $data
116  * @throws Exception
117  */
118 function geonames_addon_settings(array &$data)
119 {
120         if (!DI::userSession()->getLocalUserId()) {
121                 return;
122         }
123
124         $geo_account = DI::config()->get('geonames', 'username');
125         if (!$geo_account) {
126                 return;
127         }
128
129         $enabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'geonames', 'enable'));
130
131         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/geonames/');
132         $html = Renderer::replaceMacros($t, [
133                 '$info'   => DI::l10n()->t('Replace numerical coordinates by the nearest populated location name in your posts.'),
134                 '$enable' => ['geonames-enable', DI::l10n()->t('Enable Geonames Addon'), $enabled],
135         ]);
136
137         $data = [
138                 'addon' => 'geonames',
139                 'title' => DI::l10n()->t('Geonames Settings'),
140                 'html'  => $html,
141         ];
142 }