Reformat source code: the library
[plcapi.git] / src / Client.php
1 <?php
2
3 namespace PhpXmlRpc;
4
5 class Client
6 {
7     /// @todo: do these need to be public?
8     public $path;
9     public $server;
10     public $port = 0;
11     public $method = 'http';
12     public $errno;
13     public $errstr;
14     public $debug = 0;
15     public $username = '';
16     public $password = '';
17     public $authtype = 1;
18     public $cert = '';
19     public $certpass = '';
20     public $cacert = '';
21     public $cacertdir = '';
22     public $key = '';
23     public $keypass = '';
24     public $verifypeer = true;
25     public $verifyhost = 2;
26     public $no_multicall = false;
27     public $proxy = '';
28     public $proxyport = 0;
29     public $proxy_user = '';
30     public $proxy_pass = '';
31     public $proxy_authtype = 1;
32     public $cookies = array();
33     public $extracurlopts = array();
34
35     /**
36      * List of http compression methods accepted by the client for responses.
37      * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib.
38      *
39      * NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since
40      * in those cases it will be up to CURL to decide the compression methods
41      * it supports. You might check for the presence of 'zlib' in the output of
42      * curl_version() to determine wheter compression is supported or not
43      */
44     public $accepted_compression = array();
45     /**
46      * Name of compression scheme to be used for sending requests.
47      * Either null, gzip or deflate.
48      */
49     public $request_compression = '';
50     /**
51      * CURL handle: used for keep-alive connections (PHP 4.3.8 up, see:
52      * http://curl.haxx.se/docs/faq.html#7.3).
53      */
54     public $xmlrpc_curl_handle = null;
55     /// Whether to use persistent connections for http 1.1 and https
56     public $keepalive = false;
57     /// Charset encodings that can be decoded without problems by the client
58     public $accepted_charset_encodings = array();
59     /// Charset encoding to be used in serializing request. NULL = use ASCII
60     public $request_charset_encoding = '';
61     /**
62      * Decides the content of Response objects returned by calls to send()
63      * valid strings are 'xmlrpcvals', 'phpvals' or 'xml'.
64      */
65     public $return_type = 'xmlrpcvals';
66     /**
67      * Sent to servers in http headers.
68      */
69     public $user_agent;
70
71     /**
72      * @param string $path either the complete server URL or the PATH part of the xmlrc server URL, e.g. /xmlrpc/server.php
73      * @param string $server the server name / ip address
74      * @param integer $port the port the server is listening on, defaults to 80 or 443 depending on protocol used
75      * @param string $method the http protocol variant: defaults to 'http', 'https' and 'http11' can be used if CURL is installed
76      */
77     public function __construct($path, $server = '', $port = '', $method = '')
78     {
79         // allow user to specify all params in $path
80         if ($server == '' and $port == '' and $method == '') {
81             $parts = parse_url($path);
82             $server = $parts['host'];
83             $path = isset($parts['path']) ? $parts['path'] : '';
84             if (isset($parts['query'])) {
85                 $path .= '?' . $parts['query'];
86             }
87             if (isset($parts['fragment'])) {
88                 $path .= '#' . $parts['fragment'];
89             }
90             if (isset($parts['port'])) {
91                 $port = $parts['port'];
92             }
93             if (isset($parts['scheme'])) {
94                 $method = $parts['scheme'];
95             }
96             if (isset($parts['user'])) {
97                 $this->username = $parts['user'];
98             }
99             if (isset($parts['pass'])) {
100                 $this->password = $parts['pass'];
101             }
102         }
103         if ($path == '' || $path[0] != '/') {
104             $this->path = '/' . $path;
105         } else {
106             $this->path = $path;
107         }
108         $this->server = $server;
109         if ($port != '') {
110             $this->port = $port;
111         }
112         if ($method != '') {
113             $this->method = $method;
114         }
115
116         // if ZLIB is enabled, let the client by default accept compressed responses
117         if (function_exists('gzinflate') || (
118                 function_exists('curl_init') && (($info = curl_version()) &&
119                     ((is_string($info) && strpos($info, 'zlib') !== null) || isset($info['libz_version'])))
120             )
121         ) {
122             $this->accepted_compression = array('gzip', 'deflate');
123         }
124
125         // keepalives: enabled by default
126         $this->keepalive = true;
127
128         // by default the xml parser can support these 3 charset encodings
129         $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII');
130
131         // initialize user_agent string
132         $this->user_agent = PhpXmlRpc::$xmlrpcName . ' ' . PhpXmlRpc::$xmlrpcVersion;
133     }
134
135     /**
136      * Enables/disables the echoing to screen of the xmlrpc responses received.
137      *
138      * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)
139      */
140     public function setDebug($in)
141     {
142         $this->debug = $in;
143     }
144
145     /**
146      * Add some http BASIC AUTH credentials, used by the client to authenticate.
147      *
148      * @param string $u username
149      * @param string $p password
150      * @param integer $t auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC (basic auth)
151      */
152     public function setCredentials($u, $p, $t = 1)
153     {
154         $this->username = $u;
155         $this->password = $p;
156         $this->authtype = $t;
157     }
158
159     /**
160      * Add a client-side https certificate.
161      *
162      * @param string $cert
163      * @param string $certpass
164      */
165     public function setCertificate($cert, $certpass)
166     {
167         $this->cert = $cert;
168         $this->certpass = $certpass;
169     }
170
171     /**
172      * Add a CA certificate to verify server with (see man page about
173      * CURLOPT_CAINFO for more details).
174      *
175      * @param string $cacert certificate file name (or dir holding certificates)
176      * @param bool $is_dir set to true to indicate cacert is a dir. defaults to false
177      */
178     public function setCaCertificate($cacert, $is_dir = false)
179     {
180         if ($is_dir) {
181             $this->cacertdir = $cacert;
182         } else {
183             $this->cacert = $cacert;
184         }
185     }
186
187     /**
188      * Set attributes for SSL communication: private SSL key
189      * NB: does not work in older php/curl installs
190      * Thanks to Daniel Convissor.
191      *
192      * @param string $key The name of a file containing a private SSL key
193      * @param string $keypass The secret password needed to use the private SSL key
194      */
195     public function setKey($key, $keypass)
196     {
197         $this->key = $key;
198         $this->keypass = $keypass;
199     }
200
201     /**
202      * Set attributes for SSL communication: verify server certificate.
203      *
204      * @param bool $i enable/disable verification of peer certificate
205      */
206     public function setSSLVerifyPeer($i)
207     {
208         $this->verifypeer = $i;
209     }
210
211     /**
212      * Set attributes for SSL communication: verify match of server cert w. hostname.
213      *
214      * @param int $i
215      */
216     public function setSSLVerifyHost($i)
217     {
218         $this->verifyhost = $i;
219     }
220
221     /**
222      * Set proxy info.
223      *
224      * @param string $proxyhost
225      * @param string $proxyport Defaults to 8080 for HTTP and 443 for HTTPS
226      * @param string $proxyusername Leave blank if proxy has public access
227      * @param string $proxypassword Leave blank if proxy has public access
228      * @param int $proxyauthtype set to constant CURLAUTH_NTLM to use NTLM auth with proxy
229      */
230     public function setProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1)
231     {
232         $this->proxy = $proxyhost;
233         $this->proxyport = $proxyport;
234         $this->proxy_user = $proxyusername;
235         $this->proxy_pass = $proxypassword;
236         $this->proxy_authtype = $proxyauthtype;
237     }
238
239     /**
240      * Enables/disables reception of compressed xmlrpc responses.
241      * Note that enabling reception of compressed responses merely adds some standard
242      * http headers to xmlrpc requests. It is up to the xmlrpc server to return
243      * compressed responses when receiving such requests.
244      *
245      * @param string $compmethod either 'gzip', 'deflate', 'any' or ''
246      */
247     public function setAcceptedCompression($compmethod)
248     {
249         if ($compmethod == 'any') {
250             $this->accepted_compression = array('gzip', 'deflate');
251         } elseif ($compmethod == false) {
252             $this->accepted_compression = array();
253         } else {
254             $this->accepted_compression = array($compmethod);
255         }
256     }
257
258     /**
259      * Enables/disables http compression of xmlrpc request.
260      * Take care when sending compressed requests: servers might not support them
261      * (and automatic fallback to uncompressed requests is not yet implemented).
262      *
263      * @param string $compmethod either 'gzip', 'deflate' or ''
264      */
265     public function setRequestCompression($compmethod)
266     {
267         $this->request_compression = $compmethod;
268     }
269
270     /**
271      * Adds a cookie to list of cookies that will be sent to server.
272      * NB: setting any param but name and value will turn the cookie into a 'version 1' cookie:
273      * do not do it unless you know what you are doing.
274      *
275      * @param string $name
276      * @param string $value
277      * @param string $path
278      * @param string $domain
279      * @param int $port
280      *
281      * @todo check correctness of urlencoding cookie value (copied from php way of doing it...)
282      */
283     public function setCookie($name, $value = '', $path = '', $domain = '', $port = null)
284     {
285         $this->cookies[$name]['value'] = urlencode($value);
286         if ($path || $domain || $port) {
287             $this->cookies[$name]['path'] = $path;
288             $this->cookies[$name]['domain'] = $domain;
289             $this->cookies[$name]['port'] = $port;
290             $this->cookies[$name]['version'] = 1;
291         } else {
292             $this->cookies[$name]['version'] = 0;
293         }
294     }
295
296     /**
297      * Directly set cURL options, for extra flexibility
298      * It allows eg. to bind client to a specific IP interface / address.
299      *
300      * @param array $options
301      */
302     public function SetCurlOptions($options)
303     {
304         $this->extracurlopts = $options;
305     }
306
307     /**
308      * Set user-agent string that will be used by this client instance
309      * in http headers sent to the server.
310      */
311     public function SetUserAgent($agentstring)
312     {
313         $this->user_agent = $agentstring;
314     }
315
316     /**
317      * Send an xmlrpc request.
318      *
319      * @param mixed $msg The request object, or an array of requests for using multicall, or the complete xml representation of a request
320      * @param integer $timeout Connection timeout, in seconds, If unspecified, a platform specific timeout will apply
321      * @param string $method if left unspecified, the http protocol chosen during creation of the object will be used
322      *
323      * @return Response
324      */
325     public function & send($msg, $timeout = 0, $method = '')
326     {
327         // if user deos not specify http protocol, use native method of this client
328         // (i.e. method set during call to constructor)
329         if ($method == '') {
330             $method = $this->method;
331         }
332
333         if (is_array($msg)) {
334             // $msg is an array of Requests
335             $r = $this->multicall($msg, $timeout, $method);
336
337             return $r;
338         } elseif (is_string($msg)) {
339             $n = new Message('');
340             $n->payload = $msg;
341             $msg = $n;
342         }
343
344         // where msg is a Request
345         $msg->debug = $this->debug;
346
347         if ($method == 'https') {
348             $r = $this->sendPayloadHTTPS(
349                 $msg,
350                 $this->server,
351                 $this->port,
352                 $timeout,
353                 $this->username,
354                 $this->password,
355                 $this->authtype,
356                 $this->cert,
357                 $this->certpass,
358                 $this->cacert,
359                 $this->cacertdir,
360                 $this->proxy,
361                 $this->proxyport,
362                 $this->proxy_user,
363                 $this->proxy_pass,
364                 $this->proxy_authtype,
365                 $this->keepalive,
366                 $this->key,
367                 $this->keypass
368             );
369         } elseif ($method == 'http11') {
370             $r = $this->sendPayloadCURL(
371                 $msg,
372                 $this->server,
373                 $this->port,
374                 $timeout,
375                 $this->username,
376                 $this->password,
377                 $this->authtype,
378                 null,
379                 null,
380                 null,
381                 null,
382                 $this->proxy,
383                 $this->proxyport,
384                 $this->proxy_user,
385                 $this->proxy_pass,
386                 $this->proxy_authtype,
387                 'http',
388                 $this->keepalive
389             );
390         } else {
391             $r = $this->sendPayloadHTTP10(
392                 $msg,
393                 $this->server,
394                 $this->port,
395                 $timeout,
396                 $this->username,
397                 $this->password,
398                 $this->authtype,
399                 $this->proxy,
400                 $this->proxyport,
401                 $this->proxy_user,
402                 $this->proxy_pass,
403                 $this->proxy_authtype
404             );
405         }
406
407         return $r;
408     }
409
410     private function sendPayloadHTTP10($msg, $server, $port, $timeout = 0,
411                                        $username = '', $password = '', $authtype = 1, $proxyhost = '',
412                                        $proxyport = 0, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1)
413     {
414         if ($port == 0) {
415             $port = 80;
416         }
417
418         // Only create the payload if it was not created previously
419         if (empty($msg->payload)) {
420             $msg->createPayload($this->request_charset_encoding);
421         }
422
423         $payload = $msg->payload;
424         // Deflate request body and set appropriate request headers
425         if (function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) {
426             if ($this->request_compression == 'gzip') {
427                 $a = @gzencode($payload);
428                 if ($a) {
429                     $payload = $a;
430                     $encoding_hdr = "Content-Encoding: gzip\r\n";
431                 }
432             } else {
433                 $a = @gzcompress($payload);
434                 if ($a) {
435                     $payload = $a;
436                     $encoding_hdr = "Content-Encoding: deflate\r\n";
437                 }
438             }
439         } else {
440             $encoding_hdr = '';
441         }
442
443         // thanks to Grant Rauscher <grant7@firstworld.net> for this
444         $credentials = '';
445         if ($username != '') {
446             $credentials = 'Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n";
447             if ($authtype != 1) {
448                 error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported with HTTP 1.0');
449             }
450         }
451
452         $accepted_encoding = '';
453         if (is_array($this->accepted_compression) && count($this->accepted_compression)) {
454             $accepted_encoding = 'Accept-Encoding: ' . implode(', ', $this->accepted_compression) . "\r\n";
455         }
456
457         $proxy_credentials = '';
458         if ($proxyhost) {
459             if ($proxyport == 0) {
460                 $proxyport = 8080;
461             }
462             $connectserver = $proxyhost;
463             $connectport = $proxyport;
464             $uri = 'http://' . $server . ':' . $port . $this->path;
465             if ($proxyusername != '') {
466                 if ($proxyauthtype != 1) {
467                     error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported with HTTP 1.0');
468                 }
469                 $proxy_credentials = 'Proxy-Authorization: Basic ' . base64_encode($proxyusername . ':' . $proxypassword) . "\r\n";
470             }
471         } else {
472             $connectserver = $server;
473             $connectport = $port;
474             $uri = $this->path;
475         }
476
477         // Cookie generation, as per rfc2965 (version 1 cookies) or
478         // netscape's rules (version 0 cookies)
479         $cookieheader = '';
480         if (count($this->cookies)) {
481             $version = '';
482             foreach ($this->cookies as $name => $cookie) {
483                 if ($cookie['version']) {
484                     $version = ' $Version="' . $cookie['version'] . '";';
485                     $cookieheader .= ' ' . $name . '="' . $cookie['value'] . '";';
486                     if ($cookie['path']) {
487                         $cookieheader .= ' $Path="' . $cookie['path'] . '";';
488                     }
489                     if ($cookie['domain']) {
490                         $cookieheader .= ' $Domain="' . $cookie['domain'] . '";';
491                     }
492                     if ($cookie['port']) {
493                         $cookieheader .= ' $Port="' . $cookie['port'] . '";';
494                     }
495                 } else {
496                     $cookieheader .= ' ' . $name . '=' . $cookie['value'] . ";";
497                 }
498             }
499             $cookieheader = 'Cookie:' . $version . substr($cookieheader, 0, -1) . "\r\n";
500         }
501
502         // omit port if 80
503         $port = ($port == 80) ? '' : (':' . $port);
504
505         $op = 'POST ' . $uri . " HTTP/1.0\r\n" .
506             'User-Agent: ' . $this->user_agent . "\r\n" .
507             'Host: ' . $server . $port . "\r\n" .
508             $credentials .
509             $proxy_credentials .
510             $accepted_encoding .
511             $encoding_hdr .
512             'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings) . "\r\n" .
513             $cookieheader .
514             'Content-Type: ' . $msg->content_type . "\r\nContent-Length: " .
515             strlen($payload) . "\r\n\r\n" .
516             $payload;
517
518         if ($this->debug > 1) {
519             print "<PRE>\n---SENDING---\n" . htmlentities($op) . "\n---END---\n</PRE>";
520             // let the client see this now in case http times out...
521             flush();
522         }
523
524         if ($timeout > 0) {
525             $fp = @fsockopen($connectserver, $connectport, $this->errno, $this->errstr, $timeout);
526         } else {
527             $fp = @fsockopen($connectserver, $connectport, $this->errno, $this->errstr);
528         }
529         if ($fp) {
530             if ($timeout > 0 && function_exists('stream_set_timeout')) {
531                 stream_set_timeout($fp, $timeout);
532             }
533         } else {
534             $this->errstr = 'Connect error: ' . $this->errstr;
535             $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr . ' (' . $this->errno . ')');
536
537             return $r;
538         }
539
540         if (!fputs($fp, $op, strlen($op))) {
541             fclose($fp);
542             $this->errstr = 'Write error';
543             $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr);
544
545             return $r;
546         } else {
547             // reset errno and errstr on successful socket connection
548             $this->errstr = '';
549         }
550         // G. Giunta 2005/10/24: close socket before parsing.
551         // should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects)
552         $ipd = '';
553         do {
554             // shall we check for $data === FALSE?
555             // as per the manual, it signals an error
556             $ipd .= fread($fp, 32768);
557         } while (!feof($fp));
558         fclose($fp);
559         $r = $msg->parseResponse($ipd, false, $this->return_type);
560
561         return $r;
562     }
563
564     private function sendPayloadHTTPS($msg, $server, $port, $timeout = 0, $username = '',
565                                       $password = '', $authtype = 1, $cert = '', $certpass = '', $cacert = '', $cacertdir = '',
566                                       $proxyhost = '', $proxyport = 0, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1,
567                                       $keepalive = false, $key = '', $keypass = '')
568     {
569         $r = $this->sendPayloadCURL($msg, $server, $port, $timeout, $username,
570             $password, $authtype, $cert, $certpass, $cacert, $cacertdir, $proxyhost, $proxyport,
571             $proxyusername, $proxypassword, $proxyauthtype, 'https', $keepalive, $key, $keypass);
572
573         return $r;
574     }
575
576     /**
577      * Contributed by Justin Miller <justin@voxel.net>
578      * Requires curl to be built into PHP
579      * NB: CURL versions before 7.11.10 cannot use proxy to talk to https servers!
580      */
581     private function sendPayloadCURL($msg, $server, $port, $timeout = 0, $username = '',
582                                      $password = '', $authtype = 1, $cert = '', $certpass = '', $cacert = '', $cacertdir = '',
583                                      $proxyhost = '', $proxyport = 0, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1, $method = 'https',
584                                      $keepalive = false, $key = '', $keypass = '')
585     {
586         if (!function_exists('curl_init')) {
587             $this->errstr = 'CURL unavailable on this install';
588             $r = new Response(0, PhpXmlRpc::$xmlrpcerr['no_curl'], PhpXmlRpc::$xmlrpcstr['no_curl']);
589
590             return $r;
591         }
592         if ($method == 'https') {
593             if (($info = curl_version()) &&
594                 ((is_string($info) && strpos($info, 'OpenSSL') === null) || (is_array($info) && !isset($info['ssl_version'])))
595             ) {
596                 $this->errstr = 'SSL unavailable on this install';
597                 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['no_ssl'], PhpXmlRpc::$xmlrpcstr['no_ssl']);
598
599                 return $r;
600             }
601         }
602
603         if ($port == 0) {
604             if ($method == 'http') {
605                 $port = 80;
606             } else {
607                 $port = 443;
608             }
609         }
610
611         // Only create the payload if it was not created previously
612         if (empty($msg->payload)) {
613             $msg->createPayload($this->request_charset_encoding);
614         }
615
616         // Deflate request body and set appropriate request headers
617         $payload = $msg->payload;
618         if (function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) {
619             if ($this->request_compression == 'gzip') {
620                 $a = @gzencode($payload);
621                 if ($a) {
622                     $payload = $a;
623                     $encoding_hdr = 'Content-Encoding: gzip';
624                 }
625             } else {
626                 $a = @gzcompress($payload);
627                 if ($a) {
628                     $payload = $a;
629                     $encoding_hdr = 'Content-Encoding: deflate';
630                 }
631             }
632         } else {
633             $encoding_hdr = '';
634         }
635
636         if ($this->debug > 1) {
637             print "<PRE>\n---SENDING---\n" . htmlentities($payload) . "\n---END---\n</PRE>";
638             // let the client see this now in case http times out...
639             flush();
640         }
641
642         if (!$keepalive || !$this->xmlrpc_curl_handle) {
643             $curl = curl_init($method . '://' . $server . ':' . $port . $this->path);
644             if ($keepalive) {
645                 $this->xmlrpc_curl_handle = $curl;
646             }
647         } else {
648             $curl = $this->xmlrpc_curl_handle;
649         }
650
651         // results into variable
652         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
653
654         if ($this->debug) {
655             curl_setopt($curl, CURLOPT_VERBOSE, 1);
656         }
657         curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
658         // required for XMLRPC: post the data
659         curl_setopt($curl, CURLOPT_POST, 1);
660         // the data
661         curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
662
663         // return the header too
664         curl_setopt($curl, CURLOPT_HEADER, 1);
665
666         // NB: if we set an empty string, CURL will add http header indicating
667         // ALL methods it is supporting. This is possibly a better option than
668         // letting the user tell what curl can / cannot do...
669         if (is_array($this->accepted_compression) && count($this->accepted_compression)) {
670             //curl_setopt($curl, CURLOPT_ENCODING, implode(',', $this->accepted_compression));
671             // empty string means 'any supported by CURL' (shall we catch errors in case CURLOPT_SSLKEY undefined ?)
672             if (count($this->accepted_compression) == 1) {
673                 curl_setopt($curl, CURLOPT_ENCODING, $this->accepted_compression[0]);
674             } else {
675                 curl_setopt($curl, CURLOPT_ENCODING, '');
676             }
677         }
678         // extra headers
679         $headers = array('Content-Type: ' . $msg->content_type, 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings));
680         // if no keepalive is wanted, let the server know it in advance
681         if (!$keepalive) {
682             $headers[] = 'Connection: close';
683         }
684         // request compression header
685         if ($encoding_hdr) {
686             $headers[] = $encoding_hdr;
687         }
688
689         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
690         // timeout is borked
691         if ($timeout) {
692             curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 : $timeout - 1);
693         }
694
695         if ($username && $password) {
696             curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $password);
697             if (defined('CURLOPT_HTTPAUTH')) {
698                 curl_setopt($curl, CURLOPT_HTTPAUTH, $authtype);
699             } elseif ($authtype != 1) {
700                 error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported by the current PHP/curl install');
701             }
702         }
703
704         if ($method == 'https') {
705             // set cert file
706             if ($cert) {
707                 curl_setopt($curl, CURLOPT_SSLCERT, $cert);
708             }
709             // set cert password
710             if ($certpass) {
711                 curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $certpass);
712             }
713             // whether to verify remote host's cert
714             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifypeer);
715             // set ca certificates file/dir
716             if ($cacert) {
717                 curl_setopt($curl, CURLOPT_CAINFO, $cacert);
718             }
719             if ($cacertdir) {
720                 curl_setopt($curl, CURLOPT_CAPATH, $cacertdir);
721             }
722             // set key file (shall we catch errors in case CURLOPT_SSLKEY undefined ?)
723             if ($key) {
724                 curl_setopt($curl, CURLOPT_SSLKEY, $key);
725             }
726             // set key password (shall we catch errors in case CURLOPT_SSLKEY undefined ?)
727             if ($keypass) {
728                 curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $keypass);
729             }
730             // whether to verify cert's common name (CN); 0 for no, 1 to verify that it exists, and 2 to verify that it matches the hostname used
731             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->verifyhost);
732         }
733
734         // proxy info
735         if ($proxyhost) {
736             if ($proxyport == 0) {
737                 $proxyport = 8080; // NB: even for HTTPS, local connection is on port 8080
738             }
739             curl_setopt($curl, CURLOPT_PROXY, $proxyhost . ':' . $proxyport);
740             //curl_setopt($curl, CURLOPT_PROXYPORT,$proxyport);
741             if ($proxyusername) {
742                 curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyusername . ':' . $proxypassword);
743                 if (defined('CURLOPT_PROXYAUTH')) {
744                     curl_setopt($curl, CURLOPT_PROXYAUTH, $proxyauthtype);
745                 } elseif ($proxyauthtype != 1) {
746                     error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported by the current PHP/curl install');
747                 }
748             }
749         }
750
751         // NB: should we build cookie http headers by hand rather than let CURL do it?
752         // the following code does not honour 'expires', 'path' and 'domain' cookie attributes
753         // set to client obj the the user...
754         if (count($this->cookies)) {
755             $cookieheader = '';
756             foreach ($this->cookies as $name => $cookie) {
757                 $cookieheader .= $name . '=' . $cookie['value'] . '; ';
758             }
759             curl_setopt($curl, CURLOPT_COOKIE, substr($cookieheader, 0, -2));
760         }
761
762         foreach ($this->extracurlopts as $opt => $val) {
763             curl_setopt($curl, $opt, $val);
764         }
765
766         $result = curl_exec($curl);
767
768         if ($this->debug > 1) {
769             print "<PRE>\n---CURL INFO---\n";
770             foreach (curl_getinfo($curl) as $name => $val) {
771                 if (is_array($val)) {
772                     $val = implode("\n", $val);
773                 }
774                 print $name . ': ' . htmlentities($val) . "\n";
775             }
776
777             print "---END---\n</PRE>";
778         }
779
780         if (!$result) {
781             /// @todo we should use a better check here - what if we get back '' or '0'?
782
783             $this->errstr = 'no response';
784             $resp = new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error($curl));
785             curl_close($curl);
786             if ($keepalive) {
787                 $this->xmlrpc_curl_handle = null;
788             }
789         } else {
790             if (!$keepalive) {
791                 curl_close($curl);
792             }
793             $resp = $msg->parseResponse($result, true, $this->return_type);
794             // if we got back a 302, we can not reuse the curl handle for later calls
795             if ($resp->faultCode() == PhpXmlRpc::$xmlrpcerr['http_error'] && $keepalive) {
796                 curl_close($curl);
797                 $this->xmlrpc_curl_handle = null;
798             }
799         }
800
801         return $resp;
802     }
803
804     /**
805      * Send an array of requests and return an array of responses.
806      * Unless $this->no_multicall has been set to true, it will try first
807      * to use one single xmlrpc call to server method system.multicall, and
808      * revert to sending many successive calls in case of failure.
809      * This failure is also stored in $this->no_multicall for subsequent calls.
810      * Unfortunately, there is no server error code universally used to denote
811      * the fact that multicall is unsupported, so there is no way to reliably
812      * distinguish between that and a temporary failure.
813      * If you are sure that server supports multicall and do not want to
814      * fallback to using many single calls, set the fourth parameter to FALSE.
815      *
816      * NB: trying to shoehorn extra functionality into existing syntax has resulted
817      * in pretty much convoluted code...
818      *
819      * @param Request[] $msgs an array of Request objects
820      * @param integer $timeout connection timeout (in seconds)
821      * @param string $method the http protocol variant to be used
822      * @param boolean fallback When true, upon receiving an error during multicall, multiple single calls will be attempted
823      *
824      * @return array
825      */
826     public function multicall($msgs, $timeout = 0, $method = '', $fallback = true)
827     {
828         if ($method == '') {
829             $method = $this->method;
830         }
831         if (!$this->no_multicall) {
832             $results = $this->_try_multicall($msgs, $timeout, $method);
833             if (is_array($results)) {
834                 // System.multicall succeeded
835                 return $results;
836             } else {
837                 // either system.multicall is unsupported by server,
838                 // or call failed for some other reason.
839                 if ($fallback) {
840                     // Don't try it next time...
841                     $this->no_multicall = true;
842                 } else {
843                     if (is_a($results, '\PhpXmlRpc\Response')) {
844                         $result = $results;
845                     } else {
846                         $result = new Response(0, PhpXmlRpc::$xmlrpcerr['multicall_error'], PhpXmlRpc::$xmlrpcstr['multicall_error']);
847                     }
848                 }
849             }
850         } else {
851             // override fallback, in case careless user tries to do two
852             // opposite things at the same time
853             $fallback = true;
854         }
855
856         $results = array();
857         if ($fallback) {
858             // system.multicall is (probably) unsupported by server:
859             // emulate multicall via multiple requests
860             foreach ($msgs as $msg) {
861                 $results[] = $this->send($msg, $timeout, $method);
862             }
863         } else {
864             // user does NOT want to fallback on many single calls:
865             // since we should always return an array of responses,
866             // return an array with the same error repeated n times
867             foreach ($msgs as $msg) {
868                 $results[] = $result;
869             }
870         }
871
872         return $results;
873     }
874
875     /**
876      * Attempt to boxcar $msgs via system.multicall.
877      * Returns either an array of xmlrpcreponses, an xmlrpc error response
878      * or false (when received response does not respect valid multicall syntax).
879      */
880     private function _try_multicall($msgs, $timeout, $method)
881     {
882         // Construct multicall request
883         $calls = array();
884         foreach ($msgs as $msg) {
885             $call['methodName'] = new Value($msg->method(), 'string');
886             $numParams = $msg->getNumParams();
887             $params = array();
888             for ($i = 0; $i < $numParams; $i++) {
889                 $params[$i] = $msg->getParam($i);
890             }
891             $call['params'] = new Value($params, 'array');
892             $calls[] = new Value($call, 'struct');
893         }
894         $multicall = new Request('system.multicall');
895         $multicall->addParam(new Value($calls, 'array'));
896
897         // Attempt RPC call
898         $result = $this->send($multicall, $timeout, $method);
899
900         if ($result->faultCode() != 0) {
901             // call to system.multicall failed
902             return $result;
903         }
904
905         // Unpack responses.
906         $rets = $result->value();
907
908         if ($this->return_type == 'xml') {
909             return $rets;
910         } elseif ($this->return_type == 'phpvals') {
911             ///@todo test this code branch...
912             $rets = $result->value();
913             if (!is_array($rets)) {
914                 return false;       // bad return type from system.multicall
915             }
916             $numRets = count($rets);
917             if ($numRets != count($msgs)) {
918                 return false;       // wrong number of return values.
919             }
920
921             $response = array();
922             for ($i = 0; $i < $numRets; $i++) {
923                 $val = $rets[$i];
924                 if (!is_array($val)) {
925                     return false;
926                 }
927                 switch (count($val)) {
928                     case 1:
929                         if (!isset($val[0])) {
930                             return false;       // Bad value
931                         }
932                         // Normal return value
933                         $response[$i] = new Response($val[0], 0, '', 'phpvals');
934                         break;
935                     case 2:
936                         /// @todo remove usage of @: it is apparently quite slow
937                         $code = @$val['faultCode'];
938                         if (!is_int($code)) {
939                             return false;
940                         }
941                         $str = @$val['faultString'];
942                         if (!is_string($str)) {
943                             return false;
944                         }
945                         $response[$i] = new Response(0, $code, $str);
946                         break;
947                     default:
948                         return false;
949                 }
950             }
951
952             return $response;
953         } else {
954             // return type == 'xmlrpcvals'
955
956             $rets = $result->value();
957             if ($rets->kindOf() != 'array') {
958                 return false;       // bad return type from system.multicall
959             }
960             $numRets = $rets->arraysize();
961             if ($numRets != count($msgs)) {
962                 return false;       // wrong number of return values.
963             }
964
965             $response = array();
966             for ($i = 0; $i < $numRets; $i++) {
967                 $val = $rets->arraymem($i);
968                 switch ($val->kindOf()) {
969                     case 'array':
970                         if ($val->arraysize() != 1) {
971                             return false;       // Bad value
972                         }
973                         // Normal return value
974                         $response[$i] = new Response($val->arraymem(0));
975                         break;
976                     case 'struct':
977                         $code = $val->structmem('faultCode');
978                         if ($code->kindOf() != 'scalar' || $code->scalartyp() != 'int') {
979                             return false;
980                         }
981                         $str = $val->structmem('faultString');
982                         if ($str->kindOf() != 'scalar' || $str->scalartyp() != 'string') {
983                             return false;
984                         }
985                         $response[$i] = new Response(0, $code->scalarval(), $str->scalarval());
986                         break;
987                     default:
988                         return false;
989                 }
990             }
991
992             return $response;
993         }
994     }
995 }