$timeout, 'decompress' => false, '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"; } } // workaround for bug in decompress function, class wp_http in wp 2.9 $derg = @gzinflate($erg); if ($derg !== false) $erg = $derg; 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; case "bft": $wbft = 0; $bft = array(0.3, 1.6, 3.4, 5.5, 8.0, 10.8, 13.9, 17.2, 20.8, 24.5, 28.5, 32.7); foreach($bft as $b) { if ($wspeed < $b) { $wbft--; break; } $wbft++; } $wunit="bft"; $wspeed = $wbft; break; } pdebug(2,"End of windstr ()"); return $wspeed." ".$wunit; } // // wrapper to make sure the correct option is used whether multisite or wp is used // only wraps main parameters and pass through all others // function wpf_get_option($name) { global $blog_id; if ( !function_exists("is_multisite") || ! is_multisite() || $blog_id==1 ) return get_option($name); else { // this is the multisite part if ( $name == "wpf_sa_defaults" or $name == "wpf_sa_allowed" ) return get_blog_option(1, $name); else return get_blog_option($blog_id, $name); } } // // wrapper for update option to hide differences between wp and wpmu // // function wpf_update_option($name, $value) { global $blog_id; if ( !function_exists("is_multisite") || ! is_multisite() || $blog_id==1) update_option($name, $value); else update_blog_option($blog_id, $name, $value); } // // wrapper for add option to hide differences between wp and wpmu // // function wpf_add_option($name, $value) { global $blog_id; if ( !function_exists("is_multisite") || ! is_multisite() || $blog_id==1 ) add_option($name, $value); else add_blog_option($blog_id, $name, $value); } // // reads all wp-forecast options and returns an array // function get_wpf_opts($wpfcid) { pdebug(1,"Start of get_wpf_opts ($wpfcid)"); global $blog_id; $av=array(); $opt = wpf_get_option("wp-forecast-opts".$wpfcid); if (! empty($opt)) { // unpack if necessary $av=maybe_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['timeoffset'] = get_option("wp-forecast-timeoffset".$wpfcid); $av['title'] = get_option("wp-forecast-title".$wpfcid); // replace old options by new one row option wpf_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="; $av['GOOGLE_LOC_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/locate_city.asp?location="; $av['GOOGLE_BASE_URI']="http://www.google.com/ig/api?"; // if we use multisite then merge admin options if ( function_exists("is_multisite") && is_multisite() && $blog_id !=1) { // read defaults and allowed fields $defaults = maybe_unserialize(wpf_get_option("wpf_sa_defaults")); $allowed = maybe_unserialize(wpf_get_option("wpf_sa_allowed")); // in case allowed is still empty if (!$allowed) $allowed=array(); // set wpf_maxwidgets for users global $blog_id, $wpf_maxwidgets; if ($blog_id > "1" and isset($defaults["wp-forecast-count"])) $wpf_maxwidgets = $defaults["wp-forecast-count"]; // map rest of fields foreach($allowed as $f => $fswitch) { $fname = substr($f,3); // strip ue_ prefix if ( $fswitch != "1" or ! isset($av[ $fname ]) ) { // replace value in av with forced default if (array_key_exists($fname, $defaults)) $av[ $fname ] = $defaults[$fname]; } } } 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 // if (! function_exists("pdebug") ) { 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