<?php

namespace app\models\mikrotik\capsman;

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 Security extends Model {

    public $id;
    public $name;
    public $authentication_types;
    public $encryption;
    public $group_encryption;
    public $passphrase;
    public $eap_methods;
    public $tls_mode;
    public $tls_certificate;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['group_encryption', 'string'],
            ['passphrase', 'string'],
            ['tls_mode', 'string'],
            ['tls_certificate', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/security/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Security();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->authentication_types = $response->getProperty('authentication-types');
                $c->encryption = $response->getProperty('encryption');
                $c->group_encryption = $response->getProperty('group-encryption');
                $c->passphrase = $response->getProperty('passphrase');
                $c->eap_methods = $response->getProperty('eap-methods');
                $c->tls_mode = $response->getProperty('tls-mode');
                $c->tls_certificate = $response->getProperty('tls-certificate');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/security/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->authentication_types = $response->getProperty('authentication-types');
                $this->encryption = $response->getProperty('encryption');
                $this->group_encryption = $response->getProperty('group-encryption');
                $this->passphrase = $response->getProperty('passphrase');
                $this->eap_methods = $response->getProperty('eap-methods');
                $this->tls_mode = $response->getProperty('tls-mode');
                $this->tls_certificate = $response->getProperty('tls-certificate');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Security();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/security/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->authentication_types != $c->authentication_types) {
            if ($this->authentication_types == '') {
                $unset[] = 'authentication-types';
            } else {
                $setRequest->setargument('authentication-types', $this->authentication_types);
            }
        }
        if ($this->encryption != $c->encryption) {
            if ($this->encryption == '') {
                $unset[] = 'encryption';
            } else {
                $setRequest->setargument('encryption', $this->encryption);
            }
        }
        if ($this->group_encryption != $c->group_encryption) {
            if ($this->group_encryption == '') {
                $unset[] = 'group-encryption';
            } else {
                $setRequest->setargument('group-encryption', $this->group_encryption);
            }
        }
        if ($this->passphrase != $c->passphrase) {
            if ($this->passphrase == '') {
                $unset[] = 'passphrase';
            } else {
                $setRequest->setargument('passphrase', $this->passphrase);
            }
        }
        if ($this->eap_methods != $c->eap_methods) {
            if ($this->eap_methods == '') {
                $unset[] = 'eap-methods';
            } else {
                $setRequest->setargument('eap-methods', $this->eap_methods);
            }
        }
        if ($this->tls_mode != $c->tls_mode) {
            $setRequest->setargument('tls-mode', $this->tls_mode);
        }
        if ($this->tls_certificate != $c->tls_certificate) {
            $setRequest->setargument('tls-certificate', $this->tls_certificate);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/caps-man/security/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('/caps-man/security/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/security/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->authentication_types != "") {
            $setRequest->setargument('authentication-types', $this->authentication_types);
        }
        if ($this->encryption != "") {
            $setRequest->setargument('encryption', $this->encryption);
        }
        if ($this->group_encryption != "") {
            $setRequest->setargument('group-encryption', $this->group_encryption);
        }
        if ($this->passphrase != "") {
            $setRequest->setargument('passphrase', $this->passphrase);
        }
        if ($this->eap_methods != "") {
            $setRequest->setargument('eap-methods', $this->eap_methods);
        }
        if ($this->tls_mode != "") {
            $setRequest->setargument('tls-mode', $this->tls_mode);
        }
        if ($this->tls_certificate != "") {
            $setRequest->setargument('tls-certificate', $this->tls_certificate);
        }
        $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('/caps-man/security');
        $util->remove(RouterOS\Query::where('.id', $id));
        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;
    }

}
