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

    public $id;
    public $src_address;
    public $src_port;
    public $dst_address;
    public $dst_port;
    public $protocol;
    public $action;
    public $template;
    public $level;
    public $ipsec_protocols;
    public $tunnel;
    public $sa_src_address;
    public $sa_dst_address;
    public $proposal;
    public $priority;
    public $dynamic;
    public $inactive;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['src_address', 'string'],
            ['src_port', 'string'],
            ['dst_address', 'string'],
            ['dst_port', 'string'],
            ['protocol', 'string'],
            ['action', 'string'],
            ['template', 'string'],
            ['level', 'string'],
            ['ipsec_protocols', 'string'],
            ['tunnel', 'string'],
            ['sa_src_address', 'string'],
            ['sa_dst_address', 'string'],
            ['proposal', 'string'],
            ['priority', 'string'],
            ['dynamic', 'string'],
            ['inactive', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/policy/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Policies();
                $c->id = $response->getProperty('.id');
                $c->src_address = $response->getProperty('src-address');
                $c->src_port = $response->getProperty('src-port');
                $c->dst_address = $response->getProperty('dst-address');
                $c->dst_port = $response->getProperty('dst-port');
                $c->protocol = $response->getProperty('protocol');
                $c->action = $response->getProperty('action');
                $c->template = $response->getProperty('template');
                $c->level = $response->getProperty('level');
                $c->ipsec_protocols = $response->getProperty('ipsec-protocols');
                $c->tunnel = $response->getProperty('tunnel');
                $c->sa_src_address = $response->getProperty('sa-src-address');
                $c->sa_dst_address = $response->getProperty('sa-dst-address');
                $c->proposal = $response->getProperty('proposal');
                $c->priority = $response->getProperty('priority');
                $c->dynamic = $response->getProperty('dynamic');
                $c->inactive = $response->getProperty('inactive');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/policy/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->src_address = $response->getProperty('src-address');
                $this->src_port = $response->getProperty('src-port');
                $this->dst_address = $response->getProperty('dst-address');
                $this->dst_port = $response->getProperty('dst-port');
                $this->protocol = $response->getProperty('protocol');
                $this->action = $response->getProperty('action');
                $this->template = (($response->getProperty('template') == 'true') ? true : false);
                $this->level = $response->getProperty('level');
                $this->ipsec_protocols = $response->getProperty('ipsec-protocols');
                $this->tunnel = (($response->getProperty('tunnel') == 'true') ? true : false);
                $this->sa_src_address = $response->getProperty('sa-src-address');
                $this->sa_dst_address = $response->getProperty('sa-dst-address');
                $this->proposal = $response->getProperty('proposal');
                $this->priority = $response->getProperty('priority');
                $this->dynamic = (($response->getProperty('dynamic') == 'true') ? true : false);
                $this->inactive = (($response->getProperty('inactive') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Policies();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/ipsec/policy/set');
        $setRequest->setargument('.id', $id);
        if ($this->src_address != $c->src_address) {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->src_port != $c->src_port) {
            $setRequest->setargument('src-port', $this->src_port);
        }
        if ($this->dst_address != $c->dst_address) {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->dst_port != $c->dst_port) {
            $setRequest->setargument('dst-port', $this->dst_port);
        }
        if ($this->protocol != $c->protocol) {
            $setRequest->setargument('protocol', $this->protocol);
        }
        if ($this->action != $c->action) {
            $setRequest->setargument('action', $this->action);
        }
        $setRequest->setargument('template', (($this->template == true) ? "true" : "false"));
        if ($this->level != $c->level) {
            $setRequest->setargument('level', $this->level);
        }
        if ($this->ipsec_protocols != $c->ipsec_protocols) {
            $setRequest->setargument('ipsec-protocols', $this->ipsec_protocols);
        }
        $setRequest->setargument('tunnel', (($this->tunnel == true) ? "true" : "false"));
        if ($this->sa_src_address != $c->sa_src_address) {
            $setRequest->setargument('sa-src-address', $this->sa_src_address);
        }
        if ($this->sa_dst_address != $c->sa_dst_address) {
            $setRequest->setargument('sa-dst-address', $this->sa_dst_address);
        }
        if ($this->proposal != $c->proposal) {
            $setRequest->setargument('proposal', $this->proposal);
        }
        if ($this->priority != $c->priority) {
            $setRequest->setargument('priority', $this->priority);
        }

        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/ip/ipsec/policy/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/policy/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/ipsec/policy/add');
        if ($this->src_address != "") {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->src_port != "") {
            $setRequest->setargument('src-port', $this->src_port);
        }
        if ($this->dst_address != "") {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->dst_port != "") {
            $setRequest->setargument('dst-port', $this->dst_port);
        }
        if ($this->protocol != "") {
            $setRequest->setargument('protocol', $this->protocol);
        }
        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->template != "") {
            $setRequest->setargument('template', (($this->template == true) ? "true" : "false"));
        }
        if ($this->level != "") {
            $setRequest->setargument('level', $this->level);
        }
        if ($this->ipsec_protocols != "") {
            $setRequest->setargument('ipsec-protocols', $this->ipsec_protocols);
        }
        if ($this->tunnel != "") {
            $setRequest->setargument('tunnel', (($this->tunnel == true) ? "true" : "false"));
        }
        if ($this->sa_src_address != "") {
            $setRequest->setargument('sa-src-address', $this->sa_src_address);
        }
        if ($this->sa_dst_address != "") {
            $setRequest->setargument('sa-dst-address', $this->sa_dst_address);
        }
        if ($this->proposal != "") {
            $setRequest->setargument('proposal', $this->proposal);
        }
        if ($this->priority != "") {
            $setRequest->setargument('priority', $this->priority);
        }

        $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/policy');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

    public function getProposal() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/proposal/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;
    }

}
