<?php

namespace app\models\mikrotik\system;

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

    public $id;
    public $ca_cert;
    public $next_ca_cert;
    public $path;
    public $days_valid;
    public $request_lifetime;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['ca_cert', 'string'],
            ['next_ca_cert', 'string'],
            ['path', 'string'],
            ['days_valid', 'string'],
            ['request_lifetime', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/certificate/scep-server/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Scep();
                $c->id = $response->getProperty('.id');
                $c->ca_cert = $response->getProperty('ca-cert');
                $c->next_ca_cert = $response->getProperty('next-ca-cert');
                $c->path = $response->getProperty('path');
                $c->days_valid = $response->getProperty('days-valid');
                $c->request_lifetime = $response->getProperty('request-lifetime');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/certificate/scep-server/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->ca_cert = $response->getProperty('ca-cert');
                $this->next_ca_cert = $response->getProperty('next-ca-cert');
                $this->path = $response->getProperty('path');
                $this->days_valid = $response->getProperty('days-valid');
                $this->request_lifetime = $response->getProperty('request-lifetime');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Scep();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/certificate/scep-server/set');
        $setRequest->setargument('.id', $id);
        if ($this->ca_cert != $c->ca_cert) {
            $setRequest->setargument('ca-cert', $this->ca_cert);
        }
        if ($this->next_ca_cert != $c->next_ca_cert) {
            $setRequest->setargument('next-ca-cert', $this->next_ca_cert);
        }
        if ($this->path != $c->path) {
            $setRequest->setargument('path', $this->path);
        }
        if ($this->days_valid != $c->days_valid) {
            $setRequest->setargument('days-valid', $this->days_valid);
        }
        if ($this->request_lifetime != $c->request_lifetime) {
            $setRequest->setargument('request-lifetime', $this->request_lifetime);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/certificate/scep-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 disable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/certificate/scep-server/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/certificate/scep-server/add');
        if ($this->ca_cert != "") {
            $setRequest->setargument('ca-cert', $this->ca_cert);
        }
        if ($this->next_ca_cert != "") {
            $setRequest->setargument('next-ca-cert', $this->next_ca_cert);
        }
        if ($this->path != "") {
            $setRequest->setargument('path', $this->path);
        }
        if ($this->days_valid != "") {
            $setRequest->setargument('days-valid', $this->days_valid);
        }
        if ($this->request_lifetime != "") {
            $setRequest->setargument('request-lifetime', $this->request_lifetime);
        }
        $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('/certificate/scep-server');
        $util->remove(RouterOS\Query::where('.id', $id));
        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('authority') != null) {
                $return[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $return;
    }

}
