telephone-icon+38(063)606-25-95
Main menu
ГлавнаяБлог1С-БитриксШаблонСкрытное создание нового пользователя и авторизация

Скрытное создание нового пользователя и авторизация

Автоматическое создание нового пользователя и авторизация под ним на сайте без необходимости каких-либо действий от посетителя сайта.

if(!$USER->IsAuthorized())
{
  $arUserResult["USER_EMAIL"] = $post['form_text_11'];//e-mail пользователя
  if(strlen($arUserResult["USER_EMAIL"]) > 0)
  {
    $NEW_LOGIN = $arUserResult["USER_EMAIL"];
    $NEW_EMAIL = $arUserResult["USER_EMAIL"];
    $NEW_NAME = "";
    $NEW_LAST_NAME = "";
    $arUserResult["PAYER_NAME"] = $post['form_text_10'];//Ф.И.О. пользователя
    if(strlen($arUserResult["PAYER_NAME"]) > 0)
    {
      $arNames = explode(" ", $arUserResult["PAYER_NAME"]);
      $NEW_NAME = $arNames[1];
      $NEW_LAST_NAME = $arNames[0];
    }
    $pos = strpos($NEW_LOGIN, "@");
    if ($pos !== false) $NEW_LOGIN = substr($NEW_LOGIN, 0, $pos);
    if (strlen($NEW_LOGIN) > 47) $NEW_LOGIN = substr($NEW_LOGIN, 0, 47);
    if (strlen($NEW_LOGIN) < 3) $NEW_LOGIN .= "_";
    if (strlen($NEW_LOGIN) < 3) $NEW_LOGIN .= "_";

    $dbUserLogin = CUser::GetByLogin($NEW_LOGIN);
    if ($arUserLogin = $dbUserLogin->Fetch())
    {
      $newLoginTmp = $NEW_LOGIN;
      $uind = 0;
      do
      {
        $uind++;
        if ($uind == 10)
        {
          $NEW_LOGIN = $arUserResult["USER_EMAIL"];
          $newLoginTmp = $NEW_LOGIN;
        }
        elseif ($uind > 10)
        {
          $NEW_LOGIN = "buyer".time().GetRandomCode(2);
          $newLoginTmp = $NEW_LOGIN;
          break;
        }
        else
        {
          $newLoginTmp = $NEW_LOGIN.$uind;
        }
        $dbUserLogin = CUser::GetByLogin($newLoginTmp);
      }
      while ($arUserLogin = $dbUserLogin->Fetch());
      $NEW_LOGIN = $newLoginTmp;
    }
    $def_group = COption::GetOptionString("main", "new_user_registration_def_group", "");
    if($def_group!="")
    {
      $GROUP_ID = explode(",", $def_group);
      $arPolicy = $USER->GetGroupPolicy($GROUP_ID);
    }
    else
    {
      $arPolicy = $USER->GetGroupPolicy(array());
    }
    $password_min_length = intval($arPolicy["PASSWORD_LENGTH"]);
    if($password_min_length <= 0) $password_min_length = 6;
    $password_chars = array(
      "abcdefghijklnmopqrstuvwxyz",
      "ABCDEFGHIJKLNMOPQRSTUVWXYZ",
      "0123456789",
    );
    if($arPolicy["PASSWORD_PUNCTUATION"] === "Y") $password_chars[] = ",.<>/?;:'\"[]{}\|`~!@#\$%^&*()-_+=";
    $NEW_PASSWORD = $NEW_PASSWORD_CONFIRM = randString($password_min_length+2, $password_chars);

    $user = new CUser;
    $arAuthResult = $user->Add(Array(
      "LOGIN" => $NEW_LOGIN,
      "NAME" => $NEW_NAME,
      "LAST_NAME" => $NEW_LAST_NAME,
      "PASSWORD" => $NEW_PASSWORD,
      "CONFIRM_PASSWORD" => $NEW_PASSWORD_CONFIRM,
      "EMAIL" => $NEW_EMAIL,
      "GROUP_ID" => $GROUP_ID,
      "ACTIVE" => "Y",
      "LID" => SITE_ID,
      )
    );

    if (IntVal($arAuthResult) <= 0)
    {
      $arResult["ERROR"][] = GetMessage("STOF_ERROR_REG").((strlen($user->LAST_ERROR) > 0) ? ": ".$user->LAST_ERROR : "" );
    }
    else
    {
      $USER->Authorize($arAuthResult);
      if ($USER->IsAuthorized())
      {
        if($arParams["SEND_NEW_USER_NOTIFY"] == "Y")
          CUser::SendUserInfo($USER->GetID(), SITE_ID, GetMessage("INFO_REQ"), true);
      }
      else
      {
        $arResult["ERROR"][] = GetMessage("STOF_ERROR_REG_CONFIRM");
      }
    }
  }
  else
    $arResult["ERROR"][] = GetMessage("STOF_ERROR_EMAIL");
}
ВКонтакт Facebook Google Plus Одноклассники Twitter Livejournal Liveinternet Mail.Ru

Возврат к списку