0a2379353d81165288cbb87bb54cd646601b611c
[ZNCAdmin.git] / zncadmin / HTTPConnector.class.php
1 <?php
2 /* HTTPConnector.class.php - HTTP Connector Class - ZNCAdmin
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
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.
9  * 
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.
14  * 
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/>. 
17  */
18
19 class HTTPConnector {
20         private $cookies = array();
21         
22         public function post($host, $post) {
23                 $ssl = false;
24                 if(strtolower(substr($host, 0, 7)) == 'http://') {
25                         $host = substr($host, 7);
26                 } else if(strtolower(substr($host, 0, 8)) == 'https://') {
27                         $host = substr($host, 8);
28                         $ssl = true;
29                 }
30                 $hexp = explode('/', $host, 2);
31                 $pexp = explode(':', $hexp[0]);
32                 $host = $pexp[0];
33                 if(count($pexp) > 1)
34                         $port = $pexp[1];
35                 else
36                         $port = 80;
37                 $rhost = $host;
38                 if($ssl){
39                         $rhost = "ssl://".$host;
40                 }
41                 $path="/".$hexp[1];
42                 $data = "";
43                 $fp = fsockopen($rhost, $port);
44                 fputs($fp, "POST $path HTTP/1.1\r\n");
45                 foreach($post as $key => $val) {
46             if(is_array($val)) {
47                 foreach($val as $subval) {
48                     if($data != "") { $data.="&"; }
49                     $data .= $key."=".$subval;
50                 }
51             } else {
52                 if($data != "") { $data.="&"; }
53                 $data .= $key."=".$val;
54             }
55                 }
56                 fputs($fp, "Accept-Language: de-DE\r\n");
57                 fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
58                 fputs($fp, "Accept-Encoding: deflate\r\n");
59                 fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)\r\n");
60                 fputs($fp, "Host: ".$host."\r\n");
61                 foreach($this->cookies as $cookiename => $cookievalue) {
62                         fputs($fp, "Cookie: ".$cookiename."=".$cookievalue."\r\n");
63                 }
64                 fputs($fp, "Content-Length: ".strlen($data)."\r\n");
65                 fputs($fp, "Connection: Keep-Alive\r\n");
66                 fputs($fp, "Cache-Control: no-cache\r\n");
67                 fputs($fp, "\r\n");
68                 fputs($fp, $data);
69         $res = "";
70                 while(!feof($fp)) {
71                         $res .= fread($fp, 256);
72                 }
73                 $exp = explode("\n", str_replace("\r", "", $res));
74                 for($i=0;$i<count($exp);$i++) {
75                         $expb = explode(" ",$exp[$i],2);
76                         if($expb[0] == "Set-Cookie:") {
77                                 $cookie = explode("=", $expb[1], 2);
78                                 $cookieval = explode(";", $cookie[1]);
79                                 $this->cookies[$cookie[0]] = $cookieval[0];
80                         } else if($exp[$i] == "") {
81                                 break;
82                         }
83                 }
84                 fclose($fp);
85                 return $res;
86         }
87         
88         public function get($host) {
89                 $ssl = false;
90                 if(strtolower(substr($host, 0, 7)) == 'http://') {
91                         $host = substr($host, 7);
92                 } else if(strtolower(substr($host, 0, 8)) == 'https://') {
93                         $host = substr($host, 8);
94                         $ssl = true;
95                 }
96                 $hexp = explode('/', $host, 2);
97                 $pexp = explode(':', $hexp[0]);
98                 $host = $pexp[0];
99                 if(count($pexp) > 1)
100                         $port = $pexp[1];
101                 else
102                         $port = 80;
103                 $rhost = $host;
104                 if($ssl){
105                         $rhost = "ssl://".$host;
106                 }
107                 $path="/".$hexp[1];
108                 $fp = fsockopen($rhost, $port);
109                 fputs($fp, "GET $path HTTP/1.1\r\n");
110                 fputs($fp, "Accept-Language: de-DE\r\n");
111                 fputs($fp, "Accept-Encoding: deflate\r\n");
112                 fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)\r\n");
113                 fputs($fp, "Host: ".$host."\r\n");
114                 foreach($this->cookies as $cookiename => $cookievalue) {
115                         fputs($fp, "Cookie: ".$cookiename."=".$cookievalue."\r\n");
116                 }
117                 fputs($fp, "Connection: Keep-Alive\r\n");
118                 fputs($fp, "Cache-Control: no-cache\r\n");
119                 fputs($fp, "\r\n");
120         $res = "";
121                 while(!feof($fp)) {
122                         $res .= fread($fp, 256);
123                 }
124                 $exp = explode("\n", str_replace("\r", "", $res));
125                 for($i=0;$i<count($exp);$i++) {
126                         $expb = explode(" ",$exp[$i],2);
127                         if($expb[0] == "Set-Cookie:") {
128                                 $cookie = explode("=", $expb[1], 2);
129                                 $cookieval = explode(";", $cookie[1]);
130                                 $this->cookies[$cookie[0]] = $cookieval[0];
131                         } else if($exp[$i] == "") {
132                                 break;
133                         }
134                 }
135                 fclose($fp);
136                 return $res;
137         }
138         
139         
140 }
141
142 ?>