1<?php
2
3namespace dokuwiki\Remote\IXR;
4
5use dokuwiki\HTTP\HTTPClient;
6use IXR\Message\Message;
7use IXR\Request\Request;
8
9/**
10 * This implements a XML-RPC client using our own HTTPClient
11 *
12 * Note: this now inherits from the IXR library's client and no longer from HTTPClient. Instead composition
13 * is used to add the HTTP client.
14 */
15class Client extends \IXR\Client\Client
16{
17    /** @var HTTPClient */
18    protected $httpClient;
19
20    /** @var string */
21    protected $posturl = '';
22
23    /** @inheritdoc */
24    public function __construct($server, $path = false, $port = 80, $timeout = 15, $timeout_io = null)
25    {
26        parent::__construct($server, $path, $port, $timeout, $timeout_io);
27        if (!$path) {
28            // Assume we have been given an URL instead
29            $this->posturl = $server;
30        } else {
31            $this->posturl = 'http://' . $server . ':' . $port . $path;
32        }
33
34        $this->httpClient = new HTTPClient();
35        $this->httpClient->timeout = $timeout;
36    }
37
38    /** @inheritdoc */
39    public function query(...$args)
40    {
41        $method = array_shift($args);
42        $request = new Request($method, $args);
43        $length = $request->getLength();
44        $xml = $request->getXml();
45
46        $this->headers['Content-Type'] = 'text/xml';
47        $this->headers['Content-Length'] = $length;
48        $this->httpClient->headers = array_merge($this->httpClient->headers, $this->headers);
49
50        if (!$this->httpClient->sendRequest($this->posturl, $xml, 'POST')) {
51            $this->handleError(-32300, 'transport error - ' . $this->httpClient->error);
52            return false;
53        }
54
55        // Check HTTP Response code
56        if ($this->httpClient->status < 200 || $this->httpClient->status > 206) {
57            $this->handleError(-32300, 'transport error - HTTP status ' . $this->httpClient->status);
58            return false;
59        }
60
61        // Now parse what we've got back
62        $this->message = new Message($this->httpClient->resp_body);
63        if (!$this->message->parse()) {
64            // XML error
65            return $this->handleError(-32700, 'Parse error. Message not well formed');
66        }
67
68        // Is the message a fault?
69        if ($this->message->messageType == 'fault') {
70            return $this->handleError($this->message->faultCode, $this->message->faultString);
71        }
72
73        // Message must be OK
74        return true;
75    }
76
77    /**
78     * Direct access to the underlying HTTP client if needed
79     *
80     * @return HTTPClient
81     */
82    public function getHTTPClient()
83    {
84        return $this->httpClient;
85    }
86}
87