<?php

namespace dokuwiki\Remote;

use IXR\DataType\Base64;
use IXR\DataType\Date;
use IXR\Exception\ServerException;
use IXR\Message\Error;
use IXR\Server\Server;

/**
 * Contains needed wrapper functions and registers all available XMLRPC functions.
 */
class XmlRpcServer extends Server
{
    protected $remote;

    /**
     * Constructor. Register methods and run Server
     */
    public function __construct($wait=false)
    {
        $this->remote = new Api();
        $this->remote->setDateTransformation(array($this, 'toDate'));
        $this->remote->setFileTransformation(array($this, 'toFile'));
        parent::__construct(false, false, $wait);
    }

    /**
     * @inheritdoc
     */
    public function call($methodname, $args)
    {
        try {
            $result = $this->remote->call($methodname, $args);
            return $result;
        } catch (AccessDeniedException $e) {
            if (!isset($_SERVER['REMOTE_USER'])) {
                http_status(401);
                return new Error(-32603, "server error. not authorized to call method $methodname");
            } else {
                http_status(403);
                return new Error(-32604, "server error. forbidden to call the method $methodname");
            }
        } catch (RemoteException $e) {
            return new Error($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
     * @return Date
     */
    public function toDate($data)
    {
        return new Date($data);
    }

    /**
     * @param string $data
     * @return Base64
     */
    public function toFile($data)
    {
        return new Base64($data);
    }
}
