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';
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;
22 // get the weather data from OpenWeatherMap
23 function getWeather( $loc, $units='metric', $lang='en', $appid='', $cachetime=0) {
24 $url = "http://api.openweathermap.org/data/2.5/weather?q=".$loc."&appid=".$appid."&lang=".$lang."&units=".$units."&mode=xml";
25 $cached = Cache::get('curweather'.md5($url));
26 $now = new DateTime();
27 if (!is_null($cached)) {
28 $cdate = PConfig::get(local_user(), 'curweather', 'last');
29 $cached = unserialize($cached);
30 if ($cdate + $cachetime > $now->getTimestamp()) {
35 $res = new SimpleXMLElement(Network::fetchUrl($url));
36 } catch (Exception $e) {
37 if (!$_SESSION['curweather_notice_shown']) {
38 info(L10n::t('Error fetching weather data. Error was: '.$e->getMessage()));
39 $_SESSION['curweather_notice_shown'] = true;
43 unset($_SESSION['curweather_notice_shown']);
44 if ((string)$res->temperature['unit']==='metric') {
51 if ( trim((string)$res->weather['value']) == trim((string)$res->clouds['name']) ) {
52 $desc = (string)$res->clouds['name'];
54 $desc = (string)$res->weather['value'].', '.(string)$res->clouds['name'];
57 'city'=> (string) $res->city['name'][0],
58 'country' => (string) $res->city->country[0],
59 'lat' => (string) $res->city->coord['lat'],
60 'lon' => (string) $res->city->coord['lon'],
61 'temperature' => (string) $res->temperature['value'][0].$tunit,
62 'pressure' => (string) $res->pressure['value'].(string)$res->pressure['unit'],
63 'humidity' => (string) $res->humidity['value'].(string)$res->humidity['unit'],
64 'descripion' => $desc,
65 'wind' => (string)$res->wind->speed['name'].' ('.(string)$res->wind->speed['value'].$wunit.')',
66 'update' => (string)$res->lastupdate['value'],
67 'icon' => (string)$res->weather['icon']
69 PConfig::set(local_user(), 'curweather', 'last', $now->getTimestamp());
70 Cache::set('curweather'.md5($url), serialize($r), CACHE_HOUR);
74 function curweather_install()
76 Addon::registerHook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
77 Addon::registerHook('addon_settings', 'addon/curweather/curweather.php', 'curweather_addon_settings');
78 Addon::registerHook('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
81 function curweather_uninstall() {
82 Addon::unregisterHook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
83 Addon::unregisterHook('addon_settings', 'addon/curweather/curweather.php', 'curweather_addon_settings');
84 Addon::unregisterHook('addon_settings_post', 'addon/curweather/curweather.php', 'curweather_addon_settings_post');
87 function curweather_network_mod_init(&$fk_app,&$b) {
89 if(! intval(PConfig::get(local_user(),'curweather','curweather_enable')))
92 $fk_app->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
94 // $rpt value is needed for location
95 // $lang will be taken from the browser session to honour user settings
96 // TODO $lang does not work if the default settings are used
97 // and not all response strings are translated
98 // $units can be set in the settings by the user
99 // $appid is configured by the admin in the admin panel
100 // those parameters will be used to get: cloud status, temperature, preassure
101 // and relative humidity for display, also the relevent area of the map is
102 // linked from lat/log of the reply of OWMp
103 $rpt = PConfig::get(local_user(), 'curweather', 'curweather_loc');
105 // set the language to the browsers language and use metric units
106 $lang = $_SESSION['language'];
107 $units = PConfig::get( local_user(), 'curweather', 'curweather_units');
108 $appid = Config::get('curweather','appid');
109 $cachetime = intval(Config::get('curweather','cachetime'));
114 $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
119 $t = get_markup_template("widget.tpl", "addon/curweather/" );
120 $curweather = replace_macros ($t, [
121 '$title' => L10n::t("Current Weather"),
122 '$icon' => proxy_url('http://openweathermap.org/img/w/'.$res['icon'].'.png'),
123 '$city' => $res['city'],
124 '$lon' => $res['lon'],
125 '$lat' => $res['lat'],
126 '$description' => $res['descripion'],
127 '$temp' => $res['temperature'],
128 '$relhumidity' => ['caption'=>L10n::t('Relative Humidity'), 'val'=>$res['humidity']],
129 '$pressure' => ['caption'=>L10n::t('Pressure'), 'val'=>$res['pressure']],
130 '$wind' => ['caption'=>L10n::t('Wind'), 'val'=> $res['wind']],
131 '$lastupdate' => L10n::t('Last Updated').': '.$res['update'].'UTC',
132 '$databy' => L10n::t('Data by'),
133 '$showonmap' => L10n::t('Show on map')
136 $t = get_markup_template('widget-error.tpl', 'addon/curweather/');
137 $curweather = replace_macros( $t, [
138 '$problem' => L10n::t('There was a problem accessing the weather data. But have a look'),
140 '$atOWM' => L10n::t('at OpenWeatherMap')
144 $fk_app->page['aside'] = $curweather.$fk_app->page['aside'];
147 function curweather_addon_settings_post($a,$post) {
148 if(! local_user() || (! x($_POST,'curweather-settings-submit')))
150 PConfig::set(local_user(),'curweather','curweather_loc',trim($_POST['curweather_loc']));
151 PConfig::set(local_user(),'curweather','curweather_enable',intval($_POST['curweather_enable']));
152 PConfig::set(local_user(),'curweather','curweather_units',trim($_POST['curweather_units']));
154 info(L10n::t('Current Weather settings updated.') . EOL);
157 function curweather_addon_settings(&$a,&$s) {
162 /* Get the current state of our config variable */
164 $curweather_loc = PConfig::get(local_user(), 'curweather', 'curweather_loc');
165 $curweather_units = PConfig::get(local_user(), 'curweather', 'curweather_units');
166 $appid = Config::get('curweather','appid');
168 $noappidtext = L10n::t('No APPID found, please contact your admin to obtain one.');
172 $enable = intval(PConfig::get(local_user(),'curweather','curweather_enable'));
173 $enable_checked = (($enable) ? ' checked="checked" ' : '');
175 // load template and replace the macros
176 $t = get_markup_template("settings.tpl", "addon/curweather/" );
177 $s = replace_macros ($t, [
178 '$submit' => L10n::t('Save Settings'),
179 '$header' => L10n::t('Current Weather').' '.L10n::t('Settings'),
180 '$noappidtext' => $noappidtext,
181 '$info' => L10n::t('Enter either the name of your location or the zip code.'),
182 '$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>.') ],
183 '$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']],
184 '$enabled' => [ 'curweather_enable', L10n::t('Show weather data'), $enable, '']
189 // Config stuff for the admin panel to let the admin of the node set a APPID
190 // for accessing the API of openweathermap
191 function curweather_addon_admin_post (&$a) {
192 if(! is_site_admin())
194 if ($_POST['curweather-submit']) {
195 Config::set('curweather','appid',trim($_POST['appid']));
196 Config::set('curweather','cachetime',trim($_POST['cachetime']));
197 info(L10n::t('Curweather settings saved.'.EOL));
201 function curweather_addon_admin (&$a, &$o) {
202 if(! is_site_admin())
204 $appid = Config::get('curweather','appid');
205 $cachetime = Config::get('curweather','cachetime');
206 $t = get_markup_template("admin.tpl", "addon/curweather/" );
207 $o = replace_macros ($t, [
208 '$submit' => L10n::t('Save Settings'),
211 L10n::t('Caching Interval'),
213 L10n::t('For how long should the weather data be cached? Choose according your OpenWeatherMap account type.'), [
214 '0'=>L10n::t('no cache'),
215 '300'=>'5 '.L10n::t('minutes'),
216 '900'=>'15 '.L10n::t('minutes'),
217 '1800'=>'30 '.L10n::t('minutes'),
218 '3600'=>'60 '.L10n::t('minutes')
221 '$appid' => ['appid', L10n::t('Your APPID'), $appid, L10n::t('Your API key provided by OpenWeatherMap')]