<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

if($_GET['action']=="vote") {
  $pollID = $_POST['pollID'];
  $vote = $_POST['vote'];
  include("_mysql.php");
	include("_settings.php");
	include("_functions.php");

 	$anz=mysql_num_rows(safe_query("SELECT pollID FROM `".PREFIX."poll` WHERE pollID='$pollID' AND hosts LIKE '%".$_SERVER[REMOTE_ADDR]."%' AND intern<=".isclanmember($userID).""));
  if($_COOKIE['poll'] != $pollID AND !$anz AND isset($_POST['vote'])) {

   //write cookie
 	 setcookie ("poll", $pollID, time()+(3600*24*365));
 	 
   //write ip
 	 $ds=mysql_fetch_array(safe_query("SELECT hosts FROM `".PREFIX."poll` WHERE pollID='$pollID'"));
	 safe_query("UPDATE ".PREFIX."poll SET hosts='".$ds[hosts]."#".$_SERVER[REMOTE_ADDR]."#' WHERE pollID='$pollID'");
	 
	 //write vote
   safe_query("UPDATE ".PREFIX."poll_votes SET o$vote=o$vote+1 WHERE pollID='$pollID'");
	}
  die(redirect('index.php?site=polls','',0));
}
elseif($_POST['save'])  {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');
	if(!ispollsadmin($userID)) die('No access.');

	safe_query("INSERT INTO ".PREFIX."poll (aktiv, titel, o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, comments, laufzeit, intern)
		         	values( '1', '".$_POST['title']."', '".$_POST['op1']."', '".$_POST['op2']."', '".$_POST['op3']."', '".$_POST['op4']."', '".$_POST['op5']."', '".$_POST['op6']."', '".$_POST['op7']."', '".$_POST['op8']."', '".$_POST['op9']."', '".$_POST['op10']."', '".$_POST['comments']."' ,'".mktime($_POST['laufzeit_hour'],$_POST['laufzeit_minute'],0,$_POST['laufzeit_month'],$_POST['laufzeit_day'],$_POST['laufzeit_year'])."', '".$_POST['intern']."')");
	$id = mysql_insert_id();
	
	safe_query("INSERT INTO ".PREFIX."poll_votes (pollID, o1, o2, o3, o4, o5, o6, o7, o8, o9, o10)
		         values( '$id', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' )");
  die(redirect('index.php?site=polls','',0));
}
elseif($_POST['saveedit']) {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');
	if(!ispollsadmin($userID)) die('No access.');

  $pollID = $_POST['pollID'];
	if($_POST['reset']) {
		safe_query("DELETE FROM ".PREFIX."poll WHERE pollID='$pollID'");
		safe_query("DELETE FROM ".PREFIX."poll_votes WHERE pollID='$pollID'");
		
		safe_query("INSERT INTO ".PREFIX."poll (aktiv, titel, o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, comments, laufzeit, intern) values( '1', '".$_POST['title']."', '".$_POST['op1']."', '".$_POST['op2']."', '".$_POST['op3']."', '".$_POST['op4']."', '".$_POST['op5']."', '".$_POST['op6']."', '".$_POST['op7']."', '".$_POST['op8']."', '".$_POST['op9']."', '".$_POST['op10']."', '".$_POST['comments']."', '".mktime($_POST['laufzeit_hour'],$_POST['laufzeit_minute'],0,$_POST['laufzeit_month'],$_POST['laufzeit_day'],$_POST['laufzeit_year'])."' , '".$_POST['intern']."')");
		$id = mysql_insert_id();
		safe_query("INSERT INTO ".PREFIX."poll_votes (pollID, o1, o2, o3, o4, o5, o6, o7, o8, o9, o10) values( '$id', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' )");
	}
	else safe_query("UPDATE ".PREFIX."poll SET titel='".$_POST['title']."', o1='".$_POST['op1']."', o2='".$_POST['op2']."', o3='".$_POST['op3']."', o4='".$_POST['op4']."', o5='".$_POST['op5']."', o6='".$_POST['op6']."', o7='".$_POST['op7']."', o8='".$_POST['op8']."', o9='".$_POST['op9']."', o10='".$_POST['op10']."', comments='".$_POST['comments']."', laufzeit='".mktime($_POST['laufzeit_hour'],$_POST['laufzeit_minute'],0,$_POST['laufzeit_month'],$_POST['laufzeit_day'],$_POST['laufzeit_year'])."', intern='".$_POST['intern']."' WHERE pollID='$pollID'");
  die(redirect('index.php?site=polls','',0));
}
elseif($_GET['end'])  {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');
	if(!ispollsadmin($userID)) die('No access.');
  $pollID = $_GET['pollID'];
	safe_query("UPDATE ".PREFIX."poll SET aktiv='0' WHERE pollID='$pollID'");
  die(redirect('index.php?site=polls','',0));
}
elseif($_GET['delete']) {
	include("_mysql.php");
	include("_settings.php");
	include('_functions.php');
	if(!ispollsadmin($userID)) die('No access.');

  $pollID = $_GET['pollID'];
	safe_query("DELETE FROM ".PREFIX."poll WHERE pollID = '$pollID'");
	safe_query("DELETE FROM ".PREFIX."poll_votes WHERE pollID = '$pollID'");
  die(redirect('index.php?site=polls','',0));
}

eval ("\$title_polls = \"".gettemplate("title_polls")."\";");
echo $title_polls;

if($_GET['action']=="new") {
    if(ispollsadmin($userID)) {
		$bg1=BG_1;
		eval ("\$polls_new = \"".gettemplate("polls_new")."\";");
	    echo $polls_new;  
	}
	else redirect('index.php?site=news', 'no access!');
}
elseif($_GET['action']=="edit") {
    if(ispollsadmin($userID)) {
    $pollID = $_GET['pollID'];
		$ergebnis = safe_query("SELECT * FROM ".PREFIX."poll WHERE pollID='$pollID'");
		$ds = mysql_fetch_array($ergebnis);

    $ds["laufzeit_year"] = date("y",$ds["laufzeit"]);
    $ds["laufzeit_month"] = date("m",$ds["laufzeit"]);
    $ds["laufzeit_day"] = date("d",$ds["laufzeit"]);
    $ds["laufzeit_hour"] = date("H",$ds["laufzeit"]);
    $ds["laufzeit_minute"] = date("i",$ds["laufzeit"]);
    
		$comments='<option value="0">disable comments</option><option value="1">enable user comments</option><option value="2">enable visitor comments</option>';								
		$comments=str_replace('value="'.$ds[comments].'"', 'value="'.$ds[comments].'" selected', $comments);
		if($ds[intern]) $intern = "checked";
		
		$bg1=BG_1;
		eval ("\$polls_edit = \"".gettemplate("polls_edit")."\";");
	    echo $polls_edit; 
	}
	else redirect('index.php?site=polls', 'no access!');		
}
elseif(isset($_GET['pollID'])) {
  $pollID = $_GET['pollID'];
	if(ispollsadmin($userID)) echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=polls&action=new\');return document.MM_returnValue" value="new poll"><br><br>';
		
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."poll WHERE pollID='$pollID' AND intern<=".isclanmember($userID));
	$ds = mysql_fetch_array($ergebnis);
	$bg1=BG_1;
	$title=stripslashes($ds[titel]);

  if($ds[laufzeit] < time() OR $ds[aktiv] == "0") $timeleft = "poll already ended"; else $timeleft = floor(($ds[laufzeit]-time())/(60*60*24))." days";
						
	for ($n=1; $n<=10; $n++) {
        if($ds[o.$n]) $options[]=$ds[o.$n];
	}
			
	if(ispollsadmin($userID)) {
		if($ds[aktiv]) {
			$stop=' <input type="button" class="button" onClick="MM_confirm(\'really stop this poll?\', \'polls.php?end=true&pollID='.$ds[pollID].'\')" value="stop poll"> ';
			$edit=' <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=polls&action=edit&pollID='.$ds[pollID].'\');return document.MM_returnValue" value="edit"> ';
		}
		else {
			$stop=' <b>stopped Poll</b>';
			$edit='';
		}
		$adminactions=$edit.'<input type="button" class="button" onClick="MM_confirm(\'really delete this poll?\', \'polls.php?delete=true&pollID='.$ds[pollID].'\')" value="delete">'.$stop;			
	}
			
	$votes = safe_query("SELECT * FROM ".PREFIX."poll_votes WHERE pollID='$pollID' ");
	$dv = mysql_fetch_array($votes);
	$gesamtstimmen = $dv[o1] + $dv[o2] + $dv[o3] + $dv[o4] + $dv[o5] + $dv[o6] + $dv[o7] + $dv[o8] + $dv[o9] + $dv[o10]; 
	$n=1;
			
	eval ("\$polls_head = \"".gettemplate("polls_head")."\";");
    echo $polls_head;
			
	foreach ($options as $option) {
		$stimmen = $dv[o.$n];
		if ($gesamtstimmen) {
	        $perc = $stimmen / $gesamtstimmen * 10000;
		    settype($perc, "integer");
		    $perc = $perc/100;
		}
		else $perc = 0;
		$picwidth = $perc;
		settype($picwidth, "integer");
									
		if($picwidth) $pic='<table width="100" cellspacing="1" cellpadding="1" bgcolor="'.BORDER.'">
							  <tr bgcolor="'.BG_2.'">
							    <td background="images/icons/poll_bg.gif"><img src="images/icons/poll_start.gif" width="1" height="5"><img src="images/icons/poll.gif" width="'.$picwidth.'" height="5"><img src="images/icons/poll_end.gif" width="1" height="5"></td>
							  </tr>
							</table>';
		else $pic='';
				
		eval ("\$polls_content = \"".gettemplate("polls_content")."\";");
		echo $polls_content; 
		$n++;
	}
			
			
	eval ("\$polls_foot = \"".gettemplate("polls_foot")."\";");
    echo $polls_foot; 
	
	$comments_allowed = $ds[comments];
	$parentID = $pollID;
	$type = "po";
	$referer = "index.php?site=polls&pollID=$pollID";
		
	include("comments.php");
}
elseif(isset($_GET['vote'])) {

	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;

  $poll = $_GET['vote'];
	
  $lastpoll = safe_query("SELECT * FROM ".PREFIX."poll WHERE aktiv='1' AND laufzeit>".time()." AND intern<=".isclanmember($userID)." and pollID='".$poll."' LIMIT 0,1");

	$anz = mysql_num_rows($lastpoll);
	$ds = mysql_fetch_array($lastpoll);
	if($anz) {
      $anz=mysql_num_rows(safe_query("SELECT pollID FROM `".PREFIX."poll` WHERE pollID='".$ds[pollID]."' AND hosts LIKE '%".$_SERVER[REMOTE_ADDR]."%' AND intern<=".isclanmember($userID).""));
      if($_COOKIE['poll'] == $ds[pollID] OR $anz) redirect('index.php?site=polls&pollID='.$ds[pollID],'You have already voted.');
		else {
			echo'<table cellspacing="2" cellpadding="0">
				   <tr><form method="post" action="polls.php?action=vote">
				   <td><b>'.$ds[titel].'</b><br><br></td></tr><tr><td>';
				
			for ($n=1; $n<=10; $n++) {
			    if($ds[o.$n]) $options[]=$ds[o.$n];
			}
			$n=1;
			foreach ($options as $option) {
		        echo'<input class="input" type="radio" name="vote" value="'.$n.'"> '.$option.'<br>';
				$n++;    
			}
			echo'</td></tr><tr><td><br>
			    <input type="hidden" name="pollID" value="'.$ds[pollID].'">
			    <input type="submit" value="vote"></td></form></tr><tr><td><br>&#8226; <a href="index.php?site=polls">show polls</a></td></tr></table>';
	    }
	}	else redirect('index.php?site=polls&pollID='.$ds[pollID],'Poll already ended.');
}
else {
	if(ispollsadmin($userID)) echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=polls&action=new\');return document.MM_returnValue" value="new poll"><br><br>';
		
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."poll WHERE intern<=".isclanmember($userID)." ORDER BY pollID DESC");
	$anz = mysql_num_rows($ergebnis);
	if($anz) {
	    $i=1;
		while ($ds = mysql_fetch_array($ergebnis)) {
			if($i%2) $bg1=BG_1;
			else $bg1=BG_2;

			$title=stripslashes($ds[titel]);

      if($ds[laufzeit] < time() OR $ds[aktiv] == "0") $timeleft = "poll already ended"; else $timeleft = floor(($ds[laufzeit]-time())/(60*60*24))." days (".date("d.m.Y H:i",$ds[laufzeit]).") <br><a href='index.php?site=polls&vote=".$ds[pollID]."'>[vote now]</a>";
      			
			for ($n=1; $n<=10; $n++) {
		        if($ds[o.$n]) $options[]=$ds[o.$n];
			}
			
			if(ispollsadmin($userID)) {
				if($ds[aktiv]) {
					$stop=' <input type="button" class="button" onClick="MM_confirm(\'really stop this poll?\', \'polls.php?end=true&pollID='.$ds[pollID].'\')" value="stop poll"> ';
					$edit=' <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=polls&action=edit&pollID='.$ds[pollID].'\');return document.MM_returnValue" value="edit"> ';
				}
				else {
					$stop=' <b>stopped Poll</b>';
					$edit='';
				}
				
				$adminactions=$edit.'<input type="button" class="button" onClick="MM_confirm(\'really delete this poll?\', \'polls.php?delete=true&pollID='.$ds[pollID].'\')" value="delete">'.$stop;			
			}
			
			$votes = safe_query("SELECT * FROM ".PREFIX."poll_votes WHERE pollID='$ds[pollID]' ");
			$dv = mysql_fetch_array($votes);
      $gesamtstimmen = $dv[o1] + $dv[o2] + $dv[o3] + $dv[o4] + $dv[o5] + $dv[o6] + $dv[o7] + $dv[o8] + $dv[o9] + $dv[o10];
			$n=1;
			
			eval ("\$polls_head = \"".gettemplate("polls_head")."\";");
	        echo $polls_head;
			
			foreach ($options as $option) {
				$stimmen = $dv[o.$n];
				if ($gesamtstimmen) {
			        $perc = $stimmen / $gesamtstimmen * 10000;
				    settype($perc, "integer");
				    $perc = $perc/100;
				}
				else $perc = 0;
				$picwidth = $perc;
				settype($picwidth, "integer");
										
				if($picwidth) $pic='<table width="104" cellspacing="1" cellpadding="1" bgcolor="'.BORDER.'">
									  <tr bgcolor="'.BG_2.'">
									    <td background="images/icons/poll_bg.gif"><img src="images/icons/poll.gif" width="'.($picwidth).'" height="5"></td>
									  </tr>
									</table>';
				else $pic='<table width="104" cellspacing="1" cellpadding="1" bgcolor="'.BORDER.'">
									  <tr bgcolor="'.BG_2.'">
									    <td background="images/icons/poll_bg.gif"><img src="images/spacer.gif" width="100" height="5"></td>
									  </tr>
									</table>';
				
				$anzcomments = getanzcomments($ds[pollID], 'po');
				if($anzcomments) $comments='<a href="index.php?site=polls&pollID='.$ds[pollID].'">['.$anzcomments.'] comments</a> latest by '.getlastcommentposter($ds[pollID], 'po').' - '.date("d.m.Y - H:i", getlastcommentdate($ds[pollID], 'po'));
				else $comments='<a href="index.php?site=polls&pollID='.$ds[pollID].'">[0] comments</a>';
				
				eval ("\$polls_content = \"".gettemplate("polls_content")."\";");
	            echo $polls_content; 
				
				$n++;
			}
			
			eval ("\$polls_foot = \"".gettemplate("polls_foot")."\";");
	        echo $polls_foot;  
			$i++;
			
			
			unset($options);
		}
	}
	else echo'no entries!';

}

?>
