PluginProbe
wp-forecast / 2.8
wp-forecast v2.8
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.8, at funclib.php

495 lines 13.4 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 case "bft":
154 $wbft = 0;
155 $bft = array(0.3, 1.6, 3.4, 5.5, 8.0, 10.8, 13.9, 17.2, 20.8, 24.5, 28.5, 32.7);
156 foreach($bft as $b)
157 {
158 if ($wspeed < $b)
159 {
160 $wbft--;
161 break;
162 }
163 $wbft++;
164 }
165 $wunit="bft";
166 $wspeed = $wbft;
167 break;
168 }
169
170 pdebug(2,"End of windstr ()");
171
172 return $wspeed." ".$wunit;
173 }
174
175
176 function get_wpf_opts($wpfcid)
177 {
178 pdebug(1,"Start of get_wpf_opts ($wpfcid)");
179
180 $av=array();
181 $opt = get_option("wp-forecast-opts".$wpfcid);
182 if (! empty($opt))
183 {
184 // get widget options from database
185 $av=unserialize($opt);
186 }
187 else if (get_option("wp-forecast-location".$wpfcid) !="" )
188 {
189 // get old widget options from database
190 $av['service'] = get_option("wp-forecast-service".$wpfcid);
191 $av['apikey1'] = get_option("wp-forecast-apikey1".$wpfcid);
192 $av['apikey2'] = get_option("wp-forecast-apikey2".$wpfcid);
193 $av['location'] = get_option("wp-forecast-location".$wpfcid);
194 $av['locname'] = get_option("wp-forecast-locname".$wpfcid);
195 $av['refresh'] = get_option("wp-forecast-refresh".$wpfcid);
196 $av['metric'] = get_option("wp-forecast-metric".$wpfcid);
197 $av['wpf_language'] = get_option("wp-forecast-language".$wpfcid);
198 $av['daytime'] = get_option("wp-forecast-daytime".$wpfcid);
199 $av['nighttime'] = get_option("wp-forecast-nighttime".$wpfcid);
200 $av['dispconfig'] = get_option("wp-forecast-dispconfig".$wpfcid);
201 $av['windunit'] = get_option("wp-forecast-windunit".$wpfcid);
202 $av['currtime'] = get_option("wp-forecast-currtime".$wpfcid);
203 $av['timeoffset'] = get_option("wp-forecast-timeoffset".$wpfcid);
204 $av['title'] = get_option("wp-forecast-title".$wpfcid);
205 // replace old options by new one row option
206 add_option("wp-forecast-opts".$wpfcid,serialize($av));
207 // remove old options from database
208 delete_option("wp-forecast-location".$wpfcid);
209 delete_option("wp-forecast-locname".$wpfcid);
210 delete_option("wp-forecast-refresh".$wpfcid);
211 delete_option("wp-forecast-metric".$wpfcid);
212 delete_option("wp-forecast-language".$wpfcid);
213 delete_option("wp-forecast-daytime".$wpfcid);
214 delete_option("wp-forecast-nighttime".$wpfcid);
215 delete_option("wp-forecast-dispconfig".$wpfcid);
216 delete_option("wp-forecast-windunit".$wpfcid);
217 delete_option("wp-forecast-currtime".$wpfcid);
218 delete_option("wp-forecast-title".$wpfcid);
219 delete_option("wp-forecast-service".$wpfcid);
220 delete_option("wp-forecast-apikey1".$wpfcid);
221 delete_option("wp-forecast-apikey2".$wpfcid);
222 }
223 else
224 {
225 $av=array();
226 }
227
228 // add expire options
229 $av['expire']=get_option("wp-forecast-expire".$wpfcid);
230
231 // add generic options
232 $av['fc_date_format']=get_option("date_format");
233 $av['fc_time_format']=get_option("time_format");
234 $av['xmlerror']="";
235
236 // set static uris for each provider
237 //$av['BASE_URI']="http://host1/accuweather/weather_data.php?"; // for testing
238 $av['ACCU_LOC_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/locate_city.asp?location=";
239 $av['ACCU_BASE_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/weather_data.asp?";
240
241 $av['BUG_LOC_URI']="http://#apicode#.api.wxbug.net/getLocationsXML.aspx?ACode=#apicode#&SearchString=";
242 //$av['BUG_STAT_URI']="http://#apicode#.api.wxbug.net/getStationsXML.aspx?ACode=#apicode#&cityCode=";
243 $av['BUG_BASE_URI']="http://#apicode#.api.wxbug.net/getLiveWeatherRSS.aspx?ACode=#apicode#&cityCode=";
244 $av['BUG_FORC_URI']="http://#apicode#.api.wxbug.net/getForecastRSS.aspx?ACode=#apicode#&cityCode=";
245
246 pdebug(1,"End of get_wpf_opts ()");
247
248 return $av;
249 }
250
251
252 //
253 // build the url from the parameters and fetch the weather-data
254 // return it as one long string
255 //
256 function get_weather($uri,$loc,$metric)
257 {
258 pdebug(1,"Start of get_weather ()");
259
260 $url=$uri . "location=" . urlencode($loc) . "&metric=" .
261 $metric;
262
263 $xml = fetchURL($url);
264
265 pdebug(1,"End of get_weather ()");
266
267 return $xml;
268 }
269
270 //
271 // just return the css link
272 // this function is called via the wp_head hook
273 //
274 function wp_forecast_css($wpfcid="A")
275 {
276 pdebug(1,"Start of function wp_forecast_css ()");
277
278 $def = "wp-forecast-default.css";
279 $user = "wp-forecast.css";
280
281 if (file_exists( WP_PLUGIN_DIR . "/wp-forecast/" . $user))
282 $def =$user;
283
284 $plugin_url = plugins_url("wp-forecast/");
285
286 echo '<link rel="stylesheet" id="wp-forecast-css" href="'. $plugin_url . $def . '" type="text/css" media="screen" />' ."\n";
287
288
289
290 pdebug(1,"End of function wp_forecast_css ()");
291 }
292
293
294 //
295 // just return the css link when not using wordpress
296 // this function is called when showing widget directly via wp-forecast-show.php
297 //
298 function wp_forecast_css_nowp($wpfcid="A")
299 {
300 pdebug(1,"Start of function wp_forecast_css_nowp ()");
301
302 $def = "wp-forecast-default-nowp.css";
303 $user = "wp-forecast-nowp.css";
304
305 if (file_exists( WP_PLUGIN_DIR . "/wp-forecast/" . $user))
306 $def =$user;
307
308 $plugin_url = plugins_url("wp-forecast/");
309
310 echo '<link rel="stylesheet" id="wp-forecast-nowp-css" href="'. $plugin_url . $def . '" type="text/css" media="screen" />' ."\n";
311
312 pdebug(1,"End of function wp_forecast_css_nowp ()");
313 }
314
315
316 //
317 // little debug output routine
318 //
319 function pdebug($level,$dstr)
320 {
321 global $wpf_debug;
322 if ($wpf_debug >= $level)
323 echo $dstr."<br />\n";
324 }
325
326 //
327 // returns the number's widget id used with wp-forecast
328 // maximum is 999999 :-)
329 //
330 function get_widget_id($number)
331 {
332 // if negative take the first id
333 if ($number < 0 )
334 return "A";
335
336 // the first widgets use chars above we go with 0 padded numbers
337 if ( $number <= 25 )
338 return substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",$number,1);
339 else
340 return str_pad($number, 6, "0", STR_PAD_LEFT);
341 }
342
343 //
344 // function tries to determine the icon path for icon number ino
345 //
346 function find_icon($ino)
347 {
348 $path = ABSPATH . "wp-content/plugins/wp-forecast/icons/".$ino;
349 $ext=".gif";
350
351 if ( file_exists($path.".gif") )
352 $ext= ".gif";
353 else if ( file_exists($path.".png") )
354 $ext= ".png";
355 else if ( file_exists($path.".jpg") )
356 $ext= ".jpg";
357 else if ( file_exists($path.".GIF") )
358 $ext= ".GIF";
359 else if ( file_exists($path.".PNG") )
360 $ext= ".PNG";
361 else if ( file_exists($path.".JPG") )
362 $ext= ".JPG";
363 else if ( file_exists($path.".jpeg") )
364 $ext= ".jpeg";
365 else if ( file_exists($path.".JPEG") )
366 $ext= ".JPEG";
367 return $ino . $ext;
368 }
369
370 function translate_winddir($wdir,$tdom)
371 {
372 // translate winddir char by char
373 $winddir="";
374 for ($i=0;$i<strlen($wdir);$i++)
375 $winddir=$winddir . __($wdir{$i},$tdom);
376 return $winddir;
377 }
378
379 }
380
381 /*
382 functions to check the wordpress transport methods
383
384 if wpf selected transport option is set to empty,
385 then we are in probing mode and do not change the wordpress result
386 else we only keep alive what was selected via admin dialog
387
388 */
389
390 function wpf_check_fsockopen($use, $args = array())
391 {
392 $sel_transport = get_option("wp-forecast-wp-transport");
393 if ( $sel_transport == "" || $sel_transport == "default" )
394 return $use;
395 else if ( $sel_transport == "fsockopen" )
396 return true;
397 else
398 return false;
399 }
400
401 function wpf_check_fopen($use, $args = array())
402 {
403 $sel_transport = get_option("wp-forecast-wp-transport");
404 if ( $sel_transport == "" || $sel_transport == "default" )
405 return $use;
406 else if ( $sel_transport == "fopen" )
407 return true;
408 else
409 return false;
410 }
411
412 function wpf_check_streams($use, $args = array())
413 {
414 $sel_transport = get_option("wp-forecast-wp-transport");
415 if ( $sel_transport == "" || $sel_transport == "default" )
416 return $use;
417 else if ( $sel_transport == "streams" )
418 return true;
419 else
420 return false;
421 }
422
423 function wpf_check_exthttp($use, $args = array())
424 {
425 $sel_transport = get_option("wp-forecast-wp-transport");
426 if ( $sel_transport == "" || $sel_transport == "default" )
427 return $use;
428 else if ( $sel_transport == "exthttp" )
429 return true;
430 else
431 return false;
432 }
433
434 function wpf_check_curl($use, $args = array())
435 {
436 $sel_transport = get_option("wp-forecast-wp-transport");
437 if ( $sel_transport == "" || $sel_transport == "default" )
438 return $use;
439 else if ( $sel_transport == "curl" )
440 return true;
441 else
442 return false;
443 }
444
445 // function to get the list of supported transports ignoring
446 // any preset method
447 function get_wp_transports()
448 {
449 $tlist = array();
450
451 // remove but store selected transport
452 $wp_transport = get_option("wp-forecast-wp-transport");
453 update_option("wp-forecast-wp-transport","default");
454
455 // get wordpress default transports
456 $wplist = array();
457
458 if ( true === WP_Http_ExtHttp::test( array() ) )
459 $tlist[] = "exthttp";
460
461 if ( true === WP_Http_Fsockopen::test( array() ) )
462 $tlist[] = "fsockopen";
463
464 if ( true === WP_Http_Streams::test( array() ) )
465 $tlist[] = "streams";
466
467 // disabled fopen, since this class sends no headers
468 //if ( true === WP_Http_Fopen::test( array() ) )
469 // $tlist[] = "fopen";
470
471 if ( true === WP_Http_Curl::test( array() ) )
472 $tlist[] = "curl";
473
474
475 // write back selected transport
476 update_option("wp-forecast-wp-transport",$wp_transport);
477
478 return $tlist;
479 }
480
481 //
482 // function to turn on/off wp-forecast preselected transport
483 //
484 function switch_wpf_transport($sw)
485 {
486 $wptrans = "default";
487
488 if ($sw == true)
489 $wptrans = get_option("wp-forecast-pre-transport");
490
491 pdebug(1,"Setting preselected transport to ".$wptrans);
492 update_option("wp-forecast-wp-transport",$wptrans);
493 }
494 ?>
495