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