<?php

/**
 * OpenKM, Open Knowledge Management System S.L.  (http://www.openkm.com)
 * Copyright (c) 2006-2018
 *
 * No bytes were intentionally harmed during the development of this application.
 *
 * This program is commercial software; you can use it under the terms of the
 * EULA - OpenKM SDK End User License Agreement as published by OpenKM Knowledge Management System S.L.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * EULA - OpenKM SDK End User License Agreement for more details:
 * http://docs.openkm.com/kcenter/view/licenses/eula-openkm-sdk-end-user-license-agreement.html
 */

namespace openkm\impl;

use openkm\impl\AuthImpl;
use openkm\impl\DocumentImpl;
use openkm\impl\FolderImpl;
use openkm\impl\MailImpl;
use openkm\impl\NoteImpl;
use openkm\impl\PropertyGroupImpl;
use openkm\impl\PropertyImpl;
use openkm\impl\RepositoryImpl;
use openkm\impl\SearchImpl;
use openkm\impl\RecordImpl;
use openkm\impl\RelationImpl;
use openkm\impl\WorkflowImpl;
use openkm\impl\ConversionImpl;
use openkm\impl\TaskImpl;
use openkm\impl\ReportImpl;

/**
 * OKMWebservices
 *
 * @author sochoa
 */
class OKMWebservices {

    public $auth;
    public $document;
    public $folder;
    public $node;
    public $note;
    public $propertyGroup;
    public $search;
    public $plugin;

    public function __construct($host) {
        $this->auth = new AuthImpl($host);
        $this->document = new DocumentImpl($host);
        $this->folder = new FolderImpl($host);
        $this->node = new NodeImpl($host);
        $this->note = new NoteImpl($host);
        $this->propertyGroup = new PropertyGroupImpl($host);
        $this->search = new SearchImpl($host);
        $this->plugin = new PluginImpl($host);
    }

    public function setAuthorizationToken($authorizationToken) {
        $this->auth->setAuthorizationToken($authorizationToken);
        $this->document->setAuthorizationToken($authorizationToken);
        $this->folder->setAuthorizationToken($authorizationToken);
        $this->node->setAuthorizationToken($authorizationToken);
        $this->note->setAuthorizationToken($authorizationToken);
        $this->propertyGroup->setAuthorizationToken($authorizationToken);
        $this->search->setAuthorizationToken($authorizationToken);
        $this->plugin->setAuthorizationToken($authorizationToken);
    }

    /**
     * Simulate first login process
     *
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws UnknowException
     */
    public function login($username, $password, $expiration = 1, $restrictIp = false) {
        $authorizationToken = $this->auth->login($username, $password, $expiration, $restrictIp);
        $this->setAuthorizationToken($authorizationToken); // Initialize authorization token
        return $authorizationToken;
    }

    public function logout() {
        $this->auth->logout();
        $this->setAuthorizationToken(null); // Reset authorization token
    }
}

?>
