<?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                                                   #
#                                                                        #
 ########################################################################
*/

$action = $_GET['action'];

if($action=="save") {

  $opponent = $_POST['opponent'];
  $opphp = $_POST['opphp'];
  $oppcountry = $_POST['oppcounty'];
  $squad = $_POST['squad'];
  $league = $_POST['league'];
  $map = $_POST['map'];
  $server = $_POST['server'];
  $email = $_POST['email'];
  $info = $_POST['info'];
  $hour = $_POST['hour'];
  $minute = $_POST['minute'];
  $month = $_POST['month'];
  $day = $_POST['day'];
  $year = $_POST['year'];	
  
 	$CAPCLASS = new Captcha;
	if(!$CAPCLASS->check_captcha($_POST['captcha'], $_POST['captcha_hash'])) {
			$error="The security code was wrong!";
		die('<b>ERROR: '.$error.'</b><br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
	}
	if(!(strlen(trim($opponent)))) {
	    $error="you forgot to enter your clanname!";
		die('<b>ERROR: '.$error.'</b><br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
	}
	if(!(strlen(trim($league)))) {
	    $error="you forgot to enter a league!";
		die('<b>ERROR: '.$error.'</b><br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
	}
	if(!(strlen(trim($map)))) {
	    $error="you forgot to enter a map!";
		die('<b>ERROR: '.$error.'</b><br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
	}
	if(!(strlen(trim($server)))) {
	    $error="you forgot to enter a server!";
		die('<b>ERROR: '.$error.'</b><br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
	}
	$sem = '^[a-z0-9_\.-]+@[a-z0-9_-]+\.[a-z0-9_\.-]+$';
	if(!(eregi($sem, $email))) {
	    $error="your e-mail is not valid!";
		die('<b>ERROR: '.$error.'</b><br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
	}
	$sem = '^[http://]+[a-z0-9_\.-]+[a-z0-9_-]+$';
	if(!(eregi($sem, $opphp))) {
	    $error="your homepage is not a valid url!";
		die('<b>ERROR: '.$error.'</b><br><br><input type="button" class="button" onClick="javascript:history.back()" value="Back">');
	}
	
	$date=time();
	$cwdate=mktime($hour,$minute,0,$month,$day,$year);
	safe_query("INSERT INTO ".PREFIX."challenge ( date, cwdate, squadID, opponent, opphp, oppcountry, league, map, server, email, info)
                            values( '$date', '$cwdate', '$squad', '$opponent', '$opphp', '$oppcountry', '$league', '$map', '$server', '$email', '$info' ) ");

	$ergebnis=safe_query("SELECT userID FROM ".PREFIX."squads_members WHERE warmember='1' AND squadID='".$squad."'");
	while($ds=mysql_fetch_array($ergebnis)) {
		$touser[]=$ds[userID];
	}

	if($touser[0] != "") {
 	$date = time();
	$message = '[b]There is a new challenge entry![/b]\n\n Click here: [URL]index.php?site=challenge[/URL]';
	foreach($touser as $id) {

          safe_query("INSERT INTO ".PREFIX."messenger (userID, date, fromuser, touser, title, message, viewed)
          values('$id', '$date', '1', '$id', 'Fight us', '".$message."', '0')");  // eintrag für empfänger
          safe_query("UPDATE ".PREFIX."user SET pmgot=pmgot+1 WHERE userID='$id'");
   }
	}

		
		
	redirect('index.php?site=challenge', 'thank you for your entry.');
}
elseif($action=="delete") {
    $chID = $_GET['chID'];
    if(isclanwaradmin($userID)) {
		safe_query("DELETE FROM ".PREFIX."challenge WHERE chID='$chID'");
		redirect('index.php?site=challenge', 'the entry has been deleted.'); 
	}
	else redirect('index.php?site=challenge', 'no access!');
}
else {
    echo'<h2>challenge</h2>';
    
  $type = $_GET['type'];
	
	for($i=1; $i<32; $i++) {
	    if($i==date("d", time())) $day.='<option selected>'.$i.'</option>';
		else $day.='<option>'.$i.'</option>';
	}
	for($i=1; $i<13; $i++) {
	    if($i==date("n", time())) $month.='<option value="'.$i.'" selected>'.date("M", time()).'</option>';
		else $month.='<option value="'.$i.'">'.date("M", mktime(0,0,0,$i,1,2000)).'</option>';
	}
	for($i=2000; $i<2010; $i++) {
	    if($i==date("Y", time())) $year.='<option value="'.$i.'" selected>'.date("Y", time()).'</option>';
		else $year.='<option value="'.$i.'">'.$i.'</option>';
	}
	$squads=getgamesquads();
	
	$CAPCLASS = new Captcha;
	$captcha = $CAPCLASS->create_captcha();
	$hash = $CAPCLASS->get_hash();
	$CAPCLASS->clear_oldcaptcha();
	
	$bg1 = BG_1;
	
	eval ("\$challenge_new = \"".gettemplate("challenge_new")."\";");
    echo $challenge_new;
	
	eval ("\$title_challenge = \"".gettemplate("title_challenge")."\";");
    echo $title_challenge;
	
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."challenge ORDER BY date $type");
	$anz=mysql_num_rows($ergebnis);
	if($anz) {
	    if(!isset($type)) $type = "DESC";
   	    
		if($type=="ASC") 
		    echo'<a href="index.php?site=challenge&type=DESC">Sort:</a> <img src="images/icons/asc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';
		else 
		    echo'<a href="index.php?site=challenge&type=ASC">Sort:</a> <img src="images/icons/desc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';

		
        echo'<br><br>';
        while ($ds = mysql_fetch_array($ergebnis)) {
		    $date = date("d.m.Y", $ds[date]);
			$cwdate = date("d.m.Y - H:i", $ds[cwdate]);
			$squad=getsquadname($ds[squadID]);
			$oppcountry="[flag]".$ds[oppcountry]."[/flag]";
			$country=flags($oppcountry);
			$opponent='<a href="'.$ds[opphp].'" target="_blank">'.clearfromtags($ds[opponent]).'</a>';
			$league=clearfromtags($ds[league]);
			$map=clearfromtags($ds[map]);
			$server=clearfromtags($ds[server]);
			$info=cleartext($ds[info]);
						
			$email = '<a href="mailto:'.$ds[email].'">'.$ds[email].'</a>';
						
			$sem = '^[http://]+[a-z0-9_\.-]+[a-z0-9_-]+$';
	        if(!(eregi($sem, $ds[hp]))) $homepage='';
		    else $homepage='<a href="'.$ds[hp].'" target="_blank"><img src="images/icons/hp.gif" border="0" width="14" height="14" alt="homepage"></a>';
			
			$name=cleartext($ds[name]);
			$message=cleartext($ds[comment]);
			
			if(isclanwaradmin($userID)) 
				$actions='<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=calendar&action=addwar&chID='.$ds[chID].'\');return document.MM_returnValue" value="insert in calendar">
						  <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=challenge&action=delete&chID='.$ds[chID].'\');return document.MM_returnValue" value="delete challange">';
			else $actions='';
						
			eval ("\$challenges = \"".gettemplate("challenges")."\";");
	        echo $challenges;
		}
		echo'<br>';
    }
	else echo'no entries';
}
?>
			

			
