<?php

namespace app\models\mikrotik\ppp;

use yii\base\Model;
use Yii;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Ovpnclient extends Model {

    public $type = 'ovpn-client';
    public $id;
    public $name;
    public $mac_address;
    public $max_mtu;
    public $connect_to;
    public $port;
    public $mode;
    public $user;
    public $password;
    public $profile;
    public $certificate;
    public $auth;
    public $cipher;
    public $add_default_route;
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['mac_address', 'string'],
            ['max_mtu', 'string'],
            ['connect_to', 'string'],
            ['port', 'string'],
            ['mode', 'string'],
            ['user', 'string'],
            ['password', 'string'],
            ['profile', 'string'],
            ['certificate', 'string'],
            ['auth', 'string'],
            ['cipher', 'string'],
            ['add_default_route', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ovpn-client/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Ovpnclient();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->mac_address = $response->getProperty('mac-address');
                $c->max_mtu = $response->getProperty('max-mtu');
                $c->connect_to = $response->getProperty('connect-to');
                $c->port = $response->getProperty('port');
                $c->mode = $response->getProperty('mode');
                $c->user = $response->getProperty('user');
                $c->password = $response->getProperty('password');
                $c->profile = $response->getProperty('profile');
                $c->certificate = $response->getProperty('certificate');
                $c->auth = $response->getProperty('auth');
                $c->cipher = $response->getProperty('cipher');
                $c->add_default_route = $response->getProperty('add-default-route');
                $c->running = $response->getProperty('running');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        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;
    }

    public function getProfiles() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ppp/profile/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 get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ovpn-client/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->name = $response->getProperty('name');
                $this->mac_address = $response->getProperty('mac-address');
                $this->max_mtu = $response->getProperty('max-mtu');
                $this->connect_to = $response->getProperty('connect-to');
                $this->port = $response->getProperty('port');
                $this->mode = $response->getProperty('mode');
                $this->user = $response->getProperty('user');
                $this->password = $response->getProperty('password');
                $this->profile = $response->getProperty('profile');
                $this->certificate = $response->getProperty('certificate');
                $this->auth = $response->getProperty('auth');
                $this->cipher = $response->getProperty('cipher');
                $this->add_default_route = (($response->getProperty('add-default-route') == 'true') ? true : false);
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Ovpnclient();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ovpn-client/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mac_address != $c->mac_address) {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->max_mtu != $c->max_mtu) {
            $setRequest->setargument('max-mtu', $this->max_mtu);
        }
        if ($this->connect_to != $c->connect_to) {
            $setRequest->setargument('connect-to', $this->connect_to);
        }
        if ($this->port != $c->port) {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->mode != $c->mode) {
            $setRequest->setargument('mode', $this->mode);
        }
        if ($this->user != $c->user) {
            $setRequest->setargument('user', $this->user);
        }
        if ($this->password != $c->password) {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->profile != $c->profile) {
            $setRequest->setargument('profile', $this->profile);
        }
        if ($this->certificate != $c->certificate) {
            $setRequest->setargument('certificate', $this->certificate);
        }
        if ($this->auth != $c->auth) {
            $setRequest->setargument('auth', $this->auth);
        }
        if ($this->cipher != $c->cipher) {
            $setRequest->setargument('cipher', $this->cipher);
        }
        $setRequest->setargument('add-default-route', (($this->add_default_route == true) ? "true" : "false"));
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/ovpn-client/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('/interface/ovpn-client/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ovpn-client/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mac_address != "") {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->max_mtu != "") {
            $setRequest->setargument('max-mtu', $this->max_mtu);
        }
        if ($this->connect_to != "") {
            $setRequest->setargument('connect-to', $this->connect_to);
        }
        if ($this->port != "") {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->mode != "") {
            $setRequest->setargument('mode', $this->mode);
        }
        if ($this->user != "") {
            $setRequest->setargument('user', $this->user);
        }
        if ($this->password != "") {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->profile != "") {
            $setRequest->setargument('profile', $this->profile);
        }
        if ($this->certificate != "") {
            $setRequest->setargument('certificate', $this->certificate);
        }
        if ($this->auth != "") {
            $setRequest->setargument('auth', $this->auth);
        }
        if ($this->cipher != "") {
            $setRequest->setargument('cipher', $this->cipher);
        }
        if ($this->add_default_route != "") {
            $setRequest->setargument('add-default-route', (($this->add_default_route == true) ? "true" : "false"));
        }
        $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('/interface/ovpn-client');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
