xref: /dokuwiki/inc/Remote/IXR/Client.php (revision 177d6836e2f75d0e404be9c566e61725852a1e07)
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(...$args)
41    {
42        $method = array_shift($args);
43        $request = new Request($method, $args);
44        $length = $request->getLength();
45        $xml = $request->getXml();
46
47        $this->headers['Content-Type'] = 'text/xml';
48        $this->headers['Content-Length'] = $length;
49        $this->httpClient->headers = array_merge($this->httpClient->headers, $this->headers);
50
51        if (!$this->httpClient->sendRequest($this->posturl, $xml, 'POST')) {
52            $this->handleError(-32300, 'transport error - ' . $this->httpClient->error);
53            return false;
54        }
55
56        // Check HTTP Response code
57        if ($this->httpClient->status < 200 || $this->httpClient->status > 206) {
58            $this->handleError(-32300, 'transport error - HTTP status ' . $this->httpClient->status);
59            return false;
60        }
61
62        // Now parse what we've got back
63        $this->message = new Message($this->httpClient->resp_body);
64        if (!$this->message->parse()) {
65            // XML error
66            return $this->handleError(-32700, 'Parse error. Message not well formed');
67        }
68
69        // Is the message a fault?
70        if ($this->message->messageType == 'fault') {
71            return $this->handleError($this->message->faultCode, $this->message->faultString);
72        }
73
74        // Message must be OK
75        return true;
76    }
77
78    /**
79     * Direct access to the underlying HTTP client if needed
80     *
81     * @return HTTPClient
82     */
83    public function getHTTPClient()
84    {
85        return $this->httpClient;
86    }
87}
88