Repair XmlrpcClient
[platal.git] / classes / xmlrpcclient.php
CommitLineData
0337d704 1<?php
0337d704 2/******************************************************************************
3 * *
4 * Original file can be found on http://xmlrpc-epi.sourceforge.net/ *
5 * in the module xmlrpc-epi-php v0.51 file samples/utils/utils.php *
6 * *
7 * The Polytechnique.org TEAM *
8 * *
27cb2301 9 **************************************************************************{{{*/
0337d704 10
11/*
27cb2301 12 This file is part of, or distributed with, libXMLRPC - a C library for
0337d704 13 xml-encoded function calls.
14
15 Author: Dan Libby (dan@libby.com)
16 Epinions.com may be contacted at feedback@epinions-inc.com
17*/
18
27cb2301 19/*
20 Copyright 2001 Epinions, Inc.
0337d704 21
27cb2301 22 Subject to the following 3 conditions, Epinions, Inc. permits you, free
23 of charge, to (a) use, copy, distribute, modify, perform and display this
24 software and associated documentation files (the "Software"), and (b)
25 permit others to whom the Software is furnished to do so as well.
0337d704 26
27cb2301 27 1) The above copyright notice and this permission notice shall be included
28 without modification in all copies or substantial portions of the
29 Software.
0337d704 30
27cb2301 31 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
32 ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
33 IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
34 PURPOSE OR NONINFRINGEMENT.
0337d704 35
27cb2301 36 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
37 SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
38 OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
39 NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH
40 DAMAGES.
0337d704 41
42*/
43
27cb2301 44/* xmlrpc utilities (xu)
0337d704 45 * author: Dan Libby (dan@libby.com)
46 */
47
48/* generic function to call an http server with post method */
c36b9cb0 49function xu_query_http_post($request, $host, $uri, $port, $timeout, $user,
50 $pass, $secure=false)
51{
52 $response_buf = '';
0337d704 53 if ($host && $uri && $port) {
54 $content_len = strlen($request);
c36b9cb0 55 $fsockopen = $secure ? 'fsockopen_ssl' : 'fsockopen';
56 $query_fd = $fsockopen($host, $port, $errno, $errstr, 10);
0337d704 57
58 if ($query_fd) {
59
c36b9cb0 60 $auth = '';
0337d704 61 if ($user) {
c36b9cb0 62 $auth = 'Authorization: Basic ' . base64_encode($user . ':' . $pass) . "\r\n";
0337d704 63 }
64
65 $http_request =
66 "POST $uri HTTP/1.0\r\n" .
67 "Host: $host:$port\r\n" .
68 $auth .
69 "User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" .
70 "Content-Type: text/xml\r\n" .
27cb2301 71 "Content-Length: $content_len\r\n" .
0337d704 72 "Connection: Close\r\n" .
73 "\r\n" .
74 $request;
75
0337d704 76 fputs($query_fd, $http_request, strlen($http_request));
77
0337d704 78 $header_parsed = false;
0337d704 79 while (!feof($query_fd)) {
80 $line = fgets($query_fd, 4096);
81 if (!$header_parsed) {
82 if ($line === "\r\n" || $line === "\n") {
83 $header_parsed = 1;
84 }
c36b9cb0 85 } else {
0337d704 86 $response_buf .= $line;
87 }
88 }
89
90 fclose($query_fd);
91 }
0337d704 92 }
93
0337d704 94 return $response_buf;
95}
96
c36b9cb0 97function find_and_decode_xml($buf)
98{
99 if (strlen($buf)) {
100 $xml_begin = substr($buf, strpos($buf, '<?xml'));
101 if (strlen($xml_begin)) {
102 $retval = xmlrpc_decode($xml_begin);
103 }
104 }
105 return $retval;
0337d704 106}
107
27cb2301 108
0337d704 109/**
c36b9cb0 110 * @param params a struct containing 3 or more of these key/val pairs:
111 * @param host remote host (required)
112 * @param uri remote uri (required)
113 * @param port remote port (required)
114 * @param method name of method to call
115 * @param args arguments to send (parameters to remote xmlrpc server)
116 * @param timeout timeout in secs. (0 = never)
27cb2301 117 * @param user user name for authentication.
0337d704 118 * @param pass password for authentication
119 * @param secure secure. wether to use fsockopen_ssl. (requires special php build).
0337d704 120 */
121function xu_rpc_http_concise($params) {
c36b9cb0 122 $host = $uri = $port = $method = $args = null;
123 $timeout = $user = $pass = $secure = null;
0337d704 124
c36b9cb0 125 extract($params);
0337d704 126
c36b9cb0 127 // default values
128 if (!$port) {
129 $port = 80;
130 }
131 if (!$uri) {
132 $uri = '/';
133 }
134 if ($host && $uri && $port) {
135 $request_xml = xmlrpc_encode_request($method, $args);
136 $response_buf = xu_query_http_post($request_xml, $host, $uri, $port,
137 $timeout, $user, $pass, $secure);
138 $retval = find_and_decode_xml($response_buf);
0337d704 139 }
140 return $retval;
141}
142
27cb2301 143/*}}}***********************************************************************
144 * Copyright (C) 2003-2006 Polytechnique.org *
145 * http://opensource.polytechnique.org/ *
146 * *
147 * This program is free software; you can redistribute it and/or modify *
148 * it under the terms of the GNU General Public License as published by *
149 * the Free Software Foundation; either version 2 of the License, or *
150 * (at your option) any later version. *
151 * *
152 * This program is distributed in the hope that it will be useful, *
153 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
154 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
155 * GNU General Public License for more details. *
156 * *
157 * You should have received a copy of the GNU General Public License *
158 * along with this program; if not, write to the Free Software *
159 * Foundation, Inc., *
160 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
161 ***************************************************************************/
162
163/* taken from : http://fr2.php.net/xml-rpc
164 * Author mboeren@php.net
165 *
166 * Usage:
167 * $client = new xmlrpc_client("http://localhost:7080");
168 * print $client->echo('x')."\n";
169 * print $client->add(1, 3)."\n";
170 */
171
172class XmlrpcClient
173{
174 var $url;
175 var $urlparts;
176
177 function XmlrpcClient($url)
178 {
179 $this->url = $url;
180 $this->urlparts = parse_url($this->url);
181 foreach (array('scheme', 'host', 'user', 'pass', 'path', 'query', 'fragment') as $part) {
182 if (!isset($this->urlparts[$part])) {
183 $this->urlparts[$part] = null;
184 }
185 }
186 }
187
df69f0b9 188 function __call($function, $arguments)
27cb2301 189 {
190 $requestprms['host'] = $this->urlparts['host'];
191 $requestprms['port'] = $this->urlparts['port'];
192 $requestprms['uri'] = $this->urlparts['path'];
193 $requestprms['user'] = $this->urlparts['user'];
194 $requestprms['pass'] = $this->urlparts['pass'];
195 $requestprms['method'] = $function;
196 $requestprms['args'] = $arguments;
197 $requestprms['timeout'] = 0;
198 $requestprms['secure'] = 0;
199
200 $result = xu_rpc_http_concise($requestprms);
201 if (is_array($result) && isset($result['faultCode'])) {
202 print('Error in xmlrpc call \''.$function.'\''."\n");
203 print(' code : '.$result['faultCode']."\n");
204 print(' message: '.$result['faultString']."\n");
df69f0b9 205 return null;
27cb2301 206 }
df69f0b9 207 return $result;
27cb2301 208 }
209
210}
211
27cb2301 212// vim:set et sw=4 sts=4 sws=4:
0337d704 213?>