<?php
/**************************************************************************************************
|
| iDocScript - Document Sharing Script
| http://www.idocscript.com
| webmaster@idocscript.com
|
|**************************************************************************************************
|
| By using this software you agree that you have read and acknowledged our End-User License 
| Agreement available at http://www.idocscript.com/eula.html and to be bound by it.
|
| Copyright (c) 2011 iDocScript.com. All rights reserved.
|**************************************************************************************************/

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']!="")
	{
		$deletedoc = $_POST['deletedoc'];
		$svcount = count($deletedoc);
		for ($i = 0; $i < $svcount; $i++)
		{
			if ($deletedoc[$i] != "" && $deletedoc[$i] >= 0 && is_numeric($deletedoc[$i]))
			{		
				$query = "SELECT USERID,doc_name FROM docs WHERE DID='".mysql_real_escape_string($deletedoc[$i])."'";
				$executequery = $conn->execute($query);
				$theuserid = $executequery->fields['USERID'];
				$doc_name = $executequery->fields['doc_name'];
				
				if(mysql_affected_rows()>=1)
				{					
  					$docpath = $config['docdir']."/".$doc_name;
  					@chmod($docpath, 0777);
  					
					if (file_exists($docpath))
					{
						@unlink($docpath);
					}

  					if($theuserid == $_SESSION['USERID'])
  					{
						$deletefrom[] = "docs";
    					$deletefrom[] = "docs_comments";
    					$deletefrom[] = "docs_favorited";

    					for($j=0;$j < count($deletefrom);$j++)
    					{
      						$query = "DELETE FROM ".$deletefrom[$j]." WHERE DID='$deletedoc[$i]'";
      						$conn->Execute($query);
    					}
						
						$tempthumbs = $config['thumbdir']."/".$deletedoc[$i].".jpg";
						if(file_exists($tempthumbs)) 
						{
							@unlink($tempthumbs);
						}
						
						if ($svcount > 1)
						{
    						$message = $lang['643'];
						}
						else
						{
							$message = $lang['644'];
						}
  					} 
					else
					{
						if ($svcount > 1)
						{
    						$error = $lang['645'];
						}
						else
						{
    						$error = $lang['646'];
						}
  					}

				}
			}
		}
	}
	
	$sortby = cleanit($_REQUEST['sortby']);
	$page = cleanit($_REQUEST['page']);

	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 docs where active='1' AND USERID='$_SESSION[USERID]' order by DID $sort limit $config[maximum_results]";
	$query2 = "SELECT * from docs where active='1' AND USERID='$_SESSION[USERID]' order by DID $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = $lang['466'];

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

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

	$totaldocs = $executequery1->fields['total'];
	if ($totaldocs > 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);
		$docs = $executequery2->getrows();
		$beginning=$pagingstart+1;
		$ending=$pagingstart+$executequery2->recordcount();
	
		$pagelinks="";
		$k=1;
		$theprevpage=$currentpage-1;
		$thenextpage=$currentpage+1;
		$added = "page=";
		if ($currentpage > 0)
		{
		
			if($currentpage > 1) 
			{
			$pagelinks.="<a href='$thebaseurl/mydocs?".$added."1' class='page'>$lang[611]</a>";
			$pagelinks.="<a href='$thebaseurl/mydocs?".$added."$theprevpage' class='page'>$lang[612]</a>";
			};
			
			$counter=0;
			
			$lowercount = $currentpage-5;
			if ($lowercount <= 0) $lowercount = 1;
			
			while ($lowercount < $currentpage)
			{
				$pagelinks.="<a href='$thebaseurl/mydocs?".$added."$lowercount' class='page'>$lowercount</a>";
				$lowercount++;
				$counter++;
			}
			
			$pagelinks.="<span class='current'>$currentpage</span>";
			
			$uppercounter = $currentpage+1;
			
			while (($uppercounter < $currentpage+10-$counter) && ($uppercounter<=$toppage))
			{
				$pagelinks.="<a href='$thebaseurl/mydocs?".$added."$uppercounter' class='page'>$uppercounter</a>";
				$uppercounter++;
			}
			
			if($currentpage < $toppage) 
			{
			
			$pagelinks.="<a href='$thebaseurl/mydocs?".$added."$thenextpage' class='page'>$lang[613]</a>";
			$pagelinks.="<a href='$thebaseurl/mydocs?".$added."$toppage' title='last page' class='page'>$lang[614]</a>";
			}
		
		}
	}
	else
	{
		$error = $lang['647'];
		STemplate::assign('error',$error);
	}
}
else
{
	$redirect = base64_encode($thebaseurl."/mydocs");
	header("Location:$config[baseurl]/login.php?redirect=$redirect");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('docs',$docs);
STemplate::display('header.tpl');
STemplate::display('mydocs.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>