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

274 lines 6.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,2007 Hans Matzen (email : webmaster at tuxlog.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 global $wpf_debug;
29
30 if ($wpf_debug > 0)
31 pdebug("Start of fetchURL ()");
32
33 // get timeout parameter
34 $timeout = get_option("wp-forecast-timeout");
35 if ( $timeout =="")
36 $timeout = 30;
37
38 $url_parsed = parse_url($url);
39 $host = $url_parsed["host"];
40 if (!isset($url_parsed["port"])) {
41 $port = 80;
42 }
43 else {
44 $port = $url_parsed["port"];
45 }
46 $path = $url_parsed["path"];
47 if ($url_parsed["query"] != "") $path .= "?" . $url_parsed["query"];
48 $out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
49 $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
50 // set timeout for reading
51 stream_set_timeout($fp, $timeout);
52 $in = "";
53 if ($fp) {
54 fwrite($fp, $out);
55 $body = false;
56 while (!feof($fp)) {
57 $s = fgets($fp, 1024);
58 if ($body) $in .= $s;
59 if ($s == "\r\n") $body = true;
60 }
61 fclose($fp);
62 }
63
64 if ($wpf_debug > 0)
65 pdebug("End of fetchURL ()");
66
67 return $in;
68 }
69
70 //
71 // converts an array to an cookie like string
72 // e.g. a=5&b=6&c=7
73 function arr2str($a)
74 {
75 global $wpf_debug;
76
77 if ($wpf_debug > 0)
78 pdebug("Start of arr2str ()");
79
80 $s="";
81 foreach ($a as $name => $value){
82 $s=$s . $name . "=" . $value . "&";
83 }
84
85 if ($wpf_debug > 0)
86 pdebug("End of arr2str ()");
87
88 return $s;
89 }
90
91 //
92 // converts a cookie like string (see above) to an array
93 //
94 function str2arr($s)
95 {
96 global $wpf_debug;
97
98 if ($wpf_debug > 0)
99 pdebug("Start of str2arr ()");
100
101 $a=array();
102 parse_str($s,$a);
103
104 if ($wpf_debug > 0)
105 pdebug("End of str2arr ()");
106
107 return $a;
108 }
109
110 //
111 // converts the given wind parameters into a suitable windstring
112 //
113 function windstr($metric,$wspeed,$windunit)
114 {
115 global $wpf_debug;
116
117 if ($wpf_debug > 0)
118 pdebug("Start of windstr ()");
119
120 // if its mph convert it to m/s
121 if ($metric != 1)
122 $wspeed = round($wspeed * 0.44704,0);
123
124 // convert it to selected unit
125 switch ($windunit) {
126 case "ms":
127 $wunit="m/s";
128 break;
129 case "kmh":
130 $wspeed=round($wspeed*3.6,0);
131 $wunit="km/h";
132 break;
133 case "mph":
134 $wspeed=round($wspeed*2.23694,0);
135 $wunit="mph";
136 break;
137 case "kts":
138 $wspeed=round($wspeed*1.9438,0);
139 $wunit="kts";
140 break;
141 }
142
143 if ($wpf_debug > 0)
144 pdebug("End of windstr ()");
145
146 return $wspeed." ".$wunit;
147 }
148
149
150 function get_wpf_opts($wpfcid)
151 {
152 global $wpf_debug;
153
154 if ($wpf_debug > 0)
155 pdebug("Start of get_wpf_opts ()");
156
157 $av=array();
158
159 // get options from database
160 $av['location']=get_option("wp-forecast-location".$wpfcid);
161 $av['locname']=get_option("wp-forecast-locname".$wpfcid);
162 $av['refresh']=get_option("wp-forecast-refresh".$wpfcid);
163 $av['metric']=get_option("wp-forecast-metric".$wpfcid);
164 $av['wpf_language']=get_option("wp-forecast-language".$wpfcid);
165 $av['daytime']=get_option("wp-forecast-daytime".$wpfcid);
166 $av['nighttime']=get_option("wp-forecast-nighttime".$wpfcid);
167 $av['fc_date_format']=get_option("date_format");
168 $av['fc_time_format']=get_option("time_format");
169 $av['dispconfig']=get_option("wp-forecast-dispconfig".$wpfcid);
170 $av['windunit']=get_option("wp-forecast-windunit".$wpfcid);
171 $av['currtime']=get_option("wp-forecast-currtime".$wpfcid);
172 $av['title'] = get_option("wp-forecast-title".$wpfcid);
173 $av['xmlerror']="";
174
175 // set accuweather uri
176 $av['BASE_URI']="http://forecastfox.accuweather.com/adcbin/forecastfox/weather_data.asp?";
177
178 if ($wpf_debug > 0)
179 pdebug("End of get_wpf_opts ()");
180
181 return $av;
182 }
183
184
185 //
186 // build the url from the parameters and fetch the weather-data
187 // return it as one long string
188 //
189 function get_weather($uri,$loc,$metric)
190 {
191 global $wpf_debug;
192
193 if ($wpf_debug > 0)
194 pdebug("Start of get_weather ()");
195
196 $url=$uri . "location=" . urlencode($loc) . "&metric=" .
197 $metric;// . "&partner=forecastfox";
198
199 $xml = fetchURL($url);
200
201 if ($wpf_debug > 0)
202 pdebug("End of get_weather ()");
203
204 return $xml;
205 }
206
207 //
208 // just return the css link
209 // this function is called via the wp_head hook
210 //
211 function wp_forecast_css($wpfcid="A") {
212
213 global $wpf_debug;
214
215 if ($wpf_debug > 0)
216 pdebug("Start of function wp_forecast_css ()");
217
218 $plugin_path = get_settings('siteurl') . '/wp-content/plugins/wp-forecast';
219 echo "<link rel=\"stylesheet\" href=\"". $plugin_path. "/wp-forecast.css\" type=\"text/css\" media=\"screen\" />\n";
220
221
222 if ($wpf_debug > 0)
223 pdebug("End of function wp_forecast_css ()");
224 }
225
226
227 //
228 // just return the css link when not using wordpress
229 // this function is called when showing widget directly via wp-forecast-show.php
230 //
231 function wp_forecast_css_nowp($wpfcid="A") {
232
233 global $wpf_debug;
234
235 if ($wpf_debug > 0)
236 pdebug("Start of function wp_forecast_css_nowp ()");
237
238 $plugin_path = get_settings('siteurl') . '/wp-content/plugins/wp-forecast';
239 echo "<link rel=\"stylesheet\" href=\"". $plugin_path. "/wp-forecast-nowp.css\" type=\"text/css\" media=\"screen\" />\n";
240
241
242 if ($wpf_debug > 0)
243 pdebug("End of function wp_forecast_css ()");
244 }
245
246
247 //
248 // little debug output routine
249 //
250 function pdebug($dstr)
251 {
252 echo $dstr."<br />\n";
253 }
254
255 //
256 // returns the number's widget id used with wp-forecast
257 // maximum is 999999 :-)
258 //
259 function get_widget_id($number)
260 {
261 // if negative take the first id
262 if ($number < 0 )
263 return "A";
264
265 // the first widgets use chars above we go with 0 padded numbers
266 if ( $number <= 25 )
267 return substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",$number,1);
268 else
269 return str_pad($number, 6, "0", STR_PAD_LEFT);
270 }
271
272 }
273 ?>
274