<?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 Sstpclient extends Model {

    public $type = 'sstp-client';
    public $id;
    public $name;
    public $max_mtu;
    public $max_mru;
    public $mrru;
    public $connect_to;
    public $http_proxy;
    public $certificate;
    public $verify_server_certificate;
    public $verify_server_address_from_certificate;
    public $user;
    public $password;
    public $profile;
    public $keepalive_timeout;
    public $add_default_route;
    public $default_route_distance;
    public $dial_on_demand;
    public $authentication;
    public $pfs;
    public $tls_version = 'any';
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['max_mtu', 'string'],
            ['max_mru', 'string'],
            ['mrru', 'string'],
            ['connect_to', 'string'],
            ['http_proxy', 'string'],
            ['certificate', 'string'],
            ['verify_server_certificate', 'string'],
            ['verify_server_address_from_certificate', 'string'],
            ['user', 'string'],
            ['password', 'string'],
            ['profile', 'string'],
            ['keepalive_timeout', 'string'],
            ['add_default_route', 'string'],
            ['default_route_distance', 'string'],
            ['dial_on_demand', 'string'],
            ['pfs', 'string'],
            ['tls_version', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/sstp-client/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Sstpclient();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->max_mtu = $response->getProperty('max-mtu');
                $c->max_mru = $response->getProperty('max-mru');
                $c->mrru = $response->getProperty('mrru');
                $c->connect_to = $response->getProperty('connect-to');
                $c->http_proxy = $response->getProperty('http-proxy');
                $c->certificate = $response->getProperty('certificate');
                $c->verify_server_certificate = $response->getProperty('verify-server-certificate');
                $c->verify_server_address_from_certificate = $response->getProperty('verify-server-address-from-certificate');
                $c->user = $response->getProperty('user');
                $c->password = $response->getProperty('password');
                $c->profile = $response->getProperty('profile');
                $c->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $c->add_default_route = $response->getProperty('add-default-route');
                $c->default_route_distance = $response->getProperty('default-route-distance');
                $c->dial_on_demand = $response->getProperty('dial-on-demand');
                $c->authentication = $response->getProperty('authentication');
                $c->pfs = $response->getProperty('pfs');
                $c->tls_version = $response->getProperty('tls-version');
                $c->running = $response->getProperty('running');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    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 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;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/sstp-client/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $this->id = $response->getProperty('.id');
                $this->name = $response->getProperty('name');
                $this->max_mtu = $response->getProperty('max-mtu');
                $this->max_mru = $response->getProperty('max-mru');
                $this->mrru = $response->getProperty('mrru');
                $this->connect_to = $response->getProperty('connect-to');
                $this->http_proxy = $response->getProperty('http-proxy');
                $this->certificate = $response->getProperty('certificate');
                $this->verify_server_certificate = (($response->getProperty('verify-server-certificate') == 'true') ? true : false);
                $this->verify_server_address_from_certificate = (($response->getProperty('verify-server-address-from-certificate') == 'true') ? true : false);
                $this->user = $response->getProperty('user');
                $this->password = $response->getProperty('password');
                $this->profile = $response->getProperty('profile');
                $this->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $this->add_default_route = (($response->getProperty('add-default-route') == 'true') ? true : false);
                $this->default_route_distance = $response->getProperty('default-route-distance');
                $this->dial_on_demand = (($response->getProperty('dial-on-demand') == 'true') ? true : false);
                $this->authentication = $response->getProperty('authentication');
                $this->pfs = (($response->getProperty('pfs') == 'true') ? true : false);
                $this->tls_version = $response->getProperty('tls-version');
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Sstpclient();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/sstp-client/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        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->connect_to != $c->connect_to) {
            $setRequest->setargument('connect-to', $this->connect_to);
        }
        if ($this->http_proxy != $c->http_proxy) {
            $setRequest->setargument('http-proxy', $this->http_proxy);
        }
        if ($this->certificate != $c->certificate) {
            $setRequest->setargument('certificate', $this->certificate);
        }
        $setRequest->setargument('verify-server-certificate', (($this->verify_server_certificate == true) ? "true" : "false"));
        $setRequest->setargument('verify-server-address-from-certificate', (($this->verify_server_address_from_certificate == true) ? "true" : "false"));
        if ($this->user != $c->user) {
            $setRequest->setargument('user', $this->user);
        }
        if ($this->password != $c->password) {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->profile != $c->profile) {
            $setRequest->setargument('profile', $this->profile);
        }
        if ($this->keepalive_timeout != $c->keepalive_timeout) {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        $setRequest->setargument('add-default-route', (($this->add_default_route == true) ? "true" : "false"));
        if ($this->default_route_distance != $c->default_route_distance) {
            $setRequest->setargument('default-route-distance', $this->default_route_distance);
        }
        $setRequest->setargument('dial-on-demand', (($this->dial_on_demand == true) ? "true" : "false"));
        if ($this->authentication != $c->authentication) {
            $setRequest->setargument('authentication', $this->authentication);
        }
        $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-client/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 disable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/sstp-client/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function enable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/sstp-client/enable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/sstp-client/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->max_mtu != "") {
            $setRequest->setargument('max-mtu', $this->max_mtu);
        }
        if ($this->max_mru != "") {
            $setRequest->setargument('max-mru', $this->max_mru);
        }
        if ($this->mrru != "") {
            $setRequest->setargument('mrru', $this->mrru);
        }
        if ($this->connect_to != "") {
            $setRequest->setargument('connect-to', $this->connect_to);
        }
        if ($this->http_proxy != "") {
            $setRequest->setargument('http-proxy', $this->http_proxy);
        }
        if ($this->certificate != "") {
            $setRequest->setargument('certificate', $this->certificate);
        }
        if ($this->verify_server_certificate != "") {
            $setRequest->setargument('verify-server-certificate', (($this->verify_server_certificate == true) ? "true" : "false"));
        }
        if ($this->verify_server_address_from_certificate != "") {
            $setRequest->setargument('verify-server-address-from-certificate', (($this->verify_server_address_from_certificate == true) ? "true" : "false"));
        }
        if ($this->user != "") {
            $setRequest->setargument('user', $this->user);
        }
        if ($this->password != "") {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->profile != "") {
            $setRequest->setargument('profile', $this->profile);
        }
        if ($this->keepalive_timeout != "") {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        if ($this->add_default_route != "") {
            $setRequest->setargument('add-default-route', (($this->add_default_route == true) ? "true" : "false"));
        }
        if ($this->default_route_distance != "") {
            $setRequest->setargument('default-route-distance', $this->default_route_distance);
        }
        if ($this->dial_on_demand != "") {
            $setRequest->setargument('dial-on-demand', (($this->dial_on_demand == true) ? "true" : "false"));
        }
        if ($this->authentication != "") {
            $setRequest->setargument('authentication', $this->authentication);
        }
        if ($this->pfs != "") {
            $setRequest->setargument('pfs', (($this->pfs == true) ? "true" : "false"));
        }
        if ($this->tls_version != "") {
            $setRequest->setargument('tls-version', $this->tls_version);
        }
        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();
        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/interface/sstp-client');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
