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/network.php');
13 require_once("mod/proxy.php");
14 require_once('include/text.php');
16 use Friendica\Core\Config;
17 use Friendica\Core\PConfig;
19 // get the weather data from OpenWeatherMap
20 function getWeather( $loc, $units='metric', $lang='en', $appid='', $cachetime=0) {
21 $url = "http://api.openweathermap.org/data/2.5/weather?q=".$loc."&appid=".$appid."&lang=".$lang."&units=".$units."&mode=xml";
22 $cached = Cache::get('curweather'.md5($url));
23 $now = new DateTime();
24 if (!is_null($cached)) {
25 $cdate = PConfig::get(local_user(), 'curweather', 'last');
26 $cached = unserialize($cached);
27 if ($cdate + $cachetime > $now->getTimestamp()) {
32 $res = new SimpleXMLElement(fetch_url($url));
33 } catch (Exception $e) {
34 info(t('Error fetching weather data.\nError was: '.$e->getMessage()));
37 if ((string)$res->temperature['unit']==='metric') {
44 if ( trim((string)$res->weather['value']) == trim((string)$res->clouds['name']) ) {
45 $desc = (string)$res->clouds['name'];
47 $desc = (string)$res->weather['value'].', '.(string)$res->clouds['name'];
50 'city'=> (string) $res->city['name'][0],
51 'country' => (string) $res->city->country[0],
52 'lat' => (string) $res->city->coord['lat'],
53 'lon' => (string) $res->city->coord['lon'],
54 'temperature' => (string) $res->temperature['value'][0].$tunit,
55 'pressure' => (string) $res->pressure['value'].(string)$res->pressure['unit'],
56 'humidity' => (string) $res->humidity['value'].(string)$res->humidity['unit'],
57 'descripion' => $desc,
58 'wind' => (string)$res->wind->speed['name'].' ('.(string)$res->wind->speed['value'].$wunit.')',
59 'update' => (string)$res->lastupdate['value'],
60 'icon' => (string)$res->weather['icon']
62 PConfig::set(local_user(), 'curweather', 'last', $now->getTimestamp());
63 Cache::set('curweather'.md5($url), serialize($r), CACHE_HOUR);
67 function curweather_install() {
68 register_hook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
69 register_hook('plugin_settings', 'addon/curweather/curweather.php', 'curweather_plugin_settings');
70 register_hook('plugin_settings_post', 'addon/curweather/curweather.php', 'curweather_plugin_settings_post');
73 function curweather_uninstall() {
74 unregister_hook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
75 unregister_hook('plugin_settings', 'addon/curweather/curweather.php', 'curweather_plugin_settings');
76 unregister_hook('plugin_settings_post', 'addon/curweather/curweather.php', 'curweather_plugin_settings_post');
79 function curweather_network_mod_init(&$fk_app,&$b) {
81 if(! intval(PConfig::get(local_user(),'curweather','curweather_enable')))
84 $fk_app->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
86 // $rpt value is needed for location
87 // $lang will be taken from the browser session to honour user settings
88 // TODO $lang does not work if the default settings are used
89 // and not all response strings are translated
90 // $units can be set in the settings by the user
91 // $appid is configured by the admin in the admin panel
92 // those parameters will be used to get: cloud status, temperature, preassure
93 // and relative humidity for display, also the relevent area of the map is
94 // linked from lat/log of the reply of OWMp
95 $rpt = PConfig::get(local_user(), 'curweather', 'curweather_loc');
98 // set the language to the browsers language and use metric units
99 $lang = $_SESSION['language'];
100 $units = PConfig::get( local_user(), 'curweather', 'curweather_units');
101 $appid = Config::get('curweather','appid');
102 $cachetime = intval(Config::get('curweather','cachetime'));
107 $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
112 $t = get_markup_template("widget.tpl", "addon/curweather/" );
113 $curweather = replace_macros ($t, array(
114 '$title' => t("Current Weather"),
115 '$icon' => proxy_url('http://openweathermap.org/img/w/'.$res['icon'].'.png'),
116 '$city' => $res['city'],
117 '$lon' => $res['lon'],
118 '$lat' => $res['lat'],
119 '$description' => $res['descripion'],
120 '$temp' => $res['temperature'],
121 '$relhumidity' => array('caption'=>t('Relative Humidity'), 'val'=>$res['humidity']),
122 '$pressure' => array('caption'=>t('Pressure'), 'val'=>$res['pressure']),
123 '$wind' => array('caption'=>t('Wind'), 'val'=> $res['wind']),
124 '$lastupdate' => t('Last Updated').': '.$res['update'].'UTC',
125 '$databy' => t('Data by'),
126 '$showonmap' => t('Show on map')
129 $t = get_markup_template('widget-error.tpl', 'addon/curweather/');
130 $curweather = replace_macros( $t, array(
131 '$problem' => t('There was a problem accessing the weather data. But have a look'),
133 '$atOWM' => t('at OpenWeatherMap')
137 $fk_app->page['aside'] = $curweather.$fk_app->page['aside'];
142 function curweather_plugin_settings_post($a,$post) {
143 if(! local_user() || (! x($_POST,'curweather-settings-submit')))
145 PConfig::set(local_user(),'curweather','curweather_loc',trim($_POST['curweather_loc']));
146 PConfig::set(local_user(),'curweather','curweather_enable',intval($_POST['curweather_enable']));
147 PConfig::set(local_user(),'curweather','curweather_units',trim($_POST['curweather_units']));
149 info( t('Current Weather settings updated.') . EOL);
153 function curweather_plugin_settings(&$a,&$s) {
158 /* Get the current state of our config variable */
160 $curweather_loc = PConfig::get(local_user(), 'curweather', 'curweather_loc');
161 $curweather_units = PConfig::get(local_user(), 'curweather', 'curweather_units');
162 $appid = Config::get('curweather','appid');
164 $noappidtext = t('No APPID found, please contact your admin to obtain one.');
168 $enable = intval(PConfig::get(local_user(),'curweather','curweather_enable'));
169 $enable_checked = (($enable) ? ' checked="checked" ' : '');
171 // load template and replace the macros
172 $t = get_markup_template("settings.tpl", "addon/curweather/" );
173 $s = replace_macros ($t, array(
174 '$submit' => t('Save Settings'),
175 '$header' => t('Current Weather').' '.t('Settings'),
176 '$noappidtext' => $noappidtext,
177 '$info' => t('Enter either the name of your location or the zip code.'),
178 '$curweather_loc' => array( 'curweather_loc', t('Your Location'), $curweather_loc, t('Identifier of your location (name or zip code), e.g. <em>Berlin,DE</em> or <em>14476,DE</em>.') ),
179 '$curweather_units' => array( 'curweather_units', t('Units'), $curweather_units, t('select if the temperature should be displayed in °C or °F'), array('metric'=>'°C', 'imperial'=>'°F')),
180 '$enabled' => array( 'curweather_enable', t('Show weather data'), $enable, '')
185 // Config stuff for the admin panel to let the admin of the node set a APPID
186 // for accessing the API of openweathermap
187 function curweather_plugin_admin_post (&$a) {
188 if(! is_site_admin())
190 if ($_POST['curweather-submit']) {
191 Config::set('curweather','appid',trim($_POST['appid']));
192 Config::set('curweather','cachetime',trim($_POST['cachetime']));
193 info( t('Curweather settings saved.'.EOL));
196 function curweather_plugin_admin (&$a, &$o) {
197 if(! is_site_admin())
199 $appid = Config::get('curweather','appid');
200 $cachetime = Config::get('curweather','cachetime');
201 $t = get_markup_template("admin.tpl", "addon/curweather/" );
202 $o = replace_macros ($t, array(
203 '$submit' => t('Save Settings'),
204 '$cachetime' => array('cachetime', t('Caching Interval'), $cachetime, t('For how long should the weather data be cached? Choose according your OpenWeatherMap account type.'), array('0'=>t('no cache'), '300'=>'5 '.t('minutes'), '900'=>'15 '.t('minutes'), '1800'=>'30 '.t('minutes'), '3600'=>'60 '.t('minutes'))),
205 '$appid' => array('appid', t('Your APPID'), $appid, t('Your API key provided by OpenWeatherMap'))