Skip to content

Commit

Permalink
Revert "管理者はメンテナンスモードを回避可能に変更"
Browse files Browse the repository at this point in the history
This reverts commit 1e947c0.
  • Loading branch information
chihiro-adachi committed Aug 31, 2021
1 parent 65daac3 commit 343ae79
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 129 deletions.
20 changes: 7 additions & 13 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Eccube\Kernel;
use Eccube\Service\SystemService;
use Symfony\Component\Debug\Debug;
use Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -63,18 +62,13 @@
$adminPath = env('ECCUBE_ADMIN_ROUTE', 'admin');
$adminPath = '/'.\trim($adminPath, '/').'/';
if (\strpos($pathInfo, $adminPath) !== 0) {
$maintenanceContents = file_get_contents($maintenanceFile);
$maintenanceToken = explode(':', $maintenanceContents)[1] ?? null;
$tokenInCookie = $request->cookies->get(SystemService::MAINTENANCE_TOKEN_KEY);
if ($tokenInCookie === null || $tokenInCookie !== $maintenanceToken) {
$locale = env('ECCUBE_LOCALE');
$templateCode = env('ECCUBE_TEMPLATE_CODE');
$baseUrl = \htmlspecialchars(\rawurldecode($request->getBaseUrl()), ENT_QUOTES);

header('HTTP/1.1 503 Service Temporarily Unavailable');
require __DIR__.'/maintenance.php';
return;
}
$locale = env('ECCUBE_LOCALE');
$templateCode = env('ECCUBE_TEMPLATE_CODE');
$baseUrl = \htmlspecialchars(\rawurldecode($request->getBaseUrl()), ENT_QUOTES);

header('HTTP/1.1 503 Service Temporarily Unavailable');
require __DIR__.'/maintenance.php';
return;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Eccube/Controller/Admin/Content/MaintenanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,18 @@ public function index(Request $request)

if ($form->isSubmitted() && $form->isValid()) {
$changeTo = $request->request->get('maintenance');
$path = $this->container->getParameter('eccube_content_maintenance_file_path');

if ($isMaintenance === false && $changeTo == 'on') {
// 現在メンテナンスモードではない かつ メンテナンスモードを有効 にした場合
// メンテナンスモードを有効にする
$this->systemService->enableMaintenance('', true);
file_put_contents($path, null);

$this->addSuccess('admin.content.maintenance_switch__on_message', 'admin');
} elseif ($isMaintenance && $changeTo == 'off') {
// 現在メンテナンスモード かつ メンテナンスモードを無効 にした場合
// メンテナンスモードを無効にする
$this->systemService->disableMaintenanceNow('', true);
unlink($path);

$this->addSuccess('admin.content.maintenance_switch__off_message', 'admin');
}
Expand Down
64 changes: 0 additions & 64 deletions src/Eccube/EventListener/MaintenanceListener.php

This file was deleted.

62 changes: 12 additions & 50 deletions src/Eccube/Service/SystemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,14 @@
namespace Eccube\Service;

use Doctrine\ORM\EntityManagerInterface;
use Eccube\Util\StringUtil;
use function explode;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use function unlink;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;

class SystemService implements EventSubscriberInterface
{
const MAINTENANCE_TOKEN_KEY = 'maintenance_token';
const AUTO_MAINTENANCE = 'auto_maintenance';
const AUTO_MAINTENANCE_UPDATE = 'auto_maintenance_update';

Expand Down Expand Up @@ -147,27 +140,20 @@ public function getMemoryLimit()
*
* @param bool $isEnable
* @param string $mode
* @param bool $force
*/
public function switchMaintenance($isEnable = false, $mode = self::AUTO_MAINTENANCE, bool $force = false): void
{
if ($isEnable) {
$this->enableMaintenance($mode, $force);
} else {
$this->disableMaintenanceNow($mode, $force);
}
}

public function getMaintenanceToken(): ?string
public function switchMaintenance($isEnable = false, $mode = self::AUTO_MAINTENANCE)
{
$isMaintenanceMode = $this->isMaintenanceMode();
$path = $this->container->getParameter('eccube_content_maintenance_file_path');
if (!file_exists($path)) {
return null;
}

$contents = file_get_contents($path);

return explode(':', $contents)[1] ?? null;
if ($isEnable && $isMaintenanceMode === false) {
file_put_contents($path, $mode);
} elseif ($isEnable === false && $isMaintenanceMode) {
$contents = file_get_contents($path);
if ($contents == $mode) {
unlink($path);
}
}
}

/**
Expand All @@ -182,15 +168,6 @@ public function disableMaintenanceEvent(PostResponseEvent $event)
}
}

public function enableMaintenance($mode = self::AUTO_MAINTENANCE, bool $force = false): void
{
if ($force || !$this->isMaintenanceMode()) {
$path = $this->container->getParameter('eccube_content_maintenance_file_path');
$token = StringUtil::random(32);
file_put_contents($path, "{$mode}:{$token}");
}
}

/**
* メンテナンスモードを解除する
*
Expand All @@ -204,21 +181,6 @@ public function disableMaintenance($mode = self::AUTO_MAINTENANCE)
$this->maintenanceMode = $mode;
}

public function disableMaintenanceNow($mode = self::AUTO_MAINTENANCE, bool $force = false): void
{
if (!$this->isMaintenanceMode()) {
return;
}

$path = $this->container->getParameter('eccube_content_maintenance_file_path');
$contents = file_get_contents($path);
$currentMode = explode(':', $contents)[0] ?? null;

if ($force || $currentMode === $mode) {
unlink($path);
}
}

/**
* メンテナンスモードの状態を判定する
*
Expand Down

0 comments on commit 343ae79

Please sign in to comment.