<commit_before><?php

    include('includes/encrypt.class.php');
    include('includes/swift/lib/swift_required.php');
    include('configuration.php');
    include('includes/klogger.class.php');

    class Authenticate
    {
        //The database settings and table structures are inherited from the Configuration.php file.
        private $db_host = DB_HOST;
        private $db_username = DB_USERNAME;
        private $db_password = <PASSWORD>;
        private $db_name = DB_NAME;
        private $user_table = TABLE_WITH_USERS;
        private $email_col = COLUMN_WITH_EMAILS;
        private $password_col = COLUMN_WITH_PASSWORD_HASHES;
        private $activated_col = COLUMN_CONFIRMING_ACCOUNT_ACTIVATION;
        private $emailed_hash_col = COLUMN_WITH_EMAILED_HASHES;
        private $encryption_rounds = ENCRYPTION_ROUNDS;
        private $mysqli;
        private $log;

        //Start a database connection.
        public function __construct()
        {
            $this->log = new Klogger(LOG_DIRECTORY, 7);

            $this->mysqli = new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name);

            if($this->mysqli->connect_error)
            {
                $this->log->logFatal('The database connection failed', $this->mysqli->connect_error);
            }
        }

        public function create_user($email, $password)
        {
            if(!($email && $password))
            {
                return FALSE;
            }

            if($this->is_registered($email))
            {
                return FALSE;
            }

            $password = $this->encrypt_password($password);

            //To be sent in the email confirmation link.
            $random_hash = $this->generate_random_hash();

            //Add the email, password, and random hash to the database. The $activated_col should be 0 by default, and 1 once the emailed link is clicked.
            $stmt = $this->mysqli->prepare("INSERT INTO `$this->user_table`(`$this->email_col`, `$this->password_col`, `$this->emailed_hash_col`, `$this->activated_col`) VALUES(?, ?, ?, 0)");
            $stmt->bind_param('sss', $email, $password, $random_hash);
            $stmt->execute();

            if($this->mysqli->error)
            {
                $this->log->logFatal('Failed to create user', $this->mysqli->error);
                return FALSE;
            }

            //Generate the email.
            $mail = $this->send_email($email, 'registration', $random_hash);

            //Send the email.
            if($mail)
            {
                return TRUE;
            }
            else
            {
                //Rollback the database insertion.
                $this->delete_user($email, $password);

                return FALSE;
            }
        }

        //This function will check if a user exists in the database.
        public function is_registered($email)
        {
            if(!$email)
            {
                return FALSE;<commit_msg>Send another email if a user tries to create_user() multiple times, instead of returning FALSE.<commit_after><?php

    include('includes/encrypt.class.php');
    include('includes/swift/lib/swift_required.php');
    include('configuration.php');
    include('includes/klogger.class.php');

    class Authenticate
    {
        //The database settings and table structures are inherited from the Configuration.php file.
        private $db_host = DB_HOST;
        private $db_username = DB_USERNAME;
        private $db_password = <PASSWORD>;
        private $db_name = DB_NAME;
        private $user_table = TABLE_WITH_USERS;
        private $email_col = COLUMN_WITH_EMAILS;
        private $password_col = COLUMN_WITH_PASSWORD_HASHES;
        private $activated_col = COLUMN_CONFIRMING_ACCOUNT_ACTIVATION;
        private $emailed_hash_col = COLUMN_WITH_EMAILED_HASHES;
        private $encryption_rounds = ENCRYPTION_ROUNDS;
        private $mysqli;
        private $log;

        //Start a database connection.
        public function __construct()
        {
            $this->log = new Klogger(LOG_DIRECTORY, 7);

            $this->mysqli = new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name);

            if($this->mysqli->connect_error)
            {
                $this->log->logFatal('The database connection failed', $this->mysqli->connect_error);
            }
        }

        public function create_user($email, $password)
        {
            if(!($email && $password))
            {
                return FALSE;
            }

            //If the user has already registered, and is trying again, it is possible that they lost the original email and is simply requesting a new one.
            //In this case, we need to make sure the $emailed_hash we email to them is the same as the first one.
            if($this->is_registered($email))
            {

                $stmt = $this->mysqli->prepare("SELECT `$this->emailed_hash_col`, `$this->activated_col` FROM `$this->user_table` WHERE `$this->email_col` = ?");
                $stmt->bind_param('s', $email);
                $stmt->execute();
                $stmt->bind_result($random_hash, $activated);
                $stmt->fetch();

                //If the user has already managed to activate their account, why would they try registering again?
                if($activated)
                {
                    return FALSE;
                }
            }
            else
            {
                $password = $<PASSWORD>($password);

                //To be sent in the email confirmation link.
                $random_hash = $this->generate_random_hash();

                //Add the email, password, and random hash to the database. The $activated_col should be 0 by default, and 1 once the emailed link is clicked.
                $stmt = $this->mysqli->prepare("INSERT INTO `$this->user_table`(`$this->email_col`, `$this->password_col`, `$this->emailed_hash_col`, `$this->activated_col`) VALUES(?, ?, ?, 0)");
                $stmt->bind_param('sss', $email, $password, $random_hash);
                $stmt->execute();
            }

            if($this->mysqli->error)
            {
                $this->log->logFatal('Failed to create user', $this->mysqli->error);
                return FALSE;
            }

            //Generate the email.
            $mail = $this->send_email($email, 'registration', $random_hash);

            //Send the email.
            if($mail)
            {
                return TRUE;
            }
            else
            {
                //Rollback the database insertion.
                $this->delete_user($email, $password);

                return FALSE;
            }
        }

        //This function will check if a user exists in the database.
        public function is_registered($email)
        {
            if(!$email)
            {
                return FALSE;
{'fraction_non_alphanumeric': 0.11259323006310958, 'fraction_numerical': 0.0011474469305794606, 'mean_word_length': 1.8207928802588997, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3676751', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1849, 'n_words': 462}