<commit_before><?php

/*
 *
 * Distributed under the terms of the license described in COPYING
 *
 */

class_exists('CApi') or die();

CApi::Inc('common.plugins.change-password');

class CCustomChangePasswordPlugin extends AApiChangePasswordPlugin
{
	/**
	 * @param CApiPluginManager $oPluginManager
	 */
	public function __construct(CApiPluginManager $oPluginManager)
	{
		parent::__construct('1.0', $oPluginManager);
	}

	/**
	 * @param CAccount $oAccount
	 * @return bool
	 */
	public function validateIfAccountCanChangePassword($oAccount)
	{
		$bResult = false;
		if ($oAccount instanceof CAccount)
		{
			$bResult = true;
		}

		return $bResult;
	}

	/**
	 * @param CAccount $oAccount
	 * @return bool
	 */
	public function ChangePasswordProcess($oAccount)
	{
		$bResult = false;
		if (0 < strlen($oAccount->PreviousMailPassword) &&
			$oAccount->PreviousMailPassword !== $oAccount->IncomingMailPassword)
		{

			$aPostfixadmin = [
				"host" => CApi::GetConf('plugins.postfixadmin-change-password.config.host', '127.0.0.1'),
				"dbuser" => CApi::GetConf('plugins.postfixadmin-change-password.config.dbuser', 'root'),
				"dbpassword" => CApi::GetConf('plugins.postfixadmin-change-password.config.dbpassword', ''),
				"dbname" => CApi::GetConf('plugins.postfixadmin-change-password.config.dbname', 'postfixadmin'),
			];

			//connect to ispconfig database
			$mysqlcon=mysqli_connect($aPostfixadmin['host'],$aPostfixadmin['dbuser'],$aPostfixadmin['dbpassword'],$aPostfixadmin['dbname']);

		 	if($mysqlcon){
				//check old pass is correct
				$username = $oAccount->IncomingMailLogin;
				$password = $oAccount->PreviousMailPassword;
				$new_password = $oAccount->IncomingMailPassword;

				$sql = "SELECT * FROM mailbox WHERE username='$username'";
				$result = mysqli_query($mysqlcon,$sql);
				$mailuser = mysqli_fetch_array($result);

				$saved_password = $mailuser['password'];

				// get salt
				$parts = explode("\$", $saved_password);
				$salt = "";
				if($parts[1] == '1' && count($parts) == 4) {
					$salt = $parts[2];
				}

				// load md5crypt
				include_once __DIR__.'/md5crypt.php';

				//* Check if mailuser password is correct
				if(md5crypt(stripslashes($password), $salt) == $saved_password) {
					$mailuser_id = $mailuser['username'];

					$new_password = <PASSWORD>($<PASSWORD>);
					$sql = "UPDATE mailbox SET password='$<PASSWORD>' WHERE username='$mailuser_id'";
					$result = mysqli_query($mysqlcon,$sql);

					if (!$result){
						//password update error
						throw new CApiManagerException(Errs::UserManager_AccountNewPasswordUpdateError);
					}
				} else {
					//old and new passwords dont match
					throw new CApiManagerException(Errs::UserManager_AccountOldPasswordNotCorrect);
				}
				//disconnect from database<commit_msg>Implemented adding log after password change<commit_after><?php

/*
 *
 * Distributed under the terms of the license described in COPYING
 *
 */

class_exists('CApi') or die();

CApi::Inc('common.plugins.change-password');

class CCustomChangePasswordPlugin extends AApiChangePasswordPlugin
{
	/**
	 * @param CApiPluginManager $oPluginManager
	 */
	public function __construct(CApiPluginManager $oPluginManager)
	{
		parent::__construct('1.1', $oPluginManager);
	}

	/**
	 * @param CAccount $oAccount
	 * @return bool
	 */
	public function validateIfAccountCanChangePassword($oAccount)
	{
		$bResult = false;
		if ($oAccount instanceof CAccount)
		{
			$bResult = true;
		}

		return $bResult;
	}

	/**
	 * @param CAccount $oAccount
	 * @return bool
	 */
	public function ChangePasswordProcess($oAccount)
	{
		$bResult = false;
		if (0 < strlen($oAccount->PreviousMailPassword) &&
			$oAccount->PreviousMailPassword !== $oAccount->IncomingMailPassword)
		{

			$aPostfixadmin = [
				"host" => CApi::GetConf('plugins.postfixadmin-change-password.config.host', '127.0.0.1'),
				"dbuser" => CApi::GetConf('plugins.postfixadmin-change-password.config.dbuser', 'root'),
				"dbpassword" => CApi::GetConf('plugins.postfixadmin-change-password.config.dbpassword', ''),
				"dbname" => CApi::GetConf('plugins.postfixadmin-change-password.config.dbname', 'postfixadmin'),
			];

			//connect to postfixadmin database
			$mysqlcon = mysqli_connect($aPostfixadmin['host'],$aPostfixadmin['dbuser'],$aPostfixadmin['dbpassword'],$aPostfixadmin['dbname']);

		 	if($mysqlcon){
				//check old pass is correct
				$username = $oAccount->IncomingMailLogin;
				$password = $oAccount->PreviousMailPassword;
				$new_password = $oAccount->IncomingMailPassword;

				$sql = "SELECT * FROM mailbox WHERE username='$username'";
				$result = mysqli_query($mysqlcon,$sql);
				$mailuser = mysqli_fetch_array($result);

				$saved_password = $mailuser['password'];

				// get salt
				$parts = explode("\$", $saved_password);
				$salt = "";
				if($parts[1] == '1' && count($parts) == 4) {
					$salt = $parts[2];
				}

				// load md5crypt
				include_once __DIR__.'/md5crypt.php';

				//* Check if mailuser password is correct
				if(md5crypt(stripslashes($password), $salt) == $saved_password) {
					$mailuser_id = $mailuser['username'];

					$new_password = <PASSWORD>($new_password);
					$sql = "UPDATE mailbox SET password='$<PASSWORD>',modified=CURRENT_TIMESTAMP WHERE username='$mailuser_id'";
					$result = mysqli_query($mysqlcon,$sql);

					if (!$result){
						//password update error
						throw new CApiManagerException(Errs::UserManager_AccountNewPasswordUpdateError);
					}

					//add log into postfixadmin
					list(,$domain) = explode("@", $mailuser_id);
					mysqli_query($mysqlcon, "INSERT INTO log VALUES(CURRENT_TIMESTAMP, '{$mailuser_id} ({$_SERVER["REMOTE_ADDR"]})', '{$domain}', 'edit_password', '{$mailuser_id}, web<PASSWORD>')");
				} else {
					//old and new passwords dont match
					throw new CApiManagerException(Errs::UserManager_AccountOldPasswordNotCorrect);
				}
				//disconnect from database
{'fraction_non_alphanumeric': 0.1519051651143099, 'fraction_numerical': 0.00541913632514818, 'mean_word_length': 4.335140018066848, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2292566', 'n_tokens_mistral': 2225, 'n_tokens_neox': 1862, 'n_words': 345}