0337d704 |
1 | <?php |
2 | |
3 | /****************************************************************************** |
4 | * * |
5 | * Original file can be found on http://xmlrpc-epi.sourceforge.net/ * |
6 | * in the module xmlrpc-epi-php v0.51 file samples/utils/utils.php * |
7 | * * |
8 | * The Polytechnique.org TEAM * |
9 | * * |
10 | ******************************************************************************/ |
11 | |
12 | /* |
13 | This file is part of, or distributed with, libXMLRPC - a C library for |
14 | xml-encoded function calls. |
15 | |
16 | Author: Dan Libby (dan@libby.com) |
17 | Epinions.com may be contacted at feedback@epinions-inc.com |
18 | */ |
19 | |
20 | /* |
21 | Copyright 2001 Epinions, Inc. |
22 | |
23 | Subject to the following 3 conditions, Epinions, Inc. permits you, free |
24 | of charge, to (a) use, copy, distribute, modify, perform and display this |
25 | software and associated documentation files (the "Software"), and (b) |
26 | permit others to whom the Software is furnished to do so as well. |
27 | |
28 | 1) The above copyright notice and this permission notice shall be included |
29 | without modification in all copies or substantial portions of the |
30 | Software. |
31 | |
32 | 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF |
33 | ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY |
34 | IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR |
35 | PURPOSE OR NONINFRINGEMENT. |
36 | |
37 | 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, |
38 | SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT |
39 | OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING |
40 | NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH |
41 | DAMAGES. |
42 | |
43 | */ |
44 | |
45 | /* xmlrpc utilities (xu) |
46 | * author: Dan Libby (dan@libby.com) |
47 | */ |
48 | |
49 | /* generic function to call an http server with post method */ |
50 | function xu_query_http_post($request, $host, $uri, $port, $debug, |
51 | $timeout, $user, $pass, $secure=false) { |
52 | $response_buf = ""; |
53 | if ($host && $uri && $port) { |
54 | $content_len = strlen($request); |
55 | |
56 | $fsockopen = $secure ? "fsockopen_ssl" : "fsockopen"; |
57 | |
58 | dbg1("opening socket to host: $host, port: $port, uri: $uri", $debug); |
59 | $query_fd = $fsockopen($host, $port, $errno, $errstr, 10); |
60 | |
61 | if ($query_fd) { |
62 | |
63 | $auth = ""; |
64 | if ($user) { |
65 | $auth = "Authorization: Basic " . base64_encode($user . ":" . $pass) . "\r\n"; |
66 | } |
67 | |
68 | $http_request = |
69 | "POST $uri HTTP/1.0\r\n" . |
70 | "Host: $host:$port\r\n" . |
71 | $auth . |
72 | "User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" . |
73 | "Content-Type: text/xml\r\n" . |
74 | "Content-Length: $content_len\r\n" . |
75 | "Connection: Close\r\n" . |
76 | "\r\n" . |
77 | $request; |
78 | |
79 | dbg1("sending http request:</h3> <xmp>\n$http_request\n</xmp>", $debug); |
80 | |
81 | fputs($query_fd, $http_request, strlen($http_request)); |
82 | |
83 | dbg1("receiving response...", $debug); |
84 | |
85 | $header_parsed = false; |
86 | |
87 | while (!feof($query_fd)) { |
88 | $line = fgets($query_fd, 4096); |
89 | if (!$header_parsed) { |
90 | if ($line === "\r\n" || $line === "\n") { |
91 | $header_parsed = 1; |
92 | } |
93 | dbg2("got header - $line", $debug); |
94 | } |
95 | else { |
96 | $response_buf .= $line; |
97 | } |
98 | } |
99 | |
100 | fclose($query_fd); |
101 | } |
102 | else { |
103 | dbg1("socket open failed", $debug); |
104 | } |
105 | } |
106 | else { |
107 | dbg1("missing param(s)", $debug); |
108 | } |
109 | |
110 | dbg1("got response:</h3>. <xmp>\n$response_buf\n</xmp>\n", $debug); |
111 | |
112 | return $response_buf; |
113 | } |
114 | |
115 | function xu_fault_code($code, $string) { |
116 | return array('faultCode' => $code, |
117 | 'faultString' => $string); |
118 | } |
119 | |
120 | |
121 | function find_and_decode_xml($buf, $debug) { |
122 | if (strlen($buf)) { |
123 | $xml_begin = substr($buf, strpos($buf, "<?xml")); |
124 | if (strlen($xml_begin)) { |
125 | |
126 | $retval = xmlrpc_decode($xml_begin); |
127 | } |
128 | else { |
129 | dbg1("xml start token not found", $debug); |
130 | } |
131 | } |
132 | else { |
133 | dbg1("no data", $debug); |
134 | } |
135 | return $retval; |
136 | } |
137 | |
138 | |
139 | /** |
140 | * @param params a struct containing 3 or more of these key/val pairs: |
141 | * @param host remote host (required) |
142 | * @param uri remote uri (required) |
143 | * @param port remote port (required) |
144 | * @param method name of method to call |
145 | * @param args arguments to send (parameters to remote xmlrpc server) |
146 | * @param debug debug level (0 none, 1, some, 2 more) |
147 | * @param timeout timeout in secs. (0 = never) |
148 | * @param user user name for authentication. |
149 | * @param pass password for authentication |
150 | * @param secure secure. wether to use fsockopen_ssl. (requires special php build). |
151 | * @param output array. xml output options. can be null. details below: |
152 | * |
153 | * output_type: return data as either php native data types or xml |
154 | * encoded. ifphp is used, then the other values are ignored. default = xml |
155 | * verbosity: determine compactness of generated xml. options are |
156 | * no_white_space, newlines_only, and pretty. default = pretty |
157 | * escaping: determine how/whether to escape certain characters. 1 or |
158 | * more values are allowed. If multiple, they need to be specified as |
159 | * a sub-array. options are: cdata, non-ascii, non-print, and |
160 | * markup. default = non-ascii | non-print | markup |
161 | * version: version of xml vocabulary to use. currently, three are |
162 | * supported: xmlrpc, soap 1.1, and simple. The keyword auto is also |
163 | * recognized to mean respond in whichever version the request came |
164 | * in. default = auto (when applicable), xmlrpc |
165 | * encoding: the encoding that the data is in. Since PHP defaults to |
166 | * iso-8859-1 you will usually want to use that. Change it if you know |
167 | * what you are doing. default=iso-8859-1 |
168 | * |
169 | * example usage |
170 | * |
171 | * $output_options = array('output_type' => 'xml', |
172 | * 'verbosity' => 'pretty', |
173 | * 'escaping' => array('markup', 'non-ascii', 'non-print'), |
174 | * 'version' => 'xmlrpc', |
175 | * 'encoding' => 'utf-8' |
176 | * ); |
177 | * or |
178 | * |
179 | * $output_options = array('output_type' => 'php'); |
180 | */ |
181 | function xu_rpc_http_concise($params) { |
182 | $output = $host = $uri = $port = $method = $args = $debug = null; |
183 | $timeout = $user = $pass = $secure = $debug = null; |
184 | |
185 | extract($params); |
186 | |
187 | // default values |
188 | if(!$port) { |
189 | $port = 80; |
190 | } |
191 | if(!$uri) { |
192 | $uri = '/'; |
193 | } |
194 | if(!$output) { |
195 | $output = array('version' => 'xmlrpc'); |
196 | } |
197 | |
198 | $response_buf = ""; |
199 | if ($host && $uri && $port) { |
200 | $request_xml = xmlrpc_encode_request($method, $args, $output); |
201 | $response_buf = xu_query_http_post($request_xml, $host, $uri, $port, $debug, |
202 | $timeout, $user, $pass, $secure); |
203 | |
204 | $retval = find_and_decode_xml($response_buf, $debug); |
205 | } |
206 | return $retval; |
207 | } |
208 | |
209 | /* call an xmlrpc method on a remote http server. legacy support. */ |
210 | function xu_rpc_http($method, $args, $host, $uri="/", $port=80, $debug=false, |
211 | $timeout=0, $user=false, $pass=false, $secure=false) { |
212 | return xu_rpc_http_concise( |
213 | array( |
214 | 'method' => $method, |
215 | 'args' => $args, |
216 | 'host' => $host, |
217 | 'uri' => $uri, |
218 | 'port' => $port, |
219 | 'debug' => $debug, |
220 | 'timeout' => $timeout, |
221 | 'user' => $user, |
222 | 'pass' => $pass, |
223 | 'secure' => $secure |
224 | )); |
225 | } |
226 | |
227 | |
228 | |
229 | function xu_is_fault($arg) { |
230 | // xmlrpc extension finally supports this. |
231 | return is_array($arg) ? xmlrpc_is_fault($arg) : false; |
232 | } |
233 | |
234 | /* sets some http headers and prints xml */ |
235 | function xu_server_send_http_response($xml) { |
236 | header("Content-type: text/xml"); |
237 | header("Content-length: " . strlen($xml) ); |
238 | echo $xml; |
239 | } |
240 | |
241 | |
242 | function dbg($msg) { |
243 | echo "<h3>$msg</h3>"; flush(); |
244 | } |
245 | function dbg1($msg, $debug_level) { |
246 | if ($debug_level >= 1) { |
247 | dbg($msg); |
248 | } |
249 | } |
250 | function dbg2($msg, $debug_level) { |
251 | if ($debug_level >= 2) { |
252 | dbg($msg); |
253 | } |
254 | } |
255 | |
256 | |
257 | ?> |