<commit_before><?php

/**
 * GintonicCMS : Full Stack Content Management System (http://cms.gintonicweb.com)
 * Copyright (c) <NAME>, Inc. (http://phillafrance.com)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) <NAME> (http://phillafrance.com)
 * @link          http://cms.gintonicweb.com GintonicCMS Project
 * @since         0.0.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace GintonicCMS\Controller;

use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Core\Plugin;
use Cake\Database\Exception\MissingConnectionException;
use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;
use GintonicCMS\Controller\AppController;
use Migrations\Command\Migrate;
use PDOException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

/**
 * Represents the Settings Controller
 * Settings Controller is used to Configure settings of
 * GintonicCMS Plugin.
 */
class SettingsController extends AppController
{

    /**
     * Call before executoin of every request, also set some action
     * to access without login.
     * access login as below.
     * if database is not created then it will allow all action to users.
     * if database is created but users table is not created then it will allow all action to users.
     * if users table is created but there is no any admin account then it will allow all action to users.
     * if admin record is found in users table then it ask Authentication in order to access actions of this controller.
     *
     * @param Cake\Event\Event $event Describe the Events.
     */
    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        if (!$this->__databaseConnection()) {
            $this->Auth->allow();
            return;
        }

        if (!$this->__tableExists()) {
            $this->Auth->allow();
            return;
        }

