3 * Name: Current Weather
4 * Description: Shows current weather conditions for user's location on their network page.
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>
13 use Friendica\Core\Cache\Duration;
14 use Friendica\Core\Config;
15 use Friendica\Core\Hook;
16 use Friendica\Core\Renderer;
17 use Friendica\Core\Session;
19 use Friendica\Util\Network;
20 use Friendica\Util\Proxy as ProxyUtils;
22 function curweather_install()
24 Hook::register('network_mod_init' , 'addon/curweather/curweather.php', 'curweather_network_mod_init');
25 Hook::register('addon_settings' , 'addon/curweather/curweather.php', 'curweather_addon_settings');
26 Hook::register('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
29 function curweather_uninstall()
31 Hook::unregister('network_mod_init' , 'addon/curweather/curweather.php', 'curweather_network_mod_init');
32 Hook::unregister('addon_settings' , 'addon/curweather/curweather.php', 'curweather_addon_settings');
33 Hook::unregister('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
36 // get the weather data from OpenWeatherMap
37 function getWeather($loc, $units = 'metric', $lang = 'en', $appid = '', $cachetime = 0)
39 $url = "http://api.openweathermap.org/data/2.5/weather?q=" . $loc . "&appid=" . $appid . "&lang=" . $lang . "&units=" . $units . "&mode=xml";
40 $cached = DI::cache()->get('curweather'.md5($url));
41 $now = new DateTime();
43 if (!is_null($cached)) {
44 $cdate = DI::pConfig()->get(local_user(), 'curweather', 'last');
45 $cached = unserialize($cached);
47 if ($cdate + $cachetime > $now->getTimestamp()) {
53 $res = new SimpleXMLElement(Network::fetchUrl($url));
54 } catch (Exception $e) {
55 if (empty($_SESSION['curweather_notice_shown'])) {
56 info(DI::l10n()->t('Error fetching weather data. Error was: '.$e->getMessage()));
57 $_SESSION['curweather_notice_shown'] = true;
63 unset($_SESSION['curweather_notice_shown']);
65 if (in_array((string) $res->temperature['unit'], ['celsius', 'metric'])) {
73 if (trim((string) $res->weather['value']) == trim((string) $res->clouds['name'])) {
74 $desc = (string) $res->clouds['name'];
76 $desc = (string) $res->weather['value'] . ', ' . (string) $res->clouds['name'];
80 'city' => (string) $res->city['name'][0],
81 'country' => (string) $res->city->country[0],
82 'lat' => (string) $res->city->coord['lat'],
83 'lon' => (string) $res->city->coord['lon'],
84 'temperature' => (string) $res->temperature['value'][0].$tunit,
85 'pressure' => (string) $res->pressure['value'] . (string) $res->pressure['unit'],
86 'humidity' => (string) $res->humidity['value'] . (string) $res->humidity['unit'],
87 'descripion' => $desc,
88 'wind' => (string) $res->wind->speed['name'] . ' (' . (string) $res->wind->speed['value'] . $wunit . ')',
89 'update' => (string) $res->lastupdate['value'],
90 'icon' => (string) $res->weather['icon'],
93 DI::pConfig()->set(local_user(), 'curweather', 'last', $now->getTimestamp());
94 DI::cache()->set('curweather'.md5($url), serialize($r), Duration::HOUR);
99 function curweather_network_mod_init(App $a, &$b)
101 if (!intval(DI::pConfig()->get(local_user(), 'curweather', 'curweather_enable'))) {
105 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
107 // $rpt value is needed for location
108 // $lang will be taken from the browser session to honour user settings
109 // TODO $lang does not work if the default settings are used
110 // and not all response strings are translated
111 // $units can be set in the settings by the user
112 // $appid is configured by the admin in the admin panel
113 // those parameters will be used to get: cloud status, temperature, preassure
114 // and relative humidity for display, also the relevent area of the map is
115 // linked from lat/log of the reply of OWMp
116 $rpt = DI::pConfig()->get(local_user(), 'curweather', 'curweather_loc');
118 // Set the language to the browsers language or default and use metric units
119 $lang = Session::get('language', DI::config()->get('system', 'language'));
120 $units = DI::pConfig()->get( local_user(), 'curweather', 'curweather_units');
121 $appid = DI::config()->get('curweather', 'appid');
122 $cachetime = intval(DI::config()->get('curweather', 'cachetime'));
130 $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
132 if ($res === false) {
137 $t = Renderer::getMarkupTemplate("widget.tpl", "addon/curweather/" );
138 $curweather = Renderer::replaceMacros($t, [
139 '$title' => DI::l10n()->t("Current Weather"),
140 '$icon' => ProxyUtils::proxifyUrl('http://openweathermap.org/img/w/'.$res['icon'].'.png'),
141 '$city' => $res['city'],
142 '$lon' => $res['lon'],
143 '$lat' => $res['lat'],
144 '$description' => $res['descripion'],
145 '$temp' => $res['temperature'],
146 '$relhumidity' => ['caption'=>DI::l10n()->t('Relative Humidity'), 'val'=>$res['humidity']],
147 '$pressure' => ['caption'=>DI::l10n()->t('Pressure'), 'val'=>$res['pressure']],
148 '$wind' => ['caption'=>DI::l10n()->t('Wind'), 'val'=> $res['wind']],
149 '$lastupdate' => DI::l10n()->t('Last Updated').': '.$res['update'].'UTC',
150 '$databy' => DI::l10n()->t('Data by'),
151 '$showonmap' => DI::l10n()->t('Show on map')
154 $t = Renderer::getMarkupTemplate('widget-error.tpl', 'addon/curweather/');
155 $curweather = Renderer::replaceMacros( $t, [
156 '$problem' => DI::l10n()->t('There was a problem accessing the weather data. But have a look'),
158 '$atOWM' => DI::l10n()->t('at OpenWeatherMap')
162 DI::page()['aside'] = $curweather . DI::page()['aside'];
165 function curweather_addon_settings_post(App $a, $post)
167 if (!local_user() || empty($_POST['curweather-settings-submit'])) {
171 DI::pConfig()->set(local_user(), 'curweather', 'curweather_loc' , trim($_POST['curweather_loc']));
172 DI::pConfig()->set(local_user(), 'curweather', 'curweather_enable', intval($_POST['curweather_enable']));
173 DI::pConfig()->set(local_user(), 'curweather', 'curweather_units' , trim($_POST['curweather_units']));
175 info(DI::l10n()->t('Current Weather settings updated.') . EOL);
178 function curweather_addon_settings(App $a, &$s)
184 /* Get the current state of our config variable */
185 $curweather_loc = DI::pConfig()->get(local_user(), 'curweather', 'curweather_loc');
186 $curweather_units = DI::pConfig()->get(local_user(), 'curweather', 'curweather_units');
187 $appid = DI::config()->get('curweather', 'appid');
190 $noappidtext = DI::l10n()->t('No APPID found, please contact your admin to obtain one.');
195 $enable = intval(DI::pConfig()->get(local_user(), 'curweather', 'curweather_enable'));
196 $enable_checked = (($enable) ? ' checked="checked" ' : '');
198 // load template and replace the macros
199 $t = Renderer::getMarkupTemplate("settings.tpl", "addon/curweather/" );
201 $s = Renderer::replaceMacros($t, [
202 '$submit' => DI::l10n()->t('Save Settings'),
203 '$header' => DI::l10n()->t('Current Weather').' '.DI::l10n()->t('Settings'),
204 '$noappidtext' => $noappidtext,
205 '$info' => DI::l10n()->t('Enter either the name of your location or the zip code.'),
206 '$curweather_loc' => [ 'curweather_loc', DI::l10n()->t('Your Location'), $curweather_loc, DI::l10n()->t('Identifier of your location (name or zip code), e.g. <em>Berlin,DE</em> or <em>14476,DE</em>.') ],
207 '$curweather_units' => [ 'curweather_units', DI::l10n()->t('Units'), $curweather_units, DI::l10n()->t('select if the temperature should be displayed in °C or °F'), ['metric'=>'°C', 'imperial'=>'°F']],
208 '$enabled' => [ 'curweather_enable', DI::l10n()->t('Show weather data'), $enable, '']
214 // Config stuff for the admin panel to let the admin of the node set a APPID
215 // for accessing the API of openweathermap
216 function curweather_addon_admin_post(App $a)
218 if (!is_site_admin()) {
222 if (!empty($_POST['curweather-submit'])) {
223 DI::config()->set('curweather', 'appid', trim($_POST['appid']));
224 DI::config()->set('curweather', 'cachetime', trim($_POST['cachetime']));
226 info(DI::l10n()->t('Curweather settings saved.' . PHP_EOL));
230 function curweather_addon_admin(App $a, &$o)
232 if (!is_site_admin()) {
236 $appid = DI::config()->get('curweather', 'appid');
237 $cachetime = DI::config()->get('curweather', 'cachetime');
239 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/curweather/" );
241 $o = Renderer::replaceMacros($t, [
242 '$submit' => DI::l10n()->t('Save Settings'),
245 DI::l10n()->t('Caching Interval'),
247 DI::l10n()->t('For how long should the weather data be cached? Choose according your OpenWeatherMap account type.'), [
248 '0' => DI::l10n()->t('no cache'),
249 '300' => '5 ' . DI::l10n()->t('minutes'),
250 '900' => '15 ' . DI::l10n()->t('minutes'),
251 '1800' => '30 ' . DI::l10n()->t('minutes'),
252 '3600' => '60 ' . DI::l10n()->t('minutes')
255 '$appid' => ['appid', DI::l10n()->t('Your APPID'), $appid, DI::l10n()->t('Your API key provided by OpenWeatherMap')]