Jeremiah, Garett, TechGnome, Beel, and Viper007Bond Plugin URI: http://www.viper007bond.com/wordpress-plugins/weathericon/ Version: 2.3.2 Description: Shows the current weather conditions by querying the NOAA website. Also includes a weather "widget" for WP-Dash. Configure on the Options > WeatherIcon page and/or view comprehensive test debug data here. Get support for this plugin here. Date: 07 Oct 2005 *************************************************************************** Current Weather Conditions Plugin For Wordpress by: Jeremiah Poling ( http://www.sonicpling.net ) Garett Harnish ( http://zy.ca ) Incorporating improvements by: TechGnome ( http://tannagh.com/ ) Beel ( http://www.billhayes.us/wordpress ) Viper007Bond ( http://www.viper007bond.com/ ) Based on: "MyWeather 1.4.1" module for 'Mambo' (c) 2003 Vasco Nunes ( www.vasconunes.com ) and "GetWx", a GPL script by Mark Woodward ( woody.cowpi.com/phpscripts.html ) and icons from phpweather project *************************************************************************** */ $wi_version = '2.3.2'; $weather_default_settings = array( 'station' => 'KLAF', 'language' => 'english', 'icon_location' => get_settings('siteurl') . '/wp-content/plugins/WeatherIcon/wicons/', 'icon_ext' => '.png', 'temp' => 'F', 'wind_units' => 'mph', 'visibility_units' => 'miles', 'usetime' => 'currenttime', 'sunrise_hour' => '6', 'sunset_hour' => '19', 'cache_type' => 'DB', 'cache_timeout' => 1800, 'cache_path' => ABSPATH . 'wp-content/plugins/WeatherIcon/cache', ); $weather_default_options = array( 'fetch_suns', 'display_icon', 'display_temp', 'display_humidity', 'display_wind', 'display_dewpt', 'display_visibility', 'display_clouds', 'display_barometer', 'display_windchill', 'display_heatindex' ); if (!get_settings('weather_settings') || !get_settings('weather_options')){ update_option('weather_settings', $weather_default_settings); update_option('weather_options', $weather_default_options); } # Fixes a bug in l10n.php where some guy decided there was no reason # to load files which are required for their l10n functions if no # language is defined in Wordpress. require_once(ABSPATH . 'wp-includes/streams.php'); require_once(ABSPATH . 'wp-includes/gettext.php'); if (!function_exists('check_flag')) { function check_flag($flagname, $allflags) { echo (in_array($flagname, $allflags) ? 'checked="checked"' : ''); } } # Grab new settings and do the update now # It might be more efficient to do this in # is_plugin_page segment, but it results in an annoying problem # with the l10n/i18n settings. if (isset($_POST['update_weather'])) { $weather_settings = $_POST['weather_settings']; foreach($weather_default_settings as $key => $val) { if (empty($weather_settings[$key])) { $weather_settings[$key] = $val; } } update_option('weather_settings', $weather_settings); update_option('weather_options', $_POST['weather_options']); if (is_plugin_page()) { ?>

query($query); if (mysql_error()) { _e('Could not create METAR Cache Table, defaulting to file cache', 'WeatherIcon'); $weather_settings['cache_type'] = 'FILE'; } } // If the form says clear the cache, let's do it if (isset($_POST['wi_clear_cache'])) { // If clearing DB cache if (isset($_POST['wi_clear_db_cache'])) { if ($wpdb->query("TRUNCATE TABLE `${table_prefix}metar_cache`") === FALSE && is_plugin_page()) { ?>

cache directory writable. Please chmod 777 this folder.', 'WeatherIcon'); ?>

Weather Icon

type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
type="checkbox" />
        
type="checkbox" />
type="checkbox" />

  1. <?php if (function_exists('WeatherIcon')) : ?>
         <li>Lufkin, Texas
              <ul><?php WeatherIcon('station=KLFK'); ?></ul>
         </li>
    <?php endif; ?>
  2. WordPress Codex for information on how to pass arguments to a function in a query string format). You only need to set 'station', the rest are optional.", 'WeatherIcon'); ?>

    station
    string) The 4-letter ICAO airport indentifier code to display the weather for. Find a location code here: http://weather.gov/tg/siteloc.shtml. Defaults to 'KLFK'.", 'WeatherIcon'); ?>
    before
    string) Text to display before each weather item description (i.e. the icon, the temperature, the wind, etc.). Defaults to '<li>'.", 'WeatherIcon'); ?>
    after
    string) Text to display after each weather item description (i.e. the icon, the temperature, the wind, etc.). Defaults to '</li>'.", 'WeatherIcon'); ?>
    debug
    boolean) Overrides global debug values. If true, all debug information (cache & condition) will be displayed, If false, all debug information will be supressed. Valid values:', 'WeatherIcon'); ?>
    • 1
    • 0
    display
    boolean) Sets whether to echo or print the function's results. Valid values:", 'WeatherIcon'); ?>
    • 1 ', 'WeatherIcon'); ?>
    • 0 ', 'WeatherIcon'); ?>
    override_cache
    boolean) Overrides the cache. If true, the cache will be ignored. There is no need for a false value since that is default, but it is available. Valid values:', 'WeatherIcon'); ?>
    • 1
    • 0
    language
    string) The language in which to display plugin. The language must match the name of the mo file in the languages directory w/o the extension. IE: deutsch.mo would be simply language=deutsch.', 'WeatherIcon'); ?>
  3. <span>'s with these classes:", 'WeatherIcon'); ?>

    weather_title
    weather_info

    .weather_title {
         font-weight: bold;
    }
