<?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 Poll
{
	//=================================================================
	// Umfragen-Archiv
	//=================================================================
	function GetArchive()
	{
		$LC = $_SESSION['Langcode'];
		$polls = array();
		
		$num_r = $GLOBALS['db']->RowQuery("SELECT DISTINCT(Id) AS Anzahl FROM " . PREFIX . "_umfrage WHERE Sektion='" . $_SESSION['area'] . "' AND Start<='" . stime() . "' ORDER BY Aktiv DESC,Id DESC");
		if(is_object($num_r))
		{
			$num = $num_r->Anzahl;
			
			$limit = limit();
			$seiten = ceil($num / $limit);
			$a = prepage() * $limit - $limit;
			
			$sql = $GLOBALS['db']->Query("SELECT *,Titel_1 AS Titel FROM " . PREFIX . "_umfrage WHERE Sektion='" . $_SESSION['area'] . "' AND Start<='" . stime() . "' ORDER BY Aktiv DESC,Id DESC LIMIT $a,$limit");
			
			while($row = $sql->fetchrow())
			{
				$PollItems = array();
				$pollcount = $GLOBALS['db']->RowQuery("SELECT SUM(Hits) AS HitCount FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='$row->Id'");
				$answ = $GLOBALS['db']->Query("SELECT *,Frage_{$LC} AS Frage FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='$row->Id' ORDER BY Position ASC");
				while($row_answ = $answ->fetchrow())
				{
					$row_answ->Perc = ($row_answ->Hits==0) ? 1 : round(100/$pollcount->HitCount*$row_answ->Hits,2);
					array_push($PollItems, $row_answ);
				}
				$row->HitsAll = $pollcount->HitCount;
				$row->PollItems = $PollItems;
				array_push($polls,$row);
			}
			
			if($num > $limit)
			{
				$GLOBALS['tmpl']->assign('pollNavi', pagenav($seiten, '', 
						"<a class=\"page_navigation\" style=\"text-decoration:none\" href=\"index.php?p=poll&amp;action=archive&amp;page={s}&amp;area=" . AREA . "\">{t}</a> "));
			}
		}
		
		setMeta($GLOBALS['config_vars']['Poll_Archive'],$GLOBALS['config_vars']['Poll_Archive']);
		$GLOBALS['tmpl']->assign('polls', $polls);
		$GLOBALS['tmpl']->assign('headernav', $GLOBALS['config_vars']['Poll_Archive']);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Poll_Archive']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/poll/pollarchive.tpl'));
	}
	
	//=================================================================
	// Umfrage anzeigen
	//=================================================================
	function GetCurrentPoll($id='')
	{
		$LC = $_SESSION['Langcode'];
		if($id=='') 
		{
			$poll = $this->SmallPollResult(@$_REQUEST['polloption'],1,'',$id);
		} else {
			$PollItemsOnce = array();
			$pollcount = $GLOBALS['db']->RowQuery("SELECT SUM(Hits) AS HitCount FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='$id'");
			$answ = $GLOBALS['db']->Query("SELECT *,Frage_{$LC} AS Frage FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='$id' ORDER BY Position ASC");
			while($row_answ = $answ->fetchrow())
			{
				$row_answ->Perc = ($row_answ->Hits==0) ? 1 : round(100/$pollcount->HitCount*$row_answ->Hits,2);
				array_push($PollItemsOnce, $row_answ);
			}
			
			$q = "SELECT Titel_{$LC} AS Titel FROM " . PREFIX . "_umfrage WHERE Id='$id'";
			$Question = $GLOBALS['db']->RowQuery($q);
		
			$GLOBALS['tmpl']->assign('Question', $Question->Titel);
			$GLOBALS['tmpl']->assign('polls_once', $PollItemsOnce);
			$GLOBALS['tmpl']->assign('Extern', 1);
			$poll = $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/poll/polldisplay_result.tpl');
		}
		
				
		
		$where = ($id!='') ? " WHERE Id = '$id' AND Start<='" . stime() . "'" : " WHERE Aktiv='1' AND Start<='" . stime() . "' AND Ende>='" . stime() . "'";
		$res = $GLOBALS['db']->RowQuery("SELECT Id,Titel_{$LC} AS Titel, Start,Ende,Kommentare,Aktiv,IpLog FROM " . PREFIX . "_umfrage {$where}  AND Sektion='" . $_SESSION['area'] . "'");
		
		
		if(is_object($res))
		{
			$pollhits = $GLOBALS['db']->RowQuery("SELECT SUM(Hits) AS HitCount FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='$res->Id'");
			$res->HitsAll = count(explode(',',$res->IpLog))-1;//$pollhits->HitCount;
			
			if($res->Ende < stime() || $res->Start > stime())
			{
				$res->Aktiv='0';
			} else {
				//=================================================================
				// Kommentar neu
				//=================================================================
				if(permission('comments') && $res->Kommentare == 1)
				{
					$comment = new Comments;
					$comment->NewComment('poll', @$_POST['Redir'], $res->Id);
				}
			}
				
			//=================================================================
			// Kommentare
			//=================================================================
			if($res->Kommentare == 1)
			{
				$comment = new Comments;
				if(isset($_POST['comment_action']) && $_POST['comment_action'] == 'edit' && permission('edit_comments')) $comment->EditComment('poll',$_POST['comment_id']);
				if($res->Aktiv!=1) $GLOBALS['tmpl']->assign('noComment', 1);
				$Comments = $comment->GetComments('poll',$res->Id,'index.php?p=poll&amp;id=' . $res->Id . '&amp;name=' . seotitle($res->Titel));
				
			}
			
		} else {
			$GLOBALS['tmpl']->assign('Inactive', 1);
		}
		
		setMeta($GLOBALS['config_vars']['Poll_Name'] . ',' . @$res->Titel,@$res->Titel);
		$GLOBALS['tmpl']->assign('PollRes', $res);
		$GLOBALS['tmpl']->assign('CPoll', $poll);
		$GLOBALS['tmpl']->assign('GetComments',$GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/comments/comments.tpl'));
		$GLOBALS['tmpl']->assign('headernav',  '<a href="index.php?p=poll&amp;action=archive&amp;area=' . AREA . '">' . $GLOBALS['config_vars']['Poll_Name'] . '</a>');
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Poll_Archive'] . $GLOBALS['config_vars']['PageSep'] . @$res->Titel);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/poll/showpoll.tpl'));
	}
	
	//=================================================================
	// Umfrage fuer externe Seite ausgeben
	//=================================================================
	function GetPoll()
	{
		$LC = $_SESSION['Langcode'];
		$q = "SELECT *,Titel_{$LC} AS Titel FROM " . PREFIX . "_umfrage WHERE Aktiv='1' AND Start<='" . stime() . "' AND Ende>='" . stime() . "' AND Sektion='" . $_SESSION['area'] . "'";
		$res = $GLOBALS['db']->RowQuery($q);
		
		if(is_object($res))
		{
			// Gruppen-Check
			$GroupC = explode(',',$res->Gruppen);
			$IPC    = explode(',',$res->IpLog);

			$PollItems = array();
			$pollcount = $GLOBALS['db']->RowQuery("SELECT SUM(Hits) AS HitCount FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='$res->Id'");
			
			$answ = $GLOBALS['db']->Query("SELECT *,Frage_{$LC} AS Frage FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='$res->Id' ORDER BY Position ASC");
			while($row_answ = $answ->fetchrow())
			{
				$row_answ->Perc = ($row_answ->Hits==0) ? 1 : round(100/$pollcount->HitCount*$row_answ->Hits,2);
				array_push($PollItems, $row_answ);
			}
			
			$GLOBALS['tmpl']->assign('PollResultsSmall', $PollItems);
			$GLOBALS['tmpl']->assign('PollAllreadySmall', (in_array($_SERVER['REMOTE_ADDR'],$IPC) ? 1 : 0));
			$GLOBALS['tmpl']->assign('PollPermSmall', (in_array($_SESSION['user_group'],$GroupC) ? 1 : 0));
			$GLOBALS['tmpl']->assign('PollAnswersSmall', $PollItems);
			$GLOBALS['tmpl']->assign('PollTitleSmall', $res->Titel);
			$GLOBALS['tmpl']->assign('PollRes', $res);
		} else {
			// Kein Poll aktiv...
		}
	}
	
	//=================================================================
	// AJAX-Umfrage-Resultat zurueckgeben
	//=================================================================
	function SmallPollResult($pollanswerid,$intern=0,$extern=0,$id='')
	{
		$Entry = true;
		$pollanswerid = abs((int)$pollanswerid);
		if($pollanswerid==0)
		{
			if($intern==1)
			{
				$GLOBALS['tmpl']->assign('Extern', 1);
				return $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/poll/pollout_small_raw.tpl');
			} else {
				if($extern==1) $GLOBALS['tmpl']->assign('Extern', 1);
				echo $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/poll/pollout_small_raw.tpl');
			}
		} else {
			
			$where = ($id!='') ? " WHERE Id = '$id' AND Start<='" . stime() . "'" : " WHERE Aktiv='1' AND Start<='" . stime() . "' AND Ende>='" . stime() . "'";
			$LC = $_SESSION['Langcode'];
			$q = "SELECT *,Titel_{$LC} AS Titel FROM " . PREFIX . "_umfrage {$where}";
			$res = $GLOBALS['db']->RowQuery($q);
			
			// Gruppen-Check
			$GroupC = explode(',',$res->Gruppen);
			$IPC    = explode(',',$res->IpLog);
			
			if(in_array($_SERVER['REMOTE_ADDR'], explode(',',$res->IpLog))) $Entry = false;
			if(!in_array($_SESSION['user_group'], explode(',',$res->Gruppen))) $Entry = false;
			
			if($Entry)
			{
				
				
				$uupdate = ($_SESSION['benutzer_id']>0) ? ",UserLog = CONCAT(UserLog, ',', '" . $_SESSION['benutzer_id'] . "')" : "";
				
				if($res->Multi==1)
				{
					foreach($_POST['polloption'] as $pollval => $pollopt)
					{
						$GLOBALS['db']->Query("UPDATE " . PREFIX . "_umfrage_fragen SET Hits=Hits+1 WHERE Id='" . $_POST['polloption'][$pollopt] . "' AND UmfrageId='$res->Id'");
					}
				} else {
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_umfrage_fragen SET Hits=Hits+1 WHERE Id='$pollanswerid' AND UmfrageId='$res->Id'");
				}
				
				
				// User eintragen
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_umfrage SET IpLog = CONCAT(IpLog, ',', '" . $_SERVER['REMOTE_ADDR'] . "') {$uupdate} WHERE Id='$res->Id'");
				
				
				$PollItems = array();
				$pollcount = $GLOBALS['db']->RowQuery("SELECT SUM(Hits) AS HitCount FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='$res->Id'");
				$answ      = $GLOBALS['db']->Query("SELECT *,Frage_{$LC} AS Frage FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='$res->Id' ORDER BY Position ASC");
				while($row_answ = $answ->fetchrow())
				{
					$row_answ->Perc = ($row_answ->Hits==0) ? 1 : round(100/$pollcount->HitCount*$row_answ->Hits);
					array_push($PollItems, $row_answ);
				}
				
				if($extern==1) $GLOBALS['tmpl']->assign('Extern', 1);
				$GLOBALS['tmpl']->assign('PollRes', $res);
				$GLOBALS['tmpl']->assign('PollResultsSmall', $PollItems);
				$GLOBALS['tmpl']->assign('PollAllreadySmall',1);
				
				$out = $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/poll/pollout_small_raw.tpl');
				
				if($intern==1)
				{
				} else {
					echo $out;
				}
			} else {
				echo 'Possible Attack by: '.$_SERVER['REMOTE_ADDR'];
			}
		}
	}
	
	
	
	
}
?>