<?php

namespace app\models\mikrotik\ppp;

use yii\base\Model;
use Yii;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Sstp extends Model {

    public $enabled;
    public $port;
    public $max_mtu;
    public $max_mru;
    public $mrru;
    public $keepalive_timeout;
    public $default_profile;
    public $authentication;
    public $certificate;
    public $verify_client_certificate;
    public $force_aes;
    public $pfs;
    public $tls_version;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['port', 'string'],
            ['max_mtu', 'string'],
            ['max_mru', 'string'],
            ['mrru', 'string'],
            ['keepalive_timeout', 'string'],
            ['default_profile', 'string'],
            ['certificate', 'string'],
            ['verify_client_certificate', 'string'],
            ['force_aes', 'string'],
            ['pfs', 'string'],
            ['tls_version', 'string'],
        ];
    }

    public function getKeys() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/certificate/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {

                if ($response->getProperty('private-key') == 'true') {
                    $return[$response->getProperty('name')] = $response->getProperty('name');
                }
            }
        }
        return $return;
    }

    public function get() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/sstp-server/server/print');

        $response = $connect->getConnection()->sendSync($getRequest);

        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->port = $response->getProperty('port');
        $this->max_mtu = $response->getProperty('max-mtu');
        $this->max_mru = $response->getProperty('max-mru');
        $this->mrru = $response->getProperty('mrru');
        $this->keepalive_timeout = $response->getProperty('keepalive-timeout');
        $this->default_profile = $response->getProperty('default-profile');
        $this->authentication = $response->getProperty('authentication');
        $this->certificate = $response->getProperty('certificate');
        $this->verify_client_certificate = (($response->getProperty('verify-client-certificate') == 'true') ? true : false);
        $this->force_aes = (($response->getProperty('force-aes') == 'true') ? true : false);
        $this->pfs = (($response->getProperty('pfs') == 'true') ? true : false);
        $this->tls_version = $response->getProperty('tls-version');

        return;
    }

    public function update() {
        $c = new Sstp();
        $c->get();
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/sstp-server/server/set');

        $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        if ($this->port != $c->port) {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->max_mtu != $c->max_mtu) {
            $setRequest->setargument('max-mtu', $this->max_mtu);
        }
        if ($this->max_mru != $c->max_mru) {
            $setRequest->setargument('max-mru', $this->max_mru);
        }
        if ($this->mrru != $c->mrru) {
            $setRequest->setargument('mrru', $this->mrru);
        }
        if ($this->keepalive_timeout != $c->keepalive_timeout) {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        if ($this->default_profile != $c->default_profile) {
            $setRequest->setargument('default-profile', $this->default_profile);
        }
        if ($this->authentication != $c->authentication) {
            $setRequest->setargument('authentication', $this->authentication);
        }
        if ($this->certificate != $c->certificate) {
            $setRequest->setargument('certificate', $this->certificate);
        }
        $setRequest->setargument('verify-client-certificate', (($this->verify_client_certificate == true) ? "true" : "false"));
        $setRequest->setargument('force-aes', (($this->force_aes == true) ? "true" : "false"));
        $setRequest->setargument('pfs', (($this->pfs == true) ? "true" : "false"));
        if ($this->tls_version != $c->tls_version) {
            $setRequest->setargument('tls-version', $this->tls_version);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/sstp-server/server/unset');
            foreach ($unset as $value) {
                $unsetRequest->setArgument('.id', $id);
                $unsetRequest->setArgument('value-name', $value);
                $resp = $connect->getConnection()->sendSync($unsetRequest);
                if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
                    if (!isset($re)) {
                        $re = '';
                    } $re.= ' ' . $resp->getProperty('message');
                }
            }
        } $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            if (isset($re)) {
                $re.= ' ' . $resp->getProperty('message');
            } else {
                $re = $resp->getProperty('message');
            }
        } if (isset($re)) {
            return $re;
        } else {
            return '1';
        }
    }

    public function getProfiles() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ppp/profile/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $return[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $return;
    }

}
