<commit_before><?php

namespace ApiFramework;

/**
 * User
 *
 * Manages users and sessions
 * @version 1.0
 * @package User
*/
class User extends BaseModule
{

    private static $user = 'guest';
    private static $password = '<PASSWORD>';
    private static $sessionsFolder = 'storage/sessions/';

    /**
     * Login a user.
     *
     * @param string $key
     * @return mixed
     */
    public function login ()
    {
        // Get credentials
        $user = Request::input('user');
        $password = Request::input('password');

        // Check credentials
        if (!self::isValid($user, $password)) {
            return self::error(401, 'Invalid user');
        }

        // Store session file
        $token = md5(time());
        file_put_contents(self::getSessionsPath() . $token . '.json', json_encode(['user' => $user]));

        // Set session cookies
        setcookie('user', $user, time() + 3600, '/');
        setcookie('token', $token, time() + 3600, '/');

        // Return token
        return ['user' => $user, 'token' => $token];
    }

    /**
     * Retrieve the logged user info.
     *
     * @param string $key
     * @return mixed
     */
    public static function info ($token)
    {
        // Get token
        $token = ($token)?: Request::token();

        // Check session file
        $sessionFile = self::getSessionsPath() . $token . '.json';
        if (!file_exists($sessionFile)) {
            return self::error(401, 'Invalid token');
        }

        // Get session data
        $session = json_decode(file_get_contents(self::getSessionsPath() . $token . '.json'), true);

        // Check session user
        if (!$session['user']) {
            return self::error(401, 'Invalid token');
        }

        // Return user info
        return ['user' => $session['user'], 'token' => $token];
    }

    /**
     * Logout a user.
     *
     * @param string $key
     * @return mixed
     */
    public function logout ($token)
    {
        // Get session
        $token = ($token)?: Request::token();
        $session = json_decode(file_get_contents(self::getSessionsPath() . $token . '.json'), true);

        // Check session
        if (!$session['user']) {
            return self::error(401, 'Invalid token');
        }

        // Delete session file
        unlink(self::getSessionsPath() . $token . '.json');

        // Delete session cookies
        setcookie('user', '', time() - 3600, '/');
        setcookie('token', '', time() - 3600, '/');

        // Return response
        return true;
    }

    /**
     * Validates a user - password pair.
     *
     * @param string $key
     * @return mixed
     */
    public function isValid ($user, $password)
    {
        if (($user !== self::$user) || (md5($password) !== self::$password)) {
            return false;
        }
        return true;
    }

    /**
     * Get the session's folder path
     *
     * @return string
     */
    private static function getSessionsPath ()
    {
        return CONFIG_DOCUMENT_ROOT . self::$sessionsFolder;
    }

}<commit_msg>Updated User module with configurable usernameColumn and passwordColumn<commit_after><?php

namespace ApiFramework;

/**
 * User
 *
 * Manages users and sessions
 * @version 1.0
 * @package User
*/
class User extends BaseModule
{
    protected $table = 'users';
    protected $validFilters = ['email' => 'users.email'];
    private $sessionsFolder = 'storage/sessions/';
    private $usernameColumn = 'email';
    private $passwordColumn = 'password';

    /**
     * Login a user.
     *
     * @param string $key
     * @return mixed
     */
    public function login ()
    {
        // Get credentials
        $user = Request::input('user');
        $password = Request::input('password');

        // Check credentials
        if (!$this->isValid($user, $password)) {
            return $this->error(401, 'Invalid user');
        }

        // Store session file
        $token = md5(time());
        file_put_contents($this->getSessionsPath() . $token . '.json', json_encode(['user' => $user]));

        // Set session cookies
        setcookie('user', $user, time() + 3600, '/');
        setcookie('token', $token, time() + 3600, '/');

        // Return token
        return ['user' => $user, 'token' => $token];
    }

    /**
     * Retrieve the logged user info.
     *
     * @param string $key
     * @return mixed
     */
    public function info ($token)
    {
        // Get token
        $token = ($token)?: Request::token();

        // Check session file
        $sessionFile = $this->getSessionsPath() . $token . '.json';
        if (!file_exists($sessionFile)) {
            return $this->error(401, 'Invalid token');
        }

        // Get session data
        $session = json_decode(file_get_contents($this->getSessionsPath() . $token . '.json'), true);

        // Check session user
        if (!$session['user']) {
            return $this->error(401, 'Invalid token');
        }

        // Return user info
        return ['user' => $session['user'], 'token' => $token];
    }

    /**
     * Logout a user.
     *
     * @param string $key
     * @return mixed
     */
    public function logout ($token)
    {
        // Get session
        $token = ($token)?: Request::token();
        $session = json_decode(file_get_contents($this->getSessionsPath() . $token . '.json'), true);

        // Check session
        if (!$session['user']) {
            return $this->error(401, 'Invalid token');
        }

        // Delete session file
        unlink($this->getSessionsPath() . $token . '.json');

        // Delete session cookies
        setcookie('user', '', time() - 3600, '/');
        setcookie('token', '', time() - 3600, '/');

        // Return response
        return true;
    }

    /**
     * Validates a user - password pair.
     *
     * @param string $key
     * @return mixed
     */
    public function isValid ($user, $password)
    {
        $userData = $this->where($this->usernameColumn, $user)->first();
        if (!$userData) {
            return false;
        }
        return password_verify($password, $userData[$this->passwordColumn]);
    }

    /**
     * Get the session's folder path
     *
     * @return string
     */
    public function getSessionsPath ()
    {
        return CONFIG_DOCUMENT_ROOT . $this->sessionsFolder;
    }

}
{'fraction_non_alphanumeric': 0.1587748085638381, 'fraction_numerical': 0.009845288326300985, 'mean_word_length': 1.9479502533394748, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 30, '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': '5598306', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1799, 'n_words': 464}