<commit_before><?php
 
// get the HTTP method, path and body of the request
$method = $_SERVER['REQUEST_METHOD'];
  
// We only want to deal with GET here
if ($method !== 'POST') {
	http_response_code(405);
	header("Allow: POST");
	exit();
}

// Check Auth
//Did they come from skitter? (Env variable?)
/*
if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] !== "superskitter.com"){
	http_response_code(403);
	exit();
}
*/
//Do they have a valid SessionID
if(!isset($_COOKIE["sid"])){
	http_response_code(403);
	exit();
}
else{
	//Make request to /isAuthenticated
	
	//Is auth will reply with this in the future
	$email = "<EMAIL>";
}

//They are Authed

// connect to the mysql database
// TODO ENV VARIABLES OR SOMETHING BETTER
$link = mysqli_connect("172.17.0.2", "root", "password", "<PASSWORD>");

//Check connection
if (mysqli_connect_errno()) {
	http_response_code(500);
    exit();
}

//Prepared statement
if ($stmt = mysqli_prepare($link, "UPDATE users SET username=? WHERE email=?")){
	 //Get user they want
	 $username = $_POST["newUsername"];
	 //Escape input
	 if(!mysqli_set_charset($link, "utf8")){
		http_response_code(500);
		exit();
	 }
	 $username = mysqli_real_escape_string($link, $username);
	 $email = mysqli_real_escape_string($link, $email);
	 if ($username === NULL || $email === NULL){
		http_response_code(500);
		exit();
	 }
	 //Bind parameters
	 mysqli_stmt_bind_param($stmt, "ss", $username, $email);
	 //Execute
	 $success = mysqli_stmt_execute($stmt);

	 //Return result as json
	 if (!$success){
		http_response_code(500);
	 }
	 else {
	 	 $json = json_encode($success);
		 if(json_last_error()){
			http_response_code(500);
			exit();
		 }
		 echo $json;
	 }
	 //Close Statement
	 mysqli_stmt_close($stmt);
}

// close mysql connection
mysqli_close($link);
?><commit_msg>No changing to someone else's username!<commit_after><?php
 
// get the HTTP method, path and body of the request
$method = $_SERVER['REQUEST_METHOD'];
  
// We only want to deal with GET here
if ($method !== 'POST') {
	http_response_code(405);
	header("Allow: POST");
	exit();
}

// Check Auth
//Did they come from skitter? (Env variable?)
/*
if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] !== "superskitter.com"){
	http_response_code(403);
	exit();
}
*/
//Do they have a valid SessionID
if(!isset($_COOKIE["sid"])){
	http_response_code(403);
	exit();
}
else{
	//Make request to /isAuthenticated
	
	//Is auth will reply with this in the future
	$email = "<EMAIL>";
}

//They are Authed

// connect to the mysql database
// TODO ENV VARIABLES OR SOMETHING BETTER
$link = mysqli_connect("172.17.0.2", "root", "password", "<PASSWORD>");

//Check connection
if (mysqli_connect_errno()) {
	http_response_code(500);
    exit();
}

//Set charset
if(!mysqli_set_charset($link, "utf8")){
		http_response_code(500);
		exit();
}
//Get user they want
$username = $_POST["newUsername"];
$username = mysqli_real_escape_string($link, $username);
//Check if their username is taken
if ($stmt = mysqli_prepare($link, "SELECT username FROM users WHERE username=?")){

	 //Bind parameters
	 mysqli_stmt_bind_param($stmt, "s", $username);
	 //Execute
	 $success = mysqli_stmt_execute($stmt);
	 //Bind result
	 mysqli_stmt_bind_result($stmt, $result);
	 //Fetch
	 if (mysqli_stmt_fetch($stmt)){
		echo '{"error": "Duplicate Username"}';
		exit();
	 }

}

//Prepared statement
if ($stmt = mysqli_prepare($link, "UPDATE users SET username=? WHERE email=?")){
	 
	 //Escape input
	 $email = mysqli_real_escape_string($link, $email);
	 if ($username === NULL || $email === NULL){
		http_response_code(500);
		exit();
	 }
	 //Bind parameters
	 mysqli_stmt_bind_param($stmt, "ss", $username, $email);
	 //Execute
	 $success = mysqli_stmt_execute($stmt);

	 //Return result as json
	 if (!$success){
		http_response_code(500);
	 }
	 else {
	 	 $json = json_encode($success);
		 if(json_last_error()){
			http_response_code(500);
			exit();
		 }
		 echo $json;
	 }
	 //Close Statement
	 mysqli_stmt_close($stmt);
}

// close mysql connection
mysqli_close($link);
?>
{'fraction_non_alphanumeric': 0.16863033873343153, 'fraction_numerical': 0.015709376534118802, 'mean_word_length': 4.383091149273448, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, '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': '5506906', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1359, 'n_words': 362}