<?php
//=====================================
// LINKER Category Engine
//=====================================
// Filename : index.php
// Path  : Main directory
// last update : 2002/09/01
// Desc : View Main Page
//=====================================
//=====================================
// File include part
//=====================================
$time1 = microtime();
include ("./function/base_info.php");
include ("./function/main_func.php");
include ("./function/$Sdb_type.php");
include ("./function/template.php");
include ("./function/userfield_info.php"); 
$***_dir = $HTTP_COOKIE_VARS[cook_***];
if ($***_dir && file_exists("./$***_dir/u_common.php")) 
include ("./$***_dir/u_common.php"); 
else include ("./$Slang/u_common.php"); 
//=====================================
// Prepare progrom
//=====================================
isset($_GET) ? extract($_GET) : extract($HTTP_GET_VARS) ;
isset($_POST) ? extract($_POST) : extract($HTTP_POST_VARS) ;
$db = new DB_Sql($Sdb_host,$Sdb_user,$Sdb_pass,$Sselect_db);
$tpl = new Stemplate($Stemplate);
//=====================================
if (!$cat) {
$tpl->define(array( mpage	=>	 "main.html", mcol	 => "main_col.html" ));
$cat = 0;
$cols = $Smain_col;
}
else {
$tpl->define(array( mpage	=>	 "submain.html",  mcol => "sub_col.html" ));
$cols = $Ssub_col;
}
$tpl->dyn_define ( "tdcol", "mpage" );
//=====================================
// Get this Category config Data
//=====================================
$db->query("update {$Stb_name}_CAT set count = count+1 where sid = '$cat'");
$db->query("select * from {$Stb_name}_CAT where sid = '$cat'");
if (!$db->numrow) errmsg($l_error_nocat);
$cat_row = $db->nrecord();
//=====================================
// Main page head
//=====================================
$col_width = ceil(100 / $cols) . "%";
$tpl->assign(array(
DOMAIN			=> $Sdomain,
TEMPLATE	=> $Stemplate,
CHARSET		=> $Scharset,
BGCOLOR		=> $Sbgcolor,
MWIDTH	 		=> $Smain_width,
ALIGN			=> $Salign,
SID				=> $cat,
CWIDTH			=> $col_width	
));
//====================================
// Sub category data
//====================================
// parent = $cat
//$db->query("select * from  {$Stb_name}_CAT where parent = '$cat' order by title");
$db->query("select a.*,b.sub_cat as r_sc,b.sub_link as r_sl from {$Stb_name}_CAT as a left join  {$Stb_name}_CAT as b
on a.relate=b.sid where  a.parent='$cat'  order by title");
$total_row = $db->numrow;
for ($i = 0; $i <$total_row ;$i++) {
$row[$i] = $db->nrecord();
$temp_ar[$row[$i]["sid"]] = $i;
}
// pparent = $cat 
if ($cat_row[c_sub] == 2) 
{
	$db->query("select sid,parent,title,relate from {$Stb_name}_CAT where pparent='$cat' order by parent,title");
	while ($prow = $db->nrecord()) 
	{
		$tsid = $temp_ar[$prow["parent"]];
		if ($row[$tsid]["subnum"] < $Smain_sub_num) 
		{
		$row[$tsid]["subnum"]++;
		$cat_sid = ($prow[relate]) ? $prow["relate"] : $prow["sid"];
		$subsm[$tsid][] = "<a href='index.php?cat=$cat_sid'><font color=blue class=smallfont>$prow[title]</font></a>";
		}
	}
}
//=====================================
// Category Loop
//=====================================
$now_time = time(); // now timestamp for directory new mark
for ($col=0; $col<$cols ; $col++) 
{
$prev_col = ceil(count($row) * ($col / $cols ));
$next_col = ceil(count($row) * (($col+1) / $cols ));
	for ($i = $prev_col; $i < $next_col; $i++ ) 
	{
		// for directory new mark
		unset($dmark,$interval);
		if ($row[$i][bingo1]) 
		{
		    $interval = ceil( ($now_time - $row[$i][bingo1])/86400 );
		if ($interval <=1) $dmark="<img src='$Sdomain/$Stemplate/image/new_1.gif'>";	
		elseif ($interval<=3) $dmark="<img src='$Sdomain/$Stemplate/image/new_3.gif'>";
		elseif ($interval<=7) $dmark="<img src='$Sdomain/$Stemplate/image/new_7.gif'>";
		else $dmark = "";
		}
		// end directory new makr
		switch ($cat_row[c_sub]) 
		{
			case 2 : $sm = @implode(",&nbsp;" ,$subsm[$i]);break;
			case 1 : $sm = $row[$i]["descript"];	break;
			default: $sm = "";
		}
		if ($Smain_sub_num < $row[$i]["sub_cat"] && $cat_row["c_sub"]==2) 
		$sm .="...";
		if ($row[$i][relate]>0 ) {
		$row[$i]["title"] = "@".stripslashes($row[$i]["title"]);
		$row[$i]["sid"] = $row[$i]["relate"];
		}
		$subcat = ($row[$i]['relate']) ? $row[$i]['r_sc'] : $row[$i]['sub_cat'];
		$sublink = ($row[$i]['relate']) ? $row[$i]['r_sl'] : $row[$i]['sub_link'];
		$tpl->assign( array(
		CAT			=> $row[$i]["sid"],
		TITLE			=> stripslashes($row[$i]["title"]),
		DNEW		=> $dmark,
		SUBCAT		=> $subcat,
		SUBLINK		=> $sublink,
		SM			=> $sm
		));
		$tpl->parse("DROW",".mcol");
	}
	$tpl->parse("TDCOL", ".tdcol");
	$tpl->clear("DROW");
}
//====================================
// LINK output part 
//====================================
if ($cat) {
$tpl->define( array( link_row => "link.html"));
$db->query("select count(*) as TotalLink from {$Stb_name}_LINK where parent='$cat'");
if ($db->numrow) $Trow = $db->nrecord();
$TotalLink = $Trow["TotalLink"];
$page_num = ceil($TotalLink / $Spagecut);
$page = ($page) ? $page : 1;
$vstart = $Spagecut * ($page-1);
if ($Sorderpick) $pick_add = "c_pick desc,";
if (!$u_sort) $u_sort = $Ssort;
if (!$u_order) $u_order = $Sorder;
if ($Sordernew) 
{
	$newlink_time = time() - ( 7 * 86400 );
	$new_add1 = ", uptime >= $newlink_time as newlink ";
	$new_add2 = " newlink desc,";
}
$db->query("select sid,parent,pathtxt,title,url,name,email,scont,lcont,uptime,passwd,"
        ."c_new,c_pick,count,visit,comment,upfile,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,"
        ."user1,user2,user3,user4,user5 $new_add1 from {$Stb_name}_LINK where "
        ."parent = '$cat' order by $new_add2 $pick_add $u_sort $u_order limit $vstart,$Spagecut") ;
//============================
// Make Directbar button
//============================
$page_start = floor(($page-1)/ $Sdirectcut ) * $Sdirectcut ; 
$page_end = $page_start + $Sdirectcut; 
for ($p=$page_start+1 ; ($p <= $page_end) && ($p <= $page_num)  ; $p++ ) 
{	
	if ($page == $p) 	$direct_bar .= "[<b>$p</b>]";
	else 	$direct_bar .= "[<a href='index.php?page=$p&cat=$cat&u_sort=$u_sort&u_order=$u_order'><font class=midsmall>$p</font></a>]";
}
if ($TotalLink > $vstart+$Spagecut ) {		// Next button
$next_p=$page+1;
$next_list = "<a href='index.php?page=$next_p&cat=$cat&u_sort=$u_sort&u_order=$u_order'><img src='$Stemplate/image/next.gif' border=0 valign=middle></a>&nbsp;\n";
}
if ($page>1) {	// Prev button
$prev_p=$page-1;
$prev_list="<a href='index.php?page=$prev_p&cat=$cat&u_sort=$u_sort&u_order=$u_order'><img src='$Stemplate/image/prev.gif' border=0 valign=middle></a>&nbsp;\n";
}
//============================
// Link assign and make
//============================
	link_assign();
	while ($row = $db->nrecord()) {
	$link .= view_link($row);
	}
} // if ($cat)
//============================
// end if ($cat)
//============================
else 
{
    $db->query("select count(*) as total from {$Stb_name}_CAT");
    $t_catrow = $db->nrecord();
    $db->query("select count(*) as total from {$Stb_name}_LINK");
    $t_linkrow = $db->nrecord();
}
//====================================
// Output Main page
//====================================
show_menu($cat);
show_here($cat_row["path"],$cat_row["pathtxt"]);
// search bar
$cat_count = $cat_row["count"];
$cat_path = $cat_row["path"];
if ($cat) {
	$total_cat = $total_row;
	$total_link = $TotalLink;
	show_search(1,1);
	show_linkbar();
	$tpl->assign(array (
	LINK					=> $link,
	PREV					=> $prev_list,
	DIRECT				=> $direct_bar,
	NEXT					=> $next_list ));
		$pagetitle = $cat_row["title"];
}
else 
{
	$total_cat = $t_catrow["total"];
	$total_link = $t_linkrow["total"];
	show_search(1);
	if ($Stkword) show_tkword(5);
	if ($Sfmaillist) show_mailling();
	$pagetitle = $Ssite_name;
}
if (!$cat_row["meta_key"]) $cat_row["meta_key"] = $cat_row["title"] ;
$tpl->assign(array(
SNAME				=> $pagetitle,
CAT_HEADER		=> $cat_row["header"],
CAT_FOOTER		=> $cat_row["footer"],
META					=> $cat_row["meta_key"],
PAGE_HEADER  => implode("",file("$Stemplate/header.html")),
PAGE_FOOTER  => implode("",file("$Stemplate/footer.html"))
));
// index Language 
$tpl->assign(array( 
L_NOKWORD	=> $l_error_nokword,
L_NOADDR		=> $l_noaddr
));
$tpl->parse("MAIN","mpage");
$tpl->simprint("MAIN");
//====================================
// Eastablish Time
//====================================
if ($Stime == "Y") check_time($time1);
?>