<html>
<head>
<?php session_start(); ?>
</head>
<body>
<?php
require_once("../../mysql_connect.php");
if(isset($_POST['register_submit'])){
	$response = $dbc->query("select username from ".$dbc_unver_accounts);
	$response2 = $dbc->query("select username from ".$dbc_ver_accounts);
	$uexist=false;
	if($response){
		//cheak if exist
		while($row = $response->fetch(PDO::FETCH_ASSOC)){
			if($row['username']==$_POST['usernameR']){
				$uexist=true;
				break;
			}
		}
		while($row = $response2->fetch(PDO::FETCH_ASSOC)){
			if($row['username']==$_POST['usernameR']){
				$uexist=true;
				break;
			}
		}
		//creation
		if($uexist==true){
			$_SESSION['message'] = "user allready exist";
		} else {
			//send data
			$query="INSERT INTO ".$dbc_unver_accounts." (username, password, 
			discord, email) VALUES (?,?,?,?)";
			$stmt=$dbc->prepare($query);
			$password=password_hash($_POST['passwordR'], PASSWORD_DEFAULT);
			$stmt->execute([$_POST['usernameR'],$password,$_POST['discord'],
			$_POST['email']]);
			$_SESSION['message']="complete";
		}
	} else {
		$_SESSION['message'] = "internal server error";
	}
}
if(isset($_POST['manageconfirm'])){
	if($_POST['action']=="deny"){
		$stmt=$dbc->prepare("delete from ".$dbc_unver_accounts." where username=?");
		$stmt->execute([$_POST['user']]);
		$action="denied";
	}
	if($_POST['action']=="accept"){
		$getinfo=$dbc->prepare("select * from ".$dbc_unver_accounts." where username=?");
		$getinfo->execute([$_POST['user']]);
		$getinfoarr=array();
		while($row=$getinfo->fetch()){
			$getinfoarr=array($row['username'],$row['password'],$row['discord'],$row['email']);
		}
		$drop=$dbc->prepare("delete from ".$dbc_unver_accounts." where username=?");
		$drop->execute([$_POST['user']]);
		$stmt=$dbc->prepare("INSERT INTO ".$dbc_ver_accounts." 
		(username, password, discord, email, date_created, primary_key) 
		VALUES (?,?,?,?,NOW(),NULL)");
		$stmt->execute($getinfoarr);
		$file=fopen("../account_data/".$_POST['user'].".xml","w");
		fwrite($file,'<?xml version="1.0" encoding="UTF-8"?>'."\n<user>\n	<color>#ffffff</color>\n	<permissions>\n	</permissions>\n</user>");
		fclose($file);
		$action="confirmed";
	}
	$xml=simplexml_load_file("log.xml");
	$file = fopen("log.xml","w");
	fwrite($file,'<?xml version="1.0" encoding="UTF-8"?>');
	fwrite($file,"\n<log>");
	fwrite($file,"\n".'<user name="'.$_POST['user'].'" action="'.$action.'">'.$_POST['reason'].'</user>');
	for($i=0;$i<=4;$i++){
		fwrite($file,"\n".'<user name="'.$xml->user[$i]['name'].'" action="'.$xml->user[$i]['action'].'">'.$xml->user[$i].'</user>');
	}
	fwrite($file,"\n</log>");
	fclose($file);
}
$dbc=null;
?>

<script language="javascript">window.close();</script>
</body>
</html>