| 1 |
<?php |
| 2 |
/* This file is part of the wp-forecast plugin for wordpress */ |
| 3 |
|
| 4 |
/* Copyright 2006,2007,2008 Hans Matzen (email : webmaster at tuxlog.de) |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software |
| 18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
*/ |
| 20 |
|
| 21 |
if (!function_exists('fetchURL')) { |
| 22 |
|
| 23 |
// |
| 24 |
// this function fetches an url an returns it as one whole string |
| 25 |
// |
| 26 |
function fetchURL($url) |
| 27 |
{ |
| 28 |
global $wpf_debug; |
| 29 |
|
| 30 |
if ($wpf_debug > 0) |
| 31 |
pdebug("Start of fetchURL ()"); |
| 32 |
|
| 33 |
// get timeout parameter |
| 34 |
$timeout = get_option("wp-forecast-timeout"); |
| 35 |
if ( $timeout =="") |
| 36 |
$timeout = 30; |
| 37 |
|
| 38 |
$url_parsed = parse_url($url); |
| 39 |
$host = $url_parsed["host"]; |
| 40 |
if (!isset($url_parsed["port"])) { |
| 41 |
$port = 80; |
| 42 |
} |
| 43 |
else { |
| 44 |
$port = $url_parsed["port"]; |
| 45 |
} |
| 46 |
$path = $url_parsed["path"]; |
| 47 |
if ($url_parsed["query"] != "") $path .= "?" . $url_parsed["query"]; |
| 48 |
$out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n"; |
| 49 |
// open connection |
| 50 |
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout); |
| 51 |
|
| 52 |
$in = ""; |
| 53 |
if ($fp) { |
| 54 |
// set timeout for reading |
| 55 |
stream_set_timeout($fp, $timeout); |
| 56 |
// send request |
| 57 |
fwrite($fp, $out); |
| 58 |
$body = false; |
| 59 |
// read answer |
| 60 |
while (!feof($fp)) { |
| 61 |
$s = fgets($fp, 1024); |
| 62 |
if ($body) $in .= $s; |
| 63 |
if ($s == "\r\n") $body = true; |
| 64 |
} |
| 65 |
// close connection |
| 66 |
fclose($fp); |
| 67 |
} else { |
| 68 |
// error handling |
| 69 |
echo '<!-- Connection-Error, Error-No.: ' . $errno . ' >> ' . $errstr . "-->\n"; |
| 70 |
} |
| 71 |
|
| 72 |
if ($wpf_debug > 0) |
| 73 |
pdebug("End of fetchURL ()"); |
| 74 |
|
| 75 |
return $in; |
| 76 |
} |
| 77 |
|
| 78 |
// |
| 79 |
// converts an array to an cookie like string |
| 80 |
// e.g. a=5&b=6&c=7 |
| 81 |
function arr2str($a) |
| 82 |
{ |
| 83 |
global $wpf_debug; |
| 84 |
|
| 85 |
if ($wpf_debug > 0) |
| 86 |
pdebug("Start of arr2str ()"); |
| 87 |
|
| 88 |
$s=""; |
| 89 |
foreach ($a as $name => $value){ |
| 90 |
$s=$s . $name . "=" . $value . "&"; |
| 91 |
} |
| 92 |
|
| 93 |
if ($wpf_debug > 0) |
| 94 |
pdebug("End of arr2str ()"); |
| 95 |
|
| 96 |
return $s; |
| 97 |
} |
| 98 |
|
| 99 |
// |
| 100 |
// converts a cookie like string (see above) to an array |
| 101 |
// |
| 102 |
function str2arr($s) |
| 103 |
{ |
| 104 |
global $wpf_debug; |
| 105 |
|
| 106 |
if ($wpf_debug > 0) |
| 107 |
pdebug("Start of str2arr ()"); |
| 108 |
|
| 109 |
$a=array(); |
| 110 |
parse_str($s,$a); |
| 111 |
|
| 112 |
if ($wpf_debug > 0) |
| 113 |
pdebug("End of str2arr ()"); |
| 114 |
|
| 115 |
return $a; |
| 116 |
} |
| 117 |
|
| 118 |
// |
| 119 |
// converts the given wind parameters into a suitable windstring |
| 120 |
// |
| 121 |
function windstr($metric,$wspeed,$windunit) |
| 122 |
{ |
| 123 |
global $wpf_debug; |
| 124 |
|
| 125 |
if ($wpf_debug > 0) |
| 126 |
pdebug("Start of windstr ()"); |
| 127 |
|
| 128 |
// if its mph convert it to m/s |
| 129 |
if ($metric != 1) |
| 130 |
$wspeed = round($wspeed * 0.44704,0); |
| 131 |
|
| 132 |
// convert it to selected unit |
| 133 |
switch ($windunit) { |
| 134 |
case "ms": |
| 135 |
$wunit="m/s"; |
| 136 |
break; |
| 137 |
case "kmh": |
| 138 |
$wspeed=round($wspeed*3.6,0); |
| 139 |
$wunit="km/h"; |
| 140 |
break; |
| 141 |
case "mph": |
| 142 |
$wspeed=round($wspeed*2.23694,0); |
| 143 |
$wunit="mph"; |
| 144 |
break; |
| 145 |
case "kts": |
| 146 |
$wspeed=round($wspeed*1.9438,0); |
| 147 |
$wunit="kts"; |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
if ($wpf_debug > 0) |
| 152 |
pdebug("End of windstr ()"); |
| 153 |
|
| 154 |
return $wspeed." ".$wunit; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
function get_wpf_opts($wpfcid) |
| 159 |
{ |
| 160 |
global $wpf_debug; |
| 161 |
|
| 162 |
if ($wpf_debug > 0) |
| 163 |
pdebug("Start of get_wpf_opts ()"); |
| 164 |
|
| 165 |
$av=array(); |
| 166 |
|
| 167 |
// get options from database |
| 168 |
$av['location']=get_option("wp-forecast-location".$wpfcid); |
| 169 |
$av['locname']=get_option("wp-forecast-locname".$wpfcid); |
| 170 |
$av['refresh']=get_option("wp-forecast-refresh".$wpfcid); |
| 171 |
$av['metric']=get_option("wp-forecast-metric".$wpfcid); |
| 172 |
$av['wpf_language']=get_option("wp-forecast-language".$wpfcid); |
| 173 |
$av['daytime']=get_option("wp-forecast-daytime".$wpfcid); |
| 174 |
$av['nighttime']=get_option("wp-forecast-nighttime".$wpfcid); |
| 175 |
$av['fc_date_format']=get_option("date_format"); |
| 176 |
$av['fc_time_format']=get_option("time_format"); |
| 177 |
$av['dispconfig']=get_option("wp-forecast-dispconfig".$wpfcid); |
| 178 |
$av['windunit']=get_option("wp-forecast-windunit".$wpfcid); |
| 179 |
$av['currtime']=get_option("wp-forecast-currtime".$wpfcid); |
| 180 |
$av['title'] = get_option("wp-forecast-title".$wpfcid); |
| 181 |
$av['xmlerror']=""; |
| 182 |
|
| 183 |
// set accuweather uri |
| 184 |
$av['BASE_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/weather_data.asp?"; |
| 185 |
|
| 186 |
if ($wpf_debug > 0) |
| 187 |
pdebug("End of get_wpf_opts ()"); |
| 188 |
|
| 189 |
return $av; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
// |
| 194 |
// build the url from the parameters and fetch the weather-data |
| 195 |
// return it as one long string |
| 196 |
// |
| 197 |
function get_weather($uri,$loc,$metric) |
| 198 |
{ |
| 199 |
global $wpf_debug; |
| 200 |
|
| 201 |
if ($wpf_debug > 0) |
| 202 |
pdebug("Start of get_weather ()"); |
| 203 |
|
| 204 |
$url=$uri . "location=" . urlencode($loc) . "&metric=" . |
| 205 |
$metric;// . "&partner=forecastfox"; |
| 206 |
|
| 207 |
$xml = fetchURL($url); |
| 208 |
|
| 209 |
if ($wpf_debug > 0) |
| 210 |
pdebug("End of get_weather ()"); |
| 211 |
|
| 212 |
return $xml; |
| 213 |
} |
| 214 |
|
| 215 |
// |
| 216 |
// just return the css link |
| 217 |
// this function is called via the wp_head hook |
| 218 |
// |
| 219 |
function wp_forecast_css($wpfcid="A") { |
| 220 |
|
| 221 |
global $wpf_debug; |
| 222 |
|
| 223 |
if ($wpf_debug > 0) |
| 224 |
pdebug("Start of function wp_forecast_css ()"); |
| 225 |
|
| 226 |
$plugin_path = get_settings('siteurl') . '/wp-content/plugins/wp-forecast'; |
| 227 |
echo "<link rel=\"stylesheet\" href=\"". $plugin_path. "/wp-forecast.css\" type=\"text/css\" media=\"screen\" />\n"; |
| 228 |
|
| 229 |
|
| 230 |
if ($wpf_debug > 0) |
| 231 |
pdebug("End of function wp_forecast_css ()"); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
// |
| 236 |
// just return the css link when not using wordpress |
| 237 |
// this function is called when showing widget directly via wp-forecast-show.php |
| 238 |
// |
| 239 |
function wp_forecast_css_nowp($wpfcid="A") { |
| 240 |
|
| 241 |
global $wpf_debug; |
| 242 |
|
| 243 |
if ($wpf_debug > 0) |
| 244 |
pdebug("Start of function wp_forecast_css_nowp ()"); |
| 245 |
|
| 246 |
$plugin_path = get_settings('siteurl') . '/wp-content/plugins/wp-forecast'; |
| 247 |
echo "<link rel=\"stylesheet\" href=\"". $plugin_path. "/wp-forecast-nowp.css\" type=\"text/css\" media=\"screen\" />\n"; |
| 248 |
|
| 249 |
|
| 250 |
if ($wpf_debug > 0) |
| 251 |
pdebug("End of function wp_forecast_css ()"); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
// |
| 256 |
// little debug output routine |
| 257 |
// |
| 258 |
function pdebug($dstr) |
| 259 |
{ |
| 260 |
echo $dstr."<br />\n"; |
| 261 |
} |
| 262 |
|
| 263 |
// |
| 264 |
// returns the number's widget id used with wp-forecast |
| 265 |
// maximum is 999999 :-) |
| 266 |
// |
| 267 |
function get_widget_id($number) |
| 268 |
{ |
| 269 |
// if negative take the first id |
| 270 |
if ($number < 0 ) |
| 271 |
return "A"; |
| 272 |
|
| 273 |
// the first widgets use chars above we go with 0 padded numbers |
| 274 |
if ( $number <= 25 ) |
| 275 |
return substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",$number,1); |
| 276 |
else |
| 277 |
return str_pad($number, 6, "0", STR_PAD_LEFT); |
| 278 |
} |
| 279 |
|
| 280 |
// |
| 281 |
// function tries to determine the icon path for icon number ino |
| 282 |
// |
| 283 |
function find_icon($ino) |
| 284 |
{ |
| 285 |
$path = ABSPATH . "wp-content/plugins/wp-forecast/icons/".$ino; |
| 286 |
$ext=".gif"; |
| 287 |
|
| 288 |
if ( file_exists($path.".gif") ) |
| 289 |
$ext= ".gif"; |
| 290 |
else if ( file_exists($path.".png") ) |
| 291 |
$ext= ".png"; |
| 292 |
else if ( file_exists($path.".jpg") ) |
| 293 |
$ext= ".jpg"; |
| 294 |
else if ( file_exists($path.".GIF") ) |
| 295 |
$ext= ".GIF"; |
| 296 |
else if ( file_exists($path.".PNG") ) |
| 297 |
$ext= ".PNG"; |
| 298 |
else if ( file_exists($path.".JPG") ) |
| 299 |
$ext= ".JPG"; |
| 300 |
else if ( file_exists($path.".jpeg") ) |
| 301 |
$ext= ".jpeg"; |
| 302 |
else if ( file_exists($path.".JPEG") ) |
| 303 |
$ext= ".JPEG"; |
| 304 |
return $ino . $ext; |
| 305 |
} |
| 306 |
|
| 307 |
} |
| 308 |
?> |
| 309 |
|