<commit_before><?php

require_once 'SwatDB/SwatDB.php';
require_once 'Swat/SwatString.php';
require_once 'Site/SiteResetPasswordMailMessage.php';
require_once 'SwatDB/SwatDBDataObject.php';
require_once 'Site/dataobjects/SiteAccountWrapper.php';

/**
 * A account for a web application
 *
 * SiteAccount objects contain data like name and email that correspond
 * directly to database fields.
 *
 * There are three typical ways to use a SiteAccount object:
 *
 * - Create a new SiteAccount object with a blank constructor. Modify some
 *   properties of the account object and call the SiteAccount::save()
 *   method. A new row is inserted into the database.
 *
 * <code>
 * $new_account = new SiteAccount();
 * $new_account->email = '<EMAIL>';
 * $new_account->setPassword('<PASSWORD>');
 * $new_account->save();
 * </code>
 *
 * - Create a new SiteAccount object with a blank constructor. Call the
 *   {@link SiteAccount::load()} or {@link SiteAccount::loadWithCredentials}
 *   method on the object instance. Modify some properties and call the save()
 *   method. The modified properties are updated in the database.
 *
 * <code>
 * // using regular data-object load() method
 * $account = new SiteAccount();
 * $account->load(123);
 * echo 'Hello ' . $account->fullname;
 * $account->email = '<EMAIL>';
 * $account->save();
 *
 * // using loadWithCredentials()
 * $account = new SiteAccount();
 * if ($account->loadWithCredentials('<EMAIL>', '<PASSWORD>')) {
 *     echo 'Hello ' . $account->fullname;
 *     $account->email = '<EMAIL>';
 *     $account->save();
 * }
 * </code>
 *
 * - Create a new SiteAccount object passing a record set into the
 *   constructor. The first row of the record set will be loaded as the data
 *   for the object instance. Modify some properties and call the
 *   SiteAccount::save() method. The modified properties are updated
 *   in the database.
 *
 * Example usage as an MDB2 wrapper:
 *
 * <code>
 * $sql = '-- select an account here';
 * $account = $db->query($sql, null, true, 'Account');
 * echo 'Hello ' . $account->fullname;
 * $account->email = '<EMAIL>';
 * $account->save();
 * </code>
 *
 * @package   Site
 * @copyright 2005-2007 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 * @see       SiteAccountWrapper
 */
class SiteAccount extends SwatDBDataObject
{
	// {{{ class constants

	/**
	 * Length of salt value used to protect accounts's passwords from
	 * dictionary attacks
	 */
	const PASSWORD_SALT_LENGTH = 16;

	// }}}
	// {{{ public properties

	/**
	 * The database id of this account
	 *
	 * If this property is null or 0 when SiteAccount::save() method is
	 * called, a new account is inserted in the database.
	 *
	 * @var string
	 */
	public $id;

	/**
	 * The full name of this account
	 *
	 * @var string
	 */
	public $fullname;

	/**
	 * The email address of this account
	 *
	 * @var string
	 */
	public $email;

	/**
	 * Hashed version of this account's salted password
	 *
	 * By design, there is no way to get the actual password of this account
	 * through the SiteAccount object.
	 *
	 * @var string
	 *
	 * @see SiteAccount::setPassword()
	 */
	public $password;

	/**
	 * The salt value used to protect this account's password base-64 encoded
	 *
	 * @var string
	 */
	public $password_salt;

	/**
	 * Hashed password tag for reseting the account password
	 *
	 * @var text
	 */
	public $password_tag;

	/**
	 * The date this account was created on
	 *
	 * @var SwatDate
	 */
	public $createdate;

	/**
	 * The last date on which this account was logged into
	 *
	 * @var SwatDate
	 */
	public $last_login;

	// }}}
	// {{{ public function loadWithCredentials()

