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>
12 require_once 'include/text.php';
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\Core\Renderer;
21 use Friendica\Util\Network;
22 use Friendica\Util\Proxy as ProxyUtils;
24 function curweather_install()
26 Addon::registerHook('network_mod_init' , 'addon/curweather/curweather.php', 'curweather_network_mod_init');
27 Addon::registerHook('addon_settings' , 'addon/curweather/curweather.php', 'curweather_addon_settings');
28 Addon::registerHook('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
31 function curweather_uninstall()
33 Addon::unregisterHook('network_mod_init' , 'addon/curweather/curweather.php', 'curweather_network_mod_init');
34 Addon::unregisterHook('addon_settings' , 'addon/curweather/curweather.php', 'curweather_addon_settings');
35 Addon::unregisterHook('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
38 // get the weather data from OpenWeatherMap
39 function getWeather($loc, $units = 'metric', $lang = 'en', $appid = '', $cachetime = 0)
41 $url = "http://api.openweathermap.org/data/2.5/weather?q=" . $loc . "&appid=" . $appid . "&lang=" . $lang . "&units=" . $units . "&mode=xml";
42 $cached = Cache::get('curweather'.md5($url));
43 $now = new DateTime();
45 if (!is_null($cached)) {
46 $cdate = PConfig::get(local_user(), 'curweather', 'last');
47 $cached = unserialize($cached);
49 if ($cdate + $cachetime > $now->getTimestamp()) {
55 $res = new SimpleXMLElement(Network::fetchUrl($url));
56 } catch (Exception $e) {
57 if (empty($_SESSION['curweather_notice_shown'])) {
58 info(L10n::t('Error fetching weather data. Error was: '.$e->getMessage()));
59 $_SESSION['curweather_notice_shown'] = true;
65 unset($_SESSION['curweather_notice_shown']);
67 if ((string) $res->temperature['unit'] === 'metric') {
75 if (trim((string) $res->weather['value']) == trim((string) $res->clouds['name'])) {
76 $desc = (string) $res->clouds['name'];
78 $desc = (string) $res->weather['value'] . ', ' . (string) $res->clouds['name'];
82 'city' => (string) $res->city['name'][0],
83 'country' => (string) $res->city->country[0],
84 'lat' => (string) $res->city->coord['lat'],
85 'lon' => (string) $res->city->coord['lon'],
86 'temperature' => (string) $res->temperature['value'][0].$tunit,
87 'pressure' => (string) $res->pressure['value'] . (string) $res->pressure['unit'],
88 'humidity' => (string) $res->humidity['value'] . (string) $res->humidity['unit'],
89 'descripion' => $desc,
90 'wind' => (string) $res->wind->speed['name'] . ' (' . (string) $res->wind->speed['value'] . $wunit . ')',
91 'update' => (string) $res->lastupdate['value'],
92 'icon' => (string) $res->weather['icon'],
95 PConfig::set(local_user(), 'curweather', 'last', $now->getTimestamp());
96 Cache::set('curweather'.md5($url), serialize($r), Cache::HOUR);
101 function curweather_network_mod_init(App $a, &$b)
103 if (!intval(PConfig::get(local_user(), 'curweather', 'curweather_enable'))) {
107 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
109 // $rpt value is needed for location
110 // $lang will be taken from the browser session to honour user settings
111 // TODO $lang does not work if the default settings are used
112 // and not all response strings are translated
113 // $units can be set in the settings by the user
114 // $appid is configured by the admin in the admin panel
115 // those parameters will be used to get: cloud status, temperature, preassure
116 // and relative humidity for display, also the relevent area of the map is
117 // linked from lat/log of the reply of OWMp
118 $rpt = PConfig::get(local_user(), 'curweather', 'curweather_loc');
120 // Set the language to the browsers language or default and use metric units
121 $lang = (!empty($_SESSION['language']) ? $_SESSION['language'] : Config::get('system', 'language'));
122 $units = PConfig::get( local_user(), 'curweather', 'curweather_units');
123 $appid = Config::get('curweather', 'appid');
124 $cachetime = intval(Config::get('curweather', 'cachetime'));
132 $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
134 if ($res === false) {
139 $t = Renderer::getMarkupTemplate("widget.tpl", "addon/curweather/" );
140 $curweather = Renderer::replaceMacros($t, [
141 '$title' => L10n::t("Current Weather"),
142 '$icon' => ProxyUtils::proxifyUrl('http://openweathermap.org/img/w/'.$res['icon'].'.png'),
143 '$city' => $res['city'],
144 '$lon' => $res['lon'],
145 '$lat' => $res['lat'],
146 '$description' => $res['descripion'],
147 '$temp' => $res['temperature'],
148 '$relhumidity' => ['caption'=>L10n::t('Relative Humidity'), 'val'=>$res['humidity']],
149 '$pressure' => ['caption'=>L10n::t('Pressure'), 'val'=>$res['pressure']],
150 '$wind' => ['caption'=>L10n::t('Wind'), 'val'=> $res['wind']],
151 '$lastupdate' => L10n::t('Last Updated').': '.$res['update'].'UTC',
152 '$databy' => L10n::t('Data by'),
153 '$showonmap' => L10n::t('Show on map')
156 $t = Renderer::getMarkupTemplate('widget-error.tpl', 'addon/curweather/');
157 $curweather = Renderer::replaceMacros( $t, [
158 '$problem' => L10n::t('There was a problem accessing the weather data. But have a look'),
160 '$atOWM' => L10n::t('at OpenWeatherMap')
164 $a->page['aside'] = $curweather . $a->page['aside'];
167 function curweather_addon_settings_post(App $a, $post)
169 if (!local_user() || empty($_POST['curweather-settings-submit'])) {
173 PConfig::set(local_user(), 'curweather', 'curweather_loc' , trim($_POST['curweather_loc']));
174 PConfig::set(local_user(), 'curweather', 'curweather_enable', intval($_POST['curweather_enable']));
175 PConfig::set(local_user(), 'curweather', 'curweather_units' , trim($_POST['curweather_units']));
177 info(L10n::t('Current Weather settings updated.') . EOL);
180 function curweather_addon_settings(App $a, &$s)
186 /* Get the current state of our config variable */
187 $curweather_loc = PConfig::get(local_user(), 'curweather', 'curweather_loc');
188 $curweather_units = PConfig::get(local_user(), 'curweather', 'curweather_units');
189 $appid = Config::get('curweather', 'appid');
192 $noappidtext = L10n::t('No APPID found, please contact your admin to obtain one.');
197 $enable = intval(PConfig::get(local_user(), 'curweather', 'curweather_enable'));
198 $enable_checked = (($enable) ? ' checked="checked" ' : '');
200 // load template and replace the macros
201 $t = Renderer::getMarkupTemplate("settings.tpl", "addon/curweather/" );
203 $s = Renderer::replaceMacros($t, [
204 '$submit' => L10n::t('Save Settings'),
205 '$header' => L10n::t('Current Weather').' '.L10n::t('Settings'),
206 '$noappidtext' => $noappidtext,
207 '$info' => L10n::t('Enter either the name of your location or the zip code.'),
208 '$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>.') ],
209 '$curweather_units' => [ 'curweather_units', L10n::t('Units'), $curweather_units, L10n::t('select if the temperature should be displayed in °C or °F'), ['metric'=>'°C', 'imperial'=>'°F']],
210 '$enabled' => [ 'curweather_enable', L10n::t('Show weather data'), $enable, '']
216 // Config stuff for the admin panel to let the admin of the node set a APPID
217 // for accessing the API of openweathermap
218 function curweather_addon_admin_post(App $a)
220 if (!is_site_admin()) {
224 if (!empty($_POST['curweather-submit'])) {
225 Config::set('curweather', 'appid', trim($_POST['appid']));
226 Config::set('curweather', 'cachetime', trim($_POST['cachetime']));
228 info(L10n::t('Curweather settings saved.' . PHP_EOL));
232 function curweather_addon_admin(App $a, &$o)
234 if (!is_site_admin()) {
238 $appid = Config::get('curweather', 'appid');
239 $cachetime = Config::get('curweather', 'cachetime');
241 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/curweather/" );
243 $o = Renderer::replaceMacros($t, [
244 '$submit' => L10n::t('Save Settings'),
247 L10n::t('Caching Interval'),
249 L10n::t('For how long should the weather data be cached? Choose according your OpenWeatherMap account type.'), [
250 '0' => L10n::t('no cache'),
251 '300' => '5 ' . L10n::t('minutes'),
252 '900' => '15 ' . L10n::t('minutes'),
253 '1800' => '30 ' . L10n::t('minutes'),
254 '3600' => '60 ' . L10n::t('minutes')
257 '$appid' => ['appid', L10n::t('Your APPID'), $appid, L10n::t('Your API key provided by OpenWeatherMap')]