<commit_before><?php

require_once('inc/mysql.php');
require_once('inc/PasswordHash.php');
require_once('inc/permissions.php');

class UserExistsException extends Exception { /* empty */ }
class UserDoesNotExistException extends Exception { /* empty */ }

class Login
{
    public static function TryLogin($username, $password)
    {
        self::StartSession();
        session_regenerate_id(true);
        if (Account::CheckPassword($username, $password)) {
            $account = new Account($username);
            $_SESSION['account'] = $account;
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public static function GetLoggedInUser()
    {
        self::StartSession();
        if (isset($_SESSION['account'])) {
            return $_SESSION['account'];
        } else {
            return FALSE;
        }
    }

    public static function RequireLogin($location)
    {
        $current_user = Login::GetLoggedInUser();
        if ($current_user === FALSE) {
            header("Location: $location");
            die();
        }
    }

    public static function RequireAdmin($location)
    {
        $current_user = Login::GetLoggedInUser();
        if ($current_user === FALSE || !$current_user->isAdmin()) {
            header("Location: $location");
            die();
        }
    }

    public static function LogOut()
    {
        self::StartSession();
        if (ini_get("session.use_cookies")) {
            $params = session_get_cookie_params();
            setcookie(session_name(), '', time() - 42000,
                $params["path"], $params["domain"],
                $params["secure"], $params["httponly"]
            );
        }
        session_destroy();
    }

    private static function StartSession()
    {
        static $started = FALSE;
        if ($started === FALSE) {
            session_start();
            $started = TRUE;
        } 
    }
}

class Account
{
    public static function CreateAccount($username, $password)
    {
        global $DB;
        if ($username == "") {
            throw new Exception('Empty username.');
        }

        if (!self::UserExists($username)) {
            $q = $DB->prepare(
                "INSERT INTO accounts 
                (username, password_hash, is_admin) 
                VALUES (:username, :password_hash, :is_admin)"
            );
            $q->bindValue(':username', $username);
            $q->bindValue(':password_hash', create_hash($password));
            $q->bindValue(':is_admin', 0);
            $q->execute();

        } else {
            throw new Exception('User already exists');
        }
    }

    public static function UserExists($username)
    {
        global $DB;
        $q = $DB->prepare("SELECT id FROM accounts WHERE username = :username");
        $q->bindValue(':username', $username);
        $q->execute();
        $records = $q->fetchAll();
        return !empty($records);
    }

    public static function CheckPassword($username, $password)
    {
        global $DB;

        $q = $DB->prepare("SELECT password_hash FROM accounts WHERE username = :username");
        $q->bindValue(':username', $username);
        $q->execute();
        $row = $q->fetch();
        if ($row === FALSE) {
            return FALSE;
        }

        $correct_hash = $row['password_hash'];
        if (!validate_password($password, $correct_hash)) {
            return FALSE;
        }

        return TRUE;
    }

    public static function GetAllUsers()
    {
        global $DB;

        $q = $DB->prepare("SELECT username FROM accounts");
        $q->execute();
        $all_users = array();
        while (($row = $q->fetch()) !== FALSE) {
            $all_users[] = new Account($row['username']);
        }
        return $all_users;
    }

    private $id;
    private $username;

    function __construct($username)
    {
        global $DB;

        $q = $DB->prepare("SELECT * FROM accounts WHERE username = :username");
        $q->bindValue(':username', $username);
        $q->execute();
        $row = $q->fetch();

        if ($row === FALSE) {
            throw new UserDoesNotExistException('User does not exist.');
        }

        $this->id = $row['id'];
        $this->username = $row['username'];
    }

    function getID()
    {
        return $id;
    }

    function getUsername()
    {
        return $this->username;
    }

    function isAdmin()
    {
        global $DB;

        $q = $DB->prepare("SELECT is_admin FROM accounts WHERE id = :id");
        $q->bindValue(':id', $this->id);
        $q->execute();
        $row = $q->fetch();

        return $row['is_admin'] == 1;
    }

    function setAdmin($is_admin)
    {
        $is_admin = ($is_admin) ? 1 : 0;<commit_msg>Fix the Account class's getID().<commit_after><?php

require_once('inc/mysql.php');
require_once('inc/PasswordHash.php');
require_once('inc/permissions.php');

class UserExistsException extends Exception { /* empty */ }
class UserDoesNotExistException extends Exception { /* empty */ }

class Login
{
    public static function TryLogin($username, $password)
    {
        self::StartSession();
        session_regenerate_id(true);
        if (Account::CheckPassword($username, $password)) {
            $account = new Account($username);
            $_SESSION['account'] = $account;
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public static function GetLoggedInUser()
    {
        self::StartSession();
        if (isset($_SESSION['account'])) {
            return $_SESSION['account'];
        } else {
            return FALSE;
        }
    }

    public static function RequireLogin($location)
    {
        $current_user = Login::GetLoggedInUser();
        if ($current_user === FALSE) {
            header("Location: $location");
            die();
        }
    }

    public static function RequireAdmin($location)
    {
        $current_user = Login::GetLoggedInUser();
        if ($current_user === FALSE || !$current_user->isAdmin()) {
            header("Location: $location");
            die();
        }
    }

    public static function LogOut()
    {
        self::StartSession();
        if (ini_get("session.use_cookies")) {
            $params = session_get_cookie_params();
            setcookie(session_name(), '', time() - 42000,
                $params["path"], $params["domain"],
                $params["secure"], $params["httponly"]
            );
        }
        session_destroy();
    }

    private static function StartSession()
    {
        static $started = FALSE;
        if ($started === FALSE) {
            session_start();
            $started = TRUE;
        } 
    }
}

class Account
{
    public static function CreateAccount($username, $password)
    {
        global $DB;
        if ($username == "") {
            throw new Exception('Empty username.');
        }

        if (!self::UserExists($username)) {
            $q = $DB->prepare(
                "INSERT INTO accounts 
                (username, password_hash, is_admin) 
                VALUES (:username, :password_hash, :is_admin)"
            );
            $q->bindValue(':username', $username);
            $q->bindValue(':password_hash', create_hash($password));
            $q->bindValue(':is_admin', 0);
            $q->execute();

        } else {
            throw new Exception('User already exists');
        }
    }

    public static function UserExists($username)
    {
        global $DB;
        $q = $DB->prepare("SELECT id FROM accounts WHERE username = :username");
        $q->bindValue(':username', $username);
        $q->execute();
        $records = $q->fetchAll();
        return !empty($records);
    }

    public static function CheckPassword($username, $password)
    {
        global $DB;

        $q = $DB->prepare("SELECT password_hash FROM accounts WHERE username = :username");
        $q->bindValue(':username', $username);
        $q->execute();
        $row = $q->fetch();
        if ($row === FALSE) {
            return FALSE;
        }

        $correct_hash = $row['password_hash'];
        if (!validate_password($password, $correct_hash)) {
            return FALSE;
        }

        return TRUE;
    }

    public static function GetAllUsers()
    {
        global $DB;

        $q = $DB->prepare("SELECT username FROM accounts");
        $q->execute();
        $all_users = array();
        while (($row = $q->fetch()) !== FALSE) {
            $all_users[] = new Account($row['username']);
        }
        return $all_users;
    }

    private $id;
    private $username;

    function __construct($username)
    {
        global $DB;

        $q = $DB->prepare("SELECT * FROM accounts WHERE username = :username");
        $q->bindValue(':username', $username);
        $q->execute();
        $row = $q->fetch();

        if ($row === FALSE) {
            throw new UserDoesNotExistException('User does not exist.');
        }

        $this->id = $row['id'];
        $this->username = $row['username'];
    }

    function getID()
    {
        return $this->id;
    }

    function getUsername()
    {
        return $this->username;
    }

    function isAdmin()
    {
        global $DB;

        $q = $DB->prepare("SELECT is_admin FROM accounts WHERE id = :id");
        $q->bindValue(':id', $this->id);
        $q->execute();
        $row = $q->fetch();

        return $row['is_admin'] == 1;
    }

    function setAdmin($is_admin)
    {
        $is_admin = ($is_admin) ? 1 : 0;
{'fraction_non_alphanumeric': 0.14921000313906038, 'fraction_numerical': 0.0018834362247567228, 'mean_word_length': 1.9310027598896045, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, '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': '876927', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2596, 'n_words': 583}