Convert source code to UTF-8
[platal.git] / classes / xmlrpcclient.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 /* taken from : http://fr2.php.net/xml-rpc
23 * Author mboeren@php.net
24 *
25 * Usage:
26 * $client = new xmlrpc_client("http://localhost:7080");
27 * print $client->echo('x')."\n";
28 * print $client->add(1, 3)."\n";
29 */
30
31 class XmlrpcClient
32 {
33 private $url;
34 private $urlparts;
35
36 public function __construct($url)
37 {
38 $this->url = $url;
39 $this->urlparts = parse_url($this->url);
40
41 if (empty($this->urlparts['port'])) {
42 $this->urlparts['port'] = 80;
43 }
44
45 if (empty($this->urlparts['path'])) {
46 $this->urlparts['path'] = '/';
47 }
48 }
49
50 private function http_post($request)
51 {
52 $host = $path = $port = $user = $pass = null;
53 extract($this->urlparts);
54
55 if ($scheme == 'https') {
56 $host = 'ssl://'.$host;
57 }
58
59 $query_fd = fsockopen($host, $port, $errno, $errstr, 10);
60 if (!$query_fd)
61 return null;
62
63 $auth = '';
64 if ($user) {
65 $auth = 'Authorization: Basic ' . base64_encode("$user:$pass") . "\r\n";
66 }
67
68 $content_len = strlen($request);
69 $http_request =
70 "POST $path HTTP/1.0\r\n" .
71 $auth .
72 "Content-Type: text/xml\r\n" .
73 "Content-Length: $content_len\r\n" .
74 "Connection: Close\r\n" .
75 "Host: $host:$port\r\n" .
76 "\r\n" .
77 $request;
78
79 fputs($query_fd, $http_request, strlen($http_request));
80
81 $buf = '';
82 while (!feof($query_fd)) {
83 $buf .= fread($query_fd, 8192);
84 }
85
86 fclose($query_fd);
87 return $buf;
88 }
89
90 private function find_and_decode_xml($buf)
91 {
92 $pos = strpos($buf, '<?xml');
93 if ($pos !== false) {
94 return xmlrpc_decode(substr($buf, $pos));
95 }
96 trigger_error("Cannot parse XML\n".$buf);
97 }
98
99 public function __call($method, $args)
100 {
101 $query = xmlrpc_encode_request($method, $args);
102 $answer = $this->http_post($query, $this->urlparts);
103 $result = $this->find_and_decode_xml($answer);
104
105 if (is_array($result) && isset($result['faultCode'])) {
106 trigger_error("Error in xmlrpc call $function\n".
107 " code : {$result['faultCode']}\n".
108 " message: {$result['faultString']}\n");
109 return null;
110 }
111 return $result;
112 }
113 }
114
115 // vim:set et sw=4 sts=4 sws=4 enc=utf-8:
116 ?>