<commit_before><?php
//Take in the username on the post. Get the user's profile by username. If it exists, send the salt back in the reply with status 200. If it doesn't exist, send error message status 401.
require_once dirname(__DIR__, 3) . "/php/class/autoloader.php";
require_once dirname(__DIR__, 3) . "/php/lib/xsrf.php";
require_once("/etc/apache2/capstone-mysql/encrypted-config.php");

use Edu\Cnm\KiteCrypt\Profile;


/**
 * api for the Salt class
 *
 * @author <NAME> <EMAIL>
 **/

//verify the session, start if not active
if(session_status() !== PHP_SESSION_ACTIVE) {
	session_start();
}

//prepare an empty reply
$reply = new stdClass();
$reply->status = 200;
$reply->data = null;


$exceptionMessage = "Username or Password invalid";
$exceptionCode = 401;

try {
	//grab the mySQL connection
	$pdo = connectToEncryptedMySQL("/etc/apache2/capstone-mysql/kitecrypt.ini");

	//determine which HTTP method was used
	$method = array_key_exists("HTTP_X_HTTP_METHOD", $_SERVER) ? $_SERVER["HTTP_X_HTTP_METHOD"] : $_SERVER["REQUEST_METHOD"];


	// handle GET request//
	if($method === "GET") {
		//set XSRF cookie
		setXsrfCookie("/");
		throw(new \BadMethodCallException($exceptionMessage, $exceptionCode));

	} else if($method === "POST") {

		verifyXsrf();
		$requestContent = file_get_contents("php://input");
		$requestObject = json_decode($requestContent);
		if(empty($requestObject->username) === true) {
			throw(new \InvalidArgumentException ($exceptionMessage, $exceptionCode));
		}
//		New User field
		$newUser = filter_var($requestObject->newUser, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
		if($newUser === null) {
			throw (new \InvalidArgumentException($exceptionMessage, $exceptionCode));
		}
		if($newUser === true) {
			$profileFromDatabase = Profile::getProfileByUserName($pdo, $requestObject->username);
			if($profileFromDatabase->getSize() === 0) {
				$pepper = new stdClass();
				$pepper->salt = bin2hex(random_bytes(16));
				$reply->data = $pepper;
			} else {
				throw (new \InvalidArgumentException($exceptionMessage, $exceptionCode));
			}
		} else {
			//perform the actual post - POST only, GET requests put username in the URL. POST requests are not shareable//
			$profile = Profile::getProfileByUserName($pdo, $requestObject->username);
			if(count($profile) === 0) {
				throw (new \InvalidArgumentException($exceptionMessage, $exceptionCode));
			}
			$singleProfile = $profile[0];
			$pepper = new stdClass();
			$pepper->salt = $singleProfile->getProfilePasswordSalt();
			$pepper->id = $singleProfile->getProfileId();
			$reply->data = $pepper;
//<commit_msg>Return array of id and salt<commit_after><?php
//Take in the username on the post. Get the user's profile by username. If it exists, send the salt back in the reply with status 200. If it doesn't exist, send error message status 401.
require_once dirname(__DIR__, 3) . "/php/class/autoloader.php";
require_once dirname(__DIR__, 3) . "/php/lib/xsrf.php";
require_once("/etc/apache2/capstone-mysql/encrypted-config.php");

use Edu\Cnm\KiteCrypt\Profile;


/**
 * api for the Salt class
 *
 * @author <NAME> <EMAIL>
 **/

//verify the session, start if not active
if(session_status() !== PHP_SESSION_ACTIVE) {
	session_start();
}

//prepare an empty reply
$reply = new stdClass();
$reply->status = 200;
$reply->data = null;


$exceptionMessage = "Username or Password invalid";
$exceptionCode = 401;

try {
	//grab the mySQL connection
	$pdo = connectToEncryptedMySQL("/etc/apache2/capstone-mysql/kitecrypt.ini");

	//determine which HTTP method was used
	$method = array_key_exists("HTTP_X_HTTP_METHOD", $_SERVER) ? $_SERVER["HTTP_X_HTTP_METHOD"] : $_SERVER["REQUEST_METHOD"];


	// handle GET request//
	if($method === "GET") {
		//set XSRF cookie
		setXsrfCookie("/");
		throw(new \BadMethodCallException($exceptionMessage, $exceptionCode));

	} else if($method === "POST") {

		verifyXsrf();
		$requestContent = file_get_contents("php://input");
		$requestObject = json_decode($requestContent);
		if(empty($requestObject->username) === true) {
			throw(new \InvalidArgumentException ($exceptionMessage, $exceptionCode));
		}
//		New User field
		$newUser = filter_var($requestObject->newUser, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
		if($newUser === null) {
			throw (new \InvalidArgumentException($exceptionMessage, $exceptionCode));
		}
		if($newUser === true) {
			$profileFromDatabase = Profile::getProfileByUserName($pdo, $requestObject->username);
			if($profileFromDatabase->getSize() === 0) {
				$pepper = new stdClass();
				$pepper->salt = bin2hex(random_bytes(16));
				$reply->data = $pepper;
			} else {
				throw (new \InvalidArgumentException($exceptionMessage, $exceptionCode));
			}
		} else {
			//perform the actual post - POST only, GET requests put username in the URL. POST requests are not shareable//
			$profile = Profile::getProfileByUserName($pdo, $requestObject->username);
			if(count($profile) === 0) {
				throw (new \InvalidArgumentException($exceptionMessage, $exceptionCode));
			}
			$singleProfile = $profile[0];
//			$pepper = new stdClass();
//			$pepper->salt = $singleProfile->getProfilePasswordSalt();
//			$pepper->id = $singleProfile->getProfileId();
//			$reply->data = $pepper;
			$reply->data = $singleProfile->toArray();
//
{'fraction_non_alphanumeric': 0.15285903000566145, 'fraction_numerical': 0.008303453481789017, 'mean_word_length': 4.698924731182796, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, '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': '1759254', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1651, 'n_words': 403}