	/**
	 * Loads an acount from the database with account credentials
	 *
	 * @param string $email the email address of the account.
	 * @param string $password the password of the account.
	 * @param SiteInstance $instance Optional site instance for this account.
	 *                               Used when the site implements
	 *                               {@link SiteMultipleInstanceModule}.
	 *
	 * @return boolean true if the loading was successful and false if it was
	 *                  not.
	 */
	public function loadWithCredentials($email, $password,
		SiteInstance $instance = null)
	{
		$this->checkDB();

		$sql = sprintf('select password_salt from %s
			where lower(email) = lower(%s)',
			$this->table,
			$this->db->quote($email, 'text'));

		if ($instance !== null)
			$sql.= sprintf(' and instance = %s',
				$this->db->quote($instance->id, 'integer'));

		$salt = SwatDB::queryOne($this->db, $sql);

		if ($salt === null)
			return false;

		// salt might be base-64 encoded
		$decoded_salt = base64_decode($salt, true);
		if ($decoded_salt !== false)
			$salt = $decoded_salt;

		$sql = sprintf('select id from %s
			where lower(email) = lower(%s) and password = %s',
			$this->table,
			$this->db->quote($email, 'text'),<commit_msg>mark the password as sensitive so that it doesn't get included in stack traces. also, its 2011. holy crap.<commit_after><?php

require_once 'SwatDB/SwatDB.php';
require_once 'Swat/SwatString.php';
require_once 'Site/SiteResetPasswordMailMessage.php';
require_once 'SwatDB/SwatDBDataObject.php';
require_once 'Site/dataobjects/SiteAccountWrapper.php';

/**
 * A account for a web application
 *
 * SiteAccount objects contain data like name and email that correspond
 * directly to database fields.
 *
 * There are three typical ways to use a SiteAccount object:
 *
 * - Create a new SiteAccount object with a blank constructor. Modify some
 *   properties of the account object and call the SiteAccount::save()
 *   method. A new row is inserted into the database.
 *
 * <code>
 * $new_account = new SiteAccount();
 * $new_account->email = '<EMAIL>';
 * $new_account->setPassword('<PASSWORD>');
 * $new_account->save();
 * </code>
 *
 * - Create a new SiteAccount object with a blank constructor. Call the
 *   {@link SiteAccount::load()} or {@link SiteAccount::loadWithCredentials}
 *   method on the object instance. Modify some properties and call the save()
 *   method. The modified properties are updated in the database.
 *
 * <code>
 * // using regular data-object load() method
 * $account = new SiteAccount();
 * $account->load(123);
 * echo 'Hello ' . $account->fullname;
 * $account->email = '<EMAIL>';
 * $account->save();
 *
 * // using loadWithCredentials()
 * $account = new SiteAccount();
 * if ($account->loadWithCredentials('<EMAIL>', '<PASSWORD>')) {
 *     echo 'Hello ' . $account->fullname;
 *     $account->email = '<EMAIL>';
 *     $account->save();
 * }
 * </code>
 *
 * - Create a new SiteAccount object passing a record set into the
 *   constructor. The first row of the record set will be loaded as the data
 *   for the object instance. Modify some properties and call the
 *   SiteAccount::save() method. The modified properties are updated
 *   in the database.
 *
 * Example usage as an MDB2 wrapper:
 *
 * <code>
 * $sql = '-- select an account here';
 * $account = $db->query($sql, null, true, 'Account');
 * echo 'Hello ' . $account->fullname;
 * $account->email = '<EMAIL>';
 * $account->save();
 * </code>
 *
 * @package   Site
 * @copyright 2005-2011 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 * @see       SiteAccountWrapper
 */
class SiteAccount extends SwatDBDataObject
{
	// {{{ class constants

	/**
	 * Length of salt value used to protect accounts's passwords from
	 * dictionary attacks
	 */
	const PASSWORD_SALT_LENGTH = 16;

	// }}}
	// {{{ public properties

	/**
	 * The database id of this account
	 *
	 * If this property is null or 0 when SiteAccount::save() method is
	 * called, a new account is inserted in the database.
	 *
	 * @var string
	 */
	public $id;

	/**
	 * The full name of this account
	 *
	 * @var string
	 */
	public $fullname;

	/**
	 * The email address of this account
	 *
	 * @var string
	 */
	public $email;

	/**
	 * Hashed version of this account's salted password
	 *
	 * By design, there is no way to get the actual password of this account
	 * through the SiteAccount object.
	 *
	 * @var string
	 *
	 * @see SiteAccount::setPassword()
	 */
	public $password;

	/**
	 * The salt value used to protect this account's password base-64 encoded
	 *
	 * @var string
	 */
	public $password_salt;

	/**
	 * Hashed password tag for reseting the account password
	 *
	 * @var text
	 */
	public $password_tag;

	/**
	 * The date this account was created on
	 *
	 * @var SwatDate
	 */
	public $createdate;

	/**
	 * The last date on which this account was logged into
	 *
	 * @var SwatDate
	 */
	public $last_login;

	// }}}
	// {{{ public function loadWithCredentials()

	/**
	 * Loads an acount from the database with account credentials
	 *
	 * @param string $email the email address of the account.
	 * @param string $password the password of the account.
	 * @param SiteInstance $instance Optional site instance for this account.
	 *                               Used when the site implements
	 *                               {@link SiteMultipleInstanceModule}.
	 *
	 * @return boolean true if the loading was successful and false if it was
	 *                  not.
	 *
	 * @sensitive $password
	 */
	public function loadWithCredentials($email, $password,
		SiteInstance $instance = null)
	{
		$this->checkDB();

		$sql = sprintf('select password_salt from %s
			where lower(email) = lower(%s)',
			$this->table,
			$this->db->quote($email, 'text'));

		if ($instance !== null)
			$sql.= sprintf(' and instance = %s',
				$this->db->quote($instance->id, 'integer'));

		$salt = SwatDB::queryOne($this->db, $sql);

		if ($salt === null)
			return false;

		// salt might be base-64 encoded
		$decoded_salt = base64_decode($salt, true);
		if ($decoded_salt !== false)
			$salt = $decoded_salt;

		$sql = sprintf('select id from %s
			where lower(email) = lower(%s) and password = %s',
			$this->table,
			$this->db->quote($email, 'text'),
{'fraction_non_alphanumeric': 0.13077846708024815, 'fraction_numerical': 0.005003001801080648, 'mean_word_length': 3.1715358931552586, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2340679', 'n_tokens_mistral': 3157, 'n_tokens_neox': 2900, 'n_words': 1033}