<commit_before><?php
class User {
    private static $instance;
    private $dbh;

    private function __construct() {
	$this->dbh = Database::instance()->connection();
    }

    public static function instance() {
	if (!isset(self::$instance)) {
	    $class = __CLASS__;
	    self::$instance = new $class;
	}

	return self::$instance;
    }

    public function add($user) {
	$query = "insert into users(username, fullname, password, email, "
	    . "reg_date) values(?, ?, ?, ?, now())";
	$sth = $this->dbh->prepare($query);
	$sth->execute(array($user['username'], $user['fullname'],
	    $user['password'], $user['email']));
    }

    public function exists($user) {
	$query = "select count(*) from users where username = ?";
	$sth = $this->dbh->prepare($query);
	$sth->execute(array($user));
	return $sth->fetchColumn() > 0;
    }

    public function validate($user) {
	$ret = array();
	if ($user['fullname'] == '') {
	    $ret['valid'] = false;
	    $ret['msg'] = "You did not enter your name";
	} elseif ($user['username'] == '') {
	    $ret['valid'] = false;
	    $ret['msg'] = 'No username specified';
	} elseif ($this->exists($user['username'])) {
	    $ret['valid'] = false;
	    $ret['msg'] = "A user with that username already exists";
	} elseif ($user['password'] == '') {
	    $ret['valid'] = false;
	    $ret['msg'] = 'You did not set a password';
	} elseif ($user['password'] != $user['password2']) {
	    $ret['valid'] = false;
	    $ret['msg'] = "The passwords do not match";
	} elseif ($user['email'] == '') {
	    $ret['valid'] = false;
	    $ret['msg'] = 'You did not specify your email';
	} else {
	    $ret['valid'] = true;
	    $ret['msg'] = 'Valid';
	}

	return $ret;
    }
}
?><commit_msg>Created private method User::sanitize() and call it from User::add() and User::validate() to ensure that input data is clean before used.<commit_after><?php
class User {
    private static $instance;
    private $dbh;

    private function __construct() {
	$this->dbh = Database::instance()->connection();
    }

    public static function instance() {
	if (!isset(self::$instance)) {
	    $class = __CLASS__;
	    self::$instance = new $class;
	}

	return self::$instance;
    }

    public function add($user) {
	$this->sanitize($user);

	$query = "insert into users(username, fullname, password, email, "
	    . "reg_date) values(?, ?, ?, ?, now())";
	$sth = $this->dbh->prepare($query);
	$sth->execute(array($user['username'], $user['fullname'],
	    $user['password'], $user['email']));
    }

    public function exists($user) {
	$query = "select count(*) from users where username = ?";
	$sth = $this->dbh->prepare($query);
	$sth->execute(array($user));
	return $sth->fetchColumn() > 0;
    }

    public function validate($user) {
	$this->sanitize($user);

	$ret = array();
	if ($user['fullname'] == '') {
	    $ret['valid'] = false;
	    $ret['msg'] = "You did not enter your name";
	} elseif ($user['username'] == '') {
	    $ret['valid'] = false;
	    $ret['msg'] = 'No username specified';
	} elseif ($this->exists($user['username'])) {
	    $ret['valid'] = false;
	    $ret['msg'] = "A user with that username already exists";
	} elseif ($user['password'] == '') {
	    $ret['valid'] = false;
	    $ret['msg'] = 'You did not set a password';
	} elseif ($user['password'] != $user['<PASSWORD>']) {
	    $ret['valid'] = false;
	    $ret['msg'] = "The passwords do not match";
	} elseif ($user['email'] == '') {
	    $ret['valid'] = false;
	    $ret['msg'] = 'You did not specify your email';
	} else {
	    $ret['valid'] = true;
	    $ret['msg'] = 'Valid';
	}

	return $ret;
    }

    private function sanitize(&$user) {
	$user['fullname'] = trim($user['fullname']);
	$user['username'] = trim($user['username']);
	$user['email'] = trim($user['email']);
    }
}
?>
{'fraction_non_alphanumeric': 0.220611491829204, 'fraction_numerical': 0.0007907221929362151, 'mean_word_length': 3.645042839657283, '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': '4117063', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1258, 'n_words': 304}