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\Hook;
15 use Friendica\Core\Renderer;
16 use Friendica\Core\Session;
18 use Friendica\Util\Network;
19 use Friendica\Util\Proxy as ProxyUtils;
21 function curweather_install()
23 Hook::register('network_mod_init' , 'addon/curweather/curweather.php', 'curweather_network_mod_init');
24 Hook::register('addon_settings' , 'addon/curweather/curweather.php', 'curweather_addon_settings');
25 Hook::register('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
28 function curweather_uninstall()
30 Hook::unregister('network_mod_init' , 'addon/curweather/curweather.php', 'curweather_network_mod_init');
31 Hook::unregister('addon_settings' , 'addon/curweather/curweather.php', 'curweather_addon_settings');
32 Hook::unregister('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
35 // get the weather data from OpenWeatherMap
36 function getWeather($loc, $units = 'metric', $lang = 'en', $appid = '', $cachetime = 0)
38 $url = "http://api.openweathermap.org/data/2.5/weather?q=" . $loc . "&appid=" . $appid . "&lang=" . $lang . "&units=" . $units . "&mode=xml";
39 $cached = DI::cache()->get('curweather'.md5($url));
40 $now = new DateTime();
42 if (!is_null($cached)) {
43 $cdate = DI::pConfig()->get(local_user(), 'curweather', 'last');
44 $cached = unserialize($cached);
46 if ($cdate + $cachetime > $now->getTimestamp()) {
52 $res = new SimpleXMLElement(Network::fetchUrl($url));
53 } catch (Exception $e) {
54 if (empty($_SESSION['curweather_notice_shown'])) {
55 notice(DI::l10n()->t('Error fetching weather data. Error was: ' . $e->getMessage()));
56 $_SESSION['curweather_notice_shown'] = true;
62 unset($_SESSION['curweather_notice_shown']);
64 if (in_array((string) $res->temperature['unit'], ['celsius', 'metric'])) {
72 if (trim((string) $res->weather['value']) == trim((string) $res->clouds['name'])) {
73 $desc = (string) $res->clouds['name'];
75 $desc = (string) $res->weather['value'] . ', ' . (string) $res->clouds['name'];
79 'city' => (string) $res->city['name'][0],
80 'country' => (string) $res->city->country[0],
81 'lat' => (string) $res->city->coord['lat'],
82 'lon' => (string) $res->city->coord['lon'],
83 'temperature' => (string) $res->temperature['value'][0].$tunit,
84 'pressure' => (string) $res->pressure['value'] . (string) $res->pressure['unit'],
85 'humidity' => (string) $res->humidity['value'] . (string) $res->humidity['unit'],
86 'descripion' => $desc,
87 'wind' => (string) $res->wind->speed['name'] . ' (' . (string) $res->wind->speed['value'] . $wunit . ')',
88 'update' => (string) $res->lastupdate['value'],
89 'icon' => (string) $res->weather['icon'],
92 DI::pConfig()->set(local_user(), 'curweather', 'last', $now->getTimestamp());
93 DI::cache()->set('curweather'.md5($url), serialize($r), Duration::HOUR);
98 function curweather_network_mod_init(App $a, &$b)
100 if (!intval(DI::pConfig()->get(local_user(), 'curweather', 'curweather_enable'))) {
104 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
106 // $rpt value is needed for location
107 // $lang will be taken from the browser session to honour user settings
108 // TODO $lang does not work if the default settings are used
109 // and not all response strings are translated
110 // $units can be set in the settings by the user
111 // $appid is configured by the admin in the admin panel
112 // those parameters will be used to get: cloud status, temperature, preassure
113 // and relative humidity for display, also the relevent area of the map is
114 // linked from lat/log of the reply of OWMp
115 $rpt = DI::pConfig()->get(local_user(), 'curweather', 'curweather_loc');
117 // Set the language to the browsers language or default and use metric units
118 $lang = Session::get('language', DI::config()->get('system', 'language'));
119 $units = DI::pConfig()->get( local_user(), 'curweather', 'curweather_units');
120 $appid = DI::config()->get('curweather', 'appid');
121 $cachetime = intval(DI::config()->get('curweather', 'cachetime'));
129 $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
131 if ($res === false) {
136 $t = Renderer::getMarkupTemplate("widget.tpl", "addon/curweather/" );
137 $curweather = Renderer::replaceMacros($t, [
138 '$title' => DI::l10n()->t("Current Weather"),
139 '$icon' => ProxyUtils::proxifyUrl('http://openweathermap.org/img/w/'.$res['icon'].'.png'),
140 '$city' => $res['city'],
141 '$lon' => $res['lon'],
142 '$lat' => $res['lat'],
143 '$description' => $res['descripion'],
144 '$temp' => $res['temperature'],
145 '$relhumidity' => ['caption'=>DI::l10n()->t('Relative Humidity'), 'val'=>$res['humidity']],
146 '$pressure' => ['caption'=>DI::l10n()->t('Pressure'), 'val'=>$res['pressure']],
147 '$wind' => ['caption'=>DI::l10n()->t('Wind'), 'val'=> $res['wind']],
148 '$lastupdate' => DI::l10n()->t('Last Updated').': '.$res['update'].'UTC',
149 '$databy' => DI::l10n()->t('Data by'),
150 '$showonmap' => DI::l10n()->t('Show on map')
153 $t = Renderer::getMarkupTemplate('widget-error.tpl', 'addon/curweather/');
154 $curweather = Renderer::replaceMacros( $t, [
155 '$problem' => DI::l10n()->t('There was a problem accessing the weather data. But have a look'),
157 '$atOWM' => DI::l10n()->t('at OpenWeatherMap')
161 DI::page()['aside'] = $curweather . DI::page()['aside'];
164 function curweather_addon_settings_post(App $a, $post)
166 if (!local_user() || empty($_POST['curweather-settings-submit'])) {
170 DI::pConfig()->set(local_user(), 'curweather', 'curweather_loc' , trim($_POST['curweather_loc']));
171 DI::pConfig()->set(local_user(), 'curweather', 'curweather_enable', intval($_POST['curweather_enable']));
172 DI::pConfig()->set(local_user(), 'curweather', 'curweather_units' , trim($_POST['curweather_units']));
175 function curweather_addon_settings(App $a, &$s)
181 /* Get the current state of our config variable */
182 $curweather_loc = DI::pConfig()->get(local_user(), 'curweather', 'curweather_loc');
183 $curweather_units = DI::pConfig()->get(local_user(), 'curweather', 'curweather_units');
184 $appid = DI::config()->get('curweather', 'appid');
187 $noappidtext = DI::l10n()->t('No APPID found, please contact your admin to obtain one.');
192 $enable = intval(DI::pConfig()->get(local_user(), 'curweather', 'curweather_enable'));
193 $enable_checked = (($enable) ? ' checked="checked" ' : '');
195 // load template and replace the macros
196 $t = Renderer::getMarkupTemplate("settings.tpl", "addon/curweather/" );
198 $s = Renderer::replaceMacros($t, [
199 '$submit' => DI::l10n()->t('Save Settings'),
200 '$header' => DI::l10n()->t('Current Weather').' '.DI::l10n()->t('Settings'),
201 '$noappidtext' => $noappidtext,
202 '$info' => DI::l10n()->t('Enter either the name of your location or the zip code.'),
203 '$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>.') ],
204 '$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']],
205 '$enabled' => [ 'curweather_enable', DI::l10n()->t('Show weather data'), $enable, '']
211 // Config stuff for the admin panel to let the admin of the node set a APPID
212 // for accessing the API of openweathermap
213 function curweather_addon_admin_post(App $a)
215 if (!is_site_admin()) {
219 if (!empty($_POST['curweather-submit'])) {
220 DI::config()->set('curweather', 'appid', trim($_POST['appid']));
221 DI::config()->set('curweather', 'cachetime', trim($_POST['cachetime']));
225 function curweather_addon_admin(App $a, &$o)
227 if (!is_site_admin()) {
231 $appid = DI::config()->get('curweather', 'appid');
232 $cachetime = DI::config()->get('curweather', 'cachetime');
234 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/curweather/" );
236 $o = Renderer::replaceMacros($t, [
237 '$submit' => DI::l10n()->t('Save Settings'),
240 DI::l10n()->t('Caching Interval'),
242 DI::l10n()->t('For how long should the weather data be cached? Choose according your OpenWeatherMap account type.'), [
243 '0' => DI::l10n()->t('no cache'),
244 '300' => '5 ' . DI::l10n()->t('minutes'),
245 '900' => '15 ' . DI::l10n()->t('minutes'),
246 '1800' => '30 ' . DI::l10n()->t('minutes'),
247 '3600' => '60 ' . DI::l10n()->t('minutes')
250 '$appid' => ['appid', DI::l10n()->t('Your APPID'), $appid, DI::l10n()->t('Your API key provided by OpenWeatherMap')]