<?php

# newthread.php
# Allows the user to create a new thread which will get displayed on index.php

include ('includes/header.html');

# This piece of javascript will default the cursor to the Title input box.
echo "<script type=\"text/javascript\">
window.onload=function(){document.forms['newthread'].elements['title'].focus();}
</script>";

# Only let the user post if their user session is set and their HTTP_USER_AGENT matches
# the one when they signed in with.
if (isset($_SESSION['user']) && $_SESSION['agent'] == md5($_SERVER['HTTP_USER_AGENT'])) {

	# A form for the user to create the thread.
    echo '
	<form action="newthread.php" method="post" name="newthread">
	<p>  Title:<input type="text" name="title" size="40" maxlength="50" /></p>
	<p>Message: <textarea name="message" cols="60" rows="10"></textarea></p>
	<p><input type="submit" name="submit" value="Submit Thread" /><input type="submit" name="cancel" value="Cancel" /></p>
	</form>
	';
	
	# Execute this block of code once the "Submit Thread" button is pressed.
    if ($_POST['submit']) {

		# Validate that a title has been entered.
        if (empty($_POST['title'])) {
            echo "Please enter a title";
        } else {
            $title = htmlentities($_POST['title']);

			# Validate that a message has been entered.
            if (empty($_POST['message'])) {
                echo "Please enter a message.";
            } else {
                $message = htmlentities($_POST['message']);
				
				# If the user passes the validation, this statement will be used to create the thread.
				$id = $_SESSION['user'];
                $q = "INSERT into threads (thread_title, thread_author, thread_date) VALUES ('$title', '$id', NOW())";

				# Execute this block of code if the thread is created.
                if ($r = mysqli_query($dbc, $q)) {

					# Determine the GET ID of the thread created so that the user can be directed
					# there upon the successful creation of the thread.
                    $q2 = "SELECT thread_id FROM threads ORDER BY thread_id DESC LIMIT 1";
                    $r2 = mysqli_query($dbc, $q2);
                    $row = mysqli_fetch_array($r2);
                    $pageid = $row['thread_id'];

					# This statement is used to create the first post inside of the thread.
                    $q3 = "INSERT INTO posts (in_thread, post_content, post_date, post_author) VALUES ('$pageid', '$message', NOW(), '$id')";
                    $r3 = mysqli_query($dbc, $q3);
                    
                    # Redirect the user to the thread they created.
                    header("Location:thread.php?id=$pageid");
                    
                } else {
                	# Echo an error if the thread was unable to be created.
                    echo '<p>' . mysqli_error($dbc) . '<br/>' . $q . '</p>';
                }

            }

        }
    }

	# Take the user back to the home page if they decide not to post.
    if ($_POST['cancel']) {
        header("Location:index.php");
    }

# Return the user to the login screen if they are not logged in.
} else {
	unset($_SESSION['user']);
    unset($_SESSION['agent']);
    header("Location:login.php");
}

include ('includes/footer.html');

?>