0)
pdebug("Start of fetchURL ()");
// get timeout parameter
$timeout = get_option("wp-forecast-timeout");
if ( $timeout =="")
$timeout = 30;
$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";
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
// set timeout for reading
stream_set_timeout($fp, $timeout);
$in = "";
if ($fp) {
fwrite($fp, $out);
$body = false;
while (!feof($fp)) {
$s = fgets($fp, 1024);
if ($body) $in .= $s;
if ($s == "\r\n") $body = true;
}
fclose($fp);
}
if ($wpf_debug > 0)
pdebug("End of fetchURL ()");
return $in;
}
//
// converts an array to an cookie like string
// e.g. a=5&b=6&c=7
function arr2str($a)
{
global $wpf_debug;
if ($wpf_debug > 0)
pdebug("Start of arr2str ()");
$s="";
foreach ($a as $name => $value){
$s=$s . $name . "=" . $value . "&";
}
if ($wpf_debug > 0)
pdebug("End of arr2str ()");
return $s;
}
//
// converts a cookie like string (see above) to an array
//
function str2arr($s)
{
global $wpf_debug;
if ($wpf_debug > 0)
pdebug("Start of str2arr ()");
$a=array();
parse_str($s,$a);
if ($wpf_debug > 0)
pdebug("End of str2arr ()");
return $a;
}
//
// converts the given wind parameters into a suitable windstring
//
function windstr($metric,$wspeed,$windunit)
{
global $wpf_debug;
if ($wpf_debug > 0)
pdebug("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;
}
if ($wpf_debug > 0)
pdebug("End of windstr ()");
return $wspeed." ".$wunit;
}
function get_wpf_opts($wpfcid)
{
global $wpf_debug;
if ($wpf_debug > 0)
pdebug("Start of get_wpf_opts ()");
$av=array();
// get options from database
$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['fc_date_format']=get_option("date_format");
$av['fc_time_format']=get_option("time_format");
$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);
$av['xmlerror']="";
// set accuweather uri
$av['BASE_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/weather_data.asp?";
if ($wpf_debug > 0)
pdebug("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)
{
global $wpf_debug;
if ($wpf_debug > 0)
pdebug("Start of get_weather ()");
$url=$uri . "location=" . urlencode($loc) . "&metric=" .
$metric;// . "&partner=forecastfox";
$xml = fetchURL($url);
if ($wpf_debug > 0)
pdebug("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") {
global $wpf_debug;
if ($wpf_debug > 0)
pdebug("Start of function wp_forecast_css ()");
$plugin_path = get_settings('siteurl') . '/wp-content/plugins/wp-forecast';
echo "\n";
if ($wpf_debug > 0)
pdebug("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") {
global $wpf_debug;
if ($wpf_debug > 0)
pdebug("Start of function wp_forecast_css_nowp ()");
$plugin_path = get_settings('siteurl') . '/wp-content/plugins/wp-forecast';
echo "\n";
if ($wpf_debug > 0)
pdebug("End of function wp_forecast_css ()");
}
//
// little debug output routine
//
function pdebug($dstr)
{
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);
}
}
?>