1 |
cvsjoko |
1.1 |
<? |
2 |
|
|
|
3 |
|
|
class HttpLib { |
4 |
|
|
|
5 |
|
|
function HttpLib() { |
6 |
|
|
|
7 |
|
|
} |
8 |
|
|
|
9 |
|
|
function redirect($args) { |
10 |
|
|
|
11 |
|
|
// define default parameters |
12 |
|
|
|
13 |
|
|
$method = 'GET'; |
14 |
|
|
$host = ''; |
15 |
|
|
$url = ''; |
16 |
|
|
$uri = ''; |
17 |
|
|
|
18 |
|
|
$bool_limitToArgs = 1; |
19 |
|
|
//$data = array(); |
20 |
|
|
$request_data = ''; |
21 |
|
|
$request_data_arr = array(); |
22 |
|
|
|
23 |
|
|
// process args to parameters |
24 |
|
|
|
25 |
|
|
isset($args['method']) && ($method = $args['method']); |
26 |
|
|
isset($args['host']) && ($host = $args['host']); |
27 |
|
|
isset($args['url']) && ($url = $args['url']); |
28 |
|
|
isset($args['limitToArgs']) && ($bool_limitToArgs = $args['limitToArgs']); |
29 |
|
|
|
30 |
|
|
// determine/merge data to be passed on |
31 |
|
|
// there are three different kinds of data here: |
32 |
|
|
// a) data which is already set from "HTTP_GET_VARS" or "HTTP_POST_VARS" (base) (linked to "$tracking-><state>") |
33 |
|
|
// b) data which should modify this (base-)data via "$args['data']" (must be converted to "$request_data"-format) |
34 |
|
|
// c) data which should modify this (base-)data via "$args['request_data']" (is already in proper "$request_data"-format) |
35 |
|
|
|
36 |
|
|
// a) |
37 |
|
|
if (!$bool_limitToArgs) { |
38 |
|
|
global $HTTP_GET_VARS, $HTTP_POST_VARS; |
39 |
|
|
$request_data_arr = array_merge($HTTP_GET_VARS, $HTTP_POST_VARS); |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
// b) |
43 |
|
|
if (isset($args['data'])) { |
44 |
|
|
$request_data_arr = array_merge($request_data_arr, $args['data']); |
45 |
|
|
unset($request_data_arr['x']); |
46 |
|
|
unset($request_data_arr['y']); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
// c) (TODO) |
50 |
|
|
// TODO: merge both together!!! (for this we'd need a "queryString2ComplexHash()"-function) |
51 |
|
|
// isset($args['request_data']) && ($request_data = $args['request_data']); |
52 |
|
|
|
53 |
|
|
|
54 |
|
|
// build "request-data"-string |
55 |
|
|
$request_data = $this->getRequestString($request_data_arr); |
56 |
|
|
|
57 |
|
|
// modify parameters |
58 |
|
|
|
59 |
|
|
if (!$host) { |
60 |
|
|
global $SERVER_NAME; |
61 |
|
|
$host = $SERVER_NAME; |
62 |
|
|
} |
63 |
|
|
if (!$url) { |
64 |
|
|
global $PHP_SELF; |
65 |
|
|
$url = $PHP_SELF; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
/* |
69 |
|
|
// Replace 'http://' with the empty string |
70 |
|
|
// Get the Host address substring |
71 |
|
|
// Get the URI of the desired Resource |
72 |
|
|
$url = preg_replace("@^http://@i", "", $url); |
73 |
|
|
$host = substr($url, 0, strpos($url, "/")); |
74 |
|
|
$uri = strstr($url, "/"); |
75 |
|
|
*/ |
76 |
|
|
|
77 |
|
|
// do redirection |
78 |
|
|
|
79 |
|
|
switch(strtolower($method)) { |
80 |
|
|
|
81 |
|
|
case 'get': |
82 |
|
|
|
83 |
|
|
// build request-string |
84 |
|
|
$request_string = $url . '?' . $request_data; |
85 |
|
|
//print $request_string . "<br>"; |
86 |
|
|
//exit; |
87 |
|
|
Header('Location: ' . $request_string); |
88 |
|
|
exit; |
89 |
|
|
break; |
90 |
|
|
|
91 |
|
|
case 'post': |
92 |
|
|
|
93 |
|
|
// DOES NOT WORK!!!! |
94 |
|
|
// CAN NOT WORK, because sending a request from here (php-script) will not get the same session associated with the server |
95 |
|
|
// maybe we can solve this differently, but for now: forget about POSTing |
96 |
|
|
|
97 |
|
|
// build cookie-data |
98 |
|
|
$varname_PhpSessionId = ini_get('session.name'); |
99 |
|
|
global $$varname_PhpSessionId; |
100 |
|
|
$sessionid = $$varname_PhpSessionId; |
101 |
|
|
$cookie = $varname_PhpSessionId . "=" . $sessionid . ";"; |
102 |
|
|
|
103 |
|
|
// build request-string |
104 |
|
|
$request_header = |
105 |
|
|
"POST " . $url . " HTTP/1.1\r\n" . |
106 |
|
|
"Host: " . $host . "\r\n" . |
107 |
|
|
//"Cookie: " . $cookie . "\r\n" . |
108 |
|
|
"Content-type: application/x-www-form-urlencoded\r\n" . |
109 |
|
|
"Content-length: " . strlen($request_data) . "\r\n" . |
110 |
|
|
"\r\n" . |
111 |
|
|
$request_data . |
112 |
|
|
"&PHPSESSID=$sessionid" |
113 |
|
|
; |
114 |
|
|
|
115 |
|
|
// debug request-header |
116 |
|
|
print $request_header . "<br>"; |
117 |
|
|
//exit; |
118 |
|
|
|
119 |
|
|
// send request (post) and get response |
120 |
|
|
if ($response = $this->request($host, 80, $request_header)) { |
121 |
|
|
// output response |
122 |
|
|
echo "response-begin<br>"; |
123 |
|
|
echo $response; |
124 |
|
|
echo "<br>"; |
125 |
|
|
echo "response-end<br>"; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
|
129 |
|
|
break; |
130 |
|
|
|
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
function request($host, $port, $request_header) { |
136 |
|
|
|
137 |
|
|
// open connection to server |
138 |
|
|
$fp = fsockopen($host, $port, $err_num, $err_msg, 30); |
139 |
|
|
if ($fp) { |
140 |
|
|
|
141 |
|
|
// send post-request |
142 |
|
|
fputs($fp, $request_header); |
143 |
|
|
|
144 |
|
|
// get response |
145 |
|
|
$response = ''; |
146 |
|
|
while (!feof($fp)) |
147 |
|
|
$response .= fgets($fp, 1024); |
148 |
|
|
fclose($fp); |
149 |
|
|
|
150 |
|
|
return $response; |
151 |
|
|
|
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
|
157 |
|
|
function getRequestString($requestData) { |
158 |
|
|
$arr_subkeys = array(); |
159 |
|
|
$simpleHash = nestedHash2simpleHash($requestData, 0, &$arr_subkeys); |
160 |
|
|
$retval = ''; |
161 |
|
|
while( list($key, $value) = each($simpleHash) ) { |
162 |
|
|
if (!is_array($value)) { |
163 |
|
|
$retval .= urlencode($key) . '=' . urlencode($value) . '&'; |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
$retval = substr($retval, 0, strlen($retval) - 1); |
167 |
|
|
return $retval; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
?> |