        if (!$this->__adminRecordExists()) {
            $this->Auth->allow();
            return;
        }
    }

    /**
     * Used to Authorized User to access requested action.
     *
     * @param array $user contain user detail.
     * @return boolean Return true if action is allowed else return false.
     */
    public function isAuthorized($user = null)
    {
        return parent::isAuthorized($user);
    }

    /**
     * Path to the plugin config file
     *
     * @param string $vendorDir path to composer-vendor dir
     * @return string absolute file path
     */
    protected static function _configFile($vendorDir)
    {
        return $vendorDir . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
    }

    /**
     * Used to migrate the database of CMS.
     * it use bin/cake migrations migrate --plugin GintonicCMS command to migrate database.
     */
    public function migrate()
    {
        $missingConnection = true;
        if ($this->__databaseConnection()) {
            $missingConnection = false;
            $command = new Migrate();
            $input = new ArrayInput(array('--plugin' => 'GintonicCMS'));
            $output = new NullOutput();
            $resultCode = $command->run($input, $output);
            $this->set(compact('resultCode'));
        }
        $this->set(compact('missingConnection'));
    }

    /**
     * Used to install node modules.
     * it uses 'node install' command to install node dependencies
     */
    public function nodeInstall()
    {
        ini_set('max_execution_time', 0);
        $this->autoRender = false;
        exec('cd ..' . DS . 'assets && npm install');
        $status = [
            'status' => 'ok',
            'message' => 'Node Install Successfully'
        ];
        echo json_encode($status);
    }

    /**
     * Used to install bower modules.
     * it uses 'bower install' command to install bower Modules.
     */
    public function bowerInstall()
    {
        ini_set('max_execution_time', 0);
        $this->autoRender = false;
        //exec('cd ..' . DS . 'assets'.DS.'node_modules' . DS . 'bower' . DS . 'bin && bower install');
        exec('cd ..' . DS . 'assets && bower install');
        $status = [
            'status' => 'ok',
            'message' => 'Bower Install Successfully'
        ];
        echo json_encode($status);
    }

    /**
     * Used to Compile dependency code
     * It won't minify and optimize code.
     */
    public function gruntDev()
    {
        ini_set('max_execution_time', 0);
        $this->autoRender = false;
        $path = Plugin::path('GintonicCMS');
        exec('cd ..' . DS . 'assets && grunt dev --gintonic=' . $path);
        $status = [
            'status' => 'ok',
            'message' => 'Grunt dev run Successfully'
        ];
        echo json_encode($status);
    }

    /**
     * Used to Compile dependency code
     * Will optimize code and minify it so it takes much longer to run.
     */
    public function grunt()
    {
        ini_set('max_execution_time', 0);
        $this->autoRender = false;
        $path = Plugin::path('GintonicCMS');
        exec('cd ..' . DS . 'assets && grunt --gintonic=' . $path);
        $status = [
            'status' => 'ok',
            'message' => 'Grunt run Successfully'
        ];
        echo json_encode($status);
    }

    /**
     * Used to configure database options.
     */
    public function databaseSetup()
    {
        Configure::config('default', new PhpConfig());
        Configure::load('app');
        $default = Configure::read('Datasources.default');
        if ($this->request->is(['post', 'put'])) {
            $default = array_merge($default, $this->request->data);
            ConnectionManager::config('userDb', $default);
            if ($this->__databaseConnection('userDb')) {
                Configure::write('Datasources.default', $default);
                Configure::dump('default');
                return $this->redirect([
                    'controller' => 'Pages',
                    'action' => 'home'
                ]);
            } else {
                $this->Flash->set(__('Impossible to connect to the database'), [
                    'element' => 'GintonicCMS.alert',
                    'params' => ['class' => 'alert-danger']
                ]);
            }
        }
        $this->request->data = $default;
    }

    /**
     * Create Admin account.
     * if there is already one admin account in user table then it won't allow to create another account.
     */
    public function createAdmin()
    {
        if (!$this->__tableExists('users')) {
            $tableExists = false;
        } else {<commit_msg>Writing config to the app.php file<commit_after><?php

/**
 * GintonicCMS : Full Stack Content Management System (http://cms.gintonicweb.com)
 * Copyright (c) <NAME>, Inc. (http://phillafrance.com)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) <NAME> (http://phillafrance.com)
 * @link          http://cms.gintonicweb.com GintonicCMS Project
 * @since         0.0.0
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace GintonicCMS\Controller;

use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Core\Plugin;
use Cake\Database\Exception\MissingConnectionException;
use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;
use GintonicCMS\Controller\AppController;
use Migrations\Command\Migrate;
use PDOException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

/**
 * Represents the Settings Controller
 * Settings Controller is used to Configure settings of
 * GintonicCMS Plugin.
 */
class SettingsController extends AppController
{

    /**
     * Call before executoin of every request, also set some action
     * to access without login.
     * access login as below.
     * if database is not created then it will allow all action to users.
     * if database is created but users table is not created then it will allow all action to users.
     * if users table is created but there is no any admin account then it will allow all action to users.
     * if admin record is found in users table then it ask Authentication in order to access actions of this controller.
     *
     * @param Cake\Event\Event $event Describe the Events.
     */
    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        if (!$this->__databaseConnection()) {
            $this->Auth->allow();
            return;
        }

        if (!$this->__tableExists()) {
            $this->Auth->allow();
            return;
        }

        if (!$this->__adminRecordExists()) {
            $this->Auth->allow();
            return;
        }
    }

    /**
     * Used to Authorized User to access requested action.
     *
     * @param array $user contain user detail.
     * @return boolean Return true if action is allowed else return false.
     */
    public function isAuthorized($user = null)
    {
        return parent::isAuthorized($user);
    }

    /**
     * Path to the plugin config file
     *
     * @param string $vendorDir path to composer-vendor dir
     * @return string absolute file path
     */
    protected static function _configFile($vendorDir)
    {
        return $vendorDir . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
    }

    /**
     * Used to migrate the database of CMS.
     * it use bin/cake migrations migrate --plugin GintonicCMS command to migrate database.
     */
    public function migrate()
    {
        $missingConnection = true;
        if ($this->__databaseConnection()) {
            $missingConnection = false;
            $command = new Migrate();
            $input = new ArrayInput(array('--plugin' => 'GintonicCMS'));
            $output = new NullOutput();
            $resultCode = $command->run($input, $output);
            $this->set(compact('resultCode'));
        }
        $this->set(compact('missingConnection'));
    }

    /**
     * Used to install node modules.
     * it uses 'node install' command to install node dependencies
     */
    public function nodeInstall()
    {
        ini_set('max_execution_time', 0);
        $this->autoRender = false;
        exec('cd ..' . DS . 'assets && npm install');
        $status = [
            'status' => 'ok',
            'message' => 'Node Install Successfully'
        ];
        echo json_encode($status);
    }

    /**
     * Used to install bower modules.
     * it uses 'bower install' command to install bower Modules.
     */
    public function bowerInstall()
    {
        ini_set('max_execution_time', 0);
        $this->autoRender = false;
        //exec('cd ..' . DS . 'assets'.DS.'node_modules' . DS . 'bower' . DS . 'bin && bower install');
        exec('cd ..' . DS . 'assets && bower install');
        $status = [
            'status' => 'ok',
            'message' => 'Bower Install Successfully'
        ];
        echo json_encode($status);
    }

    /**
     * Used to Compile dependency code
     * It won't minify and optimize code.
     */
    public function gruntDev()
    {
        ini_set('max_execution_time', 0);
        $this->autoRender = false;
        $path = Plugin::path('GintonicCMS');
        exec('cd ..' . DS . 'assets && grunt dev --gintonic=' . $path);
        $status = [
            'status' => 'ok',
            'message' => 'Grunt dev run Successfully'
        ];
        echo json_encode($status);
    }

    /**
     * Used to Compile dependency code
     * Will optimize code and minify it so it takes much longer to run.
     */
    public function grunt()
    {
        ini_set('max_execution_time', 0);
        $this->autoRender = false;
        $path = Plugin::path('GintonicCMS');
        exec('cd ..' . DS . 'assets && grunt --gintonic=' . $path);
        $status = [
            'status' => 'ok',
            'message' => 'Grunt run Successfully'
        ];
        echo json_encode($status);
    }

    /**
     * Used to configure database options.
     */
    public function databaseSetup()
    {
        Configure::config('default', new PhpConfig());
        Configure::load('app');
        $default = Configure::read('Datasources.default');
        if ($this->request->is(['post', 'put'])) {
            $default = array_merge($default, $this->request->data);
            ConnectionManager::config('userDb', $default);
            if ($this->__databaseConnection('userDb')) {
                Configure::write('Datasources.default', $default);
                Configure::dump('app');
                return $this->redirect([
                    'controller' => 'Pages',
                    'action' => 'home'
                ]);
            } else {
                $this->Flash->set(__('Impossible to connect to the database'), [
                    'element' => 'GintonicCMS.alert',
                    'params' => ['class' => 'alert-danger']
                ]);
            }
        }
        $this->request->data = $default;
    }

    /**
     * Create Admin account.
     * if there is already one admin account in user table then it won't allow to create another account.
     */
    public function createAdmin()
    {
        if (!$this->__tableExists('users')) {
            $tableExists = false;
        } else {
{'fraction_non_alphanumeric': 0.12510088781275222, 'fraction_numerical': 0.001027221366204417, 'mean_word_length': 2.116853418705694, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5092351', 'n_tokens_mistral': 3803, 'n_tokens_neox': 3679, 'n_words': 1083}