<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.6 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

session_start();
require('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');

$page = $_GET['page'];

if(empty($page) || !is_numeric($page) || $page == '')
	$page = 1;
	
$limit = _BROWSER_PAGE;
$from = $page * $limit - ($limit);
$total_results = 0;
$err = '';

$tag = urldecode(trim($_GET['t']));
$tag = str_replace( array("%", "<", ">"), '', $tag);

if($tag != '' && strlen($tag) > 1)
{
	$tag = safe_tag($tag);
	$tag = secure_sql($tag);
	
	$sql = "SELECT uniq_id FROM pm_tags WHERE safe_tag LIKE '".$tag."'";
	$result = mysql_query($sql);// or die(mysql_error());
	if(!$result)
	{
		$err = "<center><br><b>".$lang['search_results_msg1']."</b></center>";
	}
	else
	{
		$v_ids = array();
		$videos = '';
		$total_results = mysql_num_rows($result);
		while($row = mysql_fetch_assoc($result))
		{
			$v_ids[] = $row['uniq_id'];
			$videos .= "'".$row['uniq_id']."', ";
		}
		mysql_free_result($result);
		$videos = substr($videos, 0, -2);

		if($total_results > 0)
		{
			$sql = "SELECT * FROM pm_videos WHERE uniq_id IN(".$videos.")";
			$sql .= " LIMIT ".$from.", ".$limit;
			$result = mysql_query($sql) or die($lang['tag_msg1']." <b>".__LINE__."</b> in <b>".__FILE__."</b>");	// or die(mysql_error());
			$j = 0; 
			$item = '';
			while ($row = mysql_fetch_array($result)) 
			{ 
			// ** Mark as new ** //
			$sql_date = date('Y-m-d', $row['added']);
			$date_diff = round( abs(strtotime(date('Y-m-d'))-strtotime($sql_date)) / 86400, 0 );

			$item .= "<li class=\"item column\"><a class=\"row-img\" href=\"".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."\"><img src=\"".show_thumb($row['uniq_id'])."\"  alt=\"".$row['video_title']."\" /></a>";
			$item .= "<div class=\"top-list-info-box\"><span>".fewchars($row['artist'], 15)."</span>";
			$item .= "<span><a href=\"".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."\">".fewchars($row['video_title'], 15)."</a></span><span class=\"view-count\">". pm_number_format($row['site_views']) ."  views </span></div>";
			$item .= "<div class=\"tag\"></div></li>";
			} 
			mysql_free_result($result);
		}
		else
		{
			$err = "<center><br><b>".$lang['search_results_msg1']."</b></center>";
		}
	}
}
else
{	//	the tag is either too short, or no tag was given
	$err = "<center><br><b>".$lang['search_results_msg1']."</b></center>";
}

// generate pagination
$uri = $_SERVER['REQUEST_URI'];
$uri = explode('?', $uri);

if (strlen($uri[1]) == 0)
{
	$uri[1] ='t='. $_GET['t'];
}

$filename = ('' != $_SERVER['PHP_SELF']) ? basename($_SERVER['PHP_SELF']) : 'tag.php';

$adjacents = 1;
$targetpage = (_SEOMOD) ? $uri[0] : $filename;
if(_SEOMOD)
{
	if($targetpage[strlen($targetpage)-1] != "/")
		$targetpage .= "/";
	if( ! @preg_match('#/page-([0-9]+)/#', $targetpage))
	{
		$targetpage .= 'page-1/';
	}
}
$totalitems = $total_results;
$pagestring = $uri[1];
$seomod = _SEOMOD;
$pagination = '';
//-----------------------------------------
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($totalitems / $limit);
$lpm1 = $lastpage - 1;
//echo $targetpage;
$seo_replace_regex = '/page-([0-9]*)/';
$preg_replace_regex = '/page=([0-9]*)/';

