<?php
use Customize\Kernel;
use Eccube\Service\SystemService;
use Symfony\Component\ErrorHandler\Debug;
use Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\IpUtils;
// システム要件チェック
if (version_compare(PHP_VERSION, '7.4.0') < 0) {
die('Your PHP installation is too old. EC-CUBE requires at least PHP 7.4.0. See the <a href="https://doc4.ec-cube.net/quickstart/requirement" target="_blank">system requirements</a> page for more information.');
}
$autoload = __DIR__.'/vendor/autoload.php';
if (!file_exists($autoload) && !is_readable($autoload)) {
die('Composer is not installed.');
}
require $autoload;
// The check is to ensure we don't use .env in production
if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
if (file_exists(__DIR__.'/.env')) {
(Dotenv::createUnsafeMutable(__DIR__))->load();
if (strpos(getenv('DATABASE_URL'), 'sqlite') !== false && !extension_loaded('pdo_sqlite')) {
(Dotenv::createUnsafeMutable(__DIR__, '.env.install'))->load();
}
} else {
(Dotenv::createUnsafeMutable(__DIR__, '.env.install'))->load();
}
}
error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
$env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'dev';
$debug = isset($_SERVER['APP_DEBUG']) ? $_SERVER['APP_DEBUG'] : ('prod' !== $env);
if ($debug) {
umask(0000);
Debug::enable();
}
if (!isset($_SERVER['TRUSTED_PROXIES'])) {
$_SERVER['TRUSTED_PROXIES'] = getenv('TRUSTED_PROXIES');
}
$trustedProxies = isset($_SERVER['TRUSTED_PROXIES']) ? $_SERVER['TRUSTED_PROXIES'] : false;
if ($trustedProxies) {
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}
$trustedHosts = isset($_SERVER['TRUSTED_HOSTS']) ? $_SERVER['TRUSTED_HOSTS'] : false;
if ($trustedHosts) {
Request::setTrustedHosts(explode(',', $trustedHosts));
}
// リバースプロキシ×SSL接続対応
if (!isset($_SERVER['HTTPS']) || ($https = $_SERVER['HTTPS']) !== 'https') {
try {
// リバースプロキシ×SSL接続対応: 環境毎の設定
if (($remoteAddr = $_SERVER['REMOTE_ADDR'])) {
//
switch (true) {
case isset($_SERVER['HTTP_X_AMZN_TRACE_ID']):
// Internet --> ELB --> Application (production, staging).
Request::setTrustedProxies([
'127.0.0.1',
$remoteAddr,
], Request::HEADER_X_FORWARDED_AWS_ELB);
break;
case ($trustedProxies = Request::getTrustedProxies())
&& ($httpXForwardedFor = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'])
&& (IpUtils::checkIp($httpXForwardedFor, $trustedProxies)):
// Internal network --> nginx-proxy --> Application (production, staging).
case ($trustedProxies = Request::getTrustedProxies())
&& ($httpHost = $_SERVER['HTTP_HOST'])
&& (IpUtils::checkIp($httpHost, $trustedProxies)):
// Internal network --> Application (production, staging).
if ((($ua = $_SERVER['HTTP_USER_AGENT']) === 'ELB-HealthChecker/2.0')
&& (IpUtils::checkIp($remoteAddr, $trustedProxies))) {
// ELB Health Check
Request::setTrustedProxies([
'127.0.0.1',
$remoteAddr,
], Request::HEADER_X_FORWARDED_AWS_ELB);
//
$_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https';
$_SERVER['HTTP_X_FORWARDED_PORT'] = '443';
} else {
// Internet --> nginx-proxy --> Application (development).
Request::setTrustedProxies([
'127.0.0.1',
$remoteAddr,
], Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}
break;
default:
throw new Exception("trusted proxies verification failed. due to:\n" . var_export(compact('trustedProxies', 'httpXForwardedFor'), true));
}
} else {
throw new Exception("get the server var “REMOTE_ADDR” failed.");
}
//
$_SERVER['HTTPS'] = 'on';
} catch (Exception $e) {
error_log("The request is not from trusted proxies. due to:\n" . $e->getMessage());
}
}
$request = Request::createFromGlobals();
$maintenanceFile = env('ECCUBE_MAINTENANCE_FILE_PATH', __DIR__.'/.maintenance');
if (file_exists($maintenanceFile)) {
$pathInfo = \rawurldecode($request->getPathInfo());
$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;
}
}
}
$kernel = new Kernel($env, $debug);
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);