Your IP : 216.73.216.79


Current Path : /var/www/html/brspace/backend/modules/installer/controllers/
Upload File :
Current File : /var/www/html/brspace/backend/modules/installer/controllers/DefaultController.php

<?php

namespace backend\modules\installer\controllers;

use backend\modules\installer\helpers\SystemChecker;
use backend\modules\installer\helpers\Configuration;
use backend\modules\installer\models\DatabaseForm;
use backend\modules\installer\models\GeneralForm;
use backend\modules\installer\models\MailerForm;
use Yii;
use yii\web\Controller;
use yii\db\Connection;

/**
 * Default controller for the `installer` module
 *
 * Class DefaultController
 * @package awsaf\installer\controllers
 */
class DefaultController extends Controller
{
    public $layout = 'setup';
    /**
     * Renders the index view for the module
     *
     * (Step 1)
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * Prerequisites action checks application requirement using the SystemChecker
     * Library
     *
     * (Step 2)
     */
    public function actionPrerequisites()
    {
        $checks = SystemChecker::getResults();

        $hasError = FALSE;
        foreach ($checks as $check) {
            if ($check['state'] == 'ERROR')
                $hasError = TRUE;
        }

        // Render template
        return $this->render('prerequisites', ['checks' => $checks, 'hasError' => $hasError]);
    }

    /**
     * Set Database config.
     *
     * (Step 3)
     * @return mixed
     */
    public function actionDatabase()
    {
        $success = FALSE;
        $errorMsg = '';

        $form = new DatabaseForm();
        if ($form->load(Yii::$app->request->post())) {
            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = Response::FORMAT_JSON;

                return ActiveForm::validate($form);
            }


            if ($form->validate()) {
                try {
                    $dsn = "mysql:host=" . $form->hostname;
                    // Create Test DB Connection
                    Yii::$app->set('db', [
                        'class'    => Connection::className(),
                        'dsn'      => $dsn,
                        'username' => $form->username,
                        'password' => $form->password,
                        'charset'  => 'utf8'
                    ]);
                    Yii::$app->db->open();
                    // Check DB Connection
                    if ($this->checkDbConnection()) {
                        // Write Config
                        $config['class'] = Connection::className();
                        $config['dsn'] = $dsn . ';dbname=' . $form->database;
                        $config['username'] = $form->username;
                        $config['password'] = $form->password;
                        $config['charset'] = 'utf8';
                        
                        Yii::$app->db->createCommand('CREATE DATABASE IF NOT EXISTS ' . $form->database)->execute();

                        Configuration::set('db', $config);
                        
                        $name = (dirname(__DIR__) . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR . 'data.sql');
                        $success = TRUE;
                        
                    } else {
                        $errorMsg = 'Incorrect configuration';
                    }
                } catch (\Throwable $e) {
                    $errorMsg = $e->getMessage();
                }
            }
        }

        return $this->render('database', ['model' => $form, 'success' => $success, 'errorMsg' => $errorMsg]);
    }

    /**
     * Set Mail Config
     *
     * (Step 4)
     *
     * @return mixed
     */
    public function actionMailer()
    {
        $mailer = new MailerForm();

        if ($mailer->load(Yii::$app->request->post())) {
            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = Response::FORMAT_JSON;

                return ActiveForm::validate($mailer);
            }
            if(Yii::$app->request->post('submit') == 'skip') {
                // Write Config
                $config['class'] = 'yii\swiftmailer\Mailer';
                $config['useFileTransport'] = true;

                Configuration::set('mail', $config);

                return $this->redirect(Yii::$app->urlManager->createUrl(['installer/default/general']));
            } else {
                if ($mailer->validate()) {
                    if ($mailer->useTransport === '0') $mailer->useTransport = FALSE; else
                        $mailer->useTransport = TRUE;

                    // Write Config
                    $config['class'] = 'yii\swiftmailer\Mailer';
                    $config['useFileTransport'] = $mailer->useTransport;
                    $config['transport']['class'] = 'Swift_SmtpTransport';
                    $config['transport']['host'] = $mailer->host;
                    $config['transport']['username'] = $mailer->username;
                    $config['transport']['password'] = $mailer->password;
                    $config['transport']['port'] = $mailer->port;
                    $config['transport']['encryption'] = $mailer->encryption;

                    Configuration::set('mail', $config);

                    return $this->redirect(Yii::$app->urlManager->createUrl(['installer/default/general']));
//                    return $this->render('welcome');
                }

            }
        }

        return $this->render('mailer', ['model' => $mailer]);
    }
    /**
     * Set General Settings
     *
     * (Step 5)
     * @return array|string|\yii\web\Response
     */
    public function actionGeneral()
    {
        $setting = new GeneralForm();

        if ($setting->load(Yii::$app->request->post())) {
            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = Response::FORMAT_JSON;

                return ActiveForm::validate($setting);
            }
            if ($setting->validate()) {
                // Write Config
                $setting = $setting->getAttributes();
                $params['installed'] = true;
                $params['adminEmail'] = $setting['adminEmail'];
                $msetting['name'] = $setting['siteName'];
                $msetting['language'] = $setting['language'];
                
                Configuration::set('params', $params);
                Configuration::set('main-setting', $msetting);
                
                return $this->render('welcome');
            }
        }

        return $this->render('general', ['model' => $setting]);
    }
    /**
     * The init action imports the database structure & initial data
     */
    public function actionWelcome()
    {
        return $this->render('welcome');
    }
    
    public function actionTableProcessing() {
        
        $post  = filter_var_array($_POST, FILTER_SANITIZE_STRING);
        $table = $post['table'];
        $dsn   = $post['dsn'];
        $uname = $post['uname'];
        $pass  = $post['pass'];
        
        
        try {
            $name = (dirname(__DIR__) . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR . $table);

            $data = file_get_contents($name);
            
            // PDO run query
            $db = new \PDO($dsn, $uname, $pass);

            // works regardless of statements emulation
            $db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, 0);

            $db->exec($data);
            
        } catch (\Throwable $t){
            return $this->asJson(['success'=> false, 'message' => $t->getMessage()]);
        }
        return $this->asJson(['success'=> true, 'table' => $table]);
    }
    
    public function actionGetListTable() {
        $path = (dirname(__DIR__) . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR);
        $files = array_values(array_diff(scandir($path), array('.', '..')));
        
        return $this->asJson($files);
    }

    private function checkDbConnection()
    {
        try {
            Yii::$app->db->isActive;

            return TRUE;
        } catch (Exception $e) {
            print_r($e->getMessage());
        }
    }
}