<commit_before><?php
/**
 * @title          Tool Controller
 *
 * @author         <NAME> <<EMAIL>>
 * @copyright      (c) 2012-2017, <NAME>. All Rights Reserved.
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package        PH7 / App / System / Module / Admin / Controller
 * @version        1.1
 */
namespace PH7;

use PH7\Framework\Mvc\Model\Engine as D;
use PH7\Framework\Security\CSRF\Token;
use PH7\Framework\Layout\Html\Security as HtmlSecurity;
use PH7\Framework\Date\CDateTime;
use PH7\Framework\Cache\Cache;
use PH7\Framework\Layout\Tpl\Engine\PH7Tpl\PH7Tpl;
use PH7\Framework\Layout\Gzip\Gzip;
use PH7\Framework\Url\Header;
use PH7\Framework\Mvc\Router\Uri;

class ToolController extends Controller
{
    /** @var  string */
    private $sTitle;

    public function index()
    {
        $this->sTitle = t('General Tools');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->output();
    }

    public function cache()
    {
        // Adding a CSRF token for the remove ajax cache.
        $this->view->csrf_token = (new Token)->generate('cache');

        // Adding the common CSS and JS files for the ajax cache and the chart.
        $this->design->addCss(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_CSS, 'general.css');
        $this->design->addJs(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_JS, 'common.js');

        $this->sTitle = t('Caches Management');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->view->aChartData = [
            ['title' => t('Database and Other Data'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . Cache::CACHE_DIR)],
            ['title' => t('Server Code Template'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . PH7Tpl::COMPILE_DIR)],
            ['title' => t('HTML Template'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . PH7Tpl::CACHE_DIR)],
            ['title' => t('Static Files'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . Gzip::CACHE_DIR)]
        ];

        $this->output();
    }

    public function cacheConfig()
    {
        $this->sTitle = t('Cache Settings');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->output();
    }

    public function freeSpace()
    {
        // Adding the common CSS for the chart.
        $this->design->addCss(
            PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_CSS,
            'general.css'
        );

        $this->sTitle = t('Free Space of Server');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->view->aChartData = [
            ['title' => t('Public Root'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_ROOT)],
            ['title' => t('Public data'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_PUBLIC_DATA)],
            ['title' => t('Protected Root'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_PROTECTED)],
            ['title' => t('Protected data'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_DATA)]
        ];

        $this->output();
    }

    public function envMode()
    {
        $this->sTitle = t('Environment Mode');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->output();
    }

    public function backup()
    {
        $this->view->designSecurity = new HtmlSecurity; // Security Design Class

        $this->sTitle = t('Backup Management');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $aDumpList = $this->file->getFileList(PH7_PATH_BACKUP_SQL, array('.sql', '.gz'));
        // Removing the path
        $aDumpList = array_map(function ($sValue) { return str_replace(PH7_PATH_BACKUP_SQL, '', $sValue); }, $aDumpList);
        $this->view->aDumpList = $aDumpList;


        $oSecurityToken = new Token;

        if ($this->httpRequest->postExists('backup'))
        {
            if (!$oSecurityToken->check('backup'))
            {
                $this->design->setFlashMsg(Form::errorTokenMsg(), 'error');
            }
            else
            {
                // Clean the site name to avoid bug with the backup path
                $sSiteName = str_replace(array(' ', '/', '\\'), '_', $this->registry->site_name);

                switch ($this->httpRequest->post('backup_type'))
                {
                    case 'server':
                        $sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . (new Framework\Date\CDateTime)->get()->date() . '.sql';
                        (new D\Util\Backup($sFullPath))->back()->save();
                        $this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath);
                    break;

                    case 'server_archive':
                        $sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . (new Framework\Date\CDateTime)->get()->date() . '.sql.gz';
                        (new D\Util\Backup($sFullPath))->back()->saveArchive();
                        $this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath);
                    break;

                    case 'client':
                        (new D\Util\Backup($sSiteName . '_' . (new Framework\Date\CDateTime)->get()->date() . '.sql'))->back()->download();
                    break;

                    case 'client_archive':
                        (new D\Util\Backup($sSiteName . '_' . (new Framework\Date\CDateTime)->get()->date() . '.sql.gz'))->back()->downloadArchive();
                    break;

                    case 'show':
                        $this->view->sql_content = (new D\Util\Backup)->back()->show();
                    break;

                    default:
                        $this->design->setFlashMsg(t('Please select a field.'), 'error');
                }
            }
        }<commit_msg>[DRY] Store the current date into a variable<commit_after><?php
/**
 * @title          Tool Controller
 *
 * @author         <NAME> <<EMAIL>>
 * @copyright      (c) 2012-2017, <NAME>. All Rights Reserved.
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package        PH7 / App / System / Module / Admin / Controller
 * @version        1.1
 */
