<?php


session_start();
include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

$isvideouploadpage = "1";
STemplate::assign('isvideouploadpage',$isvideouploadpage);

$mainmenu = "5";
STemplate::assign('mainmenu',$mainmenu);

$submenu = "0";
STemplate::assign('submenu',$submenu);

if ($_SESSION[USERID] != "" && $_SESSION[USERID] >= 0 && is_numeric($_SESSION[USERID]))
{
	if ($_SESSION[VERIFIED] != "1")
	{
		$error = "$lang[685]";
	}
	
	if ($error == "")
	{
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    	header('Cache-Control: no-store, no-cache, must-revalidate');
    	header('Cache-Control: post-check=0, pre-check=0', FALSE);
    	header('Pragma: no-cache');

    	include "uu_conlib.php";
    	$tmp_sid = md5(uniqid(mt_rand(), true));
    	$config_file = $default_config_file;                                                                                                   
    	$path_to_upload_script .= '?tmp_sid=' . $tmp_sid;
    	$path_to_ini_status_script .= '?tmp_sid=' . $tmp_sid;
		
		STemplate::assign('tmp_sid', $tmp_sid);
    	STemplate::assign('disallow_extensions', $disallow_extensions);
    	STemplate::assign('allow_extensions', $allow_extensions);
    	STemplate::assign('path_to_ini_status_script', $path_to_ini_status_script);
    	STemplate::assign('check_file_name_format', $check_file_name_format);
    	STemplate::assign('check_disallow_extensions', $check_disallow_extensions);
    	STemplate::assign('check_allow_extensions', $check_allow_extensions);
    	STemplate::assign('check_null_file_count', $check_null_file_count);
    	STemplate::assign('check_duplicate_file_count', $check_duplicate_file_count);
    	STemplate::assign('max_upload_slots', $max_upload_slots);
    	STemplate::assign('progress_bar_width', $progress_bar_width);
    	STemplate::assign('path_to_upload_script', $path_to_upload_script);
    	STemplate::assign('multi_upload_slots', $multi_upload_slots);
		
		if(isset($_GET['rnd_id']))
		{  
    		$temp_dir = $_REQUEST['temp_dir'];
    		$_POST_DATA = getPostData($temp_dir, $_REQUEST['tmp_sid']);
    
    		$upload_dir = $_POST_DATA['upload_dir'];
    		$upload_file = $_POST_DATA['upfile_0'];
    		$upload_file_path = $upload_dir . $upload_file;
				
    		if(!is_file($upload_file_path) || filesize($upload_file_path) < 0)
    		{
				$error = "$lang[686]";
    		}
    
    		$upload_file_size = filesize($uploaded_file_path);
				
    		if($error == "")
    		{
				$pos = strrpos($upload_file,".");
				$ph = strtolower(substr($upload_file,$pos+1,strlen($upload_file)-$pos));
				$space = round($upload_file_size/(1024*1024));
					
				if ($ph!="3gp" && $ph!="mp4" && $ph!="mov" && $ph!="asf" && $ph!="flv" && $ph!="mpg" && $ph!="avi" && $ph!="mpeg" && $ph!="wmv" && $ph!="rm" && $ph!="dat")
	    			$error = "$lang[687]";	
    			}
				elseif ($space>$config[max_video_size])
				{
					$error = "$lang[688]";	
				}
    
    			if($error == "")
    			{

					$approve_videos = $config[approve_videos];
				
					if($approve_videos == 1) 
					{
						$active = "active='0'";
					} 
					else 
					{
						$active = "active='1'";
					}
				

  					$thetitle = htmlentities(strip_tags($_POST_DATA[title]),ENT_QUOTES,'UTF-8'); 
					$thetags = htmlentities(strip_tags($_POST_DATA[tags]),ENT_QUOTES,'UTF-8'); 
  					$thedesc = htmlentities(strip_tags($_POST_DATA[vwdescription]),ENT_QUOTES,'UTF-8'); 
					$thecat = htmlentities(strip_tags($_POST_DATA[category]),ENT_QUOTES,'UTF-8');
					$thepublic = htmlentities(strip_tags($_POST_DATA['public']),ENT_QUOTES,'UTF-8');
					$theallowcomments = htmlentities(strip_tags($_POST_DATA[allowcomments]),ENT_QUOTES,'UTF-8');
					$theallowratings = htmlentities(strip_tags($_POST_DATA[allowratings]),ENT_QUOTES,'UTF-8');
					$theallowembeds = htmlentities(strip_tags($_POST_DATA[allowembeds]),ENT_QUOTES,'UTF-8');
					$theallowdownloads = htmlentities(strip_tags($_POST_DATA[allowdownloads]),ENT_QUOTES,'UTF-8');
					$themature = htmlentities(strip_tags($_POST_DATA[mature]),ENT_QUOTES,'UTF-8');
					$videoseries = htmlentities(strip_tags($_POST_DATA[videoseries]),ENT_QUOTES,'UTF-8');
					
					if ($videoseries != "" && is_numeric($videoseries) && $videoseries > 0)
					{
						$addseriesdata = ", SID='".mysql_real_escape_string($videoseries)."' ";
					}
					else
					{
						$addseriesdata = "";
					}

  					$query = "INSERT INTO videos SET USERID='$_SESSION[USERID]', title='".mysql_real_escape_string($thetitle)."', description='".mysql_real_escape_string($thedesc)."', tags='".mysql_real_escape_string($thetags)."', categories='".mysql_real_escape_string($thecat)."', filesize='$space', public='".mysql_real_escape_string($thepublic)."', time_added='".time()."', date_added='".date("Y-m-d")."', $active, allowcomments='".mysql_real_escape_string($theallowcomments)."', allowratings='".mysql_real_escape_string($theallowratings)."', allowembeds='".mysql_real_escape_string($theallowembeds)."', allowdownloads='".mysql_real_escape_string($theallowdownloads)."'$addseriesdata, mature='".mysql_real_escape_string($themature)."'";

					$conn->execute($query);
    				$videoid = mysql_insert_id();
        			
					$original = $videoid.".".$ph;
					$videor = $config['originalvdir'].'/'.$original;
	
					if(rename($upload_file_path,$videor))
					{
						if($config[vresize] == 1)
						{
							$encodecommand = "$config[mencoder] $config[originalvdir]/$original -o $config[videodir]/".$videoid.".flv -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=$config[vbitrate]:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -vop scale=$config[vresize_x]:$config[vresize_y] -srate $config[sbitrate]";
						}
						else 
						{
							$encodecommand="$config[mencoder] $config[originalvdir]/$original -o $config[videodir]/".$videoid.".flv -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=$config[vbitrate]:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -srate $config[sbitrate]";
						}

						exec("$config[phppath] $config[basedir]/bgconversion.php $original $videoid $videor> /dev/null &"); 
						
						
						


						generate_video_thumbs($videor, $videoid);
			
						exec("$config[mplayer] -vo null -ao null -frames 0 -identify $config[originalvdir]/$original", $difh);
    					
						while (list($k,$v)=each($difh))
    					{
        	    			if ($length = strstr($v,'ID_LENGTH='))
        	    				break;
    					}
						
    					$lx = explode("=",$length);
    					$runtime = $lx[1];

					} 
					else
					{
	    				$error = "$lang[689]";
					}
    			}
    
    			if($error == "")
    			{
					if ($addseriesdata != "")
					{
						$query = "UPDATE videos_series SET last_added='".time()."' WHERE SID='".mysql_real_escape_string($videoseries)."'";
        				$conn->execute($query);
					}

        			$query = "UPDATE videos SET original_video='$original', video_name='".$videoid.".flv', runtime='$runtime' WHERE VIDEOID='$videoid'";
        			$conn->execute($query);
        
        			header("Location:$config[baseurl]/upload_complete.php?videoid=$videoid");
    			}	
		}
		$templateselect = "uploadvideo.tpl";
	}
	else
	{
		$templateselect = "empty.tpl";
	}
}
else
{
	header("Location:$config[baseurl]/login.php");exit;
}

STemplate::assign('pagetitle',"$lang[42]");

//TEMPLATES BEGIN
STemplate::assign('error',$error);
STemplate::display('header.tpl');
STemplate::display($templateselect);
STemplate::display('footer.tpl');
//TEMPLATES END
?>