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