<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
class Links
{
	var $_allowed_html = '<br /><br>';
	
	function Settings()
	{
		$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_links_einstellungen");
		$GLOBALS['Linksettings'] = $res;
		$GLOBALS['tmpl']->assign('Linksettings', $GLOBALS['Linksettings']);
	}
	
	function SearchLinks()
	{
		$LC = $_SESSION['Langcode'];
		$Search = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9_)( ]*)/', '', @$_REQUEST['ql']);
		$Search2 = koobispecialchars($Search);
		$_REQUEST['ql'] = $Search;
		
		if(strlen($Search) < 3) 
		{
			$Search = '';
			$Search2 = '';
		}
		
		$entries = array();
		
		$check = $GLOBALS['db']->RowQuery("SELECT 
			COUNT(Id) AS ResCount 
		FROM 
			" . PREFIX . "_links 
		WHERE 
			Aktiv='1' 
		AND
			((Name_{$LC} LIKE '%{$Search}%' OR Beschreibung_{$LC} LIKE '%{$Search}%') OR 
			(Name_{$LC} LIKE '%{$Search2}%' OR Beschreibung_{$LC} LIKE '%{$Search2}%'))
		AND 
			Sektion = '" . AREA . "' 
		ORDER BY Name_{$LC} ASC");
		
		$num = $check->ResCount;
		$limit = GetLim(15);
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
			
		// Such-Abfrage
		$query_items = $GLOBALS['db']->Query("SELECT 
			*,
			Kategorie, 
			Name_{$LC} AS Name,
			Beschreibung_{$LC} AS Beschreibung 
		FROM 
			" . PREFIX . "_links 
		WHERE 
			Aktiv='1' 
		AND
			((Name_{$LC} LIKE '%{$Search}%' OR Beschreibung_{$LC} LIKE '%{$Search}%') OR 
			(Name_{$LC} LIKE '%{$Search2}%' OR Beschreibung_{$LC} LIKE '%{$Search2}%'))
		AND 
			Sektion = '" . AREA . "'  
		ORDER BY Name_{$LC} ASC LIMIT $a,$limit");
		
		
		
		while($row_items = $query_items->fetchrow())
		{
			// Wertung
			if($GLOBALS['Linksettings']->Wertung==1)
			{
				$row_items->Wertung = getRating($row_items->Id,'links');
			}
			
			$res_name = $GLOBALS['db']->RowQuery("SELECT  Name_{$LC} AS Name FROM " . PREFIX . "_links_kategorie WHERE Id='$row_items->Kategorie'");
			$row_items->KategName = $res_name->Name;
			$row_items->Link_Categ   = "index.php?p=links&amp;area=" . AREA . "&amp;categ=" . $row_items->Kategorie . "&amp;name=" . seotitle($row_items->KategName);
			$row_items->CCount = getPageCommentCount($row_items->Id,'links');
			$row_items->Beschreibung = (strlen($Search) >= 3) ? high(strip_tags($row_items->Beschreibung,'<span>'),$_REQUEST['ql']) : strip_tags($row_items->Beschreibung,$this->_allowed_html);
			$row_items->Beschreibung = (strlen($Search) >= 3) ? high(strip_tags($row_items->Beschreibung,'<span>'),$Search2) : strip_tags($row_items->Beschreibung,$this->_allowed_html);
			$row_items->Link_Details = "index.php?p=links&amp;action=showdetails&amp;area=" . AREA . "&amp;categ=" . $row_items->Kategorie . "&amp;id=" . $row_items->Id . "&amp;name=" . seotitle($row_items->Name);
			array_push($entries, $row_items);
		}
		
		// Seiten-Navigation
		$headernav = '<a href="index.php?p=links&amp;area=' . AREA . '">' . $GLOBALS['config_vars']['Links'] . '</a> ';
		
		// Navi
		if($num>$limit)
		{
			$Search = ($Search=='') ? '-' : $Search;
			$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, "", "<a class=\"page_navigation\" href=\"index.php?ql=" . urlencode($Search) . "&amp;action=search&amp;p=links&amp;area=".AREA."&amp;page={s}\">{t}</a> "));
		}
			
		$GLOBALS['tmpl']->assign('Entries', $entries);
		$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($headernav));
		$GLOBALS['tmpl']->assign('headernav', $headernav);
		$GLOBALS['tmpl']->assign('Results', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/entries.tpl')); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/search_result.tpl')); 
	}
	
	function showDetails($id)
	{

		$LC = $_SESSION['Langcode'];
		$res = $GLOBALS['db']->RowQuery("SELECT *,Kategorie, Name_{$LC} AS Name,Beschreibung_{$LC} AS Beschreibung FROM " . PREFIX . "_links WHERE Id='$id' AND Aktiv='1'");
		if(!is_object($res))
		{
			refer('index.php?p=links&area=1');
		}
		
		$headernav = '<a href="index.php?p=links&amp;area=' . AREA . '">' . $GLOBALS['config_vars']['Links'] . '</a>' . getNavigationPath($res->Kategorie, 'links_kategorie', 'links&amp;area=' . AREA, 'categ', 'Id', 'Name_' . $_SESSION['Langcode'], '', '', '', '', 'Parent_Id')  ;
		
		$res->UserName = fetchusername($res->Autor);
			
		//=================================================================
		// Kommentare
		//=================================================================
		if($GLOBALS['Linksettings']->Kommentare == 1)
		{
			$comment = new Comments;
			// Kommentar bearbeiten
			if(isset($_POST['comment_action']) && $_POST['comment_action'] == 'edit' && permission('edit_comments')) $comment->EditComment('links',$_POST['comment_id']);
			
			// Neuer Kommentar
			if(permission('comments'))
			{
				$comment->NewComment('links', @$_POST['Redir'],$id);
			}
			
			// Kommentare holen
			$Comments = $comment->GetComments('links',$id,'index.php?p=links&amp;action=showdetails&amp;categ=' . $res->Kategorie . '&amp;id=' . $id . '&amp;name=' . seotitle($res->Name));
			$GLOBALS['tmpl']->assign('GetComments',$GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/comments/comments.tpl'));
		}
		
		// Wertung
		if($GLOBALS['Linksettings']->Wertung==1)
		{
			$GLOBALS['tmpl']->assign('RatingUrl', 'index.php?p=rating&action=rate&id=' . $id. '&where=links');
			$GLOBALS['tmpl']->assign('RatingForm', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/other/rating.tpl')); 
		}
		
		// Seiten-Info
		setMeta($GLOBALS['config_vars']['Links'],$res->Beschreibung);
		
		$GLOBALS['tmpl']->assign('BrokenLinkSubmit', 'index.php?action=brokenlink&p=links&id=' . $id);
		$GLOBALS['tmpl']->assign('link_res', $res);
		$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($headernav) . $GLOBALS['config_vars']['PageSep'] . koobispecialchars($res->Name));
		$GLOBALS['tmpl']->assign('headernav', $headernav);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/showlink.tpl')); 
	}
	
	//=================================================================
	// Link-Kategorien und Links anzeigen
	//=================================================================
	function displayCategs()
	{
		$LC = $_SESSION['Langcode'];
		$id = 0;
		$getid = (isset($_REQUEST['categ']) && !empty($_REQUEST['categ'])) ? abs((int)$_REQUEST['categ']) : 0 ;
		
		
		switch(@$_REQUEST['sort'])
		{
			case '':
			default:
			case 'datedesc':
				$req = 'datedesc';
				$defsort = "ORDER BY Sponsor DESC, Datum DESC";
				$defsort_n = "&amp;sort=datedesc";
				$GLOBALS['tmpl']->assign('img_date', 'sorter_down');
				$GLOBALS['tmpl']->assign('datesort', 'dateasc');
			break;
			
			case 'dateasc':
				$req = 'dateasc';
				$defsort = "ORDER BY Sponsor DESC, Datum ASC";
				$defsort_n = "&amp;sort=dateasc";
				$GLOBALS['tmpl']->assign('img_date', 'sorter_up');
				$GLOBALS['tmpl']->assign('datesort', 'datedesc');
			break;
			
			case 'namedesc':
				$req = 'namedesc';
				$defsort = "ORDER BY Sponsor DESC, Name_{$LC} DESC";
				$defsort_n = "&amp;sort=namedesc";
				$GLOBALS['tmpl']->assign('img_name', 'sorter_down');
				$GLOBALS['tmpl']->assign('namesort', 'nameasc');
			break;
			
			case 'nameasc':
				$req = 'nameasc';
				$defsort = "ORDER BY Sponsor DESC, Name_{$LC} ASC";
				$defsort_n = "&amp;sort=nameasc";
				$GLOBALS['tmpl']->assign('img_name', 'sorter_up');
				$GLOBALS['tmpl']->assign('namesort', 'namedesc');
			break;
			
			
			case 'hitsdesc':
				$req = 'hitsdesc';
				$defsort = "ORDER BY Sponsor DESC, Hits DESC";
				$defsort_n = "&amp;sort=hitsdesc";
				$GLOBALS['tmpl']->assign('img_hits', 'sorter_down');
				$GLOBALS['tmpl']->assign('hitssort', 'hitsasc');
			break;
			
			case 'hitsasc':
				$req = 'hitsasc';
				$defsort = "ORDER BY Sponsor DESC, Hits ASC";
				$defsort_n = "&amp;sort=hitsasc";
				$GLOBALS['tmpl']->assign('img_hits', 'sorter_up');
				$GLOBALS['tmpl']->assign('hitssort', 'hitsdesc');
			break;
		}
		
		$_REQUEST['sort'] = $req;
		
		$prefix = '';
		$news_categ = '';
		$area = AREA;
		$news_categ = array();
		
		$Categs = $this->ListCategs($getid, $prefix, $news_categ, $area);
		
		if(isset($_REQUEST['categ']) && !empty($_REQUEST['categ']))
		{
			// Eintraege
			$entries = array();
			$cid = abs((int)$_REQUEST['categ']);
			$items = array();
			
			$res_name = $GLOBALS['db']->RowQuery("SELECT  Name_{$LC} AS Name FROM " . PREFIX . "_links_kategorie WHERE Id='$getid'");
			
			$res = $GLOBALS['db']->RowQuery("SELECT COUNT(Id) AS Gesamt FROM " . PREFIX . "_links WHERE Kategorie='$cid' AND Aktiv='1' {$defsort}");
			$num = $res->Gesamt;
			$limit = GetLim(15);
			$seiten = ceil($num / $limit);
			$a = prepage() * $limit - $limit;
			
			$query_items = $GLOBALS['db']->Query("SELECT *,Kategorie, Name_{$LC} AS Name,Beschreibung_{$LC} AS Beschreibung FROM " . PREFIX . "_links WHERE Kategorie='$cid' AND Aktiv='1' {$defsort} LIMIT $a,$limit");
			
			while($row_items = $query_items->fetchrow())
			{
				// Wertung
				if($GLOBALS['Linksettings']->Wertung==1)
				{
					$row_items->Wertung = getRating($row_items->Id,'links');
				}
				
				$row_items->CCount = getPageCommentCount($row_items->Id,'links');
				$row_items->KategName = $res_name->Name;
				$row_items->Beschreibung = strip_tags($row_items->Beschreibung,$this->_allowed_html);
				$row_items->Link_Details = "index.php?p=links&amp;action=showdetails&amp;area=" . AREA . "&amp;categ=" . $getid . "&amp;id=" . $row_items->Id . "&amp;name=" . seotitle($row_items->Name);
				array_push($entries, $row_items);
			}
			
			// Navi
			if($num>$limit)
			{
				$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, "", "<a class=\"page_navigation\" href=\"index.php?p=links&amp;area=".AREA."&amp;categ=".$getid."&amp;name=".seotitle($res_name->Name)."&amp;page={s}{$defsort_n}\">{t}</a> "));
			}
			
			// Uebergeben
			$GLOBALS['tmpl']->assign('Entries',$entries);
		}
		
		// Seiten-Navigation
		$headernav = '<a href="index.php?p=links&amp;area=' . AREA . '">' . $GLOBALS['config_vars']['Links'] . '</a>' . getNavigationPath($getid, 'links_kategorie', 'links&amp;area=' . AREA, 'categ', 'Id', 'Name_' . $_SESSION['Langcode'], '', '', '', '', 'Parent_Id');
		
		// Seiten-Info
		$hinf = clean_page_title($headernav);
		setMeta($GLOBALS['config_vars']['Links'],$hinf);
		
		if(!isset($_REQUEST['categ'])) $GLOBALS['tmpl']->assign('Entries', $this->newEntries());
		$GLOBALS['tmpl']->assign('pagetitle_html', $hinf);
		$GLOBALS['tmpl']->assign('headernav', $headernav);
		$GLOBALS['tmpl']->assign('Categs',$Categs);
		$GLOBALS['tmpl']->assign('CategName',@seotitle($res_name->Name));
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/showcategs.tpl')); 
	}
	
	//=================================================================
	// Neueste
	//=================================================================
	function newEntries($search='',$limit=10,$ext=0)
	{
		$LC = $_SESSION['Langcode'];
		$entries = array();
		$query_items = $GLOBALS['db']->Query("SELECT *, Kategorie, Name_{$LC} AS Name,Beschreibung_{$LC} AS Beschreibung FROM " . PREFIX . "_links WHERE Sektion='" . AREA . "' AND Aktiv='1' ORDER BY Id DESC LIMIT {$limit}");
		while($row_items = $query_items->fetchrow())
		{
			$res_name = $GLOBALS['db']->RowQuery("SELECT  Name_{$LC} AS Name FROM " . PREFIX . "_links_kategorie WHERE Id='$row_items->Kategorie'");
			
			// Wertung
			if($GLOBALS['Linksettings']->Wertung==1)
			{
				$row_items->Wertung = getRating($row_items->Id,'links');
			}
			if($ext==1) $this->_allowed_html = '';
			$row_items->CCount = getPageCommentCount($row_items->Id,'links');
			$row_items->KategName = $res_name->Name;
			$row_items->Beschreibung = strip_tags($row_items->Beschreibung,$this->_allowed_html);
			$row_items->Link_Categ   = "index.php?p=links&amp;area=" . AREA . "&amp;categ=" . $row_items->Kategorie . "&amp;name=" . seotitle($row_items->KategName);
			$row_items->Link_Details = "index.php?p=links&amp;action=showdetails&amp;area=" . AREA . "&amp;categ=" . $row_items->Kategorie . "&amp;id=" . $row_items->Id . "&amp;name=" . seotitle($row_items->Name);
			array_push($entries, $row_items);
		}
		return $entries;
	}
	
	//=================================================================
	// Kategorien fuer Seite holen
	//=================================================================
	function ListCategs($id, $prefix, &$news_categ, &$area)
	{
		$LC = $_SESSION['Langcode'];
		$query = $GLOBALS['db']->Query("SELECT Id, Parent_Id, Beschreibung_{$LC} AS Beschreibung, Name_{$LC} AS Name FROM " . PREFIX . "_links_kategorie WHERE Parent_Id = '$id' AND Sektion = '$area' ORDER BY Name ASC");
		if (!$query->numrows()) 
		{
			return;
		} 
		
		while ($item = $query->fetchrow())
		{
			$res = $GLOBALS['db']->RowQuery("SELECT COUNT(Id) AS LinkCount FROM " . PREFIX . "_links WHERE Kategorie='$item->Id' AND Aktiv='1'");
			$item->visible_title = $prefix . " " . $item->Name;
			$item->HLink = "index.php?p=links&amp;area=" . AREA . "&amp;categ=$item->Id&amp;name=" . seotitle($item->Name);
			$item->LinkCount = ($res->LinkCount + @$this->ReadLinkCount($item->Id));
			
			array_push($news_categ, $item);
		} 
		return $news_categ;
	}
	
	
	//=================================================================
	// Kategorien fuer Dropdown holen
	//=================================================================
	function ListCategsSimple($id, $prefix, &$news_categ, &$area)
	{
		$LC = $_SESSION['Langcode'];
		$query = $GLOBALS['db']->Query("SELECT Id, Parent_Id, Beschreibung_{$LC} AS Beschreibung, Name_{$LC} AS Name FROM " . PREFIX . "_links_kategorie WHERE Parent_Id = '$id' AND Sektion = '$area' ORDER BY Name ASC");
		if (!$query->numrows()) 
		{
			return;
		} 
		
		while ($item = $query->fetchrow())
		{
			$res = $GLOBALS['db']->RowQuery("SELECT COUNT(Id) AS LinkCount FROM " . PREFIX . "_links WHERE Kategorie='$item->Id' AND Aktiv='1'");
			$item->visible_title = $prefix . " " . $item->Name;
			$item->HLink = "index.php?p=links&amp;area=" . AREA . "&amp;categ=$item->Id&amp;name=" . seotitle($item->Name);
			$item->LinkCount = ($res->LinkCount + @$this->ReadLinkCount($item->Id));
			array_push($news_categ, $item);
		} 
		return $news_categ;
	}
	
	//=================================================================
	// Zaehlfunktion
	// Zaehlt, wie viele Links sich in einer Kategorie befinden (rekursiv)
	//=================================================================
	function ReadLinkCount($categ, &$count)
	{
		$query = $GLOBALS['db']->Query("SELECT Id,Parent_Id FROM " . PREFIX . "_links_kategorie WHERE Parent_Id='$categ'");
		if (!$query->numrows()) 
		{
			return 0;
		}
	
		while ($item = $query->fetchrow())
		{
			$res = $GLOBALS['db']->RowQuery("SELECT COUNT(Id) AS LinkCount FROM " . PREFIX . "_links WHERE Kategorie='$item->Id' AND Aktiv='1'");
			$count += ($res->LinkCount);
			$this->ReadLinkCount($item->Id,$count);
			
		}
		return $count;
	}
	
	
	//=================================================================
	// Hitcount erhoehen
	//=================================================================
	function Updatehitcount($id)
	{
		// Nur erhoehen, wenn Click von gleicher Seite
		if(strpos(@$_SERVER['HTTP_REFERER'], HOME_PATH)!==false)
		{
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_links Set Hits=Hits+1 WHERE Id='" . abs((int)$id) . "'");
		}
	}
	
	function BrokenLink($id)
	{
		
		$allowed = array('Links_Broken_dnserror',
			'Links_Broken_noconnection',
			'Links_Broken_auth',
			'Links_Broken_notfound',
			'Links_Broken_servererror',
			'Links_Broken_other');
		
		$res = $GLOBALS['db']->RowQuery("SELECT Id,DefektGemeldet FROM " . PREFIX . "_links WHERE Id='$id'");
		//
		if(is_object($res) && $res->DefektGemeldet=='' && strpos(@$_SERVER['HTTP_REFERER'], HOME_PATH)!==false && (in_array(@$_REQUEST['BrokenReason'],$allowed)))
		{
			$Reason = $GLOBALS['config_vars'][$_REQUEST['BrokenReason']];
			$Email  = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9.@]*)/', '', @$_REQUEST['email']);
			$Name = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9. ]*)/', '', @$_REQUEST['name']);
			$Page = str_replace('&amp;','&',base64_decode(preg_replace('/([^A-Za-zÀ-ÖØ-öø-ÿ0-9]*)/', '', $_REQUEST['dpage'])));
			
			
			$GLOBALS['db']->RowQuery("UPDATE " . PREFIX . "_links 
			SET
				DefektGemeldet='$Reason',
				DEmail = '$Email',
				DName = '" . $Name . "',
				DDatum = '" . stime() . "' 
			WHERE Id='$id'");
			
			$Text = str_replace(array('__BENUTZER__','__EMAIL__','__LINK__','__GRUND__'), array($Name,$Email,$Page,$Reason), $GLOBALS['config_vars']['Links_E_BrokenText']);
			$Koobi = new Koobi;
			$Koobi->koobi_mail($GLOBALS['settings']['Mail_Absender'], $Text, $GLOBALS['config_vars']['Links_E_BrokenSubject'], $GLOBALS['settings']['Mail_Absender'], stripslashes($GLOBALS['settings']['Mail_Name']), 'text', '');	
			
			echo $GLOBALS['config_vars']['Links_ErrorSendBrokenOk'];
			exit;
		} else {
			echo $GLOBALS['config_vars']['Links_ErrorSendBrokenAllready'];
			exit;
		}
	}
	
}
?>