xref: /dokuwiki/inc/Remote/IXR/Client.php (revision 104a3b7cee0d6263c9886c95da6cd5d929f79a06)
17f8f2456SAndreas Gohr<?php
27f8f2456SAndreas Gohr
37f8f2456SAndreas Gohrnamespace dokuwiki\Remote\IXR;
47f8f2456SAndreas Gohr
57f8f2456SAndreas Gohruse dokuwiki\HTTP\HTTPClient;
67f8f2456SAndreas Gohruse IXR\Message\Message;
77f8f2456SAndreas Gohruse IXR\Request\Request;
87f8f2456SAndreas Gohr
97f8f2456SAndreas Gohr/**
107f8f2456SAndreas Gohr * This implements a XML-RPC client using our own HTTPClient
117f8f2456SAndreas Gohr *
127f8f2456SAndreas Gohr * Note: this now inherits from the IXR library's client and no longer from HTTPClient. Instead composition
137f8f2456SAndreas Gohr * is used to add the HTTP client.
147f8f2456SAndreas Gohr */
157f8f2456SAndreas Gohrclass Client extends \IXR\Client\Client
167f8f2456SAndreas Gohr{
177f8f2456SAndreas Gohr
187f8f2456SAndreas Gohr    /** @var HTTPClient */
197f8f2456SAndreas Gohr    protected $httpClient;
207f8f2456SAndreas Gohr
217f8f2456SAndreas Gohr    /** @var string */
227f8f2456SAndreas Gohr    protected $posturl = '';
237f8f2456SAndreas Gohr
247f8f2456SAndreas Gohr    /** @inheritdoc */
257f8f2456SAndreas Gohr    public function __construct($server, $path = false, $port = 80, $timeout = 15, $timeout_io = null)
267f8f2456SAndreas Gohr    {
277f8f2456SAndreas Gohr        parent::__construct($server, $path, $port, $timeout, $timeout_io);
287f8f2456SAndreas Gohr        if (!$path) {
297f8f2456SAndreas Gohr            // Assume we have been given an URL instead
307f8f2456SAndreas Gohr            $this->posturl = $server;
317f8f2456SAndreas Gohr        } else {
327f8f2456SAndreas Gohr            $this->posturl = 'http://' . $server . ':' . $port . $path;
337f8f2456SAndreas Gohr        }
347f8f2456SAndreas Gohr
357f8f2456SAndreas Gohr        $this->httpClient = new HTTPClient();
367f8f2456SAndreas Gohr        $this->httpClient->timeout = $timeout;
377f8f2456SAndreas Gohr    }
387f8f2456SAndreas Gohr
397f8f2456SAndreas Gohr    /** @inheritdoc */
40*104a3b7cSAndreas Gohr    public function query(...$args)
417f8f2456SAndreas Gohr    {
427f8f2456SAndreas Gohr        $method = array_shift($args);
437f8f2456SAndreas Gohr        $request = new Request($method, $args);
447f8f2456SAndreas Gohr        $length = $request->getLength();
457f8f2456SAndreas Gohr        $xml = $request->getXml();
467f8f2456SAndreas Gohr
477f8f2456SAndreas Gohr        $this->headers['Content-Type'] = 'text/xml';
487f8f2456SAndreas Gohr        $this->headers['Content-Length'] = $length;
497f8f2456SAndreas Gohr        $this->httpClient->headers = array_merge($this->httpClient->headers, $this->headers);
507f8f2456SAndreas Gohr
517f8f2456SAndreas Gohr        if (!$this->httpClient->sendRequest($this->posturl, $xml, 'POST')) {
527f8f2456SAndreas Gohr            $this->handleError(-32300, 'transport error - ' . $this->httpClient->error);
537f8f2456SAndreas Gohr            return false;
547f8f2456SAndreas Gohr        }
557f8f2456SAndreas Gohr
567f8f2456SAndreas Gohr        // Check HTTP Response code
577f8f2456SAndreas Gohr        if ($this->httpClient->status < 200 || $this->httpClient->status > 206) {
587f8f2456SAndreas Gohr            $this->handleError(-32300, 'transport error - HTTP status ' . $this->httpClient->status);
597f8f2456SAndreas Gohr            return false;
607f8f2456SAndreas Gohr        }
617f8f2456SAndreas Gohr
627f8f2456SAndreas Gohr        // Now parse what we've got back
637f8f2456SAndreas Gohr        $this->message = new Message($this->httpClient->resp_body);
647f8f2456SAndreas Gohr        if (!$this->message->parse()) {
657f8f2456SAndreas Gohr            // XML error
667f8f2456SAndreas Gohr            return $this->handleError(-32700, 'Parse error. Message not well formed');
677f8f2456SAndreas Gohr        }
687f8f2456SAndreas Gohr
697f8f2456SAndreas Gohr        // Is the message a fault?
707f8f2456SAndreas Gohr        if ($this->message->messageType == 'fault') {
717f8f2456SAndreas Gohr            return $this->handleError($this->message->faultCode, $this->message->faultString);
727f8f2456SAndreas Gohr        }
737f8f2456SAndreas Gohr
747f8f2456SAndreas Gohr        // Message must be OK
757f8f2456SAndreas Gohr        return true;
767f8f2456SAndreas Gohr    }
777f8f2456SAndreas Gohr
787f8f2456SAndreas Gohr    /**
797f8f2456SAndreas Gohr     * Direct access to the underlying HTTP client if needed
807f8f2456SAndreas Gohr     *
817f8f2456SAndreas Gohr     * @return HTTPClient
827f8f2456SAndreas Gohr     */
837f8f2456SAndreas Gohr    public function getHTTPClient()
847f8f2456SAndreas Gohr    {
857f8f2456SAndreas Gohr        return $this->httpClient;
867f8f2456SAndreas Gohr    }
877f8f2456SAndreas Gohr}
88