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

590 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 //wp_register_style('wpf_css', $plugin_url . $def);
375 //wp_enqueue_style( 'wpf_css');
376
377 echo '<link rel="stylesheet" id="wp-forecast-css" href="'. $plugin_url . $def . '" type="text/css" media="screen" />' ."\n";
378
379 pdebug(1,"End of function wp_forecast_css ()");
380 }
381
382
383 //
384 // just return the css link when not using wordpress
385 // this function is called when showing widget directly via wp-forecast-show.php
386 //
387 function wp_forecast_css_nowp($wpfcid="A")
388 {
389 pdebug(1,"Start of function wp_forecast_css_nowp ()");
390
391 $def = "wp-forecast-default-nowp.css";
392 $user = "wp-forecast-nowp.css";
393
394 if (file_exists( WP_PLUGIN_DIR . "/wp-forecast/" . $user))
395 $def =$user;
396
397 $plugin_url = plugins_url("wp-forecast/");
398
399 echo '<link rel="stylesheet" id="wp-forecast-nowp-css" href="'. $plugin_url . $def . '" type="text/css" media="screen" />' ."\n";
400
401 pdebug(1,"End of function wp_forecast_css_nowp ()");
402 }
403
404
405 //
406 // little debug output routine
407 //
408 if (! function_exists("pdebug") )
409 {
410 function pdebug($level,$dstr)
411 {
412 global $wpf_debug;
413 if ($wpf_debug >= $level)
414 echo $dstr."<br />\n";
415 }
416 }
417
418 //
419 // returns the number's widget id used with wp-forecast
420 // maximum is 999999 :-)
421 //
422 function get_widget_id($number)
423 {
424 // if negative take the first id
425 if ($number < 0 )
426 return "A";
427
428 // the first widgets use chars above we go with 0 padded numbers
429 if ( $number <= 25 )
430 return substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",$number,1);
431 else
432 return str_pad($number, 6, "0", STR_PAD_LEFT);
433 }
434
435 //
436 // function tries to determine the icon path for icon number ino
437 //
438 function find_icon($ino)
439 {
440 $path = ABSPATH . "wp-content/plugins/wp-forecast/icons/".$ino;
441 $ext=".gif";
442
443 if ( file_exists($path.".gif") )
444 $ext= ".gif";
445 else if ( file_exists($path.".png") )
446 $ext= ".png";
447 else if ( file_exists($path.".jpg") )
448 $ext= ".jpg";
449 else if ( file_exists($path.".GIF") )
450 $ext= ".GIF";
451 else if ( file_exists($path.".PNG") )
452 $ext= ".PNG";
453 else if ( file_exists($path.".JPG") )
454 $ext= ".JPG";
455 else if ( file_exists($path.".jpeg") )
456 $ext= ".jpeg";
457 else if ( file_exists($path.".JPEG") )
458 $ext= ".JPEG";
459 return $ino . $ext;
460 }
461
462 function translate_winddir($wdir,$tdom)
463 {
464 // translate winddir char by char
465 $winddir="";
466 for ($i=0;$i<strlen($wdir);$i++)
467 $winddir=$winddir . __($wdir{$i},$tdom);
468 return $winddir;
469 }
470
471 }
472
473 /*
474 functions to check the wordpress transport methods
475
476 if wpf selected transport option is set to empty,
477 then we are in probing mode and do not change the wordpress result
478 else we only keep alive what was selected via admin dialog
479
480 */
481
482 function wpf_check_fsockopen($use, $args = array())
483 {
484 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
485 if ( $sel_transport == "" || $sel_transport == "default" )
486 return $use;
487 else if ( $sel_transport == "fsockopen" )
488 return true;
489 else
490 return false;
491 }
492
493 function wpf_check_fopen($use, $args = array())
494 {
495 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
496 if ( $sel_transport == "" || $sel_transport == "default" )
497 return $use;
498 else if ( $sel_transport == "fopen" )
499 return true;
500 else
501 return false;
502 }
503
504 function wpf_check_streams($use, $args = array())
505 {
506 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
507 if ( $sel_transport == "" || $sel_transport == "default" )
508 return $use;
509 else if ( $sel_transport == "streams" )
510 return true;
511 else
512 return false;
513 }
514
515 function wpf_check_exthttp($use, $args = array())
516 {
517 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
518 if ( $sel_transport == "" || $sel_transport == "default" )
519 return $use;
520 else if ( $sel_transport == "exthttp" )
521 return true;
522 else
523 return false;
524 }
525
526 function wpf_check_curl($use, $args = array())
527 {
528 $sel_transport = wpf_get_option("wp-forecast-wp-transport");
529 if ( $sel_transport == "" || $sel_transport == "default" )
530 return $use;
531 else if ( $sel_transport == "curl" )
532 return true;
533 else
534 return false;
535 }
536
537 // function to get the list of supported transports ignoring
538 // any preset method
539 if (! function_exists("get_wp_transports") )
540 {
541 function get_wp_transports()
542 {
543 $tlist = array();
544
545 // remove but store selected transport
546 $wp_transport = wpf_get_option("wp-forecast-wp-transport");
547 wpf_update_option("wp-forecast-wp-transport","default");
548
549 // get wordpress default transports
550 $wplist = array();
551
552 //if ( true === WP_Http_ExtHttp::test( array() ) )
553 //$tlist[] = "exthttp";
554
555 if ( true === WP_Http_Fsockopen::test( array() ) )
556 $tlist[] = "fsockopen";
557
558 if ( true === WP_Http_Streams::test( array() ) )
559 $tlist[] = "streams";
560
561 // disabled fopen, since this class sends no headers
562 //if ( true === WP_Http_Fopen::test( array() ) )
563 // $tlist[] = "fopen";
564
565 if ( true === WP_Http_Curl::test( array() ) )
566 $tlist[] = "curl";
567
568
569 // write back selected transport
570 wpf_update_option("wp-forecast-wp-transport",$wp_transport);
571
572 return $tlist;
573 }
574 }
575
576 //
577 // function to turn on/off wp-forecast preselected transport
578 //
579 function switch_wpf_transport($sw)
580 {
581 $wptrans = "default";
582
583 if ($sw == true)
584 $wptrans = wpf_get_option("wp-forecast-pre-transport");
585
586 pdebug(1,"Setting preselected transport to ".$wptrans);
587 update_option("wp-forecast-wp-transport",$wptrans);
588 }
589 ?>
590