Merge pull request #73 from gnieark/dev

manage bots
This commit is contained in:
Gnieark 2016-06-09 20:39:39 +02:00
commit edf9de6541
6 changed files with 206 additions and 101 deletions

3
.gitignore vendored
View File

@ -7,6 +7,7 @@
Icon? Icon?
ehthumbs.db ehthumbs.db
Thumbs.db Thumbs.db
.*.kate-swp
.swp.*
src/config.php src/config.php
src/countBattles.txt src/countBattles.txt

View File

@ -167,7 +167,7 @@ if(isset($_GET['arena'])){
die; die;
} }
$rs=mysqli_query($lnMysql, $rs=mysqli_query($lnMysql,
"SELECT id,name,game,url,description,date_inscription "SELECT id,name,game,url,description,unclean_description,date_inscription
FROM bots FROM bots
WHERE id='".mysqli_real_escape_string($lnMysql,$_GET['params'])."' WHERE id='".mysqli_real_escape_string($lnMysql,$_GET['params'])."'
AND active='1'"); AND active='1'");
@ -181,7 +181,8 @@ if(isset($_GET['arena'])){
'game' => $r[2], 'game' => $r[2],
'url' => $r[3], 'url' => $r[3],
'description' => $r[4], 'description' => $r[4],
'date_inscription' => $r[5] 'unclean_description'=> $r[5],
'date_inscription' => $r[6]
); );
$siteTitle="Modifier un bot"; $siteTitle="Modifier un bot";
$siteDescription="bots arena "; $siteDescription="bots arena ";
@ -191,6 +192,30 @@ if(isset($_GET['arena'])){
$cssAdditionalScript=""; $cssAdditionalScript="";
$jsAdditionalScript=""; $jsAdditionalScript="";
break; break;
case "validateEditBot":
//check if secret is ok
if(!isset($_GET['params'])){
error(404,"Page does not exists");
die;
}
$rs=mysqli_query($lnMysql," SELECT 1 FROM bots_modifs WHERE validate_secret='".mysqli_real_escape_string($lnMysql,$_GET['params'])."';");
if(!$r=mysqli_fetch_row($rs)){
error(404,"Page doesn't exist");
die;
}
$siteTitle="Your bot is changed";
$siteDescription="bots arena ";
$permitIndex=false;
$mainSectionScript="../src/validateEditBot.php";
$asideSectionContent=''; //to do
$cssAdditionalScript="";
$jsAdditionalScript="";
break;
default: default:
error(404,"Not found"); error(404,"Not found");
break; break;

View File

@ -1,4 +1,9 @@
CREATE TABLE `arena_history` (
--
-- Structure de la table `arena_history`
--
CREATE TABLE IF NOT EXISTS `arena_history` (
`game` varchar(8) NOT NULL, `game` varchar(8) NOT NULL,
`player1_id` int(11) NOT NULL, `player1_id` int(11) NOT NULL,
`player2_id` int(11) NOT NULL, `player2_id` int(11) NOT NULL,
@ -6,28 +11,54 @@ CREATE TABLE `arena_history` (
`player2_winsCount` int(11) NOT NULL, `player2_winsCount` int(11) NOT NULL,
`nulCount` int(11) NOT NULL, `nulCount` int(11) NOT NULL,
PRIMARY KEY (`game`,`player1_id`,`player2_id`) PRIMARY KEY (`game`,`player1_id`,`player2_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; );
LOCK TABLES `arena_history` WRITE;
/*!40000 ALTER TABLE `arena_history` DISABLE KEYS */;
INSERT INTO `arena_history` VALUES ('Battlesh',10,10,173,438,0),('tictacto',1,1,0,0,44),('tictacto',1,2,46,0,2),('tictacto',1,3,23,0,47),('tictacto',2,1,0,20,4),('tictacto',2,3,0,5,0),('tictacto',3,1,0,0,6),('tictacto',3,2,2,0,0),('tictacto',3,3,3,0,0); --
/*!40000 ALTER TABLE `arena_history` ENABLE KEYS */; -- Structure de la table `bots`
UNLOCK TABLES; --
DROP TABLE IF EXISTS `bots`;
/*!40101 SET @saved_cs_client = @@character_set_client */; CREATE TABLE IF NOT EXISTS `bots` (
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bots` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL, `name` text NOT NULL,
`game` varchar(10) NOT NULL, `game` varchar(10) NOT NULL,
`url` text NOT NULL, `url` text NOT NULL,
`description` text NOT NULL, `description` text NOT NULL,
`unclean_description` text NOT NULL,
`active` int(1) NOT NULL, `active` int(1) NOT NULL,
`date_inscription` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `date_inscription` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`validate_secret` varchar(8) NOT NULL, `validate_secret` varchar(8) NOT NULL,
`author_email` text NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1; );
LOCK TABLES `bots` WRITE;
/*!40000 ALTER TABLE `bots` DISABLE KEYS */; --
INSERT INTO `bots` VALUES (1,'moul','tictactoe','http://tictactoe.m.42.am/','moul\'s Tic Tac Toe resolver in Golang. <a href=\"https://github.com/moul/tictactoe\">Open sourced</a> using Minimax algorithm',1,'2015-12-03 10:55:34',''),(2,'stupidAI','tictactoe','http://morpionmaster.tinad.fr/stupidIa.php','A PHP script that choose next case by... random. <a href=\"https://github.com/jeannedhack/programmingChallenges/blob/master/morpionsFights/Master/stupidIa.php>By Gnieark, here on github</a>',1,'2015-12-03 10:55:34',''),(3,'Gnieark','tictactoe','http://morpionmaster.tinad.fr/gnieark.php','Gnieark\'s PHP AI, using minmax algorythm. <a href=\"https://github.com/gnieark/tictactoeChallenge\">Published on github</a>',1,'2015-12-03 10:55:34',''),(10,'stupidIA','Battleship','https://botsArena.tinad.fr/StupidIABattleship.php','',1,'2015-12-11 11:16:50','!!!'); -- Contenu de la table `bots` only stupid ias
/*!40000 ALTER TABLE `bots` ENABLE KEYS */; --
UNLOCK TABLES;
INSERT INTO `bots` (`id`, `name`, `game`, `url`, `description`, `unclean_description`, `active`, `date_inscription`, `validate_secret`, `author_email`) VALUES
(2, 'stupidAI', 'tictactoe', 'https://ias.tinad.fr/stupidIATictactoe.php', '', '', 1, '2015-12-03 10:55:34', '', ''),
(3, 'stupidAI', 'Battleship', 'https://botsArena.tinad.fr/StupidIABattleship.php', '', '', 1, '2015-12-11 11:16:50', '', ''),
(4, 'stupidAI', 'connectFou', 'https://ias.tinad.fr/StupidIAconnectFour.php', '', '', 1, '2016-05-11 07:47:57', '', '');
-- --------------------------------------------------------
--
-- Structure de la table `bots_modifs`
--
CREATE TABLE IF NOT EXISTS `bots_modifs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`real_id` int(11) NOT NULL,
`name` text NOT NULL,
`game` varchar(10) NOT NULL,
`url` text NOT NULL,
`description` text NOT NULL,
`unclean_description` text NOT NULL,
`date_modification` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`validate_secret` varchar(8) NOT NULL,
`author_email` text NOT NULL,
PRIMARY KEY (`id`);
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

View File

@ -27,7 +27,7 @@ switch($_POST['act']){
} }
//BotUrl //BotUrl
if(!preg_match("/^(http|https):\/\//", $_POST['botURL'])){ if (!preg_match("/^(http|https):\/\//", $_POST['botURL'])){
$alerts.="L'URL n'est pas valide.\n"; $alerts.="L'URL n'est pas valide.\n";
} }
@ -45,7 +45,7 @@ switch($_POST['act']){
//last char must be alphanum. Mail client should cut url if isn't. //last char must be alphanum. Mail client should cut url if isn't.
$secret.=rand_str(1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'); $secret.=rand_str(1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890');
$sql = "INSERT INTO bots (name,game,url,description,active,date_inscription,validate_secret,author_email) VALUES( $sql = "INSERT INTO bots (name,game,url,description,unclean_description,active,date_inscription,validate_secret,author_email) VALUES(
'".mysqli_real_escape_string($lnMysql,htmlentities($_POST['botName']))."', '".mysqli_real_escape_string($lnMysql,htmlentities($_POST['botName']))."',
'".mysqli_real_escape_string($lnMysql,$_POST['botGame'])."', '".mysqli_real_escape_string($lnMysql,$_POST['botGame'])."',
'".mysqli_real_escape_string($lnMysql,htmlentities($_POST['botURL']))."', '".mysqli_real_escape_string($lnMysql,htmlentities($_POST['botURL']))."',
@ -53,6 +53,7 @@ switch($_POST['act']){
preg_replace('#^(http|https|mailto|ftp)://(([a-z0-9\/\.\?-_=\#@:~])*)#i','<a href="$1://$2">$1://$2</a>' preg_replace('#^(http|https|mailto|ftp)://(([a-z0-9\/\.\?-_=\#@:~])*)#i','<a href="$1://$2">$1://$2</a>'
,nl2br(htmlentities($_POST['botDescription']))) ,nl2br(htmlentities($_POST['botDescription'])))
)."', )."',
'".mysqli_real_escape_string($lnMysql,htmlentities($_POST['botDescription']))."',
'0', '0',
NOW(), NOW(),
'".$secret."', '".$secret."',
@ -97,6 +98,7 @@ switch($_POST['act']){
error(404,"wrong post parameter"); error(404,"wrong post parameter");
die; die;
} }
$err=""; $err="";
//check author e-mail //check author e-mail
@ -106,7 +108,7 @@ switch($_POST['act']){
AND id='".mysqli_real_escape_string($lnMysql,$_POST['botId'])."'" AND id='".mysqli_real_escape_string($lnMysql,$_POST['botId'])."'"
); );
if(!$r=mysqli_fetch_row($rs)){ if(!$r=mysqli_fetch_row($rs)){
$err.= "L'adresse e-mail ne correspond pas à celle enregistrée\n"; $err.= "L'adresse e-mail ne correspond pas à celle qui a servi à l'inscription du bot.\n";
} }
//check name //check name
$rs=mysqli_query($lnMysql, $rs=mysqli_query($lnMysql,
@ -117,29 +119,44 @@ switch($_POST['act']){
); );
if($r=mysqli_fetch_row($rs)){ if($r=mysqli_fetch_row($rs)){
$err.="Un bot du même nom existe déjà"; $err.="Un bot du même nom existe déjà.";
} }
//BotUrl //BotUrl
if(!preg_match("/^(http|https):\/\//", $_POST['botURL'])){ if(($_POST['botURL'] <> "") && (!preg_match("/^(http|https):\/\//", $_POST['botURL']))){
$alerts.="L'URL n'est pas valide.\n"; $err.="L'URL n'est pas valide.\n";
} }
if($err == ""){ if($err == ""){
//save bot on temp table //save bot on temp table
$secret=rand_str(8, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'); $secret=rand_str(8, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890');
if( $_POST['botURL'] == "" ){
$rs=mysqli_query($lnMysql,
"SELECT url FROM bots
WHERE game='".mysqli_real_escape_string($lnMysql,$_POST['botGame'])."'
AND id ='".mysqli_real_escape_string($lnMysql,$_POST['botId'])."'"
);
$r=mysqli_fetch_row($rs);
$botUrl = $r[0];
}else{
$botUrl = $_POST['botURL'];
}
mysqli_query($lnMysql, mysqli_query($lnMysql,
" INSERT INTO bots_modifs( name, game, url, description, date_modification, validate_secret, author_email) VALUES ( " INSERT INTO bots_modifs( real_id, name, game, url, description,unclean_description, date_modification, validate_secret, author_email) VALUES (
'".mysqli_real_escape_string($lnMysql,$_POST['botId'])."',
'".mysqli_real_escape_string($lnMysql,htmlentities($_POST['botName']))."', '".mysqli_real_escape_string($lnMysql,htmlentities($_POST['botName']))."',
'".mysqli_real_escape_string($lnMysql,$_POST['botGame'])."', '".mysqli_real_escape_string($lnMysql,$_POST['botGame'])."',
'".mysqli_real_escape_string($lnMysql,$_POST['botURL'])."', '".mysqli_real_escape_string($lnMysql,$botUrl)."',
'".mysqli_real_escape_string($lnMysql, '".mysqli_real_escape_string($lnMysql,
preg_replace('#^(http|https|mailto|ftp)://(([a-z0-9\/\.\?-_=\#@:~])*)#i','<a href="$1://$2">$1://$2</a>' preg_replace('#^(http|https|mailto|ftp)://(([a-z0-9\/\.\?-_=\#@:~])*)#i','<a href="$1://$2">$1://$2</a>'
,nl2br(htmlentities($_POST['botDescription']))) ,nl2br(htmlentities($_POST['botDescription'])))
)."', )."',
'".mysqli_real_escape_string($lnMysql,$_POST['botDescription'])."',
NOW(), NOW(),
'".$secret."', '".$secret."',
'".mysqli_real_escape_string($lnMysql,$_POST['email'])."'" '".mysqli_real_escape_string($lnMysql,$_POST['email'])."')"
); );
//send e-mail //send e-mail
@ -161,7 +178,7 @@ switch($_POST['act']){
$mail->setFrom($smtpParams['username'], 'Bots Arena'); $mail->setFrom($smtpParams['username'], 'Bots Arena');
$mail->Subject = 'BotsArena'; $mail->Subject = 'BotsArena';
$mail->addAddress($_POST['email']); $mail->addAddress($_POST['email']);
$mail->Body = $lang['E_MAIL_EDIT_BOT']."\n".$siteParam['BASEURL'].'p/editBot/'.$secret."\n".$lang['E_MAIL_ADD_BOT_SIGNATURE']; $mail->Body = $lang['E_MAIL_EDIT_BOT']."\n".$siteParam['BASEURL'].'p/validateEditBot/'.$secret."\n".$lang['E_MAIL_ADD_BOT_SIGNATURE'];
if (!$mail->send()) { if (!$mail->send()) {
error(500,"Mailer Error: " . $mail->ErrorInfo); error(500,"Mailer Error: " . $mail->ErrorInfo);
} else { } else {
@ -170,15 +187,11 @@ switch($_POST['act']){
}else{
//echo "plop".$err."plop"; die;
} }
break; break;
default: default:
error(500,"erf"); error(404,"erf");
break; break;
} }

View File

@ -1,28 +1,40 @@
<?php <?php
if(isset($_POST['xd_check'])){ if(isset($_POST['xd_check'])){
//un formulaire a été soumis
$botName=$_POST['botName']; $botName=$_POST['botName'];
$botGame=$_POST['botGame']; $botGame=$_POST['botGame'];
$botURL=$_POST['botURL']; $botURL=$_POST['botURL'];
$botDescription=$_POST['botDescription']; $botDescription=$_POST['botDescription'];
$email=$_POST['email']; $email=$_POST['email'];
if((isset($err)) && ($err <> "")){
$message="<h3>".$err."</h3>";
$editDone=false;
}else{
$editDone=true;
}
}else{ }else{
$botName=$theBot['name']; $botName=$theBot['name'];
$botGame=$theBot['game']; $botGame=$theBot['game'];
$botURL=$theBot['url']; $botURL=$theBot['url'];
$botDescription=$theBot['description']; $botDescription=$theBot['unclean_description'];
$email=""; $email="";
$message="";
$editDone=false;
} }
if($editDone){
echo ' <h2>EditBot</h2><p>Un e-mail vient de vous être envoyé.
Il contient un lien qui vous permettra de confirmer les modifications que vous souhaitez apporter.</p>';
}else{
?> ?>
<h2>EditBot</h2> <h2>EditBot</h2>
<form method="POST" action="/p/editBot/<?php echo $theBot['id']; ?>"> <?php echo $message; ?>
<form method="POST" action="/p/editBot/<?php echo $theBot['id']; ?>">
<?php echo xd_check_input(0); ?><input type="hidden" name="act" value="editBot"/><input type="hidden" name="botId" value="<?php echo $theBot['id']; ?>"/> <?php echo xd_check_input(0); ?><input type="hidden" name="act" value="editBot"/><input type="hidden" name="botId" value="<?php echo $theBot['id']; ?>"/>
<p><label for="botName"><?php echo $lang['BOT_NAME']; ?></label><input id="botName" type="text" name="botName" value="<?php echo htmlentities($botName); ?>" placeholder="<?php echo $lang['YOUR_ALIAS_FOR_EXEMPLE'];?>"/></p> <p><label for="botName"><?php echo $lang['BOT_NAME']; ?></label><input id="botName" type="text" name="botName" value="<?php echo htmlentities($botName); ?>" placeholder="<?php echo $lang['YOUR_ALIAS_FOR_EXEMPLE'];?>"/></p>
<p><label for="botGame"><?php echo $lang['BOT_GAME']; ?></label> <p><label for="botGame"><?php echo $lang['BOT_GAME']; ?></label>
@ -38,8 +50,10 @@ if(isset($_POST['xd_check'])){
} }
?> ?>
</select></p> </select></p>
<p><label for="botURL"><?php echo $lang['BOT_URL']; ?></label><input type="text" name="botURL" id="botURL" value="<?php echo htmlentities($botURL);?>" placeholder="http://"/></p> <p><label for="botURL"><?php echo $lang['BOT_URL']; ?></label><input type="text" name="botURL" id="botURL" value="" placeholder="let empty for keeping the same URL"/></p>
<p><label><?php echo $lang['BOT_DESCRIPTION']; ?></label><textarea name="botDescription"><?php echo htmlentities($botDescription);?></textarea></p> <p><label><?php echo $lang['BOT_DESCRIPTION']; ?></label><textarea name="botDescription"><?php echo htmlentities($botDescription);?></textarea></p>
<p><label for="email"><?php echo $lang['YOUR_EMAIL_FOR_BOT_EDIT']; ?></label><input type="text" name="email" value="<?php echo htmlentities($email);?>" id="email"/></p> <p><label for="email"><?php echo $lang['YOUR_EMAIL_FOR_BOT_EDIT']; ?></label><input type="text" name="email" value="<?php echo htmlentities($email);?>" id="email"/></p>
<p><label for="sub"></label><input id="sub" type="submit" value="<?php echo $lang['SAVE_BOT']; ?>"/></p> <p><label for="sub"></label><input id="sub" type="submit" value="<?php echo $lang['SAVE_BOT']; ?>"/></p>
</form> </form>
<?php
}

21
src/validateEditBot.php Normal file
View File

@ -0,0 +1,21 @@
<?php
$rs=mysqli_query($lnMysql,"SELECT 1 FROM bots_modifs WHERE validate_secret='".mysqli_real_escape_string($lnMysql,$_GET['params'])."';");
if(!$r=mysqli_fetch_row($rs)){
error(404,"Page doesn't exist");
die;
}
mysqli_query($lnMysql,
"UPDATE bots, bots_modifs
SET bots.name = bots_modifs.name
, bots.game = bots_modifs.game
, bots.url = bots_modifs.url
, bots.description = bots_modifs.description
, bots.unclean_description = bots_modifs.unclean_description
WHERE
bots.id=bots_modifs.real_id
AND bots_modifs.validate_secret='".mysqli_real_escape_string($lnMysql,$_GET['params'])."';");
?>
<h2>Thanks!</h2>
<p> Votre bot est validé, merci d'avoir donné à manger à Bots'Arena</p>