Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
6682f91d | 2 | /*************************************************************************** |
e92ecb8c | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
27cb2301 | 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 | { | |
6682f91d | 33 | private $url; |
34 | private $urlparts; | |
d3f26be9 | 35 | public $bt = null; |
27cb2301 | 36 | |
6682f91d | 37 | public function __construct($url) |
27cb2301 | 38 | { |
39 | $this->url = $url; | |
40 | $this->urlparts = parse_url($this->url); | |
6682f91d | 41 | |
42 | if (empty($this->urlparts['port'])) { | |
43 | $this->urlparts['port'] = 80; | |
27cb2301 | 44 | } |
6682f91d | 45 | |
46 | if (empty($this->urlparts['path'])) { | |
47 | $this->urlparts['path'] = '/'; | |
48 | } | |
49 | } | |
50 | ||
51 | private function http_post($request) | |
52 | { | |
53 | $host = $path = $port = $user = $pass = null; | |
54 | extract($this->urlparts); | |
55 | ||
56 | if ($scheme == 'https') { | |
57 | $host = 'ssl://'.$host; | |
58 | } | |
59 | ||
60 | $query_fd = fsockopen($host, $port, $errno, $errstr, 10); | |
61 | if (!$query_fd) | |
62 | return null; | |
63 | ||
64 | $auth = ''; | |
65 | if ($user) { | |
66 | $auth = 'Authorization: Basic ' . base64_encode("$user:$pass") . "\r\n"; | |
67 | } | |
68 | ||
69 | $content_len = strlen($request); | |
70 | $http_request = | |
71 | "POST $path HTTP/1.0\r\n" . | |
72 | $auth . | |
73 | "Content-Type: text/xml\r\n" . | |
74 | "Content-Length: $content_len\r\n" . | |
75 | "Connection: Close\r\n" . | |
76 | "Host: $host:$port\r\n" . | |
77 | "\r\n" . | |
78 | $request; | |
79 | ||
80 | fputs($query_fd, $http_request, strlen($http_request)); | |
81 | ||
82 | $buf = ''; | |
83 | while (!feof($query_fd)) { | |
84 | $buf .= fread($query_fd, 8192); | |
85 | } | |
86 | ||
87 | fclose($query_fd); | |
88 | return $buf; | |
89 | } | |
90 | ||
91 | private function find_and_decode_xml($buf) | |
92 | { | |
93 | $pos = strpos($buf, '<?xml'); | |
94 | if ($pos !== false) { | |
95 | return xmlrpc_decode(substr($buf, $pos)); | |
96 | } | |
97 | trigger_error("Cannot parse XML\n".$buf); | |
27cb2301 | 98 | } |
99 | ||
6682f91d | 100 | public function __call($method, $args) |
27cb2301 | 101 | { |
6682f91d | 102 | $query = xmlrpc_encode_request($method, $args); |
d3f26be9 | 103 | if ($this->bt) { |
104 | $this->bt->start($method . "\n" . var_export($args, true)); | |
105 | } | |
6682f91d | 106 | $answer = $this->http_post($query, $this->urlparts); |
d3f26be9 | 107 | if ($this->bt) { |
108 | $this->bt->stop(); | |
109 | } | |
e10ca15e FB |
110 | if (is_null($answer)) { |
111 | Platal::page()->trigError("L'accès aux listes de diffusion n'est pas disponible actuellement."); | |
112 | $this->bt->update(0, "Connection failed"); | |
113 | return null; | |
114 | } | |
115 | Platal::assert(starts_with($answer, 'HTTP/1.0 200 OK'), "HTTP Error:\n" . $answer, | |
116 | "Une erreur est survenue lors de l'accès aux listes de diffusion."); | |
6682f91d | 117 | $result = $this->find_and_decode_xml($answer); |
d3f26be9 | 118 | if ($this->bt) { |
923dd561 | 119 | if (is_array($result) && isset($result['faultCode'])) { |
d3f26be9 | 120 | $this->bt->update(0, $result['faultString']); |
121 | } else { | |
122 | $this->bt->update(count($result)); | |
123 | } | |
e10ca15e | 124 | } else { |
798c1f4b | 125 | Platal::assert(!is_array($result) || !isset($result['faultCode']), |
502bb918 | 126 | "RPC Error:\n" . $answer, |
e10ca15e | 127 | "Une erreur est survenue lors de l'accès aux listes de diffusion."); |
d3f26be9 | 128 | } |
6682f91d | 129 | |
27cb2301 | 130 | if (is_array($result) && isset($result['faultCode'])) { |
6682f91d | 131 | trigger_error("Error in xmlrpc call $function\n". |
132 | " code : {$result['faultCode']}\n". | |
133 | " message: {$result['faultString']}\n"); | |
df69f0b9 | 134 | return null; |
27cb2301 | 135 | } |
df69f0b9 | 136 | return $result; |
27cb2301 | 137 | } |
27cb2301 | 138 | } |
139 | ||
a7de4ef7 | 140 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
0337d704 | 141 | ?> |