if($seomod == 1)
{
	$pagestring1 = preg_replace($seo_replace_regex, 'page-1', $targetpage);
	$pagestring2 = preg_replace($seo_replace_regex, 'page-2', $targetpage);
	$pagestringlpm1 = preg_replace($seo_replace_regex, 'page-'.$lpm1, $targetpage);
	$pagestringlast = preg_replace($seo_replace_regex, 'page-'.$lastpage, $targetpage);
}
else
{
	if(strpos($pagestring, 'page=', 0) === FALSE)
		$pagestring .= "&page=";
	
	$pagestring1 = preg_replace($preg_replace_regex, 'page=1', $pagestring);
	$pagestring2 = preg_replace($preg_replace_regex, 'page=2', $pagestring);
	$pagestringlpm1 = preg_replace($preg_replace_regex, 'page='.$lpm1, $pagestring);
	$pagestringlast = preg_replace($preg_replace_regex, 'page='.$lastpage, $pagestring);
}	
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\"";
		if($margin || $padding)
		{
			$pagination .= " style=\"";
			if($margin)
				$pagination .= "margin: $margin;";
			if($padding)
				$pagination .= "padding: $padding;";
			$pagination .= "\"";
		}
		$pagination .= ">";

		//previous button
		if ($page > 1)
		{
			if($seomod == 1)
			{
				$url_query = preg_replace($seo_replace_regex, 'page-'.$prev, $targetpage); 
				$pagination .= "<a href=\"$url_query\">&laquo; prev</a>";
			}
			else
			{
				$url_query = preg_replace($preg_replace_regex, 'page='.$prev, $pagestring);
				$pagination .= "<a href=\"$targetpage?$url_query\">&laquo; prev</a>";
			}
		}
		else
				$pagination .= "<span class=\"disabled\">&laquo; prev</span>";
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination .= "<span class=\"current\">$counter</span>";
				else
				{
					if($seomod == 1)
					{
						$url_query = preg_replace($seo_replace_regex, 'page-'.$counter, $targetpage); 
						$pagination .= "<a href=\"$url_query\">$counter</a>";
					}
					else
					{
						$url_query = preg_replace($preg_replace_regex, 'page='.$counter, $pagestring);
						$pagination .= "<a href=\"$targetpage?$url_query\">$counter</a>";
					}
				}					
			}
		}
		elseif($lastpage >= 7 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 2 + ($adjacents * 3))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination .= "<span class=\"current\">$counter</span>";
					else
					{
						if($seomod == 1)
						{
							$url_query = preg_replace($seo_replace_regex, 'page-'.$counter, $targetpage); 
							$pagination .= "<a href=\"$url_query\">$counter</a>";
						}
						else
						{
							$url_query = preg_replace($preg_replace_regex, 'page='.$counter, $pagestring);
							$pagination .= "<a href=\"$targetpage?$url_query\">$counter</a>";
						}
//							$url_query = preg_replace($preg_replace_regex, 'page='.$counter, $pagestring);
//							$pagination .= "<a href=\"$targetpage?$url_query\">$counter</a>";	
					}				
				}
				$pagination .= "...";
				if($seomod == 1)
				{					
					$pagination .= "<a href=\"$pagestringlpm1\">$lpm1</a>";
					$pagination .= "<a href=\"$pagestringlast\">$lastpage</a>";		
				}
				else
				{
					$pagination .= "<a href=\"$targetpage?$pagestringlpm1\">$lpm1</a>";
					$pagination .= "<a href=\"$targetpage?$pagestringlast\">$lastpage</a>";		
				}
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				if($seomod == 1)
				{					
					$pagination .= "<a href=\"$pagestring1\">1</a>";
					$pagination .= "<a href=\"$pagestring2\">2</a>";		
				}
				else
				{
					$pagination .= "<a href=\"$targetpage?$pagestring1\">1</a>";
					$pagination .= "<a href=\"$targetpage?$pagestring2\">2</a>";
				}				
				$pagination .= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination .= "<span class=\"current\">$counter</span>";
					else
					{
						if($seomod == 1)
						{
							$url_query = preg_replace($seo_replace_regex, 'page-'.$counter, $targetpage); 
							$pagination .= "<a href=\"$url_query\">$counter</a>";
						}
						else
						{
							$url_query = preg_replace($preg_replace_regex, 'page='.$counter, $pagestring);
							$pagination .= "<a href=\"$targetpage?$url_query\">$counter</a>";
						}						
					}
				}
				$pagination .= "...";
				if($seomod == 1)
				{					
					$pagination .= "<a href=\"$pagestringlpm1\">$lpm1</a>";
					$pagination .= "<a href=\"$pagestringlast\">$lastpage</a>";		
				}
				else
				{
					$pagination .= "<a href=\"$targetpage?$pagestringlpm1\">$lpm1</a>";
					$pagination .= "<a href=\"$targetpage?$pagestringlast\">$lastpage</a>";		
				}					
			}
			//close to end; only hide early pages
			else
			{
				if($seomod == 1)
				{					
					$pagination .= "<a href=\"$pagestring1\">1</a>";
					$pagination .= "<a href=\"$pagestring2\">2</a>";		
				}
				else
				{
					$pagination .= "<a href=\"$targetpage?$pagestring1\">1</a>";
					$pagination .= "<a href=\"$targetpage?$pagestring2\">2</a>";
				}				
				$pagination .= "...";
				for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination .= "<span class=\"current\">$counter</span>";
					else
					{
						if($seomod == 1)
						{
							$url_query = preg_replace($seo_replace_regex, 'page-'.$counter, $targetpage); 
							$pagination .= "<a href=\"$url_query\">$counter</a>";
						}
						else
						{
							$url_query = preg_replace($preg_replace_regex, 'page='.$counter, $pagestring);
							$pagination .= "<a href=\"$targetpage?$url_query\">$counter</a>";
						}
					}
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
		{
			if($seomod == 1)
			{
				$url_query = preg_replace($seo_replace_regex, 'page-'.$next, $targetpage); 
				$pagination .= "<a href=\"$url_query\">next &raquo;</a>";
			}
			else
			{
				$url_query = preg_replace($preg_replace_regex, 'page='.$next, $pagestring);
				$pagination .= "<a href=\"$targetpage?$url_query\">next &raquo;</a>";
			}				
		}
		else
			$pagination .= "<span class=\"disabled\">next &raquo;</span>";				
		$pagination .= "</div>\n";
	}
//	}	
$pagination;

//-----------------------------------------

//	Get tag's real name;
$real_tag = '';
$real_tag = safe2tag($tag);
$real_tag = ($real_tag === false) ? '' : $real_tag;

if($config['show_tags'] == 1)
{
	$tag_cloud = tag_cloud(0, $config['tag_cloud_limit'], $config['shuffle_tags']);
	$smarty->assign('tags', $tag_cloud);
	$smarty->assign('show_tags', 1);
}
// define meta tags & common variables
$meta_title = _SITENAME.' - '.htmlspecialchars($real_tag);
$meta_description = '';
// end

$smarty->assign('tags', $tag_cloud);
$smarty->assign('error_msg', $err);
//$smarty->assign('searchstring', htmlspecialchars($_GET['t']));
$smarty->assign('searchstring', htmlspecialchars($real_tag));
$smarty->assign('results', $item);
$smarty->assign('pagination', $pagination);
// --- DEFAULT SYSTEM FILES - DO NOT REMOVE --- //
$smarty->assign('meta_title', $meta_title);
$smarty->assign('meta_description', $meta_description);
$smarty->assign('template_dir', $template_f);
$smarty->display('tag.tpl');
?>