<?php

ob_start();

$NETCAT_FOLDER = join(strstr(__FILE__, "/") ? "/" : "\\", array_slice(preg_split("/[\/\\\]+/", __FILE__), 0, -4)).( strstr(__FILE__, "/") ? "/" : "\\" );
include_once ($NETCAT_FOLDER."vars.inc.php");

require_once ($ROOT_FOLDER."connect_io.php");
require_once ($ADMIN_FOLDER."admin.inc.php");

$url = $parsed_url['path'];
$url = str_replace($nc_core->SUB_FOLDER, '', $url);

$current_catalogue = $nc_core->catalogue->get_by_host_name($_SERVER['HTTP_HOST']);

if (!$sub) {
    $SQL = "
        SELECT sub.Catalogue_ID,
               sub.Subdivision_ID
            FROM Subdivision AS sub
                WHERE sub.Catalogue_ID = " . +$current_catalogue['Catalogue_ID'] . "
                  AND sub.ExternalURL LIKE '%" . $db->escape($url) . "%'";
    if ($result = $db->get_row($SQL, ARRAY_N)) list($catalogue, $sub) = $result;
}

require_once ($INCLUDE_FOLDER."index.php");

$id = intval($id);
$code = $db->escape($code);
$nc_user_confirm = 0;

if ($id && $code) {
    $IsChecked = ( $nc_core->get_settings('premoderation', 'auth') ) ? 0 : 1;

    // подтверждение пользователя
    $res = $db->query("UPDATE `User`
		SET `Confirmed` = '1', `RegistrationCode` = ''".($IsChecked ? ", `Checked` = '".$IsChecked."'" : "")."
		WHERE `RegistrationCode` = '".$code."' AND `User_ID` = '".$id."'");
    $nc_user_confirm = $db->rows_affected;
    unset($res);

    // если пользователь включен и стоит опция "авторизация после подтверждения"
    if ($nc_user_confirm && $IsChecked && $nc_core->get_settings('autoauthorize', 'auth')) {
        Authorize($id, 'authorize');
    }
}

if ($nc_user_confirm) { // успешное подтверждение
    $CheckActionTemplate = $db->get_var("SELECT `CheckActionTemplate` FROM `Class`
                                       WHERE `System_Table_ID`=3 AND `ClassTemplate` = 0 ");

    if ($CheckActionTemplate) eval("echo \"".$CheckActionTemplate."\";");

    if ($nc_core->get_settings('confirm_after_mail', 'auth')) {
        $fromname = $nc_core->get_settings('SpamFromName');
        $fromemail = $nc_core->get_settings('SpamFromEmail');
        $EmailField = $nc_core->get_settings('UserEmailField');

        $mail_info = $nc_auth->get_confirm_after_mail($id);

        $mail_body = $nc_core->mail->attachment_attach($mail_info['body'], 'auth_confirm_after_' . $catalogue);
        $nc_core->mail->mailbody(strip_tags($mail_body), $mail_info['html'] ? $mail_body : '');
        $nc_core->mail->send($mail_info['user_email'], $fromemail, $fromemail, $mail_info['subject'], $fromname);
    }

    $after_msg = output_auth_field('confirm_after', $File_Mode);
    echo $after_msg ? $after_msg : "<div id='confirm_after'>".NETCAT_MODULE_AUTH_REG_OK."</div>";
    
} elseif (!$id || !$code) { // неправильная ссылка
    $warnText = NETCAT_MODULE_AUTH_REG_INVALIDLINK;
} else { // пользователь не найден
    $warnText = NETCAT_MODULE_AUTH_REG_ERROR;
}

if ($warnText) {
    $warn_msg = output_auth_field('confirm_after_warn', $File_Mode);
    echo $warn_msg ? $warn_msg : "<div id='confirm_warn'>$warnText</div>";
}

$nc_result_msg = ob_get_clean();

if ($File_Mode) {
    require_once $INCLUDE_FOLDER.'index_fs.inc.php';
    
    echo $template_header;
    echo $nc_result_msg;
    echo $template_footer;
} else {
    eval("echo \"".$template_header."\";");
    echo $nc_result_msg;
    eval("echo \"".$template_footer."\";");
}

function output_auth_field($field, $file_mode) {
    global $warnText;
    ob_start();

    if ($file_mode) {
        $auth_view = new nc_module_view();
        $auth_view->load('auth', nc_core()->get_interface());
        $field_path = $auth_view->get_field_path($field);
        if (file_get_contents($field_path)) {
            include($field_path);
        }
    }
    else {
        eval("echo \"" . nc_core()->get_settings($field, 'auth') . "\";");
    }

    return ob_get_clean();
}