<?php



defined("ABSPATH") or die("");

/**
 * Variables
 *
 * @var Duplicator\Core\Controllers\ControllersManager $ctrlMng
 * @var Duplicator\Core\Views\TplMng $tplMng
 */

use Duplicator\Models\DynamicGlobalEntity;
use Duplicator\Models\GlobalEntity;
use Duplicator\Libs\Snap\SnapIO;
use Duplicator\Libs\WpUtils\WpArchiveUtils;
use Duplicator\Package\ClientSideKick;
use Duplicator\Utils\LockUtil;

$global  = GlobalEntity::getInstance();
$dGlobal = DynamicGlobalEntity::getInstance();

$lockModeOverride     = $dGlobal->getValString(LockUtil::LOCK_MODE_OVERRIDE_KEY, 'auto');
$kickoffOverride      = $dGlobal->getValString(ClientSideKick::KICKOFF_OVERRIDE_KEY, 'auto');
$ajaxProtocolOverride = $dGlobal->getValString(ClientSideKick::AJAX_PROTOCOL_OVERRIDE_KEY, 'auto');
$ajaxUrlOverride      = $dGlobal->getValString(ClientSideKick::AJAX_URL_OVERRIDE_KEY, '');

$prefillAuth         = $tplMng->getDataValueBool('prefillAuth');
$showMismatchWarning = $tplMng->getDataValueBool('showMismatchWarning');
$detectedAuthUser    = $tplMng->getDataValueString('detectedAuthUser');
$savedAuthUser       = $tplMng->getDataValueString('savedAuthUser');
$authChecked         = $tplMng->getDataValueBool('authChecked');
$authUser            = $tplMng->getDataValueString('authUser');
$authPass            = $tplMng->getDataValueString('authPass');
$authFieldsDisabled  = $tplMng->getDataValueBool('authFieldsDisabled');

?>

<div style="margin-top: 30px;"></div>

<h3 class="title">
    <?php esc_html_e("Server Detection", 'duplicator-pro'); ?>
</h3>
<hr size="1" />

<label class="lbl-larger">
    <?php esc_html_e("Thread Lock", 'duplicator-pro'); ?>
</label>
<div class="margin-bottom-1">
    <input type="radio" name="override_lock_mode" id="lock_mode_auto" class="margin-0"
        value="auto" <?php checked($lockModeOverride, 'auto'); ?>>
    <label for="lock_mode_auto"><?php esc_html_e("Auto", 'duplicator-pro'); ?></label>&nbsp;
    <input type="radio" name="override_lock_mode" id="lock_mode_sql" class="margin-0"
        value="sql" <?php checked($lockModeOverride, 'sql'); ?>>
    <label for="lock_mode_sql"><?php esc_html_e("SQL", 'duplicator-pro'); ?></label>&nbsp;
    <input type="radio" name="override_lock_mode" id="lock_mode_file" class="margin-0"
        value="file" <?php checked($lockModeOverride, 'file'); ?>>
    <label for="lock_mode_file"><?php esc_html_e("File", 'duplicator-pro'); ?></label>
    <p class="description">
        <?php esc_html_e('Auto uses the value detected by the server capability tests. Override only if instructed by support.', 'duplicator-pro'); ?>
    </p>
</div>

<label class="lbl-larger">
    <?php esc_html_e('Client-side Kickoff', 'duplicator-pro'); ?>
</label>
<div class="margin-bottom-1">
    <input type="radio" name="override_kickoff" id="kickoff_auto" class="margin-0"
        value="auto" <?php checked($kickoffOverride, 'auto'); ?>>
    <label for="kickoff_auto"><?php esc_html_e("Auto", 'duplicator-pro'); ?></label>&nbsp;
    <input type="radio" name="override_kickoff" id="kickoff_server" class="margin-0"
        value="server" <?php checked($kickoffOverride, 'server'); ?>>
    <label for="kickoff_server"><?php esc_html_e("Disabled", 'duplicator-pro'); ?></label>&nbsp;
    <input type="radio" name="override_kickoff" id="kickoff_client" class="margin-0"
        value="client" <?php checked($kickoffOverride, 'client'); ?>>
    <label for="kickoff_client"><?php esc_html_e("Enabled", 'duplicator-pro'); ?></label>
    <p class="description">
        <?php esc_html_e('Auto uses the loopback detection result. Override only if instructed by support.', 'duplicator-pro'); ?>
    </p>
</div>

