<?php
/*
 **************************************************************************************************
 *    Copyright (c) 2010-2012 Just Another Video script
 *
 * 	  This file is part of Just Another Video Script.
 *    Version 1.4.3
 *    info@justanothervideoscript.com
 **************************************************************************************************
 *    Come to website for support or buying additional plugins/modules.
 *    http://justanothervideoscript.com/
 **************************************************************************************************
 *    By using this software you agree that you have read and acknowledged our End-User License
 *    Agreement available at http://justanothervideoscript.com/license and can be bound by it.
 **********************************************************************************
 */
global $db, $sitepath, $rootpath ;
if ($loggedIn) {

if (isset($_POST['emsubmit'])) { 	
$valid = "yes" ;
	$title = removespechar($_POST["emtitle"]) ;
	$description = quote_smart(trim($_POST["emdescription"])) ;
	$tags = quote_smart(trim($_POST["emtags"])) ;
	$catergory = quote_smart($_POST["emcatergory"]) ;
	$poster = $_SESSION['session_username'];
	$embedcode = addslashes($_POST["emembedcode"]) ;

$encodeset = str_replace('-', '', crc32(rand(100,999).time()));


$allowed_filetypes = array('.jpg','.gif','.png');
$max_filesize = "1048576" ;
$upload_path = $rootpath."uploads/thumbs/" ;
$filename_thumb = $_FILES['emthumbe']['name'] ;
$ext = substr($filename_thumb, strpos($filename_thumb,'.'), strlen($filename_thumb)-1);

$db->query("SELECT * from media WHERE title ='".$title."'") ;
if ($db->numRows() >= 1) {
	$title = $encodeset." ".$title ;
}

if(!in_array($ext,$allowed_filetypes)) {
	$valid = "no" ;
	unset ($_POST["emthumbe"]) ;
	$msgb = "<p>Thumb filetype is not allowed, only jpg, gif, png are allowed.</p>" ;
}
if(filesize($_FILES['emthumbe']['tmp_name']) >= $max_filesize) {
	$valid = "no" ;
	unset ($_POST["emthumbe"]) ;
	$msgb = "<p>Thumb filesize is to high, max allowed is 1MB</p>" ;
}	
if ($valid == "yes") {
	if(move_uploaded_file($_FILES['emthumbe']['tmp_name'],$upload_path . $filename_thumb)) {
		//make new path for thumb
		mkdir($rootpath."uploads/thumbs/".$encodeset, 0755);
		createThumb($filename_thumb, getSetting("avatar_w", $db), getSetting("avatar_h", $db), $rootpath."uploads/thumbs/", "1.jpg");
		if (file_exists($rootpath."uploads/thumbs/1.jpg"))  {
			unlink($upload_path . $filename_thumb) ;
			rename($rootpath."uploads/thumbs/1.jpg", $rootpath."uploads/thumbs/".$encodeset."/1.jpg");	
		}
	}
	$added = time() ;
	//maybe in the future add auto accept and approve mail. its better to check embedded shit first for security and spam
	$status = "false" ;
	
	//make sure al weird spaces are removed from title
		$title = preg_replace('/\s\s+/', ' ', removespechar($title));
	

	
	
$sql = "INSERT INTO `media` (title, category, description, tags, fileid, poster, added, status, embed, mediatype, mainthumb) VALUES ('".$title."', '".$catergory."', '".$description."', '".$tags."', '".$encodeset."', '".$poster."', '".$added."', '".$status."', '".$embedcode."', 'embed', '1')" ;
$db->query($sql) ;
	$msgb = "<p>Thank you for embedding a video. fileid: ".$encodeset."</p>" ;
}
	
	
	
	
}
}
?>