<commit_before><?php

namespace Bolt;

use Silex;
use Bolt\Helpers\String;
use Bolt\Translation\Translator as Trans;

/**
 * Class to handle things dealing with users..
 */
class Users
{
    const ANONYMOUS = 0;
    const EDITOR = 2;
    const ADMIN = 4;
    const DEVELOPER = 6;

    public $db;
    public $config;
    public $usertable;
    public $authtokentable;
    public $users;
    public $session;
    public $currentuser;
    public $allowed;
    private $hashStrength;

    public function __construct(Silex\Application $app)
    {
        $this->app = $app;
        $this->db = $app['db'];

        $prefix = $this->app['config']->get('general/database/prefix', "bolt_");

        // Hashstrength has a default of '10', don't allow less than '8'.
        $this->hashStrength = max($this->app['config']->get('general/hash_strength'), 8);

        $this->usertable = $prefix . "users";
        $this->authtokentable = $prefix . "authtoken";
        $this->users = array();
        $this->session = $app['session'];
        $this->remoteIP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : "";

        // Set 'validsession', to see if the current session is valid.
        $this->validsession = $this->checkValidSession();

        $this->allowed = array(
            'dashboard' => self::EDITOR,
            'settings' => self::ADMIN,
            'login' => self::ANONYMOUS,
            'logout' => self::EDITOR,
            'dbcheck' => self::ADMIN,
            'dbupdate' => self::ADMIN,
            'clearcache' => self::ADMIN,
            'prefill' => self::DEVELOPER,
            'users' => self::ADMIN,
            'useredit' => self::ADMIN,
            'useraction' => self::ADMIN,
            'overview' => self::EDITOR,
            'editcontent' => self::EDITOR,
            'editcontent:own' => self::EDITOR,
            'editcontent:all' => self::ADMIN,
            'contentaction' => self::EDITOR,
            'about' => self::EDITOR,
            'extensions' => self::DEVELOPER,
            'files' => self::EDITOR,
            'files:config' => self::DEVELOPER,
            'files:theme' => self::DEVELOPER,
            'files:uploads' => self::ADMIN,
            'translation' => self::DEVELOPER,
            'activitylog' => self::ADMIN,
            'fileedit' => self::ADMIN
        );
    }

