<?php

namespace app\models\mikrotik\wireless;

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

    public $id;
    public $name;
    public $mode;
    public $authentication_types;
    public $unicast_ciphers;
    public $group_ciphers;
    public $wpa_pre_shared_key;
    public $wpa2_pre_shared_key;
    public $supplicant_identity;
    public $eap_methods;
    public $tls_mode;
    public $tls_certificate;
    public $mschapv2_username;
    public $mschapv2_password;
    public $static_algo_0;
    public $static_key_0;
    public $static_algo_1;
    public $static_key_1;
    public $static_algo_2;
    public $static_key_2;
    public $static_algo_3;
    public $static_key_3;
    public $static_transmit_key;
    public $static_sta_private_algo;
    public $static_sta_private_key;
    public $radius_mac_authentication;
    public $radius_mac_accounting;
    public $radius_eap_accounting;
    public $interim_update;
    public $radius_mac_format;
    public $radius_mac_mode;
    public $radius_mac_caching;
    public $group_key_update;
    public $management_protection;
    public $management_protection_key;
    public $default;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['mode', 'string'],
            ['wpa_pre_shared_key', 'string'],
            ['wpa2_pre_shared_key', 'string'],
            ['supplicant_identity', 'string'],
            ['eap_methods', 'string'],
            ['tls_mode', 'string'],
            ['tls_certificate', 'string'],
            ['mschapv2_username', 'string'],
            ['mschapv2_password', 'string'],
            ['static_algo_0', 'string'],
            ['static_key_0', 'string'],
            ['static_algo_1', 'string'],
            ['static_key_1', 'string'],
            ['static_algo_2', 'string'],
            ['static_key_2', 'string'],
            ['static_algo_3', 'string'],
            ['static_key_3', 'string'],
            ['static_transmit_key', 'string'],
            ['static_sta_private_algo', 'string'],
            ['static_sta_private_key', 'string'],
            ['radius_mac_authentication', 'string'],
            ['radius_mac_accounting', 'string'],
            ['radius_eap_accounting', 'string'],
            ['interim_update', 'string'],
            ['radius_mac_format', 'string'],
            ['radius_mac_mode', 'string'],
            ['radius_mac_caching', 'string'],
            ['group_key_update', 'string'],
            ['management_protection', 'string'],
            ['management_protection_key', 'string'],
            ['default', '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 getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/wireless/security-profiles/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Securityprofiles();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->mode = $response->getProperty('mode');
                $c->authentication_types = $response->getProperty('authentication-types');
                $c->unicast_ciphers = $response->getProperty('unicast-ciphers');
                $c->group_ciphers = $response->getProperty('group-ciphers');
                $c->wpa_pre_shared_key = $response->getProperty('wpa-pre-shared-key');
                $c->wpa2_pre_shared_key = $response->getProperty('wpa2-pre-shared-key');
                $c->supplicant_identity = $response->getProperty('supplicant-identity');
                $c->eap_methods = $response->getProperty('eap-methods');
                $c->tls_mode = $response->getProperty('tls-mode');
                $c->tls_certificate = $response->getProperty('tls-certificate');
                $c->mschapv2_username = $response->getProperty('mschapv2-username');
                $c->mschapv2_password = $response->getProperty('mschapv2-password');
                $c->static_algo_0 = $response->getProperty('static-algo-0');
                $c->static_key_0 = $response->getProperty('static-key-0');
                $c->static_algo_1 = $response->getProperty('static-algo-1');
                $c->static_key_1 = $response->getProperty('static-key-1');
                $c->static_algo_2 = $response->getProperty('static-algo-2');
                $c->static_key_2 = $response->getProperty('static-key-2');
                $c->static_algo_3 = $response->getProperty('static-algo-3');
                $c->static_key_3 = $response->getProperty('static-key-3');
                $c->static_transmit_key = $response->getProperty('static-transmit-key');
                $c->static_sta_private_algo = $response->getProperty('static-sta-private-algo');
                $c->static_sta_private_key = $response->getProperty('static-sta-private-key');
                $c->radius_mac_authentication = $response->getProperty('radius-mac-authentication');
                $c->radius_mac_accounting = $response->getProperty('radius-mac-accounting');
                $c->radius_eap_accounting = $response->getProperty('radius-eap-accounting');
                $c->interim_update = $response->getProperty('interim-update');
                $c->radius_mac_format = $response->getProperty('radius-mac-format');
                $c->radius_mac_mode = $response->getProperty('radius-mac-mode');
                $c->radius_mac_caching = $response->getProperty('radius-mac-caching');
                $c->group_key_update = $response->getProperty('group-key-update');
                $c->management_protection = $response->getProperty('management-protection');
                $c->management_protection_key = $response->getProperty('management-protection-key');
                $c->default = $response->getProperty('default');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/wireless/security-profiles/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->mode = $response->getProperty('mode');
                $this->authentication_types = $response->getProperty('authentication-types');
                $this->unicast_ciphers = $response->getProperty('unicast-ciphers');
                $this->group_ciphers = $response->getProperty('group-ciphers');
                $this->wpa_pre_shared_key = $response->getProperty('wpa-pre-shared-key');
                $this->wpa2_pre_shared_key = $response->getProperty('wpa2-pre-shared-key');
                $this->supplicant_identity = $response->getProperty('supplicant-identity');
                $this->eap_methods = $response->getProperty('eap-methods');
                $this->tls_mode = $response->getProperty('tls-mode');
                $this->tls_certificate = $response->getProperty('tls-certificate');
                $this->mschapv2_username = $response->getProperty('mschapv2-username');
                $this->mschapv2_password = $response->getProperty('mschapv2-password');
                $this->static_algo_0 = $response->getProperty('static-algo-0');
                $this->static_key_0 = $response->getProperty('static-key-0');
                $this->static_algo_1 = $response->getProperty('static-algo-1');
                $this->static_key_1 = $response->getProperty('static-key-1');
                $this->static_algo_2 = $response->getProperty('static-algo-2');
                $this->static_key_2 = $response->getProperty('static-key-2');
                $this->static_algo_3 = $response->getProperty('static-algo-3');
                $this->static_key_3 = $response->getProperty('static-key-3');
                $this->static_transmit_key = $response->getProperty('static-transmit-key');
                $this->static_sta_private_algo = $response->getProperty('static-sta-private-algo');
                $this->static_sta_private_key = $response->getProperty('static-sta-private-key');
                $this->radius_mac_authentication = (($response->getProperty('radius-mac-authentication') == 'true') ? true : false);
                $this->radius_mac_accounting = (($response->getProperty('radius-mac-accounting') == 'true') ? true : false);
                $this->radius_eap_accounting = (($response->getProperty('radius-eap-accounting') == 'true') ? true : false);
                $this->interim_update = $response->getProperty('interim-update');
                $this->radius_mac_format = $response->getProperty('radius-mac-format');
                $this->radius_mac_mode = $response->getProperty('radius-mac-mode');
                $this->radius_mac_caching = $response->getProperty('radius-mac-caching');
                $this->group_key_update = $response->getProperty('group-key-update');
                $this->management_protection = $response->getProperty('management-protection');
                $this->management_protection_key = $response->getProperty('management-protection-key');
                $this->default = (($response->getProperty('default') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Securityprofiles();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/wireless/security-profiles/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mode != $c->mode) {
            $setRequest->setargument('mode', $this->mode);
        }
        if ($this->authentication_types != $c->authentication_types) {
            $setRequest->setargument('authentication-types', $this->authentication_types);
        }
        if ($this->unicast_ciphers != $c->unicast_ciphers) {
            $setRequest->setargument('unicast-ciphers', $this->unicast_ciphers);
        }
        if ($this->group_ciphers != $c->group_ciphers) {
            $setRequest->setargument('group-ciphers', $this->group_ciphers);
        }
        if ($this->wpa_pre_shared_key != $c->wpa_pre_shared_key) {
            $setRequest->setargument('wpa-pre-shared-key', $this->wpa_pre_shared_key);
        }
        if ($this->wpa2_pre_shared_key != $c->wpa2_pre_shared_key) {
            $setRequest->setargument('wpa2-pre-shared-key', $this->wpa2_pre_shared_key);
        }
        if ($this->supplicant_identity != $c->supplicant_identity) {
            $setRequest->setargument('supplicant-identity', $this->supplicant_identity);
        }
        if ($this->eap_methods != $c->eap_methods) {
            $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 ($this->mschapv2_username != $c->mschapv2_username) {
            $setRequest->setargument('mschapv2-username', $this->mschapv2_username);
        }
        if ($this->mschapv2_password != $c->mschapv2_password) {
            $setRequest->setargument('mschapv2-password', $this->mschapv2_password);
        }
        if ($this->static_algo_0 != $c->static_algo_0) {
            $setRequest->setargument('static-algo-0', $this->static_algo_0);
        }
        if ($this->static_key_0 != $c->static_key_0) {
            $setRequest->setargument('static-key-0', $this->static_key_0);
        }
        if ($this->static_algo_1 != $c->static_algo_1) {
            $setRequest->setargument('static-algo-1', $this->static_algo_1);
        }
        if ($this->static_key_1 != $c->static_key_1) {
            $setRequest->setargument('static-key-1', $this->static_key_1);
        }
        if ($this->static_algo_2 != $c->static_algo_2) {
            $setRequest->setargument('static-algo-2', $this->static_algo_2);
        }
        if ($this->static_key_2 != $c->static_key_2) {
            $setRequest->setargument('static-key-2', $this->static_key_2);
        }
        if ($this->static_algo_3 != $c->static_algo_3) {
            $setRequest->setargument('static-algo-3', $this->static_algo_3);
        }
        if ($this->static_key_3 != $c->static_key_3) {
            $setRequest->setargument('static-key-3', $this->static_key_3);
        }
        if ($this->static_transmit_key != $c->static_transmit_key) {
            $setRequest->setargument('static-transmit-key', $this->static_transmit_key);
        }
        if ($this->static_sta_private_algo != $c->static_sta_private_algo) {
            $setRequest->setargument('static-sta-private-algo', $this->static_sta_private_algo);
        }
        if ($this->static_sta_private_key != $c->static_sta_private_key) {
            $setRequest->setargument('static-sta-private-key', $this->static_sta_private_key);
        }
        $setRequest->setargument('radius-mac-authentication', (($this->radius_mac_authentication == true) ? "true" : "false"));
        $setRequest->setargument('radius-mac-accounting', (($this->radius_mac_accounting == true) ? "true" : "false"));
        $setRequest->setargument('radius-eap-accounting', (($this->radius_eap_accounting == true) ? "true" : "false"));
        if ($this->interim_update != $c->interim_update) {
            $setRequest->setargument('interim-update', $this->interim_update);
        }
        if ($this->radius_mac_format != $c->radius_mac_format) {
            $setRequest->setargument('radius-mac-format', $this->radius_mac_format);
        }
        if ($this->radius_mac_mode != $c->radius_mac_mode) {
            $setRequest->setargument('radius-mac-mode', $this->radius_mac_mode);
        }
        if ($this->radius_mac_caching != $c->radius_mac_caching) {
            $setRequest->setargument('radius-mac-caching', $this->radius_mac_caching);
        }
        if ($this->group_key_update != $c->group_key_update) {
            $setRequest->setargument('group-key-update', $this->group_key_update);
        }
        if ($this->management_protection != $c->management_protection) {
            $setRequest->setargument('management-protection', $this->management_protection);
        }
        if ($this->management_protection_key != $c->management_protection_key) {
            $setRequest->setargument('management-protection-key', $this->management_protection_key);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/wireless/security-profiles/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/wireless/security-profiles/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/wireless/security-profiles/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mode != "") {
            $setRequest->setargument('mode', $this->mode);
        }
        if ($this->authentication_types != "") {
            $setRequest->setargument('authentication-types', $this->authentication_types);
        }
        if ($this->unicast_ciphers != "") {
            $setRequest->setargument('unicast-ciphers', $this->unicast_ciphers);
        }
        if ($this->group_ciphers != "") {
            $setRequest->setargument('group-ciphers', $this->group_ciphers);
        }
        if ($this->wpa_pre_shared_key != "") {
            $setRequest->setargument('wpa-pre-shared-key', $this->wpa_pre_shared_key);
        }
        if ($this->wpa2_pre_shared_key != "") {
            $setRequest->setargument('wpa2-pre-shared-key', $this->wpa2_pre_shared_key);
        }
        if ($this->supplicant_identity != "") {
            $setRequest->setargument('supplicant-identity', $this->supplicant_identity);
        }
        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);
        }
        if ($this->mschapv2_username != "") {
            $setRequest->setargument('mschapv2-username', $this->mschapv2_username);
        }
        if ($this->mschapv2_password != "") {
            $setRequest->setargument('mschapv2-password', $this->mschapv2_password);
        }
        if ($this->static_algo_0 != "") {
            $setRequest->setargument('static-algo-0', $this->static_algo_0);
        }
        if ($this->static_key_0 != "") {
            $setRequest->setargument('static-key-0', $this->static_key_0);
        }
        if ($this->static_algo_1 != "") {
            $setRequest->setargument('static-algo-1', $this->static_algo_1);
        }
        if ($this->static_key_1 != "") {
            $setRequest->setargument('static-key-1', $this->static_key_1);
        }
        if ($this->static_algo_2 != "") {
            $setRequest->setargument('static-algo-2', $this->static_algo_2);
        }
        if ($this->static_key_2 != "") {
            $setRequest->setargument('static-key-2', $this->static_key_2);
        }
        if ($this->static_algo_3 != "") {
            $setRequest->setargument('static-algo-3', $this->static_algo_3);
        }
        if ($this->static_key_3 != "") {
            $setRequest->setargument('static-key-3', $this->static_key_3);
        }
        if ($this->static_transmit_key != "") {
            $setRequest->setargument('static-transmit-key', $this->static_transmit_key);
        }
        if ($this->static_sta_private_algo != "") {
            $setRequest->setargument('static-sta-private-algo', $this->static_sta_private_algo);
        }
        if ($this->static_sta_private_key != "") {
            $setRequest->setargument('static-sta-private-key', $this->static_sta_private_key);
        }
        if ($this->radius_mac_authentication != "") {
            $setRequest->setargument('radius-mac-authentication', (($this->radius_mac_authentication == true) ? "true" : "false"));
        }
        if ($this->radius_mac_accounting != "") {
            $setRequest->setargument('radius-mac-accounting', (($this->radius_mac_accounting == true) ? "true" : "false"));
        }
        if ($this->radius_eap_accounting != "") {
            $setRequest->setargument('radius-eap-accounting', (($this->radius_eap_accounting == true) ? "true" : "false"));
        }
        if ($this->interim_update != "") {
            $setRequest->setargument('interim-update', $this->interim_update);
        }
        if ($this->radius_mac_format != "") {
            $setRequest->setargument('radius-mac-format', $this->radius_mac_format);
        }
        if ($this->radius_mac_mode != "") {
            $setRequest->setargument('radius-mac-mode', $this->radius_mac_mode);
        }
        if ($this->radius_mac_caching != "") {
            $setRequest->setargument('radius-mac-caching', $this->radius_mac_caching);
        }
        if ($this->group_key_update != "") {
            $setRequest->setargument('group-key-update', $this->group_key_update);
        }
        if ($this->management_protection != "") {
            $setRequest->setargument('management-protection', $this->management_protection);
        }
        if ($this->management_protection_key != "") {
            $setRequest->setargument('management-protection-key', $this->management_protection_key);
        }
        $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/wireless/security-profiles');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
