public function Register($USER_LOGIN, $USER_NAME, $USER_LAST_NAME, $USER_PASSWORD, $USER_CONFIRM_PASSWORD, $USER_EMAIL, $SITE_ID = false, $captcha_word = '', $captcha_sid = 0, $bSkipConfirm = false, $USER_PHONE_NUMBER = '')
{
/**
* @global CMain $APPLICATION
* @global CUserTypeManager $USER_FIELD_MANAGER
*/
global $APPLICATION, $DB, $USER_FIELD_MANAGER;
$APPLICATION->ResetException();
if (defined("ADMIN_SECTION") && ADMIN_SECTION === true && $SITE_ID !== false)
{
$APPLICATION->ThrowException(GetMessage("MAIN_FUNCTION_REGISTER_NA_INADMIN"));
return ["MESSAGE" => GetMessage("MAIN_FUNCTION_REGISTER_NA_INADMIN"), "TYPE" => "ERROR"];
}
$strError = '';
if (Option::get('main', 'captcha_registration', 'N') == 'Y')
{
if (!($APPLICATION->CaptchaCheckCode($captcha_word, $captcha_sid)))
{
$strError .= GetMessage("MAIN_FUNCTION_REGISTER_CAPTCHA") . "
";
}
}
if ($strError)
{
if (Option::get('main', 'event_log_register_fail', 'N') === 'Y')
{
CEventLog::Log('SECURITY', 'USER_REGISTER_FAIL', 'main', false, $strError);
}
$APPLICATION->ThrowException($strError);
return ["MESSAGE" => $strError, "TYPE" => "ERROR"];
}
if ($SITE_ID === false)
{
$SITE_ID = SITE_ID;
}
$bConfirmReq = !$bSkipConfirm && (Option::get('main', 'new_user_registration_email_confirmation', 'N') == 'Y' && Option::get('main', 'new_user_email_required', 'Y') != 'N');
$phoneRegistration = (Option::get('main', 'new_user_phone_auth', 'N') == 'Y');
$phoneRequired = ($phoneRegistration && Option::get('main', 'new_user_phone_required', 'N') == 'Y');
$active = ($bConfirmReq || $phoneRequired ? 'N' : 'Y');
$arFields = [
"LOGIN" => $USER_LOGIN,
"NAME" => $USER_NAME,
"LAST_NAME" => $USER_LAST_NAME,
"PASSWORD" => $USER_PASSWORD,
"CHECKWORD" => Random::getString(32),
"~CHECKWORD_TIME" => $DB->CurrentTimeFunction(),
"CONFIRM_PASSWORD" => $USER_CONFIRM_PASSWORD,
"EMAIL" => $USER_EMAIL,
"PHONE_NUMBER" => $USER_PHONE_NUMBER,
"ACTIVE" => $active,
"CONFIRM_CODE" => ($bConfirmReq ? Random::getString(8, true) : ''),
"SITE_ID" => $SITE_ID,
"LANGUAGE_ID" => LANGUAGE_ID,
"USER_IP" => $_SERVER["REMOTE_ADDR"],
"USER_HOST" => @gethostbyaddr($_SERVER["REMOTE_ADDR"]),
];
$USER_FIELD_MANAGER->EditFormAddFields("USER", $arFields);
$def_group = Option::get('main', 'new_user_registration_def_group');
if ($def_group != '')
{
$arFields["GROUP_ID"] = explode(",", $def_group);
}
$bOk = true;
$result_message = true;
foreach (GetModuleEvents('main', 'OnBeforeUserRegister', true) as $arEvent)
{
if (ExecuteModuleEventEx($arEvent, [&$arFields]) === false)
{
if ($err = $APPLICATION->GetException())
{
$result_message = ["MESSAGE" => $err->GetString() . "
", "TYPE" => "ERROR"];
}
else
{
$APPLICATION->ThrowException("Unknown error");
$result_message = ["MESSAGE" => "Unknown error" . "
", "TYPE" => "ERROR"];
}
$bOk = false;
break;
}
}
$ID = false;
$phoneReg = false;
if ($bOk)
{
if ($arFields["SITE_ID"] === false)
{
$arFields["SITE_ID"] = CSite::GetDefSite();
}
$arFields["LID"] = $arFields["SITE_ID"];
if ($ID = $this->Add($arFields))
{
if ($phoneRegistration && $arFields["PHONE_NUMBER"] != '')
{
$phoneReg = true;
//added the phone number for the user, now sending a confirmation SMS
[$code, $phoneNumber] = static::GeneratePhoneCode($ID);
$sms = new MainSmsEvent(
"SMS_USER_CONFIRM_NUMBER",
[
"USER_PHONE" => $phoneNumber,
"CODE" => $code,
]
);
$sms->setSite($arFields["SITE_ID"]);
$smsResult = $sms->send(true);
$signedData = MainControllerPhoneAuth::signData(['phoneNumber' => $phoneNumber]);
if ($smsResult->isSuccess())
{
$result_message = [
"MESSAGE" => GetMessage("main_register_sms_sent"),
"TYPE" => "OK",
"SIGNED_DATA" => $signedData,
"ID" => $ID,
];
}
else
{
$result_message = [
"MESSAGE" => $smsResult->getErrorMessages(),
"TYPE" => "ERROR",
"SIGNED_DATA" => $signedData,
"ID" => $ID,
];
}
}
else
{
$result_message = [
"MESSAGE" => GetMessage("USER_REGISTER_OK"),
"TYPE" => "OK",
"ID" => $ID,
];
}
$arFields["USER_ID"] = $ID;
$arEventFields = $arFields;
unset($arEventFields["PASSWORD"]);
unset($arEventFields["CONFIRM_PASSWORD"]);
unset($arEventFields["~CHECKWORD_TIME"]);
$event = new CEvent;
$event->SendImmediate("NEW_USER", $arEventFields["SITE_ID"], $arEventFields);
if ($bConfirmReq)
{
$event->SendImmediate("NEW_USER_CONFIRM", $arEventFields["SITE_ID"], $arEventFields);
}
}
else
{
$APPLICATION->ThrowException($this->LAST_ERROR);
$result_message = ["MESSAGE" => $this->LAST_ERROR, "TYPE" => "ERROR"];
}
}
if (is_array($result_message))
{
if ($result_message["TYPE"] == "OK")
{
if (Option::get('main', 'event_log_register', 'N') === 'Y')
{
$res_log["user"] = ($USER_NAME != '' || $USER_LAST_NAME != '') ? trim($USER_NAME . ' ' . $USER_LAST_NAME) : $USER_LOGIN;
CEventLog::Log('SECURITY', 'USER_REGISTER', 'main', $ID, serialize($res_log));
}
}
else
{
if (Option::get('main', 'event_log_register_fail', 'N') === 'Y')
{
CEventLog::Log('SECURITY', 'USER_REGISTER_FAIL', 'main', $ID, $result_message['MESSAGE']);
}
}
}
//authorize succesfully registered user, except email or phone confirmation is required
$isAuthorize = false;
if ($ID !== false && $arFields["ACTIVE"] === 'Y' && $phoneReg === false)
{
$isAuthorize = $this->Authorize($ID);
}
$agreementId = (int)Option::get('main', 'new_user_agreement');
if ($agreementId && $isAuthorize)
{
$agreementObject = new MainUserConsentAgreement($agreementId);
if ($agreementObject->isExist() && $agreementObject->isActive() && $_REQUEST["USER_AGREEMENT"] == 'Y')
{
MainUserConsentConsent::addByContext($agreementId, "main/reg", "register");
}
}
$arFields["RESULT_MESSAGE"] = $result_message;
foreach (GetModuleEvents('main', 'OnAfterUserRegister', true) as $arEvent)
{
ExecuteModuleEventEx($arEvent, [&$arFields]);
}
return $arFields["RESULT_MESSAGE"];
}