$timeout,
'headers' => array(
'Connection' => 'Close',
'Accept' => '*/*'
)
);
// use generic wordpress function to retrieve data
$s = time();
$resp = wp_remote_request($url, $wprr_args);
$e = time();
if ( is_wp_error($resp) )
$blen = "-1";
else
$blen = strlen( $resp['body'] );
pdebug(1,"Fetch took ".(int) ($e-$s) . " seconds and got ".$blen." Bytes.");
// switch to wp-forecast transport
switch_wpf_transport(false);
if ( is_wp_error($resp) ) {
$errcode = $resp->get_error_code();
$errmesg = $resp->get_error_message($errcode);
$erg="Connection Error:".$errcode . "
";
$erg .= $errmesg ."\n";
} else
$erg = $resp['body'];
} else {
pdebug(1,"Using build in fsockopen method");
// fallback to old fsockopen variant
$url_parsed = parse_url($url);
$host = $url_parsed["host"];
if (!isset($url_parsed["port"]))
$port = 80;
else
$port = $url_parsed["port"];
$path = $url_parsed["path"];
if ($url_parsed["query"] != "") $path .= "?" . $url_parsed["query"];
$out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
// open connection
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
$erg = "";
if ($fp) {
// set timeout for reading
stream_set_timeout($fp, $timeout);
// send request
fwrite($fp, $out);
$body = false;
// read answer
while (!feof($fp)) {
$s = fgets($fp, 1024);
if ($body) $erg .= $s;
if ($s == "\r\n") $body = true;
}
// close connection
fclose($fp);
} else {
// error handling
$erg="Connection Error:".$errno. " >> ". $errstr ."\n";
}
}
pdebug(1,"End of fetchURL ()");
return $erg;
}
//
// converts the given wind parameters into a suitable windstring
//
function windstr($metric,$wspeed,$windunit)
{
pdebug(2,"Start of windstr ()");
// if its mph convert it to m/s
if ($metric != 1)
$wspeed = round($wspeed * 0.44704,0);
// convert it to selected unit
switch ($windunit) {
case "ms":
$wunit="m/s";
break;
case "kmh":
$wspeed=round($wspeed*3.6,0);
$wunit="km/h";
break;
case "mph":
$wspeed=round($wspeed*2.23694,0);
$wunit="mph";
break;
case "kts":
$wspeed=round($wspeed*1.9438,0);
$wunit="kts";
break;
}
pdebug(2,"End of windstr ()");
return $wspeed." ".$wunit;
}
function get_wpf_opts($wpfcid)
{
pdebug(1,"Start of get_wpf_opts ($wpfcid)");
$av=array();
$opt = get_option("wp-forecast-opts".$wpfcid);
if (! empty($opt))
{
// get widget options from database
$av=unserialize($opt);
}
else if (get_option("wp-forecast-location".$wpfcid) !="" )
{
// get old widget options from database
$av['service'] = get_option("wp-forecast-service".$wpfcid);
$av['apikey1'] = get_option("wp-forecast-apikey1".$wpfcid);
$av['apikey2'] = get_option("wp-forecast-apikey2".$wpfcid);
$av['location'] = get_option("wp-forecast-location".$wpfcid);
$av['locname'] = get_option("wp-forecast-locname".$wpfcid);
$av['refresh'] = get_option("wp-forecast-refresh".$wpfcid);
$av['metric'] = get_option("wp-forecast-metric".$wpfcid);
$av['wpf_language'] = get_option("wp-forecast-language".$wpfcid);
$av['daytime'] = get_option("wp-forecast-daytime".$wpfcid);
$av['nighttime'] = get_option("wp-forecast-nighttime".$wpfcid);
$av['dispconfig'] = get_option("wp-forecast-dispconfig".$wpfcid);
$av['windunit'] = get_option("wp-forecast-windunit".$wpfcid);
$av['currtime'] = get_option("wp-forecast-currtime".$wpfcid);
$av['title'] = get_option("wp-forecast-title".$wpfcid);
// replace old options by new one row option
add_option("wp-forecast-opts".$wpfcid,serialize($av));
// remove old options from database
delete_option("wp-forecast-location".$wpfcid);
delete_option("wp-forecast-locname".$wpfcid);
delete_option("wp-forecast-refresh".$wpfcid);
delete_option("wp-forecast-metric".$wpfcid);
delete_option("wp-forecast-language".$wpfcid);
delete_option("wp-forecast-daytime".$wpfcid);
delete_option("wp-forecast-nighttime".$wpfcid);
delete_option("wp-forecast-dispconfig".$wpfcid);
delete_option("wp-forecast-windunit".$wpfcid);
delete_option("wp-forecast-currtime".$wpfcid);
delete_option("wp-forecast-title".$wpfcid);
delete_option("wp-forecast-service".$wpfcid);
delete_option("wp-forecast-apikey1".$wpfcid);
delete_option("wp-forecast-apikey2".$wpfcid);
}
else
{
$av=array();
}
// add expire options
$av['expire']=get_option("wp-forecast-expire".$wpfcid);
// add generic options
$av['fc_date_format']=get_option("date_format");
$av['fc_time_format']=get_option("time_format");
$av['xmlerror']="";
// set static uris for each provider
//$av['BASE_URI']="http://host1/accuweather/weather_data.php?"; // for testing
$av['ACCU_LOC_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/locate_city.asp?location=";
$av['ACCU_BASE_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/weather_data.asp?";
$av['BUG_LOC_URI']="http://#apicode#.api.wxbug.net/getLocationsXML.aspx?ACode=#apicode#&SearchString=";
//$av['BUG_STAT_URI']="http://#apicode#.api.wxbug.net/getStationsXML.aspx?ACode=#apicode#&cityCode=";
$av['BUG_BASE_URI']="http://#apicode#.api.wxbug.net/getLiveWeatherRSS.aspx?ACode=#apicode#&cityCode=";
$av['BUG_FORC_URI']="http://#apicode#.api.wxbug.net/getForecastRSS.aspx?ACode=#apicode#&cityCode=";
pdebug(1,"End of get_wpf_opts ()");
return $av;
}
//
// build the url from the parameters and fetch the weather-data
// return it as one long string
//
function get_weather($uri,$loc,$metric)
{
pdebug(1,"Start of get_weather ()");
$url=$uri . "location=" . urlencode($loc) . "&metric=" .
$metric;
$xml = fetchURL($url);
pdebug(1,"End of get_weather ()");
return $xml;
}
//
// just return the css link
// this function is called via the wp_head hook
//
function wp_forecast_css($wpfcid="A")
{
pdebug(1,"Start of function wp_forecast_css ()");
$def = "wp-forecast-default.css";
$user = "wp-forecast.css";
if (file_exists( WP_PLUGIN_DIR . "/wp-forecast/" . $user))
$def =$user;
$plugin_url = plugins_url("wp-forecast/");
echo '' ."\n";
pdebug(1,"End of function wp_forecast_css ()");
}
//
// just return the css link when not using wordpress
// this function is called when showing widget directly via wp-forecast-show.php
//
function wp_forecast_css_nowp($wpfcid="A")
{
pdebug(1,"Start of function wp_forecast_css_nowp ()");
$def = "wp-forecast-default-nowp.css";
$user = "wp-forecast-nowp.css";
if (file_exists( WP_PLUGIN_DIR . "/wp-forecast/" . $user))
$def =$user;
$plugin_url = plugins_url("wp-forecast/");
echo '' ."\n";
pdebug(1,"End of function wp_forecast_css_nowp ()");
}
//
// little debug output routine
//
function pdebug($level,$dstr)
{
global $wpf_debug;
if ($wpf_debug >= $level)
echo $dstr."
\n";
}
//
// returns the number's widget id used with wp-forecast
// maximum is 999999 :-)
//
function get_widget_id($number)
{
// if negative take the first id
if ($number < 0 )
return "A";
// the first widgets use chars above we go with 0 padded numbers
if ( $number <= 25 )
return substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",$number,1);
else
return str_pad($number, 6, "0", STR_PAD_LEFT);
}
//
// function tries to determine the icon path for icon number ino
//
function find_icon($ino)
{
$path = ABSPATH . "wp-content/plugins/wp-forecast/icons/".$ino;
$ext=".gif";
if ( file_exists($path.".gif") )
$ext= ".gif";
else if ( file_exists($path.".png") )
$ext= ".png";
else if ( file_exists($path.".jpg") )
$ext= ".jpg";
else if ( file_exists($path.".GIF") )
$ext= ".GIF";
else if ( file_exists($path.".PNG") )
$ext= ".PNG";
else if ( file_exists($path.".JPG") )
$ext= ".JPG";
else if ( file_exists($path.".jpeg") )
$ext= ".jpeg";
else if ( file_exists($path.".JPEG") )
$ext= ".JPEG";
return $ino . $ext;
}
function translate_winddir($wdir,$tdom)
{
// translate winddir char by char
$winddir="";
for ($i=0;$i