<label class="lbl-larger">
    <?php esc_html_e('Server-to-Server Ajax', 'duplicator-pro'); ?>
</label>
<div class="margin-bottom-1">
    <input type="radio" name="override_ajax_protocol" id="ajax_proto_auto" class="ajax_protocol margin-0"
        value="auto" <?php checked($ajaxProtocolOverride, 'auto'); ?>>
    <label for="ajax_proto_auto"><?php esc_html_e("Auto", 'duplicator-pro'); ?></label>&nbsp;
    <input type="radio" name="override_ajax_protocol" id="ajax_proto_http" class="ajax_protocol margin-0"
        value="http" <?php checked($ajaxProtocolOverride, 'http'); ?>>
    <label for="ajax_proto_http"><?php esc_html_e("HTTP", 'duplicator-pro'); ?></label>&nbsp;
    <input type="radio" name="override_ajax_protocol" id="ajax_proto_https" class="ajax_protocol margin-0"
        value="https" <?php checked($ajaxProtocolOverride, 'https'); ?>>
    <label for="ajax_proto_https"><?php esc_html_e("HTTPS", 'duplicator-pro'); ?></label>&nbsp;
    <input type="radio" name="override_ajax_protocol" id="ajax_proto_custom" class="ajax_protocol margin-0"
        value="custom" <?php checked($ajaxProtocolOverride, 'custom'); ?>>
    <label for="ajax_proto_custom"><?php esc_html_e("Custom URL", 'duplicator-pro'); ?></label>
    <br />
    <input type="<?php echo ($ajaxProtocolOverride === 'custom' ? 'text' : 'hidden'); ?>"
        id="override_ajax_url" name="override_ajax_url" class="width-xlarge"
        placeholder="<?php esc_attr_e('Consult support before changing.', 'duplicator-pro'); ?>"
        value="<?php echo esc_attr($ajaxUrlOverride); ?>">
    <p class="description">
        <?php esc_html_e(
            'Backend URL used by the server to call itself and chain build steps (not the browser AJAX URL).
            Auto uses the scheme proven by the loopback test. Only change if instructed by support.',
            'duplicator-pro'
        ); ?>
    </p>
</div>

    <label class="lbl-larger">&nbsp;</label>
    <div class="margin-bottom-1">
        <button type="submit" class="button secondary hollow tiny margin-bottom-0">
            <?php esc_html_e("Save & Test Server Detection", 'duplicator-pro'); ?>
        </button>
    </div>

<div style="margin-top: 30px;"></div>

<h3 class="title">
    <?php esc_html_e("Advanced", 'duplicator-pro'); ?>
</h3>
<hr size="1" />

<label class="lbl-larger">
    <?php esc_html_e('Root path', 'duplicator-pro') ?>
</label>
<div class="margin-bottom-1">
    <input
        type="checkbox"
        name="homepath_as_abspath"
        id="homepath_as_abspath"
        class="margin-0"
        <?php disabled(WpArchiveUtils::isAbspathHomepathEquivalent()); ?>
        <?php checked($global->homepath_as_abspath); ?>
        value="1">
    <label for="homepath_as_abspath">
        <?php
        printf(
            esc_html_x(
                'Use ABSPATH %s as root path.',
                '%s represents the ABSPATH surrounded with bold (<b>) tags',
                'duplicator-pro'
            ),
            '<b>' . esc_html(WpArchiveUtils::getArchiveListPaths('abs')) . '</b>'
        );
        ?>
        <br>
    </label>
    <p class="description">
        <?php
        if (WpArchiveUtils::isAbspathHomepathEquivalent()) {
            esc_html_e('Abspath and home path are equivalent so this option is disabled', 'duplicator-pro');
        } else {
            ?>
            <?php
            printf(
                esc_html_x(
                    'In this installation the default root path is %s.',
                    '%s represents the root path surrounded with bold (<b>) tags',
                    'duplicator-pro'
                ),
                '<b>' . esc_html(SnapIO::safePathUntrailingslashit(get_home_path(), true)) . '</b>'
            ); ?><br>
            <?php
            esc_html_e(
                'The path of the WordPress core is different. Activate this option if you want to consider ABSPATH as root path.',
                'duplicator-pro'
            );
        }
        ?>

    </p>
</div>

<label class="lbl-larger">
    <?php esc_html_e('Scan File Checks', 'duplicator-pro'); ?>
