<?php

namespace app\controllers\mikrotik\ip\firewall;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use Yii;
use yii\helpers\Url;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\ip\firewall\Filter;
use app\models\mikrotik\ip\firewall\Nat;
use app\models\mikrotik\ip\firewall\Mangle;
use app\models\mikrotik\ip\firewall\Addresslists;
use app\models\mikrotik\ip\firewall\Connections;
use app\models\mikrotik\ip\firewall\Serviceports;
use app\models\mikrotik\ip\firewall\Layer7protocols;
use backup;

class FirewallController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewFirewall'],
                    ],
                    [
                        '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 actionIndex() {
        //$back = new backup();
        //$back->Backup();
        $menu = new MikrotikSideMenu();
        $model = new Filter();
        $nat = new Nat();
        $provider['nat'] = $nat->getAll();
        $provide['nat'] = new ArrayDataProvider([
            'allModels' => $provider['nat'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'action',
                    'chain',
                    'src_address',
                    'dst_address',
                    'protocol',
                    'src_port',
                    'dst_port',
                    'in_interface',
                    'out_interface',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $service = new Serviceports();
        $provider['service'] = $service->getAll();
        $provide['service'] = new ArrayDataProvider([
            'allModels' => $provider['service'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'ports',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $layer7protocols = new Layer7protocols();
        $provider['layer7protocols'] = $layer7protocols->getAll();
        $provide['layer7protocols'] = new ArrayDataProvider([
            'allModels' => $provider['layer7protocols'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'regexp',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $mangle = new Mangle();
        $provider['mangle'] = $mangle->getAll();
        $provide['mangle'] = new ArrayDataProvider([
            'allModels' => $provider['mangle'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'action',
                    'chain',
                    'src_address',
                    'dst_address',
                    'protocol',
                    'src_port',
                    'dst_port',
                    'in_interface',
                    'out_interface',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $provider['filter'] = $model->getAll();
        $provide['filter'] = new ArrayDataProvider([
            'allModels' => $provider['filter'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'action',
                    'chain',
                    'src_address',
                    'dst_address',
                    'protocol',
                    'src_port',
                    'dst_port',
                    'in_interface',
                    'out_interface',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $con = new Connections();
        $provider['con'] = $con->getAll();
        $provide['con'] = new ArrayDataProvider([
            'allModels' => $provider['con'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'id',
                    'protocol',
                    'src_address',
                    'dst_address',
                    'timeout',
                    'orig_packets',
                    'orig_bytes',
                    'repl_packets',
                    'repl_bytes',
                ],
            ],
            'pagination' => [
                'pageSize' => 40,
            ],
        ]);
        $addr = new Addresslists();
        $provider['address'] = $addr->getAll();
        $provide['address'] = new ArrayDataProvider([
            'allModels' => $provider['address'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'list',
                    'address',
                    'timeout',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        return $this->render('/mikrotik/ip/firewall/index', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionWebaccess() {

        $menu = new MikrotikSideMenu();
        $model = new Accounting(['scenario' => 'actionWebaccess']);


        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->updateWeb();
            return $this->redirect(Url::toRoute('mikrotik/ip/accounting/webaccess'));
        }
        $model->getAllWeb();
        return $this->render('webAccess', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