    /**
     * Save changes to a user to the database. (re)hashing the password, if needed.
     *
     * @param  array $user
     * @return mixed
     */
    public function saveUser($user)
    {
        // Make an array with the allowed columns. these are the columns that are always present.
        $allowedcolumns = array(
                'id',
                'username',
                'password',
                'email',
                'lastseen',
                'lastip',
                'displayname',
                'enabled',
                'stack',
                'roles',
            );

        // unset columns we don't need to store..
        foreach ($user as $key => $value) {
            if (!in_array($key, $allowedcolumns)) {
                unset($user[$key]);
            }
        }

        if (!empty($user['password']) && $user['password'] != "**<PASSWORD>**") {
            $hasher = new \Hautelook\Phpass\PasswordHash($this->hashStrength, true);
            $user['password'] = $hasher->HashPassword($user['password']);
        } else {
            unset($user['password']);
        }

        // make sure the username is slug-like
        $user['username'] = String::slug($user['username']);

        if (empty($user['lastseen'])) {
            $user['lastseen'] = "1900-01-01";
        }

        if (empty($user['enabled']) && $user['enabled'] !== 0) {
            $user['enabled'] = 1;
        }

        if (empty($user['shadowvalidity'])) {
            $user['shadowvalidity'] = "1900-01-01";
        }

        if (empty($user['throttleduntil'])) {
            $user['throttleduntil'] = "1900-01-01";
        }

        if (empty($user['failedlogins'])) {
            $user['failedlogins'] = 0;
        }

        // Make sure the 'stack' is set.
        if (empty($user['stack'])) {
            $user['stack'] = json_encode(array());
        } elseif (is_array($user['stack'])) {
            $user['stack'] = json_encode($user['stack']);
        }

        // Serialize roles array
        if (empty($user['roles']) || !is_array($user['roles'])) {
            $user['roles'] = '[]';
        } else {
            $user['roles'] = json_encode(array_values(array_unique($user['roles'])));
        }

        // Decide whether to insert a new record, or update an existing one.
        if (empty($user['id'])) {
            unset($user['id']);

            return $this->db->insert($this->usertable, $user);
        } else {<commit_msg>Set a null default for lastseen, shadowvalidity and throttleduntil in saveUser()<commit_after><?php

namespace Bolt;

use Silex;
use Bolt\Helpers\String;
use Bolt\Translation\Translator as Trans;

/**
 * Class to handle things dealing with users..
 */
class Users
{
    const ANONYMOUS = 0;
    const EDITOR = 2;
    const ADMIN = 4;
    const DEVELOPER = 6;

    public $db;
    public $config;
    public $usertable;
    public $authtokentable;
    public $users;
    public $session;
    public $currentuser;
    public $allowed;
    private $hashStrength;

    public function __construct(Silex\Application $app)
    {
        $this->app = $app;
        $this->db = $app['db'];

        $prefix = $this->app['config']->get('general/database/prefix', "bolt_");

        // Hashstrength has a default of '10', don't allow less than '8'.
        $this->hashStrength = max($this->app['config']->get('general/hash_strength'), 8);

        $this->usertable = $prefix . "users";
        $this->authtokentable = $prefix . "authtoken";
        $this->users = array();
        $this->session = $app['session'];
        $this->remoteIP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : "";

        // Set 'validsession', to see if the current session is valid.
        $this->validsession = $this->checkValidSession();

        $this->allowed = array(
            'dashboard' => self::EDITOR,
            'settings' => self::ADMIN,
            'login' => self::ANONYMOUS,
            'logout' => self::EDITOR,
            'dbcheck' => self::ADMIN,
            'dbupdate' => self::ADMIN,
            'clearcache' => self::ADMIN,
            'prefill' => self::DEVELOPER,
            'users' => self::ADMIN,
            'useredit' => self::ADMIN,
            'useraction' => self::ADMIN,
            'overview' => self::EDITOR,
            'editcontent' => self::EDITOR,
            'editcontent:own' => self::EDITOR,
            'editcontent:all' => self::ADMIN,
            'contentaction' => self::EDITOR,
            'about' => self::EDITOR,
            'extensions' => self::DEVELOPER,
            'files' => self::EDITOR,
            'files:config' => self::DEVELOPER,
            'files:theme' => self::DEVELOPER,
            'files:uploads' => self::ADMIN,
            'translation' => self::DEVELOPER,
            'activitylog' => self::ADMIN,
            'fileedit' => self::ADMIN
        );
    }

    /**
     * Save changes to a user to the database. (re)hashing the password, if needed.
     *
     * @param  array $user
     * @return mixed
     */
    public function saveUser($user)
    {
        // Make an array with the allowed columns. these are the columns that are always present.
        $allowedcolumns = array(
                'id',
                'username',
                'password',
                'email',
                'lastseen',
                'lastip',
                'displayname',
                'enabled',
                'stack',
                'roles',
            );

        // unset columns we don't need to store..
        foreach ($user as $key => $value) {
            if (!in_array($key, $allowedcolumns)) {
                unset($user[$key]);
            }
        }

        if (!empty($user['password']) && $user['password'] != "**<PASSWORD>**") {
            $hasher = new \Hautelook\Phpass\PasswordHash($this->hashStrength, true);
            $user['password'] = $hasher->HashPassword($user['password']);
        } else {
            unset($user['password']);
        }

        // make sure the username is slug-like
        $user['username'] = String::slug($user['username']);

        if (empty($user['lastseen'])) {
            $user['lastseen'] = null;
        }

        if (empty($user['enabled']) && $user['enabled'] !== 0) {
            $user['enabled'] = 1;
        }

        if (empty($user['shadowvalidity'])) {
            $user['shadowvalidity'] = null;
        }

        if (empty($user['throttleduntil'])) {
            $user['throttleduntil'] = null;
        }

        if (empty($user['failedlogins'])) {
            $user['failedlogins'] = 0;
        }

        // Make sure the 'stack' is set.
        if (empty($user['stack'])) {
            $user['stack'] = json_encode(array());
        } elseif (is_array($user['stack'])) {
            $user['stack'] = json_encode($user['stack']);
        }

        // Serialize roles array
        if (empty($user['roles']) || !is_array($user['roles'])) {
            $user['roles'] = '[]';
        } else {
            $user['roles'] = json_encode(array_values(array_unique($user['roles'])));
        }

        // Decide whether to insert a new record, or update an existing one.
        if (empty($user['id'])) {
            unset($user['id']);

            return $this->db->insert($this->usertable, $user);
        } else {
{'fraction_non_alphanumeric': 0.16730470770822556, 'fraction_numerical': 0.004759441282979824, 'mean_word_length': 1.9140789870364787, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 97, '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': '6365319', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2817, 'n_words': 612}