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') {
41 'city'=> (string) $res->city['name'][0],
42 'country' => (string) $res->city->country[0],
43 'lat' => (string) $res->city->coord['lat'],
44 'lon' => (string) $res->city->coord['lon'],
45 'temperature' => (string) $res->temperature['value'][0].$tunit,
46 'pressure' => (string) $res->pressure['value'].(string)$res->pressure['unit'],
47 'humidity' => (string) $res->humidity['value'].(string)$res->humidity['unit'],
48 'descripion' => (string)$res->weather['value'].', '.(string)$res->clouds['name'],
49 'wind' => (string)$res->wind->speed['name'].' ('.(string)$res->wind->speed['value'].$wunit.')',
50 'update' => (string)$res->lastupdate['value']
52 set_pconfig(local_user(), 'curweather', 'last', $now->getTimestamp());
53 Cache::set('curweather'.md5($url), serialize($r));
57 function curweather_install() {
58 register_hook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
59 register_hook('plugin_settings', 'addon/curweather/curweather.php', 'curweather_plugin_settings');
60 register_hook('plugin_settings_post', 'addon/curweather/curweather.php', 'curweather_plugin_settings_post');
63 function curweather_uninstall() {
64 unregister_hook('network_mod_init', 'addon/curweather/curweather.php', 'curweather_network_mod_init');
65 unregister_hook('plugin_settings', 'addon/curweather/curweather.php', 'curweather_plugin_settings');
66 unregister_hook('plugin_settings_post', 'addon/curweather/curweather.php', 'curweather_plugin_settings_post');
69 function curweather_network_mod_init(&$fk_app,&$b) {
71 if(! intval(get_pconfig(local_user(),'curweather','curweather_enable')))
74 $fk_app->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $fk_app->get_baseurl() . '/addon/curweather/curweather.css' . '" media="all" />' . "\r\n";
76 // the OpenWeatherMap-PHP-APIlib does all the work here
77 // the $rpt value is needed for location
78 // $lang will be taken from the browser session to honour user settings
79 // $units can be set in the settings by the user
80 // $appid is configured by the admin in the admin panel
81 // those parameters will be used to get: cloud status, temperature, preassure
82 // and relative humidity for display, also the relevent area of the map is
83 // linked from lat/log of the reply of OWMp
84 $rpt = get_pconfig(local_user(), 'curweather', 'curweather_loc');
87 // set the language to the browsers language and use metric units
88 $lang = $_SESSION['language'];
89 $units = get_pconfig( local_user(), 'curweather', 'curweather_units');
90 $appid = get_config('curweather','appid');
91 $cachetime = intval(get_config('curweather','cachetime'));
96 $res = getWeather($rpt, $units, $lang, $appid, $cachetime);
101 $t = get_markup_template("widget.tpl", "addon/curweather/" );
102 $curweather = replace_macros ($t, array(
103 '$title' => t("Current Weather"),
104 '$city' => $res['city'],
105 '$lon' => $res['lon'],
106 '$lat' => $res['lat'],
107 '$description' => $res['descripion'],
108 '$temp' => $res['temperature'],
109 '$relhumidity' => array('caption'=>t('Relative Humidity'), 'val'=>$res['humidity']),
110 '$pressure' => array('caption'=>t('Pressure'), 'val'=>$res['pressure']),
111 '$wind' => array('caption'=>t('Wind'), 'val'=> $res['wind']),
112 '$lastupdate' => t('Last Updated').': '.$res['update'].'UTC',
113 '$databy' => t('Data by'),
114 '$showonmap' => t('Show on map')
117 $t = get_markup_template('widget-error.tpl', 'addon/curweather/');
118 $curweather = replace_macros( $t, array(
119 '$problem' => t('There was a problem accessing the weather data. But have a look'),
121 '$atOWM' => t('at OpenWeatherMap')
125 $fk_app->page['aside'] = $curweather.$fk_app->page['aside'];
130 function curweather_plugin_settings_post($a,$post) {
131 if(! local_user() || (! x($_POST,'curweather-settings-submit')))
133 set_pconfig(local_user(),'curweather','curweather_loc',trim($_POST['curweather_loc']));
134 set_pconfig(local_user(),'curweather','curweather_enable',intval($_POST['curweather_enable']));
135 set_pconfig(local_user(),'curweather','curweather_units',trim($_POST['curweather_units']));
137 info( t('Current Weather settings updated.') . EOL);
141 function curweather_plugin_settings(&$a,&$s) {
146 /* Get the current state of our config variable */
148 $curweather_loc = get_pconfig(local_user(), 'curweather', 'curweather_loc');
149 $curweather_units = get_pconfig(local_user(), 'curweather', 'curweather_units');
150 $appid = get_config('curweather','appid');
152 $noappidtext = t('No APPID found, please contact your admin to optain one.');
156 $enable = intval(get_pconfig(local_user(),'curweather','curweather_enable'));
157 $enable_checked = (($enable) ? ' checked="checked" ' : '');
159 // load template and replace the macros
160 $t = get_markup_template("settings.tpl", "addon/curweather/" );
161 $s = replace_macros ($t, array(
162 '$submit' => t('Save Settings'),
163 '$header' => t('Current Weather').' '.t('Settings'),
164 '$noappidtext' => $noappidtext,
165 '$info' => t('Enter either the name of your location or the zip code.'),
166 '$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>.') ),
167 '$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')),
168 '$enabled' => array( 'curweather_enable', t('Show weather data'), $enable, '')
173 // Config stuff for the admin panel to let the admin of the node set a APPID
174 // for accessing the API of openweathermap
175 function curweather_plugin_admin_post (&$a) {
176 if(! is_site_admin())
178 if ($_POST['curweather-submit']) {
179 set_config('curweather','appid',trim($_POST['appid']));
180 set_config('curweather','cachetime',trim($_POST['cachetime']));
181 info( t('Curweather settings saved.'.EOL));
184 function curweather_plugin_admin (&$a, &$o) {
185 if(! is_site_admin())
187 $appid = get_config('curweather','appid');
188 $cachetime = get_config('curweather','cachetime');
189 $t = get_markup_template("admin.tpl", "addon/curweather/" );
190 $o = replace_macros ($t, array(
191 '$submit' => t('Save Settings'),
192 '$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'))),
193 '$appid' => array('appid', t('Your APPID'), $appid, t('Your API key provided by OpenWeatherMap'))