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

    public $enabled;
    public $max_mtu;
    public $max_mru;
    public $mrru;
    public $authentication;
    public $keepalive_timeout;
    public $max_sessions;
    public $default_profile;
    public $use_ipsec;
    public $ipsec_secret;
    public $allow_fast_path;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['max_mtu', 'string'],
            ['max_mru', 'string'],
            ['mrru', 'string'],
            ['keepalive_timeout', 'string'],
            ['max_sessions', 'string'],
            ['default_profile', 'string'],
            ['use_ipsec', 'string'],
            ['ipsec_secret', 'string'],
            ['allow_fast_path', 'string'],
        ];
    }

    public function get() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/l2tp-server/server/print');

        $response = $connect->getConnection()->sendSync($getRequest);

        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->max_mtu = $response->getProperty('max-mtu');
        $this->max_mru = $response->getProperty('max-mru');
        $this->mrru = $response->getProperty('mrru');
        $this->authentication = $response->getProperty('authentication');
        $this->keepalive_timeout = $response->getProperty('keepalive-timeout');
        $this->max_sessions = $response->getProperty('max-sessions');
        $this->default_profile = $response->getProperty('default-profile');
        $this->use_ipsec = (($response->getProperty('use-ipsec') == 'true') ? true : false);
        $this->ipsec_secret = $response->getProperty('ipsec-secret');
        $this->allow_fast_path = (($response->getProperty('allow-fast-path') == 'true') ? true : false);

        return;
    }

    public function update($id) {
        $c = new L2tp();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/l2tp-server/server/set');
        $setRequest->setargument('.id', $id);
        $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        if ($this->max_mtu != $c->max_mtu) {
            $setRequest->setargument('max-mtu', $this->max_mtu);
        }
        if ($this->max_mru != $c->max_mru) {
            $setRequest->setargument('max-mru', $this->max_mru);
        }
        if ($this->mrru != $c->mrru) {
            $setRequest->setargument('mrru', $this->mrru);
        }
        if ($this->authentication != $c->authentication) {
            $setRequest->setargument('authentication', $this->authentication);
        }
        if ($this->keepalive_timeout != $c->keepalive_timeout) {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        if ($this->max_sessions != $c->max_sessions) {
            $setRequest->setargument('max-sessions', $this->max_sessions);
        }
        if ($this->default_profile != $c->default_profile) {
            $setRequest->setargument('default-profile', $this->default_profile);
        }
        $setRequest->setargument('use-ipsec', (($this->use_ipsec == true) ? "true" : "false"));
        if ($this->ipsec_secret != $c->ipsec_secret) {
            $setRequest->setargument('ipsec-secret', $this->ipsec_secret);
        }
        $setRequest->setargument('allow-fast-path', (($this->allow_fast_path == true) ? "true" : "false"));
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/l2tp-server/server/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 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;
    }

}