</label>
<div class="margin-bottom-1">
    <input
        type="checkbox"
        name="_skip_archive_scan"
        id="_skip_archive_scan"
        class="margin-0"
        <?php checked($global->skip_archive_scan); ?>>
    <label for="_skip_archive_scan">
        <?php esc_html_e("Skip", 'duplicator-pro') ?>
    </label><br />
    <p class="description">
        <?php
        esc_html_e(
            'If enabled all file checks on scan will be skipped before Backup creation.
            In some cases, this option can be beneficial if the scan process is having issues running or returning errors.',
            'duplicator-pro'
        );
        ?>
    </p>
</div>

<label class="lbl-larger">
    <?php esc_html_e("Password-Protected Access", 'duplicator-pro'); ?>
</label>
<?php if ($prefillAuth) : ?>
    <div class="notice notice-info inline dupli-notice-inline-gap">
        <p>
            <?php
            printf(
                esc_html_x(
                    'Basic authentication detected on this server (user: %s).
                    The fields below have been pre-filled with the detected credentials. Review and click Save Backup Settings to enable.',
                    '%s is the detected username',
                    'duplicator-pro'
                ),
                '<code>' . esc_html($detectedAuthUser) . '</code>'
            );
            ?>
        </p>
    </div>
<?php elseif ($showMismatchWarning) : ?>
    <div class="notice notice-warning inline dupli-notice-inline-gap">
        <p>
            <?php
            printf(
                esc_html_x(
                    'Detected credentials differ from saved ones. Detected user: %1$s, Saved user: %2$s.
                    If builds are failing, consider updating the saved credentials.',
                    '%1$s is the detected username, %2$s is the saved username',
                    'duplicator-pro'
                ),
                '<code>' . esc_html($detectedAuthUser) . '</code>',
                '<code>' . esc_html($savedAuthUser) . '</code>'
            );
            ?>
        </p>
    </div>
<?php endif; ?>
<div class="margin-bottom-1">
    <input
        type="checkbox"
        name="_basic_auth_enabled"
        id="_basic_auth_enabled"
        value="1"
        class="margin-0"
        <?php checked($authChecked); ?>>
    <label for="_basic_auth_enabled">
        <?php esc_html_e("Enabled", 'duplicator-pro') ?>
    </label>
    <i class="fa-solid fa-question-circle fa-sm dark-gray-color"
        data-tooltip-title="<?php esc_attr_e("HTTP Basic authentication", 'duplicator-pro'); ?>"
        data-tooltip="<?php esc_attr_e(
            "When HTTP Basic authentication is applied Duplicator needs to attach the login credentials to each request to the server.
                This is required for the build process to work properly. If you see a browser popup login window when accessing the admin area,
                then basic authentication should be enabled. If you are not sure, please consult your hosting provider.",
            'duplicator-pro'
        ); ?>"></i>
    <div id="dup-basic-auth-login-wrapper">
        <input
            autocomplete="off"
            placeholder="<?php esc_attr_e('User', 'duplicator-pro'); ?>"
            type="text"
            name="basic_auth_user"
            id="basic_auth_user"
            class="margin-0"
            value="<?php echo esc_attr($authUser); ?>"
            <?php disabled($authFieldsDisabled); ?>>
        <span class="dup-password-toggle">
            <input
                autocomplete="off"
                placeholder="<?php esc_attr_e('Password', 'duplicator-pro'); ?>"
                type="password"
                name="basic_auth_password"
                id="basic_auth_password"
                class="margin-0"
                value="<?php echo esc_attr($authPass); ?>"
                <?php disabled($authFieldsDisabled); ?>>
            <button type="button">
                <i class="fas fa-eye fa-sm"></i>
            </button>
        </span>
    </div>
    <p class="description">
        <?php esc_html_e(
            'Enable this function and provide username and password in case of a Backup creation error.
            Essential for making authorized HTTP requests in a server protected folder.',
            'duplicator-pro'
        ); ?>
    </p>
</div>

<script>
    (function($) {
        $('#_basic_auth_enabled').on('change', function() {
            if ($(this).is(':checked')) {
                $('#dup-basic-auth-login-wrapper input').prop('disabled', false);
            } else {
                $('#dup-basic-auth-login-wrapper input').prop('disabled', true);
            }
        });

        $('.ajax_protocol').on('change', function() {
            var field = $('#override_ajax_url');
            if ($(this).val() === 'custom') {
                field.attr('type', 'text').show();
            } else {
                field.attr('type', 'hidden');
            }
        });
    }(window.jQuery || jQuery))
</script>
