PluginProbe
wp-forecast / 2.6
wp-forecast v2.6
10.0 trunk 1.4 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3..5 3..8 3.0 3.1 3.2 3.3 3.4 3.6 All 86 releases
wp-forecast / funclib.php

funclib.php in wp-forecast 2.6, at funclib.php

480 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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('fetchURL')) {
22
23 //
24 // this function fetches an url an returns it as one whole string
25 //
26 function fetchURL($url)
27 {
28 pdebug(1,"Start of fetchURL ()");
29
30 // get timeout parameter
31 $timeout = get_option("wp-forecast-timeout");
32 if ( $timeout =="")
33 $timeout = 10;
34
35
36 if ( function_exists("wp_remote_request") ) {
37 pdebug(1,"Using wp_remote_request");
38
39 // switch to wp-forecast transport
40 switch_wpf_transport(true);
41
42 $wprr_args = array(
43 'timeout' => $timeout,
44 'decompress' => false,
45 'headers' => array(
46 'Connection' => 'Close',
47 'Accept' => '*/*'
48 )
49 );
50
51
52 // use generic wordpress function to retrieve data
53 $s = time();
54 $resp = wp_remote_request($url, $wprr_args);
55 $e = time();
56
57 if ( is_wp_error($resp) )
58 $blen = "-1";
59 else
60 $blen = strlen( $resp['body'] );
61
62 pdebug(1,"Fetch took ".(int) ($e-$s) . " seconds and got ".$blen." Bytes.");
63
64 // switch to wp-forecast transport
65 switch_wpf_transport(false);
66
67 if ( is_wp_error($resp) ) {
68 $errcode = $resp->get_error_code();
69 $errmesg = $resp->get_error_message($errcode);
70
71 $erg="<ADC_DATABASE><FAILURE>Connection Error:".$errcode . "<br/>";
72 $erg .= $errmesg ."</FAILURE></ADC_DATABASE>\n";
73 } else
74 $erg = $resp['body'];
75
76
77 } else {
78 pdebug(1,"Using build in fsockopen method");
79 // fallback to old fsockopen variant
80 $url_parsed = parse_url($url);
81 $host = $url_parsed["host"];
82 if (!isset($url_parsed["port"]))
83 $port = 80;
84 else
85 $port = $url_parsed["port"];
86
87 $path = $url_parsed["path"];
88 if ($url_parsed["query"] != "") $path .= "?" . $url_parsed["query"];
89 $out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
90 // open connection
91 $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
92
93 $erg = "";
94 if ($fp) {
95 // set timeout for reading
96 stream_set_timeout($fp, $timeout);
97 // send request
98 fwrite($fp, $out);
99 $body = false;
100 // read answer
101 while (!feof($fp)) {
102 $s = fgets($fp, 1024);
103 if ($body) $erg .= $s;
104 if ($s == "\r\n") $body = true;
105 }
106 // close connection
107 fclose($fp);
108 } else {
109 // error handling
110 $erg="<ADC_DATABASE><FAILURE>Connection Error:".$errno. " >> ". $errstr ."</FAILURE></ADC_DATABASE>\n";
111 }
112 }
113
114 // workaround for bug in decompress function, class wp_http in wp 2.9
115 $derg = @gzinflate($erg);
116 if ($derg !== false)
117 $erg = $derg;
118
119 pdebug(1,"End of fetchURL ()");
120
121 return $erg;
122 }
123
124
125 //
126 // converts the given wind parameters into a suitable windstring
127 //
128 function windstr($metric,$wspeed,$windunit)
129 {
130 pdebug(2,"Start of windstr ()");
131
132 // if its mph convert it to m/s
133 if ($metric != 1)
134 $wspeed = round($wspeed * 0.44704,0);
135
136 // convert it to selected unit
137 switch ($windunit) {
138 case "ms":
139 $wunit="m/s";
140 break;
141 case "kmh":
142 $wspeed=round($wspeed*3.6,0);
143 $wunit="km/h";
144 break;
145 case "mph":
146 $wspeed=round($wspeed*2.23694,0);
147 $wunit="mph";
148 break;
149 case "kts":
150 $wspeed=round($wspeed*1.9438,0);
151 $wunit="kts";
152 break;
153 }
154
155 pdebug(2,"End of windstr ()");
156
157 return $wspeed." ".$wunit;
158 }
159
160
161 function get_wpf_opts($wpfcid)
162 {
163 pdebug(1,"Start of get_wpf_opts ($wpfcid)");
164
165 $av=array();
166 $opt = get_option("wp-forecast-opts".$wpfcid);
167 if (! empty($opt))
168 {
169 // get widget options from database
170 $av=unserialize($opt);
171 }
172 else if (get_option("wp-forecast-location".$wpfcid) !="" )
173 {
174 // get old widget options from database
175 $av['service'] = get_option("wp-forecast-service".$wpfcid);
176 $av['apikey1'] = get_option("wp-forecast-apikey1".$wpfcid);
177 $av['apikey2'] = get_option("wp-forecast-apikey2".$wpfcid);
178 $av['location'] = get_option("wp-forecast-location".$wpfcid);
179 $av['locname'] = get_option("wp-forecast-locname".$wpfcid);
180 $av['refresh'] = get_option("wp-forecast-refresh".$wpfcid);
181 $av['metric'] = get_option("wp-forecast-metric".$wpfcid);
182 $av['wpf_language'] = get_option("wp-forecast-language".$wpfcid);
183 $av['daytime'] = get_option("wp-forecast-daytime".$wpfcid);
184 $av['nighttime'] = get_option("wp-forecast-nighttime".$wpfcid);
185 $av['dispconfig'] = get_option("wp-forecast-dispconfig".$wpfcid);
186 $av['windunit'] = get_option("wp-forecast-windunit".$wpfcid);
187 $av['currtime'] = get_option("wp-forecast-currtime".$wpfcid);
188 $av['timeoffset'] = get_option("wp-forecast-timeoffset".$wpfcid);
189 $av['title'] = get_option("wp-forecast-title".$wpfcid);
190 // replace old options by new one row option
191 add_option("wp-forecast-opts".$wpfcid,serialize($av));
192 // remove old options from database
193 delete_option("wp-forecast-location".$wpfcid);
194 delete_option("wp-forecast-locname".$wpfcid);
195 delete_option("wp-forecast-refresh".$wpfcid);
196 delete_option("wp-forecast-metric".$wpfcid);
197 delete_option("wp-forecast-language".$wpfcid);
198 delete_option("wp-forecast-daytime".$wpfcid);
199 delete_option("wp-forecast-nighttime".$wpfcid);
200 delete_option("wp-forecast-dispconfig".$wpfcid);
201 delete_option("wp-forecast-windunit".$wpfcid);
202 delete_option("wp-forecast-currtime".$wpfcid);
203 delete_option("wp-forecast-title".$wpfcid);
204 delete_option("wp-forecast-service".$wpfcid);
205 delete_option("wp-forecast-apikey1".$wpfcid);
206 delete_option("wp-forecast-apikey2".$wpfcid);
207 }
208 else
209 {
210 $av=array();
211 }
212
213 // add expire options
214 $av['expire']=get_option("wp-forecast-expire".$wpfcid);
215
216 // add generic options
217 $av['fc_date_format']=get_option("date_format");
218 $av['fc_time_format']=get_option("time_format");
219 $av['xmlerror']="";
220
221 // set static uris for each provider
222 //$av['BASE_URI']="http://host1/accuweather/weather_data.php?"; // for testing
223 $av['ACCU_LOC_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/locate_city.asp?location=";
224 $av['ACCU_BASE_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/weather_data.asp?";
225
226 $av['BUG_LOC_URI']="http://#apicode#.api.wxbug.net/getLocationsXML.aspx?ACode=#apicode#&SearchString=";
227 //$av['BUG_STAT_URI']="http://#apicode#.api.wxbug.net/getStationsXML.aspx?ACode=#apicode#&cityCode=";
228 $av['BUG_BASE_URI']="http://#apicode#.api.wxbug.net/getLiveWeatherRSS.aspx?ACode=#apicode#&cityCode=";
229 $av['BUG_FORC_URI']="http://#apicode#.api.wxbug.net/getForecastRSS.aspx?ACode=#apicode#&cityCode=";
230
231 pdebug(1,"End of get_wpf_opts ()");
232
233 return $av;
234 }
235
236
237 //
238 // build the url from the parameters and fetch the weather-data
239 // return it as one long string
240 //
241 function get_weather($uri,$loc,$metric)
242 {
243 pdebug(1,"Start of get_weather ()");
244
245 $url=$uri . "location=" . urlencode($loc) . "&metric=" .
246 $metric;
247
248 $xml = fetchURL($url);
249
250 pdebug(1,"End of get_weather ()");
251
252 return $xml;
253 }
254
255 //
256 // just return the css link
257 // this function is called via the wp_head hook
258 //
259 function wp_forecast_css($wpfcid="A")
260 {
261 pdebug(1,"Start of function wp_forecast_css ()");
262
263 $def = "wp-forecast-default.css";
264 $user = "wp-forecast.css";
265
266 if (file_exists( WP_PLUGIN_DIR . "/wp-forecast/" . $user))
267 $def =$user;
268
269 $plugin_url = plugins_url("wp-forecast/");
270
271 echo '<link rel="stylesheet" id="wp-forecast-css" href="'. $plugin_url . $def . '" type="text/css" media="screen" />' ."\n";
272
273
274
275 pdebug(1,"End of function wp_forecast_css ()");
276 }
277
278
279 //
280 // just return the css link when not using wordpress
281 // this function is called when showing widget directly via wp-forecast-show.php
282 //
283 function wp_forecast_css_nowp($wpfcid="A")
284 {
285 pdebug(1,"Start of function wp_forecast_css_nowp ()");
286
287 $def = "wp-forecast-default-nowp.css";
288 $user = "wp-forecast-nowp.css";
289
290 if (file_exists( WP_PLUGIN_DIR . "/wp-forecast/" . $user))
291 $def =$user;
292
293 $plugin_url = plugins_url("wp-forecast/");
294
295 echo '<link rel="stylesheet" id="wp-forecast-nowp-css" href="'. $plugin_url . $def . '" type="text/css" media="screen" />' ."\n";
296
297 pdebug(1,"End of function wp_forecast_css_nowp ()");
298 }
299
300
301 //
302 // little debug output routine
303 //
304 function pdebug($level,$dstr)
305 {
306 global $wpf_debug;
307 if ($wpf_debug >= $level)
308 echo $dstr."<br />\n";
309 }
310
311 //
312 // returns the number's widget id used with wp-forecast
313 // maximum is 999999 :-)
314 //
315 function get_widget_id($number)
316 {
317 // if negative take the first id
318 if ($number < 0 )
319 return "A";
320
321 // the first widgets use chars above we go with 0 padded numbers
322 if ( $number <= 25 )
323 return substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",$number,1);
324 else
325 return str_pad($number, 6, "0", STR_PAD_LEFT);
326 }
327
328 //
329 // function tries to determine the icon path for icon number ino
330 //
331 function find_icon($ino)
332 {
333 $path = ABSPATH . "wp-content/plugins/wp-forecast/icons/".$ino;
334 $ext=".gif";
335
336 if ( file_exists($path.".gif") )
337 $ext= ".gif";
338 else if ( file_exists($path.".png") )
339 $ext= ".png";
340 else if ( file_exists($path.".jpg") )
341 $ext= ".jpg";
342 else if ( file_exists($path.".GIF") )
343 $ext= ".GIF";
344 else if ( file_exists($path.".PNG") )
345 $ext= ".PNG";
346 else if ( file_exists($path.".JPG") )
347 $ext= ".JPG";
348 else if ( file_exists($path.".jpeg") )
349 $ext= ".jpeg";
350 else if ( file_exists($path.".JPEG") )
351 $ext= ".JPEG";
352 return $ino . $ext;
353 }
354
355 function translate_winddir($wdir,$tdom)
356 {
357 // translate winddir char by char
358 $winddir="";
359 for ($i=0;$i<strlen($wdir);$i++)
360 $winddir=$winddir . __($wdir{$i},$tdom);
361 return $winddir;
362 }
363
364 }
365
366 /*
367 functions to check the wordpress transport methods
368
369 if wpf selected transport option is set to empty,
370 then we are in probing mode and do not change the wordpress result
371 else we only keep alive what was selected via admin dialog
372
373 */
374
375 function wpf_check_fsockopen($use, $args = array())
376 {
377 $sel_transport = get_option("wp-forecast-wp-transport");
378 if ( $sel_transport == "" || $sel_transport == "default" )
379 return $use;
380 else if ( $sel_transport == "fsockopen" )
381 return true;
382 else
383 return false;
384 }
385
386 function wpf_check_fopen($use, $args = array())
387 {
388 $sel_transport = get_option("wp-forecast-wp-transport");
389 if ( $sel_transport == "" || $sel_transport == "default" )
390 return $use;
391 else if ( $sel_transport == "fopen" )
392 return true;
393 else
394 return false;
395 }
396
397 function wpf_check_streams($use, $args = array())
398 {
399 $sel_transport = get_option("wp-forecast-wp-transport");
400 if ( $sel_transport == "" || $sel_transport == "default" )
401 return $use;
402 else if ( $sel_transport == "streams" )
403 return true;
404 else
405 return false;
406 }
407
408 function wpf_check_exthttp($use, $args = array())
409 {
410 $sel_transport = get_option("wp-forecast-wp-transport");
411 if ( $sel_transport == "" || $sel_transport == "default" )
412 return $use;
413 else if ( $sel_transport == "exthttp" )
414 return true;
415 else
416 return false;
417 }
418
419 function wpf_check_curl($use, $args = array())
420 {
421 $sel_transport = get_option("wp-forecast-wp-transport");
422 if ( $sel_transport == "" || $sel_transport == "default" )
423 return $use;
424 else if ( $sel_transport == "curl" )
425 return true;
426 else
427 return false;
428 }
429
430 // function to get the list of supported transports ignoring
431 // any preset method
432 function get_wp_transports()
433 {
434 $tlist = array();
435
436 // remove but store selected transport
437 $wp_transport = get_option("wp-forecast-wp-transport");
438 update_option("wp-forecast-wp-transport","default");
439
440 // get wordpress default transports
441 $wplist = array();
442
443 if ( true === WP_Http_ExtHttp::test( array() ) )
444 $tlist[] = "exthttp";
445
446 if ( true === WP_Http_Fsockopen::test( array() ) )
447 $tlist[] = "fsockopen";
448
449 if ( true === WP_Http_Streams::test( array() ) )
450 $tlist[] = "streams";
451
452 // disabled fopen, since this class sends no headers
453 //if ( true === WP_Http_Fopen::test( array() ) )
454 // $tlist[] = "fopen";
455
456 if ( true === WP_Http_Curl::test( array() ) )
457 $tlist[] = "curl";
458
459
460 // write back selected transport
461 update_option("wp-forecast-wp-transport",$wp_transport);
462
463 return $tlist;
464 }
465
466 //
467 // function to turn on/off wp-forecast preselected transport
468 //
469 function switch_wpf_transport($sw)
470 {
471 $wptrans = "default";
472
473 if ($sw == true)
474 $wptrans = get_option("wp-forecast-pre-transport");
475
476 pdebug(1,"Setting preselected transport to ".$wptrans);
477 update_option("wp-forecast-wp-transport",$wptrans);
478 }
479 ?>
480