commit | author | age
|
e372dd
|
1 |
<?php |
M |
2 |
|
|
3 |
/* |
|
4 |
Copyright (c) 2013, Marius Cramer, pixcept KG |
|
5 |
All rights reserved. |
|
6 |
|
|
7 |
Redistribution and use in source and binary forms, with or without modification, |
|
8 |
are permitted provided that the following conditions are met: |
|
9 |
|
|
10 |
* Redistributions of source code must retain the above copyright notice, |
|
11 |
this list of conditions and the following disclaimer. |
|
12 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
13 |
this list of conditions and the following disclaimer in the documentation |
|
14 |
and/or other materials provided with the distribution. |
|
15 |
* Neither the name of ISPConfig nor the names of its contributors |
|
16 |
may be used to endorse or promote products derived from this software without |
|
17 |
specific prior written permission. |
|
18 |
|
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
*/ |
|
30 |
|
|
31 |
//* The purpose of this library is to provide some general functions. |
|
32 |
//* This class is loaded automatically by the ispconfig framework. |
|
33 |
|
|
34 |
abstract class ISPConfigRequest { |
b1a6a5
|
35 |
/** |
MC |
36 |
* Get header data and contents from an url |
|
37 |
* |
|
38 |
* Calls an url and returns an array containing the http header and the page content |
|
39 |
* |
|
40 |
* @access public |
|
41 |
* @param string $url the url to call |
|
42 |
* @param string $store_in the file to store the data in instead of returning them |
|
43 |
* @return array The array with header data at index 0 and page content at index 1, returns boolean false on error. If $store_in is set only the headers are returned |
|
44 |
*/ |
e372dd
|
45 |
|
M |
46 |
|
b1a6a5
|
47 |
public static function get_with_headers($url, $store_in = null, $follow_redirects = false, $user_agent = false) { |
MC |
48 |
if($follow_redirects === true) $follow_redirects = 5; |
|
49 |
elseif($follow_redirects !== false) $follow_redirects--; |
|
50 |
|
|
51 |
if(!$user_agent) $user_agent = 'pxFW GET proxy'; |
|
52 |
|
|
53 |
$url_info = parse_url($url); |
|
54 |
if(isset($url_info['scheme']) && $url_info['scheme'] == 'https') { |
|
55 |
$port = isset($url_info['port']) ? $url_info['port'] : 443; |
797215
|
56 |
@$fp = fsockopen('tls://' . $url_info['host'], $port, $errno, $errstr, 10); |
b1a6a5
|
57 |
} else { |
MC |
58 |
$port = isset($url_info['port']) ? $url_info['port'] : 80; |
|
59 |
@$fp = fsockopen($url_info['host'], $port, $errno, $errstr, 10); |
|
60 |
} |
|
61 |
|
|
62 |
if($store_in) { |
|
63 |
$outfp = fopen($store_in, 'w'); |
|
64 |
if(!$outfp) return false; |
|
65 |
} |
|
66 |
if($fp) { |
|
67 |
stream_set_timeout($fp, 10); |
|
68 |
$head = 'GET ' . (isset($url_info['path']) ? $url_info['path'] : '/') . (isset($url_info['query']) ? '?' . $url_info['query'] : ''); |
|
69 |
$head .= " HTTP/1.0\r\nHost: " . (isset($url_info['host']) ? $url_info['host'] : '') . "\r\n"; |
|
70 |
$head .= "User-Agent: " . $user_agent . "\r\n"; |
|
71 |
if(isset($url_info['user'])) { |
|
72 |
if(!array_key_exists('pass', $url_info)) $url_info['pass'] = ''; |
|
73 |
$head .= "Authorization: basic " . base64_encode($url_info['user'] . ':' . $url_info['pass']) . "\r\n"; |
|
74 |
} |
|
75 |
$head .= "Connection: Close\r\n"; |
|
76 |
$head .= "Accept: */*\r\n\r\n"; |
|
77 |
|
|
78 |
$data = ''; |
|
79 |
$eoheader = false; |
|
80 |
fputs($fp, $head); |
|
81 |
while(!feof($fp)) { |
|
82 |
if($header = fgets($fp, 1024)) { |
|
83 |
if($eoheader == true) { |
|
84 |
if($store_in) fputs($outfp, $header); |
|
85 |
else $data .= $header; |
|
86 |
continue; |
|
87 |
} |
|
88 |
|
|
89 |
if ($header == "\r\n") { |
|
90 |
$eoheader = true; |
|
91 |
continue; |
|
92 |
} else { |
|
93 |
$header = trim($header); |
|
94 |
} |
|
95 |
$sc_pos = strpos($header, ':'); |
|
96 |
if($sc_pos === false) { |
|
97 |
$headers['status'] = $header; |
|
98 |
$headers['http_code'] = intval(preg_replace('/^HTTP\/\d+\.\d+\s+(\d+)\s+.*$/', '$1', $header)); |
|
99 |
} else { |
|
100 |
$label = substr($header, 0, $sc_pos); |
|
101 |
$value = substr($header, $sc_pos + 1); |
|
102 |
$headers[strtolower($label)] = trim($value); |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
fclose($fp); |
|
107 |
if(isset($headers['http_code']) && isset($headers['location']) && ($headers['http_code'] == 301 || $headers['http_code'] == 302) && $follow_redirects > 0) { |
|
108 |
if($store_in) fclose($outfp); |
|
109 |
return $self::get_with_headers($headers['location'], $store_in, $follow_redirects); |
|
110 |
} |
|
111 |
if($store_in) { |
|
112 |
fclose($outfp); |
|
113 |
|
|
114 |
$code = intval(preg_replace('/^HTTP\/\d+\.\d+\s+(\d+)\s+.*$/', '$1', $headers['status'])); |
|
115 |
if($code != 200) { |
|
116 |
return false; |
|
117 |
} |
|
118 |
return $headers; |
|
119 |
} else { |
|
120 |
return array($headers, $data); |
|
121 |
} |
|
122 |
} else { |
|
123 |
if($store_in) { |
|
124 |
fclose($outfp); |
|
125 |
@unlink($store_in); |
|
126 |
} |
|
127 |
return false; |
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* Gets the content of an url |
|
133 |
* |
|
134 |
* Checks for the php function file_get_contents and uses an alternative if not found |
|
135 |
* |
|
136 |
* @access public |
|
137 |
* @param string $url url to get |
|
138 |
* @return string url data including headers |
|
139 |
* @see file_get_contents |
|
140 |
*/ |
|
141 |
public static function get($url) { |
|
142 |
if(function_exists('file_get_contents')) return file_get_contents($url); |
|
143 |
|
|
144 |
$fp = fopen($url, 'r'); |
|
145 |
$data = ''; |
|
146 |
while(!feof($fp)) { |
|
147 |
$data .= fgets($fp, 8192); |
|
148 |
} |
|
149 |
fclose($fp); |
|
150 |
|
|
151 |
return $data; |
|
152 |
} |
|
153 |
|
|
154 |
|
|
155 |
/** |
|
156 |
* Make a post request and get data |
|
157 |
* |
|
158 |
* Calls an url with a post request and returns the data - and optionally the header content |
|
159 |
* |
|
160 |
* @access public |
|
161 |
* @param string $url the url to call |
|
162 |
* @param string $data the post data to send |
|
163 |
* @param bool $get_headers if true, the function will return an array like PXUrl::get_with_headers(), otherwise the content is returned as a string |
|
164 |
* @return mixed Content data as string or - if get_headers is true - the array with header data at index 0 and page content at index 1 |
|
165 |
* @see get_url_and_headers |
|
166 |
*/ |
|
167 |
public static function post($url, $data, $get_headers = false, $user_agent = false) { |
|
168 |
$url_info = parse_url($url); |
|
169 |
if((isset($url_info['scheme']) && $url_info['scheme'] == 'https') || $url_info['port'] == 443) { |
|
170 |
$port = (!isset($url_info['port']) || !$url_info['port'] || $url_info['port'] == 443 || $url_info['port'] == 80) ? 443 : $url_info['port']; |
797215
|
171 |
@$fp = fsockopen('tls://' . $url_info['host'], $port, $errno, $errstr, 10); |
b1a6a5
|
172 |
} else { |
MC |
173 |
$port = isset($url_info['port']) ? $url_info['port'] : 80; |
|
174 |
@$fp = fsockopen($url_info['host'], $port, $errno, $errstr, 10); |
|
175 |
} |
|
176 |
|
|
177 |
if(!$fp) return ''; |
|
178 |
|
|
179 |
if(!$user_agent) $user_agent = 'pxFW GET proxy'; |
|
180 |
|
|
181 |
$header = 'POST ' . (isset($url_info['path']) ? $url_info['path'] : '/') . (isset($url_info['query']) ? '?' . @$url_info['query'] : '') . " HTTP/1.1\r\n"; |
|
182 |
$header .= "Host: " . @$url_info['host'] . "\r\n"; |
|
183 |
$header .= "User-Agent: " . $user_agent . "\r\n"; |
|
184 |
if(isset($url_info['user'])) { |
|
185 |
if(!array_key_exists('pass', $url_info)) $url_info['pass'] = ''; |
|
186 |
$header .= "Authorization: basic " . base64_encode($url_info['user'] . ':' . $url_info['pass']) . "\r\n"; |
|
187 |
} |
|
188 |
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
|
189 |
$header .= "Content-Length: " . strlen($data) . "\r\n"; |
|
190 |
$header .= "Connection: close\r\n\r\n"; |
|
191 |
$header .= $data . "\r\n\r\n"; |
|
192 |
|
|
193 |
fwrite($fp, $header); |
|
194 |
|
|
195 |
$response = ''; |
|
196 |
$eoheader = false; |
|
197 |
$header = ''; |
|
198 |
$tmpdata = ''; |
|
199 |
$chunked = false; |
|
200 |
$chunklen = 0; |
|
201 |
|
|
202 |
while(!feof($fp)) { |
|
203 |
if($header = @fgets($fp, 1024)) { |
|
204 |
if($eoheader == true) { |
|
205 |
$response .= $header; |
|
206 |
continue; |
|
207 |
} |
|
208 |
|
|
209 |
if ($header == "\r\n") { |
|
210 |
$eoheader = true; |
|
211 |
continue; |
|
212 |
} else { |
|
213 |
$tmpdata .= $header; |
|
214 |
if(preg_match('/Transfer-Encoding:\s+chunked/i', $tmpdata)) $chunked = true; |
|
215 |
} |
|
216 |
} |
|
217 |
} |
|
218 |
//var_dump($response, $chunked, $header); |
|
219 |
if($chunked == true) { |
|
220 |
$lines = explode("\n", $response); |
|
221 |
$response = ''; |
|
222 |
$chunklen = 0; |
|
223 |
foreach($lines as $line) { |
|
224 |
$line .= "\n"; |
|
225 |
if($chunklen <= 0) { |
|
226 |
if(preg_match('/^([0-9a-f]+)\s*$/is', $line, $matches)) { |
|
227 |
$chunklen = hexdec($matches[1]); |
|
228 |
} |
|
229 |
continue; |
|
230 |
} |
|
231 |
|
|
232 |
if(strlen($line) > $chunklen) { |
|
233 |
//echo "Warnung: " . strlen($line) . " > " . $chunklen . "\n"; |
|
234 |
$line = substr($line, 0, $chunklen); |
|
235 |
} |
|
236 |
$response .= $line; |
|
237 |
$chunklen -= strlen($line); |
|
238 |
} |
|
239 |
|
|
240 |
$start = strpos($response, '<?xml'); |
|
241 |
$end = strrpos($response, '>'); |
|
242 |
if($start !== false && $end !== false) $response = substr($response, $start, $end - $start + 1); |
|
243 |
} |
|
244 |
|
|
245 |
fclose($fp); |
|
246 |
|
|
247 |
if($get_headers == true) { |
|
248 |
$tmpheaders = explode("\n", $tmpdata); |
|
249 |
$headers = array(); |
|
250 |
foreach($tmpheaders as $cur) { |
|
251 |
if(preg_match('/^(\w+)\:\s*(.*)$/is', $cur, $matches)) { |
|
252 |
$headers["$matches[1]"] = trim($matches[2]); |
|
253 |
} |
|
254 |
} |
|
255 |
return array($headers, $response); |
|
256 |
} else return $response; |
|
257 |
} |
|
258 |
|
e372dd
|
259 |
} |
M |
260 |
|
|
261 |
?> |