| 1 |
<?php |
| 2 |
/* This file is part of the wp-forecast plugin for wordpress */ |
| 3 |
|
| 4 |
/* Copyright 2010 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('google_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 google_get_weather($uri,$loc,$lang) |
| 29 |
{ |
| 30 |
pdebug(1,"Start of google_get_weather ()"); |
| 31 |
|
| 32 |
$url=$uri . "weather=" . urlencode($loc) . "&hl=" . $lang; |
| 33 |
|
| 34 |
$xml = fetchURL($url); |
| 35 |
|
| 36 |
pdebug(1,"End of google_get_weather ()"); |
| 37 |
|
| 38 |
return $xml; |
| 39 |
} |
| 40 |
|
| 41 |
// start_element() - called for every start tag |
| 42 |
function google_start_element( $parser, $name, $attribute ) |
| 43 |
{ |
| 44 |
global $wpf_pstack,$wpf_go_ahead,$wpf_fc_daynumber,$wpf_weather; |
| 45 |
|
| 46 |
pdebug(2,"Start of start_element ()"); |
| 47 |
|
| 48 |
$wpf_path_table = |
| 49 |
array( |
| 50 |
"/XML_API_REPLY/WEATHER/FORECAST_INFORMATION/UNIT_SYSTEM" => "un_system", |
| 51 |
"/XML_API_REPLY/WEATHER/FORECAST_INFORMATION/CITY" => "city", |
| 52 |
"/XML_API_REPLY/WEATHER/FORECAST_INFORMATION/LATITUDE_E6" => "lat", |
| 53 |
"/XML_API_REPLY/WEATHER/FORECAST_INFORMATION/LONGITUDE_E6" => "lon", |
| 54 |
"/XML_API_REPLY/WEATHER/CURRENT_CONDITIONS/TEMP_C" => "temperature", |
| 55 |
"/XML_API_REPLY/WEATHER/CURRENT_CONDITIONS/HUMIDITY" => "humidity", |
| 56 |
"/XML_API_REPLY/WEATHER/CURRENT_CONDITIONS/CONDITION" => "weathertext", |
| 57 |
"/XML_API_REPLY/WEATHER/CURRENT_CONDITIONS/ICON" => "weathericon", |
| 58 |
"/XML_API_REPLY/WEATHER/CURRENT_CONDITIONS/WIND_CONDITION" => "wind", |
| 59 |
"/XML_API_REPLY/WEATHER/FORECAST_INFORMATION/CURRENT_DATE_TIME" => "fc_obsdate", |
| 60 |
"/XML_API_REPLY/WEATHER/FORECAST_CONDITIONS/CONDITION" => "fc_dt_short", |
| 61 |
"/XML_API_REPLY/WEATHER/FORECAST_CONDITIONS/ICON" => "fc_dt_icon", |
| 62 |
"/XML_API_REPLY/WEATHER/FORECAST_CONDITIONS/HIGH" => "fc_dt_htemp", |
| 63 |
"/XML_API_REPLY/WEATHER/FORECAST_CONDITIONS/LOW" => "fc_dt_ltemp", |
| 64 |
"/XML_API_REPLY/WEATHER/PROBLEM_CAUSE" => "failure" |
| 65 |
); |
| 66 |
|
| 67 |
$wpf_pstack .= "/$name"; |
| 68 |
|
| 69 |
if (isset( $wpf_path_table[ $wpf_pstack ])) { |
| 70 |
$wpf_go_ahead = $wpf_path_table[$wpf_pstack]; |
| 71 |
|
| 72 |
if (strpos( $wpf_pstack, "XML_API_REPLY/WEATHER/FORECAST_CONDITIONS") > 0 ) { |
| 73 |
$wpf_weather[$wpf_path_table[ $wpf_pstack ] . "_" . $wpf_fc_daynumber ] = $attribute['DATA']; |
| 74 |
} else |
| 75 |
$wpf_weather[$wpf_path_table[ $wpf_pstack ]] = $attribute['DATA']; |
| 76 |
} |
| 77 |
|
| 78 |
pdebug(2,"End of start_element ()"); |
| 79 |
} |
| 80 |
|
| 81 |
// end_element() - called for every end tag |
| 82 |
function google_end_element( $parser, $name ) |
| 83 |
{ |
| 84 |
global $wpf_pstack,$wpf_fc_daynumber; |
| 85 |
|
| 86 |
pdebug(2,"Start of end_element ()"); |
| 87 |
|
| 88 |
// reduce xml path |
| 89 |
$wpf_pstack = substr($wpf_pstack,0, strrpos($wpf_pstack,"/")); |
| 90 |
|
| 91 |
if ($name == "FORECAST_CONDITIONS") |
| 92 |
$wpf_fc_daynumber +=1; |
| 93 |
|
| 94 |
pdebug(2,"End of end_element ()"); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// daten() - called for everey cdata |
| 99 |
function google_daten( $parser, $data ) |
| 100 |
{ |
| 101 |
global $wpf_weather,$wpf_go_ahead; |
| 102 |
|
| 103 |
pdebug(2,"Start of daten ()"); |
| 104 |
|
| 105 |
// nothing to do here since google uses attributes |
| 106 |
|
| 107 |
pdebug(2,"End of daten ()"); |
| 108 |
} |
| 109 |
|
| 110 |
// |
| 111 |
// parses the xml for the paths in path_tabelle |
| 112 |
// |
| 113 |
|
| 114 |
function google_xml_parser($xmlstring) |
| 115 |
{ |
| 116 |
global $wpf_weather,$wpf_pstack,$wpf_go_ahead,$wpf_fc_daynumber; |
| 117 |
|
| 118 |
pdebug(1,"Start of google_xml_parser ()"); |
| 119 |
|
| 120 |
$wpf_weather=array(); |
| 121 |
$xmlerror=""; |
| 122 |
|
| 123 |
$wpf_pstack=""; |
| 124 |
$wpf_go_ahead = ""; |
| 125 |
$wpf_fc_daynumber="0"; |
| 126 |
|
| 127 |
// create an xml parser object |
| 128 |
$parser = xml_parser_create(); |
| 129 |
|
| 130 |
// set parameters for xml parser |
| 131 |
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, true ); |
| 132 |
|
| 133 |
// set handler for start and end-tags |
| 134 |
xml_set_element_handler( $parser,"google_start_element","google_end_element"); |
| 135 |
// set handler for CDATA |
| 136 |
xml_set_character_data_handler( $parser,"google_daten"); |
| 137 |
|
| 138 |
// try to parse the xml |
| 139 |
if( !xml_parse( $parser, $xmlstring, true ) ) |
| 140 |
{ |
| 141 |
// Error --> stop execution |
| 142 |
$xmlerror="XML Fehler: " . |
| 143 |
xml_error_string( xml_get_error_code( $parser ) ) . " in Zeile " . |
| 144 |
xml_get_current_line_number( $parser ) |
| 145 |
; |
| 146 |
} |
| 147 |
|
| 148 |
// Vom XML-Parser belegten Speicher freigeben |
| 149 |
xml_parser_free( $parser ); |
| 150 |
|
| 151 |
// check for error |
| 152 |
if ($xmlerror!="") { |
| 153 |
$wpf_weather=array(); |
| 154 |
} |
| 155 |
|
| 156 |
pdebug(1,"End of wpf_xml_parser ()"); |
| 157 |
|
| 158 |
// and return result, empty array if error |
| 159 |
return $wpf_weather; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
// |
| 165 |
// returns the widget data as an array one line per item |
| 166 |
// |
| 167 |
function google_forecast_data($wpfcid="A", $language_override=null) |
| 168 |
{ |
| 169 |
pdebug(1,"Start of function google_forecast_data ()"); |
| 170 |
|
| 171 |
$wpf_vars=get_wpf_opts($wpfcid); |
| 172 |
if (!empty($language_override)) { |
| 173 |
$wpf_vars['wpf_language']=$language_override; |
| 174 |
} |
| 175 |
|
| 176 |
extract($wpf_vars); |
| 177 |
$w=maybe_unserialize(wpf_get_option("wp-forecast-cache".$wpfcid)); |
| 178 |
|
| 179 |
// get translations |
| 180 |
if(function_exists('load_textdomain')) { |
| 181 |
global $l10n; |
| 182 |
if (!isset($l10n["wp-forecast_".$wpf_language])) |
| 183 |
load_textdomain("wp-forecast_".$wpf_language, ABSPATH . "wp-content/plugins/wp-forecast/lang/".$wpf_language.".mo"); |
| 184 |
} |
| 185 |
|
| 186 |
$weather_arr=array(); |
| 187 |
|
| 188 |
// -------------------------------------------------------------- |
| 189 |
// calc values for current conditions |
| 190 |
if ( ! isset($w['failure'])) { |
| 191 |
$weather_arr['servicelink']= 'http://www.google.de/#q=google+weather+' . $location; |
| 192 |
$weather_arr['location'] = $locname; |
| 193 |
$weather_arr['locname']= $w["city"]; |
| 194 |
|
| 195 |
$lt = time() - date("Z"); // this is the GMT |
| 196 |
$ct = $lt + (3600 * ($w['gmtdiff'])); // local time |
| 197 |
$ct = strtotime(current_time('mysql',false)); |
| 198 |
|
| 199 |
if ( $w['gmtdiffdls'] == 1) |
| 200 |
$ct += 3600; // time with daylightsavings |
| 201 |
|
| 202 |
|
| 203 |
$ct = $ct + $wpf_vars['timeoffset'] * 60; // add or subtract time offset |
| 204 |
|
| 205 |
$weather_arr['blogdate']=date_i18n($fc_date_format, $ct); |
| 206 |
$weather_arr['blogtime']=date_i18n($fc_time_format, $ct); |
| 207 |
|
| 208 |
$cts = $w['fc_obsdate']; |
| 209 |
$ct = strtotime($cts); |
| 210 |
//$ct = $ct + $wpf_vars['timeoffset'] * 60; // add or subtract time offset |
| 211 |
$weather_arr['googledate']=date_i18n($fc_date_format, $ct); |
| 212 |
$weather_arr['googletime']=date_i18n($fc_time_format, $ct); |
| 213 |
|
| 214 |
|
| 215 |
$iconfile="http://www.google.de" . $w['weathericon']; |
| 216 |
$weather_arr['icon'] = $iconfile; |
| 217 |
$weather_arr['iconcode'] = $w['weathericon']; |
| 218 |
|
| 219 |
$weather_arr['shorttext']= $w["weathertext"]; |
| 220 |
|
| 221 |
$weather_arr['temperature']=$w["temperature"]. "°".$w['un_temp']; |
| 222 |
$weather_arr['humidity'] = trim(substr($w["humidity"], strpos($w["humidity"], " "),3)); |
| 223 |
$wdirarr = explode(" ", $w['wind']); |
| 224 |
$weather_arr['windspeed']=windstr($metric,$wdirarr[3] / 3.6,"kmh"); |
| 225 |
$weather_arr['winddir']=translate_winddir($wdirarr[1],"wp-forecast_".$wpf_language); |
| 226 |
$weather_arr['copyright']='<a href="http://www.google.com">© 2010 GoogleWeather</a>'; |
| 227 |
|
| 228 |
|
| 229 |
// calc values for forecast |
| 230 |
for ($i = 0; $i < 4; $i++) { |
| 231 |
// daytime forecast |
| 232 |
$weather_arr['fc_obsdate_'.$i] = date_i18n($fc_date_format, strtotime($w['fc_obsdate'])+($i * 3600 *24)); |
| 233 |
$weather_arr["fc_dt_icon_".$i] = "http://www.google.de" . $w['fc_dt_icon_' . $i]; |
| 234 |
$weather_arr["fc_dt_iconcode_".$i] = $w['fc_dt_icon_' . $i]; |
| 235 |
$weather_arr["fc_dt_desc_".$i]= $w["fc_dt_short_".$i]; |
| 236 |
$weather_arr["fc_dt_htemp_".$i]= $w["fc_dt_htemp_".$i]."°".$w['un_temp']; |
| 237 |
$weather_arr["fc_dt_ltemp_".$i]= $w["fc_dt_ltemp_".$i]."°".$w['un_temp']; |
| 238 |
} |
| 239 |
// additional info |
| 240 |
$weather_arr['lat']=$w['lat']; |
| 241 |
$weather_arr['lon']=$w['lon']; |
| 242 |
|
| 243 |
} |
| 244 |
// fill failure anyway |
| 245 |
$weather_arr['failure']=( isset($w['failure']) ? $w['failure'] : ''); |
| 246 |
|
| 247 |
pdebug(1,"End of function google_forecast_data ()"); |
| 248 |
|
| 249 |
return $weather_arr; |
| 250 |
} |
| 251 |
} |
| 252 |
?> |