<commit_before><?php

/*©mit**************************************************************************
*                                                                              *
* This file is part of FRIEND UNIFYING PLATFORM.                               *
* Copyright (c) Friend Software Labs AS. All rights reserved.                  *
*                                                                              *
* Licensed under the Source EULA. Please refer to the copy of the MIT License, *
* found in the file license_mit.txt.                                           *
*                                                                              *
*****************************************************************************©*/

	/**
		This file is used for forget password functionality.
	*/
	if( $argv[1] )
	{
		$tmp = explode('/',$argv[1]);
		
		if( isset( $tmp[2] ) && isset(  $tmp[3] ) && $tmp[2] == 'username' )
		{
			//check for user and get a mail to him out if he exists.
			$cfg = parse_ini_file( 'cfg/cfg.ini', true);
			if( $cfg && isset( $cfg['DatabaseUser'] ) )
			{
				include_once( 'classes/dbio.php' );
				$SqlDatabase = new SqlDatabase();
				if( !$SqlDatabase->Open( $cfg['DatabaseUser']['host'], $cfg['DatabaseUser']['login'], $cfg['DatabaseUser']['password'] ) )
					die( 'fail<!--separate-->Could not connect to database.' );
				$SqlDatabase->SelectDatabase( $cfg['DatabaseUser']['dbname'] );
				
				if( $rs = $SqlDatabase->fetchObject('SELECT * FROM FUser WHERE Name = \'' . mysqli_real_escape_string($SqlDatabase->_link, urldecode( $tmp[3] ) ) . '\' OR Email = \'' . mysqli_real_escape_string($SqlDatabase->_link, urldecode( $tmp[3] ) ) . '\' ORDER BY ID ASC LIMIT 1' ) )
				{
					$link = '';
					if( isset( $rs->ID ) && trim($rs->Email) != '' )
					{

						$randomhash = bin2hex(openssl_random_pseudo_bytes(10));
						
						//set a setting that indicates that a password reset has been requested
						$q = 'DELETE FROM FSetting WHERE `UserID` = '. $rs->ID .' AND `Type` = \'system\' AND `Key` = \'passwordreset\';';
						$r = $SqlDatabase->query( $q );
						
						
						
						$q = 'INSERT INTO FSetting (`UserID`,`Type`,`Key`,`Data`) VALUES ( '. $rs->ID .',\'system\',\'passwordreset\',\''.  mysqli_real_escape_string($SqlDatabase->_link, $randomhash ) .'\' )';
						$r = $SqlDatabase->query( $q );

						// we have an email. build a link...
						$link = 'http';
						$link.= ( isset($cfg['Core'])&&isset($cfg['Core']['SSLEnable'])&& $cfg['Core']['SSLEnable'] == 1 ? 's' : '' ) . '://';
						$link.= $cfg['FriendCore']['fchost'] . ':' . $cfg['FriendCore']['fcport'] . '/';
						$link.= 'forgotpassword/token/';
						$link.= urlencode( str_replace('{S6}', '', $rs->Password) ) . '/';
						$link.= urlencode( $rs->Email ) . '/' . urlencode( $randomhash );
					}
					else
					{
						die( 'done1' );
					}
					
					//now get mail template and out everything together
					try
					{
						$mailtemplate = file_get_contents('./resources/webclient/templates/mail_forgotpassword.html');
					}
					catch(Exception $e)	
					{
						$mailtemplate = $link;
					}

					$mailtemplate = str_replace([
						'{user}',
						'{passwordreseturl}',
						'{username}'
						
					], [
						htmlentities( $rs->FullName != '' ? $rs->FullName : $rs->Name),
						$link,
						htmlentities( $rs->Name )
					], $mailtemplate);
					
					include_once('3rdparty/phpmailer/class.phpmailer.php');
					include_once('3rdparty/phpmailer/class.smtp.php');
					$mail = new PHPMailer();
					$mail->isSMTP();                                      // Set mailer to use SMTP
					$mail->Host = $cfg['Mail']['host']; 					 // Specify main and backup SMTP servers
					$mail->SMTPAuth = true;                               // Enable SMTP authentication
					$mail->Username = $cfg['Mail']['user'];                 // SMTP username
					$mail->Password = $cfg['Mail']['pass'];                           // SMTP password
				
					$mail->From = $cfg['Mail']['from'];
					$mail->FromName = $cfg['Mail']['fromname'];
					$mail->CharSet = 'UTF-8';
					$mail->isHTML(true); 
					
					
					
					$mail->Subject = 'FriendUP password recovery';
					$mail->Body    = $mailtemplate;
					$mail->AltBody = strip_tags($mailtemplate);
					
					$mail->addAddress( $rs->Email );
					if( $mail->send() )
					{
						die('ok');
					}
					else
					{
						die('done2');
					}										
				}
				die( 'done3' );
			}
		}
		else if( isset( $tmp[2] ) && isset(  $tmp[3] ) && $tmp[2] == 'token' && isset( $tmp[4] ) && isset( $tmp[5] ) )
		{
			
			/*<commit_msg>Path for mail sending.<commit_after><?php

/*©mit**************************************************************************
*                                                                              *
* This file is part of FRIEND UNIFYING PLATFORM.                               *
* Copyright (c) Friend Software Labs AS. All rights reserved.                  *
*                                                                              *
* Licensed under the Source EULA. Please refer to the copy of the MIT License, *
* found in the file license_mit.txt.                                           *
*                                                                              *
*****************************************************************************©*/

	/**
		This file is used for forget password functionality.
	*/
	if( $argv[1] )
	{
		$tmp = explode('/',$argv[1]);
		
		if( isset( $tmp[2] ) && isset(  $tmp[3] ) && $tmp[2] == 'username' )
		{
			//check for user and get a mail to him out if he exists.
			$cfg = parse_ini_file( 'cfg/cfg.ini', true);
			if( $cfg && isset( $cfg['DatabaseUser'] ) )
			{
				include_once( 'classes/dbio.php' );
				$SqlDatabase = new SqlDatabase();
				if( !$SqlDatabase->Open( $cfg['DatabaseUser']['host'], $cfg['DatabaseUser']['login'], $cfg['DatabaseUser']['password'] ) )
					die( 'fail<!--separate-->Could not connect to database.' );
				$SqlDatabase->SelectDatabase( $cfg['DatabaseUser']['dbname'] );
				
				if( $rs = $SqlDatabase->fetchObject('SELECT * FROM FUser WHERE Name = \'' . mysqli_real_escape_string($SqlDatabase->_link, urldecode( $tmp[3] ) ) . '\' OR Email = \'' . mysqli_real_escape_string($SqlDatabase->_link, urldecode( $tmp[3] ) ) . '\' ORDER BY ID ASC LIMIT 1' ) )
				{
					$link = '';
					if( isset( $rs->ID ) && trim($rs->Email) != '' )
					{

						$randomhash = bin2hex(openssl_random_pseudo_bytes(10));
						
						//set a setting that indicates that a password reset has been requested
						$q = 'DELETE FROM FSetting WHERE `UserID` = '. $rs->ID .' AND `Type` = \'system\' AND `Key` = \'passwordreset\';';
						$r = $SqlDatabase->query( $q );
						
						
						
						$q = 'INSERT INTO FSetting (`UserID`,`Type`,`Key`,`Data`) VALUES ( '. $rs->ID .',\'system\',\'passwordreset\',\''.  mysqli_real_escape_string($SqlDatabase->_link, $randomhash ) .'\' )';
						$r = $SqlDatabase->query( $q );

						// we have an email. build a link...
						$link = 'http';
						$link.= ( isset($cfg['Core'])&&isset($cfg['Core']['SSLEnable'])&& $cfg['Core']['SSLEnable'] == 1 ? 's' : '' ) . '://';
						$link.= $cfg['FriendCore']['fchost'] . ':' . $cfg['FriendCore']['fcport'] . '/';
						$link.= 'forgotpassword/token/';
						$link.= urlencode( str_replace('{S6}', '', $rs->Password) ) . '/';
						$link.= urlencode( $rs->Email ) . '/' . urlencode( $randomhash );
					}
					else
					{
						die( 'done1' );
					}
					
					//now get mail template and out everything together
					try
					{
						$mailtemplate = file_get_contents('./resources/webclient/templates/mail_forgotpassword.html');
					}
					catch(Exception $e)	
					{
						$mailtemplate = $link;
					}

					$mailtemplate = str_replace([
						'{user}',
						'{passwordreseturl}',
						'{username}'
						
					], [
						htmlentities( $rs->FullName != '' ? $rs->FullName : $rs->Name),
						$link,
						htmlentities( $rs->Name )
					], $mailtemplate);
					
					include_once('3rdparty/phpmailer/class.phpmailer.php');
					include_once('3rdparty/phpmailer/class.smtp.php');
					$mail = new PHPMailer();
					//$mail->SMTPDebug = SMTP::DEBUG_SERVER;
					$mail->isSMTP();       // Set mailer to use SMTP
					$mail->Port = isset( $cfg['Mail']['port'] ) ? intval( $cfg['Mail']['port'] ) :  587; 
					$mail->Host = $cfg['Mail']['host']; 					 // Specify main and backup SMTP servers
					$mail->SMTPAuth = true;                               // Enable SMTP authentication
					$mail->Username = $cfg['Mail']['user'];                 // SMTP username
					$mail->Password = $cfg['Mail']['pass'];                           // SMTP password
				
					$mail->From = $cfg['Mail']['from'];
					$mail->FromName = $cfg['Mail']['fromname'];
					$mail->CharSet = 'UTF-8';
					$mail->isHTML(true); 
					
					
					
					$mail->Subject = 'FriendUP password recovery';
					$mail->Body    = $mailtemplate;
					$mail->AltBody = strip_tags($mailtemplate);
					
					$mail->addAddress( $rs->Email );
			
			
					try {
						
						if( $mail->send() )
						{
							die('ok');
						}
						else
						{
							die('done2'); // . $mail->ErrorInfo
						}
										
					} catch (phpmailerException $e) {
						echo $e->errorMessage(); //Pretty error messages from PHPMailer
					} catch (Exception $e) {
						echo $e->getMessage(); //Boring error messages from anything else!
					}
					die();
				}
				die( 'done3' );
			}
		}
		else if( isset( $tmp[2] ) && isset(  $tmp[3] ) && $tmp[2] == 'token' && isset( $tmp[4] ) && isset( $tmp[5] ) )
		{
			
			/*
{'fraction_non_alphanumeric': 0.2108490075859919, 'fraction_numerical': 0.0052998025563753505, 'mean_word_length': 2.0046831095847644, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2188976', 'n_tokens_mistral': 3812, 'n_tokens_neox': 2907, 'n_words': 621}