<?php

namespace app\models\mikrotik\ip\ipsec;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Peers extends Model {

    public $id;
    public $address;
    public $local_address;
    public $passive;
    public $port;
    public $auth_method;
    public $remote_certificate;
    public $xauth_login;
    public $xauth_password;
    public $generate_policy;
    public $policy_template_group;
    public $exchange_mode;
    public $mode_config;
    public $send_initial_contact;
    public $nat_traversal;
    public $my_id;
    public $proposal_check;
    public $hash_algorithm;
    public $enc_algorithm;
    public $dh_group;
    public $lifetime;
    public $lifebytes;
    public $dpd_interval;
    public $dpd_maximum_failures;
    public $dynamic;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['address', 'string'],
            ['local_address', 'string'],
            ['passive', 'string'],
            ['port', 'string'],
            ['auth_method', 'string'],
            ['remote_certificate', 'string'],
            ['xauth_login', 'string'],
            ['xauth_password', 'string'],
            ['generate_policy', 'string'],
            ['policy_template_group', 'string'],
            ['exchange_mode', 'string'],
            ['mode_config', 'string'],
            ['send_initial_contact', 'string'],
            ['nat_traversal', 'string'],
            ['my_id', 'string'],
            ['proposal_check', 'string'],
            ['hash_algorithm', 'string'],
            ['lifetime', 'string'],
            ['lifebytes', 'string'],
            ['dpd_interval', 'string'],
            ['dpd_maximum_failures', 'string'],
            ['dynamic', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/peer/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Peers();
                $c->id = $response->getProperty('.id');
                $c->address = $response->getProperty('address');
                $c->local_address = $response->getProperty('local-address');
                $c->passive = $response->getProperty('passive');
                $c->port = $response->getProperty('port');
                $c->auth_method = $response->getProperty('auth-method');
                $c->remote_certificate = $response->getProperty('remote-certificate');
                $c->xauth_login = $response->getProperty('xauth-login');
                $c->xauth_password = $response->getProperty('xauth-password');
                $c->generate_policy = $response->getProperty('generate-policy');
                $c->policy_template_group = $response->getProperty('policy-template-group');
                $c->exchange_mode = $response->getProperty('exchange-mode');
                $c->mode_config = $response->getProperty('mode-config');
                $c->send_initial_contact = $response->getProperty('send-initial-contact');
                $c->nat_traversal = $response->getProperty('nat-traversal');
                $c->my_id = $response->getProperty('my-id');
                $c->proposal_check = $response->getProperty('proposal-check');
                $c->hash_algorithm = $response->getProperty('hash-algorithm');
                $c->enc_algorithm = $response->getProperty('enc-algorithm');
                $c->dh_group = $response->getProperty('dh-group');
                $c->lifetime = $response->getProperty('lifetime');
                $c->lifebytes = $response->getProperty('lifebytes');
                $c->dpd_interval = $response->getProperty('dpd-interval');
                $c->dpd_maximum_failures = $response->getProperty('dpd-maximum-failures');
                $c->dynamic = $response->getProperty('dynamic');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/peer/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->address = $response->getProperty('address');
                $this->local_address = $response->getProperty('local-address');
                $this->passive = (($response->getProperty('passive') == 'true') ? true : false);
                $this->port = $response->getProperty('port');
                $this->auth_method = $response->getProperty('auth-method');
                $this->remote_certificate = $response->getProperty('remote-certificate');
                $this->xauth_login = $response->getProperty('xauth-login');
                $this->xauth_password = $response->getProperty('xauth-password');
                $this->generate_policy = $response->getProperty('generate-policy');
                $this->policy_template_group = $response->getProperty('policy-template-group');
                $this->exchange_mode = $response->getProperty('exchange-mode');
                $this->mode_config = $response->getProperty('mode-config');
                $this->send_initial_contact = (($response->getProperty('send-initial-contact') == 'true') ? true : false);
                $this->nat_traversal = (($response->getProperty('nat-traversal') == 'true') ? true : false);
                $this->my_id = $response->getProperty('my-id');
                $this->proposal_check = $response->getProperty('proposal-check');
                $this->hash_algorithm = $response->getProperty('hash-algorithm');
                $this->enc_algorithm = $response->getProperty('enc-algorithm');
                $this->dh_group = $response->getProperty('dh-group');
                $this->lifetime = $response->getProperty('lifetime');
                $this->lifebytes = $response->getProperty('lifebytes');
                $this->dpd_interval = $response->getProperty('dpd-interval');
                $this->dpd_maximum_failures = $response->getProperty('dpd-maximum-failures');
                $this->dynamic = (($response->getProperty('dynamic') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Peers();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/ipsec/peer/set');
        $setRequest->setargument('.id', $id);
        if ($this->address != $c->address) {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->local_address != $c->local_address) {
            $setRequest->setargument('local-address', $this->local_address);
        }
        $setRequest->setargument('passive', (($this->passive == true) ? "true" : "false"));
        if ($this->port != $c->port) {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->auth_method != $c->auth_method) {
            $setRequest->setargument('auth-method', $this->auth_method);
        }
        if ($this->remote_certificate != $c->remote_certificate) {
            $setRequest->setargument('remote-certificate', $this->remote_certificate);
        }
        if ($this->xauth_login != $c->xauth_login) {
            $setRequest->setargument('xauth-login', $this->xauth_login);
        }
        if ($this->xauth_password != $c->xauth_password) {
            $setRequest->setargument('xauth-password', $this->xauth_password);
        }
        if ($this->generate_policy != $c->generate_policy) {
            $setRequest->setargument('generate-policy', $this->generate_policy);
        }
        if ($this->policy_template_group != $c->policy_template_group) {
            $setRequest->setargument('policy-template-group', $this->policy_template_group);
        }
        if ($this->exchange_mode != $c->exchange_mode) {
            $setRequest->setargument('exchange-mode', $this->exchange_mode);
        }
        if ($this->mode_config != $c->mode_config) {
            $setRequest->setargument('mode-config', $this->mode_config);
        }
        $setRequest->setargument('send-initial-contact', (($this->send_initial_contact == true) ? "true" : "false"));
        $setRequest->setargument('nat-traversal', (($this->nat_traversal == true) ? "true" : "false"));
        if ($this->my_id != $c->my_id) {
            $setRequest->setargument('my-id', $this->my_id);
        }
        if ($this->proposal_check != $c->proposal_check) {
            $setRequest->setargument('proposal-check', $this->proposal_check);
        }
        if ($this->hash_algorithm != $c->hash_algorithm) {
            $setRequest->setargument('hash-algorithm', $this->hash_algorithm);
        }
        if ($this->enc_algorithm != $c->enc_algorithm) {
            $setRequest->setargument('enc-algorithm', $this->enc_algorithm);
        }
        if ($this->dh_group != $c->dh_group) {
            $setRequest->setargument('dh-group', $this->dh_group);
        }
        if ($this->lifetime != $c->lifetime) {
            $setRequest->setargument('lifetime', $this->lifetime);
        }
        if ($this->lifebytes != $c->lifebytes) {
            $setRequest->setargument('lifebytes', $this->lifebytes);
        }
        if ($this->dpd_interval != $c->dpd_interval) {
            $setRequest->setargument('dpd-interval', $this->dpd_interval);
        }
        if ($this->dpd_maximum_failures != $c->dpd_maximum_failures) {
            $setRequest->setargument('dpd-maximum-failures', $this->dpd_maximum_failures);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/ip/ipsec/peer/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('/ip/ipsec/peer/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/ipsec/peer/add');
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->local_address != "") {
            $setRequest->setargument('local-address', $this->local_address);
        }
        if ($this->passive != "") {
            $setRequest->setargument('passive', (($this->passive == true) ? "true" : "false"));
        }
        if ($this->port != "") {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->auth_method != "") {
            $setRequest->setargument('auth-method', $this->auth_method);
        }
        if ($this->remote_certificate != "") {
            $setRequest->setargument('remote-certificate', $this->remote_certificate);
        }
        if ($this->xauth_login != "") {
            $setRequest->setargument('xauth-login', $this->xauth_login);
        }
        if ($this->xauth_password != "") {
            $setRequest->setargument('xauth-password', $this->xauth_password);
        }
        if ($this->generate_policy != "") {
            $setRequest->setargument('generate-policy', $this->generate_policy);
        }
        if ($this->policy_template_group != "") {
            $setRequest->setargument('policy-template-group', $this->policy_template_group);
        }
        if ($this->exchange_mode != "") {
            $setRequest->setargument('exchange-mode', $this->exchange_mode);
        }
        if ($this->mode_config != "") {
            $setRequest->setargument('mode-config', $this->mode_config);
        }
        if ($this->send_initial_contact != "") {
            $setRequest->setargument('send-initial-contact', (($this->send_initial_contact == true) ? "true" : "false"));
        }
        if ($this->nat_traversal != "") {
            $setRequest->setargument('nat-traversal', (($this->nat_traversal == true) ? "true" : "false"));
        }
        if ($this->my_id != "") {
            $setRequest->setargument('my-id', $this->my_id);
        }
        if ($this->proposal_check != "") {
            $setRequest->setargument('proposal-check', $this->proposal_check);
        }
        if ($this->hash_algorithm != "") {
            $setRequest->setargument('hash-algorithm', $this->hash_algorithm);
        }
        if ($this->enc_algorithm != "") {
            $setRequest->setargument('enc-algorithm', $this->enc_algorithm);
        }
        if ($this->dh_group != "") {
            $setRequest->setargument('dh-group', $this->dh_group);
        }
        if ($this->lifetime != "") {
            $setRequest->setargument('lifetime', $this->lifetime);
        }
        if ($this->lifebytes != "") {
            $setRequest->setargument('lifebytes', $this->lifebytes);
        }
        if ($this->dpd_interval != "") {
            $setRequest->setargument('dpd-interval', $this->dpd_interval);
        }
        if ($this->dpd_maximum_failures != "") {
            $setRequest->setargument('dpd-maximum-failures', $this->dpd_maximum_failures);
        }
        $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('/ip/ipsec/peer');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

    public function getPolicy() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/policy/group/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 getMode() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/mode-config/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 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;
    }

}
