-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.php
More file actions
203 lines (187 loc) · 4.91 KB
/
Network.php
File metadata and controls
203 lines (187 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* @copyright Clock Limited 2010
* @version 3.2 - $Revision: 1329 $ - $Date: 2010-02-24 22:41:33 +0000 (Wed, 24 Feb 2010) $
* @package Core
*/
/**
* @author Paul Serby {@link mailto:paul.serby@clock.co.uk paul.serbyr@clock.co.uk }
* @copyright Clock Limited 2010
* @version 3.2 - $Revision: 1329 $ - $Date: 2010-02-24 22:41:33 +0000 (Wed, 24 Feb 2010) $
* @package Core
*/
class NetworkControl {
function proxyPost($host, $page, $port, $literal = null) {
$postvals = "";
$headers = "";
$requestHeaders = apache_request_headers();
$requestHeaders["Host"] = $host;
unset($requestHeaders["Cookie"]);
unset($requestHeaders["Accept-Encoding"]);
unset($requestHeaders["Connection"]);
$errno = "";
$errstr = "";
$sock = fsockopen ($host, $port, $errno, $errstr, 30);
if (!$sock) {
return "$errstr ($errno)<br>\n";
} else {
$postvals = file_get_contents('php://input');
foreach ($requestHeaders as $key => $value) {
$headers .= "$key: $value\r\n";
}
$method = $_SERVER["REQUEST_METHOD"];
$request = "$method $page HTTP/1.0\r\n" .
"$headers" .
"\r\n" .
"$postvals\r\n" .
"\r\n";
fputs($sock, $request);
$headers = "";
$body = "";
while (!feof($sock)) {
$body .= fread($sock, 4096);
}
fclose($sock);
}
// Get HTTP response code.
$responseArray = explode("\n", $body, 1);
$firstLine = explode(" ", $responseArray[0]);
if ($firstLine[1] != "200") {
mail("ed.pearson@clock.co.uk", "SMSGateway: Request", $request, true);
mail("ed.pearson@clock.co.uk", "SMSGateway: Unexpected Response", $body, true);
mail("paul.serby@clock.co.uk", "SMSGateway: Request", $request, true);
mail("paul.serby@clock.co.uk", "SMSGateway: Unexpected Response", $body, true);
}
return $body;
}
function postData($host, $page, $port, $data, $userHeader = null) {
$errno = "";
$errstr = "";
$sock = fsockopen ($host, $port, $errno, $errstr, 30);
if (!$sock) {
return "$errstr ($errno)<br>\n";
} else {
fputs($sock, "POST $page HTTP/1.0\r\n");
fputs($sock, "Host: $host\r\n");
// Add user header
if ($userHeader != null) {
fputs($sock, $userHeader);
}
fputs($sock, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($sock, "Content-length: " . mb_strlen($data) . "\r\n");
fputs($sock, "\r\n");
fputs($sock, "$data\r\n");
fputs($sock, "\r\n");
$headers = "";
while ($str = trim(fgets($sock, 4096))) {
$headers .= "$str\n";
}
$body = "";
while (!feof($sock)) {
$body .= fgets($sock, 4096);
}
fclose($sock);
}
$response = array();
$response["headers"] = $headers;
$response["body"] = $body;
return $response;
}
function getData($host, $page, $port, $userHeader = null) {
$errno = "";
$errstr = "";
$sock = @fsockopen($host, $port, $errno, $errstr, 30);
if (!$sock) {
return "$errstr ($errno)<br>\n";
} else {
fputs($sock, "GET $page HTTP/1.1\r\n");
fputs($sock, "Host: $host\r\n");
// Add user header
if ($userHeader != null) {
fputs($sock, $userHeader);
}
fputs($sock, "\r\n\r\n");
$headers = "";
while ($str = trim(fgets($sock, 4096))) {
$headers .= "$str\n";
}
$body = "";
while (!feof($sock)) {
$body .= fgets($sock, 4096);
}
fclose($sock);
}
$response = array();
$response["headers"] = $headers;
$response["body"] = $body;
return $response;
}
}
class CookieControl {
var $cookies = array();
/**
* Adds a cookie to the $cookies array
* @param string $host
* @param string $value
*/
function addCookie($host, $value) {
$this->cookies[] = array(
"Value" => $value,
"Host" => $host);
}
/**
* Parses HTTP headers and extracts the cookies, then sends them to addCookie()
*
* @param string $headers
* @param string $host
*/
function parseHeaders($headers, $host) {
$cookieArray = array();
$headers = explode("\n", $headers);
foreach ($headers as $headerLine) {
if (strstr(mb_strtolower($headerLine), "set-cookie")) {
$cookieArray[] = $headerLine;
}
}
foreach ($cookieArray as $cookieLine) {
$cookie = str_replace("set-cookie: ", "", $cookieLine);
$cookie = explode(";", $cookie);
foreach ($cookie as $value) {
$value = str_replace(" ", "", $value);
if (!strstr($value, "expiry=")) {
$this->addCookie($host, $value);
} else {
break;
}
}
}
}
/**
* Builds a HTTP Header from the stored cookies
*
* @param array $cookieArray
* @return string
*/
function buildHttpString($cookieArray) {
$cookieString = "Cookie: ";
foreach ($cookieArray as $cookie) {
$cookieString .= $cookie["Value"] . "; ";
}
return $cookieString;
}
/**
* Gets the relevant cookies for a certain host and return an array
*
* @param string $host
* @return array
*/
function getCookieArray($host) {
$cookies = array();
foreach ($this->cookies as $cookie) {
if ($cookie["Host"] == $host) {
$cookies[] = $cookie;
}
}
return $cookies;
}
}