&after=', $before = '
  • ', $after = '
  • ') { global $Itemid, $conditions, $weather_settings, $weather_options, $table_prefix, $wpdb, $wi_version, $wp_version, $l10n; // We're using the 'var1=foo&var2=bar' input format now, so let's take care of it -Viper if (stristr($arguments, '=') === FALSE) { // Okay, this means they are still using the old format $wxInfo['STATION'] = $arguments; // No need for $before and $after as they were either set by the user or above $display = TRUE; $override_cache = FALSE; unset($l10n['WeatherIcon']); load_textdomain('WeatherIcon', ABSPATH . 'wp-content/plugins/WeatherIcon/languages/' . $weather_settings['language'] . '.mo'); } else { // Yay, new input format! $arguments = explode('&', $arguments); // Break each variable/value set up foreach($arguments as $varvalue) { $temp = explode('=', $varvalue); $new_args[$temp[0]] = $temp[1]; } $wxInfo['STATION'] = (isset($new_args['station'])) ? $new_args['station'] : $weather_settings['station']; $before = (isset($new_args['before'])) ? $new_args['before'] : '
  • '; $after = (isset($new_args['after'])) ? $new_args['after'] : '
  • '; if (strtoupper($new_args['debug']) == 'FALSE' || $new_args['debug'] == '0') $debug = FALSE; elseif (strtoupper($new_args['debug']) == 'TRUE' || $new_args['debug'] == '1') $debug = TRUE; $display = (strtoupper($new_args['display']) == 'FALSE' || $new_args['display'] == '0') ? FALSE : TRUE; $override_cache = (strtoupper($new_args['override_cache']) == 'FALSE' || $new_args['override_cache'] == '0') ? FALSE : TRUE; $show_all = (strtoupper($new_args['show_all']) == 'FALSE' || $new_args['show_all'] == '0') ? FALSE : TRUE; if ($new_args['language']) { load_textdomain('WeatherIcon', ABSPATH . 'wp-content/plugins/WeatherIcon/languages/' . strtolower($new_args['language']) . '.mo'); } else { unset($l10n['WeatherIcon']); load_textdomain('WeatherIcon', ABSPATH . 'wp-content/plugins/WeatherIcon/languages/' . $weather_settings['language'] . '.mo'); } } $wxInfo['TEMPERATURE'] = $weather_settings['temp']; $wxInfo['CACHE_TIMEOUT'] = $weather_settings['cache_timeout']; $wxInfo['CACHE_PATH'] = $weather_settings['cache_path']; $wxInfo['CACHE_TYPE'] = $weather_settings['cache_type']; if ($wxInfo['CACHE_TYPE'] == 'DB') { $cache = $wpdb->get_var("SELECT wxInfo FROM `${table_prefix}metar_cache` WHERE station = '" . $wxInfo['STATION'] ."'"); if ($cache) { $wxInfo = unserialize($cache); } } else { $wxInfo['CTIME'] = time(); } // This allows us to over-ride outputting any debug $wxInfo['CACHE_DEBUG'] = (!isset($debug)) ? in_array('debug_cache', $weather_options) : $debug; $wxInfo['CONDITIONS_DEBUG'] = (!isset($debug)) ? in_array('debug_conditions', $weather_options) : $debug; if ($wxInfo['CACHE_DEBUG'] || $wxInfo['CONDITIONS_DEBUG']) { echo "General Information\n"; echo ""; } if ($wxInfo['CACHE_TYPE'] == 'DB') { if ($wxInfo['CACHE_DEBUG']) { echo "Cache Debug\n"; echo " \n"; } } if ($wxInfo['CACHE_DEBUG']) { echo "METAR DATA:"; echo ""; } if ( empty($wxInfo['ERROR']) ) { // icon $no_night=0; ///////// // --- BEGIN BUG FIX --- // Date: Oct 28. 2004 // Name: Uses Overcast instead of Fog or Snow // Modification by: Garett Harnish // // Description // ----------- // In many case, when there is snow or fog, there is also the cloud status // __('overcast', 'WeatherIcon') . In the original code, the Fog and Snow blocks were not // linked together with the rest. As a result, __('overcast', 'WeatherIcon') overrode the // fog and snow icons. In addition, added CONDITIONS_STATUS 11 and 9 // to the list. // fog if ($wxInfo['CONDITIONS_STATUS']=='8' or $wxInfo['CONDITIONS_STATUS']=='11') { $wxInfo['ICON']="1cloud_fog"; $no_night=1; } // smoke if ($wxInfo['CONDITIONS_STATUS']=='9') { $wxInfo['ICON']="3cloud_fog"; $no_night=1; } // heavy snow elseif ($wxInfo['CONDITIONS_STATUS']=='5' && $wxInfo['CONDITIONS_SEVERITY']=='2') { $wxInfo['ICON']="4cloud_heavysnow"; $no_night=1; } // light snow elseif ($wxInfo['CONDITIONS_STATUS']=='5' && $wxInfo['CONDITIONS_SEVERITY']=='1') { $wxInfo['ICON']="4cloud_modsnow"; $no_night=1; } // snow elseif ($wxInfo['CONDITIONS_STATUS']=='5' && $wxInfo['CONDITIONS_SEVERITY']=='0') { $wxInfo['ICON']="4cloud_lightsnow"; $no_night=1; } // general snow elseif ($wxInfo['CONDITIONS_STATUS']=='5') { $wxInfo['ICON']="4cloud_lightsnow"; $no_night=1; } // heavy rain elseif ($wxInfo['CONDITIONS_STATUS']=='4' && $wxInfo['CONDITIONS_SEVERITY']=='2') { $wxInfo['ICON']="4cloud_heavyrain"; $no_night=1; } // light rain elseif ($wxInfo['CONDITIONS_STATUS']=='4' && $wxInfo['CONDITIONS_SEVERITY']=='0') { $wxInfo['ICON']="4cloud_lightrain"; $no_night=1; } // rain elseif ($wxInfo['CONDITIONS_STATUS']=='4' && $wxInfo['CONDITIONS_SEVERITY']=='1') { $wxInfo['ICON']="4cloud_modrain"; $no_night=1; } // thunder elseif ($wxInfo['CONDITIONS_STATUS']=='2') { $wxInfo['ICON']="3cloud_thunders"; $no_night=0; } // scattered clouds elseif (($wxInfo['CONDITIONS_SEVERITY']=='' || $wxInfo['CONDITIONS_SEVERITY']=='0') && $wxInfo['CLOUDS_STATUS']=='2') { $wxInfo['ICON']="2cloud_norain"; $no_night=0; } // mostly cloudy elseif (($wxInfo['CONDITIONS_SEVERITY']=='' || $wxInfo['CONDITIONS_SEVERITY']=='0') && $wxInfo['CLOUDS_STATUS']=='3') { $wxInfo['ICON']="3cloud_norain"; $no_night=0; } // partly cloudy elseif (($wxInfo['CONDITIONS_SEVERITY']=='' || $wxInfo['CONDITIONS_SEVERITY']=='0') && $wxInfo['CLOUDS_STATUS']=='1') { $wxInfo['ICON']="1cloud_norain"; $no_night=0; } // clear skies elseif ($wxInfo['CLOUDS_STATUS']=='0') { $wxInfo['ICON']="0cloud"; $no_night=0; } // overcast elseif ($wxInfo['CLOUDS_STATUS']==__('overcast', 'WeatherIcon') ) { $wxInfo['ICON']="4cloud_norain"; $no_night=1; } // drizzle elseif ($wxInfo['CONDITIONS_STATUS']=='12' && $wxInfo['CONDITIONS_SEVERITY']=='1') { $wxInfo['ICON']="4cloud_lightrain"; $no_night=1; } // --- END BUG FIX --- // Use which time (current/metar) determining night if ($weather_settings['usetime'] == 'metartime') { $now=intval(date("H", $wxInfo["TIME"])) + (intval(date("i", $wxInfo["TIME"]))/60); } else { $now = intval(gmdate("H", $wxInfo['CTIME'])) + intval(get_option('gmt_offset')) + (intval(gmdate("i", $wxInfo['CTIME']))/60); // Ensure hour is within the 0-24 range if ($now < 0) $now += 24; else if ($now > 24) $now -= 24; } // if night, icon maybe different if (($now >= $wxInfo['SUNSETHOUR'] && $now <= $wxInfo['SUNRISEHOUR']) ||((($now >= $wxInfo['SUNSETHOUR'] && $now <= 24) || ($now <= $wxInfo['SUNRISEHOUR'] && $now >= 0)) && $wxInfo['SUNSETHOUR'] > $wxInfo['SUNRISEHOUR'])) { if (!$no_night) $wxInfo['ICON']= "n_".$wxInfo['ICON']; } // Debug The Time if ($wxInfo['CACHE_DEBUG']) { echo "Time Debug"; echo ""; } // Debug the Conditions if ($wxInfo['CONDITIONS_DEBUG']) { echo "Conditions Debug"; echo " "; } ///////// if (in_array('display_icon', $weather_options) && $wxInfo['ICON']<>'') { $weather .= $before; $msie = '/msie\s(5\.[5-9]|[6-9]\.[0-9]*).*(win)/i'; if( !isset($_SERVER['HTTP_USER_AGENT']) || !preg_match($msie,$_SERVER['HTTP_USER_AGENT']) || preg_match('/opera/i',$_SERVER['HTTP_USER_AGENT'])) { $weather .= ''; } $weather .= $after; } if (in_array('display_temp', $weather_options) || $show_all) { $weather .= $before; $weather .= '' . __('', 'WeatherIcon') . ' '; $weather .= '

    ' . $wxInfo['TEMP_' . $weather_settings['temp']] . '°' . $weather_settings['temp'] . ''; $weather .= $after; } if ((in_array('display_windchill', $weather_options) || $show_all) && $wxInfo['WIND CHILL'] <> '') { $weather .= $before; $weather .= '' . __('Feels Like', 'WeatherIcon') . ' '; $weather .= '' . $wxInfo['WIND CHILL'] . '
    '; $weather .= $after; } }// end else $weather .= "\n'; // The content gets echoed or returned here if ($display) echo $weather; else return $weather; } // End WeatherIcon Function function get_sunset ( &$wxInfo ) { $host = 'mobile.wunderground.com'; $location = "/cgi-bin/findweather/getForecast?brand=mobile&query=".$wxInfo['STATION']; $request = "HTTP/1.1\r\n" . "If-Modified-Since: Sat, 29 Oct 1994 09:00:00 GMT\r\n" . "Pragma: no-cache\r\n". "Cache-Control: no-cache\r\n"; @$fp = fsockopen($host, 80); $request = "GET $location $request" . "Host: $host\r\n" . "Content-Type: text/html\r\n" . "Connection: Close\r\n\r\n"; $sunset = false; if ($fp) { fputs($fp, $request); if (strpos(fgets($fp, 1024), '200 ')) { do { $line = fgets($fp, 1024); } while ($line != "\r\n"); $webpage_contents = ''; while ($line = fgets($fp, 1024)) { $webpage_contents .= $line; } fclose($fp); } } preg_match_all("/(Sunrise|Sunset)<\/td>([^>]*)<\/b><\/td><\/tr>/", $webpage_contents, $suns); if (strtoupper($suns[1][0]) == 'SUNRISE' || strtoupper($suns[1][1]) == 'SUNSET') { $sunset = true; eval( '$wxInfo[\''.strtoupper($suns[1][0]). '\']=strtotime("'.$suns[2][0].'");' ); eval( '$wxInfo[\''.strtoupper($suns[1][1]). '\']=strtotime("'.$suns[2][1].'");' ); } return $sunset; } /////////////////////////////////////////////////////////////////////// // Below here is the Noaa Query Code and the code to parse the output// /////////////////////////////////////////////////////////////////////// function get_metar ( &$wxInfo) { if ($wxInfo['CACHE_TYPE'] == 'FILE') { $metar_cache= $wxInfo['CACHE_PATH']. "/metar_cache_".$wxInfo['STATION'].".txt"; if (file_exists($metar_cache)) { $cache_age=(time()-filemtime($metar_cache)); } if ($wxInfo['CACHE_DEBUG']) { echo "Cache Debug\n"; echo " "; } return $metar; } } if ($wxInfo['CACHE_DEBUG']) { echo "
  • Fetching New Metar Data
  • \n"; } $host = 'weather.noaa.gov'; $location = "/pub/data/observations/metar/stations/".$wxInfo['STATION'].".TXT"; $request = "HTTP/1.1\r\n" . "If-Modified-Since: Sat, 29 Oct 1994 09:00:00 GMT\r\n" . "Pragma: no-cache\r\n". "Cache-Control: no-cache\r\n"; @$fp = fsockopen($host, 80, $errno, $errstr, 60); $request = "GET $location $request" . "Host: $host\r\n" . "Content-Type: text/html\r\n" . "Connection: Close\r\n\r\n"; $metar = false; if ($fp) { fputs($fp, $request); $httpresponse = fgets($fp, 1024); if ($wxInfo['CACHE_DEBUG']) { echo "
  • NOAA Responded:
  • "; echo ""; } else { if ($wxInfo['CACHE_DEBUG']) { echo "
  • Could Not Open Socket: ". $errstr . "(". $errno .")
  • \n"; } } if ($wxInfo['CACHE_TYPE'] == 'FILE') { if ($fp = fopen($metar_cache, "w")) { fwrite($fp, $metar); fclose($fp); } } if ($wxInfo['CACHE_DEBUG']) { echo " "; } return $metar; } function process_metar(&$wxInfo,$visibility_unit,$wind_unit) { $metarParts = explode(' ',$wxInfo['METAR']); $groupName = array('get_station','get_time','get_station_type','get_wind','get_var_wind','get_visibility','get_runway','get_conditions','get_cloud_cover','get_temperature','get_altimeter', 'get_remarks'); $metarPtr = 1; // get_station identity is ignored $group = 1; $part = $metarParts[$metarPtr]; while ($group < count($groupName) && strlen($part) > 0) { $groupName[$group]($part,$metarPtr,$group,$wxInfo,$visibility_unit,$wind_unit); // $groupName is a function variable $part = $metarParts[$metarPtr]; } } function get_station($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { if (strlen($part) == 4 and $group == 0) { $group++; $metarPtr++; } } function get_time($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { if (substr($part,-1) == 'Z') $metarPtr++; $group++; // determine date if (gmdate("d",time()) == substr($part,0,2)) { $time = time(); } else { $time = time() - 86400; } $wxInfo['TIME'] = gmmktime(intval(substr($part,2,2)), intval(substr($part,4,2)), 0, intval(gmdate("m", $time)), intval(gmdate("d", $time)), intval(gmdate("Y", $time))); } function get_station_type($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { if ($part == 'AUTO' || $part == 'COR') $metarPtr++; $group++; } function get_wind($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { if (!function_exists('speed')) { // --- BEGIN BUG FIX --- // Date: Oct 28. 2004 // Name: Wind: Calm mph // Modification by: Garett Harnish // // Description // ----------- // I completely rewrote the speed function, not solely to fix the // "Wind: Calm mph" bug in a more elegant manner, but to make it // easier to read the function w/ its comments instead of having // to look through a jumbled if-then-else construction. In any case, // by attaching the wind units to the speed directly, we ensure that // in the cases where a speed is displayed it will have it's units // and when a speed isn't, units shan't be display. function speed($part, $unit,$wind_unit) { switch ($wind_unit.$unit) { // Convert to miles per hour case 'mphKT': return round(1.1508 * $part) . " $wind_unit"; // from knots case 'mphMPS': return round(2.23694 * $part) . " $wind_unit"; // from meter/sec case 'mphKMH': return round(0.621371 * $part) . " $wind_unit"; // from km/h // Convert to kilometers per hour case 'km/hKT': return round(1.852 * $part) . " $wind_unit"; // from knots case 'km/hMPS': return round($part * 3.6) . " $wind_unit"; // from meter/sec case 'km/hKMH': return $part . " $wind_unit"; // from km/h // Convert to meters per second case "m/sKT": return round(0.514 * $part) . " $wind_unit"; // from knots case "m/sMPS": return $part . " $wind_unit"; // from meter/sec case "m/sKMH": return round(0.278 * $part) . " $wind_unit"; // from km/h // Convert to knots case 'ktKT': return ($part - 0.0) . " $wind_unit"; // from knots case 'ktMPS': return round(0.86897*$part) . " $wind_unit"; // from meter/sec case 'ktKMH': return round(0.539956803*$part) . " $wind_unit"; // from km/h default: return "**INVALID DATA**"; } } // --- END BUG FIX --- } if (ereg('^([0-9G]{5,10}|VRB[0-9]{2,3})(KT|MPS|KMH)$',$part,$pieces)) { $part = $pieces[1]; $unit = $pieces[2]; if ($part == '00000') { $wxInfo['WIND'] = __('Calm', 'WeatherIcon'); // no wind } else { ereg('([0-9]{3}|VRB)([0-9]{2,3})G?([0-9]{2,3})?',$part,$pieces); if ($pieces[1] == 'VRB') { $direction = __('Varies', 'WeatherIcon') ; } else { $angle = (integer) $pieces[1]; $compass = array(__('N', 'WeatherIcon'),__('NNE', 'WeatherIcon'),__('NE', 'WeatherIcon'),__('ENE', 'WeatherIcon'),__('E', 'WeatherIcon'),__('ESE', 'WeatherIcon'),__('SE', 'WeatherIcon'),__('SSE', 'WeatherIcon'),__('S', 'WeatherIcon'),__('SSW', 'WeatherIcon'),__('SW', 'WeatherIcon'),__('WSW', 'WeatherIcon'),__('W', 'WeatherIcon'),__('WNW', 'WeatherIcon'),__('NW', 'WeatherIcon'),__('NNW', 'WeatherIcon')); $direction = $compass[round($angle / 22.5) % 16]; } if ($pieces[3] == 0) { $gust = ''; } else { $gust = ', '. __('gusting to', 'WeatherIcon') .' ' . speed($pieces[3], $unit,$wind_unit); } $wxInfo['WIND'] = $direction . ' '.__('at', 'WeatherIcon').' ' . speed($pieces[2], $unit,$wind_unit) . $gust; } $metarPtr++; } $group++; } function get_var_wind($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { if (ereg('([0-9]{3})V([0-9]{3})',$part,$pieces)) $metarPtr++; $group++; } function get_visibility($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { static $integerMile = ''; // --- BEGIN BUG FIX --- // Date: Feb 22, 2005 // Name: Reports Imperial Data as Metric // Modification by: Garett Harnish // // Description // ----------- // If plugin set to display information in metric, and data was // provided in imperial, the plugin would display the imperial // data as metric. if (strlen($part) == 1) { // visibility is limited to a whole mile plus a fraction part if ($visibility_unit=='km') $integerMile= $part * 1.609344; else $integerMile = $part . ' '; $metarPtr++; } elseif (substr($part,-2) == 'SM') { // visibility is in miles $part = substr($part,0,strlen($part)-2); if (substr($part,0,1) == 'M') { $prefix = __('less than', 'WeatherIcon') .' '; $part = substr($part, 1); } else $prefix = ''; if ($visibility_unit=='km') { str_replace(" ", "*", $part); eval("\$distance = round($integerMile + ($part * 1.609344), 1);"); } else { $distance = $integerMile . $part; } if ((($integerMile == '' && ereg('[/]',$part,$pieces)) || $part == '1') && $visibility_unit=='miles') { $unit = ' mile'; } else { if ($visibility_unit=='miles') $unit = ' miles'; else $unit = ' km'; } $wxInfo['VISIBILITY'] = $prefix . $distance . $unit; $metarPtr++; $group++; } // --- END BUG FIX --- elseif (substr($part,-2) == 'KM') { // unknown (Reported by NFFN in Fiji) $metarPtr++; $group++; } elseif (ereg('^([0-9]{4})$',$part,$pieces)) { // visibility is in meters if ($visibility_unit=='miles') $distance = round($part/ 1609.344, 1); // convert to miles else $distance = $part/1000; if ($distance > 5) $distance = round($distance); if ($distance <= 1 && $visibility_unit=='miles') $unit = ' mile'; elseif ($distance <> 1 && $visibility_unit=='miles') $unit = ' miles'; if ($visibility_unit=='km') $unit = ' km'; $wxInfo['VISIBILITY'] = $distance . $unit; $metarPtr++; $group++; } elseif ($part == 'CAVOK') { // good weather if ($visibility_unit=='miles') $wxInfo['VISIBILITY'] = __('greater than', 'WeatherIcon') .' 7 '.__('miles', 'WeatherIcon') ; else $wxInfo['VISIBILITY'] =__('greater than', 'WeatherIcon') .' 10 km'; $wxInfo['CONDITIONS'] = ''; $wxInfo['CLOUDS'] = __('clear skies', 'WeatherIcon') ; $wxInfo['CLOUDS_STATUS']='0'; $metarPtr++; $group += 4; // can skip the next 3 groups } else { $group++; } } function get_runway($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { if (substr($part,0,1) == 'R') $metarPtr++; else $group++; } function get_conditions($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { // --- BEGIN BUG FIX --- // Date: Oct 18, 2004 // Name: Duplicate Conditions // Modification by: Garett Harnish // // Description // ----------- // This fix corrects a problem with mutliple reporting locations where // condition data (ie: blowing snow) was not cleared from one location to // the next. Changing the variable from a static to a global and placing // code in the function WeatherIcon to clear the global on run fixed the // problem // // Original Code // ------------- // static $conditions = ''; global $conditions; // --- END BUG FIX --- $wxCode = array( 'VC' => __('nearby', 'WeatherIcon'), 'MI' => __('shallow', 'WeatherIcon'), 'PR' => __('partial', 'WeatherIcon'), 'BC' => __('patches of', 'WeatherIcon'), 'DR' => __('low drifting', 'WeatherIcon'), 'BL' => __('blowing', 'WeatherIcon'), 'SH' => __('showers', 'WeatherIcon'), 'TS' => __('thunderstorm', 'WeatherIcon'), 'FZ' => __('freezing', 'WeatherIcon'), 'DZ' => __('drizzle', 'WeatherIcon'), 'RA' => __('rain', 'WeatherIcon'), 'SN' => __('snow', 'WeatherIcon'), 'SG' => __('snow grains', 'WeatherIcon'), 'IC' => __('ice crystals', 'WeatherIcon'), 'PE' => __('ice pellets', 'WeatherIcon'), 'GR' => __('hail', 'WeatherIcon'), 'GS' => __('smallhail', 'WeatherIcon'), 'UP' => __('unknown', 'WeatherIcon'), 'BR' => __('mist', 'WeatherIcon'), 'FG' => __('fog', 'WeatherIcon'), 'FU' => __('smoke', 'WeatherIcon'), 'VA' => __('volcanic ash', 'WeatherIcon'), 'DU' => __('widespread dust', 'WeatherIcon'), 'SA' => __('sand', 'WeatherIcon'), 'HZ' => __('haze', 'WeatherIcon'), 'PY' => __('spray', 'WeatherIcon'), 'PO' => __('well-developed dust/sand whirls', 'WeatherIcon'), 'SQ' => __('squalls', 'WeatherIcon'), 'FC' => __('funnel cloud, tornado, or waterspout', 'WeatherIcon'), 'SS' => __('sandstorm/duststorm', 'WeatherIcon')); static $wxIcon = array( 'TS' => '2', 'FZ' => '3', 'RA' => '4', 'SN' => '5', 'SG' => '5', 'IC' => '7', 'PE' => '7', 'FG' => '8', 'FU' => '9', 'SA' => '10', 'HZ' => '11', 'DZ' => '12'); if (ereg('^(-|\+|VC)?(TS|SH|FZ|BL|DR|MI|BC|PR|RA|DZ|SN|SG|GR|GS|PE|IC|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS)+$',$part,$pieces)) { if (strlen($conditions) == 0) { $join = ''; $wxInfo['CONDITIONS_SEVERITY'] = '0'; } else $join = ' & '; if (substr($part,0,1) == '-') { $wxInfo['CONDITIONS_SEVERITY'] = '1'; $prefix = __('light', 'WeatherIcon') .' '; $part = substr($part,1); } elseif (substr($part,0,1) == '+') { $wxInfo['CONDITIONS_SEVERITY'] = '2'; $prefix = __('heavy', 'WeatherIcon') .' '; $part = substr($part,1); } else $prefix = ''; // moderate conditions have no descriptor $conditions .= $join . $prefix; // The 'showers' code 'SH' is moved behind the next 2-letter code to make the English translation read better. if (substr($part,0,2) == 'SH') $part = substr($part,2,2) . substr($part,0,2). substr($part, 4); while ($code = substr($part,0,2)) { $conditions .= $wxCode[$code] . ' '; // --- BEGIN BUG FIX --- // Date: Oct 28. 2004 // Name: Fog, Haze, Smoke, and Sand don't set a CONDITION STATUS. // Modification by: Garett Harnish // // Description // ----------- // Changed the if/then statement to check the Icon Array for a matching // code and then assigns a condition status to it if a match is found. // This ensure if there is a condition status defined, it is assigned. if (inarray($wxIcon, $code)) { $wxInfo['CONDITIONS_STATUS'] = $wxIcon[$code]; } // --- END BUG FIX --- $part = substr($part,2); } $wxInfo['CONDITIONS'] = $conditions; $metarPtr++; } else { $wxInfo['CONDITIONS'] = $conditions; $group++; } $wxInfo['CONDITIONS'] = $conditions; if (ereg('^(-|\+|VC)?(SH|RA)+$',$part,$pieces)) $wxInfo['CONDITIONS_STATUS'] = '1'; //showers rain if (ereg('^(-|\+|VC)?(SH|SN)+$',$part,$pieces)) $wxInfo['CONDITIONS_STATUS'] = '2'; //showers snow } function get_cloud_cover($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { $cloudCode = array( 'SKC' => __('clear skies', 'WeatherIcon'), 'CLR' => __('clear skies', 'WeatherIcon'), 'NSC' => __('clear skies', 'WeatherIcon'), 'FEW' => __('partly cloudy', 'WeatherIcon'), 'SCT' => __('scattered clouds', 'WeatherIcon'), 'BKN' => __('mostly cloudy', 'WeatherIcon'), 'OVC' => __('overcast', 'WeatherIcon'), 'VV' => __('vertical visibility', 'WeatherIcon') ); $cloudIcon = array( 'SKC' => '0', 'CLR' => '0', 'NSC' => '0', 'FEW' => '1', 'SCT' => '2', 'BKN' => '3', 'OVC' => __('overcast', 'WeatherIcon'), 'VV' => __('vertical visibility', 'WeatherIcon')); if ($part == 'SKC' || $part == 'CLR' || $part == 'NSC') { $wxInfo['CLOUDS'] = $cloudCode[$part]; $wxInfo['CLOUDS_STATUS'] = '0'; $metarPtr++; $group++; } else { if (ereg('^([A-Z]{2,3})([0-9]{3})',$part,$pieces)) { // codes for CB and TCU are ignored $wxInfo['CLOUDS'] = $cloudCode[$pieces[1]]; $wxInfo['CLOUDS_STATUS'] = $cloudIcon[$pieces[1]]; if ($pieces[1] == 'VV') { $altitude = (integer) 100 * $pieces[2]; // units are feet $wxInfo['CLOUDS'] .= " to $altitude ft"; } $metarPtr++; } /** Fix for BKN/// error **/ elseif (ereg('^([A-Z]{2,3})', $part,$pieces)) { // bad data (ignore) $metarPtr++; } else { $group++; } } } function get_temperature($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { if (!function_exists('get_heat_index')) { function get_heat_index($tempF, $rh, &$wxInfo) { // Calculate Heat Index based on temperature in F and relative humidity (65 = 65%) if ($tempF > 79 && $rh > 39) { $hiF = -42.379 + 2.04901523 * $tempF + 10.14333127 * $rh - 0.22475541 * $tempF * $rh; $hiF += -0.00683783 * pow($tempF, 2) - 0.05481717 * pow($rh, 2); $hiF += 0.00122874 * pow($tempF, 2) * $rh + 0.00085282 * $tempF * pow($rh, 2); $hiF += -0.00000199 * pow($tempF, 2) * pow($rh, 2); $hiF = round($hiF); $hiC = round(($hiF - 32) / 1.8); if ($wxInfo['TEMPERATURE']=='F') $wxInfo['HEAT INDEX'] = "$hiF°F"; else $wxInfo['HEAT INDEX'] = "$hiC°C"; } } } if (!function_exists('get_wind_chill')) { function get_wind_chill($tempF, &$wxInfo) { // Calculate Wind Chill Temperature based on temperature in F and // wind speed in miles per hour if ($tempF < 51 && $wxInfo['WIND'] != __('Calm', 'WeatherIcon') ) { $pieces = explode(' ', $wxInfo['WIND']); $windspeed = (integer) $pieces[2]; // wind speed must be in miles per hour if ($windspeed > 3) { $chillF = 35.74 + 0.6215 * $tempF - 35.75 * pow($windspeed, 0.16) + 0.4275 * $tempF * pow($windspeed, 0.16); $chillF = round($chillF); $chillC = round(($chillF - 32) / 1.8); if ($wxInfo['TEMPERATURE']=='F') $wxInfo['WIND CHILL'] = "$chillF°F"; else $wxInfo['WIND CHILL'] = "$chillC°C"; } } } } if (ereg('^(M?[0-9]{2})/(M?[0-9]{2}|[X]{2})?$',$part,$pieces)) { $tempC = (integer) strtr($pieces[1], 'M', '-'); $tempF = round(1.8 * $tempC + 32); $wxInfo['TEMP_C'] = $tempC; $wxInfo['TEMP_F'] = $tempF; get_wind_chill($tempF, $wxInfo); if (strlen($pieces[2]) != 0 && $pieces[2] != 'XX') { $dewC = (integer) strtr($pieces[2], 'M', '-'); $dewF = round(1.8 * $dewC + 32); $wxInfo['DEWPT_C'] = $dewC; $wxInfo['DEWPT_F'] = $dewF; $rh = round(100 * pow((112 - (0.1 * $tempC) + $dewC) / (112 + (0.9 * $tempC)), 8)); $wxInfo['HUMIDITY'] = $rh . '%'; get_heat_index($tempF, $rh, $wxInfo); } $metarPtr++; $group++; } else { $group++; } } function get_altimeter($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { if (ereg('^(A|Q)([0-9]{4})',$part,$pieces)) { if ($pieces[1] == 'A') { $pressureIN = substr($pieces[2],0,2) . '.' . substr($pieces[2],2); // units are inches Hg $pressureHPA = round($pressureIN / 0.02953); // convert to hectoPascals } else { $pressureHPA = (integer) $pieces[2]; // units are hectoPascals $pressureIN = round(0.02953 * $pressureHPA,2); // convert to inches Hg } $wxInfo['BAROMETER'] = "$pressureIN".__('"', 'WeatherIcon') ." Hg ($pressureHPA hPa)"; $metarPtr++; $group++; } else { $group++; } } function get_remarks($part, &$metarPtr, &$group, &$wxInfo,$visibility_unit,$wind_unit) { if (ereg('VIRGA',$part,$pieces)) { if (strlen($conditions) == 0) { $join = ''; $wxInfo['CONDITIONS_SEVERITY'] = '0'; } else $join = ' & '; $conditions .= $join . __('virga', 'WeatherIcon'); // It's raining ... kinda $wxInfo['CONDITIONS_STATUS'] = '4'; $wxInfo['CONDITIONS'] = $conditions; $metarPtr++; } else { $metarPtr++; } } function inarray($array,$needle) { foreach ($array as $key => $value) if (stristr($key, $needle)) return true; return false; } /* And down here at the bottom we have the widget by Viper007Bond for WP-Dash */ function weathericon_css($ID) { $options = get_option("weathericon_widget$ID"); return default_widget_css($ID) . " #widget$ID { width: 220px; } #content$ID { width: 219px; overflow: auto; text-align: center; padding-bottom: 1px; } #weatherconfig$ID input { text-align: center; } "; } // This handles the submit of the widget config options function weathericon_updateconfig() { global $wpdb, $widgettable; if (isset($_POST['configweathericon'])) { $ID = $_POST['configweathericon']; $location = $_POST['weather_location']; $locationlabel = $_POST['weather_locationlabel']; if (isset($_POST['weather_labelheader']) && $locationlabel != '') { $wpdb->query("UPDATE $widgettable SET name='$locationlabel' WHERE ID='$ID'"); update_option("weathericon_widget$ID", array('location' => $location, 'locationlabel' => $locationlabel, 'nameinheader' => TRUE)); } else { $wpdb->query("UPDATE $widgettable SET name='WeatherIcon' WHERE ID='$ID'"); update_option("weathericon_widget$ID", array('location' => $location, 'locationlabel' => $locationlabel, 'nameinheader' => FALSE)); } } } add_action('init', 'weathericon_updateconfig', 5); // This spits out the widget config page function weathericon_configure($ID) { global $widgettable, $wpdb; $options = get_option("weathericon_widget$ID"); $header = $wpdb->get_var("SELECT name FROM $widgettable WHERE ID='$ID'"); if ($header !== 'WeatherIcon') $headerchecked = 'checked="checked" '; $output = '
    ' . __("Location Code", "WeatherIcon") . ':

    ' . __("Label", "WeatherIcon") . ':

      ' . __("Rename widget?", 'WeatherIcon') . '

    '; return $output; } // This outputs the main content of the widget -- the weather function weathericon_content($ID) { $options = get_option("weathericon_widget$ID"); // If no options exist for this particular box, die saying so if (!$options['location']) return __('This WeatherIcon widget instance has not yet been configured.', 'WeatherIcon') . " " . __('Click here', 'WeatherIcon') . ' ' . __('to configure it.', 'WeatherIcon'); if ($options['locationlabel'] && $options['nameinheader'] != TRUE) $output .= 'Weather for ' . $options['locationlabel'] . ':
    '; $output .= WeatherIcon('station=' . $options['location'] . '&before=&after=
    &debug=0&display=0'); return $output; } // This gives the widget to WP-Dash function weathericon_available() { if(function_exists('make_widget_available')) make_widget_available('WeatherIcon', __('Displays current weather for specified location(s).', 'WeatherIcon'), 'weathericon_', 'weathericon_configure', 'configurable=true;spawn_multiple=true;'); } add_action('init', 'weathericon_available'); // This removes the widget instance's config options from the database on close function weathericon_deactivate($ID) { global $wpdb; delete_option("weathericon_widget$ID"); $wpdb->hide_errors(); $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'rss$ID'"); } } ?>