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 include_once('include/text.php');
15 // get the weather data from OpenWeatherMap
16 function getWeather( $loc, $units='metric', $lang='en', $appid='', $cachetime=0) {
17 $url = "http://api.openweathermap.org/data/2.5/weather?q=".$loc."&appid=".$appid."&lang=".$lang."&units=".$units."&mode=xml";
18 $cached = Cache::get('curweather'.md5($url));
19 $now = new DateTime();
20 if (!is_null($cached)) {
21 $cdate = get_pconfig(local_user(), 'curweather', 'last');
22 $cached = unserialize($cached);
23 if ($cdate + $cachetime > $now->getTimestamp()) {
28 $res = new SimpleXMLElement(fetch_url($url));
29 } catch (Exception $e) {
30 info(t('Error fetching weather data.\nError was: '.$e->getMessage()));
33 if ((string)$res->temperature['unit']==='metric') {
40 if ( trim((string)$res->weather['value']) == trim((string)$res->clouds['name']) ) {
41 $desc = (string)$res->clouds['name'];
43 $desc = (string)$res->weather['value'].', '.(string)$res->clouds['name'];
46 'city'=> (string) $res->city['name'][0],
47 'country' => (string) $res->city->country[0],
48 'lat' => (string) $res->city->coord['lat'],
49 'lon' => (string) $res->city->coord['lon'],
50 'temperature' => (string) $res->temperature['value'][0].$tunit,
51 'pressure' => (string) $res->pressure['value'].(string)$res->pressure['unit'],
52 'humidity' => (string) $res->humidity['value'].(string)$res->humidity['unit'],
53 'descripion' => $desc,
54 'wind' => (string)$res->wind->speed['name'].' ('.(string)$res->wind->speed['value'].$wunit.')',
55 'update' => (string)$res->lastupdate['value'],
56 'icon' => (string)$res->weather['icon']
58 set_pconfig(local_user(), 'curweather', 'last', $now->getTimestamp());
59 Cache::set('curweather'.md5($url), serialize($r));
63 function curweather_install() {
64 register_hook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
65 register_hook('plugin_settings', 'addon/curweather/curweather.php', 'curweather_plugin_settings');
66 register_hook('plugin_settings_post', 'addon/curweather/curweather.php', 'curweather_plugin_settings_post');
69 function curweather_uninstall() {
70 unregister_hook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
71 unregister_hook('plugin_settings', 'addon/curweather/curweather.php', 'curweather_plugin_settings');
72 unregister_hook('plugin_settings_post', 'addon/curweather/curweather.php', 'curweather_plugin_settings_post');
75 function curweather_network_mod_init(&$fk_app,&$b) {
77 if(! intval(get_pconfig(local_user(),'curweather','curweather_enable')))
80 $fk_app->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
82 // $rpt value is needed for location
83 // $lang will be taken from the browser session to honour user settings
84 // TODO $lang does not work if the default settings are used
85 // and not all response strings are translated
86 // $units can be set in the settings by the user
87 // $appid is configured by the admin in the admin panel
88 // those parameters will be used to get: cloud status, temperature, preassure
89 // and relative humidity for display, also the relevent area of the map is
90 // linked from lat/log of the reply of OWMp
91 $rpt = get_pconfig(local_user(), 'curweather', 'curweather_loc');
94 // set the language to the browsers language and use metric units
95 $lang = $_SESSION['language'];
96 $units = get_pconfig( local_user(), 'curweather', 'curweather_units');
97 $appid = get_config('curweather','appid');
98 $cachetime = intval(get_config('curweather','cachetime'));
103 $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
108 $t = get_markup_template("widget.tpl", "addon/curweather/" );
109 $curweather = replace_macros ($t, array(
110 '$title' => t("Current Weather"),
111 '$icon' => $res['icon'],
112 '$city' => $res['city'],
113 '$lon' => $res['lon'],
114 '$lat' => $res['lat'],
115 '$description' => $res['descripion'],
116 '$temp' => $res['temperature'],
117 '$relhumidity' => array('caption'=>t('Relative Humidity'), 'val'=>$res['humidity']),
118 '$pressure' => array('caption'=>t('Pressure'), 'val'=>$res['pressure']),
119 '$wind' => array('caption'=>t('Wind'), 'val'=> $res['wind']),
120 '$lastupdate' => t('Last Updated').': '.$res['update'].'UTC',
121 '$databy' => t('Data by'),
122 '$showonmap' => t('Show on map')
125 $t = get_markup_template('widget-error.tpl', 'addon/curweather/');
126 $curweather = replace_macros( $t, array(
127 '$problem' => t('There was a problem accessing the weather data. But have a look'),
129 '$atOWM' => t('at OpenWeatherMap')
133 $fk_app->page['aside'] = $curweather.$fk_app->page['aside'];
138 function curweather_plugin_settings_post($a,$post) {
139 if(! local_user() || (! x($_POST,'curweather-settings-submit')))
141 set_pconfig(local_user(),'curweather','curweather_loc',trim($_POST['curweather_loc']));
142 set_pconfig(local_user(),'curweather','curweather_enable',intval($_POST['curweather_enable']));
143 set_pconfig(local_user(),'curweather','curweather_units',trim($_POST['curweather_units']));
145 info( t('Current Weather settings updated.') . EOL);
149 function curweather_plugin_settings(&$a,&$s) {
154 /* Get the current state of our config variable */
156 $curweather_loc = get_pconfig(local_user(), 'curweather', 'curweather_loc');
157 $curweather_units = get_pconfig(local_user(), 'curweather', 'curweather_units');
158 $appid = get_config('curweather','appid');
160 $noappidtext = t('No APPID found, please contact your admin to optain one.');
164 $enable = intval(get_pconfig(local_user(),'curweather','curweather_enable'));
165 $enable_checked = (($enable) ? ' checked="checked" ' : '');
167 // load template and replace the macros
168 $t = get_markup_template("settings.tpl", "addon/curweather/" );
169 $s = replace_macros ($t, array(
170 '$submit' => t('Save Settings'),
171 '$header' => t('Current Weather').' '.t('Settings'),
172 '$noappidtext' => $noappidtext,
173 '$info' => t('Enter either the name of your location or the zip code.'),
174 '$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>.') ),
175 '$curweather_units' => array( 'curweather_units', t('Units'), $curweather_units, t('select if the temperatur should be displayed in °C or °F'), array('metric'=>'°C', 'imperial'=>'°F')),
176 '$enabled' => array( 'curweather_enable', t('Show weather data'), $enable, '')
181 // Config stuff for the admin panel to let the admin of the node set a APPID
182 // for accessing the API of openweathermap
183 function curweather_plugin_admin_post (&$a) {
184 if(! is_site_admin())
186 if ($_POST['curweather-submit']) {
187 set_config('curweather','appid',trim($_POST['appid']));
188 set_config('curweather','cachetime',trim($_POST['cachetime']));
189 info( t('Curweather settings saved.'.EOL));
192 function curweather_plugin_admin (&$a, &$o) {
193 if(! is_site_admin())
195 $appid = get_config('curweather','appid');
196 $cachetime = get_config('curweather','cachetime');
197 $t = get_markup_template("admin.tpl", "addon/curweather/" );
198 $o = replace_macros ($t, array(
199 '$submit' => t('Save Settings'),
200 '$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'))),
201 '$appid' => array('appid', t('Your APPID'), $appid, t('Your API key provided by OpenWeatherMap'))