Skip to content

Commit

Permalink
Update admin controller
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rgan01 committed Jan 17, 2025
1 parent 6792732 commit a48f61d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 154 deletions.
3 changes: 3 additions & 0 deletions classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ protected function setupEnvironment(InputInterface $input, OutputInterface $outp
$this->upgradeContainer->setLogger($this->logger);
(new ErrorHandler($this->logger))->enable();
$this->logger->debug('Error handler enabled.');

$moduleDir = $this->upgradeContainer->getProperty(UpgradeContainer::WORKSPACE_PATH);
$this->upgradeContainer->getWorkspace()->init($moduleDir);
}

/**
Expand Down
1 change: 1 addition & 0 deletions classes/Log/WebLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function log($level, string $message, array $context = []): void

$this->normalMessages[] = $log;

// deprecated : todo need to be removed after NEW UI is completed.
if ($level > self::ERROR) {
$this->severeMessages[] = $log;
}
Expand Down
1 change: 0 additions & 1 deletion classes/Task/Runner/ChainedTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ private function setupLogging(): void
$initializationSteps = [TaskName::TASK_BACKUP_INITIALIZATION, TaskName::TASK_UPDATE_INITIALIZATION, TaskName::TASK_RESTORE_INITIALIZATION];

if (in_array($this->step, $initializationSteps)) {
$this->container->getWorkspace()->createFolders();
$timestamp = date('Y-m-d-His');
switch ($this->step) {
case TaskName::TASK_BACKUP_INITIALIZATION:
Expand Down
55 changes: 52 additions & 3 deletions classes/Workspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

namespace PrestaShop\Module\AutoUpgrade;

use Exception;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;
use Symfony\Component\Filesystem\Exception\IOException;

class Workspace
{
Expand All @@ -55,11 +55,60 @@ public function createFolders(): void
{
foreach ($this->paths as $path) {
if (!file_exists($path) && !@mkdir($path)) {
throw new Exception($this->translator->trans('Unable to create directory %s', [$path]));
throw new IOException($this->translator->trans('Unable to create directory %s', [$path]));
}
if (!is_writable($path)) {
throw new Exception($this->translator->trans('Cannot write to the directory. Please ensure you have the necessary write permissions on "%s".', [$path]));
throw new IOException($this->translator->trans('Cannot write to the directory. Please ensure you have the necessary write permissions on "%s".', [$path]));
}

$this->createPhpIndex($path);
}
}

/**
* @throws IOException
*/
public function createPhpIndex(string $directoryPath): void
{
$filePath = $directoryPath . DIRECTORY_SEPARATOR . 'index.php';
if (!file_exists($filePath)) {
if (!copy(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'index.php', $filePath)) {
throw new IOException($this->translator->trans('Unable to create file %s', [$filePath]));
}
}
}

/**
* @throws IOException
*/
public function createHtAccess(string $modulePath): void
{
$filePath = $modulePath . DIRECTORY_SEPARATOR . '.htaccess';
$content = <<<HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine On
# Si on accède au dossier "log", on autorise l'accès
RewriteCond %{REQUEST_URI} ^/log/ [NC]
RewriteRule ^ - [L]
# Sinon, on interdit l'accès
RewriteRule ^ - [F]
</IfModule>
HTACCESS;
if (!file_exists($filePath)) {
if (!file_put_contents($filePath, $content)) {
throw new IOException($this->translator->trans('Unable to create file %s', [$filePath]));
}
}
}

/**
* @throws IOException
*/
public function init($modulePath): void

Check failure on line 109 in classes/Workspace.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.2.5)

Method PrestaShop\Module\AutoUpgrade\Workspace::init() has parameter $modulePath with no type specified.

Check failure on line 109 in classes/Workspace.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.3.4)

Method PrestaShop\Module\AutoUpgrade\Workspace::init() has parameter $modulePath with no type specified.

Check failure on line 109 in classes/Workspace.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.4.4)

Method PrestaShop\Module\AutoUpgrade\Workspace::init() has parameter $modulePath with no type specified.
{
$this->createFolders();
$this->createHtAccess($modulePath);
}
}
153 changes: 3 additions & 150 deletions controllers/admin/AdminSelfUpgradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Router\Router;
use PrestaShop\Module\AutoUpgrade\Tools14;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
Expand All @@ -35,6 +34,7 @@ class AdminSelfUpgradeController extends ModuleAdminController
{
/** @var Autoupgrade */
public $module;
public $multishop_context_group = false;
/** @var bool */
public $ajax = false;
/** @var bool */
Expand Down Expand Up @@ -184,66 +184,6 @@ public function __construct()
}
}

