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

5
.gitignore vendored
View File

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

View File

@ -162,12 +162,12 @@ if(isset($_GET['arena'])){
break;
case "editBot":
if(!isset($_GET['params'])){
if(!isset($_GET['params'])){
error(404,"Page does not exists");
die;
}
$rs=mysqli_query($lnMysql,
"SELECT id,name,game,url,description,date_inscription
"SELECT id,name,game,url,description,unclean_description,date_inscription
FROM bots
WHERE id='".mysqli_real_escape_string($lnMysql,$_GET['params'])."'
AND active='1'");
@ -181,7 +181,8 @@ if(isset($_GET['arena'])){
'game' => $r[2],
'url' => $r[3],
'description' => $r[4],
'date_inscription' => $r[5]
'unclean_description'=> $r[5],
'date_inscription' => $r[6]
);
$siteTitle="Modifier un bot";
$siteDescription="bots arena ";
@ -191,6 +192,30 @@ if(isset($_GET['arena'])){
$cssAdditionalScript="";
$jsAdditionalScript="";
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:
error(404,"Not found");
break;

View File

@ -1,33 +1,64 @@
CREATE TABLE `arena_history` (
--
-- Structure de la table `arena_history`
--
CREATE TABLE IF NOT EXISTS `arena_history` (
`game` varchar(8) NOT NULL,
`player1_id` int(11) NOT NULL,
`player2_id` int(11) NOT NULL,
`player1_winsCount` int(11) NOT NULL,
`player2_winsCount` int(11) NOT NULL,
`nulCount` int(11) NOT NULL,
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 */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `bots`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bots` (
PRIMARY KEY (`game`,`player1_id`,`player2_id`)
);
--
-- Structure de la table `bots`
--
CREATE TABLE IF NOT EXISTS `bots` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`game` varchar(10) NOT NULL,
`url` text NOT NULL,
`description` text NOT NULL,
`unclean_description` text NOT NULL,
`active` int(1) NOT NULL,
`date_inscription` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`validate_secret` varchar(8) NOT NULL,
`author_email` text NOT NULL,
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','!!!');
/*!40000 ALTER TABLE `bots` ENABLE KEYS */;
UNLOCK TABLES;
);
--
-- Contenu de la table `bots` only stupid ias
--
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,8 +27,8 @@ switch($_POST['act']){
}
//BotUrl
if(!preg_match("/^(http|https):\/\//", $_POST['botURL'])){
$alerts.="L'URL n'est pas valide.\n";
if (!preg_match("/^(http|https):\/\//", $_POST['botURL'])){
$alerts.="L'URL n'est pas valide.\n";
}
//email => doit être valide
@ -45,7 +45,7 @@ switch($_POST['act']){
//last char must be alphanum. Mail client should cut url if isn't.
$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,$_POST['botGame'])."',
'".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>'
,nl2br(htmlentities($_POST['botDescription'])))
)."',
'".mysqli_real_escape_string($lnMysql,htmlentities($_POST['botDescription']))."',
'0',
NOW(),
'".$secret."',
@ -97,6 +98,7 @@ switch($_POST['act']){
error(404,"wrong post parameter");
die;
}
$err="";
//check author e-mail
@ -106,7 +108,7 @@ switch($_POST['act']){
AND id='".mysqli_real_escape_string($lnMysql,$_POST['botId'])."'"
);
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
$rs=mysqli_query($lnMysql,
@ -117,68 +119,79 @@ switch($_POST['act']){
);
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
if(!preg_match("/^(http|https):\/\//", $_POST['botURL'])){
$alerts.="L'URL n'est pas valide.\n";
if(($_POST['botURL'] <> "") && (!preg_match("/^(http|https):\/\//", $_POST['botURL']))){
$err.="L'URL n'est pas valide.\n";
}
if($err == ""){
//save bot on temp table
$secret=rand_str(8, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890');
mysqli_query($lnMysql,
" INSERT INTO bots_modifs( name, game, url, description, date_modification, validate_secret, author_email) VALUES (
'".mysqli_real_escape_string($lnMysql,htmlentities($_POST['botName']))."',
'".mysqli_real_escape_string($lnMysql,$_POST['botGame'])."',
'".mysqli_real_escape_string($lnMysql,$_POST['botURL'])."',
'".mysqli_real_escape_string($lnMysql,
preg_replace('#^(http|https|mailto|ftp)://(([a-z0-9\/\.\?-_=\#@:~])*)#i','<a href="$1://$2">$1://$2</a>'
,nl2br(htmlentities($_POST['botDescription'])))
)."',
NOW(),
'".$secret."',
'".mysqli_real_escape_string($lnMysql,$_POST['email'])."'"
);
//send e-mail
include __DIR__."/config.php";
//save bot on temp table
$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,
" 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,$_POST['botGame'])."',
'".mysqli_real_escape_string($lnMysql,$botUrl)."',
'".mysqli_real_escape_string($lnMysql,
preg_replace('#^(http|https|mailto|ftp)://(([a-z0-9\/\.\?-_=\#@:~])*)#i','<a href="$1://$2">$1://$2</a>'
,nl2br(htmlentities($_POST['botDescription'])))
)."',
'".mysqli_real_escape_string($lnMysql,$_POST['botDescription'])."',
NOW(),
'".$secret."',
'".mysqli_real_escape_string($lnMysql,$_POST['email'])."')"
);
//send e-mail
include __DIR__."/config.php";
require __DIR__.'/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
//$mail->IsHTML(true);
//$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = $smtpParams['host'];
$mail->Port = $smtpParams['port'];
$mail->SMTPSecure = $smtpParams['secure'];
$mail->SMTPAuth = true;
$mail->Username = $smtpParams['username'];
$mail->Password = $smtpParams['pass'];
$mail->setFrom($smtpParams['username'], 'Bots Arena');
$mail->Subject = 'BotsArena';
$mail->addAddress($_POST['email']);
$mail->Body = $lang['E_MAIL_EDIT_BOT']."\n".$siteParam['BASEURL'].'p/editBot/'.$secret."\n".$lang['E_MAIL_ADD_BOT_SIGNATURE'];
if (!$mail->send()) {
error(500,"Mailer Error: " . $mail->ErrorInfo);
} else {
//echo "Message sent!";
}
$mail->isSMTP();
//$mail->IsHTML(true);
//$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = $smtpParams['host'];
$mail->Port = $smtpParams['port'];
$mail->SMTPSecure = $smtpParams['secure'];
$mail->SMTPAuth = true;
$mail->Username = $smtpParams['username'];
$mail->Password = $smtpParams['pass'];
$mail->setFrom($smtpParams['username'], 'Bots Arena');
$mail->Subject = 'BotsArena';
$mail->addAddress($_POST['email']);
$mail->Body = $lang['E_MAIL_EDIT_BOT']."\n".$siteParam['BASEURL'].'p/validateEditBot/'.$secret."\n".$lang['E_MAIL_ADD_BOT_SIGNATURE'];
if (!$mail->send()) {
error(500,"Mailer Error: " . $mail->ErrorInfo);
} else {
//echo "Message sent!";
}
}else{
//echo "plop".$err."plop"; die;
}
break;
default:
error(500,"erf");
error(404,"erf");
break;
}

View File

@ -1,45 +1,59 @@
<?php
if(isset($_POST['xd_check'])){
//un formulaire a été soumis
$botName=$_POST['botName'];
$botGame=$_POST['botGame'];
$botURL=$_POST['botURL'];
$botDescription=$_POST['botDescription'];
$email=$_POST['email'];
if((isset($err)) && ($err <> "")){
$message="<h3>".$err."</h3>";
$editDone=false;
}else{
$editDone=true;
}
}else{
$botName=$theBot['name'];
$botGame=$theBot['game'];
$botURL=$theBot['url'];
$botDescription=$theBot['description'];
$botDescription=$theBot['unclean_description'];
$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>
<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']; ?>"/>
<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>
<select id="botGame" name="botGame">
<?php
foreach($arenas as $arena){
if($arena['id'] == $botGame){
$selected='selected="selected"';
}else{
$selected='';
<h2>EditBot</h2>
<?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']; ?>"/>
<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>
<select id="botGame" name="botGame">
<?php
foreach($arenas as $arena){
if($arena['id'] == $botGame){
$selected='selected="selected"';
}else{
$selected='';
}
echo '<option value="'.$arena['id'].'" '.$selected.'>'.$arena['id'].'</option>';
}
echo '<option value="'.$arena['id'].'" '.$selected.'>'.$arena['id'].'</option>';
}
?>
</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><?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="sub"></label><input id="sub" type="submit" value="<?php echo $lang['SAVE_BOT']; ?>"/></p>
</form>
?>
</select></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 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>
</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>