namespace PH7;

use PH7\Framework\Mvc\Model\Engine as D;
use PH7\Framework\Security\CSRF\Token;
use PH7\Framework\Layout\Html\Security as HtmlSecurity;
use PH7\Framework\Date\CDateTime;
use PH7\Framework\Cache\Cache;
use PH7\Framework\Layout\Tpl\Engine\PH7Tpl\PH7Tpl;
use PH7\Framework\Layout\Gzip\Gzip;
use PH7\Framework\Url\Header;
use PH7\Framework\Mvc\Router\Uri;

class ToolController extends Controller
{
    /** @var  string */
    private $sTitle;

    public function index()
    {
        $this->sTitle = t('General Tools');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->output();
    }

    public function cache()
    {
        // Adding a CSRF token for the remove ajax cache.
        $this->view->csrf_token = (new Token)->generate('cache');

        // Adding the common CSS and JS files for the ajax cache and the chart.
        $this->design->addCss(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_CSS, 'general.css');
        $this->design->addJs(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_JS, 'common.js');

        $this->sTitle = t('Caches Management');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->view->aChartData = [
            ['title' => t('Database and Other Data'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . Cache::CACHE_DIR)],
            ['title' => t('Server Code Template'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . PH7Tpl::COMPILE_DIR)],
            ['title' => t('HTML Template'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . PH7Tpl::CACHE_DIR)],
            ['title' => t('Static Files'), 'size' => $this->file->getDirSize(PH7_PATH_CACHE . Gzip::CACHE_DIR)]
        ];

        $this->output();
    }

    public function cacheConfig()
    {
        $this->sTitle = t('Cache Settings');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->output();
    }

    public function freeSpace()
    {
        // Adding the common CSS for the chart.
        $this->design->addCss(
            PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_CSS,
            'general.css'
        );

        $this->sTitle = t('Free Space of Server');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->view->aChartData = [
            ['title' => t('Public Root'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_ROOT)],
            ['title' => t('Public data'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_PUBLIC_DATA)],
            ['title' => t('Protected Root'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_PROTECTED)],
            ['title' => t('Protected data'), 'size' => $this->file->getDirFreeSpace(PH7_PATH_DATA)]
        ];

        $this->output();
    }

    public function envMode()
    {
        $this->sTitle = t('Environment Mode');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $this->output();
    }

    public function backup()
    {
        $this->view->designSecurity = new HtmlSecurity; // Security Design Class

        $this->sTitle = t('Backup Management');
        $this->view->page_title = $this->sTitle;
        $this->view->h1_title = $this->sTitle;

        $aDumpList = $this->file->getFileList(PH7_PATH_BACKUP_SQL, array('.sql', '.gz'));
        // Removing the path
        $aDumpList = array_map(function ($sValue) { return str_replace(PH7_PATH_BACKUP_SQL, '', $sValue); }, $aDumpList);
        $this->view->aDumpList = $aDumpList;


        $oSecurityToken = new Token;

        if ($this->httpRequest->postExists('backup'))
        {
            if (!$oSecurityToken->check('backup'))
            {
                $this->design->setFlashMsg(Form::errorTokenMsg(), 'error');
            }
            else
            {
                // Clean the site name to avoid bug with the backup path
                $sSiteName = str_replace(array(' ', '/', '\\'), '_', $this->registry->site_name);
                $sCurrentDate = (new CDateTime)->get()->date();

                switch ($this->httpRequest->post('backup_type'))
                {
                    case 'server':
                        $sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . $sCurrentDate . '.sql';
                        (new D\Util\Backup($sFullPath))->back()->save();
                        $this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath);
                    break;

                    case 'server_archive':
                        $sFullPath = PH7_PATH_BACKUP_SQL . 'Database-dump.' . $sCurrentDate . '.sql.gz';
                        (new D\Util\Backup($sFullPath))->back()->saveArchive();
                        $this->view->msg_success = t('Data successfully dumped into file "%0%"', $sFullPath);
                    break;

                    case 'client':
                        (new D\Util\Backup($sSiteName . '_' . $sCurrentDate . '.sql'))->back()->download();
                    break;

                    case 'client_archive':
                        (new D\Util\Backup($sSiteName . '_' . $sCurrentDate . '.sql.gz'))->back()->downloadArchive();
                    break;

                    case 'show':
                        $this->view->sql_content = (new D\Util\Backup)->back()->show();
                    break;

                    default:
                        $this->design->setFlashMsg(t('Please select a field.'), 'error');
                }
            }
        }
{'fraction_non_alphanumeric': 0.16326039913440732, 'fraction_numerical': 0.011380940931313617, 'mean_word_length': 2.0658476658476657, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 283, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1793486', 'n_tokens_mistral': 3954, 'n_tokens_neox': 3809, 'n_words': 730}