fixed possible endless loop in HTTPConnector.class.php
[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         if(!$fp)
45             return;
46                 fputs($fp, "POST $path HTTP/1.1\r\n");
47                 foreach($post as $key => $val) {
48             if(is_array($val)) {
49                 foreach($val as $subval) {
50                     if($data != "") { $data.="&"; }
51                     $data .= $key."=".$subval;
52                 }
53             } else {
54                 if($data != "") { $data.="&"; }
55                 $data .= $key."=".$val;
56             }
57                 }
58                 fputs($fp, "Accept-Language: de-DE\r\n");
59                 fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
60                 fputs($fp, "Accept-Encoding: deflate\r\n");
61                 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");
62                 fputs($fp, "Host: ".$host."\r\n");
63                 foreach($this->cookies as $cookiename => $cookievalue) {
64                         fputs($fp, "Cookie: ".$cookiename."=".$cookievalue."\r\n");
65                 }
66                 fputs($fp, "Content-Length: ".strlen($data)."\r\n");
67                 fputs($fp, "Connection: Keep-Alive\r\n");
68                 fputs($fp, "Cache-Control: no-cache\r\n");
69                 fputs($fp, "\r\n");
70                 fputs($fp, $data);
71         $res = "";
72                 while(!feof($fp)) {
73                         $res .= fread($fp, 256);
74                 }
75                 $exp = explode("\n", str_replace("\r", "", $res));
76                 for($i=0;$i<count($exp);$i++) {
77                         $expb = explode(" ",$exp[$i],2);
78                         if($expb[0] == "Set-Cookie:") {
79                                 $cookie = explode("=", $expb[1], 2);
80                                 $cookieval = explode(";", $cookie[1]);
81                                 $this->cookies[$cookie[0]] = $cookieval[0];
82                         } else if($exp[$i] == "") {
83                                 break;
84                         }
85                 }
86                 fclose($fp);
87                 return $res;
88         }
89         
90         public function get($host) {
91                 $ssl = false;
92                 if(strtolower(substr($host, 0, 7)) == 'http://') {
93                         $host = substr($host, 7);
94                 } else if(strtolower(substr($host, 0, 8)) == 'https://') {
95                         $host = substr($host, 8);
96                         $ssl = true;
97                 }
98                 $hexp = explode('/', $host, 2);
99                 $pexp = explode(':', $hexp[0]);
100                 $host = $pexp[0];
101                 if(count($pexp) > 1)
102                         $port = $pexp[1];
103                 else
104                         $port = 80;
105                 $rhost = $host;
106                 if($ssl){
107                         $rhost = "ssl://".$host;
108                 }
109                 $path="/".$hexp[1];
110                 $fp = fsockopen($rhost, $port);
111         if(!$fp)
112             return;
113                 fputs($fp, "GET $path HTTP/1.1\r\n");
114                 fputs($fp, "Accept-Language: de-DE\r\n");
115                 fputs($fp, "Accept-Encoding: deflate\r\n");
116                 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");
117                 fputs($fp, "Host: ".$host."\r\n");
118                 foreach($this->cookies as $cookiename => $cookievalue) {
119                         fputs($fp, "Cookie: ".$cookiename."=".$cookievalue."\r\n");
120                 }
121                 fputs($fp, "Connection: Keep-Alive\r\n");
122                 fputs($fp, "Cache-Control: no-cache\r\n");
123                 fputs($fp, "\r\n");
124         $res = "";
125                 while(!feof($fp)) {
126                         $res .= fread($fp, 256);
127                 }
128                 $exp = explode("\n", str_replace("\r", "", $res));
129                 for($i=0;$i<count($exp);$i++) {
130                         $expb = explode(" ",$exp[$i],2);
131                         if($expb[0] == "Set-Cookie:") {
132                                 $cookie = explode("=", $expb[1], 2);
133                                 $cookieval = explode(";", $cookie[1]);
134                                 $this->cookies[$cookie[0]] = $cookieval[0];
135                         } else if($exp[$i] == "") {
136                                 break;
137                         }
138                 }
139                 fclose($fp);
140                 return $res;
141         }
142         
143         
144 }
145
146 ?>