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 'mod/proxy.php';
13 require_once 'include/text.php';
16 use Friendica\Core\Addon;
17 use Friendica\Core\Cache;
18 use Friendica\Core\Config;
19 use Friendica\Core\L10n;
20 use Friendica\Core\PConfig;
21 use Friendica\Util\Network;
23 function curweather_install()
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');
30 function curweather_uninstall()
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');
37 // get the weather data from OpenWeatherMap
38 function getWeather($loc, $units = 'metric', $lang = 'en', $appid = '', $cachetime = 0)
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();
44 if (!is_null($cached)) {
45 $cdate = PConfig::get(local_user(), 'curweather', 'last');
46 $cached = unserialize($cached);
48 if ($cdate + $cachetime > $now->getTimestamp()) {
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;
64 unset($_SESSION['curweather_notice_shown']);
66 if ((string) $res->temperature['unit'] === 'metric') {
74 if (trim((string) $res->weather['value']) == trim((string) $res->clouds['name'])) {
75 $desc = (string) $res->clouds['name'];
77 $desc = (string) $res->weather['value'] . ', ' . (string) $res->clouds['name'];
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'],
94 PConfig::set(local_user(), 'curweather', 'last', $now->getTimestamp());
95 Cache::set('curweather'.md5($url), serialize($r), CACHE_HOUR);
100 function curweather_network_mod_init(App $a, &$b)
102 if (!intval(PConfig::get(local_user(), 'curweather', 'curweather_enable'))) {
106 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
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');
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'));
131 $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
133 if ($res === false) {
138 $t = get_markup_template("widget.tpl", "addon/curweather/" );
139 $curweather = replace_macros ($t, [
140 '$title' => L10n::t("Current Weather"),
141 '$icon' => proxy_url('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')
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'),
159 '$atOWM' => L10n::t('at OpenWeatherMap')
163 $a->page['aside'] = $curweather . $a->page['aside'];
166 function curweather_addon_settings_post(App $a, $post)
168 if (!local_user() || empty($_POST['curweather-settings-submit'])) {
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']));
176 info(L10n::t('Current Weather settings updated.') . EOL);
179 function curweather_addon_settings(App $a, &$s)
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');
191 $noappidtext = L10n::t('No APPID found, please contact your admin to obtain one.');
196 $enable = intval(PConfig::get(local_user(), 'curweather', 'curweather_enable'));
197 $enable_checked = (($enable) ? ' checked="checked" ' : '');
199 // load template and replace the macros
200 $t = get_markup_template("settings.tpl", "addon/curweather/" );
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 °C or °F'), ['metric'=>'°C', 'imperial'=>'°F']],
209 '$enabled' => [ 'curweather_enable', L10n::t('Show weather data'), $enable, '']
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)
219 if (!is_site_admin()) {
223 if (!empty($_POST['curweather-submit'])) {
224 Config::set('curweather', 'appid', trim($_POST['appid']));
225 Config::set('curweather', 'cachetime', trim($_POST['cachetime']));
227 info(L10n::t('Curweather settings saved.' . PHP_EOL));
231 function curweather_addon_admin(App $a, &$o)
233 if (!is_site_admin()) {
237 $appid = Config::get('curweather', 'appid');
238 $cachetime = Config::get('curweather', 'cachetime');
240 $t = get_markup_template("admin.tpl", "addon/curweather/" );
242 $o = replace_macros ($t, [
243 '$submit' => L10n::t('Save Settings'),
246 L10n::t('Caching Interval'),
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')
256 '$appid' => ['appid', L10n::t('Your APPID'), $appid, L10n::t('Your API key provided by OpenWeatherMap')]