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

    public $id;
    public $name;
    public $regexp;

    public function rules() {
        return [
            ['name', 'string'],
            ['regexp', 'string']
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/firewall/layer7-protocol/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Layer7protocols();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->regexp = $response->getProperty('regexp');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/firewall/layer7-protocol/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->name = $response->getProperty('name');
                $this->regexp = $response->getProperty('regexp');
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/firewall/layer7-protocol/set');
        $setRequest->setargument('.id', $id);

        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->regexp != "") {
            $setRequest->setargument('regexp', $this->regexp);
        }
        $connect->getConnection()->sendSync($setRequest);
    }

    public function names() {

        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/ip/firewall/layer7-protocol/getall');
        $responses = $connect->getConnection()->sendSync($printRequest);
        $ret = array();
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $ret[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $ret;
    }

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/firewall/layer7-protocol/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->regexp != "") {
            $setRequest->setargument('regexp', $this->regexp);
        }
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();
        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/ip/firewall/layer7-protocol');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
