Location: PHPKode > projects > PHPDug > index.php
<?php
require_once('includes/config.php');
require_once('includes/functions/func.global.php');
require_once('includes/classes/class.template_engine.php');
require_once('includes/lang/lang_'.$config['lang'].'.php');

// Check if the script is installed
checkinstall($config);

// Connect to database
db_connect($config);

// Start session
session_start();

// Check if the user has a remember cookie set
checkremember($config);

// Get site categories
$cats = get_cats($config,$lang);

// Get Settings
$settings = get_settings('index.php',$config);

// Get usergroup details
$usergroup = check_user_group($config);

// Initiate story array
$stories = array();
$avatars = array();

// If no page is specified set to first page
if(!isset($_GET['page']))
{
	$_GET['page'] = 1;
}
else
{
	$_GET['page'] = makeInt($_GET['page']);
}

// Retreive stories from database
if($config['rating_mode'] == 0)
{
	$query = "SELECT story_id,story_title,story_desc,story_cat,story_digs,story_url,story_comments,user_name,user_id,story_thumb,story_time,story_tags FROM ".$config['db']['pre']."stories ORDER BY story_rating ASC LIMIT ".validate_input(($_GET['page']-1)*10).",10";
}
else
{
	$query = "SELECT story_id,story_title,story_desc,story_cat,story_digs,story_url,story_comments,user_name,user_id,story_thumb,story_time,story_tags FROM ".$config['db']['pre']."stories WHERE story_prom='1' ORDER BY story_rating ASC LIMIT ".validate_input(($_GET['page']-1)*10).",10";
}
$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
while ($info = @mysql_fetch_array($query_result))
{
	$info['story_title'] = stripslashes($info['story_title']);
	$info['story_desc'] = stripslashes($info['story_desc']);
	$info['story_tags'] = stripslashes($info['story_tags']);

	$stories[$info['story_id']] = $info;
	$stories[$info['story_id']]['dugg'] = 0;
	$stories[$info['story_id']]['buried'] = 0;
	$stories[$info['story_id']]['cat_title'] = $cats[$info['story_cat']]['cat_title'];
	$stories[$info['story_id']]['cat_id'] = $info['story_cat'];
	$stories[$info['story_id']]['ago'] = time_taken((time()-$info['story_time']));
	$stories[$info['story_id']]['domain'] = getDomain($info['story_url']);
	$stories[$info['story_id']]['tags'] = $info['story_tags'];
	$stories[$info['story_id']]['tags_links'] = parse_tags($config,$info['story_tags']);
	
	$avatars[$info['user_id']] = $info['user_id'];
	
	if($config['mod_rewrite'] == 1)
	{
		$stories[$info['story_id']]['story_link'] = $config['site_url'].'stories/'.$info['story_id'].'/'.modrewriteurl($info['story_title']).'.html';
		$stories[$info['story_id']]['cat_link'] = $config['site_url'].'view/'.$info['story_cat'].'/'.modrewriteurl($cats[$info['story_cat']]['cat_title']).'/1.html';
		$stories[$info['story_id']]['user_link'] = $config['site_url'].'users/'.$info['user_id'].'/'.$info['user_name'].'/1.html';
	}
	else
	{
		$stories[$info['story_id']]['story_link'] = $config['site_url'].'story.php?id='.$info['story_id'];
		$stories[$info['story_id']]['cat_link'] = $config['site_url'].'cat.php?i='.$info['story_cat'];
		$stories[$info['story_id']]['user_link'] = $config['site_url'].'profile.php?id='.$info['user_id'];
	}
	
	if(!isset($_SESSION['dugg'][$info['story_id']]))
	{
		if(isset($story_where))
		{
			$story_where.= " OR story_id='".$info['story_id']."'";
		}
		else
		{
			$story_where = "story_id='".$info['story_id']."'";
		}
	}
	
	if(!isset($_SESSION['bury'][$info['story_id']]))
	{
		if(isset($story_where2))
		{
			$story_where2.= " OR story_id='".$info['story_id']."'";
		}
		else
		{
			$story_where2 = "story_id='".$info['story_id']."'";
		}
	}
}

$av_count = count($avatars);

if($av_count > 0)
{
	foreach ($avatars as $key => $info) 
	{
		if(isset($avatar_where))
		{
			$avatar_where.= " OR user_id='".$key."'";
		}
		else
		{
			$avatar_where = "user_id='".$key."'";
		}
	}
	$avatar_where.= "LIMIT ".$av_count;
	
	$query = "SELECT user_id,avatar FROM ".$config['db']['pre']."users WHERE ".$avatar_where;
	$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
	while ($info = @mysql_fetch_array($query_result))
	{
		$avatars[$info['user_id']] = getUserAvatar($info['user_id'],$config,false,$info['avatar']);
	}
}

