]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/XMLRPC/utils.php
split is deprecated, replace with explode
[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
36 /* xmlrpc utilities (xu) 
37  * author: Dan Libby (dan@libby.com)
38  */
39
40 // ensure extension is loaded.
41 xu_load_extension();
42
43 // a function to ensure the xmlrpc extension is loaded.
44 // xmlrpc_epi_dir = directory where libxmlrpc.so.0 is located
45 // xmlrpc_php_dir = directory where xmlrpc-epi-php.so is located
46 function xu_load_extension($xmlrpc_php_dir="") {
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     $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         if ($secure)
72                 $query_fd = fsockopen_ssl($host, $port, $errno, $errstr, 10);
73         else
74                 $query_fd = fsockopen($host, $port, $errno, $errstr, 10);
75
76         if ($query_fd) {
77
78             $auth = "";
79             if ($user) {
80                 $auth = "Authorization: Basic " .
81                     base64_encode($user . ":" . $pass) . "\r\n";
82             }
83
84             $http_request = 
85                 "POST $uri HTTP/1.0\r\n" .
86                 "User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" .
87                 "Host: $host:$port\r\n" .
88                 $auth .
89                 "Content-Type: text/xml\r\n" .
90                 "Content-Length: $content_len\r\n" . 
91                 "\r\n" .
92                 $request;
93
94            dbg1("sending http request:</em><br /> <xmp>\n$http_request\n</xmp>", $debug);
95            fputs($query_fd, $http_request, strlen($http_request));
96            dbg1("receiving response...", $debug);
97
98            $header_parsed = false;
99
100            $line = fgets($query_fd, 4096);
101            while ($line) {
102                if (!$header_parsed) {
103                    if ($line === "\r\n" || $line === "\n") {
104                        $header_parsed = 1;
105                    }
106                    dbg2("got header - $line", $debug);
107                }
108                else {
109                    $response_buf .= $line;
110                }
111                $line = fgets($query_fd, 4096);
112            }
113
114            fclose($query_fd);
115        }
116        else {
117            dbg1("socket open failed", $debug);
118        }
119    }
120    else {
121        dbg1("missing param(s)", $debug);
122    }
123
124    dbg1("got response:</em><br />. <xmp>\n$response_buf\n</xmp>\n", $debug);
125
126    return $response_buf;
127 }
128
129 function xu_fault_code($code, $string) {
130    return array('faultCode' => $code,
131                 'faultString' => $string);
132 }
133
134
135 function find_and_decode_xml($buf, $debug) {
136     if (strlen($buf)) {
137         $xml_begin = substr($buf, strpos($buf, "<?xml"));
138         if (strlen($xml_begin)) {
139
140             $retval = xmlrpc_decode($xml_begin);
141         }
142         else {
143             dbg1("xml start token not found", $debug);
144         }
145     }
146     else {
147         dbg1("no data", $debug);
148     }
149     return $retval;
150 }
151  
152 /**
153  * @param params   a struct containing 3 or more of these key/val pairs:
154  * @param host           remote host (required)
155  * @param uri            remote uri      (required)
156  * @param port           remote port (required)
157  * @param method   name of method to call
158  * @param args      arguments to send (parameters to remote xmlrpc server)
159  * @param debug  debug level (0 none, 1, some, 2 more)
160  * @param timeout        timeout in secs.  (0 = never)
161  * @param user           user name for authentication.  
162  * @param pass           password for authentication
163  * @param secure         secure. wether to use fsockopen_ssl. (requires special php build).
164  * @param output         array. xml output options. can be null.  details below:
165  *
166  *     output_type: return data as either php native data types or xml
167  *                  encoded. if php is used, then the other values are ignored. default = xml
168  *     verbosity:   determine compactness of generated xml. options are
169  *                  no_white_space, newlines_only, and pretty. default = pretty
170  *     escaping:    determine how/whether to escape certain characters. 1 or
171  *                  more values are allowed. If multiple, they need to be specified as
172  *                  a sub-array. options are: cdata, non-ascii, non-print, and
173  *                  markup. default = non-ascii | non-print | markup
174  *     version:     version of xml vocabulary to use. currently, three are
175  *                  supported: xmlrpc, soap 1.1, and simple. The keyword auto is also
176  *                  recognized to mean respond in whichever version the request came
177  *                  in. default = auto (when applicable), xmlrpc
178  *     encoding:    the encoding that the data is in. Since PHP defaults to
179  *                  iso-8859-1 you will usually want to use that. Change it if you know
180  *                  what you are doing. default=iso-8859-1
181  *
182  *   example usage
183  *
184  *                   $output_options = array('output_type' => 'xml',
185  *                                           'verbosity' => 'pretty',
186  *                                           'escaping' => array('markup', 'non-ascii', 'non-print'),
187  *                                           'version' => 'xmlrpc',
188  *                                           'encoding' => 'utf-8'
189  *                                         );
190  *                   or
191  *
192  *                   $output_options = array('output_type' => 'php');
193  */
194 function xu_rpc_http_concise($params) {
195    $host = $uri = $port = $method = $args = $debug = null;
196    $timeout = $user = $pass = $secure = $cookies = null;
197
198    extract($params);
199
200    // default values
201    if(!$port) {
202        $port = 80;
203    }
204    if(!$uri) {
205        $uri = '/';
206    }
207    if(!$output) {
208        $output = array('version' => 'xmlrpc');
209    }
210
211    $response_buf = "";
212    if ($host && $uri && $port) {
213        $request_xml = xmlrpc_encode_request($method, $args, $output);
214        if (isWindows() and !check_php_version(4,3,0)) {
215            include_once("lib/HttpClient.php");
216            $http = new HttpClient($host, $port);
217            if ($timeout)
218                $http->timeout = $timeout;
219            $http->setDebug($debug);
220            // todo: new auth and/or session cookies
221            if ($user)
222                $http->setAuthorization($user, $pass);
223            if ($cookies)
224                $http->setCookies($cookies);
225            if ($http->post($uri, $request_xml))
226                $response_buf = $http->content;
227            else {
228                $response_buf = $http->errormsg;
229                return $response_buf;
230            }
231        } else {
232            $response_buf = xu_query_http_post($request_xml, $host, $uri, $port, $debug,
233                                               $timeout, $user, $pass, $secure);
234        }
235        $retval = find_and_decode_xml($response_buf, $debug);
236    }
237    return $retval;
238 }
239
240 /* call an xmlrpc method on a remote http server. legacy support. */
241 function xu_rpc_http($method, $args, $host, $uri="/", $port=80, $debug=false, 
242                      $timeout=0, $user=false, $pass=false, $secure=false) {
243         return xu_rpc_http_concise(
244                 array(
245                         'method'  => $method,
246                         'args'    => $args,
247                         'host'    => $host,
248                         'uri'     => $uri,
249                         'port'    => $port,
250                         'debug'   => $debug,
251                         'timeout' => $timeout,
252                         'user'    => $user,
253                         'pass'    => $pass,
254                         'secure'  => $secure
255                 ));
256 }
257
258
259
260 function xu_is_fault($arg) {
261    // xmlrpc extension finally supports this.
262    return is_array($arg) ? xmlrpc_is_fault($arg) : false;
263 }
264
265 /* sets some http headers and prints xml */
266 function xu_server_send_http_response($xml) {
267     header("Content-type: text/xml");
268     header("Content-length: " . strlen($xml) );
269     echo $xml;
270 }
271
272 function dbg($msg) {
273     echo "<em>",$msg,"</em><br />"; flush();
274 }
275 function dbg1($msg, $debug_level) {
276    if ($debug_level >= 1) {
277       dbg($msg);
278    }
279 }
280 function dbg2($msg, $debug_level) {
281    if ($debug_level >= 2) {
282       dbg($msg);
283    }
284 }
285 ?>