xref: /dokuwiki/inc/Remote/IXR/Client.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
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    /** @var HTTPClient */
187f8f2456SAndreas Gohr    protected $httpClient;
197f8f2456SAndreas Gohr
207f8f2456SAndreas Gohr    /** @var string */
217f8f2456SAndreas Gohr    protected $posturl = '';
227f8f2456SAndreas Gohr
237f8f2456SAndreas Gohr    /** @inheritdoc */
247f8f2456SAndreas Gohr    public function __construct($server, $path = false, $port = 80, $timeout = 15, $timeout_io = null)
257f8f2456SAndreas Gohr    {
267f8f2456SAndreas Gohr        parent::__construct($server, $path, $port, $timeout, $timeout_io);
277f8f2456SAndreas Gohr        if (!$path) {
287f8f2456SAndreas Gohr            // Assume we have been given an URL instead
297f8f2456SAndreas Gohr            $this->posturl = $server;
307f8f2456SAndreas Gohr        } else {
317f8f2456SAndreas Gohr            $this->posturl = 'http://' . $server . ':' . $port . $path;
327f8f2456SAndreas Gohr        }
337f8f2456SAndreas Gohr
347f8f2456SAndreas Gohr        $this->httpClient = new HTTPClient();
357f8f2456SAndreas Gohr        $this->httpClient->timeout = $timeout;
367f8f2456SAndreas Gohr    }
377f8f2456SAndreas Gohr
387f8f2456SAndreas Gohr    /** @inheritdoc */
39*104a3b7cSAndreas Gohr    public function query(...$args)
407f8f2456SAndreas Gohr    {
417f8f2456SAndreas Gohr        $method = array_shift($args);
427f8f2456SAndreas Gohr        $request = new Request($method, $args);
437f8f2456SAndreas Gohr        $length = $request->getLength();
447f8f2456SAndreas Gohr        $xml = $request->getXml();
457f8f2456SAndreas Gohr
467f8f2456SAndreas Gohr        $this->headers['Content-Type'] = 'text/xml';
477f8f2456SAndreas Gohr        $this->headers['Content-Length'] = $length;
487f8f2456SAndreas Gohr        $this->httpClient->headers = array_merge($this->httpClient->headers, $this->headers);
497f8f2456SAndreas Gohr
507f8f2456SAndreas Gohr        if (!$this->httpClient->sendRequest($this->posturl, $xml, 'POST')) {
517f8f2456SAndreas Gohr            $this->handleError(-32300, 'transport error - ' . $this->httpClient->error);
527f8f2456SAndreas Gohr            return false;
537f8f2456SAndreas Gohr        }
547f8f2456SAndreas Gohr
557f8f2456SAndreas Gohr        // Check HTTP Response code
567f8f2456SAndreas Gohr        if ($this->httpClient->status < 200 || $this->httpClient->status > 206) {
577f8f2456SAndreas Gohr            $this->handleError(-32300, 'transport error - HTTP status ' . $this->httpClient->status);
587f8f2456SAndreas Gohr            return false;
597f8f2456SAndreas Gohr        }
607f8f2456SAndreas Gohr
617f8f2456SAndreas Gohr        // Now parse what we've got back
627f8f2456SAndreas Gohr        $this->message = new Message($this->httpClient->resp_body);
637f8f2456SAndreas Gohr        if (!$this->message->parse()) {
647f8f2456SAndreas Gohr            // XML error
657f8f2456SAndreas Gohr            return $this->handleError(-32700, 'Parse error. Message not well formed');
667f8f2456SAndreas Gohr        }
677f8f2456SAndreas Gohr
687f8f2456SAndreas Gohr        // Is the message a fault?
697f8f2456SAndreas Gohr        if ($this->message->messageType == 'fault') {
707f8f2456SAndreas Gohr            return $this->handleError($this->message->faultCode, $this->message->faultString);
717f8f2456SAndreas Gohr        }
727f8f2456SAndreas Gohr
737f8f2456SAndreas Gohr        // Message must be OK
747f8f2456SAndreas Gohr        return true;
757f8f2456SAndreas Gohr    }
767f8f2456SAndreas Gohr
777f8f2456SAndreas Gohr    /**
787f8f2456SAndreas Gohr     * Direct access to the underlying HTTP client if needed
797f8f2456SAndreas Gohr     *
807f8f2456SAndreas Gohr     * @return HTTPClient
817f8f2456SAndreas Gohr     */
827f8f2456SAndreas Gohr    public function getHTTPClient()
837f8f2456SAndreas Gohr    {
847f8f2456SAndreas Gohr        return $this->httpClient;
857f8f2456SAndreas Gohr    }
867f8f2456SAndreas Gohr}
87