<?php
if(!checkactive('links')) include(dirname(__FILE__)."/lib/notactive.php" );

if (!permission('candlink')) {
	$EOUT = stderror("nopermission", $lang['nopermission_t'], str_replace("__URL__", "index.php", $lang['redirect']), "index.php", 1);
	$error = 1;
	$NOOUT = 1;
}

$getid = ($_REQUEST['parent']!="") ? escs($_REQUEST['parent']) : 0 ;
$extra = ($_REQUEST['categ']!="") ? escs($_REQUEST['categ']) : 0 ;


//=====================================================================
// <<-- DATEIEN AUSLESEN UND IN ARRAY VERFRACHTEN -->>
//=====================================================================
$searchstring = kspecialchars(escs($_REQUEST['qlink']));
if(strlen($searchstring) >=3 ) {
	$sql = new dbc("SELECT ctime,hits,title,id,catid FROM " . PREFIX . "_links WHERE title like '%$searchstring%' or text like '%$searchstring%' AND active='1'");
	$results = array();
	while($item= $sql->fetchrow()){
		$query = new dbc("SELECT group_id FROM " . PREFIX . "_linkcat WHERE catid = '$item->catid'");
		$row = $query->fetchrow();
		$g_array = @explode(",", $row->group_id);
		if(in_array(UGROUP,$g_array)){
			$item->ctime = mygmt($item->ctime);
			array_push($results, $item);
		}
	}
	
	$tmpl->assign('qlink', stripslashes($searchstring));
	$tmpl->assign('results', $results);
	$tmpl->assign('showsearch', 1);
	
	} else {
	
	//=====================================================================
	// <<-- KATEGORIEN AUSLESEN (GRUPPEN WERDEN BERÜCKSICHTIGT) -->>
	//=====================================================================
	$dlcat = array();
	getLinkcategs($getid, '', $dlcat, $area, $extra);
	
	switch($_REQUEST['sort']){
		case "" :
		case "date" :
		$sort = " ORDER BY ctime DESC";
		break;
		
		case "hits" :
		$sort = " ORDER BY hits DESC";
		break;
		
		case "name" :
		$sort = " ORDER BY title ASC";
		break;
	}
	
	switch($_REQUEST['ascdesc']){
		case '':
		case 'desc':
		$ascdesc = " DESC";
		break;
		
		case 'asc':
		$ascdesc = " ASC";
		break;
	}
	
	$limit = ($_REQUEST['pp']!='') ? (int)$_REQUEST['pp'] : 15 ;
	$sql_first = new dbc("SELECT id FROM " . PREFIX . "_links WHERE catid = '".escs($_REQUEST['categ'])."' AND active='1'");
	$num = $sql_first->numrows();
	
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
		
	$sql = new dbc("SELECT * FROM " . PREFIX . "_links WHERE catid = '".escs($_REQUEST['categ'])."' AND active='1' $sort limit $a,$limit");
	
	$files = array();
	
	// wenn es mehr Datensaetze gibt, als Limit erlaubt, Navi erzeugen...
	if($num > $limit){
		$tmpl->assign('nav', pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?sort=$_REQUEST[sort]&amp;pp=$_REQUEST[pp]&amp;categ=$_REQUEST[categ]&amp;parent=$_REQUEST[parent]&amp;p=links&amp;area=$_REQUEST[area]&amp;page={s}\">{t}</a> "));
		}
	
	while($row = $sql->fetchrow()){
		$row->ctime = mygmt($row->ctime);
		array_push($files, $row);
	}
}
$tmpl->assign('dlcat', $dlcat);
$tmpl->assign('catid', $_REQUEST['categ']);
$tmpl->assign('files',$files);
$tmpl->assign('lang',$lang);
$tmpl->assign('area',$area);
$tmpl->assign('ugroup',UGROUP);
$tmpl->register_function('in_array', 'in_array');
$tmpl->register_function('getlinks', 'getlinks');

