<commit_before><?php
/**
 * @title            Upgrade Class
 * @desc             Allows you to make updated the software (SQL, files, ...).
 *
 * @author           <NAME> <<EMAIL>>
 * @copyright        (c) 2012-2018, <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 / Core / Asset / File
 * @version          1.8
 */

namespace PH7;

defined('PH7') or exit('Restricted access');

use PH7\Framework\Cache\Cache;
use PH7\Framework\Config\Config;
use PH7\Framework\Core\Kernel;
use PH7\Framework\File as F;
use PH7\Framework\Layout\Gzip\Gzip;
use PH7\Framework\Layout\Tpl\Engine\PH7Tpl\PH7Tpl;
use PH7\Framework\Mvc\Model\DbConfig;
use PH7\Framework\Mvc\Request\Http;
use PH7\Framework\Security\Version;

@set_time_limit(0);
@ini_set('memory_limit', '528M');

class UpgradeCore
{
    /**
     * Remote update URL.
     */
    const REMOTE_URL = 'http://update.ph7cms.com/';
    const ARCHIVE_EXT = '.zip';
    const MIN_SQL_FILE_SIZE = 12; // Size in bytes

    /**
     * Internal update folders.
     *
     * @internal For better compatibility with Windows, we didn't put a slash at the end of the directory constants.
     */
    const DIR = 'upgrade';
    const FILE_DIR = 'file';
    const DATA_DIR = 'data';
    const SQL_DIR = 'sql';
    const INFO_DIR = 'info';

    const INST_INTRO_FILE = 'introduction';
    const INST_CONCL_FILE = 'conclusion';
    const UPGRADE_FILE = 'upgrade.sql';
    const VERSION_LIST_FILE = 'all_versions.txt';
    const VERSION_FILE = 'Version.class.php';

    // Use UNIX wildcard to be able to select only the sub-directories present in "public/"
    const PUBLIC_DIR = 'public/*';
    // Use UNIX wildcard to be able to select only the sub-directories present in "protected/"
    const PROTECTED_DIR = 'protected/*';


    /** @var Http */
    private $oHttpRequest;

    /** @var F\File */
    private $oFile;

    /** @var Config */
    private $oConfig;

    /** @var string */
    private $sHtml;

    private $sUpgradesDirUpgradeFolder;

    /** @var string */
    private $sVerName;

    /** @var string */
    private $sVerNumber;

    /** @var int */
    private $iVerBuild;

    /** @var bool */
    private $bAutoRemoveUpgradeDir = false;

    /** @var array */
    private $aErrors = [];

    public function __construct()
    {
        $this->oHttpRequest = new Http;
        $this->oFile = new F\File;
        $this->oConfig = Config::getInstance();

        $this->sHtml = ''; // Default HTML contents
        $this->sVerNumber = '...'; // Default value of the version number upgrade

        $this->prepare(); // Preparation and verification for software upgrade
    }

