| 1 |
<?php |
| 2 |
/* This file is part of the wp-forecast plugin for wordpress */ |
| 3 |
|
| 4 |
/* Copyright 2006-2009 Hans Matzen (email : webmaster at tuxlog dot 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('bug_xml_parser')) |
| 22 |
{ |
| 23 |
global $wpf_weather,$wpf_pstack,$wpf_go_ahead,$wpf_fc_daynumber; |
| 24 |
|
| 25 |
// |
| 26 |
// build the url from the parameters and fetch the weather-data |
| 27 |
// return it as one long string |
| 28 |
// |
| 29 |
function bug_get_weather($uri,$apikey,$loc,$metric) |
| 30 |
{ |
| 31 |
pdebug(1,"Start of bug_get_weather ()"); |
| 32 |
|
| 33 |
$uri=str_replace('#apicode#',$apikey,$uri); |
| 34 |
$url=$uri . urlencode($loc) . "&UnitType=" . $metric . "&OutputType=1"; |
| 35 |
$xml = fetchURL($url); |
| 36 |
|
| 37 |
pdebug(1,"End of bug_get_weather ()"); |
| 38 |
|
| 39 |
return $xml; |
| 40 |
} |
| 41 |
|
| 42 |
// start_element() - called for every start tag |
| 43 |
function bug_start_element( $parser, $name, $attribute ) |
| 44 |
{ |
| 45 |
global $wpf_pstack,$wpf_go_ahead,$wpf_fc_daynumber,$wpf_weather; |
| 46 |
|
| 47 |
pdebug(2,"Start of start_element ()"); |
| 48 |
|
| 49 |
$wpf_path_table = |
| 50 |
array( |
| 51 |
"/AWS:WEATHER/AWS:OB/AWS:STATION" => "city", |
| 52 |
"/AWS:WEATHER/AWS:OB/AWS:COUNTRY" => "state", |
| 53 |
"/AWS:WEATHER/AWS:INPUTLOCATIONURL" => "servicelink", |
| 54 |
"/AWS:WEATHER/AWS:OB/AWS:OB-DATE" => "time", |
| 55 |
"/AWS:WEATHER/AWS:OB/AWS:LATITUDE" => "lat", |
| 56 |
"/AWS:WEATHER/AWS:OB/AWS:LONGITUDE" => "lon", |
| 57 |
"/AWS:WEATHER/AWS:OB/AWS:PRESSURE" => "pressure", |
| 58 |
"/AWS:WEATHER/AWS:OB/AWS:TEMP" => "temperature", |
| 59 |
"/AWS:WEATHER/AWS:OB/AWS:FEELS-LIKE" => "realfeel", |
| 60 |
"/AWS:WEATHER/AWS:OB/AWS:HUMIDITY" => "humidity", |
| 61 |
"/AWS:WEATHER/AWS:OB/AWS:CURRENT-CONDITION" => "weathertext", |
| 62 |
"/AWS:WEATHER/AWS:OB/AWS:WIND-SPEED" => "windspeed", |
| 63 |
"/AWS:WEATHER/AWS:OB/AWS:WIND-DIRECTION" => "winddirection", |
| 64 |
"/AWS:WEATHER/AWS:OB/AWS:GUST-SPEED" => "wgusts", |
| 65 |
"/AWS:WEATHER/AWS:OB/AWS:GUST-DIRECTION" => "wgustsdirection", |
| 66 |
"/AWS:WEATHER/AWS:FORECASTS/DAY/OBSDATE" => "fc_obsdate", |
| 67 |
"/AWS:WEATHER/AWS:FORECASTS/AWS:FORECAST/AWS:SHORT-PREDICTION" => "fc_dt_short", |
| 68 |
"/AWS:WEATHER/AWS:FORECASTS/AWS:FORECAST/AWS:IMAGE" => "fc_dt_icon", |
| 69 |
"/AWS:WEATHER/AWS:FORECASTS/AWS:FORECAST/AWS:HIGH" => "fc_dt_htemp", |
| 70 |
"/AWS:WEATHER/AWS:FORECASTS/AWS:FORECAST/AWS:LOW" => "fc_dt_ltemp", |
| 71 |
"/AWS:ERROR" => "failure" |
| 72 |
); |
| 73 |
// path adjustment |
| 74 |
$wpf_pstack .= "/$name"; |
| 75 |
|
| 76 |
if (isset( $wpf_path_table[ $wpf_pstack])) { |
| 77 |
$wpf_go_ahead = $wpf_path_table[$wpf_pstack]; |
| 78 |
|
| 79 |
if ($wpf_fc_daynumber != "0" and $wpf_fc_daynumber != "") |
| 80 |
$wpf_go_ahead = $wpf_go_ahead . "_" . $wpf_fc_daynumber; |
| 81 |
} |
| 82 |
// for forecast days |
| 83 |
if ($wpf_pstack=="/AWS:WEATHER/AWS:FORECASTS/AWS:FORECAST") |
| 84 |
$wpf_fc_daynumber++; |
| 85 |
|
| 86 |
|
| 87 |
switch ( $wpf_pstack ) { |
| 88 |
// unit of temperature |
| 89 |
case "/AWS:WEATHER/AWS:OB/AWS:TEMP": |
| 90 |
$wpf_weather["un_temp"]=$attribute["UNITS"]; |
| 91 |
break; |
| 92 |
// unit of speed |
| 93 |
case "/AWS:WEATHER/AWS:OB/AWS:GUST-SPEED": |
| 94 |
$wpf_weather["un_speed"]=$attribute["UNITS"]; |
| 95 |
break; |
| 96 |
// unit of pressure |
| 97 |
case "/AWS:WEATHER/AWS:OB/AWS:PRESSURE": |
| 98 |
$wpf_weather["un_pres"]=$attribute["UNITS"]; |
| 99 |
break; |
| 100 |
// get observation date |
| 101 |
case "/AWS:WEATHER/AWS:OB/AWS:OB-DATE/AWS:YEAR": |
| 102 |
$wpf_weather["time_year"]=$attribute["NUMBER"]; |
| 103 |
break; |
| 104 |
case "/AWS:WEATHER/AWS:OB/AWS:OB-DATE/AWS:MONTH": |
| 105 |
$wpf_weather["time_month"]=$attribute["NUMBER"]; |
| 106 |
break; |
| 107 |
case "/AWS:WEATHER/AWS:OB/AWS:OB-DATE/AWS:DAY": |
| 108 |
$wpf_weather["time_day"]=$attribute["NUMBER"]; |
| 109 |
break; |
| 110 |
case "/AWS:WEATHER/AWS:OB/AWS:OB-DATE/AWS:HOUR": |
| 111 |
$wpf_weather["time_hour"]=$attribute["HOUR-24"]; |
| 112 |
break; |
| 113 |
case "/AWS:WEATHER/AWS:OB/AWS:OB-DATE/AWS:MINUTE": |
| 114 |
$wpf_weather["time_minute"]=$attribute["NUMBER"]; |
| 115 |
break; |
| 116 |
// get sunrise |
| 117 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNRISE/AWS:YEAR": |
| 118 |
$wpf_weather["sunrise_year"]=$attribute["NUMBER"]; |
| 119 |
break; |
| 120 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNRISE/AWS:MONTH": |
| 121 |
$wpf_weather["sunrise_month"]=$attribute["NUMBER"]; |
| 122 |
break; |
| 123 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNRISE/AWS:DAY": |
| 124 |
$wpf_weather["sunrise_day"]=$attribute["NUMBER"]; |
| 125 |
break; |
| 126 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNRISE/AWS:HOUR": |
| 127 |
$wpf_weather["sunrise_hour"]=$attribute["HOUR-24"]; |
| 128 |
break; |
| 129 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNRISE/AWS:MINUTE": |
| 130 |
$wpf_weather["sunrise_minute"]=$attribute["NUMBER"]; |
| 131 |
break; |
| 132 |
// get sunset |
| 133 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNSET/AWS:YEAR": |
| 134 |
$wpf_weather["sunset_year"]=$attribute["NUMBER"]; |
| 135 |
break; |
| 136 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNSET/AWS:MONTH": |
| 137 |
$wpf_weather["sunset_month"]=$attribute["NUMBER"]; |
| 138 |
break; |
| 139 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNSET/AWS:DAY": |
| 140 |
$wpf_weather["sunset_day"]=$attribute["NUMBER"]; |
| 141 |
break; |
| 142 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNSET/AWS:HOUR": |
| 143 |
$wpf_weather["sunset_hour"]=$attribute["HOUR-24"]; |
| 144 |
break; |
| 145 |
case "/AWS:WEATHER/AWS:OB/AWS:SUNSET/AWS:MINUTE": |
| 146 |
$wpf_weather["sunset_minute"]=$attribute["NUMBER"]; |
| 147 |
break; |
| 148 |
case "/AWS:WEATHER/AWS:OB/AWS:CURRENT-CONDITION": |
| 149 |
$wpf_weather["weathericon"] = $attribute['ICON']; |
| 150 |
} |
| 151 |
pdebug(2,"End of bug_start_element ()"); |
| 152 |
} |
| 153 |
|
| 154 |
// end_element() - called for every end tag |
| 155 |
function bug_end_element( $parser, $name ) |
| 156 |
{ |
| 157 |
global $wpf_pstack,$wpf_fc_daynumber; |
| 158 |
pdebug(2,"Start of bug_end_element ()"); |
| 159 |
// reduce xml path stack |
| 160 |
$wpf_pstack = substr($wpf_pstack,0, strrpos($wpf_pstack,"/")); |
| 161 |
pdebug(2,"End of bug_end_element ()"); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
// daten() - called for everey cdata |
| 166 |
function bug_daten( $parser, $data ) |
| 167 |
{ |
| 168 |
global $wpf_weather,$wpf_go_ahead; |
| 169 |
pdebug(2,"Start of bug_daten ()"); |
| 170 |
if ( strlen($wpf_go_ahead) > 0 ) |
| 171 |
{ |
| 172 |
$wpf_weather[$wpf_go_ahead]=$data; |
| 173 |
$wpf_go_ahead = ''; |
| 174 |
} |
| 175 |
pdebug(2,"End of bug_daten ()"); |
| 176 |
} |
| 177 |
|
| 178 |
// |
| 179 |
// parses the xml for the paths in path_tabelle |
| 180 |
// |
| 181 |
|
| 182 |
function bug_xml_parser($xmlstring) { |
| 183 |
global $wpf_weather,$wpf_pstack,$wpf_go_ahead,$wpf_fc_daynumber; |
| 184 |
pdebug(1,"Start of bug_xml_parser ()"); |
| 185 |
|
| 186 |
$wpf_weather=array(); |
| 187 |
$xmlerror=""; |
| 188 |
|
| 189 |
$wpf_pstack=""; |
| 190 |
$wpf_go_ahead = ""; |
| 191 |
$wpf_fc_daynumber="0"; |
| 192 |
|
| 193 |
// create an xml parser object |
| 194 |
$parser = xml_parser_create(); |
| 195 |
|
| 196 |
// set parameters for xml parser |
| 197 |
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, true ); |
| 198 |
|
| 199 |
// set handler for start and end-tags |
| 200 |
xml_set_element_handler( $parser,"bug_start_element","bug_end_element"); |
| 201 |
// set handler for CDATA |
| 202 |
xml_set_character_data_handler( $parser,"bug_daten"); |
| 203 |
|
| 204 |
// try to parse the xml |
| 205 |
if( !xml_parse( $parser, $xmlstring,true ) ) |
| 206 |
{ |
| 207 |
// Error --> stop execution |
| 208 |
$xmlerror="XML Fehler: " . |
| 209 |
xml_error_string( xml_get_error_code( $parser ) ) . " in Zeile " . |
| 210 |
xml_get_current_line_number( $parser ) |
| 211 |
; |
| 212 |
} |
| 213 |
|
| 214 |
// Vom XML-Parser belegten Speicher freigeben |
| 215 |
xml_parser_free( $parser ); |
| 216 |
|
| 217 |
// check for error |
| 218 |
if ($xmlerror!="") |
| 219 |
{ |
| 220 |
$wpf_weather=array(); |
| 221 |
$wpf_weather['failure'] = $xmlerror . " (" . |
| 222 |
(trim($xmlstring) !="" ? $xmlstring : __('Location does not exist') ) . ")"; |
| 223 |
} else { |
| 224 |
// neutralize data from weatherbug |
| 225 |
// obs time/date |
| 226 |
$wpf_weather['time'] = $wpf_weather['time_month']."/".$wpf_weather['time_day']."/".$wpf_weather['time_year']." ".$wpf_weather['time_hour'].":".$wpf_weather['time_minute']; |
| 227 |
unset($wpf_weather['time_month']); |
| 228 |
unset($wpf_weather['time_day']); |
| 229 |
unset($wpf_weather['time_year']); |
| 230 |
unset($wpf_weather['time_hour']); |
| 231 |
unset($wpf_weather['time_minute']); |
| 232 |
// sunset time/date |
| 233 |
$wpf_weather['sunset'] = $wpf_weather['sunset_month']."/".$wpf_weather['sunset_day']."/".$wpf_weather['sunset_year']." ".$wpf_weather['sunset_hour'].":".$wpf_weather['sunset_minute']; |
| 234 |
unset($wpf_weather['sunset_month']); |
| 235 |
unset($wpf_weather['sunset_day']); |
| 236 |
unset($wpf_weather['sunset_year']); |
| 237 |
unset($wpf_weather['sunset_hour']); |
| 238 |
unset($wpf_weather['sunset_minute']); |
| 239 |
// sunsrise time/date |
| 240 |
$wpf_weather['sunrise'] = $wpf_weather['sunrise_month']."/".$wpf_weather['sunrise_day']."/".$wpf_weather['sunrise_year']." ".$wpf_weather['sunrise_hour'].":".$wpf_weather['sunrise_minute']; |
| 241 |
unset($wpf_weather['sunrise_month']); |
| 242 |
unset($wpf_weather['sunrise_day']); |
| 243 |
unset($wpf_weather['sunrise_year']); |
| 244 |
unset($wpf_weather['sunrise_hour']); |
| 245 |
unset($wpf_weather['sunrise_minute']); |
| 246 |
|
| 247 |
} |
| 248 |
pdebug(1,"End of bug_xml_parser ()"); |
| 249 |
// and return result, empty array if error |
| 250 |
return $wpf_weather; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
// |
| 255 |
// parse xml and extract locations as an array |
| 256 |
// for later us in the admin form |
| 257 |
// |
| 258 |
function bug_get_locations($xml) |
| 259 |
{ |
| 260 |
pdebug(1,"Start of bug_get_locations ()"); |
| 261 |
|
| 262 |
// start_element() - wird vom XML-Parser bei öffnenden Tags aufgerufen |
| 263 |
function s_element( $parser, $name, $attribute ) |
| 264 |
{ |
| 265 |
global $loc,$i; |
| 266 |
if ($name == "AWS:LOCATION") { |
| 267 |
$loc[$i]=array(); |
| 268 |
$loc[$i]['city'] = $attribute['CITYNAME']; |
| 269 |
$loc[$i]['state'] = $attribute['COUNTRYNAME']; |
| 270 |
if ($attribute['CITYTYPE']==0) |
| 271 |
$loc[$i]['location'] = $attribute['ZIPCODE']; |
| 272 |
else |
| 273 |
$loc[$i]['location'] = $attribute['CITYCODE']; |
| 274 |
$i++; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
// end_element() - dummy function |
| 279 |
function e_element( $parser, $name ){} |
| 280 |
|
| 281 |
// Instanz des XML-Parsers erzeugen |
| 282 |
$parser = xml_parser_create(); |
| 283 |
|
| 284 |
// Parameter des XML-Parsers setzen |
| 285 |
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, true ); |
| 286 |
|
| 287 |
// Handler für Elemente ( öffnende / schließende Tags ) setzen |
| 288 |
xml_set_element_handler( $parser, "s_element", "e_element" ); |
| 289 |
|
| 290 |
// try to parse the xml |
| 291 |
if( !xml_parse( $parser, $xml,true ) ) |
| 292 |
{ |
| 293 |
// Fehler -> Ausführung abbrechen |
| 294 |
die( "XML Fehler: " . |
| 295 |
xml_error_string( xml_get_error_code( $parser ) ) . |
| 296 |
" in Zeile " . |
| 297 |
xml_get_current_line_number( $parser ) |
| 298 |
); |
| 299 |
} |
| 300 |
|
| 301 |
// Vom XML-Parser belegten Speicher freigeben |
| 302 |
xml_parser_free( $parser ); |
| 303 |
|
| 304 |
pdebug(1,"End of bug_get_locations ()"); |
| 305 |
|
| 306 |
// return locations |
| 307 |
return $loc; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
// |
| 312 |
// returns the widget data as an array one line per item |
| 313 |
// |
| 314 |
function bug_forecast_data($wpfcid="A", $language_override=null) |
| 315 |
{ |
| 316 |
pdebug(1,"Start of function bug_forecast_data ()"); |
| 317 |
|
| 318 |
$wpf_vars=get_wpf_opts($wpfcid); |
| 319 |
if (!empty($language_override)) { |
| 320 |
$wpf_vars['wpf_language']=$language_override; |
| 321 |
} |
| 322 |
|
| 323 |
extract($wpf_vars); |
| 324 |
$w=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid)); |
| 325 |
|
| 326 |
// get translations |
| 327 |
if(function_exists('load_textdomain')) { |
| 328 |
global $l10n; |
| 329 |
if (!isset($l10n["wp-forecast_".$wpf_language])) |
| 330 |
load_textdomain("wp-forecast_".$wpf_language, ABSPATH . "wp-content/plugins/wp-forecast/lang/".$wpf_language.".mo"); |
| 331 |
} |
| 332 |
|
| 333 |
$weather_arr=array(); |
| 334 |
|
| 335 |
// -------------------------------------------------------------- |
| 336 |
// calc values for current conditions |
| 337 |
if ( ! isset($w['failure'])) { |
| 338 |
$weather_arr['servicelink']= $w['servicelink']; |
| 339 |
$weather_arr['location'] = $locname; |
| 340 |
$weather_arr['locname']= $w["city"]." ".$w["state"]; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
$ct = time(); // this is the GMT |
| 345 |
$ct = $ct + $wpf_vars['timeoffset'] * 60; // add or subtract time offset |
| 346 |
$weather_arr['blogdate']=date_i18n($fc_date_format, $ct); |
| 347 |
$weather_arr['blogtime']=date_i18n($fc_time_format, $ct); |
| 348 |
|
| 349 |
$cts = $w['time']; |
| 350 |
$gmtoffset=0; // wpf_get_option("gmt_offset"); |
| 351 |
$ct = strtotime($cts) + ($gmtoffset * 3600); |
| 352 |
|
| 353 |
$weather_arr['bugdate']=date_i18n($fc_date_format, $ct); |
| 354 |
$weather_arr['bugtime']=date_i18n($fc_time_format, $ct); |
| 355 |
|
| 356 |
|
| 357 |
$weather_arr['icon']=$w["weathericon"]; |
| 358 |
$weather_arr['iconcode']=""; |
| 359 |
if ( trim($w["weathertext"]) !="") |
| 360 |
$weather_arr['shorttext']= __($w["weathertext"],"wp-forecast_".$wpf_language); |
| 361 |
else |
| 362 |
$weather_arr['shorttext']= __("Unknown","wp-forecast"); |
| 363 |
|
| 364 |
$weather_arr['temperature']=$w["temperature"]. $w['un_temp']; |
| 365 |
$weather_arr['realfeel']=$w["realfeel"].$w['un_temp']; |
| 366 |
$weather_arr['pressure']=round($w["pressure"],0)." ".$w["un_pres"]; |
| 367 |
$weather_arr['humidity']=round($w["humidity"],0); |
| 368 |
$weather_arr['windspeed']=windstr($metric,$w["windspeed"],$windunit); |
| 369 |
$weather_arr['winddir']=translate_winddir($w["winddirection"],"wp-forecast_".$wpf_language); |
| 370 |
$weather_arr['windgusts']=windstr($metric,$w["wgusts"],$windunit); |
| 371 |
list($dummy, $weather_arr['sunrise']) = split(" ",$w['sunrise'],2); |
| 372 |
list($dummy, $weather_arr['sunset'] ) = split(" ",$w['sunset'] ,2); |
| 373 |
$weather_arr['copyright']='<a href="http://www.weatherbug.com">© 2010 WeatherBug</a>'; |
| 374 |
|
| 375 |
// additional info |
| 376 |
$weather_arr['lat']=$w['lat']; |
| 377 |
$weather_arr['lon']=$w['lon']; |
| 378 |
|
| 379 |
// calc values for forecast |
| 380 |
for ($i = 1; $i < 7; $i++) { |
| 381 |
// forecast |
| 382 |
$bt = strtotime($w['time']); |
| 383 |
$weather_arr['fc_obsdate_'.$i]= date_i18n($fc_date_format, $bt + ( $i * 3600 *24)); |
| 384 |
$weather_arr["fc_dt_icon_".$i]=$w["fc_dt_icon_".$i]; |
| 385 |
$weather_arr["fc_dt_iconcode_".$i]=""; |
| 386 |
if (trim($w["fc_dt_short_".$i]) !="") |
| 387 |
$weather_arr["fc_dt_desc_".$i]= __($w["fc_dt_short_".$i],"wp-forecast_".$wpf_language); |
| 388 |
else |
| 389 |
$weather_arr["fc_dt_desc_".$i]= __("Unknown","wp-forecast"); |
| 390 |
|
| 391 |
$weather_arr["fc_dt_htemp_".$i]= $w["fc_dt_htemp_".$i].$w['un_temp']; |
| 392 |
$weather_arr["fc_dt_ltemp_".$i]= $w["fc_dt_ltemp_".$i].$w['un_temp']; |
| 393 |
|
| 394 |
} |
| 395 |
} |
| 396 |
// fill failure anyway |
| 397 |
$weather_arr['failure']=( isset($w['failure']) ? $w['failure'] : ''); |
| 398 |
|
| 399 |
pdebug(1,"End of function bug_forecast_data ()"); |
| 400 |
|
| 401 |
return $weather_arr; |
| 402 |
} |
| 403 |
?> |