<?php
// Copyright 1999-2017. Plesk International GmbH. All rights reserved.

namespace Plesk\MigrationLibV8\SiteImport\Backend\Files;

use Plesk\MigrationLibV8\Plesk\PleskFunctions;

class Config extends \Plesk\MigrationLibV8\Common\Backend\Files\Config
{
    const CONFIG_PASSWORD_TYPE_ENCRYPTED = 'encrypted';

    /**
     * Initialize default options, that do not depend on user's input and should be always presented
     */
    public function initDefaultOptions() {
        parent::initDefaultOptions();
        $this->setAllowInsecureConnections(false);
    }

    /**
     * Set host to transfer sites/applications from. Used for site migration scenario.
     *
     * @param $host string
     */
    public function setSourceHost($host)
    {
        $this->setSourceValue('host', $host);
    }

    /**
     * Get host to transfer sites/applications from. Used for site migration scenario.
     *
     * @return string
     */
    public function getSourceHost()
    {
        $host = $this->getSourceValue('host');
        if (is_null($host)) {
            // for compatibility with existing sessions (parameter was renamed from "ftp-host" to "host")
            $host = $this->getSourceValue('ftp-host');
        }
        return $host;
    }

    /**
     * Set username for FTP/SSH host to transfer sites/applications from. Used for site migration scenario.
     *
     * @param $username string
     */
    public function setSourceUsername($username)
    {
        $this->setSourceValue('username', $username);
    }

    /**
     * Set password for FTP/SSH host to transfer sites/applications from. Used for site migration scenario.
     *
     * @param $password string
     */
    public function setSourcePassword($password)
    {
        $passwordEncrypted = PleskFunctions::encryptPassword($password);
        $this->setSourceValue(
            'password', $passwordEncrypted
        );
        $this->setSourceValue(
            'password-type', self::CONFIG_PASSWORD_TYPE_ENCRYPTED
        );
    }

    /**
     * Set encoding in which source FTP server returns directory listings.
     *
     * @param $ftpEncoding string
     */
    public function setSourceFtpEncoding($ftpEncoding)
    {
        $this->setSourceValue('ftp-encoding', $ftpEncoding);
    }

    /**
     * Set base URL of the site to transfer. Used for site migration scenario.
     *
     * @param $baseUrl string
     */
    public function setSourceBaseUrl($baseUrl)
    {
        $this->setSourceValue('base-url', $baseUrl);
    }

    /**
     * Set document root of migrated site related to FTP root or SSH home. Used for site migration scenario.
     *
     * @param $documentRoot string
     */
    public function setSourceDocumentRoot($documentRoot)
    {
        $this->setSourceValue('document-root', $documentRoot);
    }

    /**
     * Set files root of migrated site.
     *
     * Files root is a path to start scan for files, applications and document root,
     * related to FTP/SSH root. When not set, default is used: when transport is FTP,
     * then FTP root is used, when transport is SSH, then home directory of SSH user is used.
     *
     * @param $filesRoot string
     */
    public function setSourceFilesRoot($filesRoot)
    {
        $this->setSourceValue('files-root', $filesRoot);
    }

    /**
     * Set way to list files on the source server
     *
     * Possible ways: with file transport (FTP or SSH - see "file-transport" option), with Web RPC agent,
     * with Web RPC agent with fallback to file transport.
     *
     * General considerations:
     * - Listing with Web RPC agent works much more fast than listing with FTP, but it could be less reliable.
     * - When FTP is used as file transport, listing with Web RPC agent with FTP fallback in
     * case of unicode names and or any failure of RPC agent takes advantages of both ways:
     * usually it works fast, but in case of any failure it works like FTP listing, so it is reliable.
     * By default, migrator automatically selects the way which is optimal for most cases.
     *
     * Possible values: 'auto', 'file-transport', 'web-rpc-agent', 'hybrid'
     *
     * @param $listFilesMode string
     */
    public function setListFilesMode($listFilesMode)
    {
        $this->setSourceValue('list-files-mode', $listFilesMode);
    }

    /**
     * Set domain name to which site should be transferred.
     *
     * @param $domain string
     */
    public function setSourceTargetDomain($domain)
    {
        $this->setSourceValue('target-domain', $domain);
    }

    /**
     * Set whether to allow HTTP connections and HTTPS connections without valid SSL certificate
     * When set to False only HTTPS connections with valid certificate
     * and explicitly defined connections will be allowed.
     * When set to True any connections will be allowed, SSL certificates validation will be skipped.
     *
     * @param $allowInsecureConnections bool
     */
    public function setAllowInsecureConnections($allowInsecureConnections)
    {
        $this->setSourceValue(
            'allow-insecure-connections',
            $this->_encodeBooleanValue($allowInsecureConnections)
        );
    }

    /**
     * Set file transport - way to perform file operations on remote server.
     *
     * @param $fileTransport string
     */
    public function setSourceFileTransport($fileTransport)
    {
        $this->setSourceValue('file-transport', $fileTransport);
    }

    /**
     * Set way to transfer files from source site to target one.
     *
     * @param $transferFilesMode string
     */
    public function setSourceTransferFilesMode($transferFilesMode)
    {
        $this->setSourceValue('transfer-files-mode', $transferFilesMode);
    }

    /**
     * Set whether transfer of files with web streaming should be automatically selected when it makes sense.
     *
     * Web streaming makes sense in case when transfer files mode is 'auto' and actual file transport is FTP.
     * Web streaming is a beta feature, and so it is not selected by default.
     * @param $allowAutoWebStreaming bool
     */
    public function setSourceAllowAutoWebStreaming($allowAutoWebStreaming)
    {
        $this->setSourceValue('allow-auto-web-streaming', $this->_encodeBooleanValue($allowAutoWebStreaming));
    }

    /**
     * Set port to connect to the source website by SSH.
     *
     * @param $sshPort string
     */
    public function setSourceSSHPort($sshPort)
    {
        $this->setSourceValue('ssh-port', $sshPort);
    }

    /**
     * Set whether to ignore IP address returned by FTP server when opening data channel in passive mode.

     * When is set to true, IP address returned by FTP server when opening data channel
     * in passive mode is ignored. That option could be necessary because some misconfigured
     * FTP servers return internal IPs, which could not be used to connect to the server.
     * When the option is set, hostname specified for control connection to the FTP server
     * will be used for data connections.
     *
     * @param $ignorePassiveModeServerIp bool
     */
    public function setIgnorePassiveModeServerIp($ignorePassiveModeServerIp)
    {
        $this->setSourceValue(
            'ignore-passive-mode-server-ip',
            $this->_encodeBooleanValue($ignorePassiveModeServerIp)
        );
    }
}