//=====================================================================
// <<-- DATEI- DETAILS -->>
//=====================================================================
if($_REQUEST['showlink']==1) { 
	// ====================================================================================
	// <<-- KANN LINK KOMMENTIERT WERDEN? -->>
	// ====================================================================================
	$sql = new dbc("SELECT cancomment FROM " . PREFIX . "_links WHERE id = '".escs($_REQUEST['fid'])."'");
	$row = $sql->fetchrow();
	if($row->cancomment == 1){
		// ====================================================================================
		// <<-- KOMMENTAR EINTRAGEN -->>
		// ====================================================================================
		$sql = new dbc("SELECT spamprotect,spamprotect_time,mailbycomment FROM " . PREFIX . "_settings");
		$row_spam = $sql->fetchrow();
		
		if($_REQUEST['post']==1){
			// ====================================================================================
			// <<-- WURDE DER SPAMSCHUTZ AKTIVIERT? -->>
			// ====================================================================================
			if($row_spam->spamprotect==1){
				$sql = new dbc("SELECT ip,ctime FROM ".PREFIX."_linkcomments WHERE ip='".$_SERVER['REMOTE_ADDR']."' AND dlid='".$_REQUEST['fid']."' ORDER BY id DESC limit 1");
				
				$row = $sql->fetchrow();
				if($row){
					if( ($row->ctime) + (60*$row_spam->spamprotect_time) > time()){
						
						$EOUT = msg("spamprotect","spamprotect_text", str_replace("__URL__", "index.php?showlink=1&fid=$_REQUEST[fid]&p=links&area=$_REQUEST[area]&categ=$_REQUEST[categ]", $lang['redirect']), "index.php?showlink=1&fid=$_REQUEST[fid]&p=links&area=$_REQUEST[area]&categ=$_REQUEST[categ]");
						$NOOUT = 1;
						$error = 1;
					}
				}
			}
			
			// ====================================================================================
			// <<-- WENN NICHT IN SPAMSCHUTZ- ZEIT, KOMMENTAR EINTRAGEN -->>
			// ====================================================================================
			if(!$error){
				// ====================================================================================
				// <<-- WENN MAIL GESENDET WERDEN SOLL -->>
				// ====================================================================================
				if($row_spam->mailbycomment == 1){
					$adminlink = "http://" . $_SERVER['HTTP_HOST'] . str_replace("/index.php","",$_SERVER['PHP_SELF']) . "/admin/links.php?area=$_REQUEST[area]&action=comments&id=$_REQUEST[id]";
					$path = dirname(__FILE__);
					include($path ."/lib/mailbycomment.php" );
				}
				
				$text = formtext($_POST['text'],$MAXCOMMLENGTH);
				$sql = new dbc("
				INSERT INTO ".PREFIX."_linkcomments(
				id,
				dlid,
				ctime,
				autor,
				title,
				comment,
				ip
				) VALUES (
				'',
				'".escs($_REQUEST['fid'])."',
				'".time()."', 
				'".$UID."',
				'".iform($_POST['title'],"255","")."',
				'".$text."',
				'".$_SERVER['REMOTE_ADDR']."'
				) 
				");
				$EOUT = msg("thankyou","pollcomments_thanks", str_replace("__URL__", "index.php?showlink=1&fid=$_REQUEST[fid]&p=links&area=$_REQUEST[area]&categ=$_REQUEST[categ]", $lang['redirect']), "index.php?showlink=1&fid=$_REQUEST[fid]&p=links&area=$_REQUEST[area]&categ=$_REQUEST[categ]");
				$NOOUT = 1;
			}
		}
		
		$limit = limit();
		if( !isset($_REQUEST['sort']) && $_REQUEST['sort']!="desc" && $_REQUEST['sort']!="asc" ){
			$sorts = "desc";
			$_REQUEST['sort'] = "desc";
		} else {
			$sorts = $_REQUEST['sort'];
			$_REQUEST['sort'] = $_REQUEST['sort'];
		}
		
		
		
		$sql =& new dbc("SELECT id FROM ".PREFIX."_linkcomments WHERE  dlid='".escs($_REQUEST['fid'])."'");
		$num = $sql->numrows();
		
		
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		$sql =& new dbc("SELECT * FROM ".PREFIX."_linkcomments WHERE  dlid='".escs($_REQUEST['fid'])."' ORDER BY id $sorts limit $a,$limit");
		$erg = $sql->numrows();
		
		
		$path = dirname(__FILE__);
		require_once($path ."/lib/pagesobjects.php" );
		require_once($path ."/lib/commentobjects.php" );
		
		// ====================================================================================
		// <<-- SEITEN- NAVIGATION KOMMENTARE -->>
		// ====================================================================================
		if($num>$limit){ $nav = pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?nav=1&amp;showlink=1&fid=$_REQUEST[fid]&p=links&area=$_REQUEST[area]&categ=$_REQUEST[categ]&amp;pp=$limit&amp;sort=$_REQUEST[sort]&amp;page={s}\">{t}</a> "); }
		
		$tmpl->assign('pages', $nav);
		$tmpl->assign('guest', $lang['guest']);
		$tmpl->assign('comments_array', $comments_array);
		$tmpl->assign('comments', parsetrue('container/'.container('comments'), $lang['newscomments'], $tmpl->fetch('comments/comments.tpl')));
		
		// ====================================================================================
		// <<-- DARF BENUTZER KOMMENTIEREN? -->>
		// ====================================================================================
		if(permission('download_comment')){
			$tmpl->assign('comment', $lang['commentonce']);
			$tmpl->assign('formlink', "index.php?docomment=1&amp;showlink=1&amp;fid=$_REQUEST[fid]&amp;p=links&amp;area=$_REQUEST[area]&amp;categ=$_REQUEST[categ]&amp;post=1");
			
			// ====================================================================================
			// <<-- HIER WIRD UEBERPRUEFT, OB SMILIES ODER BB- CODE VERWENDET WERDEN DARF -->>
			// ====================================================================================
			if(COMMENTSMILEYS == 1) {
				$tmpl->assign('smilie', 1);
				$tmpl->assign('listemos', $smilies);
			}
			
			if (COMMENTSBBCODE == 1) {
				$tmpl->assign('bbcodes', 1);
			}
			
			if($_REQUEST['print']!=1){
				$tmpl->assign('lang', $lang);
				$tmpl->assign('commentform', parsetrue('container/'.container('comments'), $lang['postcomment'], $tmpl->fetch('comments/commentform.tpl')));
			}
		}
		
		if(!permission('download_comment')){
			$tmpl->assign('commentform', $tmpl->fetch('comments/commentform_false.tpl'));
		}
	}
	$tmpl->assign('numcomm', $numcomm);
	
	// ====================================================================================
	// <<-- KANN LINK KOMMENTIERT WERDEN? ENDE -->>
	// ====================================================================================
	
	// ===============================================================
	// <<-- HIER WIRD VERSUCHT, DEN BESTMÖGLICHSTEN KOMPROMISS ZU FINDEN, 
	//      DER KEINE COOKIES BENÖTIGT. ES WIRD DIE IP, SOWOHL DIE USER-
	//      ID DES BESUCHERS GESPEICHERT.  -->>
	// ===============================================================
	if( ($_REQUEST['vote']==1) && (is_numeric($_REQUEST['voting'])) &&  ($_REQUEST['voting'] <= 8) ){
		$sql = new dbc("SELECT canvote,votes_ip,votes_uid FROM " . PREFIX . "_links WHERE id = '$_REQUEST[fid]'");
		$row = $sql->fetchrow();
		if($row->canvote==1){
			$votes_ip = explode(";", $row->votes_ip);
			$votes_uid = explode(";", $row->votes_uid);
			
			if ( (@in_array($_SERVER['REMOTE_ADDR'], $votes_ip )) || (@in_array(UID, $votes_uid)) ) {
				$canvote = -1;
			} else { $canvote = 1; } ;
			if($canvote == 1){
				$sql = new dbc("UPDATE " . PREFIX . "_links set  votes_data= votes_data+$_REQUEST[voting],  votes=votes+1  WHERE id = '$_REQUEST[fid]'");
				if(UGROUP != 2) { 
					$zusatz = " , votes_uid = CONCAT(votes_uid, ';', '" . UID . "')";
				}
				$sql = new dbc("UPDATE " . PREFIX . "_links SET
				votes_ip = CONCAT(votes_ip, ';', '" . $_SERVER['REMOTE_ADDR'] . "') 
				$zusatz 
				WHERE id = $_REQUEST[fid]");
				header("Location:index.php?showlink=1&fid=$_REQUEST[fid]&p=links&area=$_REQUEST[area]&categ=$_REQUEST[categ]");
				
			} else {
				header("Location:index.php?showlink=1&fid=$_REQUEST[fid]&p=links&area=$_REQUEST[area]&categ=$_REQUEST[categ]");
			}
		}
	}
	
	//=====================================================================
	// <<-- DETAIL AUSGEBEN -->>
	//=====================================================================
	$today = date("dmY", time());
	$sql = new dbc("SELECT today FROM " . PREFIX . "_links WHERE id = '$_REQUEST[fid]'");
	$row_to = $sql->fetchrow();
	if($row_to->today != $today) {
		$sql = new dbc("UPDATE " . PREFIX . "_links set today='$today', hits_today='0' WHERE id = '$_REQUEST[fid]'");
	}
	
	$sql = new dbc("SELECT * FROM " . PREFIX . "_links WHERE catid='".$_REQUEST['categ']."' and id='".$_REQUEST['fid']."' "); 
	$row_f = $sql->fetchrow();
	$row_f->text = autowords($row_f->text);
	// HIGHLIGHT 4.2.0
	$row_f->text = high($row_f->text);
	$tmpl->assign('row_f',$row_f);
	$thenav_extra = SEPARATOR . $row_f->title;
	
	//=====================================================================
	// <<-- BEWERTUNGS- DROPDOWN -->>
	//=====================================================================
	$lang['pvoting'] = ",".$lang['pvoting'];
	$langpvote = explode(",",$lang['pvoting']);
	while (list($key, $value) = each ($langpvote)) {
		if($value){
			$pvote[$value]['val'] = $value;
			$pvote[$value]['key'] = $key;
		}
	}
	$tmpl->assign('pvote',$pvote);
	//=====================================================================
	// <<-- BEWERTUNGS- GRAFIK -->>
	//=====================================================================
	if($row_f->votes_data>0) { 
		$data = round($row_f->votes_data/$row_f->votes);
		$tmpl->assign('votedata', $data);
		$visitor_vote = explode(",", $lang['pvoting']);
		$visitor_vote = $visitor_vote[($data)];
		$tmpl->assign('vvote', $visitor_vote);
	}
	
	
}
$tmpl->register_function('permission', 'permission');
//=====================================================================
// <<-- SEITENTITEL UND KOPF- NAVIGATION -->>
//=====================================================================
if($extra) $thenav = getNavigationPath($_REQUEST['categ'], 'linkcat', 'links', 'categ','catid','catname', '','1');
$dlid = ($_REQUEST['categ']!="") ? $_REQUEST['categ'] : 0;
$sname = ($_REQUEST['categ']!="") ? simpletitle($lang['title_links'] . SEPARATOR . getNavigationptitle($dlid, 'linkcat', 'links', '','catid','catname')) . $thenav_extra : simpletitle($lang['title_links'],$title);
$headtitle = (!$_REQUEST['categ']) ? $lang['title_links_overview'] : "<a class=\"boxnavi\" href=\"index.php?p=links&amp;area=".$area."\">" . $lang['title_links_overview'] . "</a>" . SEPARATOR;

$tmpl->assign('headernav', $headtitle . $thenav . $thenav_extra );

//=====================================================================
// <<-- AUSGABE -->>
//=====================================================================
$tmpl->assign('content', parsetrue('container/'.container('links_in'), $lang['title_links'], $tmpl->fetch('links/main.tpl')));

?>