<?php
defined('COT_CODE') or die('Wrong URL');

define('COT_AUTH', TRUE);

$return = array(
  'success' => 0,
  'in_stop_list' => 0,
  'user_exists' => 0,
  'error' => 'Укажите email.',
  'redirect' => ''
);

$email = cot_import('user_email', 'P', 'TXT');
$email = mb_strtolower($email);

if(!empty($email) && cot_check_email($email)) {
  if($usr['id'] > 0) {
    $return['error'] = 'Вы уже авторизованы';
  }
  else
  {
    $return['success'] = 1;
    $return['error'] = '';

    require_once cot_incfile('api', 'plug', 'banlist');

    if($return['success'] == 1) {
      $email_exists = (bool)cot::$db->query("SELECT user_id FROM ".cot::$db->users." WHERE user_email = ? LIMIT 1",
              array($email))->fetch();

      if ($email_exists && !$cfg['useremailduplicate']) {
        $return['success'] = 0;
        $return['error'] = $L['aut_emailalreadyindb'];
        $return['user_exists'] = 1;
      }

      if($return['success'] && $a == 'step2') {
        require_once cot_incfile('auth');

      	cot_shield_protect();

      	$ruser = array();

      	/* === Hook for the plugins === */
      	foreach (cot_getextplugins('users.register.add.first') as $pl)
      	{
      		include $pl;
      	}
      	/* ===== */

        $userTypes = array(
          1 => 7, //Покупатель
          2 => 4, //Продавец
          4 => 4, //Покупатель
          7 => 7, //Продавец
        );
        if(cot_plugin_active('usergroupselector')) {
          $_POST['ruserusergroup'] = $userTypes[cot_import('userType', 'P', 'INT')];
          if(!$_POST['ruserusergroup']) $_POST['ruserusergroup'] = 7;
        }

        if($cfg['users']['regnoactivation']) {
          $ruser['user_email'] = $email;
        } else {
          $ruser['user_newemail'] = $email;
        }
      	$ruser['user_name'] = cot_import('user_username','P','TXT', 100, TRUE);
      	$rpassword1 = cot_import('user_password','P','HTM',32);

        $promo = cot_import('user_promo','P','TXT');

      	$user_exists = (bool)cot::$db->query("SELECT user_id FROM ".cot::$db->users." WHERE user_name = ? LIMIT 1",
              array($ruser['user_name']))->fetch();

        $return['success'] = 0;

        if (!preg_match("/^[0-9a-zA-Z\-_]+$/", $ruser['user_name'])) {
          $return['error'] = $L['aut_invalidloginchars'];
      	} elseif (mb_strlen($ruser['user_name']) < 2) {
          $return['error'] = $L['aut_usernametooshort'];
        } elseif (mb_strlen($ruser['user_name']) > 10) {
          $return['error'] = 'Логин должен быть короче 10 символов';
        } elseif (mb_strlen($rpassword1) < 4) {
          $return['error'] = $L['aut_passwordtooshort'];
        } elseif ($user_exists) {
          $return['error'] = $L['aut_usernamealreadyindb'];
        } else {
          $return['success'] = 1;

      		$ruser['user_password'] = $rpassword1;

          require_once cot_incfile('users', 'module');
      		$userid = cot_add_user($ruser);

      		/* === Hook for the plugins === */
      		foreach (cot_getextplugins('users.register.add.done') as $pl)
      		{
      			include $pl;
      		}
      		/* ===== */

      		if ($cfg['users']['regrequireadmin'])
      		{
      			$return['redirect'] = cot_url('message', 'msg=118', '', true);
      		}
      		else
      		{
            $row = $db->query("SELECT user_lostpass,user_token,user_name FROM $db_users WHERE user_id=?",$userid)->fetch();
      			$return['redirect'] = cot_url('login', 'a=check&v='.$row['user_lostpass'].'&token='.$row['user_token'], '', true);
      		}
      	}
      }
    }
  }
}

?>
