PluginProbe
wp-forecast / 3..5
wp-forecast v3..5
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 3..5, at funclib.php

589 lines 15.9 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 //
177 // wrapper to make sure the correct option is used whether multisite or wp is used
178 // only wraps main parameters and pass through all others
179 //
180 function wpf_get_option($name)
181 {
182 global $blog_id;
183
184 if ( !function_exists("is_multisite") || ! is_multisite() || $blog_id==1 )
185 return get_option($name);
186 else {
187 // this is the multisite part
188 if ( $name == "wpf_sa_defaults" or
189 $name == "wpf_sa_allowed" )
190 return get_blog_option(1, $name);
191 else
192 return get_blog_option($blog_id, $name);
193 }
194 }
195
196 //
197 // wrapper for update option to hide differences between wp and wpmu
198 //
199 //
200 function wpf_update_option($name, $value)
201 {
202 global $blog_id;
203
204 if ( !function_exists("is_multisite") || ! is_multisite() || $blog_id==1)
205 update_option($name, $value);
206 else
207 update_blog_option($blog_id, $name, $value);
208 }
209
210 //
211 // wrapper for add option to hide differences between wp and wpmu
212 //
213 //
214 function wpf_add_option($name, $value)
215 {
216 global $blog_id;
217
218 if ( !function_exists("is_multisite") || ! is_multisite() || $blog_id==1 )
219 add_option($name, $value);
220 else
221 add_blog_option($blog_id, $name, $value);
222 }
223
224 //
225 // reads all wp-forecast options and returns an array
226 //
227 function get_wpf_opts($wpfcid)
228 {
229 pdebug(1,"Start of get_wpf_opts ($wpfcid)");
230
231 global $blog_id;
232
233 $av=array();
234 $opt = wpf_get_option("wp-forecast-opts".$wpfcid);
235
236 if (! empty($opt))
237 {
238 // unpack if necessary
239 $av=maybe_unserialize($opt);
240 }
241 else if (get_option("wp-forecast-location".$wpfcid) !="" )
242 {
243 // get old widget options from database
244 $av['service'] = get_option("wp-forecast-service".$wpfcid);
245 $av['apikey1'] = get_option("wp-forecast-apikey1".$wpfcid);
246 $av['apikey2'] = get_option("wp-forecast-apikey2".$wpfcid);
247 $av['location'] = get_option("wp-forecast-location".$wpfcid);
248 $av['locname'] = get_option("wp-forecast-locname".$wpfcid);
249 $av['refresh'] = get_option("wp-forecast-refresh".$wpfcid);
250 $av['metric'] = get_option("wp-forecast-metric".$wpfcid);
251 $av['wpf_language'] = get_option("wp-forecast-language".$wpfcid);
252 $av['daytime'] = get_option("wp-forecast-daytime".$wpfcid);
253 $av['nighttime'] = get_option("wp-forecast-nighttime".$wpfcid);
254 $av['dispconfig'] = get_option("wp-forecast-dispconfig".$wpfcid);
255 $av['windunit'] = get_option("wp-forecast-windunit".$wpfcid);
256 $av['currtime'] = get_option("wp-forecast-currtime".$wpfcid);
257 $av['timeoffset'] = get_option("wp-forecast-timeoffset".$wpfcid);
258 $av['title'] = get_option("wp-forecast-title".$wpfcid);
259 // replace old options by new one row option
260 wpf_add_option("wp-forecast-opts".$wpfcid,serialize($av));
261 // remove old options from database
262 delete_option("wp-forecast-location".$wpfcid);
263 delete_option("wp-forecast-locname".$wpfcid);
264 delete_option("wp-forecast-refresh".$wpfcid);
265 delete_option("wp-forecast-metric".$wpfcid);
266 delete_option("wp-forecast-language".$wpfcid);
267 delete_option("wp-forecast-daytime".$wpfcid);
268 delete_option("wp-forecast-nighttime".$wpfcid);
269 delete_option("wp-forecast-dispconfig".$wpfcid);
270 delete_option("wp-forecast-windunit".$wpfcid);
271 delete_option("wp-forecast-currtime".$wpfcid);
272 delete_option("wp-forecast-title".$wpfcid);
273 delete_option("wp-forecast-service".$wpfcid);
274 delete_option("wp-forecast-apikey1".$wpfcid);
275 delete_option("wp-forecast-apikey2".$wpfcid);
276 }
277 else
278 {
279 $av=array();
280 }
281
282 // add expire options
283 $av['expire']=get_option("wp-forecast-expire".$wpfcid);
284
285 // add generic options
286 $av['fc_date_format']=get_option("date_format");
287 $av['fc_time_format']=get_option("time_format");
288 $av['xmlerror']="";
289
290 // set static uris for each provider
291 //$av['BASE_URI']="http://host1/accuweather/weather_data.php?"; // for testing
292 $av['ACCU_LOC_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/locate_city.asp?location=";
293 $av['ACCU_BASE_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/weather_data.asp?";
294
295 $av['BUG_LOC_URI']="http://#apicode#.api.wxbug.net/getLocationsXML.aspx?ACode=#apicode#&SearchString=";
296 //$av['BUG_STAT_URI']="http://#apicode#.api.wxbug.net/getStationsXML.aspx?ACode=#apicode#&cityCode=";
297 $av['BUG_BASE_URI']="http://#apicode#.api.wxbug.net/getLiveWeatherRSS.aspx?ACode=#apicode#&cityCode=";
298 $av['BUG_FORC_URI']="http://#apicode#.api.wxbug.net/getForecastRSS.aspx?ACode=#apicode#&cityCode=";
299
300 $av['GOOGLE_LOC_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/locate_city.asp?location=";
301 $av['GOOGLE_BASE_URI']="http://www.google.com/ig/api?";
302
303 // if we use multisite then merge admin options
304 if ( function_exists("is_multisite") && is_multisite() && $blog_id !=1)
305 {
306
307 // read defaults and allowed fields
308 $defaults = maybe_unserialize(wpf_get_option("wpf_sa_defaults"));
309 $allowed = maybe_unserialize(wpf_get_option("wpf_sa_allowed"));
310 // in case allowed is still empty
311 if (!$allowed)
312 $allowed=array();
313
314 // set wpf_maxwidgets for users
315 global $blog_id, $wpf_maxwidgets;
316 if ($blog_id > "1" and isset($defaults["wp-forecast-count"]))
317 $wpf_maxwidgets = $defaults["wp-forecast-count"];
318
319 // map rest of fields
320 foreach($allowed as $f => $fswitch)
321 {
322 $fname = substr($f,3); // strip ue_ prefix
323
324 if ( $fswitch != "1" or ! isset($av[ $fname ]) )
325 {
326 // replace value in av with forced default
327 if (array_key_exists($fname, $defaults))
328 $av[ $fname ] = $defaults[$fname];
329 }
330 }
331
332 }
333
334 pdebug(1,"End of get_wpf_opts ()");
335
336 return $av;
337 }
338
339
340 //
341 // build the url from the parameters and fetch the weather-data
342 // return it as one long string
343 //
344 function get_weather($uri,$loc,$metric)
345 {
346 pdebug(1,"Start of get_weather ()");
347
348 $url=$uri . "location=" . urlencode($loc) . "&metric=" .
349 $metric;
350
351 $xml = fetchURL($url);
352
353 pdebug(1,"End of get_weather ()");
354
355 return $xml;
356 }
357
358 //
359 // just return the css link
360 // this function is called via the wp_head hook
361 //
362 function wp_forecast_css($wpfcid="A")
363 {
364 pdebug(1,"Start of function wp_forecast_css ()");
365
366 $def = "wp-forecast-default.css";
367 $user = "wp-forecast.css";
368
369 if (file_exists( WP_PLUGIN_DIR . "/wp-forecast/" . $user))
370 $def =$user;
371
372 $plugin_url = plugins_url("wp-forecast/");
373
374 echo '<link rel="stylesheet" id="wp-forecast-css" href="'. $plugin_url . $def . '" type="text/css" media="screen" />' ."\n";
375
376
377
378 pdebug(1,"End of function wp_forecast_css ()");
379 }
380
381
382 //
383 // just return the css link when not using wordpress
384 // this function is called when showing widget directly via wp-forecast-show.php
385 //
386 function wp_forecast_css_nowp($wpfcid="A")
387 {
388 pdebug(1,"Start of function wp_forecast_css_nowp ()");
389
390 $def = "wp-forecast-default-nowp.css";
391 $user = "wp-forecast-nowp.css";
392
393 if (file_exists( WP_PLUGIN_DIR . "/wp-forecast/" . $user))
394 $def =$user;
395
396 $plugin_url = plugins_url("wp-forecast/");
397
398 echo '<link rel="stylesheet" id="wp-forecast-nowp-css" href="'. $plugin_url . $def . '" type="text/css" media="screen" />' ."\n";
399
400 pdebug(1,"End of function wp_forecast_css_nowp ()");
401 }
402
403
404 //
405 // little debug output routine
406 //
407 if (! function_exists("pdebug") )
408 {
409 function pdebug($level,$dstr)
410 {
411 global $wpf_debug;
412 if ($wpf_debug >= $level)
413 echo $dstr."<br />\n";
414 }
415 }
416
417 //
418 // returns the number's widget id used with wp-forecast
419 // maximum is 999999 :-)
420 //
421 function get_widget_id($number)
422 {
423 // if negative take the first id
424 if ($number < 0 )
425 return "A";
426
427 // the first widgets use chars above we go with 0 padded numbers
428 if ( $number <= 25 )
429 return substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",$number,1);
430 else
431 return str_pad($number, 6, "0", STR_PAD_LEFT);
432 }
433
434 //
435 // function tries to determine the icon path for icon number ino
436 //
437 function find_icon($ino)
438 {
439 $path = ABSPATH . "wp-content/plugins/wp-forecast/icons/".$ino;
440 $ext=".gif";
441
442 if ( file_exists($path.".gif") )
443 $ext= ".gif";
444 else if ( file_exists($path.".png") )
445 $ext= ".png";
446 else if ( file_exists($path.".jpg") )
447 $ext= ".jpg";
448 else if ( file_exists($path.".GIF") )
449 $ext= ".GIF";
450 else if ( file_exists($path.".PNG") )
451 $ext= ".PNG";
452 else if ( file_exists($path.".JPG") )
453 $ext= ".JPG";
454 else if ( file_exists($path.".jpeg") )
455 $ext= ".jpeg";
456 else if ( file_exists($path.".JPEG") )
457 $ext= ".JPEG";
458 return $ino . $ext;
459 }
460
461 function translate_winddir($wdir,$tdom)
462 {
463 // translate winddir char by char
464 $winddir="";
465 for ($i=0;$i<strlen($wdir);$i++)
466 $winddir=$winddir . __($wdir{$i},$tdom);
467 return $winddir;
468 }
469
470 }
471
472 /*
473 functions to check the wordpress transport methods
474
475 if wpf selected transport option is set to empty,
476 then we are in probing mode and do not change the wordpress result
477 else we only keep alive what was selected via admin dialog
478
479 */
480
481 function wpf_check_fsockopen($use, $args = array())
482 {
483 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
484 if ( $sel_transport == "" || $sel_transport == "default" )
485 return $use;
486 else if ( $sel_transport == "fsockopen" )
487 return true;
488 else
489 return false;
490 }
491
492 function wpf_check_fopen($use, $args = array())
493 {
494 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
495 if ( $sel_transport == "" || $sel_transport == "default" )
496 return $use;
497 else if ( $sel_transport == "fopen" )
498 return true;
499 else
500 return false;
501 }
502
503 function wpf_check_streams($use, $args = array())
504 {
505 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
506 if ( $sel_transport == "" || $sel_transport == "default" )
507 return $use;
508 else if ( $sel_transport == "streams" )
509 return true;
510 else
511 return false;
512 }
513
514 function wpf_check_exthttp($use, $args = array())
515 {
516 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
517 if ( $sel_transport == "" || $sel_transport == "default" )
518 return $use;
519 else if ( $sel_transport == "exthttp" )
520 return true;
521 else
522 return false;
523 }
524
525 function wpf_check_curl($use, $args = array())
526 {
527 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
528 if ( $sel_transport == "" || $sel_transport == "default" )
529 return $use;
530 else if ( $sel_transport == "curl" )
531 return true;
532 else
533 return false;
534 }
535
536 // function to get the list of supported transports ignoring
537 // any preset method
538 if (! function_exists("get_wp_transports") )
539 {
540 function get_wp_transports()
541 {
542 $tlist = array();
543
544 // remove but store selected transport
545 $wp_transport = wpf_get_option("wp-forecast-wp-transport");
546 wpf_update_option("wp-forecast-wp-transport","default");
547
548 // get wordpress default transports
549 $wplist = array();
550
551 if ( true === WP_Http_ExtHttp::test( array() ) )
552 $tlist[] = "exthttp";
553
554 if ( true === WP_Http_Fsockopen::test( array() ) )
555 $tlist[] = "fsockopen";
556
557 if ( true === WP_Http_Streams::test( array() ) )
558 $tlist[] = "streams";
559
560 // disabled fopen, since this class sends no headers
561 //if ( true === WP_Http_Fopen::test( array() ) )
562 // $tlist[] = "fopen";
563
564 if ( true === WP_Http_Curl::test( array() ) )
565 $tlist[] = "curl";
566
567
568 // write back selected transport
569 wpf_update_option("wp-forecast-wp-transport",$wp_transport);
570
571 return $tlist;
572 }
573 }
574
575 //
576 // function to turn on/off wp-forecast preselected transport
577 //
578 function switch_wpf_transport($sw)
579 {
580 $wptrans = "default";
581
582 if ($sw == true)
583 $wptrans = wpf_get_option("wp-forecast-pre-transport");
584
585 pdebug(1,"Setting preselected transport to ".$wptrans);
586 update_option("wp-forecast-wp-transport",$wptrans);
587 }
588 ?>
589