<?php
/*
Any authenticated user can have access and create the new post. This file will allow user to create their blog, validate and store on MySQL. 
Allow user to upload file file is less then 3 MB. 
*/
session_start();
require_once("includes/mysql_login.php");
include("header.htm");
echo "<div class=left>";
include("includes/left.htm");
echo "</div>";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>IEEE Student Chapter | University of Alaska Acnhorage | New Post</title>
<link rel="stylesheet" href="includes/style.css" type="text/css" /></head>
<body> 
<?php
if(!isset($_SESSION['autheticate']))
{
	header('Location: index.php');
	exit();
}
 $identification = $_SESSION['identification'];
 echo "<div class=body>";
if(isset($_POST['submit']))
{
/*
This function can be called just once. Leave this function commented once table has been created. 
*/
	function create_table_post()
		{
			$create_table_user = 'CREATE TABLE `post`(`postid` int(6) auto_increment NOT NULL, 
											 					`title` varchar(200) NOT NULL,
											  					`category` varchar(200) NOT NULL,
																`post` longtext NOT NULL,
																`attachment` varchar(200),
											   					`identification` int NOT NULL, 
																`date_created` timestamp, PRIMARY KEY(`postid`)
																 )';  
			$create_table_user_query = mysql_query($create_table_user);
			if(!$create_table_user_query)
			{
		 	echo "Error on query". mysql_error();
		 	}
		 	else 
		 	{
		 	echo "Table post created successfully";
		 	}
		}
/*
Allow user to upload file, check for valid file size and upload on already created directory named /file_upload/
*/
	function upload_file()
	{
		$target_path = "file_upload/";
		$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
		$_FILES['uploadedfile']['tmp_name'];  
		$target_path = "file_upload/";
		$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
		if(is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) //If file not choosen
		{
				if($_FILES['uploadedfile']['size'] > 3145728 )
				{
				echo " Your file size is really large";
				
				}
				if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
 				{
   				echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    			" has been uploaded <br>";
				dump_in_mysql_post(); 
				}
 				else
 				{
    			echo " <br> <b> File can not be uploaded </b><br>";
				echo "<meta http-equiv = \"refresh\" content = \"3; URL=post.php\">";
				}
		}
	 	else
	  	{
			echo "<br>No attachment selected <br>";
			dump_in_mysql_post();
		}
	}
	/*
	Once check for invalid inputs, store the information to MySQL. Redisplay this page if information is not valid. 
	*/
	function dump_in_mysql_post()
		{
		$title = stripslashes($_POST['title']);
		$title = mysql_real_escape_string($title);
		$category = stripslashes($_POST['category']);
		$category = mysql_real_escape_string($category);
		$body = stripslashes($_POST['body']);
		$body = mysql_real_escape_string($body);
		$attachment = mysql_real_escape_string($_FILES['uploadedfile']['name']);
		$attachment = stripslashes($_FILES['uploadedfile']['name']);
		$identification = $_SESSION['identification'];
		$dump_in_mysql_post = "INSERT INTO `post` VALUES('$postid', '$title', '$category', '$body', '$attachment', '$identification', now())";
		$dump_in_mysql_post = mysql_query($dump_in_mysql_post);
			if(!$dump_in_mysql_post)
			{
				echo "Error on querying post ". "<b> ". mysql_error() . "</b>";
			}
			else
			{
				echo "<meta http-equiv = \"refresh\" content = \"1; URL=index.php\">";
				echo "Your post has been created <br> <b>Thanks you! </b>";
				
			} 
				
		}
/*
 This function call can be called only once unless table is accidently deleted
 */
//create_table_post();
/*
Check for invalid user input. 
*/
function verify_user_input()
{ 
	$errors = array();
	//echo "<br> Post: >". $_POST['body'];
	if($_POST['title'] == "")
	{
	$errors[] = "Blank title not allowed";
	}
	if($_POST['category'] == "")
	{
	$errors[] = "Blank Category Not allowed";
	}
	if($_POST['body'] == "")
	{
	$errors[] = "Empty body not allowed";
	}
	if(empty($errors))
	{
	upload_file();
	
	} else 
	{
		foreach ($errors as $error) 
		{
			echo "- ". $error;
		}
	}
}
verify_user_input();
} 
else
{
echo " Create Your Blog: <br><br>";
echo "<form action=\"$PHP_SELF\" enctype=\"multipart/form-data\" method=\"POST\">";
echo "Title: <input type = text name = title>* <br><br>"; 
echo "Category: <select name = category>
				<option> Engineering </option>
				<option> Science</option>
				<option> Missalenous </option></select>*<br><br>";
echo "Body: <br><br>";
echo "<textarea name = body cols = 40 rows = 12> </textarea>* <br><br>";
echo "Attach File<input name=uploadedfile type = file><br> <i>Attached file can not contain # (sharp) in filename</i><br>  ";
echo "<input name=submit type=submit value=Post>";
echo " <input type = reset name reset value = Clear>";
echo "</form>";
}
echo "</div>";
include("bottom.htm");
?>
</body>
</html>