<?php

namespace app\models\mikrotik\ip\dns;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

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

class Dns extends Model {

    public $servers;
    public $dynamic_servers;
    public $allow_remote_requests;
    public $max_udp_packet_size;
    public $query_server_timeout;
    public $query_total_timeout;
    public $cache_size;
    public $cache_max_ttl;
    public $cache_used;

    public function rules() {
        return [

            ['servers', 'string'],
            ['dynamic_servers', 'string'],
            ['allow_remote_requests', 'string'],
            ['max_udp_packet_size', 'string'],
            ['query_server_timeout', 'string'],
            ['query_total_timeout', 'string'],
            ['cache_size', 'string'],
            ['cache_max_ttl', 'string'],
            ['cache_used', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dns/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('allow-remote-requests')) {
                $this->servers = $response->getProperty('servers');
                $this->dynamic_servers = $response->getProperty('dynamic-servers');
                $this->allow_remote_requests = (($response->getProperty('allow-remote-requests') == 'true') ? true : false);
                $this->max_udp_packet_size = $response->getProperty('max-udp-packet-size');
                $this->query_server_timeout = $response->getProperty('query-server-timeout');
                $this->query_total_timeout = $response->getProperty('query-total-timeout');
                $this->cache_size = $response->getProperty('cache-size');
                $this->cache_max_ttl = $response->getProperty('cache-max-ttl');
                $this->cache_used = $response->getProperty('cache-used');
            }
        }
        return;
    }

    public function update() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dns/set');

        if ($this->servers != "") {
            $setRequest->setargument('servers', $this->servers);
        }

        if ($this->allow_remote_requests != "") {
            $setRequest->setargument('allow-remote-requests', (($this->allow_remote_requests == true) ? "true" : "false"));
        }
        if ($this->max_udp_packet_size != "") {
            $setRequest->setargument('max-udp-packet-size', $this->max_udp_packet_size);
        }
        if ($this->query_server_timeout != "") {
            $setRequest->setargument('query-server-timeout', $this->query_server_timeout);
        }
        if ($this->query_total_timeout != "") {
            $setRequest->setargument('query-total-timeout', $this->query_total_timeout);
        }
        if ($this->cache_size != "") {
            $setRequest->setargument('cache-size', $this->cache_size);
        }
        if ($this->cache_max_ttl != "") {
            $setRequest->setargument('cache-max-ttl', $this->cache_max_ttl);
        }

        $connect->getConnection()->sendSync($setRequest);
    }

}
