<?php
error_reporting(2048);
require_once("includes/SQLConnect.php");
require_once("includes/user_data.php");
require_once("includes/functions.php");
//This should actually redirect to main page (district.php)

if(isset($_POST['url']) && $_POST['url']!=""){

		if(strlen($_POST['excerpt'])>0){
			$url = $_POST['url'];
		
			if(filter_var($url,FILTER_VALIDATE_URL) === FALSE) //Not valid without http://
				$validation = "That is not a valid URL";
			else{
				//Grab the HTML Title
				$page = curl_get_file_contents($url,$url);
							
				if($page != FALSE){ //Managed to download the news item
					if(isset($_POST['title']) || preg_match("|<title>(.*)</title>|s", $page, $title) != FALSE){				
						if(is_array($title))
							$title = $title[1]; //$title[1] contains the non-HTML title (the real value of it)
						elseif(isset($_POST['title']))
							$title = $_POST['title'];
						else{
							$error = "Somehow the title of this news item has slipped through the cracks (an error has occurred)... Please resubmit.";
							$title_flag = true;
						}
						
						if(!isset($error)){
							$excerpt = htmlentities(strip_tags($_POST['excerpt']));
							if(mysql_query("INSERT INTO User_News(user_ID,title,excerpt,url) VALUES(".(int)$user_data['ID'].",'".mysql_real_escape_string($title)."','".mysql_real_escape_string($excerpt)."','".mysql_real_escape_string($_POST['url'])."')"))
								header("Location:district.php?success=News submitted.");
							else
								$error = "Your news item has already been submitted.";
						}
					}
					else{
						$error = "We weren't able to automagically grab the title of that document. Please enter it below.";
						$title_flag = true;
					}
				}
				else
					$error = "It seems that that URL is not valid. If you're sure it is, please try again. Your link must be publicly accessible.";
			}
		}
		else
			$validation = "You must submit an excerpt.";}

include_once("includes/header.php");

?>


<h2>Suggest News</h2>

<form action="news_suggest.php" method="POST">
	<?php
	
		if(isset($title_flag) && $title_flag){
			?>
			
			<label>Title</label>
			<input type="text" class="large" name="title">
			
			<?php
		}
	
	?>
	<label>Excerpt (200 characters or less):</label>
	<textarea rows="5" name="excerpt" id="excerpt" value="<?php if(isset($_POST['excerpt'])){echo $_POST['excerpt'];}?>"></textarea>
	
	<div id="chars-remaining">You have <span id="chars-number">200</span> characters remaining.</div>
	
	<script type="text/javascript">
		$('textarea#excerpt').bind("keyup", function(e) {
			var chars_used = $('textarea#excerpt').val().length;
			if(chars_used>200){
				$('textarea#excerpt').val($('textarea#excerpt').val().substring(0,200));
			}
			if(200 - chars_used >0)
				$('#chars-number').html(200 - chars_used);
			else
				$('#chars-number').html("0");
		});
	
	</script>
	
	<label>URL:</label>
	<input type="text" name="url"  value="<?php if(isset($_POST['url'])){echo $_POST['url'];}?>" class="large"/>
	<input type=submit value="Suggest News" class='submit'>
	
</form>
	

</form>

<?php
include_once("includes/footer.php");
?>