<commit_before><?php
/**
 * \Elabftw\Elabftw\Users
 *
 * @author <NAME> <<EMAIL>>
 * @copyright 2012 <NAME>
 * @see http://www.elabftw.net Official website
 * @license AGPL-3.0
 * @package elabftw
 */
namespace Elabftw\Elabftw;

use PDO;
use Exception;
use Swift_Message;

/**
 * Users
 */
class Users extends Auth
{

    /** flag to check if we need validation or not */
    public $needValidation = false;

    /**
     * Create a new user
     *
     * @param string $email
     * @param string $password
     * @param int $team
     * @param string $firstname
     * @param string $lastname
     * @return bool
     */
    public function create($email, $password, $team, $firstname, $lastname)
    {
        // check for duplicate of email
        if ($this->isDuplicateEmail($email)) {
            throw new Exception(_('Someone is already using that email address!'));
        }

        if (!$this->checkPasswordLength($password)) {
            $error = sprintf(_('Password must contain at least %s characters.'), self::MIN_PASSWORD_LENGTH);
            throw new Exception($error);
        }

        // Put firstname lowercase and first letter uppercase
        $firstname = $this->purifyFirstname($firstname);
        // lastname is uppercase
        $lastname = $this->purifyLastname($lastname);

        // Create salt
        $salt = hash("sha512", uniqid(rand(), true));
        // Create hash
        $passwordHash = hash("sha512", $salt . $_POST['password']);

        // Registration date is stored in epoch
        $registerDate = time();

        // get the group for the new user
        $group = $this->getGroup($team);

        // will new user be validated?
        $validated = $this->getValidated($group);

        $sql = "INSERT INTO users (
            `email`,
            `password`,
            `firstname`,
            `lastname`,
            `team`,
            `usergroup`,
            `salt`,
            `register_date`,
            `validated`,
            `lang`
        ) VALUES (
            :email,
            :password,
            :firstname,
            :lastname,
            :team,
            :usergroup,
            :salt,
            :register_date,
            :validated,
            :lang);";
        $req = $this->pdo->prepare($sql);

        $req->bindParam(':email', $email);
        $req->bindParam(':team', $team);
        $req->bindParam(':salt', $salt);
        $req->bindParam(':password', $passwordHash);
        $req->bindParam(':firstname', $firstname);
        $req->bindParam(':lastname', $lastname);
        $req->bindParam(':register_date', $registerDate);
        $req->bindParam(':validated', $validated);
        $req->bindParam(':usergroup', $group);
        $req->bindValue(':lang', get_config('lang'));

        if (!$req->execute()) {
            throw new Exception('Error inserting user in SQL!');
        }

        if ($validated === '0') {
            $this->alertAdmin($team);
            $this->needValidation = true;
        }

        return true;
    }

    /**
     * Get what will be the value of the validated column in users table
     *<commit_msg>use loose comparator<commit_after><?php
/**
 * \Elabftw\Elabftw\Users
 *
 * @author <NAME> <<EMAIL>>
 * @copyright 2012 <NAME>
 * @see http://www.elabftw.net Official website
 * @license AGPL-3.0
 * @package elabftw
 */
namespace Elabftw\Elabftw;

use PDO;
use Exception;
use Swift_Message;

/**
 * Users
 */
class Users extends Auth
{

    /** flag to check if we need validation or not */
    public $needValidation = false;

    /**
     * Create a new user
     *
     * @param string $email
     * @param string $password
     * @param int $team
     * @param string $firstname
     * @param string $lastname
     * @return bool
     */
    public function create($email, $password, $team, $firstname, $lastname)
    {
        // check for duplicate of email
        if ($this->isDuplicateEmail($email)) {
            throw new Exception(_('Someone is already using that email address!'));
        }

        if (!$this->checkPasswordLength($password)) {
            $error = sprintf(_('Password must contain at least %s characters.'), self::MIN_PASSWORD_LENGTH);
            throw new Exception($error);
        }

        // Put firstname lowercase and first letter uppercase
        $firstname = $this->purifyFirstname($firstname);
        // lastname is uppercase
        $lastname = $this->purifyLastname($lastname);

        // Create salt
        $salt = hash("sha512", uniqid(rand(), true));
        // Create hash
        $passwordHash = hash("sha512", $salt . $_POST['password']);

        // Registration date is stored in epoch
        $registerDate = time();

        // get the group for the new user
        $group = $this->getGroup($team);

        // will new user be validated?
        $validated = $this->getValidated($group);

        $sql = "INSERT INTO users (
            `email`,
            `password`,
            `firstname`,
            `lastname`,
            `team`,
            `usergroup`,
            `salt`,
            `register_date`,
            `validated`,
            `lang`
        ) VALUES (
            :email,
            :password,
            :firstname,
            :lastname,
            :team,
            :usergroup,
            :salt,
            :register_date,
            :validated,
            :lang);";
        $req = $this->pdo->prepare($sql);

        $req->bindParam(':email', $email);
        $req->bindParam(':team', $team);
        $req->bindParam(':salt', $salt);
        $req->bindParam(':password', $<PASSWORD>);
        $req->bindParam(':firstname', $firstname);
        $req->bindParam(':lastname', $lastname);
        $req->bindParam(':register_date', $registerDate);
        $req->bindParam(':validated', $validated);
        $req->bindParam(':usergroup', $group);
        $req->bindValue(':lang', get_config('lang'));

        if (!$req->execute()) {
            throw new Exception('Error inserting user in SQL!');
        }

        if ($validated == '0') {
            $this->alertAdmin($team);
            $this->needValidation = true;
        }

        return true;
    }

    /**
     * Get what will be the value of the validated column in users table
     *
{'fraction_non_alphanumeric': 0.14538216560509554, 'fraction_numerical': 0.004140127388535032, 'mean_word_length': 1.9502113668388914, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1028895', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1771, 'n_words': 454}