<?php


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

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

// Verify valid video
if (is_numeric($_REQUEST[videoid]))
{
	$videoid = $_REQUEST[videoid];
	STemplate::assign('videoid',$videoid);
}
else
{
	$error = "$lang[568]";
}

if ($error == "")
{
	if (does_video_exist($videoid) && private_video_access_check($videoid,$_SESSION[USERID]))
	{
		$query = "SELECT * FROM videos WHERE VIDEOID='".mysql_real_escape_string($videoid)."'";
       	$executequery = $conn->execute($query);
       	$videoarray = $executequery->getarray();
		STemplate::assign('videoarray',$videoarray);
		$videouserid = $videoarray[0][USERID];
		
		STemplate::assign('pagetitle',$videoarray[0][title]." Video");
		
		// Related Videos Begin
		$videotags[] = $videoarray[0][tags];
		$videotags[0] = str_replace("'", "''", $videotags[0]);
		$videotags[0] = str_replace("  ", "", $videotags[0]);
		$videotags[0] = str_replace("-", "", $videotags[0]);
		$videotagssplit = explode(" ",$videotags[0]);
		$stripapos = str_replace("'", "''", $videoarray[0][tags]);
		$videotagsstr = "%".$stripapos."%' ";
		
		if (count($videotagssplit)>1)
		{
			for($i=0;$i<count($videotagssplit);$i++)
			{
				if ($videotagssplit[$i] != "" && $videotagssplit[$i] != "-" && $videotagssplit[$i] != " ")
				{
					$videotagsstr.="or tags like '%$videotagssplit[$i]%' ";
					$videotagsstr.="or title like '%$videotagssplit[$i]%' ";
				}
			}
		}
		$videotagsstr .= "' ";
		$maxrelvids = $config['max_related_videos_pv'];
		$query2 = "SELECT * FROM videos WHERE (tags like '$videotagsstr') AND VIDEOID!='".mysql_real_escape_string($videoid)."' order by rand() limit $maxrelvids";
       	$executequery2 = $conn->execute($query2);
       	$relatedvideos = $executequery2->getarray();
		STemplate::assign('relatedvideos',$relatedvideos);
		// Related Videos End
		
		// Member Videos Begin
		$maxmemvids = $config['max_user_videos_pv'];
		$query3 = "SELECT * FROM videos WHERE USERID='$videouserid' AND VIDEOID!='".mysql_real_escape_string($videoid)."' order by VIDEOID desc limit $maxmemvids";
       	$executequery3 = $conn->execute($query3);
       	$membervideos = $executequery3->getarray();
		STemplate::assign('membervideos',$membervideos);
		// Member Videos End
		
		// Newest Videos Begin
		$maxnewvids = $config['max_new_videos_pv'];
		$query4 = "SELECT * FROM videos WHERE VIDEOID!='".mysql_real_escape_string($videoid)."' order by VIDEOID desc limit $maxnewvids";
       	$executequery4 = $conn->execute($query4);
       	$newestvideos = $executequery4->getarray();
		STemplate::assign('newestvideos',$newestvideos);
		// Newest Videos End
		
		$ismaturevideo = $videoarray[0][mature];
		if ($ismaturevideo == "1")
		{
			if ($_SESSION[USERID] != "" && $_SESSION[USERID] >= 0 && is_numeric($_SESSION[USERID]))
			{
				if ($_SESSION[FAMILYFILTER] == "0")
				{
					$ismemberauthorized = true;	
				}
				else
				{
					$ismemberauthorized = false;
				}
			}
			else
			{
				$ismemberauthorized = false;	
			}
		}
		else
		{
			$ismemberauthorized = true;	
		}

		if ($ismemberauthorized)
		{
			$USERID = $videoarray[0][USERID];
			STemplate::assign('USERID',$USERID);
			$VIDEOID = $videoarray[0][VIDEOID];
			STemplate::assign('VIDEOID',$VIDEOID);
		
			update_viewcount_video($VIDEOID);
			update_last_viewed_video($VIDEOID);
			update_your_videos_viewed($USERID);
			if (session_verification())
			{
				update_videos_you_viewed($_SESSION[USERID]);
			}
			$query2="SELECT value FROM config WHERE setting='max_comments'";
			$executequery2=$conn->execute($query2);
			$max_comments = $executequery2->fields[value];
			STemplate::assign('max_comments',$max_comments);
    		$query = "SELECT * FROM videos_comments WHERE VIDEOID='$VIDEOID' ORDER BY CID desc limit $max_comments";
    		$executequery = $conn->execute($query);
    		$showcomments =  $executequery->getarray();
			STemplate::assign('showcomments',$showcomments);
			$showtheme = "playvideo.tpl";
		}
		else
		{
			$showtheme = "playvideoverifymature.tpl";
		}
	}	
	else if (!private_video_access_check($videoid,$_SESSION[USERID]))
	{
		$query = "SELECT USERID FROM videos WHERE VIDEOID='".mysql_real_escape_string($videoid)."'";
       	$executequery = $conn->execute($query);
       	$theowneruserid = $executequery->fields[USERID];
		$query = "SELECT username FROM members WHERE USERID='".mysql_real_escape_string($theowneruserid)."'";
       	$executequery = $conn->execute($query);
       	$theownerusername = $executequery->fields[username];
		
		$error = "$lang[569] <a href='".$thebaseurl."/members/profile/".$theowneruserid."/".$theownerusername."'>".$theownerusername."</a>.";
	}
	else
	{
		$error = "$lang[570]";
	}
}


//TEMPLATES BEGIN
STemplate::assign('error',$error);
STemplate::assign('message',$message);

STemplate::display('header.tpl');
if ($error == "")
{
	STemplate::display($showtheme);
}
else
{
	STemplate::display('playvideoerror.tpl');
}
STemplate::display('footer.tpl');
//TEMPLATES END
?>