<?php
include '../db/db.php';
session_start();
$log=htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
if (empty($log)) {

	$redirectUrl = "index.php";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";

}else{

	$sqlu="SELECT * FROM details_of_users WHERE user='$log' and val='1'";
	$resultu=mysqli_query($conn, $sqlu);
	$countu=mysqli_num_rows($resultu);

	if($countu==1){

		$sqlc="SELECT * FROM details_of_users where user='$log'";
		$resultc=mysqli_query($conn, $sqlc);
		while ($rowc = mysqli_fetch_array($resultc)){
			$name=$rowc['id'];
		}

	} else {
		$redirectUrl = "index.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";

	}
}


if (array_key_exists('txt_today', $_POST)) {
	
	$txt_today = $_POST['txt_today'];

	$file_name = '../upload/page_content/txt_today.txt';

	if (!file_exists($file_name)) {
	  mkdir('../upload/page_content');
	}
	$myfile = fopen($file_name , "w") or die("Unable to open file!");
	//$txt = "John Doe\n";

	if (fwrite($myfile, $txt_today)) {
		$response['status'] = 'ok';
	}else{
		$response['status'] = 'error';
	}
	fclose($myfile);
	echo json_encode($response);
}


if (array_key_exists('txt_same', $_POST)) {
	
	$txt_same = $_POST['txt_same'];

	$file_name = '../upload/page_content/txt_same.txt';

	if (!file_exists($file_name)) {
	  mkdir('../upload/page_content');
	}
	$myfile = fopen($file_name , "w") or die("Unable to open file!");
	//$txt = "John Doe\n";

	if (fwrite($myfile, $txt_same)) {
		$response['status'] = 'ok';
	}else{
		$response['status'] = 'error';
	}
	fclose($myfile);
	echo json_encode($response);
}


if (array_key_exists('txt_int', $_POST)) {
	
	$txt_int = $_POST['txt_int'];

	$file_name = '../upload/page_content/txt_int.txt';

	if (!file_exists($file_name)) {
	  mkdir('../upload/page_content');
	}
	$myfile = fopen($file_name , "w") or die("Unable to open file!");
	//$txt = "John Doe\n";

	if (fwrite($myfile, $txt_int)) {
		$response['status'] = 'ok';
	}else{
		$response['status'] = 'error';
	}
	fclose($myfile);
	echo json_encode($response);
}


?>