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