1 |
<?php |
---|
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 |
xu_load_extension(); |
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 |
function xu_load_extension($xmlrpc_php_dir="") { |
---|
48 |
$bSuccess = true; |
---|
49 |
if(!extension_loaded('xmlrpc')) { |
---|
50 |
$bSuccess = true; |
---|
51 |
putenv("LD_LIBRARY_PATH=/usr/lib/php4/apache/xmlrpc/"); |
---|
52 |
if ($xmlrpc_php_dir) { |
---|
53 |
$xmlrpc_php_dir .= '/'; |
---|
54 |
} |
---|
55 |
if (!extension_loaded("xmlrpc")) { |
---|
56 |
if ( PHP_OS == "WINNT" || PHP_OS == "WIN32" ) |
---|
57 |
$bSuccess = dl($xmlrpc_php_dir . "php_xmlrpc.dll"); |
---|
58 |
else |
---|
59 |
$bSuccess = dl($xmlrpc_php_dir . "xmlrpc-epi-php.so"); |
---|
60 |
} |
---|
61 |
} |
---|
62 |
return $bSuccess; |
---|
63 |
} |
---|
64 |
|
---|
65 |
|
---|
66 |
function xu_query_http_post($request, $host, $uri, $port, $debug, |
---|
67 |
$timeout, $user, $pass, $secure=false) { |
---|
68 |
$response_buf = ""; |
---|
69 |
if ($host && $uri && $port) { |
---|
70 |
$content_len = strlen($request); |
---|
71 |
|
---|
72 |
$fsockopen = $secure ? "fsockopen_ssl" : "fsockopen"; |
---|
73 |
|
---|
74 |
dbg1("opening socket to host: $host, port: $port, uri: $uri", $debug); |
---|
75 |
$query_fd = $fsockopen($host, $port, $errno, $errstr, 10); |
---|
76 |
|
---|
77 |
if ($query_fd) { |
---|
78 |
|
---|
79 |
$auth = ""; |
---|
80 |
if ($user) { |
---|
81 |
$auth = "Authorization: Basic " . |
---|
82 |
base64_encode($user . ":" . $pass) . "\r\n"; |
---|
83 |
} |
---|
84 |
|
---|
85 |
$http_request = |
---|
86 |
"POST $uri HTTP/1.0\r\n" . |
---|
87 |
"User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" . |
---|
88 |
"Host: $host:$port\r\n" . |
---|
89 |
$auth . |
---|
90 |
"Content-Type: text/xml\r\n" . |
---|
91 |
"Content-Length: $content_len\r\n" . |
---|
92 |
"\r\n" . |
---|
93 |
$request; |
---|
94 |
|
---|
95 |
dbg1("sending http request:</h3> <xmp>\n$http_request\n</xmp>", $debug); |
---|
96 |
|
---|
97 |
fputs($query_fd, $http_request, strlen($http_request)); |
---|
98 |
|
---|
99 |
dbg1("receiving response...", $debug); |
---|
100 |
|
---|
101 |
$header_parsed = false; |
---|
102 |
|
---|
103 |
$line = fgets($query_fd, 4096); |
---|
104 |
while ($line) { |
---|
105 |
if (!$header_parsed) { |
---|
106 |
if ($line === "\r\n" || $line === "\n") { |
---|
107 |
$header_parsed = 1; |
---|
108 |
} |
---|
109 |
dbg2("got header - $line", $debug); |
---|
110 |
} |
---|
111 |
else { |
---|
112 |
$response_buf .= $line; |
---|
113 |
} |
---|
114 |
$line = fgets($query_fd, 4096); |
---|
115 |
} |
---|
116 |
|
---|
117 |
fclose($query_fd); |
---|
118 |
} |
---|
119 |
else { |
---|
120 |
dbg1("socket open failed", $debug); |
---|
121 |
} |
---|
122 |
} |
---|
123 |
else { |
---|
124 |
dbg1("missing param(s)", $debug); |
---|
125 |
} |
---|
126 |
|
---|
127 |
dbg1("got response:</h3>. <xmp>\n$response_buf\n</xmp>\n", $debug); |
---|
128 |
|
---|
129 |
return $response_buf; |
---|
130 |
} |
---|
131 |
|
---|
132 |
function xu_fault_code($code, $string) { |
---|
133 |
return array('faultCode' => $code, |
---|
134 |
'faultString' => $string); |
---|
135 |
} |
---|
136 |
|
---|
137 |
|
---|
138 |
function find_and_decode_xml($buf, $debug) { |
---|
139 |
$retval = ""; |
---|
140 |
if (strlen($buf)) { |
---|
141 |
$xml_begin = substr($buf, strpos($buf, "<?xml")); |
---|
142 |
if (strlen($xml_begin)) { |
---|
143 |
|
---|
144 |
$retval = xmlrpc_decode($xml_begin); |
---|
145 |
} |
---|
146 |
else { |
---|
147 |
dbg1("xml start token not found", $debug); |
---|
148 |
} |
---|
149 |
} |
---|
150 |
else { |
---|
151 |
dbg1("no data", $debug); |
---|
152 |
} |
---|
153 |
return $retval; |
---|
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 |
function xu_rpc_http_concise($params) { |
---|
200 |
$host = $uri = $port = $method = $args = $debug = null; |
---|
201 |
$timeout = $user = $pass = $secure = $debug = null; |
---|
202 |
|
---|
203 |
extract($params); |
---|
204 |
|
---|
205 |
|
---|
206 |
if(!$port) { |
---|
207 |
$port = 80; |
---|
208 |
} |
---|
209 |
if(!$uri) { |
---|
210 |
$uri = '/'; |
---|
211 |
} |
---|
212 |
if(!isset($output)) { |
---|
213 |
$output = array('version' => 'xmlrpc'); |
---|
214 |
} |
---|
215 |
|
---|
216 |
$response_buf = ""; |
---|
217 |
if ($host && $uri && $port) { |
---|
218 |
$request_xml = xmlrpc_encode_request($method, $args, $output); |
---|
219 |
|
---|
220 |
$response_buf = xu_query_http_post($request_xml, $host, $uri, $port, $debug, |
---|
221 |
$timeout, $user, $pass, $secure); |
---|
222 |
|
---|
223 |
$retval = find_and_decode_xml($response_buf, $debug); |
---|
224 |
} |
---|
225 |
return $retval; |
---|
226 |
} |
---|
227 |
|
---|
228 |
|
---|
229 |
function xu_rpc_http($method, $args, $host, $uri="/", $port=80, $debug=false, |
---|
230 |
$timeout=0, $user=false, $pass=false, $secure=false) { |
---|
231 |
return xu_rpc_http_concise( |
---|
232 |
array( |
---|
233 |
method => $method, |
---|
234 |
args => $args, |
---|
235 |
host => $host, |
---|
236 |
uri => $uri, |
---|
237 |
port => $port, |
---|
238 |
debug => $debug, |
---|
239 |
timeout => $timeout, |
---|
240 |
user => $user, |
---|
241 |
pass => $pass, |
---|
242 |
secure => $secure |
---|
243 |
)); |
---|
244 |
} |
---|
245 |
|
---|
246 |
|
---|
247 |
function xu_is_fault($arg) { |
---|
248 |
|
---|
249 |
return is_array($arg) ? xmlrpc_is_fault($arg) : false; |
---|
250 |
} |
---|
251 |
|
---|
252 |
|
---|
253 |
function xu_server_send_http_response($xml) { |
---|
254 |
header("Content-type: text/xml"); |
---|
255 |
header("Content-length: " . strlen($xml) ); |
---|
256 |
echo $xml; |
---|
257 |
} |
---|
258 |
|
---|
259 |
|
---|
260 |
function dbg($msg) { |
---|
261 |
echo "<h3>$msg</h3>"; flush(); |
---|
262 |
} |
---|
263 |
function dbg1($msg, $debug_level) { |
---|
264 |
if ($debug_level >= 1) { |
---|
265 |
dbg($msg); |
---|
266 |
} |
---|
267 |
} |
---|
268 |
function dbg2($msg, $debug_level) { |
---|
269 |
if ($debug_level >= 2) { |
---|
270 |
dbg($msg); |
---|
271 |
} |
---|
272 |
} |
---|
273 |
|
---|
274 |
|
---|
275 |
?> |
---|
276 |
|
---|