<?php


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

if ($_SESSION[USERID] != "" && $_SESSION[USERID] >= 0 && is_numeric($_SESSION[USERID]))
{
	if($_REQUEST[submitdelete]!="")
	{
		$deletevideo = $_POST[deletevideo];
		$svcount = count($deletevideo);
		for ($i = 0; $i < $svcount; $i++)
		{
			if ($deletevideo[$i] != "" && $deletevideo[$i] >= 0 && is_numeric($deletevideo[$i]))
			{		
				$query = "SELECT * FROM videos WHERE VIDEOID='".mysql_real_escape_string($deletevideo[$i])."'";
				$executequery = $conn->execute($query);
				$theuserid = $executequery->fields['USERID'];
  				$thevideoname = $executequery->fields['video_name'];
				$videofilesize = $executequery->fields['filesize'];
				$videofilesize = floor($videofilesize);
				
				if(mysql_affected_rows()>=1)
				{
					$query = "UPDATE members_subscription SET used = used - $videofilesize WHERE USERID='".mysql_real_escape_string($_SESSION[USERID])."'";
					$executequery = $conn->execute($query);
					
  					$videopath = $config['videodir']."/".$deletevideo[$i].".flv";
  					@chmod($videopath, 0777);
  					
					if (file_exists($videopath))
					{
						@unlink($videopath);
					}
  
  					$originalpath = $config['originalvdir']."/".$thevideoname;
  					@chmod($originalpath, 0777);
  					
					if (file_exists($originalpath))
					{
						@unlink($originalpath);
					}

  					if($theuserid == $_SESSION[USERID])
  					{
						$deletefrom[] = "videos";
    					$deletefrom[] = "videos_comments";
    					$deletefrom[] = "videos_favorited";

    					for($j=0;$j < count($deletefrom);$j++)
    					{
      						$query = "DELETE FROM ".$deletefrom[$j]." WHERE VIDEOID='$deletevideo[$i]'";
      						$conn->Execute($query);
    					}
						
						for($tt=1;$tt<5;$tt++)
    					{
							$tempthumbs = $config[thumbdir]."/".$deletevideo[$i]."-".$tt.".jpg";
    						if(file_exists($tempthumbs)) 
							{
								@unlink($tempthumbs);
							}
						}
						
						for($tt=1;$tt<5;$tt++)
    					{
							$tempthumb = $config['tmpimgpath']."/".$deletevideo[$i]."/0000000".$tt.".jpg";
    						if(file_exists($tempthumb)) 
							{
								@unlink($tempthumb);
							}
						}

    					@rmdir($config['tmpimgpath']."/".$deletevideo[$i]);
						
						if ($svcount > 1)
						{
    						$message = "$lang[643]";
						}
						else
						{
							$message = "$lang[644]";
						}
  					} 
					else
					{
						if ($svcount > 1)
						{
    						$error = "$lang[645]";
						}
						else
						{
    						$error = "$lang[646]";
						}
  					}

				}
				
    			$query = "UPDATE subscriber SET videos=videos-1 WHERE USERID='".mysql_real_escape_string($_SESSION[USERID])."'";
				$conn->execute($query);
			}
		}
	}
	
	$getsortby = htmlentities(strip_tags($_REQUEST[sortby]));
	$sortby = escape($getsortby);
	$getpage = htmlentities(strip_tags($_REQUEST[page]));
	$page = escape($getpage);

	if ($sortby=="a")
	{
		$sort = "asc";
	}
	else
	{
		$sortby = "z";
		$sort = "desc";
	}

	if($page=="")
	{
		$page = "1";
	}
	$currentpage = $page;

	if ($page >=2)
	{
		$pagingstart = ($page-1)*$config[items_per_page];
	}
	else
	{
		$pagingstart = "0";
	}

	$query1 = "SELECT count(*) as total from videos where public='1' AND active='1' AND USERID='$_SESSION[USERID]' order by VIDEOID $sort limit $config[maximum_results]";
	$query2 = "SELECT * from videos where public='1' AND active='1' AND USERID='$_SESSION[USERID]' order by VIDEOID $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[14]";

	STemplate::assign('showtitle',$showtitle);
STemplate::assign('pagetitle',$showtitle);

	$executequery1 = $conn->Execute($query1);

	$totalvideos = $executequery1->fields['total'];
	if ($totalvideos > 0)
	{

	if($executequery1->fields['total']<=$config[maximum_results])
	{
		$total = $executequery1->fields['total'];
	}
	else
	{
		$total = $config[maximum_results];
	}

	$toppage = ceil($total/$config[items_per_page]);
	if($toppage==0)
	{
		$xpage=$toppage+1;
	}
	else
	{
		$xpage = $toppage;
	}

	$executequery2 = $conn->Execute($query2);
	$listvideos = $executequery2->getrows();
	$beginning=$pagingstart+1;
	$ending=$pagingstart+$executequery2->recordcount();

	$pagelinks="";
	$k=1;
	$theprevpage=$currentpage-1;
	$thenextpage=$currentpage+1;

	if ($currentpage > 0)
	{
		$pagelinks.="<ul>";
	
		if($currentpage > 1) 
		{
		$pagelinks.="<li><a href='$thebaseurl/myvideos.php?sortby=$sortby&page=1' title='first page'>$lang[611]</a></li>";
		$pagelinks.="<li class='page_separator'>...</li>";
		$pagelinks.="<li><a href='$thebaseurl/myvideos.php?sortby=$sortby&page=$theprevpage'>&laquo; $lang[612]</a></li>";
		}
	
		$counter=0;
	
		$lowercount = $currentpage-5;
		if ($lowercount <= 0) $lowercount = 1;
	
		while ($lowercount < $currentpage)
		{
			$pagelinks.="<li><a href='$thebaseurl/myvideos.php?sortby=$sortby&page=$lowercount'>$lowercount</a></li>";
			$lowercount++;
			$counter++;
		}
	
		$pagelinks.="<li class='page_current'>$currentpage</li>";
	
		$uppercounter = $currentpage+1;
	
		while (($uppercounter < $currentpage+10-$counter) && ($uppercounter<=$toppage))
		{
			$pagelinks.="<li><a href='$thebaseurl/myvideos.php?sortby=$sortby&page=$uppercounter'>$uppercounter</a></li>";
			$uppercounter++;
		}
	
		if($currentpage < $toppage) 
		{
		
		$pagelinks.="<li><a href='$thebaseurl/myvideos.php?sortby=$sortby&page=$thenextpage'>$lang[613] &raquo;</a></li>";
		$pagelinks.="<li class='page_separator'>...</li>";
		$pagelinks.="<li><a href='$thebaseurl/myvideos.php?sortby=$sortby&page=$toppage' title='last page'>$lang[614]</a></li>";
		}
	
		$pagelinks.="</ul>";

		}
	}
	else
	{
		$error = "$lang[647]";
		STemplate::assign('error',$error);
	}


}
else
{
	header("Location:$config[baseurl]/login.php");exit;
}

//TEMPLATES BEGIN
STemplate::assign('error',$error);
STemplate::assign('message',$message);
STemplate::assign('beginning',$beginning);
STemplate::assign('ending',$ending);
STemplate::assign('pagelinks',$pagelinks);
STemplate::assign('total',$total);
STemplate::assign('listvideos',$listvideos);
STemplate::display('header.tpl');
STemplate::display('myvideos.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>