<?php

namespace app\models\mikrotik\ip\firewall;

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

    public $id;
    public $action;
    public $address_list;
    public $address_list_timeout;
    public $chain;
    public $comment;
    public $connection_bytes;
    public $connection_limit;
    public $connection_mark;
    public $connection_rate;
    public $connection_type;
    public $content;
    public $dscp;
    public $dst_address;
    public $dst_address_list;
    public $dst_address_type;
    public $dst_limit;
    public $dst_port;
    public $fragment;
    public $hotspot;
    public $icmp_options;
    public $in_bridge_port;
    public $in_interface;
    public $ingress_priority;
    public $ipsec_policy;
    public $ipv4_options;
    public $jump_target;
    public $layer7_protocol;
    public $limit;
    public $log_prefix;
    public $nth;
    public $out_bridge_port;
    public $out_interface;
    public $packet_mark;
    public $packet_size;
    public $per_connection_classifier;
    public $port;
    public $protocol;
    public $psd;
    public $random;
    public $routing_mark;
    public $same_not_by_dst;
    public $src_address;
    public $src_address_list;
    public $src_address_type;
    public $src_port;
    public $src_mac_address;
    public $tcp_flags;
    public $tcp_mss;
    public $time;
    public $to_addresses;
    public $to_ports;
    public $ttl;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['action', 'string'],
            ['address_list', 'string'],
            ['address_list_timeout', 'string'],
            ['chain', 'string'],
            ['comment', 'string'],
            ['connection_bytes', 'string'],
            ['connection_limit', 'string'],
            ['connection_mark', 'string'],
            ['connection_rate', 'string'],
            ['connection_type', 'string'],
            ['content', 'string'],
            ['dscp', 'string'],
            ['dst_address', 'string'],
            ['dst_address_list', 'string'],
            ['dst_address_type', 'string'],
            ['dst_limit', 'string'],
            ['dst_port', 'string'],
            ['fragment', 'string'],
            ['hotspot', 'string'],
            ['icmp_options', 'string'],
            ['in_bridge_port', 'string'],
            ['in_interface', 'string'],
            ['ingress_priority', 'string'],
            ['ipsec_policy', 'string'],
            ['ipv4_options', 'string'],
            ['jump_target', 'string'],
            ['layer7_protocol', 'string'],
            ['limit', 'string'],
            ['log_prefix', 'string'],
            ['nth', 'string'],
            ['out_bridge_port', 'string'],
            ['out_interface', 'string'],
            ['packet_mark', 'string'],
            ['packet_size', 'string'],
            ['per_connection_classifier', 'string'],
            ['port', 'string'],
            ['protocol', 'string'],
            ['psd', 'string'],
            ['random', 'string'],
            ['routing_mark', 'string'],
            ['same_not_by_dst', 'string'],
            ['src_address', 'string'],
            ['src_address_list', 'string'],
            ['src_address_type', 'string'],
            ['src_port', 'string'],
            ['src_mac_address', 'string'],
            ['tcp_flags', 'string'],
            ['tcp_mss', 'string'],
            ['time', 'string'],
            ['to_addresses', 'string'],
            ['to_ports', 'string'],
            ['ttl', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/firewall/nat/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Nat();
                $c->id = $response->getProperty('.id');
                $c->action = $response->getProperty('action');
                $c->address_list = $response->getProperty('address-list');
                $c->address_list_timeout = $response->getProperty('address-list-timeout');
                $c->chain = $response->getProperty('chain');
                $c->comment = $response->getProperty('comment');
                $c->connection_bytes = $response->getProperty('connection-bytes');
                $c->connection_limit = $response->getProperty('connection-limit');
                $c->connection_mark = $response->getProperty('connection-mark');
                $c->connection_rate = $response->getProperty('connection-rate');
                $c->connection_type = $response->getProperty('connection-type');
                $c->content = $response->getProperty('content');
                $c->dscp = $response->getProperty('dscp');
                $c->dst_address = $response->getProperty('dst-address');
                $c->dst_address_list = $response->getProperty('dst-address-list');
                $c->dst_address_type = $response->getProperty('dst-address-type');
                $c->dst_limit = $response->getProperty('dst-limit');
                $c->dst_port = $response->getProperty('dst-port');
                $c->fragment = $response->getProperty('fragment');
                $c->hotspot = $response->getProperty('hotspot');
                $c->icmp_options = $response->getProperty('icmp-options');
                $c->in_bridge_port = $response->getProperty('in-bridge-port');
                $c->in_interface = $response->getProperty('in-interface');
                $c->ingress_priority = $response->getProperty('ingress-priority');
                $c->ipsec_policy = $response->getProperty('ipsec-policy');
                $c->ipv4_options = $response->getProperty('ipv4-options');
                $c->jump_target = $response->getProperty('jump-target');
                $c->layer7_protocol = $response->getProperty('layer7-protocol');
                $c->limit = $response->getProperty('limit');
                $c->log_prefix = $response->getProperty('log-prefix');
                $c->nth = $response->getProperty('nth');
                $c->out_bridge_port = $response->getProperty('out-bridge-port');
                $c->out_interface = $response->getProperty('out-interface');
                $c->packet_mark = $response->getProperty('packet-mark');
                $c->packet_size = $response->getProperty('packet-size');
                $c->per_connection_classifier = $response->getProperty('per-connection-classifier');
                $c->port = $response->getProperty('port');
                $c->protocol = $response->getProperty('protocol');
                $c->psd = $response->getProperty('psd');
                $c->random = $response->getProperty('random');
                $c->routing_mark = $response->getProperty('routing-mark');
                $c->same_not_by_dst = $response->getProperty('same-not-by-dst');
                $c->src_address = $response->getProperty('src-address');
                $c->src_address_list = $response->getProperty('src-address-list');
                $c->src_address_type = $response->getProperty('src-address-type');
                $c->src_port = $response->getProperty('src-port');
                $c->src_mac_address = $response->getProperty('src-mac-address');
                $c->tcp_flags = $response->getProperty('tcp-flags');
                $c->tcp_mss = $response->getProperty('tcp-mss');
                $c->time = $response->getProperty('time');
                $c->to_addresses = $response->getProperty('to-addresses');
                $c->to_ports = $response->getProperty('to-ports');
                $c->ttl = $response->getProperty('ttl');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/firewall/nat/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $this->id = $id;
                $this->action = $response->getProperty('action');
                $this->address_list = $response->getProperty('address-list');
                $this->address_list_timeout = $response->getProperty('address-list-timeout');
                $this->chain = $response->getProperty('chain');
                $this->comment = $response->getProperty('comment');
                $this->connection_bytes = $response->getProperty('connection-bytes');
                $this->connection_limit = $response->getProperty('connection-limit');
                $this->connection_mark = $response->getProperty('connection-mark');
                $this->connection_rate = $response->getProperty('connection-rate');
                $this->connection_type = $response->getProperty('connection-type');
                $this->content = $response->getProperty('content');
                $this->dscp = $response->getProperty('dscp');
                $this->dst_address = $response->getProperty('dst-address');
                $this->dst_address_list = $response->getProperty('dst-address-list');
                $this->dst_address_type = $response->getProperty('dst-address-type');
                $this->dst_limit = $response->getProperty('dst-limit');
                $this->dst_port = $response->getProperty('dst-port');
                $this->fragment = $response->getProperty('fragment');
                $this->hotspot = $response->getProperty('hotspot');
                $this->icmp_options = $response->getProperty('icmp-options');
                $this->in_bridge_port = $response->getProperty('in-bridge-port');
                $this->in_interface = $response->getProperty('in-interface');
                $this->ingress_priority = $response->getProperty('ingress-priority');
                $this->ipsec_policy = $response->getProperty('ipsec-policy');
                $this->ipv4_options = $response->getProperty('ipv4-options');
                $this->jump_target = $response->getProperty('jump-target');
                $this->layer7_protocol = $response->getProperty('layer7-protocol');
                $this->limit = $response->getProperty('limit');
                $this->log_prefix = $response->getProperty('log-prefix');
                $this->nth = $response->getProperty('nth');
                $this->out_bridge_port = $response->getProperty('out-bridge-port');
                $this->out_interface = $response->getProperty('out-interface');
                $this->packet_mark = $response->getProperty('packet-mark');
                $this->packet_size = $response->getProperty('packet-size');
                $this->per_connection_classifier = $response->getProperty('per-connection-classifier');
                $this->port = $response->getProperty('port');
                $this->protocol = $response->getProperty('protocol');
                $this->psd = $response->getProperty('psd');
                $this->random = $response->getProperty('random');
                $this->routing_mark = $response->getProperty('routing-mark');
                $this->same_not_by_dst = $response->getProperty('same-not-by-dst');
                $this->src_address = $response->getProperty('src-address');
                $this->src_address_list = $response->getProperty('src-address-list');
                $this->src_address_type = $response->getProperty('src-address-type');
                $this->src_port = $response->getProperty('src-port');
                $this->src_mac_address = $response->getProperty('src-mac-address');
                $this->tcp_flags = $response->getProperty('tcp-flags');
                $this->tcp_mss = $response->getProperty('tcp-mss');
                $this->time = $response->getProperty('time');
                $this->to_addresses = $response->getProperty('to-addresses');
                $this->to_ports = $response->getProperty('to-ports');
                $this->ttl = $response->getProperty('ttl');
                $this->disabled = $response->getProperty('disabled');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Nat();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/firewall/nat/set');
        $setRequest->setargument('.id', $id);
        if ($this->action != $c->action) {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->address_list != $c->address_list) {
            $setRequest->setargument('address-list', $this->address_list);
        }
        if ($this->address_list_timeout != $c->address_list_timeout) {
            $setRequest->setargument('address-list-timeout', $this->address_list_timeout);
        }
        if ($this->chain != $c->chain) {
            $setRequest->setargument('chain', $this->chain);
        }
        if ($this->comment != $c->comment) {
            $setRequest->setargument('comment', $this->comment);
        }
        if ($this->connection_bytes != $c->connection_bytes) {
            if ($this->connection_bytes == '') {
                $unset[] = 'connection-bytes';
            } else {
                $setRequest->setargument('connection-bytes', $this->connection_bytes);
            }
        }
        if ($this->connection_limit != $c->connection_limit) {
            if ($this->connection_limit == '') {
                $unset[] = 'connection-limit';
            } else {
                $setRequest->setargument('connection-limit', $this->connection_limit);
            }
        }
        if ($this->connection_mark != $c->connection_mark) {
            if ($this->connection_mark == '') {
                $unset[] = 'connection-mark';
            } else {
                $setRequest->setargument('connection-mark', $this->connection_mark);
            }
        }
        if ($this->connection_rate != $c->connection_rate) {
            if ($this->connection_rate == '') {
                $unset[] = 'connection-rate';
            } else {
                $setRequest->setargument('connection-rate', $this->connection_rate);
            }
        }
        if ($this->connection_type != $c->connection_type) {
            if ($this->connection_type == '') {
                $unset[] = 'connection-type';
            } else {
                $setRequest->setargument('connection-type', $this->connection_type);
            }
        }
        if ($this->content != $c->content) {
            if ($this->content == '') {
                $unset[] = 'content';
            } else {
                $setRequest->setargument('content', $this->content);
            }
        }
        if ($this->dscp != $c->dscp) {
            if ($this->dscp == '') {
                $unset[] = 'dscp';
            } else {
                $setRequest->setargument('dscp', $this->dscp);
            }
        }
        if ($this->dst_address != $c->dst_address) {
            if ($this->dst_address == '') {
                $unset[] = 'dst-address';
            } else {
                $setRequest->setargument('dst-address', $this->dst_address);
            }
        }
        if ($this->dst_address_list != $c->dst_address_list) {
            if ($this->dst_address_list == '') {
                $unset[] = 'dst-address-list';
            } else {
                $setRequest->setargument('dst-address-list', $this->dst_address_list);
            }
        }
        if ($this->dst_address_type != $c->dst_address_type) {
            if ($this->dst_address_type == '') {
                $unset[] = 'dst-address-type';
            } else {
                $setRequest->setargument('dst-address-type', $this->dst_address_type);
            }
        }
        if ($this->dst_limit != $c->dst_limit) {
            if ($this->dst_limit == '') {
                $unset[] = 'dst-limit';
            } else {
                $setRequest->setargument('dst-limit', $this->dst_limit);
            }
        }
        if ($this->dst_port != $c->dst_port) {
            if ($this->dst_port == '') {
                $unset[] = 'dst-port';
            } else {
                $setRequest->setargument('dst-port', $this->dst_port);
            }
        }
        if ($this->fragment != $c->fragment) {
            $setRequest->setargument('fragment', (($this->fragment == true) ? "true" : "false"));
        }
        if ($this->hotspot != $c->hotspot) {
            if ($this->hotspot == '') {
                $unset[] = 'hotspot';
            } else {
                $setRequest->setargument('hotspot', $this->hotspot);
            }
        }
        if ($this->icmp_options != $c->icmp_options) {
            if ($this->icmp_options == '') {
                $unset[] = 'icmp-options';
            } else {
                $setRequest->setargument('icmp-options', $this->icmp_options);
            }
        }
        if ($this->in_bridge_port != $c->in_bridge_port) {
            if ($this->in_bridge_port == '') {
                $unset[] = 'in-bridge-port';
            } else {
                $setRequest->setargument('in-bridge-port', $this->in_bridge_port);
            }
        }
        if ($this->in_interface != $c->in_interface) {
            if ($this->in_interface == '') {
                $unset[] = 'in-interface';
            } else {
                $setRequest->setargument('in-interface', $this->in_interface);
            }
        }
        if ($this->ingress_priority != $c->ingress_priority) {
            if ($this->ingress_priority == '') {
                $unset[] = 'ingress-priority';
            } else {
                $setRequest->setargument('ingress-priority', $this->ingress_priority);
            }
        }
        if ($this->ipsec_policy != $c->ipsec_policy) {
            if ($this->ipsec_policy == '') {
                $unset[] = 'ipsec-policy';
            } else {
                $setRequest->setargument('ipsec-policy', $this->ipsec_policy);
            }
        }
        if ($this->ipv4_options != $c->ipv4_options) {
            if ($this->ipv4_options == '') {
                $unset[] = 'ipv4-options';
            } else {
                $setRequest->setargument('ipv4-options', $this->ipv4_options);
            }
        }
        if ($this->jump_target != $c->jump_target) {
            $setRequest->setargument('jump-target', $this->jump_target);
        }
        if ($this->layer7_protocol != $c->layer7_protocol) {
            if ($this->layer7_protocol == '') {
                $unset[] = 'layer7-protocol';
            } else {
                $setRequest->setargument('layer7-protocol', $this->layer7_protocol);
            }
        }
        if ($this->limit != $c->limit) {
            if ($this->limit == '') {
                $unset[] = 'limit';
            } else {
                $setRequest->setargument('limit', $this->limit);
            }
        }
        if ($this->log_prefix != $c->log_prefix) {
            $setRequest->setargument('log-prefix', $this->log_prefix);
        }
        if ($this->nth != $c->nth) {
            if ($this->nth == '') {
                $unset[] = 'nth';
            } else {
                $setRequest->setargument('nth', $this->nth);
            }
        }
        if ($this->out_bridge_port != $c->out_bridge_port) {
            if ($this->out_bridge_port == '') {
                $unset[] = 'out-bridge-port';
            } else {
                $setRequest->setargument('out-bridge-port', $this->out_bridge_port);
            }
        }
        if ($this->out_interface != $c->out_interface) {
            if ($this->out_interface == '') {
                $unset[] = 'out-interface';
            } else {
                $setRequest->setargument('out-interface', $this->out_interface);
            }
        }
        if ($this->packet_mark != $c->packet_mark) {
            if ($this->packet_mark == '') {
                $unset[] = 'packet-mark';
            } else {
                $setRequest->setargument('packet-mark', $this->packet_mark);
            }
        }
        if ($this->packet_size != $c->packet_size) {
            if ($this->packet_size == '') {
                $unset[] = 'packet-size';
            } else {
                $setRequest->setargument('packet-size', $this->packet_size);
            }
        }
        if ($this->per_connection_classifier != $c->per_connection_classifier) {
            if ($this->per_connection_classifier == '') {
                $unset[] = 'per-connection-classifier';
            } else {
                $setRequest->setargument('per-connection-classifier', $this->per_connection_classifier);
            }
        }
        if ($this->protocol != $c->protocol) {
            if ($this->protocol == '') {
                $unset[] = 'protocol';
            } else {
                $setRequest->setargument('protocol', $this->protocol);
            }
        }
        if ($this->psd != $c->psd) {
            if ($this->psd == '') {
                $unset[] = 'psd';
            } else {
                $setRequest->setargument('psd', $this->psd);
            }
        }
        if ($this->random != $c->random) {
            if ($this->random == '') {
                $unset[] = 'random';
            } else {
                $setRequest->setargument('random', $this->random);
            }
        }
        if ($this->routing_mark != $c->routing_mark) {
            if ($this->routing_mark == '') {
                $unset[] = 'routing-mark';
            } else {
                $setRequest->setargument('routing-mark', $this->routing_mark);
            }
        }
        if ($this->src_address != $c->src_address) {
            if ($this->src_address == '') {
                $unset[] = 'src-address';
            } else {
                $setRequest->setargument('src-address', $this->src_address);
            }
        }
        if ($this->src_address_list != $c->src_address_list) {
            if ($this->src_address_list == '') {
                $unset[] = 'src-address-list';
            } else {
                $setRequest->setargument('src-address-list', $this->src_address_list);
            }
        }
        if ($this->src_address_type != $c->src_address_type) {
            if ($this->src_address_type == '') {
                $unset[] = 'src-address-type';
            } else {
                $setRequest->setargument('src-address-type', $this->src_address_type);
            }
        }
        if ($this->src_port != $c->src_port) {
            if ($this->src_port == '') {
                $unset[] = 'src-port';
            } else {
                $setRequest->setargument('src-port', $this->src_port);
            }
        }
        if ($this->src_mac_address != $c->src_mac_address) {
            if ($this->src_mac_address == '') {
                $unset[] = 'src-mac-address';
            } else {
                $setRequest->setargument('src-mac-address', $this->src_mac_address);
            }
        }
        if ($this->tcp_flags != $c->tcp_flags) {
            if ($this->tcp_flags == '') {
                $unset[] = 'tcp-flags';
            } else {
                $setRequest->setargument('tcp-flags', $this->tcp_flags);
            }
        }
        if ($this->tcp_mss != $c->tcp_mss) {
            if ($this->tcp_mss == '') {
                $unset[] = 'tcp-mss';
            } else {
                $setRequest->setargument('tcp-mss', $this->tcp_mss);
            }
        }
        if ($this->time != $c->time) {
            if ($this->time == '') {
                $unset[] = 'time';
            } else {
                $setRequest->setargument('time', $this->time);
            }
        }
        if ($this->ttl != $c->ttl) {
            if ($this->ttl == '') {
                $unset[] = 'ttl';
            } else {
                $setRequest->setargument('ttl', $this->ttl);
            }
        }

        if ($this->port != $c->port) {
            if ($this->port == '') {
                $unset[] = 'port';
            } else {
                $setRequest->setargument('port', $this->port);
            }
        }
        if ($this->same_not_by_dst != $c->same_not_by_dst) {
            $setRequest->setargument('same-not-by-dst', $this->same_not_by_dst);
        }
        if ($this->to_addresses != $c->to_addresses) {
            $setRequest->setargument('to-addresses', $this->to_addresses);
        }
        if ($this->to_ports != $c->to_ports) {
            $setRequest->setargument('to-ports', $this->to_ports);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/ip/firewall/nat/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/firewall/nat/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/firewall/nat/add');

        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->address_list != "") {
            $setRequest->setargument('address-list', $this->address_list);
        }
        if ($this->address_list_timeout != "") {
            $setRequest->setargument('address-list-timeout', $this->address_list_timeout);
        }
        if ($this->chain != "") {
            $setRequest->setargument('chain', $this->chain);
        }
        if ($this->comment != "") {
            $setRequest->setargument('comment', $this->comment);
        }
        if ($this->connection_bytes != "") {
            $setRequest->setargument('connection-bytes', $this->connection_bytes);
        }
        if ($this->connection_limit != "") {
            $setRequest->setargument('connection-limit', $this->connection_limit);
        }
        if ($this->connection_mark != "") {
            $setRequest->setargument('connection-mark', $this->connection_mark);
        }
        if ($this->connection_rate != "") {
            $setRequest->setargument('connection-rate', $this->connection_rate);
        }
        if ($this->connection_type != "") {
            $setRequest->setargument('connection-type', $this->connection_type);
        }
        if ($this->content != "") {
            $setRequest->setargument('content', $this->content);
        }
        if ($this->dscp != "") {
            $setRequest->setargument('dscp', $this->dscp);
        }
        if ($this->dst_address != "") {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->dst_address_list != "") {
            $setRequest->setargument('dst-address-list', $this->dst_address_list);
        }
        if ($this->dst_address_type != "") {
            $setRequest->setargument('dst-address-type', $this->dst_address_type);
        }
        if ($this->dst_limit != "") {
            $setRequest->setargument('dst-limit', $this->dst_limit);
        }
        if ($this->dst_port != "") {
            $setRequest->setargument('dst-port', $this->dst_port);
        }
        if ($this->fragment != "") {
            $setRequest->setargument('fragment', (($this->fragment == true) ? "true" : "false"));
        }
        if ($this->hotspot != "") {
            $setRequest->setargument('hotspot', $this->hotspot);
        }
        if ($this->icmp_options != "") {
            $setRequest->setargument('icmp-options', $this->icmp_options);
        }
        if ($this->in_bridge_port != "") {
            $setRequest->setargument('in-bridge-port', $this->in_bridge_port);
        }
        if ($this->in_interface != "") {
            $setRequest->setargument('in-interface', $this->in_interface);
        }
        if ($this->ingress_priority != "") {
            $setRequest->setargument('ingress-priority', $this->ingress_priority);
        }
        if ($this->ipsec_policy != "") {
            $setRequest->setargument('ipsec-policy', $this->ipsec_policy);
        }
        if ($this->ipv4_options != "") {
            $setRequest->setargument('ipv4-options', $this->ipv4_options);
        }
        if ($this->jump_target != "") {
            $setRequest->setargument('jump-target', $this->jump_target);
        }
        if ($this->layer7_protocol != "") {
            $setRequest->setargument('layer7-protocol', $this->layer7_protocol);
        }
        if ($this->limit != "") {
            $setRequest->setargument('limit', $this->limit);
        }
        if ($this->log_prefix != "") {
            $setRequest->setargument('log-prefix', $this->log_prefix);
        }
        if ($this->nth != "") {
            $setRequest->setargument('nth', $this->nth);
        }
        if ($this->out_bridge_port != "") {
            $setRequest->setargument('out-bridge-port', $this->out_bridge_port);
        }
        if ($this->out_interface != "") {
            $setRequest->setargument('out-interface', $this->out_interface);
        }
        if ($this->packet_mark != "") {
            $setRequest->setargument('packet-mark', $this->packet_mark);
        }
        if ($this->packet_size != "") {
            $setRequest->setargument('packet-size', $this->packet_size);
        }
        if ($this->per_connection_classifier != "") {
            $setRequest->setargument('per-connection-classifier', $this->per_connection_classifier);
        }
        if ($this->port != "") {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->protocol != "") {
            $setRequest->setargument('protocol', $this->protocol);
        }
        if ($this->psd != "") {
            $setRequest->setargument('psd', $this->psd);
        }
        if ($this->random != "") {
            $setRequest->setargument('random', $this->random);
        }
        if ($this->routing_mark != "") {
            $setRequest->setargument('routing-mark', $this->routing_mark);
        }
        if ($this->same_not_by_dst != "") {
            $setRequest->setargument('same-not-by-dst', $this->same_not_by_dst);
        }
        if ($this->src_address != "") {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->src_address_list != "") {
            $setRequest->setargument('src-address-list', $this->src_address_list);
        }
        if ($this->src_address_type != "") {
            $setRequest->setargument('src-address-type', $this->src_address_type);
        }
        if ($this->src_port != "") {
            $setRequest->setargument('src-port', $this->src_port);
        }
        if ($this->src_mac_address != "") {
            $setRequest->setargument('src-mac-address', $this->src_mac_address);
        }
        if ($this->tcp_flags != "") {
            $setRequest->setargument('tcp-flags', $this->tcp_flags);
        }
        if ($this->tcp_mss != "") {
            $setRequest->setargument('tcp-mss', $this->tcp_mss);
        }
        if ($this->time != "") {
            $setRequest->setargument('time', $this->time);
        }
        if ($this->to_addresses != "") {
            $setRequest->setargument('to-addresses', $this->to_addresses);
        }
        if ($this->to_ports != "") {
            $setRequest->setargument('to-ports', $this->to_ports);
        }
        if ($this->ttl != "") {
            $setRequest->setargument('ttl', $this->ttl);
        }
        $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/firewall/nat');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

    public function getChain() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/firewall/nat/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $chains = array();
        foreach ($responses as $response) {
            if (($response->getProperty('.id') != null) and ( $response->getProperty('chain') != 'srcnat')and ( $response->getProperty('chain') != 'dstnat')) {
                $chains[$response->getProperty('chain')] = $response->getProperty('chain');
            }
        }
        return $chains;
    }

}
