|
| 1 | +<?php |
| 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 { |
| 35 | + /** |
| 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 | + */ |
| 45 | + |
| 46 | + |
| 47 | + public static function get_with_headers($url, $store_in = null, $follow_redirects = false, $user_agent = false) { |
| 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; |
| 56 | + //@$fp = stream_socket_client('ssl://' . $url_info['host'] . ':' . $port, $errno, $errstr, 10, STREAM_CLIENT_CONNECT, stream_context_create(array('ssl' => array('ciphers' => 'ALL:!AES:!3DES:!RC4:@STRENGTH')))); |
| 57 | + @$fp = fsockopen('sslv3://' . $url_info['host'], $port, $errno, $errstr, 10); |
| 58 | + } else { |
| 59 | + $port = isset($url_info['port']) ? $url_info['port'] : 80; |
| 60 | + @$fp = fsockopen($url_info['host'], $port, $errno, $errstr, 10); |
| 61 | + } |
| 62 | + |
| 63 | + if($store_in) { |
| 64 | + $outfp = fopen($store_in, 'w'); |
| 65 | + if(!$outfp) return false; |
| 66 | + } |
| 67 | + if($fp) { |
| 68 | + stream_set_timeout($fp, 10); |
| 69 | + $head = 'GET ' . (isset($url_info['path']) ? $url_info['path'] : '/') . (isset($url_info['query']) ? '?' . $url_info['query'] : ''); |
| 70 | + $head .= " HTTP/1.0\r\nHost: " . (isset($url_info['host']) ? $url_info['host'] : '') . "\r\n"; |
| 71 | + $head .= "User-Agent: " . $user_agent . "\r\n"; |
| 72 | + if(isset($url_info['user'])) { |
| 73 | + if(!array_key_exists('pass', $url_info)) $url_info['pass'] = ''; |
| 74 | + $head .= "Authorization: basic " . base64_encode($url_info['user'] . ':' . $url_info['pass']) . "\r\n"; |
| 75 | + } |
| 76 | + $head .= "Connection: Close\r\n"; |
| 77 | + $head .= "Accept: */*\r\n\r\n"; |
| 78 | + |
| 79 | + $data = ''; |
| 80 | + $eoheader = false; |
| 81 | + fputs($fp, $head); |
| 82 | + while(!feof($fp)) { |
| 83 | + if($header = fgets($fp, 1024)) { |
| 84 | + if($eoheader == true) { |
| 85 | + if($store_in) fputs($outfp, $header); |
| 86 | + else $data .= $header; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if ($header == "\r\n") { |
| 91 | + $eoheader = true; |
| 92 | + continue; |
| 93 | + } else { |
| 94 | + $header = trim($header); |
| 95 | + } |
| 96 | + $sc_pos = strpos($header, ':'); |
| 97 | + if($sc_pos === false) { |
| 98 | + $headers['status'] = $header; |
| 99 | + $headers['http_code'] = intval(preg_replace('/^HTTP\/\d+\.\d+\s+(\d+)\s+.*$/', '$1', $header)); |
| 100 | + } else { |
| 101 | + $label = substr($header, 0, $sc_pos); |
| 102 | + $value = substr($header, $sc_pos + 1); |
| 103 | + $headers[strtolower($label)] = trim($value); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + fclose($fp); |
| 108 | + if(isset($headers['http_code']) && isset($headers['location']) && ($headers['http_code'] == 301 || $headers['http_code'] == 302) && $follow_redirects > 0) { |
| 109 | + if($store_in) fclose($outfp); |
| 110 | + return $self::get_with_headers($headers['location'], $store_in, $follow_redirects); |
| 111 | + } |
| 112 | + if($store_in) { |
| 113 | + fclose($outfp); |
| 114 | + |
| 115 | + $code = intval(preg_replace('/^HTTP\/\d+\.\d+\s+(\d+)\s+.*$/', '$1', $headers['status'])); |
| 116 | + if($code != 200) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + return $headers; |
| 120 | + } else { |
| 121 | + return array($headers, $data); |
| 122 | + } |
| 123 | + } else { |
| 124 | + if($store_in) { |
| 125 | + fclose($outfp); |
| 126 | + @unlink($store_in); |
| 127 | + } |
| 128 | + return false; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Gets the content of an url |
| 134 | + * |
| 135 | + * Checks for the php function file_get_contents and uses an alternative if not found |
| 136 | + * |
| 137 | + * @access public |
| 138 | + * @param string $url url to get |
| 139 | + * @return string url data including headers |
| 140 | + * @see file_get_contents |
| 141 | + */ |
| 142 | + public static function get($url) { |
| 143 | + if(function_exists('file_get_contents')) return file_get_contents($url); |
| 144 | + |
| 145 | + $fp = fopen($url, 'r'); |
| 146 | + $data = ''; |
| 147 | + while(!feof($fp)) { |
| 148 | + $data .= fgets($fp, 8192); |
| 149 | + } |
| 150 | + fclose($fp); |
| 151 | + |
| 152 | + return $data; |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + /** |
| 157 | + * Make a post request and get data |
| 158 | + * |
| 159 | + * Calls an url with a post request and returns the data - and optionally the header content |
| 160 | + * |
| 161 | + * @access public |
| 162 | + * @param string $url the url to call |
| 163 | + * @param string $data the post data to send |
| 164 | + * @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 |
| 165 | + * @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 |
| 166 | + * @see get_url_and_headers |
| 167 | + */ |
| 168 | + public static function post($url, $data, $get_headers = false, $user_agent = false) { |
| 169 | + $url_info = parse_url($url); |
| 170 | + if((isset($url_info['scheme']) && $url_info['scheme'] == 'https') || $url_info['port'] == 443) { |
| 171 | + $port = (!isset($url_info['port']) || !$url_info['port'] || $url_info['port'] == 443 || $url_info['port'] == 80) ? 443 : $url_info['port']; |
| 172 | + //@$fp = stream_socket_client('ssl://' . $url_info['host'] . ':' . $port, $errno, $errstr, 10, STREAM_CLIENT_CONNECT, stream_context_create(array('ssl' => array('ciphers' => 'ALL:!AES:!3DES:!RC4:@STRENGTH')))); |
| 173 | + @$fp = fsockopen('sslv3://' . $url_info['host'], $port, $errno, $errstr, 10); |
| 174 | + } else { |
| 175 | + $port = isset($url_info['port']) ? $url_info['port'] : 80; |
| 176 | + @$fp = fsockopen($url_info['host'], $port, $errno, $errstr, 10); |
| 177 | + } |
| 178 | + |
| 179 | + if(!$fp) return ''; |
| 180 | + |
| 181 | + if(!$user_agent) $user_agent = 'pxFW GET proxy'; |
| 182 | + |
| 183 | + $header = 'POST ' . (isset($url_info['path']) ? $url_info['path'] : '/') . (isset($url_info['query']) ? '?' . @$url_info['query'] : '') . " HTTP/1.1\r\n"; |
| 184 | + $header .= "Host: " . @$url_info['host'] . "\r\n"; |
| 185 | + $header .= "User-Agent: " . $user_agent . "\r\n"; |
| 186 | + if(isset($url_info['user'])) { |
| 187 | + if(!array_key_exists('pass', $url_info)) $url_info['pass'] = ''; |
| 188 | + $header .= "Authorization: basic " . base64_encode($url_info['user'] . ':' . $url_info['pass']) . "\r\n"; |
| 189 | + } |
| 190 | + $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
| 191 | + $header .= "Content-Length: " . strlen($data) . "\r\n"; |
| 192 | + $header .= "Connection: close\r\n\r\n"; |
| 193 | + $header .= $data . "\r\n\r\n"; |
| 194 | + |
| 195 | + fwrite($fp, $header); |
| 196 | + |
| 197 | + $response = ''; |
| 198 | + $eoheader = false; |
| 199 | + $header = ''; |
| 200 | + $tmpdata = ''; |
| 201 | + $chunked = false; |
| 202 | + $chunklen = 0; |
| 203 | + |
| 204 | + while(!feof($fp)) { |
| 205 | + if($header = @fgets($fp, 1024)) { |
| 206 | + if($eoheader == true) { |
| 207 | + $response .= $header; |
| 208 | + continue; |
| 209 | + } |
| 210 | + |
| 211 | + if ($header == "\r\n") { |
| 212 | + $eoheader = true; |
| 213 | + continue; |
| 214 | + } else { |
| 215 | + $tmpdata .= $header; |
| 216 | + if(preg_match('/Transfer-Encoding:\s+chunked/i', $tmpdata)) $chunked = true; |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + //var_dump($response, $chunked, $header); |
| 221 | + if($chunked == true) { |
| 222 | + $lines = explode("\n", $response); |
| 223 | + $response = ''; |
| 224 | + $chunklen = 0; |
| 225 | + foreach($lines as $line) { |
| 226 | + $line .= "\n"; |
| 227 | + if($chunklen <= 0) { |
| 228 | + if(preg_match('/^([0-9a-f]+)\s*$/is', $line, $matches)) { |
| 229 | + $chunklen = hexdec($matches[1]); |
| 230 | + } |
| 231 | + continue; |
| 232 | + } |
| 233 | + |
| 234 | + if(strlen($line) > $chunklen) { |
| 235 | + //echo "Warnung: " . strlen($line) . " > " . $chunklen . "\n"; |
| 236 | + $line = substr($line, 0, $chunklen); |
| 237 | + } |
| 238 | + $response .= $line; |
| 239 | + $chunklen -= strlen($line); |
| 240 | + } |
| 241 | + |
| 242 | + $start = strpos($response, '<?xml'); |
| 243 | + $end = strrpos($response, '>'); |
| 244 | + if($start !== false && $end !== false) $response = substr($response, $start, $end - $start + 1); |
| 245 | + } |
| 246 | + |
| 247 | + fclose($fp); |
| 248 | + |
| 249 | + if($get_headers == true) { |
| 250 | + $tmpheaders = explode("\n", $tmpdata); |
| 251 | + $headers = array(); |
| 252 | + foreach($tmpheaders as $cur) { |
| 253 | + if(preg_match('/^(\w+)\:\s*(.*)$/is', $cur, $matches)) { |
| 254 | + $headers["$matches[1]"] = trim($matches[2]); |
| 255 | + } |
| 256 | + } |
| 257 | + return array($headers, $response); |
| 258 | + } else return $response; |
| 259 | + } |
| 260 | + |
| 261 | +} |
| 262 | + |
| 263 | +?> |
0 commit comments