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