<?php


session_start();
include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];
	
if($_REQUEST[submitsearch]!="")
{
	$searchterm = htmlentities(strip_tags($_REQUEST[searchterm]));
	$searchterm = escape($searchterm);
	$searchfor = htmlentities(strip_tags($_REQUEST[searchfor]));
	$searchfor = escape($searchfor);
	
	if ($searchfor == "videos" || $searchfor == "members" || $searchfor == "series" || $searchfor == "channels")
	{
		if ($searchterm != "")
		{
			$query="SELECT * FROM search_phrases WHERE type='".mysql_real_escape_string($searchfor)."' AND phrase='".mysql_real_escape_string($searchterm)."'";
			$executequery=$conn->execute($query);
			
			if(mysql_affected_rows()>=1)
			{
				$SID=$executequery->fields['SID'];
			}
			
			if (is_numeric($SID) && $SID != "")
			{
				$query="UPDATE search_phrases SET searchcount=searchcount+1 WHERE SID='".mysql_real_escape_string($SID)."'";
				$executequery=$conn->execute($query);
			}
			else
			{
				$query="INSERT INTO search_phrases SET type='".mysql_real_escape_string($searchfor)."', phrase='".mysql_real_escape_string($searchterm)."'";
				$executequery=$conn->execute($query);
			}
		}
	}
	
	if ($searchfor == "")
	{
		header("Location:$thebaseurl/search/videos/a/1/$searchterm");exit;
	}
	elseif ($searchfor == "videos")
	{
		header("Location:$thebaseurl/search/videos/a/1/$searchterm");
	}
	elseif ($searchfor == "members")
	{
		header("Location:$thebaseurl/search/members/a/1/$searchterm");
	}
	elseif ($searchfor == "series")
	{
		header("Location:$thebaseurl/search/series/a/1/$searchterm");
	}
	elseif ($searchfor == "channels")
	{
		header("Location:$thebaseurl/search/channels/a/1/$searchterm");
	}
	else
	{
		header("Location:$thebaseurl/search/videos/a/1/");exit;
	}
}
else
{
	header("Location:$thebaseurl/search/videos/a/1/");exit;
}

?>