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

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