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

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