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
18    /** @var HTTPClient */
19    protected $httpClient;
20
21    /** @var string */
22    protected $posturl = '';
23
24    /** @inheritdoc */
25    public function __construct($server, $path = false, $port = 80, $timeout = 15, $timeout_io = null)
26    {
27        parent::__construct($server, $path, $port, $timeout, $timeout_io);
28        if (!$path) {
29            // Assume we have been given an URL instead
30            $this->posturl = $server;
31        } else {
32            $this->posturl = 'http://' . $server . ':' . $port . $path;
33        }
34
35        $this->httpClient = new HTTPClient();
36        $this->httpClient->timeout = $timeout;
37    }
38
39    /** @inheritdoc */
40    public function query()
41    {
42        $args = func_get_args();
43        $method = array_shift($args);
44        $request = new Request($method, $args);
45        $length = $request->getLength();
46        $xml = $request->getXml();
47
48        $this->headers['Content-Type'] = 'text/xml';
49        $this->headers['Content-Length'] = $length;
50        $this->httpClient->headers = array_merge($this->httpClient->headers, $this->headers);
51
52        if (!$this->httpClient->sendRequest($this->posturl, $xml, 'POST')) {
53            $this->handleError(-32300, 'transport error - ' . $this->httpClient->error);
54            return false;
55        }
56
57        // Check HTTP Response code
58        if ($this->httpClient->status < 200 || $this->httpClient->status > 206) {
59            $this->handleError(-32300, 'transport error - HTTP status ' . $this->httpClient->status);
60            return false;
61        }
62
63        // Now parse what we've got back
64        $this->message = new Message($this->httpClient->resp_body);
65        if (!$this->message->parse()) {
66            // XML error
67            return $this->handleError(-32700, 'Parse error. Message not well formed');
68        }
69
70        // Is the message a fault?
71        if ($this->message->messageType == 'fault') {
72            return $this->handleError($this->message->faultCode, $this->message->faultString);
73        }
74
75        // Message must be OK
76        return true;
77    }
78
79    /**
80     * Direct access to the underlying HTTP client if needed
81     *
82     * @return HTTPClient
83     */
84    public function getHTTPClient()
85    {
86        return $this->httpClient;
87    }
88}
89