<?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'];

STemplate::assign('mainmenu',1);
$did = intval(cleanit($_REQUEST['did']));
if($did > 0)
{
	STemplate::assign('did',$did);
	if (does_doc_exist($did) && private_doc_access_check($did,$_SESSION['USERID']))
	{
		update_viewcount_doc($did);
		$query = "SELECT A.*, B.username FROM docs A, members B WHERE A.USERID=B.USERID AND DID='".mysql_real_escape_string($did)."'";
       	$executequery = $conn->execute($query);
       	$docarray = $executequery->getarray();
		STemplate::assign('docarray',$docarray);
		$docuserid = $docarray[0]['USERID'];
		
		STemplate::assign('pagetitle',$docarray[0]['title']." ".$lang['668']);
		
		// Related Docs Begin
		$doctags[] = $docarray[0][tags];
		$doctags[0] = str_replace("'", "''", $doctags[0]);
		$doctags[0] = str_replace("  ", "", $doctags[0]);
		$doctags[0] = str_replace("-", "", $doctags[0]);
		$doctagssplit = explode(" ",$doctags[0]);
		$stripapos = str_replace("'", "''", $docarray[0][tags]);
		$doctagsstr = "%".$stripapos."%' ";
		
		if (count($doctagssplit)>=1)
		{
			for($i=0;$i<count($doctagssplit);$i++)
			{
				if ($doctagssplit[$i] != "" && $doctagssplit[$i] != "-" && $doctagssplit[$i] != " ")
				{
					$doctagsstr.="or tags like '%$doctagssplit[$i]%' ";
					$doctagsstr.="or title like '%$doctagssplit[$i]%' ";
					$doctagsstr.="or description like '%$doctagssplit[$i]%' ";
				}
			}
		}
		$doctagsstr .= "' ";
		$maxreldocs = $config['max_related_docs_pv'];
		$query2 = "SELECT DID,title FROM docs WHERE (tags like '$doctagsstr') AND DID!='".mysql_real_escape_string($did)."' order by rand() limit $maxreldocs";
       	$executequery2 = $conn->execute($query2);
       	$docs = $executequery2->getarray();
		STemplate::assign('docs',$docs);
		// Related Docs End

		$USERID = $docarray[0][USERID];
		STemplate::assign('USERID',$USERID);
		$DID = $docarray[0][DID];
		STemplate::assign('DID',$DID);
	
		update_last_viewed_doc($DID);
		update_your_docs_viewed($USERID);
		if (session_verification())
		{
			update_docs_you_viewed($_SESSION[USERID]);
		}
		$query = "SELECT A.*, B.username FROM docs_comments A, members B WHERE DID='".mysql_real_escape_string($DID)."' AND A.USERID=B.USERID ORDER BY CID desc";
		$executequery = $conn->execute($query);
		$showcomments =  $executequery->getarray();
		STemplate::assign('showcomments',$showcomments);

	}	
	else if (!private_doc_access_check($did,$_SESSION['USERID']))
	{
		$query = "SELECT USERID FROM docs WHERE DID='".mysql_real_escape_string($did)."'";
       	$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'];
	}
}
else
{
	$error = $lang['568'];
}


//TEMPLATES BEGIN
STemplate::assign('error',$error);
STemplate::assign('message',$message);
STemplate::display('header.tpl');
if ($error == "")
{
	STemplate::display('viewdoc.tpl');
}
else
{
	STemplate::display('empty.tpl');
}
STemplate::display('footer.tpl');
//TEMPLATES END
?>