/**
* function to set configuration fields display.
*
* @return void
*/
private function _setFields()
{
$this->_fieldsBackupOptions = [
UpgradeConfiguration::PS_AUTOUP_BACKUP => [
'title' => $this->trans('Back up my files and database'),
'cast' => 'intval',
'validation' => 'isBool',
'defaultValue' => '1',
'type' => 'bool',
'desc' => $this->trans('Automatically back up your database and files in order to restore your shop if needed. This is experimental: you should still perform your own manual backup for safety.'),
],
UpgradeConfiguration::PS_AUTOUP_KEEP_IMAGES => [
'title' => $this->trans('Back up my images'),
'cast' => 'intval',
'validation' => 'isBool',
'defaultValue' => '1',
'type' => 'bool',
'desc' => $this->trans('To save time, you can decide not to back your images up. In any case, always make sure you did back them up manually.'),
],
];
$this->_fieldsUpgradeOptions = [
UpgradeConfiguration::PS_AUTOUP_CUSTOM_MOD_DESACT => [
'title' => $this->trans('Disable non-native modules'),
'cast' => 'intval',
'validation' => 'isBool',
'type' => 'bool',
'desc' => $this->trans('As non-native modules can experience some compatibility issues, we recommend to disable them by default.') . '<br />' .
$this->trans('Keeping them enabled might prevent you from loading the "Modules" page properly after the upgrade.'),
],
UpgradeConfiguration::PS_DISABLE_OVERRIDES => [
'title' => $this->trans('Disable all overrides'),
'cast' => 'intval',
'validation' => 'isBool',
'type' => 'bool',
'desc' => $this->trans('Enable or disable all classes and controllers overrides.'),
],
UpgradeConfiguration::PS_AUTOUP_CHANGE_DEFAULT_THEME => [
'title' => $this->trans('Switch to the default theme'),
'cast' => 'intval',
'validation' => 'isBool',
'defaultValue' => '0',
'type' => 'bool',
'desc' => $this->trans('This will change your theme: your shop will then use the default theme of the version of PrestaShop you are upgrading to.'),
],
UpgradeConfiguration::PS_AUTOUP_REGEN_EMAIL => [
'title' => $this->trans('Regenerate the customized email templates'),
'cast' => 'intval',
'validation' => 'isBool',
'type' => 'bool',
'desc' => $this->trans('This will not upgrade the default PrestaShop e-mails.') . '<br />'
. $this->trans('If you customized the default PrestaShop e-mail templates, switching off this option will keep your modifications.'),
],
];
}

/**
* init to build informations we need.
*
Expand Down Expand Up @@ -282,7 +222,8 @@ public function init()
}
// from $_POST or $_GET
$this->action = empty($_REQUEST['action']) ? null : $_REQUEST['action'];
$this->initPath();
$moduleDir = $this->upgradeContainer->getProperty(UpgradeContainer::WORKSPACE_PATH);
$this->upgradeContainer->getWorkspace()->init($moduleDir);

$this->upgradeContainer->getBackupState()->importFromArray(
empty($_REQUEST['params']) ? [] : $_REQUEST['params']
Expand Down Expand Up @@ -326,105 +267,17 @@ public function init()
}
}

/**
* create some required directories if they does not exists.
*
* @return void
*/
public function initPath()
{
$this->upgradeContainer->getWorkspace()->createFolders();

// set autoupgradePath, to be used in backupFiles and backupDb config values
$this->autoupgradePath = $this->adminDir . DIRECTORY_SEPARATOR . $this->autoupgradeDir;
$this->backupPath = $this->autoupgradePath . DIRECTORY_SEPARATOR . 'backup';
$this->downloadPath = $this->autoupgradePath . DIRECTORY_SEPARATOR . 'download';
$this->latestPath = $this->autoupgradePath . DIRECTORY_SEPARATOR . 'latest';
$this->tmpPath = $this->autoupgradePath . DIRECTORY_SEPARATOR . 'tmp';

if (!file_exists($this->backupPath . DIRECTORY_SEPARATOR . 'index.php')) {
if (!copy(_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'index.php', $this->backupPath . DIRECTORY_SEPARATOR . 'index.php')) {
$this->_errors[] = $this->trans('Unable to create file %s', [$this->backupPath . DIRECTORY_SEPARATOR . 'index.php']);
}
}

$tmp = "order deny,allow\ndeny from all";
if (!file_exists($this->backupPath . DIRECTORY_SEPARATOR . '.htaccess')) {
if (!file_put_contents($this->backupPath . DIRECTORY_SEPARATOR . '.htaccess', $tmp)) {
$this->_errors[] = $this->trans('Unable to create file %s', [$this->backupPath . DIRECTORY_SEPARATOR . '.htaccess']);
}
}
}

public function postProcess()
{
if (!$this->isActualPHPVersionCompatible) {
return true;
}

$this->_setFields();

if (Tools14::isSubmit('customSubmitAutoUpgrade')) {
$this->handleCustomSubmitAutoUpgradeForm();
}

if (Tools14::isSubmit('deletebackup')) {
$this->handleDeletebackupForm();
}
parent::postProcess();

return true;
}

/**
* @return void
*/
private function handleDeletebackupForm()
{
$name = Tools14::getValue('name');
try {
$this->upgradeContainer->getBackupManager()->deleteBackup($name);
Tools14::redirectAdmin(self::$currentIndex . '&conf=1&token=' . Tools14::getValue('token'));
} catch (InvalidArgumentException $e) {
$this->_errors[] = $this->trans('Error when trying to delete backups %s', [$name]);
}
}

/**
* @return void
*
* @throws Exception
*/
private function handleCustomSubmitAutoUpgradeForm()
{
$config_keys = array_keys(array_merge($this->_fieldsUpgradeOptions, $this->_fieldsBackupOptions));
$config = [];
foreach ($config_keys as $key) {
if (!isset($_POST[$key])) {
continue;
}
// The PS_DISABLE_OVERRIDES variable must only be updated on the database side
if ($key === UpgradeConfiguration::PS_DISABLE_OVERRIDES) {
UpgradeConfiguration::updatePSDisableOverrides((bool) $_POST[$key]);
} else {
$config[$key] = $_POST[$key];
}
}

$error = $this->upgradeContainer->getConfigurationValidator()->validate($config);
if (!empty($error)) {
throw new UnexpectedValueException(reset($error)['message']);
}

$UpConfig = $this->upgradeContainer->getUpdateConfiguration();
$UpConfig->merge($config);

if ($this->upgradeContainer->getConfigurationStorage()->save($UpConfig)
) {
Tools14::redirectAdmin(self::$currentIndex . '&conf=6&token=' . Tools14::getValue('token'));
}
}

/**
* @return string
*/
Expand Down

0 comments on commit a48f61d

Please sign in to comment.