Merge pull request #718 from tobiasd/20180825-nl
[friendica-addons.git/.git] / curweather / curweather.php
1 <?php
2 /**
3  * Name: Current Weather
4  * Description: Shows current weather conditions for user's location on their network page.
5  * Version: 1.1
6  * Author: Tony Baldwin <http://friendica.tonybaldwin.info/u/t0ny>
7  * Author: Fabio Comuni <http://kirkgroup.com/u/fabrixxm>
8  * Author: Tobias Diekershoff <https://f.diekershoff.de/u/tobias>
9  *
10  */
11
12 require_once 'include/text.php';
13
14 use Friendica\App;
15 use Friendica\Core\Addon;
16 use Friendica\Core\Cache;
17 use Friendica\Core\Config;
18 use Friendica\Core\L10n;
19 use Friendica\Core\PConfig;
20 use Friendica\Util\Network;
21 use Friendica\Util\Proxy as ProxyUtils;
22
23 function curweather_install()
24 {
25         Addon::registerHook('network_mod_init'   , 'addon/curweather/curweather.php', 'curweather_network_mod_init');
26         Addon::registerHook('addon_settings'     , 'addon/curweather/curweather.php', 'curweather_addon_settings');
27         Addon::registerHook('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
28 }
29
30 function curweather_uninstall()
31 {
32         Addon::unregisterHook('network_mod_init'   , 'addon/curweather/curweather.php', 'curweather_network_mod_init');
33         Addon::unregisterHook('addon_settings'     , 'addon/curweather/curweather.php', 'curweather_addon_settings');
34         Addon::unregisterHook('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
35 }
36
37 //  get the weather data from OpenWeatherMap
38 function getWeather($loc, $units = 'metric', $lang = 'en', $appid = '', $cachetime = 0)
39 {
40         $url = "http://api.openweathermap.org/data/2.5/weather?q=" . $loc . "&appid=" . $appid . "&lang=" . $lang . "&units=" . $units . "&mode=xml";
41         $cached = Cache::get('curweather'.md5($url));
42         $now = new DateTime();
43
44         if (!is_null($cached)) {
45                 $cdate = PConfig::get(local_user(), 'curweather', 'last');
46                 $cached = unserialize($cached);
47
48                 if ($cdate + $cachetime > $now->getTimestamp()) {
49                         return $cached;
50                 }
51         }
52
53         try {
54                 $res = new SimpleXMLElement(Network::fetchUrl($url));
55         } catch (Exception $e) {
56                 if (empty($_SESSION['curweather_notice_shown'])) {
57                         info(L10n::t('Error fetching weather data. Error was: '.$e->getMessage()));
58                         $_SESSION['curweather_notice_shown'] = true;
59                 }
60
61                 return false;
62         }
63
64         unset($_SESSION['curweather_notice_shown']);
65
66         if ((string) $res->temperature['unit'] === 'metric') {
67                 $tunit = '°C';
68                 $wunit = 'm/s';
69         } else {
70                 $tunit = '°F';
71                 $wunit = 'mph';
72         }
73
74         if (trim((string) $res->weather['value']) == trim((string) $res->clouds['name'])) {
75                 $desc = (string) $res->clouds['name'];
76         } else {
77                 $desc = (string) $res->weather['value'] . ', ' . (string) $res->clouds['name'];
78         }
79
80         $r = [
81                 'city'        => (string) $res->city['name'][0],
82                 'country'     => (string) $res->city->country[0],
83                 'lat'         => (string) $res->city->coord['lat'],
84                 'lon'         => (string) $res->city->coord['lon'],
85                 'temperature' => (string) $res->temperature['value'][0].$tunit,
86                 'pressure'    => (string) $res->pressure['value'] . (string) $res->pressure['unit'],
87                 'humidity'    => (string) $res->humidity['value'] . (string) $res->humidity['unit'],
88                 'descripion'  => $desc,
89                 'wind'        => (string) $res->wind->speed['name'] . ' (' . (string) $res->wind->speed['value'] . $wunit . ')',
90                 'update'      => (string) $res->lastupdate['value'],
91                 'icon'        => (string) $res->weather['icon'],
92         ];
93
94         PConfig::set(local_user(), 'curweather', 'last', $now->getTimestamp());
95         Cache::set('curweather'.md5($url), serialize($r), CACHE_HOUR);
96
97         return $r;
98 }
99
100 function curweather_network_mod_init(App $a, &$b)
101 {
102         if (!intval(PConfig::get(local_user(), 'curweather', 'curweather_enable'))) {
103                 return;
104         }
105
106         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
107
108         // $rpt value is needed for location
109         // $lang will be taken from the browser session to honour user settings
110         // TODO $lang does not work if the default settings are used
111         //      and not all response strings are translated
112         // $units can be set in the settings by the user
113         // $appid is configured by the admin in the admin panel
114         // those parameters will be used to get: cloud status, temperature, preassure
115         // and relative humidity for display, also the relevent area of the map is
116         // linked from lat/log of the reply of OWMp
117         $rpt = PConfig::get(local_user(), 'curweather', 'curweather_loc');
118
119         // Set the language to the browsers language or default and use metric units
120         $lang = (!empty($_SESSION['language']) ? $_SESSION['language'] : Config::get('system', 'language'));
121         $units = PConfig::get( local_user(), 'curweather', 'curweather_units');
122         $appid = Config::get('curweather', 'appid');
123         $cachetime = intval(Config::get('curweather', 'cachetime'));
124
125         if ($units === "") {
126                 $units = 'metric';
127         }
128
129         $ok = true;
130
131         $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
132
133         if ($res === false) {
134                 $ok = false;
135         }
136
137         if ($ok) {
138                 $t = get_markup_template("widget.tpl", "addon/curweather/" );
139                 $curweather = replace_macros ($t, [
140                         '$title' => L10n::t("Current Weather"),
141                         '$icon' => ProxyUtils::proxifyUrl('http://openweathermap.org/img/w/'.$res['icon'].'.png'),
142                         '$city' => $res['city'],
143                         '$lon' => $res['lon'],
144                         '$lat' => $res['lat'],
145                         '$description' => $res['descripion'],
146                         '$temp' => $res['temperature'],
147                         '$relhumidity' => ['caption'=>L10n::t('Relative Humidity'), 'val'=>$res['humidity']],
148                         '$pressure' => ['caption'=>L10n::t('Pressure'), 'val'=>$res['pressure']],
149                         '$wind' => ['caption'=>L10n::t('Wind'), 'val'=> $res['wind']],
150                         '$lastupdate' => L10n::t('Last Updated').': '.$res['update'].'UTC',
151                         '$databy' =>  L10n::t('Data by'),
152                         '$showonmap' => L10n::t('Show on map')
153                 ]);
154         } else {
155                 $t = get_markup_template('widget-error.tpl', 'addon/curweather/');
156                 $curweather = replace_macros( $t, [
157                         '$problem' => L10n::t('There was a problem accessing the weather data. But have a look'),
158                         '$rpt' => $rpt,
159                         '$atOWM' => L10n::t('at OpenWeatherMap')
160                 ]);
161         }
162
163         $a->page['aside'] = $curweather . $a->page['aside'];
164 }
165
166 function curweather_addon_settings_post(App $a, $post)
167 {
168         if (!local_user() || empty($_POST['curweather-settings-submit'])) {
169                 return;
170         }
171
172         PConfig::set(local_user(), 'curweather', 'curweather_loc'   , trim($_POST['curweather_loc']));
173         PConfig::set(local_user(), 'curweather', 'curweather_enable', intval($_POST['curweather_enable']));
174         PConfig::set(local_user(), 'curweather', 'curweather_units' , trim($_POST['curweather_units']));
175
176         info(L10n::t('Current Weather settings updated.') . EOL);
177 }
178
179 function curweather_addon_settings(App $a, &$s)
180 {
181         if (!local_user()) {
182                 return;
183         }
184
185         /* Get the current state of our config variable */
186         $curweather_loc = PConfig::get(local_user(), 'curweather', 'curweather_loc');
187         $curweather_units = PConfig::get(local_user(), 'curweather', 'curweather_units');
188         $appid = Config::get('curweather', 'appid');
189
190         if ($appid == "") {
191                 $noappidtext = L10n::t('No APPID found, please contact your admin to obtain one.');
192         } else {
193                 $noappidtext = '';
194         }
195
196         $enable = intval(PConfig::get(local_user(), 'curweather', 'curweather_enable'));
197         $enable_checked = (($enable) ? ' checked="checked" ' : '');
198         
199         // load template and replace the macros
200         $t = get_markup_template("settings.tpl", "addon/curweather/" );
201
202         $s = replace_macros ($t, [
203                 '$submit' => L10n::t('Save Settings'),
204                 '$header' => L10n::t('Current Weather').' '.L10n::t('Settings'),
205                 '$noappidtext' => $noappidtext,
206                 '$info' => L10n::t('Enter either the name of your location or the zip code.'),
207                 '$curweather_loc' => [ 'curweather_loc', L10n::t('Your Location'), $curweather_loc, L10n::t('Identifier of your location (name or zip code), e.g. <em>Berlin,DE</em> or <em>14476,DE</em>.') ],
208                 '$curweather_units' => [ 'curweather_units', L10n::t('Units'), $curweather_units, L10n::t('select if the temperature should be displayed in &deg;C or &deg;F'), ['metric'=>'°C', 'imperial'=>'°F']],
209                 '$enabled' => [ 'curweather_enable', L10n::t('Show weather data'), $enable, '']
210         ]);
211
212         return;
213 }
214
215 // Config stuff for the admin panel to let the admin of the node set a APPID
216 // for accessing the API of openweathermap
217 function curweather_addon_admin_post(App $a)
218 {
219         if (!is_site_admin()) {
220                 return;
221         }
222
223         if (!empty($_POST['curweather-submit'])) {
224                 Config::set('curweather', 'appid',     trim($_POST['appid']));
225                 Config::set('curweather', 'cachetime', trim($_POST['cachetime']));
226
227                 info(L10n::t('Curweather settings saved.' . PHP_EOL));
228         }
229 }
230
231 function curweather_addon_admin(App $a, &$o)
232 {
233         if (!is_site_admin()) {
234                 return;
235         }
236
237         $appid = Config::get('curweather', 'appid');
238         $cachetime = Config::get('curweather', 'cachetime');
239
240         $t = get_markup_template("admin.tpl", "addon/curweather/" );
241
242         $o = replace_macros ($t, [
243                 '$submit' => L10n::t('Save Settings'),
244                 '$cachetime' => [
245                         'cachetime',
246                         L10n::t('Caching Interval'),
247                         $cachetime,
248                         L10n::t('For how long should the weather data be cached? Choose according your OpenWeatherMap account type.'), [
249                                 '0'    => L10n::t('no cache'),
250                                 '300'  => '5 '  . L10n::t('minutes'),
251                                 '900'  => '15 ' . L10n::t('minutes'),
252                                 '1800' => '30 ' . L10n::t('minutes'),
253                                 '3600' => '60 ' . L10n::t('minutes')
254                         ]
255                 ],
256                 '$appid' => ['appid', L10n::t('Your APPID'), $appid, L10n::t('Your API key provided by OpenWeatherMap')]
257         ]);
258 }