<?php

namespace app\models\mikrotik\files;

use Net_SFTP;
use yii\base\Model;
use Yii;
use app\models\mikrotik\MikrotikCrypt;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

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

class Files extends Model {

    public $id;
    public $name;
    public $type;
    public $size;
    public $creation_time;
    public $contents;
    public $file;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['type', 'string'],
            ['size', 'string'],
            ['creation_time', 'string'],
            ['contents', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/file/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Files();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->type = $response->getProperty('type');
                $c->size = $response->getProperty('size');
                $c->creation_time = $response->getProperty('creation-time');

                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/file/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $response = $connect->getConnection()->sendSync($getRequest);

        $this->id = $response->getProperty('.id');
        $this->name = $response->getProperty('name');
        $this->type = $response->getProperty('type');
        $this->size = $response->getProperty('size');
        $this->creation_time = $response->getProperty('creation-time');
        //$this->contents = $response->getProperty('contents');

        return $this;
    }

    public function update($id) {
        $c = new Files();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/file/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->type != $c->type) {
            $setRequest->setargument('type', $this->type);
        }
        if ($this->size != $c->size) {
            $setRequest->setargument('size', $this->size);
        }
        if ($this->creation_time != $c->creation_time) {
            $setRequest->setargument('creation-time', $this->creation_time);
        }
        if ($this->contents != $c->contents) {
            $setRequest->setargument('contents', $this->contents);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/file/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('/file/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/file/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->type != "") {
            $setRequest->setargument('type', $this->type);
        }
        if ($this->size != "") {
            $setRequest->setargument('size', $this->size);
        }
        if ($this->creation_time != "") {
            $setRequest->setargument('creation-time', $this->creation_time);
        }
        if ($this->contents != "") {
            $setRequest->setargument('contents', $this->contents);
        }
        $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('/file');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

    public function download($server, $file) {
        $mcrypt = new MikrotikCrypt();
        foreach ($server as $info => $values) {
            if (($values['pwd'] != '')) {
                $values['pwd'] = trim($mcrypt->Decrypt($values['pwd'], pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3")));
            }
            $sftp = new Net_SFTP($info);
            if ($return = !$sftp->login($values['login'], $values['pwd'])) {
                return('sftp Login Failed');
            }
            $c = $sftp->get($file->name);

            return $c;
        }
        return 1;
    }

    public function upload($server, $file) {
        $mcrypt = new MikrotikCrypt();
        foreach ($server as $info => $values) {
            if (($values['pwd'] != '')) {
                $values['pwd'] = trim($mcrypt->Decrypt($values['pwd'], pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3")));
            }
            $sftp = new Net_SFTP($info);
            if ($return = !$sftp->login($values['login'], $values['pwd'])) {
                return('sftp Login Failed');
            }
            $c = $sftp->put($file->name, $file->tempName, NET_SFTP_LOCAL_FILE);
        }
        return $c;
    }

}
