| 1 |
<?php |
| 2 |
/* This file is part of the wp-forecast plugin for wordpress */ |
| 3 |
|
| 4 |
/* Copyright 2006-2011 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('accu_xml_parser')) { |
| 22 |
global $wpf_weather,$wpf_pstack,$wpf_go_ahead,$wpf_fc_daynumber; |
| 23 |
|
| 24 |
// |
| 25 |
// build the url from the parameters and fetch the weather-data |
| 26 |
// return it as one long string |
| 27 |
// |
| 28 |
function accu_get_weather($uri,$loc,$metric) |
| 29 |
{ |
| 30 |
pdebug(1,"Start of accu_get_weather ()"); |
| 31 |
|
| 32 |
$url=$uri . "location=" . urlencode($loc) . "&metric=" . |
| 33 |
$metric; |
| 34 |
|
| 35 |
$xml = fetchURL($url); |
| 36 |
|
| 37 |
pdebug(1,"End of accu_get_weather ()"); |
| 38 |
|
| 39 |
return $xml; |
| 40 |
} |
| 41 |
|
| 42 |
// start_element() - called for every start tag |
| 43 |
function accu_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 |
"/ADC_DATABASE/UNITS/TEMP" => "un_temp", |
| 52 |
"/ADC_DATABASE/UNITS/DIST" => "un_dist", |
| 53 |
"/ADC_DATABASE/UNITS/SPEED" => "un_speed", |
| 54 |
"/ADC_DATABASE/UNITS/PRES" => "un_pres", |
| 55 |
"/ADC_DATABASE/UNITS/PREC" => "un_prec", |
| 56 |
"/ADC_DATABASE/LOCAL/CITY" => "city", |
| 57 |
"/ADC_DATABASE/LOCAL/STATE" => "state", |
| 58 |
"/ADC_DATABASE/LOCAL/TIME" => "time", |
| 59 |
"/ADC_DATABASE/LOCAL/LAT" => "lat", |
| 60 |
"/ADC_DATABASE/LOCAL/LON" => "lon", |
| 61 |
"/ADC_DATABASE/LOCAL/GMTDIFF" => "gmtdiff", |
| 62 |
"/ADC_DATABASE/CURRENTCONDITIONS/PRESSURE" => "pressure", |
| 63 |
"/ADC_DATABASE/CURRENTCONDITIONS/TEMPERATURE" => "temperature", |
| 64 |
"/ADC_DATABASE/CURRENTCONDITIONS/REALFEEL" => "realfeel", |
| 65 |
"/ADC_DATABASE/CURRENTCONDITIONS/HUMIDITY" => "humidity", |
| 66 |
"/ADC_DATABASE/CURRENTCONDITIONS/WEATHERTEXT" => "weathertext", |
| 67 |
"/ADC_DATABASE/CURRENTCONDITIONS/WEATHERICON" => "weathericon", |
| 68 |
"/ADC_DATABASE/CURRENTCONDITIONS/WINDSPEED" => "windspeed", |
| 69 |
"/ADC_DATABASE/CURRENTCONDITIONS/WINDDIRECTION" => "winddirection", |
| 70 |
"/ADC_DATABASE/CURRENTCONDITIONS/WINDGUSTS" => "wgusts", |
| 71 |
"/ADC_DATABASE/PLANETS/SUN" => "sun", |
| 72 |
"/ADC_DATABASE/FORECAST/DAY/OBSDATE" => "fc_obsdate", |
| 73 |
"/ADC_DATABASE/FORECAST/DAY/DAYTIME/TXTSHORT" => "fc_dt_short", |
| 74 |
"/ADC_DATABASE/FORECAST/DAY/DAYTIME/WEATHERICON" => "fc_dt_icon", |
| 75 |
"/ADC_DATABASE/FORECAST/DAY/DAYTIME/HIGHTEMPERATURE" => "fc_dt_htemp", |
| 76 |
"/ADC_DATABASE/FORECAST/DAY/DAYTIME/LOWTEMPERATURE" => "fc_dt_ltemp", |
| 77 |
"/ADC_DATABASE/FORECAST/DAY/DAYTIME/WINDSPEED" => "fc_dt_windspeed", |
| 78 |
"/ADC_DATABASE/FORECAST/DAY/DAYTIME/WINDDIRECTION" => "fc_dt_winddir", |
| 79 |
"/ADC_DATABASE/FORECAST/DAY/NIGHTTIME/WEATHERICON" => "fc_nt_icon", |
| 80 |
"/ADC_DATABASE/FORECAST/DAY/NIGHTTIME/HIGHTEMPERATURE" => "fc_nt_htemp", |
| 81 |
"/ADC_DATABASE/FORECAST/DAY/NIGHTTIME/LOWTEMPERATURE" => "fc_nt_ltemp", |
| 82 |
"/ADC_DATABASE/FORECAST/DAY/NIGHTTIME/WINDSPEED" => "fc_nt_windspeed", |
| 83 |
"/ADC_DATABASE/FORECAST/DAY/NIGHTTIME/WINDDIRECTION" => "fc_nt_winddir", |
| 84 |
"/ADC_DATABASE/FORECAST/DAY/DAYTIME/WINDGUST" => "fc_dt_wgusts", |
| 85 |
"/ADC_DATABASE/FORECAST/DAY/NIGHTTIME/WINDGUST" => "fc_nt_wgusts", |
| 86 |
"/ADC_DATABASE/FAILURE" => "failure" |
| 87 |
); |
| 88 |
|
| 89 |
$wpf_pstack .= "/$name"; |
| 90 |
|
| 91 |
if (isset( $wpf_path_table[ $wpf_pstack])) { |
| 92 |
$wpf_go_ahead = $wpf_path_table[$wpf_pstack]; |
| 93 |
|
| 94 |
if ($wpf_fc_daynumber != "0" and $wpf_fc_daynumber != "") |
| 95 |
$wpf_go_ahead = $wpf_go_ahead . "_" . $wpf_fc_daynumber; |
| 96 |
} |
| 97 |
|
| 98 |
// for forecast days |
| 99 |
if ($wpf_pstack=="/ADC_DATABASE/FORECAST/DAY") |
| 100 |
$wpf_fc_daynumber=$attribute["NUMBER"]; |
| 101 |
|
| 102 |
// for current sun rise/set |
| 103 |
if ($wpf_pstack=="/ADC_DATABASE/PLANETS/SUN") |
| 104 |
$wpf_weather["sun"]=$attribute["RISE"]." ".$attribute["SET"]; |
| 105 |
|
| 106 |
// for current time daylightsavings |
| 107 |
if ($wpf_pstack== "/ADC_DATABASE/LOCAL/GMTDIFF") { |
| 108 |
if ( $attribute["DAYLIGHTSAVINGS"] !="") |
| 109 |
$wpf_weather['gmtdiffdls'] = $attribute["DAYLIGHTSAVINGS"]; |
| 110 |
else |
| 111 |
$wpf_weather['gmtdiffdls'] = 0; |
| 112 |
} |
| 113 |
|
| 114 |
pdebug(2,"End of start_element ()"); |
| 115 |
} |
| 116 |
|
| 117 |
// end_element() - called for every end tag |
| 118 |
function accu_end_element( $parser, $name ) |
| 119 |
{ |
| 120 |
global $wpf_pstack,$wpf_fc_daynumber; |
| 121 |
|
| 122 |
pdebug(2,"Start of end_element ()"); |
| 123 |
|
| 124 |
// reduce xml path |
| 125 |
$wpf_pstack = substr($wpf_pstack,0, strrpos($wpf_pstack,"/")); |
| 126 |
|
| 127 |
if ($name=="DAY") |
| 128 |
$wpf_fc_daynumber=0; |
| 129 |
|
| 130 |
pdebug(2,"End of end_element ()"); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
// daten() - called for everey cdata |
| 135 |
function accu_daten( $parser, $data ) |
| 136 |
{ |
| 137 |
global $wpf_weather,$wpf_go_ahead; |
| 138 |
|
| 139 |
pdebug(2,"Start of daten ()"); |
| 140 |
|
| 141 |
if ( strlen($wpf_go_ahead) > 0 and $wpf_go_ahead != "sun" and $wpf_go_ahead != "gmtdiffdls") |
| 142 |
{ |
| 143 |
$wpf_weather[$wpf_go_ahead]=$data; |
| 144 |
$wpf_go_ahead = ''; |
| 145 |
} |
| 146 |
|
| 147 |
pdebug(2,"End of daten ()"); |
| 148 |
} |
| 149 |
|
| 150 |
// |
| 151 |
// parses the xml for the paths in path_tabelle |
| 152 |
// |
| 153 |
|
| 154 |
function accu_xml_parser($xmlstring) { |
| 155 |
global $wpf_weather,$wpf_pstack,$wpf_go_ahead,$wpf_fc_daynumber; |
| 156 |
|
| 157 |
pdebug(1,"Start of accu_xml_parser ()"); |
| 158 |
|
| 159 |
$wpf_weather=array(); |
| 160 |
$xmlerror=""; |
| 161 |
|
| 162 |
$wpf_pstack=""; |
| 163 |
$wpf_go_ahead = ""; |
| 164 |
$wpf_fc_daynumber="0"; |
| 165 |
|
| 166 |
// create an xml parser object |
| 167 |
$parser = xml_parser_create(); |
| 168 |
|
| 169 |
// set parameters for xml parser |
| 170 |
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, true ); |
| 171 |
|
| 172 |
// set handler for start and end-tags |
| 173 |
xml_set_element_handler( $parser,"accu_start_element","accu_end_element"); |
| 174 |
// set handler for CDATA |
| 175 |
xml_set_character_data_handler( $parser,"accu_daten"); |
| 176 |
|
| 177 |
// try to parse the xml |
| 178 |
if( !xml_parse( $parser, $xmlstring,true ) ) |
| 179 |
{ |
| 180 |
// Error --> stop execution |
| 181 |
$xmlerror="XML Fehler: " . |
| 182 |
xml_error_string( xml_get_error_code( $parser ) ) . " in Zeile " . |
| 183 |
xml_get_current_line_number( $parser ) |
| 184 |
; |
| 185 |
} |
| 186 |
|
| 187 |
// Vom XML-Parser belegten Speicher freigeben |
| 188 |
xml_parser_free( $parser ); |
| 189 |
|
| 190 |
// check for error |
| 191 |
if ($xmlerror!="") { |
| 192 |
$wpf_weather=array(); |
| 193 |
} |
| 194 |
|
| 195 |
pdebug(1,"End of wpf_xml_parser ()"); |
| 196 |
|
| 197 |
// and return result, empty array if error |
| 198 |
return $wpf_weather; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
// |
| 203 |
// parse xml and extract locations as an array |
| 204 |
// for later us in the admin form |
| 205 |
// |
| 206 |
function accu_get_locations($xml) |
| 207 |
{ |
| 208 |
pdebug(1,"Start of get_locations ()"); |
| 209 |
|
| 210 |
// start_element() - wird vom XML-Parser bei öffnenden Tags aufgerufen |
| 211 |
function s_element( $parser, $name, $attribute ) |
| 212 |
{ |
| 213 |
global $loc,$i; |
| 214 |
if ($name == "LOCATION") { |
| 215 |
$loc[$i]=array(); |
| 216 |
$loc[$i]['city'] = $attribute['CITY']; |
| 217 |
$loc[$i]['state'] = $attribute['STATE']; |
| 218 |
$loc[$i]['location'] = $attribute['LOCATION']; |
| 219 |
$i++; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
// end_element() - dummy function |
| 224 |
function e_element( $parser, $name ){} |
| 225 |
|
| 226 |
// Instanz des XML-Parsers erzeugen |
| 227 |
$parser = xml_parser_create(); |
| 228 |
|
| 229 |
// Parameter des XML-Parsers setzen |
| 230 |
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, true ); |
| 231 |
|
| 232 |
// Handler für Elemente ( öffnende / schließende Tags ) setzen |
| 233 |
xml_set_element_handler( $parser, "s_element", "e_element" ); |
| 234 |
|
| 235 |
// try to parse the xml |
| 236 |
if( !xml_parse( $parser, $xml, true ) ) |
| 237 |
{ |
| 238 |
// Fehler -> Ausführung abbrechen |
| 239 |
die( "XML Fehler: " . |
| 240 |
xml_error_string( xml_get_error_code( $parser ) ) . |
| 241 |
" in Zeile " . |
| 242 |
xml_get_current_line_number( $parser ) |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
// Vom XML-Parser belegten Speicher freigeben |
| 247 |
xml_parser_free( $parser ); |
| 248 |
|
| 249 |
pdebug(1,"End of get_locations ()"); |
| 250 |
|
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
// |
| 256 |
// returns the widget data as an array one line per item |
| 257 |
// |
| 258 |
function accu_forecast_data($wpfcid="A", $language_override=null) |
| 259 |
{ |
| 260 |
pdebug(1,"Start of function accu_forecast_data ()"); |
| 261 |
|
| 262 |
$wpf_vars=get_wpf_opts($wpfcid); |
| 263 |
if (!empty($language_override)) { |
| 264 |
$wpf_vars['wpf_language']=$language_override; |
| 265 |
} |
| 266 |
|
| 267 |
extract($wpf_vars); |
| 268 |
$w=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid)); |
| 269 |
|
| 270 |
// get translations |
| 271 |
if(function_exists('load_textdomain')) { |
| 272 |
global $l10n; |
| 273 |
if (!isset($l10n["wp-forecast_".$wpf_language])) |
| 274 |
load_textdomain("wp-forecast_".$wpf_language, ABSPATH . "wp-content/plugins/wp-forecast/lang/".$wpf_language.".mo"); |
| 275 |
} |
| 276 |
|
| 277 |
$weather_arr=array(); |
| 278 |
|
| 279 |
// -------------------------------------------------------------- |
| 280 |
// calc values for current conditions |
| 281 |
if ( ! isset($w['failure'])) { |
| 282 |
|
| 283 |
$weather_arr['servicelink']= 'http://www.accuweather.com/quick-look.aspx?partner=accuweather&loc=' . $location . '&metric=' . $metric; |
| 284 |
// next line is for compatibility |
| 285 |
$weather_arr['acculink']=$weather_arr['servicelink']; |
| 286 |
$weather_arr['location'] = $locname; |
| 287 |
$weather_arr['locname']= $w["city"]." ".$w["state"]; |
| 288 |
|
| 289 |
|
| 290 |
$lt = time() - date("Z"); // this is the GMT |
| 291 |
$ct = $lt + (3600 * ($w['gmtdiff'])); // local time |
| 292 |
|
| 293 |
|
| 294 |
if ( $w['gmtdiffdls'] == 1) |
| 295 |
$ct += 3600; // time with daylightsavings |
| 296 |
|
| 297 |
|
| 298 |
$ct = $ct + $wpf_vars['timeoffset'] * 60; // add or subtract time offset |
| 299 |
|
| 300 |
$weather_arr['blogdate']=date_i18n($fc_date_format, $ct); |
| 301 |
$weather_arr['blogtime']=date_i18n($fc_time_format, $ct); |
| 302 |
|
| 303 |
$cts = $w['fc_obsdate_1']." ".$w['time']; |
| 304 |
$ct = strtotime($cts); |
| 305 |
//$ct = $ct + $wpf_vars['timeoffset'] * 60; // add or subtract time offset |
| 306 |
$weather_arr['accudate']=date_i18n($fc_date_format, $ct); |
| 307 |
$weather_arr['accutime']=date_i18n($fc_time_format, $ct); |
| 308 |
|
| 309 |
|
| 310 |
$iconfile=find_icon($w["weathericon"]); |
| 311 |
$weather_arr['icon']="icons/".$iconfile; |
| 312 |
$weather_arr['iconcode']=$w["weathericon"]; |
| 313 |
|
| 314 |
$weather_arr['shorttext']= __($w["weathericon"],"wp-forecast_".$wpf_language); |
| 315 |
|
| 316 |
$weather_arr['temperature']=$w["temperature"]. "°".$w['un_temp']; |
| 317 |
$weather_arr['realfeel']=$w["realfeel"]."°".$w['un_temp']; |
| 318 |
// workaround different pressure values returned by accuweather |
| 319 |
$press = round($w["pressure"],0); |
| 320 |
if (strlen($press)==3 and substr($press,0,1)=="1") |
| 321 |
$press = $press * 10; |
| 322 |
$weather_arr['pressure'] = $press . " " . $w["un_pres"]; |
| 323 |
$weather_arr['humidity']=round($w["humidity"],0); |
| 324 |
$weather_arr['windspeed']=windstr($metric,$w["windspeed"],$windunit); |
| 325 |
$weather_arr['winddir']=translate_winddir($w["winddirection"],"wp-forecast_".$wpf_language); |
| 326 |
$weather_arr['windgusts']=windstr($metric,$w["wgusts"],$windunit); |
| 327 |
$sunarr = explode(" ",$w["sun"]); |
| 328 |
$weather_arr['sunrise']=$sunarr[0]; |
| 329 |
$weather_arr['sunset']=$sunarr[1]; |
| 330 |
$weather_arr['copyright']='<a href="http://www.accuweather.com">© '.date("Y").' AccuWeather, Inc.</a>'; |
| 331 |
|
| 332 |
|
| 333 |
// calc values for forecast |
| 334 |
for ($i = 1; $i < 10; $i++) { |
| 335 |
// daytime forecast |
| 336 |
$weather_arr['fc_obsdate_'.$i]= date_i18n($fc_date_format, strtotime($w['fc_obsdate_'.$i])); |
| 337 |
$iconfile=find_icon($w["fc_dt_icon_".$i]); |
| 338 |
$weather_arr["fc_dt_icon_".$i]="icons/".$iconfile; |
| 339 |
$weather_arr["fc_dt_iconcode_".$i]=$w["fc_dt_icon_".$i]; |
| 340 |
$weather_arr["fc_dt_desc_".$i]= __($w["fc_dt_icon_".$i],"wp-forecast_".$wpf_language); |
| 341 |
$weather_arr["fc_dt_htemp_".$i]= $w["fc_dt_htemp_".$i]."°".$w['un_temp']; |
| 342 |
$wstr=windstr($metric,$w["fc_dt_windspeed_".$i],$windunit); |
| 343 |
$weather_arr["fc_dt_windspeed_".$i]= $wstr; |
| 344 |
$weather_arr["fc_dt_winddir_".$i]=translate_winddir($w["fc_dt_winddir_".$i],"wp-forecast_".$wpf_language); |
| 345 |
$weather_arr["fc_dt_wgusts_".$i] = windstr($metric,$w["fc_dt_wgusts_".$i],$windunit); |
| 346 |
|
| 347 |
// nighttime forecast |
| 348 |
$iconfile=find_icon($w["fc_nt_icon_".$i]); |
| 349 |
$weather_arr["fc_nt_icon_".$i]="icons/".$iconfile; |
| 350 |
$weather_arr["fc_nt_iconcode_".$i]=$w["fc_nt_icon_".$i]; |
| 351 |
$weather_arr["fc_nt_desc_".$i]= __($w["fc_nt_icon_".$i],"wp-forecast_".$wpf_language); |
| 352 |
$weather_arr["fc_nt_ltemp_".$i]= $w["fc_nt_ltemp_".$i]."°".$w['un_temp']; |
| 353 |
$wstr=windstr($metric,$w["fc_nt_windspeed_".$i],$windunit); |
| 354 |
$weather_arr["fc_nt_windspeed_".$i]= $wstr; |
| 355 |
$weather_arr["fc_nt_winddir_".$i]=translate_winddir($w["fc_nt_winddir_".$i],"wp-forecast_".$wpf_language); |
| 356 |
$weather_arr["fc_nt_wgusts_".$i] = windstr($metric,$w["fc_nt_wgusts_".$i],$windunit); |
| 357 |
|
| 358 |
// additional info |
| 359 |
$weather_arr['lat']=$w['lat']; |
| 360 |
$weather_arr['lon']=$w['lon']; |
| 361 |
} |
| 362 |
} |
| 363 |
// fill failure anyway |
| 364 |
$weather_arr['failure']=( isset($w['failure']) ? $w['failure'] : ''); |
| 365 |
|
| 366 |
pdebug(1,"End of function accu_forecast_data ()"); |
| 367 |
|
| 368 |
return $weather_arr; |
| 369 |
} |
| 370 |
?> |