<?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 News
{
	var $Limit;
	var $_allowed_html = '<br /><br>';
	
	function displayAllNews($archive=0,$rss=0,$topnews=0)
	{
		if(isset($_REQUEST['mode']) && $_REQUEST['mode'] == 'rss')
		{
			$rss=1;
		}
		
		if($archive == 1)
		{
			$this->Limit = (!empty($_REQUEST['limit'])) ? abs((int)$_REQUEST['limit']) :  $GLOBALS['section']['LimitNewsArchive'];
			$this->Cont  = '/news/container_archive.tpl';
			$this->Tmpl  = '/news/newsarchive.tpl';
			$this->Page  = 'newsarchive';
		} else {
			$this->Limit = $GLOBALS['section']['LimitNews'];
			$this->Cont  = '/start/start.tpl';
			$this->Tmpl  = '/news/newsticker.tpl';
			$this->Page  = 'index';
		}
		
		$db_categ = (isset($_REQUEST['catid']) && !empty($_REQUEST['catid']) && $_REQUEST['catid'] != '0') ? "AND Kategorie = '" . abs((int)$_REQUEST['catid'])  . "'" : "";
		
		//=================================================================
		// HIER WIRD UEBERPRUEFT, OB EIN START UND END-DATUM ANGEGEBEN 
		// WURDE. WURDE KEIN DATUM ANGEGEBEN, WIRD DER STARTPUNKT AUF DEN 
		// 1.1.2000 UND DER ENDPUNKT AUF DAS AKTUELLE DATUM GELEGT
		//=================================================================
		$show_start = (isset($_REQUEST['s_year']) && !empty($_REQUEST['s_year']) && isset($_REQUEST['s_month']) && !empty($_REQUEST['s_month']) && isset($_REQUEST['s_day']) && !empty($_REQUEST['s_day'])) ? mktime(0, 0, 1, $_REQUEST['s_month'], $_REQUEST['s_day'], $_REQUEST['s_year']) : mktime(0,0,0,1,1,2000);
		$show_end   = (isset($_REQUEST['e_year']) && !empty($_REQUEST['e_year']) && isset($_REQUEST['e_month']) && !empty($_REQUEST['e_month']) && isset($_REQUEST['e_day']) && !empty($_REQUEST['e_day'])) ? mktime(23, 59, 59, $_REQUEST['e_month'], $_REQUEST['e_day'], $_REQUEST['e_year']) : stime();
		$tbetween   = ((isset($_REQUEST['s_year']) && $_REQUEST['s_year']>1) && ($_REQUEST['e_year'] && $_REQUEST['e_year']>1) || ( ($show_start>1) && ($show_end>1))) ? "(ZeitStart BETWEEN " . mysql_escape_string($show_start) . " AND  " . mysql_escape_string($show_end) . ") AND " : '';
		
		//=================================================================
		// Wir muessen unterscheiden, welche Sprache gesucht werden soll
		// Also nur News anzeigen, die auch in der angeforderten Sprache
		// vorhanden sind.
		//=================================================================
		$Lc = $_SESSION['Langcode'];
		$news = array();
		
		if(isset($_REQUEST['q_news']) && !empty($_REQUEST['q_news']))
		{
			$search_and = "";
			$search_or  = "";
			
			$search_request   =  substr(eregi_replace('[^ %+A-Za-zÀ-ÖØ-öø-ÿ0-9]', '', $_REQUEST['q_news']),0,25);
			
			if(isset($_REQUEST['st']) && !empty($_REQUEST['st']) && $_REQUEST['st'] == 'and')
			{
				$_REQUEST['st'] = "and";
				$and = explode(' ',  $search_request);
				foreach($and as $a) $search_and .= " AND (Titel{$Lc} LIKE '%" . $a . "%' OR News{$Lc} LIKE '%" . $a . "%') \n";
			} else {
				$_REQUEST['st'] = "or";
				$or  = explode(' ',  $search_request);
				$search_or = "AND (Titel{$Lc} LIKE '%" . $search_request . "%' OR News{$Lc} LIKE '%" . $search_request . "%')";
				foreach($or as $o) $search_or .= " OR (Titel{$Lc} LIKE '%" . $o . "%' OR News{$Lc} LIKE '%" . $o . "%') \n";
			}
			
			
			$db_title_search  = " AND ((Suche = 1) $search_and $search_or)";
			$nav_search_title = "&amp;q_news=" . urlencode($search_request);
		} else { 
			$db_title_search = '';
			$nav_search_title = '';
		}
		
		if($topnews==1)
		{
			$stn = " AND Topnews='1' ";
			$this->Limit = 25;
		} else {
			$stn = "";
		}
	
		
		$q = "SELECT Id FROM " . PREFIX . "_news
		WHERE 
			$tbetween 
			(ZeitEnde >= " . stime() . " OR ZeitEnde = 0) AND 
			
			(Sektion = '". AREA . "' OR AlleSektionen = 1) $stn AND 
			Aktiv = 1 
			$db_categ 
			$db_title_search 
		ORDER 
			BY Id DESC ";
		
		$sql = $GLOBALS['db']->Query($q);
		$num = $sql->numrows();
		
		//=================================================================
		// Wie viele Seiten gibt das System zurueck?
		//=================================================================
		$seiten = ceil($num / $this->Limit);
		$a = prepage() * $this->Limit - $this->Limit;
		$GLOBALS['tmpl']->assign("news_limit", $this->Limit);
					
		//=================================================================
		// Seiten-Navigation
		//=================================================================
		if($num > $this->Limit)
		{
			if($archive == 1)
			{
				$nav_categ = (isset($_REQUEST['catid'] ) && !empty($_REQUEST['catid'])) ? "&amp;categ=" . @$_REQUEST['catid'] : '&amp;catid=0';
				$nav_t     = '';//(isset($_REQUEST['t'] ) && !empty($_REQUEST['t'])) ? "&amp;t=" . @$_REQUEST['t'] : '&amp;t=0';
				$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', 
				"<a class=\"page_navigation\" href=\"index.php?arc=1&amp;area=" . AREA . "&amp;p=" . $this->Page . "{$nav_categ}{$nav_t}&amp;page={s}{$nav_search_title}&amp;limit=" . $this->Limit . "\">{t}</a> "));
			} else {
				$nav_categ = '0';
				$GLOBALS['tmpl']->assign('pages', pagenav($seiten, '', 
				"<a class=\"page_navigation\" href=\"index.php?area=" . AREA . "&amp;p=" . $this->Page . "&amp;page={s}\">{t}</a> "));
			}
		}
		
		//=================================================================
		// Die eigentliche Abfrage der News
		//=================================================================
		$q = "SELECT 
			Id,
			Kategorie,
			Bewertung,
			Zeit,
			ZeitStart,
			Autor,
			Hits,
			Sektion,
			Topnews,
			Topnews_Bild_{$Lc} AS TopnewsBild,
			Topnews_Bild_1 AS DefTopnewsBild,
			BildAusrichtung,
			Bild{$Lc} AS Bild,
			Titel{$Lc} AS Titel,
			Intro{$Lc} AS Intro,
			News{$Lc} AS News,
			Titel1 AS DefTitel,
			News1 AS DefNews,
			Intro1 AS DefIntro 
		FROM " . PREFIX . "_news
		WHERE 
			$tbetween 
			(ZeitEnde >= " . stime() . " OR ZeitEnde = 0) AND 
			
			(Sektion = '". AREA . "' OR AlleSektionen = 1) $stn AND 
			Aktiv = 1 
			$db_categ 
			$db_title_search 
		ORDER 
			BY Id DESC 
		LIMIT 
			$a, " . $this->Limit;
		$sql = $GLOBALS['db']->Query($q);
		
		$User = new User;
		
		while($row = $sql->fetcharray())
		{
			$row['Thumb'] = '';
			if($row['Bild']!='')
			{
				$row['Thumb'] = str_replace('%%ECRUOS_GMI_IBOOK%%','',$row['Bild']);
				
				if(file_exists(BASE_DIR . '/' . $row['Thumb'])) 
				{
			
					$endg = substr($row['Thumb'],-4);
					$name = substr($row['Thumb'],0,-4);
					$row['Thumb'] = (file_exists(BASE_DIR . '/cache/' . md5('content_' . $row['Thumb']) . $endg)) ? "cache/" . md5('content_' . $row['Thumb']) . $endg :  "system/content_thumb.php?img=" . $row['Thumb'];
				}
			}
			
			
			
			$row['TopnewsBild'] = ($row['TopnewsBild']=='') ? $row['DefTopnewsBild'] : $row['TopnewsBild'];
			$row['Titel'] = ($row['Titel']=='') ? $row['DefTitel'] : $row['Titel'];
			$row['Intro'] = ($row['Intro']=='') ? $row['DefIntro'] : $row['Intro'];
			$row['News']  = ($row['News']=='') ? $row['DefNews'] : $row['News'];
			$row['News'] = eregi_replace("\[SCREEN:([a-zA-Z0-9\\\/\_\.\]*)\]", "", $row['News']);
			$row['News'] = eregi_replace("\[CONTACT:([a-zA-Z0-9\\\/\_\.\]*)\]", "", $row['News']);
			$row['News'] = eregi_replace("\[--NEU--\]", "", $row['News']);
			$row['News'] = strip_tags($row['News'],$this->_allowed_html);
			$row['Intro'] = strip_tags($row['Intro'],$this->_allowed_html);
			$row['LinkTitle'] = seotitle($row['Titel']);
			$row['User'] = $User->getNameById($row['Autor']);
			
			array_push($news, $row);
		}
		
		$news_categs = array();
		$dropdown = $this->getNewsCategs(0, "", $news_categs, $_SESSION['Area']);
		$GLOBALS['tmpl']->assign("dropdown", $dropdown);
		
		
		//=================================================================
		// Seitenkopf-Navigationsbaum
		//=================================================================
		//                              NEWS-KATEGORIE    TABELLE           SEITE (p=?)    KATEG    
		
		if(isset($_REQUEST['categ']) && !empty($_REQUEST['categ']))
		{
			$headernav = getNavigationPath($_REQUEST['categ'], 'news_kategorie', 'newsarchive', 'categ', 'Id', 'Name_1', '', '', '', '', 'Parent_Id',$GLOBALS['config_vars']['Newsarchive']);
			$pagetitle = clean_page_title($headernav, $row['Titel']);
			
		} else {
			$headernav = ($archive==1) ? $GLOBALS['config_vars']['Newsarchive'] : '';
			$pagetitle = ($archive==1) ? $GLOBALS['config_vars']['Newsarchive'] : '';
		}
		
		if($rss==1)
		{
			if(permission('news_rss'))
			{
				header("Content-Type: text/xml");
				header("Cache-Control: no-cache");
				header("Pragma: no-cache");
				
				$rss = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
				$rss .= "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">";
				$rss .= "<channel>";
				$rss .= "<title>" . $pagetitle . "</title>\n";
				$rss .= "<link>" . redir() . "</link>\n";
				$rss .= "<description>powered by koobi-rss / http://".$_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "</description>\n";
				$rss .= "<generator>http://www.koobi.de/</generator>\n";
				$rss .= "<language></language>\n";
				
				foreach($news as $n)
				{
					$rss .= "<item>\n";
					$rss .= "<title>" . specialchars_for_rss(stripslashes($n['Titel'])) . "</title>\n";
					$rss .= "<link>http://".$_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?p=news&amp;area=$n[Sektion]&amp;newsid=$n[Id]&amp;name=" . seotitle($n['Titel']) . "</link>\n";
					$rss .= "<description><![CDATA[" . substr(specialchars_for_rss(stripslashes($n['News'])),0,400) . "...]]></description>\n";
					$rss .= "<content:encoded><![CDATA[" . specialchars_for_rss(stripslashes($n['News'])) . "]]></content:encoded>\n";
					$rss .= "</item>\n";
				}
				$rss .= " </channel>\n";
				$rss .= "</rss>\n";
				
				header("Content-Length: " . strlen($rss));
			
				
				define("NOOUT", 1);
				define("OUTPUT", $rss);
				if($GLOBALS['settings']['use_seo'] == 1)
				{
					//include_once('functions/Func.Rewrite.php');
					//$rss = Seo($rss);
				}
				echo $rss;
				exit;
			} else {
				noperm();
			}
		} else {
		
			$GLOBALS['tmpl']->assign('rssfeed_link', redir() . '&amp;mode=rss');
			$GLOBALS['tmpl']->assign('rssfeed_title', $pagetitle);
			$GLOBALS['tmpl']->assign('pagetitle_html', $pagetitle);
			
			if($archive==1)
			{
				$GLOBALS['tmpl']->assign('headernav', $GLOBALS['config_vars']['Newsarchive']);
				setMeta($GLOBALS['config_vars']['Newsarchive'],$GLOBALS['config_vars']['Newsarchive']);
			}
			
			$GLOBALS['tmpl']->assign('pagetitle', stripslashes($row['Titel']));
			
			if($topnews==1)
			{
				$GLOBALS['tmpl']->assign('topnewsitems', $news);
			} else {
				$GLOBALS['tmpl']->assign('newsitems', $news);
			}
		}
	}
	
	
	
	
	//=================================================================
	// Angeforderte News anzeigen
	//=================================================================
	function displayNews($newsid,$noout=0)
	{
		
		if(permission('news'))
		{
			$Lc = $_SESSION['Langcode'];
			$sql = $GLOBALS['db']->Query("
			SELECT 
				*,
				Bild{$Lc} as Bild,
				Titel{$Lc} as Titel,
				Intro{$Lc} as Intro,
				News{$Lc} as News,
				Titel1 as DefTitel,
				News1 as DefNews,
				Intro1 as DefIntro 
			FROM 
				" . PREFIX . "_news 
			WHERE
				Id = '$newsid' AND 
				Aktiv = '1' AND
				Sektion = '" . $_SESSION['area'] . "' AND 
				ZeitStart <= '" . stime() . "'
				
			");
			$row = $sql->fetcharray();
			
			if(is_array($row))
			{
				$row['Titel'] = ($row['Titel']=='') ? $row['DefTitel'] : $row['Titel'];
				$row['Intro'] = ($row['Intro']=='') ? $row['DefIntro'] : $row['Intro'];
				$row['News']  = ($row['News']=='') ? $row['DefNews'] : $row['News'];
				$row['News'] = (!empty($row['Textbilder' . $_SESSION['Langcode']])) ? inlinescreens($row['Textbilder' . $_SESSION['Langcode']], $row['News']) : $row['News'];
				$row['News'] = inlineContactform($row['News']);
				$row['News'] = cleanEmptyTags($row['News']);
				$row['News'] = autowords($row['News']);
				$row['LinkTitel'] = seotitle($row['Titel']);
				$row['User'] = getUsername($row['Autor']);
				//=================================================================
				// Interne Seiten
				//=================================================================
				$_REQUEST['artpage'] = (isset($_REQUEST['artpage']) && $_REQUEST['artpage'] != '' && $_REQUEST['artpage'] >= 1) ? cleanInt($_REQUEST['artpage']) : 1;
				$seite_anzeigen = explode("[--NEU--]", $row['News']);
				$anzahl_seiten = count($seite_anzeigen);
				
				if($_REQUEST['artpage'] > $anzahl_seiten)
				{
					$_REQUEST['artpage'] = $anzahl_seiten;
					$row['News'] = $seite_anzeigen[$anzahl_seiten-1];
				} else {
					$row['News'] = $seite_anzeigen[@$_REQUEST['artpage']-1];
				}
				
				//=================================================================
				// Galerien enthalten?
				//=================================================================
				if($row['Galerien']!='')
				{
					if(file_exists(BASE_DIR . '/class/Class.Gallery.php') && get_active('gallery'))
					{
						include_once(BASE_DIR . '/class/Class.Gallery.php');
						$Gallery = new Gallery;
						$GLOBALS['tmpl']->assign('IncludedGalleries', $Gallery->getExtenGallery($row['Galerien']));
					}
				}
				
				if($row['Tags']!='')
				{
					$GLOBALS['tmpl']->assign('IncludedNews', $this->includedNews($row['Tags'],$row['Id']));
				}
				
				
				//=================================================================
				// Wenn mehrere Seiten vorhanden sind, Navi erzeugen
				//=================================================================
				if($anzahl_seiten>1)
				{
					$article_pages = artpage($anzahl_seiten, $_REQUEST['artpage'], " <a class=\"page_navigation\" href=\"index.php?p=news&amp;area=" . AREA . "&amp;newsid={$newsid}&amp;name="  . $row['LinkTitel'] . "&amp;artpage={s}\">{t}</a> ");
					$GLOBALS['tmpl']->assign('article_pages', $article_pages);
				}
				
			}
			
			//=================================================================
			// Kommentare
			//=================================================================
			if($row['Kommentare'] == 1)
			{
				$comment = new Comments;
				// Kommentar bearbeiten
				if(isset($_POST['comment_action']) && $_POST['comment_action'] == 'edit' && permission('edit_comments')) $comment->EditComment('news',$_POST['comment_id']);
				
				// Neuer Kommentar
				if(permission('comments'))
				{
					$comment->NewComment('news', @$_POST['Redir'],$row['Id']);
				}
				
				// Kommentare holen
				$Comments = $comment->GetComments('news',$row['Id'],'index.php?p=news&amp;newsid=' . $row['Id'] . '&amp;name=' . seotitle($row['Titel']));
				$GLOBALS['tmpl']->assign('GetComments',$GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/comments/comments.tpl'));
			}
			
			// Wertung
			if($row['Bewertung']==1)
			{
				$row['Wertung'] = getRating($row['Id'],'news');
				$GLOBALS['tmpl']->assign('RatingUrl', 'index.php?p=rating&action=rate&id=' . $row['Id'] . '&where=news');
				$GLOBALS['tmpl']->assign('RatingForm', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/other/rating.tpl')); 
			}
			//=================================================================
			// Seitenkopf-Navigationsbaum
			//=================================================================
			//                              NEWS-KATEGORIE    TABELLE           SEITE (p=?)    KATEG    
			
			$headernav = (!is_array($row)) ? '' : $headernav = '<a href="index.php?p=newsarchive&amp;area=' . AREA . '">' . $GLOBALS['config_vars']['Newsarchive'] . '</a>' . getNavigationPath($row['Kategorie'], 'news_kategorie', 'newsarchive&amp;area=' . AREA, 'catid', 'Id', 'Name_' . $_SESSION['Langcode'], '', '', '', '', 'Parent_Id');
			
			setMeta($row['Titel'],$row['News']);
			
			//=================================================================
			// Moegliche Dokumente verknuepfen
			//=================================================================
			if(file_exists(BASE_DIR . '/class/Class.Content.php') && get_active('content'))
			{
				include_once(BASE_DIR . '/class/Class.Content.php');
				$Content = new Content;
				$GLOBALS['tmpl']->assign('IncludedContent', $Content->includedContent($row['Tags']));
			}
			//=================================================================
			// Moegliche Artikel verknuepfen
			//=================================================================
			if(file_exists(BASE_DIR . '/class/Class.Gaming.php') && get_active('articles'))
			{
				include_once(BASE_DIR . '/class/Class.Gaming.php');
				$Gaming = new Gaming;
				$GLOBALS['tmpl']->assign('IncludedArticles', $Gaming->includedArticles($row['Tags']));
			}
			
			$GLOBALS['tmpl']->assign('row', $row);
			$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($headernav, $row['Titel']));
			$GLOBALS['tmpl']->assign('pagetitle', stripslashes($row['Titel']));
			$GLOBALS['tmpl']->assign('headernav', $headernav);
			$GLOBALS['tmpl']->assign('row', $row);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/news/shownews.tpl'));
			
			if(!isset($_SESSION['nr'][$newsid]))
			{
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_news SET Hits = Hits+1 WHERE Id = '{$newsid}'");
				$_SESSION['nr'][$newsid] = 1;
			}
			
		} else {
			noperm();
		}
	}
	
	
	function includedNews($tags,$newsid=0)
	{
		
		$Lc = $_SESSION['Langcode'];
		$tags = explode(',', $tags);
		$found_news = array();
		
		$tags = array_unique($tags);
		$where_not = ($newsid!=0) ? "AND Id != '$newsid'" : "";
		
		if($tags!='')
		{
			foreach($tags as $tag=>$word)
			{
				if($word!='')
				{
					$row = '';
					$res_q = $GLOBALS['db']->Query("
					SELECT 
						Id,
						ZeitStart,
						Kategorie,
						Autor,
						Bild{$Lc} as Bild,
						Titel{$Lc} as Titel,
						Intro{$Lc} as Intro,
						News{$Lc} as News,
						Titel1 as DefTitel,
						News1 as DefNews,
						Intro1 as DefIntro 
					FROM 
						" . PREFIX . "_news 
					WHERE
						Aktiv = '1' AND
						Sektion = '" . $_SESSION['area'] . "' AND 
						ZeitStart <= '" . stime() . "'  AND 
						(Tags LIKE '%$word%') {$where_not}  ORDER BY ZeitStart DESC LIMIT 15
					");
					
					while($row = $res_q->fetcharray())
					{
						$row['Titel'] = ($row['Titel']=='') ? $row['DefTitel'] : $row['Titel'];
						$row['Intro'] = ($row['Intro']=='') ? $row['DefIntro'] : $row['Intro'];
						$row['News']  = ($row['News']=='') ? $row['DefNews'] : $row['News'];
						$row['News'] = eregi_replace("\[CONTACT:([a-zA-Z0-9\\\/\_\.\]*)\]", "", $row['News']);
						$row['News'] = eregi_replace("\[SCREEN:([a-zA-Z0-9\\\/\_\.\]*)\]", "", $row['News']);
						$row['User'] = getUsername($row['Autor']);
						if(!in_array($row,$found_news)) array_push($found_news, $row);
					}
				}
			}
			
			$GLOBALS['tmpl']->assign('externNews',$found_news);
			return $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/news/news_extern.tpl');
		}
	}
	
	
	function getNewsCategs($id, $prefix, &$news_categ, &$area)
	{
		$query = $GLOBALS['db']->Query("SELECT *, Name_" . $_SESSION['Langcode'] . " AS Name FROM " . PREFIX . "_news_kategorie WHERE Parent_Id = '$id' AND Sektion = '$area' ORDER BY POSI ASC");
		if (!$query->numrows()) 
		{
			return;
		} 
		
		while ($item = $query->fetchrow())
		{
			$item->visible_title = $prefix . " " . $item->Name;
			$item->HLink = "index.php?p=newsarchive&amp;area={$item->Sektion}&amp;catid={$item->Id}&amp;name=" . seotitle($item->Name);
			array_push($news_categ, $item);
			$this->getNewsCategs($item->Id, $prefix . " - ", $news_categ, $area);
		} 
		return $news_categ;
	}
}
?>