<?php

namespace app\controllers\mikrotik\backup;

use Yii;
use yii\web\Controller;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use app\models\mikrotik\backup\Backupmikrotik;
use yii\helpers\Url;
use app\models\mikrotik\backup\Backupdb;
use app\models\mikrotik\MikrotikSideMenu;
use yii\data\ArrayDataProvider;

class BackupController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['backupcron'],
                'rules' => [
                     [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_backupView'],
                    ],
                    [
                        'actions' => ['backupnow'],
                        'allow' => true,
                        'roles' => ['mikrotik_backupBackup'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_backupDelete'],
                    ],
                    [
                        'actions' => ['download'],
                        'allow' => true,
                        'roles' => ['mikrotik_backupDownload'],
                    ],
                    [
                        'actions' => [],
                        'allow' => true,
                        'roles' => ['admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
//                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionBackupcron() {
        $backup_mikrotik = new Backupmikrotik();
        ob_start();
        $server['192.168.0.190'] = ['login' => 'admin', 'pwd' => ''];
        //return var_dump($server);
        $backup_mikrotik->backupMikrotikCron($server, 'cron');
        //sleep(10);

        $backup_mikrotik->doBackup($server);
        ob_end_clean();
        return;
    }

    public function actionBackupnow() {
        $backup_mikrotik = new Backupmikrotik();
        ob_start();
        $backup_mikrotik->backupMikrotik();
        //sleep(10);

        $session = Yii::$app->session;
        $session->open();

        $server[$session->get('ip')] = ['login' => $session->get('login'), 'pwd' => $session->get('pwd')];
        $backup_mikrotik->doBackup($server);
        ob_end_clean();
        return $this->redirect(Url::toRoute('mikrotik/backup/backup/index'));
    }

    public function actionDelete() {
        $backup_mikrotik = new Backupmikrotik();
        $id = Yii::$app->request->get('id');
        $backup_mikrotik->delete($id);
        return $this->redirect(Url::toRoute('mikrotik/backup/backup/index'));
    }

    public function actionDownload() {
        $backup_mikrotik = new Backupmikrotik();
        $id = Yii::$app->request->get('id');
        $c = $backup_mikrotik->getBackup($id);
        $c = $c[0];
        header('Content-Description: File Transfer');
        header('Content-length: ' . $c->size);
        header('Content-Type: ' . $c->type);
        header('Content-Disposition: attachment; filename=' . $c->name . '.' . $c->type);
        header("Content-transfer-encoding: binary");
        echo $c->content;
        return;
    }

    public function actionIndex() {
        $model = new Backupmikrotik();
        $session = Yii::$app->session;
        $session->open();
        if (isset(Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId())['admin'])) {
            $provider = $model->getAll();
        } else {
            if ($session->get('ip') != null) {
                $ip = $session->get('ip');
                $provider = $model->getAll($ip);
            } else {
                Yii::$app->session->setFlash('message', 'You Are Not Connected!');
                return Yii::$app->getResponse()->redirect(Url::toRoute('mikrotik/index'));
            }
        }
        $menu = new MikrotikSideMenu();
        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'ip',
                    'name',
                    'size',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('/mikrotik/backup/index', [
                    'model' => $model,
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
        ]);
    }

    public function actionRestore() {
        $backup_mikrotik = new Backupmikrotik();
        $session = Yii::$app->session;
        $id = Yii::$app->request->get('id');
        $c = $backup_mikrotik->getBackup($id);
        $c = $c[0];
        if ($c->ip != $session->get('ip')) {
            Yii::$app->session->setFlash('message', 'This backup file does not belong to this device');
            return $this->redirect(Url::toRoute('mikrotik/backup/backup/index'));
        }
        $c = $backup_mikrotik->restore($c->name . '.' . $c->type);
        if ($c != '1') {
            Yii::$app->session->setFlash('message', $c);
        } else {
            return $this->redirect(Url::toRoute('mikrotik/disconnect'));
        }
        return;
    }

}