if($config['rating_mode'] == 0)
{
	$total[0] = mysql_num_rows(mysql_query("SELECT 1 FROM ".$config['db']['pre']."stories"));
}
else
{
	$total[0] = mysql_num_rows(mysql_query("SELECT 1 FROM ".$config['db']['pre']."stories WHERE story_prom='1'"));
}

if(isset($story_where))
{
	if(isset($_SESSION['duser']['id']))
	{
		$query = "SELECT story_id FROM ".$config['db']['pre']."digs WHERE (user_id='".validate_input($_SESSION['duser']['id'])."' AND (".$story_where."))";
		$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
		while ($info = @mysql_fetch_array($query_result))
		{
			$stories[$info['story_id']]['dugg'] = 1;
		}
	}
	else
	{
		if($usergroup['group_dig'] == '1')
		{
			$user_ip = encode_ip($_SERVER,$_ENV);
			$longip = ip2long($user_ip);
			
			$query = "SELECT story_id FROM ".$config['db']['pre']."digs WHERE (user_ip='".validate_input($longip)."' AND (".$story_where."))";
			$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
			while ($info = @mysql_fetch_array($query_result))
			{
				$stories[$info['story_id']]['dugg'] = 1;
			}
		}
	}
}

if(isset($story_where2))
{
	if(isset($_SESSION['duser']['id']))
	{
		$query = "SELECT story_id FROM ".$config['db']['pre']."buries WHERE (user_id='".validate_input($_SESSION['duser']['id'])."' AND (".$story_where2."))";
		$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
		while ($info = @mysql_fetch_array($query_result))
		{
			$stories[$info['story_id']]['buried'] = 1;
		}
	}
}

foreach ($stories as $key => $info) 
{
	if(!$stories[$info['story_id']]['dugg'])
	{
		if(isset($_SESSION['dugg'][$info['story_id']]))
		{
			$stories[$info['story_id']]['dugg'] = 1;
		}
	}
	
	if(!$stories[$info['story_id']]['buried'])
	{
		if(isset($_SESSION['bury'][$info['story_id']]))
		{
			$stories[$info['story_id']]['buried'] = 1;
		}
	}
	
	$stories[$info['story_id']]['avatar'] = $avatars[$info['user_id']];
}

// Output data to the index.html template
if($total[0])
{
	$page = new HtmlTemplate ('templates/' . $config['tpl_name'] . '/index.html');
}
else
{
	$page = new HtmlTemplate ('templates/' . $config['tpl_name'] . '/index_noresults.html');
}
$page->SetParameter ('MODE','popular');
$page->SetParameter('ANOUNCE',$config['anounce_en']);
$page->SetParameter('ANOUNCE_TEXT',$config['anounce_txt']);
$page->SetLoop ('CATS', $cats);
$page->SetLoop ('STORIES', $stories);
if($config['mod_rewrite'])
{
	$page->SetParameter ('UPCOMING_LINK',$config['site_url'].'upcoming/0/viewall/1.html');
	$page->SetParameter ('POPULAR_LINK',$config['site_url'].'viewall/1.html');
}
else
{
	$page->SetParameter ('UPCOMING_LINK',$config['site_url'].'upcoming.php?id=0');
	$page->SetParameter ('POPULAR_LINK',$config['site_url'].'index.php');
}
$page->SetParameter ('XML_URL', $config['site_url'].'xmlfeeds.php?cmd=popular');
$page->SetParameter ('OVERALL_HEADER', create_header($config,$lang,$cats,$lang['VIEWALL']));
$page->SetParameter ('OVERALL_FOOTER', create_footer($config,$lang));
if(isset($_SESSION['duser']['id']))
{
	$page->SetParameter ('LOGGEDIN', 1);
}
else
{
	$page->SetParameter ('LOGGEDIN', 0);
}
if($config['cron_type'])
{
	if((time()-300) > $settings['cron_time'])
	{
		$page->SetParameter ('CRON', 1);
		// Output unix timestamp to stop image caching
		$page->SetParameter ('TIMESTAMP',time());
	}
	else
	{
		$page->SetParameter ('CRON', 0);
	}
}
else
{
	$page->SetParameter ('CRON', 0);
}
if($config['mod_rewrite'])
{
	$page->SetLoop ('PAGES', pagenav2($total[0],$_GET['page'],10,'index.php',0,0,$config,'',$lang));
}
else
{
	$page->SetLoop ('PAGES', pagenav($total[0],$_GET['page'],10,$config['site_url'].'index.php',0,$lang));
}
$page->CreatePageEcho($lang,$config);
?>
Return current item: PHPDug