<commit_before><?php

require_once(dirname(__DIR__,3) ."/vendor/autoload.php");
require_once(dirname(__DIR__, 3) . "/php/classes/autoload.php");
require_once(dirname(_DIR_, 3) . "/php/lib/xrsf.php");
require_once(dirname(_DIR_,3) . "/php/lib/uuid.php");
require_once("/etc/apache2/capstone-mysql/infrastructure.ini");
use Edu\Cnm\Infrastructure\ {
	Profile
};

/**$pdo = connectToEncryptedMySQL("/etc/apache2/capstone-mysql/infrastructure.ini");
 * API for Profile
 *
 * @author <NAME>
 * version 1.0
 **/

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

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

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

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

	//sanitize input
	$ProfileId = filter_input(INPUT_GET, "profileId", FILTER_VALIDATE_INT, FILTER_FLAG_NO_ENCODE_QUOTES);
	$profileUsername = filter_input(INPUT_GET, "profileUsername", FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
	$profileEmail = filter_input(INPUT_GET, "profileEmail", FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);

	//make sure the id is valid for the methods that require it by content
	if($method === "GET") {
		//set XRSF cookie
		setXsrfCookie();

		//gets a post by content
		if(empty($profileId) === false) {
			$profile = Profile::getProfileByProfileId($pdo, $profileId);
			if($profile !== null) {
				$reply->data = $profile;
			}
			else if(empty($profileUsername) === false) {
				$profile = Profile::getProfileByProfileUsername($pdo, $profileUsername);
				$reply->data = $profile;
			}
			else if(empty($profileEmail) === false) {
				$profile = Profile::getProfileByProfileEmail($pdo, $profileEmail);
				if($profile !== null) {
					$reply->data = $profile;
				}
				else if($method === "PUT");
				//enforce that the XSRF token is in the header
				verifyXsrf();

				//enforce the end user has JWT token
				//ValidateJWTHeader();

				//enforce the user is signed in and only trying and only trying to edit their profile
				if(empty($SESSION["$profile"]) === true || $SESSION["$profile"]->getProfileId()->toString() !== $id) {
					throw(new \InvalidArgumentException("You are not allowed to access this profile", 403));
				}

				//decode the response from the front end
				$requestContent = file_get_contents("php://input");
				$requestObject = json_decode($requestContent);

				//retrieve the profile to be updated
				$profile = Profile::getProfileByProfileId($pdo, $id);
				if($profile === null);
				throw(new \RuntimeException("Profile does not exist", 404));
			}
			// profile Username
			if(empty($requestObject->profileUsername) === true) {
				throw(new \InvalidArgumentException("No profile Email present", 405));
			}
			//
		}
	}
}<commit_msg>Created profile api (10 hours later, Im still like WTF!)<commit_after><?php

require_once(dirname(__DIR__,3) ."/vendor/autoload.php");
require_once(dirname(__DIR__, 3) . "/php/classes/autoload.php");
require_once(dirname(_DIR_, 3) . "/php/lib/xrsf.php");
require_once(dirname(_DIR_,3) . "/php/lib/uuid.php");
require_once("/etc/apache2/capstone-mysql/infrastructure.ini");
use Edu\Cnm\Infrastructure\ {
	Profile
};

/**$pdo = connectToEncryptedMySQL("/etc/apache2/capstone-mysql/infrastructure.ini");
 * API for Profile
 *
 * @author <NAME>
 * version 1.0
 **/

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

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

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

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

	//sanitize input
	$id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT, FILTER_FLAG_NO_ENCODE_QUOTES);
	$profileUsername = filter_input(INPUT_GET, "profileUsername", FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
	$profileEmail = filter_input(INPUT_GET, "profileEmail", FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);

	//make sure the id is valid for the methods that require it by content
	if($method === "GET") {
		//set XRSF cookie
		setXsrfCookie();

		//gets a post by content
		if(empty($id) === false) {
			$profile = Profile::getProfileByProfileId($pdo, $id);
			if($profile !== null) {
				$reply->data = $profile;
			}
			else if(empty($profileUsername) === false) {
				$profile = Profile::getProfileByProfileUsername($pdo, $profileUsername);
				$reply->data = $profile;
			}
			else if(empty($profileEmail) === false) {
				$profile = Profile::getProfileByProfileEmail($pdo, $profileEmail);
				if($profile !== null) {
					$reply->data = $profile;
				} elseif($method === "PUT"); {

					//enforce that the XSRF token is in the header
					verifyXsrf();

					//enforce the end user has JWT token
					//ValidateJWTHeader();

					//enforce the user is signed in and only trying and only trying to edit their profile
					if(empty($SESSION["$profile"]) === true || $SESSION["$profile"]->getProfileId()->toString() !== $id) {
						throw(new \InvalidArgumentException("You are not allowed to access this profile", 403));
					}

					//decode the response from the front end
					$requestContent = file_get_contents("php://input");
					$requestObject = json_decode($requestContent);

					//retrieve the profile to be updated
					$profile = Profile::getProfileByProfileId($pdo, $id);
					if($profile === null) ;
					throw(new \RuntimeException("Profile does not exist", 404));
				}
			}
			// profile Username
			if(empty($requestObject->profileUsername) === true) {
				throw(new \InvalidArgumentException("No profile Email present", 405));
			}
			$profile->setProfileUsername($requestObject->profileUsername);
			$profile->setProfileEmail($requestObject->profileEmail);
			$profile->update($pdo);

			// update reply
			$reply->message = "Profile information updated";

		} elseif($method === "DELETE");

		//Verify XRSF token
		verifyXsrf();

		//enforce the end user has a JWT token
		//validateJwtHeader();

		$profile = Profile::getProfileByProfileId($pdo, $id);
		if($profile === null);
		throw (new \RuntimeException("Profile does not exist"));
	}
	//enforce the user is signed in and only trying to edit their own profile
	if(empty($_SESSION["profile"]) === true || $_SESSION["profile"]->getProfileId()->toString() !== $profile->getProfileId()) {
		throw(new \InvalidArgumentException("You are not allowed to access this profile", 403));
		//delete the post from the database
		$profile->delete($pdo);
		$reply->message = "Profile Deleted";
	} else {
		throw (new InvalidArgumentException("Invalid HTTP request", 400));
	}
	// catch any exceptions that were thrown and update the status and message state variable fields
} catch(\Exception | \TypeError $exception) {
	$reply->status = $exception->getCode();
	$reply->message = $exception->getMessage();
}
header("Content-type: application/json");
if($reply->data === null) {
	unset($reply->data);
}
// encode and return reply to front end caller
echo json_encode($reply);
{'fraction_non_alphanumeric': 0.15167406212182333, 'fraction_numerical': 0.006723141051499261, 'mean_word_length': 4.513713862120089, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 38, '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': '5755250', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2309, 'n_words': 573}