    /**
     * Output the HTML layout of the upgrade wizard.
     *
     * @return void
     */
    public function display()
    {
        echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>', t('Upgrade %software_name% | Version %0%', $this->sVerNumber), '</title><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><style>body{background:#EFEFEF;color:#555;font:normal 10pt Arial,Helvetica,sans-serif;margin:0;padding:0}.center{margin-left:auto;margin-right:auto;text-align:center;width:80%}.bold{font-weight:bold;font-size:13px}.red,.error{color:red}.success{color:green}.italic{font-style:italic}.underline{text-decoration:underline}pre{margin:2px;font-style:italic}code{font-style:italic;font-size:11px}</style></head><body><div class="center">';
        echo $this->sHtml;
        echo '</div></body></html>';
    }

    /**
     * @return array Returns all version numbers.
     */
    public function getVersions()
    {
        return (array)file(static::REMOTE_URL . static::VERSION_LIST_FILE);
    }

    /**
     * Checks and returns the correct needed version for the current pH7CMS installation.
     *
     * @return string|bool The version needed number for the current pH7CMS installation.
     */
    public function getNextVersion()
    {
        $aVersions = $this->getVersions();

        if ($iKey = array_search(Kernel::SOFTWARE_VERSION, $aVersions, true)) {
            return $aVersions[$iKey + 1];
        }

        // If no next version is found, just returns the current one.
        return Kernel::SOFTWARE_VERSION;
    }

    private function prepare()
    {
        if (!AdminCore::auth()) {
            // Checking if the administrator is logged in
            $this->aErrors[] = t('You must be logged in as administrator to upgrade your site.');
        }

        if (!$this->hasErrors()) {
            // Download the next upgrade patch to "~/_repository/" folder
            $this->download($this->getNextVersion());

            if (!$this->showAvailableUpgrades()) {
                $this->sHtml .= '<h2>' . t('No upgrade patch for %software_name%!') . '</h2>';
            } else {
                $this->sHtml .= '<h2>' . t('Upgrade available for %software_name%:') . '</h2>';

                $this->sHtml .= '<form method="post">';

                foreach ($this->showAvailableUpgrades() as $sFolder) {
                    $this->sUpgradesDirUpgradeFolder = $this->oFile->checkExtDir($sFolder);

                    $this->readConfig();

                    $sVerName = $this->oConfig->values['upgrade.version']['name'];
                    $sVerNumber = $this->oConfig->values['upgrade.version']['number'];
                    $iVerBuild = $this->oConfig->values['upgrade.version']['build'];
                    $sDesc = $this->oConfig->values['upgrade.information']['description'];

                    if ($this->checkUpgradeFolder($this->sUpgradesDirUpgradeFolder)) {
                        $bIsValidVer = $this->isValidVersion($sVerName, $sVerNumber, $iVerBuild);
                        $this->sHtml .= '<p class="underline italic ' . ($bIsValidVer ? 'bold' : '') . '">' . t('Version Name: %0%, Version Number: %1%, Version Build: %2%', $sVerName, $sVerNumber, $iVerBuild) . '</p>';

                        if ($bIsValidVer) {
                            $sMsg = t('Upgrade <span class="bold italic">%software_version_name% %software_version% Build %software_build%</span> to version <span class="bold italic">%0%</span>', '<span class="bold italic">' . $sVerName . ' ' . $sVerNumber . ' Build ' . $iVerBuild . '</span>');
                            $this->sHtml .= '<button type="submit" class="success" name="submit_upgrade" value="' . $this->sUpgradesDirUpgradeFolder . '" onclick="return confirm(\'' . t('Have you made a backup of your website files, folders and database?') . '\');">' . $sMsg . '</button>';

                            $this->sHtml .= '<p class="underline">' . t('Description of the upgrade patch:') . '</p>';
                            $this->sHtml .= $sDesc;

                            $this->sHtml .= '<p class="bold underline">' . t('Instruction:') . '</p>';
                            $this->sHtml .= $this->readInstruction(static::INST_INTRO_FILE);
                        } else {
                            $sMsg = t('Bad "version name, version number or version build" of upgrade path!');
                            $this->sHtml .= '<button type="submit" class="error" disabled="disabled">' . $sMsg . '</button>';
                        }
                    } else {
                        $sMsg = t('Upgrade patch is not valid!');
                        $this->sHtml .= '<button type="submit" class="error" disabled="disabled">' . $sMsg . '</button>';
                    }

                    $this->sHtml .= '<br /><hr /><br />';
                }

                $this->sHtml .= '</form>';

                if ($this->isUpgradeRequested() && $this->checkUpgradeFolder($this->oHttpRequest->post('submit_upgrade'))) {
                    $this->sUpgradesDirUpgradeFolder = $this->oHttpRequest->post('submit_upgrade');

                    $this->readConfig();

                    $this->sVerName = $this->oConfig->values['upgrade.version']['name']; // Version name upgrade
                    $this->sVerNumber = $this->oConfig->values['upgrade.version']['number']; // Version number upgrade
                    $this->iVerBuild = $this->oConfig->values['upgrade.version']['build']; // Version build upgrade

                    DbConfig::setSiteMode(DbConfig::MAINTENANCE_SITE);
                    $this->oConfig->setDevelopmentMode();
                    usleep(100000);

                    $this->check();
<commit_msg>Update error message to better message<commit_after><?php
/**
 * @title            Upgrade Class
 * @desc             Allows you to make updated the software (SQL, files, ...).
 *
 * @author           <NAME> <<EMAIL>>
 * @copyright        (c) 2012-2018, <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 / Core / Asset / File
 * @version          1.8
 */

namespace PH7;

defined('PH7') or exit('Restricted access');

use PH7\Framework\Cache\Cache;
use PH7\Framework\Config\Config;
use PH7\Framework\Core\Kernel;
use PH7\Framework\File as F;
use PH7\Framework\Layout\Gzip\Gzip;
use PH7\Framework\Layout\Tpl\Engine\PH7Tpl\PH7Tpl;
use PH7\Framework\Mvc\Model\DbConfig;
use PH7\Framework\Mvc\Request\Http;
use PH7\Framework\Security\Version;

@set_time_limit(0);
@ini_set('memory_limit', '528M');

class UpgradeCore
{
    /**
     * Remote update URL.
     */
    const REMOTE_URL = 'http://update.ph7cms.com/';
    const ARCHIVE_EXT = '.zip';
    const MIN_SQL_FILE_SIZE = 12; // Size in bytes

    /**
     * Internal update folders.
     *
     * @internal For better compatibility with Windows, we didn't put a slash at the end of the directory constants.
     */
    const DIR = 'upgrade';
    const FILE_DIR = 'file';
    const DATA_DIR = 'data';
    const SQL_DIR = 'sql';
    const INFO_DIR = 'info';

    const INST_INTRO_FILE = 'introduction';
    const INST_CONCL_FILE = 'conclusion';
    const UPGRADE_FILE = 'upgrade.sql';
    const VERSION_LIST_FILE = 'all_versions.txt';
    const VERSION_FILE = 'Version.class.php';

    // Use UNIX wildcard to be able to select only the sub-directories present in "public/"
    const PUBLIC_DIR = 'public/*';
    // Use UNIX wildcard to be able to select only the sub-directories present in "protected/"
    const PROTECTED_DIR = 'protected/*';


    /** @var Http */
    private $oHttpRequest;

    /** @var F\File */
    private $oFile;

    /** @var Config */
    private $oConfig;

    /** @var string */
    private $sHtml;

    private $sUpgradesDirUpgradeFolder;

    /** @var string */
    private $sVerName;

    /** @var string */
    private $sVerNumber;

    /** @var int */
    private $iVerBuild;

    /** @var bool */
    private $bAutoRemoveUpgradeDir = false;

    /** @var array */
    private $aErrors = [];

    public function __construct()
    {
        $this->oHttpRequest = new Http;
        $this->oFile = new F\File;
        $this->oConfig = Config::getInstance();

        $this->sHtml = ''; // Default HTML contents
        $this->sVerNumber = '...'; // Default value of the version number upgrade

        $this->prepare(); // Preparation and verification for software upgrade
    }

    /**
     * Output the HTML layout of the upgrade wizard.
     *
     * @return void
     */
    public function display()
    {
        echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>', t('Upgrade %software_name% | Version %0%', $this->sVerNumber), '</title><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><style>body{background:#EFEFEF;color:#555;font:normal 10pt Arial,Helvetica,sans-serif;margin:0;padding:0}.center{margin-left:auto;margin-right:auto;text-align:center;width:80%}.bold{font-weight:bold;font-size:13px}.red,.error{color:red}.success{color:green}.italic{font-style:italic}.underline{text-decoration:underline}pre{margin:2px;font-style:italic}code{font-style:italic;font-size:11px}</style></head><body><div class="center">';
        echo $this->sHtml;
        echo '</div></body></html>';
    }

    /**
     * @return array Returns all version numbers.
     */
    public function getVersions()
    {
        return (array)file(static::REMOTE_URL . static::VERSION_LIST_FILE);
    }

    /**
     * Checks and returns the correct needed version for the current pH7CMS installation.
     *
     * @return string|bool The version needed number for the current pH7CMS installation.
     */
    public function getNextVersion()
    {
        $aVersions = $this->getVersions();

        if ($iKey = array_search(Kernel::SOFTWARE_VERSION, $aVersions, true)) {
            return $aVersions[$iKey + 1];
        }

        // If no next version is found, just returns the current one.
        return Kernel::SOFTWARE_VERSION;
    }

    private function prepare()
    {
        if (!AdminCore::auth()) {
            // Checking if the administrator is logged in
            $this->aErrors[] = t('You must be logged in as administrator to upgrade your site.');
        }

        if (!$this->hasErrors()) {
            // Download the next upgrade patch to "~/_repository/" folder
            $this->download($this->getNextVersion());

            if (!$this->showAvailableUpgrades()) {
                $this->sHtml .= '<h2>' . t('No upgrade patch for %software_name%!') . '</h2>';
            } else {
                $this->sHtml .= '<h2>' . t('Upgrade available for %software_name%:') . '</h2>';

                $this->sHtml .= '<form method="post">';

                foreach ($this->showAvailableUpgrades() as $sFolder) {
                    $this->sUpgradesDirUpgradeFolder = $this->oFile->checkExtDir($sFolder);

                    $this->readConfig();

                    $sVerName = $this->oConfig->values['upgrade.version']['name'];
                    $sVerNumber = $this->oConfig->values['upgrade.version']['number'];
                    $iVerBuild = $this->oConfig->values['upgrade.version']['build'];
                    $sDesc = $this->oConfig->values['upgrade.information']['description'];

                    if ($this->checkUpgradeFolder($this->sUpgradesDirUpgradeFolder)) {
                        $bIsValidVer = $this->isValidVersion($sVerName, $sVerNumber, $iVerBuild);
                        $this->sHtml .= '<p class="underline italic ' . ($bIsValidVer ? 'bold' : '') . '">' . t('Version Name: %0%, Version Number: %1%, Version Build: %2%', $sVerName, $sVerNumber, $iVerBuild) . '</p>';

                        if ($bIsValidVer) {
                            $sMsg = t('Upgrade <span class="bold italic">%software_version_name% %software_version% Build %software_build%</span> to version <span class="bold italic">%0%</span>', '<span class="bold italic">' . $sVerName . ' ' . $sVerNumber . ' Build ' . $iVerBuild . '</span>');
                            $this->sHtml .= '<button type="submit" class="success" name="submit_upgrade" value="' . $this->sUpgradesDirUpgradeFolder . '" onclick="return confirm(\'' . t('Have you made a backup of your website files, folders and database?') . '\');">' . $sMsg . '</button>';

                            $this->sHtml .= '<p class="underline">' . t('Description of the upgrade patch:') . '</p>';
                            $this->sHtml .= $sDesc;

                            $this->sHtml .= '<p class="bold underline">' . t('Instruction:') . '</p>';
                            $this->sHtml .= $this->readInstruction(static::INST_INTRO_FILE);
                        } else {
                            $sMsg = t('Wrong "version name/number or version build"');
                            $this->sHtml .= '<button type="submit" class="error" disabled="disabled">' . $sMsg . '</button>';
                        }
                    } else {
                        $sMsg = t('Upgrade patch is not valid!');
                        $this->sHtml .= '<button type="submit" class="error" disabled="disabled">' . $sMsg . '</button>';
                    }

                    $this->sHtml .= '<br /><hr /><br />';
                }

                $this->sHtml .= '</form>';

                if ($this->isUpgradeRequested() && $this->checkUpgradeFolder($this->oHttpRequest->post('submit_upgrade'))) {
                    $this->sUpgradesDirUpgradeFolder = $this->oHttpRequest->post('submit_upgrade');

                    $this->readConfig();

                    $this->sVerName = $this->oConfig->values['upgrade.version']['name']; // Version name upgrade
                    $this->sVerNumber = $this->oConfig->values['upgrade.version']['number']; // Version number upgrade
                    $this->iVerBuild = $this->oConfig->values['upgrade.version']['build']; // Version build upgrade

                    DbConfig::setSiteMode(DbConfig::MAINTENANCE_SITE);
                    $this->oConfig->setDevelopmentMode();
                    usleep(100000);

                    $this->check();

{'fraction_non_alphanumeric': 0.15660124628734495, 'fraction_numerical': 0.00803680624308427, 'mean_word_length': 2.3112225221750866, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 227, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '371128', 'n_tokens_mistral': 5158, 'n_tokens_neox': 4877, 'n_words': 1134}