<?php
/**
 * Public alias for the application entry point
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// Error reporting rule
ini_set('swoole.display_errors', 0);
error_reporting(0);

// Disable display errors by default
ini_set('display_startup_errors', 0);
ini_set('display_errors', 0);

// Show debug error
if ($_COOKIE['debug_mode'] == 'true') {
	ini_set('display_startup_errors', 1);
	ini_set('display_errors', 1);
	error_reporting(-1);
}

use Magento\Framework\App\Bootstrap;

try {
	require __DIR__ . '/../app/bootstrap.php';
} catch (\Exception $e) {
	echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
	http_response_code(500);
	exit(1);
}

global $marketConfig;
require_once str_replace(['/pub', '\pub'], '', getcwd()) . '/config.php';

// Allow REST V1 endpoints, robots.txt, or internal maintenance cookie to bypass maintenance mode.
$server = $_SERVER;
$requestUri = (string)($server['REQUEST_URI'] ?? '');
$requestPath = (string)(parse_url($requestUri, PHP_URL_PATH) ?? '');
$isRobotsRequest = ($requestPath !== '' && preg_match('#^/robots?\.txt$#i', $requestPath) === 1);
if (
    ($requestPath !== '' && preg_match('#(?:^|/)rest(?:/[^/]+)?/V1(?:/|$)#', $requestPath) === 1)
    || $isRobotsRequest
    || ((string)($server['HTTP_COOKIE'] ?? '') !== '' && (string)($_COOKIE['maintenance'] ?? '') === '0')
) {
    $server[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
}

$bootstrap = Bootstrap::create(BP, $server);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);
