<?php
// ======================================================================== //
//                                                                          //
//   KooBI- Webportalsystem                                                 //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   ====================================================================   //
//                                                                          //   
//   dream4 | webservices - Am Michelsgrund 7 - 69469 Weinheim - Germany    //    
//   tel. +49(0) 6201 - 980442 / fax. +49(0) 6201 - 64809                   //
//                                                                          //
// ======================================================================== //
if(!checkactive('poll')) include(dirname(__FILE__)."/lib/notactive.php" );
ob_start();

$poll_id = $_REQUEST['poll_id'];
$item_id = $_REQUEST['item'];

// <<-- archiv anzeigen -->>
if($_REQUEST['show'] == "archive"){
	$polls = array();
	
	switch($_REQUEST['sort']){
		
		case "title":
		$ord = " ORDER BY title ASC";
		break;
		
		case "hits":
		$ord = "ORDER BY hits DESC";
		break;
		
		case "time":
		$ord = "ORDER BY start DESC";
		break;
		
		case "":
		$ord = "ORDER BY start DESC";
		break;
	}
	$sql = new dbc("SELECT * FROM " . PREFIX . "_poll WHERE active='1' AND area_id = '$area' $ord");
	
	while ($row = $sql->fetchrow()){
		$sql_2 = new dbc("SELECT SUM(hits) AS summe FROM " . PREFIX . "_poll_item WHERE poll_id='$row->id'");
		$row_2 = $sql_2->fetchrow();
		$row->votes = $row_2->summe;
		$row->start = mygmt($row->start);
		array_push($polls, $row);
	}
	
	$tmpl->assign('polls', $polls);	
	$tmpl->assign('content', parsetrue("container/".container('vote'), $lang['poll_archive'], $tmpl->fetch('poll/poll_archive.tpl')));
	
	} else {
	
	if($_REQUEST['dopoll']==1){
		$r_ip = new dbc("SELECT ip,group_id,active,start,end,can_comment FROM " . PREFIX . "_poll WHERE id = '$poll_id' and end>='".time()."' and start<='".time()."'");
		$ip = $r_ip->fetchrow();
		
		
		// <<-- Ungültiger Poll- Aufruf? -->>
		if( (!$ip) || ($ip->active!=1) || ($ip->start > time()) ){
			$EOUT = msg("error_once", "votenotactive", str_replace("__URL__", "index.php?area={$area}", $lang['redirect']), "index.php?area={$area}");
			$NOOUT = 1;
		}
		if($NOOUT!=1){
			$ip_a = ($ip->ip == "") ? $ip->ip : explode(";", $ip->ip);
			$group_id = ($ip->group_id == "") ? $ip->group_id : explode(",", $ip->group_id);
			
			// <<-- Besitzt diese Gruppe die Rechte zum abstimmen? -->>
			if (@in_array(UGROUP, $group_id)) {
				
				// <<-- Wenn IP des Besuchers nicht gespeichert ist... -->>
				if (!@in_array($_SERVER['REMOTE_ADDR'], $ip_a)) {
					$inc = new dbc("UPDATE " . PREFIX . "_poll_item SET hits = hits + 1 WHERE id = '$item_id'");
					
					if(UGROUP != 2) {
						$zusatz = " , uid = CONCAT(uid, ';', '" . UID . "')";
					}
					$update = new dbc("UPDATE " . PREFIX . "_poll SET ip = CONCAT(ip, ';', '" . $_SERVER['REMOTE_ADDR'] . "') $zusatz WHERE id = $poll_id");
					$EOUT = msg("thankyou", "votethanks", str_replace("__URL__", "index.php?p=poll&amp;poll_id=$_REQUEST[poll_id]&amp;showresult=1&amp;area={$area}", $lang['redirect']), "index.php?p=poll&amp;poll_id=$_REQUEST[poll_id]&amp;showresult=1&amp;area={$area}");
					$NOOUT = 1;
					
					// <<-- Wenn User bereits abgestimmt hat -->>
				} else {
					$EOUT = msg("error_once", "justvoted", str_replace("__URL__", "index.php?p=poll&amp;poll_id=$_REQUEST[poll_id]&amp;showresult=1&amp;area={$area}", $lang['redirect']), "index.php?p=poll&amp;poll_id=$_REQUEST[poll_id]&amp;showresult=1&amp;area={$area}");
					$NOOUT = 1;
				}
				
				// <<-- Wenn User keine Berechtigung besitzt... -->>
			} else {
				$EOUT = msg("error_once", "votenopermission", str_replace("__URL__", "index.php?p=poll&amp;poll_id=$_REQUEST[poll_id]&amp;showresult=1&amp;area={$area}", $lang['redirect']), "index.php?p=poll&amp;poll_id=$_REQUEST[poll_id]&amp;showresult=1&amp;area={$area}");
				$NOOUT = 1;
			}
		}
	}
	
	// <<-- Ergebnisse anzeigen -->>
	if($_REQUEST['showresult'] == 1){
		$poll_id = $_REQUEST['poll_id'];
		$query = "SELECT SUM(hits) AS summe FROM " . PREFIX . "_poll_item WHERE poll_id = $poll_id";
		$r_sum = new dbc($query);
		$sum = $r_sum->fetchrow();
		
		$sql = new dbc("SELECT place_id, title,can_comment,start,end,active,group_id FROM " . PREFIX . "_poll WHERE id='".$poll_id."'");
		$row = $sql->fetchrow();
		$cancomment = $row->can_comment;
		
		if( ($row->end < time()) || ($row->active!=1) ) $tmpl->assign('status', $lang['voteexpired_extended']);
		if( ($row->end >= time()) && ($row->active==1) ) $tmpl->assign('status', $lang['voteactive']);
		if( ($row->start > time()) ) $tmpl->assign('status', $lang['votestillinactive']);
		
		$tmpl->assign('start', mygmt($row->start));
		$tmpl->assign('end', mygmt($row->end));
		$tmpl->assign('id', $row->place_id);
		
		$diff = $row->end - $row->start;
		
		$i = 1;
		$groups = explode(",", $row->group_id);
		foreach($groups as $v){
			$permgroups[$i]['name'] = fetchgroupname($v);
			$i++;
		}
		$tmpl->assign('row', $row);
		$tmpl->assign('permgroups', $permgroups);
		
		$r_hits = new dbc("SELECT title, hits, color,posi FROM " . PREFIX . "_poll_item WHERE poll_id = $poll_id ORDER BY posi ASC");
		
		
		$hits = array();
		
		while ($hit = $r_hits->fetchrow()) {
			if(!$hit->hits) { 
				$hit->votes = 0 ; 
				$hit->percent = 0 ;
			} else {
				$hit->percent = (int)(($hit->hits * 100) / $sum->summe);
			}
			$hit->all = $hit->hits;
			$hit->text = ($hit->hits==1) ? $lang['votestonce'] : $lang['votesall'];
			array_push($hits, $hit);
		}
		
		
		$tmpl->assign('ptitle', $row->title);
		$tmpl->assign('hits', $hits);
		$tmpl->assign('sum', $sum->summe);
		
		// <<-- SEITENTITEL -->>
		$sname = simpletitle($lang['voteresult'] . $lang['f_navi_separator'] . $row->title );
		
		// <<-- DIE EIGENTLICHE UMFRAGE -->>
		$tmpl->assign('poll', parsetrue("container/".container('vote'), $lang['voteresult'], $tmpl->fetch('poll/poll_result.tpl')));
		
		// =========================================================
		// <<-- KANN UMFRAGE KOMMENTIERT WERDEN? -->>
		// =========================================================
		if($cancomment==1){
			// <<-- NEWSKOMMENTAR EINTRAGEN -->>
			$sql = new dbc("SELECT spamprotect,spamprotect_time,mailbycomment FROM " . PREFIX . "_settings");
			$row_spam = $sql->fetchrow();
			
			if($_REQUEST['post']==1){
				if($row_spam->spamprotect==1){
					$sql = new dbc("SELECT ip,ctime FROM ".PREFIX."_pollcomments WHERE ip='".$_SERVER['REMOTE_ADDR']."' AND pollid='".$_REQUEST['poll_id']."' 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?p=poll&amp;poll_id=".$_REQUEST['poll_id']."&amp;showresult=1&amp;area=".$_REQUEST['area']."", $lang['redirect']), "index.php?p=poll&amp;poll_id=".$_REQUEST['poll_id']."&amp;showresult=1&amp;area=".$_REQUEST['area']."");
							$NOOUT = 1;
							$error = 1;
						}
					}
				}
				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/poll.php?action=comment&id=$_REQUEST[poll_id]&area=$_REQUEST[area]";
						$path = dirname(__FILE__);
						include($path ."/lib/mailbycomment.php" );
					}
					
					$text = formtext($_POST['text'],$MAXCOMMLENGTH);
					$sql = new dbc("
					INSERT INTO ".PREFIX."_pollcomments
					(
					id,
					pollid,
					ctime,
					autor,
					title,
					comment,
					ip
					) VALUES (
					'',
					'".escs($_REQUEST['poll_id'])."',
					'".time()."',
					'".$UID."',
					'".iform($_POST['title'],"255","")."',
					'".$text."',
					'".$_SERVER['REMOTE_ADDR']."
					') 
					");
					
					$EOUT = msg("thankyou","pollcomments_thanks", str_replace("__URL__", "index.php?p=poll&amp;poll_id=".$_REQUEST['poll_id']."&amp;showresult=1&amp;area=".$_REQUEST['area']."", $lang['redirect']), "index.php?p=poll&amp;poll_id=".$_REQUEST['poll_id']."&amp;showresult=1&amp;area=".$_REQUEST['area']."");
					$NOOUT = 1;
				}
				
			}
			
			// <<-- KOMMENTARE AUSLESEN -->>
			$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 pollid FROM ".PREFIX."_pollcomments WHERE pollid='".escs($_REQUEST['poll_id'])."'");
			$num = $sql->numrows();
			
			$seiten = ceil($num / $limit);
			$a = prepage() * $limit - $limit;
			
			$sql = new dbc("SELECT * FROM ".PREFIX."_pollcomments WHERE pollid='".escs($_REQUEST['poll_id'])."' ORDER BY id $sorts limit $a,$limit");
			$erg = $sql->numrows();
			
			
			$tmpl->assign('lang', $lang);
			$path = dirname(__FILE__);
			include($path ."/lib/pagesobjects.php" );
			include($path ."/lib/commentobjects.php" );
			
			// <<-- KOMMENTARE AUSGEBEN -->>
			if($num>$limit){
				$nav = pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?p=poll&amp;showresult=1&amp;poll_id=".escs($_REQUEST['poll_id'])."&amp;area=".$area."&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);
			if($erg) $tmpl->assign('comments', parsetrue("container/".container('comments'), $lang['pollcomments'], $tmpl->fetch('comments/comments.tpl')));
			
			// <<-- KOMMENTAFORM AUSGEBEN -->>
			if(permission('canpollcomment')){;
				$tmpl->assign('formlink', "index.php?p=poll&amp;poll_id=".escs($poll_id)."&amp;showresult=1&amp;area=$area&amp;post=1");
				$tmpl->assign('commentform', parsetrue("container/".container("comments"), $lang['postcomment'], $tmpl->fetch("comments/commentform.tpl")));
			} else {
				$tmpl->assign('commentform', $tmpl->fetch('comments/commentform_false.tpl'));
			}
		}
		
		
	}
	
	$tmpl->assign('content', $tmpl->fetch('poll/showpoll.tpl'));
}
?>