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

    public $enabled;
    public $port;
    public $mode;
    public $netmask;
    public $mac_address;
    public $max_mtu;
    public $keepalive_timeout;
    public $default_profile;
    public $certificate;
    public $require_client_certificate;
    public $auth;
    public $cipher;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['port', 'string'],
            ['mode', 'string'],
            ['netmask', 'string'],
            ['mac_address', 'string'],
            ['max_mtu', 'string'],
            ['keepalive_timeout', 'string'],
            ['default_profile', 'string'],
            ['certificate', 'string'],
            ['require_client_certificate', 'string'],
        ];
    }

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

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

        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->port = $response->getProperty('port');
        $this->mode = $response->getProperty('mode');
        $this->netmask = $response->getProperty('netmask');
        $this->mac_address = $response->getProperty('mac-address');
        $this->max_mtu = $response->getProperty('max-mtu');
        $this->keepalive_timeout = $response->getProperty('keepalive-timeout');
        $this->default_profile = $response->getProperty('default-profile');
        $this->certificate = $response->getProperty('certificate');
        $this->require_client_certificate = (($response->getProperty('require-client-certificate') == 'true') ? true : false);
        $this->auth = $response->getProperty('auth');
        $this->cipher = $response->getProperty('cipher');

        return;
    }

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

        $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        if ($this->port != $c->port) {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->mode != $c->mode) {
            $setRequest->setargument('mode', $this->mode);
        }
        if ($this->netmask != $c->netmask) {
            $setRequest->setargument('netmask', $this->netmask);
        }
        if ($this->mac_address != $c->mac_address) {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->max_mtu != $c->max_mtu) {
            $setRequest->setargument('max-mtu', $this->max_mtu);
        }
        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->certificate != $c->certificate) {
            $setRequest->setargument('certificate', $this->certificate);
        }
        $setRequest->setargument('require-client-certificate', (($this->require_client_certificate == true) ? "true" : "false"));
        if ($this->auth != $c->auth) {
            $setRequest->setargument('auth', $this->auth);
        }
        if ($this->cipher != $c->cipher) {
            $setRequest->setargument('cipher', $this->cipher);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/ovpn-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 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;
    }

}
