2 /* HTTPConnector.class.php - HTTP Connector Class
3 * Copyright (C) 2011-2013 Philipp Kreil (pk910)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 private $cookies = array();
21 public $debug = false;
23 private $default_header = array(
24 "Accept-Encoding" => "deflate",
25 "Accept-Language" => "de-DE",
26 "User-Agent" => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
27 "Connection" => "Keep-Alive",
28 "Cache-Control" => "no-cache",
31 private function fputs($fp, $line) {
37 private function create_request($host) {
38 if(!preg_match('#^((http|https)://|)(([a-zA-Z0-9][a-zA-Z0-9_-]+[a-zA-Z0-9]\.)+[a-zA-Z]{2,5}|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})(:([0-9]{1,5})|)(/.*|)$#', $host, $matches))
40 if(empty($matches[3]))
43 $request['host'] = $matches[3];
44 $request['ssl'] = (strtolower($matches[2]) == "https");
45 $request['port'] = ($matches[6] ? $matches[6] : 80);
46 $request['path'] = ($matches[7] ? $matches[7] : "/");
47 $request['header'] = $this->default_header;
48 $request['header_only'] = false;
49 $this->set_header($request, "Host", $request['host']);
51 $host = ($request['ssl'] ? "ssl://" : "").$request['host'];
52 $request['fp'] = fsockopen($host, $request['port']);
56 private function set_header(&$req, $header, $value) {
57 foreach($req['header'] as $name => $val) {
58 if(strtolower($name) == strtolower($header)) {
59 unset($req['header'][$name]);
63 $req['header'][$header] = $value;
66 private function send_header($req) {
67 foreach($req['header'] as $name => $val) {
68 $this->fputs($req['fp'], $name.": ".$val."\r\n");
71 foreach($this->cookies as $cookiename => $cookievalue) {
74 $cookiestr .= $cookiename."=".$cookievalue;
77 $this->fputs($req['fp'], "Cookie: ".$cookiestr."\r\n");
80 private function process_request(&$req, $data = null) {
81 $this->fputs($req['fp'], $req['method']." ".$req['path']." HTTP/1.1\r\n");
82 $this->send_header($req);
83 $this->fputs($req['fp'], "\r\n");
85 $this->fputs($req['fp'], $data);
87 $req['reply_header'] = array();
91 while(!feof($req['fp'])) {
93 $recv = fgets($req['fp']);
96 $line = str_replace(array("\r", "\n"), array("", ""), $recv);
98 $exp = explode(" ", $line, 3);
99 $req['state'] = $exp[1];
100 $req['state_text'] = $exp[2];
104 else if($line == "") {
105 $recv_header = false;
106 if($req['header_only'])
111 $line = explode(": ", $line, 2);
112 if(strtolower($line[0]) == "set-cookie") {
113 $cookie = explode("=", $line[1], 2);
114 $cookieval = explode(";", $cookie[1]);
115 $this->cookies[$cookie[0]] = $cookieval[0];
117 $req['reply_header'][$line[0]] = $line[1];
119 $recv = fread($req['fp'], 512);
122 $req['reply'] .= $recv;
125 if(!$req['header_only'])
129 public function post($host, $post, $full = false) {
130 $req = $this->create_request($host);
133 $req['method'] = "POST";
136 foreach($post as $key => $val) {
138 foreach($val as $subval) {
139 if($data != "") { $data.="&"; }
140 $data .= urlencode($key)."=".urlencode($subval);
143 if($data != "") { $data.="&"; }
144 $data .= urlencode($key)."=".urlencode($val);
148 $this->set_header($req, "Content-Type", "application/x-www-form-urlencoded");
149 $this->set_header($req, "Content-Length", strlen($data));
150 $this->process_request($req, $data);
154 return $req['reply'];
157 public function get($host, $full) {
158 $req = $this->create_request($host);
161 $req['method'] = "GET";
163 $this->process_request($req);
167 return $req['reply'];
170 public function connect($host, $target, $full = false) {
171 $req = $this->create_request($host);
174 $req['method'] = "CONNECT";
175 $req['path'] = $target;
176 $req['header_only'] = true;
178 $this->process_request($req);
182 return ($req['state'] == "200" ? $req['fp'] : NULL);