]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/MOBIClass/Http.php
3 private static $cache = false;
5 public static function Request($url){
6 $url_parts = parse_url($url);
7 $url_parts["port"] = isset($url_parts["port"]) ? $url_parts["port"] : 80;
8 $url_parts["path"] = isset($url_parts["path"]) ? $url_parts["path"] : "/";
10 return self
::FullRequest("GET", $url_parts["host"], $url_parts["port"], $url_parts["path"]);
13 public static function FullRequest(
14 $verb = 'GET', /* HTTP Request Method (GET and POST supported) */
15 $ip, /* Target IP/Hostname */
16 $port = 80, /* Target TCP port */
17 $uri = '/', /* Target URI */
18 $getdata = array(), /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */
19 $postdata = array(), /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */
20 $cookie = array(), /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */
21 $custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */
22 $timeout = 1000, /* Socket timeout in milliseconds */
23 $req_hdr = false, /* Include HTTP request headers */
24 $res_hdr = false, /* Include HTTP response headers */
25 $depth = 4 /* Depth of the iteration left (to avoid redirection loops) */
29 $cacheFile = "cache/".$ip."/".str_replace("/", "...", $uri);
31 if(is_file($cacheFile)){
32 $data = file_get_contents($cacheFile);
34 return self
::resolveTruncated($data);
38 $verb = strtoupper($verb);
40 $getdata_str = count($getdata) ? '?' : '';
43 foreach ($getdata as $k => $v)
44 $getdata_str .= urlencode($k) .'='. urlencode($v);
46 foreach ($postdata as $k => $v)
47 $postdata_str .= urlencode($k) .'='. urlencode($v) .'&';
49 foreach ($cookie as $k => $v)
50 $cookie_str .= urlencode($k) .'='. urlencode($v) .'; ';
53 $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf;
54 $req .= 'Host: '. $ip . $crlf;
55 $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf;
56 $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf;
57 $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf;
58 $req .= 'Accept-Encoding: deflate' . $crlf;
59 $req .= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' . $crlf;
62 foreach ($custom_headers as $k => $v)
63 $req .= $k .': '. $v . $crlf;
65 if (!empty($cookie_str))
66 $req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf;
68 if ($verb == 'POST' && !empty($postdata_str))
70 $postdata_str = substr($postdata_str, 0, -1);
71 $req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf;
72 $req .= 'Content-Length: '. strlen($postdata_str) . $crlf . $crlf;
73 $req .= $postdata_str;
80 if (($fp = @fsockopen($ip, $port, $errno, $errstr)) == false)
81 return "Error $errno: $errstr\n";
83 stream_set_timeout($fp, 0, $timeout * 1000);
86 $ret .= stream_get_contents($fp);
89 $headerSplit = strpos($ret, "\r\n\r\n");
90 $header = substr($ret, 0, $headerSplit);
92 $redirectURL = self
::CheckForRedirect($header);
94 if($redirectURL !== false){
96 $url_parts = parse_url($redirectURL);
97 $url_parts["port"] = isset($url_parts["port"]) ? $url_parts["port"] : 80;
98 $url_parts["path"] = isset($url_parts["path"]) ? $url_parts["path"] : "/";
100 return self
::FullRequest($verb, $url_parts["host"], $url_parts["port"], $url_parts["path"], $getdata, $postdata, $cookie, $custom_headers, $timeout, $req_hdr, $res_hdr, $depth-1);
102 return "Redirect loop, stopping...";
107 $headerLines = explode("\r\n", $header);
108 foreach($headerLines as $line){
109 list($name, $value) = explode(":", $line);
111 $value = trim($value);
113 if(strtolower($name) == "transfer-encoding" && strtolower($value) == "chunked"){ //TODO: Put right values!
119 $ret = substr($ret, $headerSplit +
4);
122 $ret = self
::resolveTruncated($ret);
125 if(!is_dir("cache")){
128 if(!is_dir("cache/".$ip)){
131 if(!is_file("cache/".$ip."/".str_replace("/", "...", $uri))){
132 $h = fopen("cache/".$ip."/".str_replace("/", "...", $uri), "w");
141 private static function resolveTruncated($data){
143 $end = strlen($data);
147 $endVal = strpos($data, "\r\n", $pos);
148 $value = hexdec(substr($data, $pos, $endVal-$pos));
149 $out .= substr($data, $endVal+
2, $value);
150 $pos = $endVal+
2+
$value;
156 private static function CheckForRedirect($header){
157 $firstLine = substr($header, 0, strpos($header, "\r\n"));
158 list($httpVersion, $statusCode, $message) = explode(" ", $firstLine);
160 if(substr($statusCode, 0, 1) == "3"){
161 $part = substr($header, strpos(strtolower($header), "location: ")+
strlen("location: "));
162 $location = trim(substr($part, 0, strpos($part, "\r\n")));
164 if(strlen($location) > 0){