<?php

use Bitrix\Main\SiteTable;

const STOP_STATISTICS = true;
const NOT_CHECK_PERMISSIONS = true;

if (
    (($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') !== 'XMLHttpRequest')
    || !isset($_POST['SITE_ID'])
) {
    http_response_code(404);
    die;
}

$siteId = trim((string)$_POST['SITE_ID']);

if (
    $siteId === ''
    || strlen($siteId) > 100
    || !preg_match('/^[a-z0-9_-]+$/i', $siteId)
) {
    http_response_code(404);
    die;
}

require_once $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php';

$site = SiteTable::getRow([
    'filter' => [
        '=LID' => $siteId,
        '=ACTIVE' => 'Y',
    ],
    'select' => ['LID'],
]);

if (!$site) {
    http_response_code(404);
    die;
}

if (!defined('SITE_ID')) {
    define('SITE_ID', $siteId);
} elseif (SITE_ID !== $siteId) {
    http_response_code(404);
    die;
}

global $APPLICATION;

$APPLICATION->ShowAjaxHead();

?>
<div class="startshop-ajax-authorize">
    <?php $APPLICATION->IncludeComponent(
        'bitrix:system.auth.authorize',
        'startshop',
        [
            'BACKURL' => $_REQUEST['backurl'] ?? '',
            'AUTH_FORGOT_PASSWORD_URL' => $_REQUEST['sForgotPasswordUrl'] ?? '',
            'AUTH_REGISTER_URL' => $_REQUEST['sRegisterUrl'] ?? '',
            'AUTH_URL' => $_REQUEST['sAuthUrl'] ?? '',
            'USE_ADAPTABILITY' => 'Y'
        ],
        null,
        ['HIDE_ICONS' => 'Y']
    ); ?>
</div>
<script>
    BX.ready(function(){
        BX('bx_auth_popup_form').style.display = 'block';
    });
</script>
