2019-08-21 20:56:46 +00:00
< ? php
2019-09-07 11:42:36 +00:00
if ( ! isset ( $_SESSION [ " team " ]))
require_once " server_files/403.php " ;
2019-09-02 18:57:26 +00:00
2019-09-07 17:01:23 +00:00
/**
* @ var Team $team
* @ var Tournament $tournament
*/
2019-09-07 11:42:36 +00:00
$team = $_SESSION [ " team " ];
2019-09-07 23:35:05 +00:00
$tournament = Tournament :: fromId ( $team -> getTournamentId ());
2019-09-07 11:42:36 +00:00
2019-09-09 22:55:51 +00:00
$has_error = false ;
$error_message = null ;
2019-09-07 17:01:23 +00:00
if ( isset ( $_POST [ " send_solution " ])) {
2019-09-09 22:55:51 +00:00
$save_solution = new SaveSolution ();
try {
$save_solution -> makeVerifications ();
$save_solution -> saveSolution ();
} catch ( AssertionError $e ) {
$has_error = true ;
$error_message = $e -> getMessage ();
}
2019-09-07 17:01:23 +00:00
}
2019-08-21 20:56:46 +00:00
2019-09-07 23:35:05 +00:00
$solutions = $tournament -> getAllSolutions ( $team -> getId ());
$solutions_final = null ;
if ( $team -> isSelectedForFinal ())
$solutions_final = $FINAL -> getAllSolutions ( $team -> getId ());
2019-09-02 18:57:26 +00:00
2019-09-09 22:55:51 +00:00
class SaveSolution
{
private $problem ;
private $file ;
2019-08-21 20:56:46 +00:00
2019-09-09 22:55:51 +00:00
public function __construct ()
{
2019-12-19 12:02:01 +00:00
$this -> file = $_FILES [ " solution " ];
2019-09-09 22:55:51 +00:00
$this -> problem = htmlspecialchars ( $_POST [ " problem " ]);
}
2019-08-21 20:56:46 +00:00
2019-09-09 22:55:51 +00:00
public function makeVerifications ()
{
global $LOCAL_PATH ;
2019-08-21 20:56:46 +00:00
2019-09-09 22:55:51 +00:00
ensure ( preg_match ( " #[1-9]# " , $this -> problem ), " Le numéro du problème est invalide. " );
ensure ( $this -> file [ " size " ] <= 2e6 , " Le fichier doit peser moins que 2 Mo. " );
ensure ( ! $this -> file [ " error " ], " Une erreur est survenue. " );
ensure ( finfo_file ( finfo_open ( FILEINFO_MIME_TYPE ), $this -> file [ " tmp_name " ]) == " application/pdf " , " Le fichier doit être au format PDF. " );
ensure ( is_dir ( " $LOCAL_PATH /files " ) || mkdir ( " $LOCAL_PATH /files " ), " Un problème est survenue dans l'envoi du fichier. Veuillez contacter l'administrateur du serveur. " );
}
2019-08-21 20:56:46 +00:00
2019-09-09 22:55:51 +00:00
public function saveSolution ()
{
global $LOCAL_PATH , $DB , $team , $tournament , $FINAL ;
2019-09-09 23:02:13 +00:00
2019-09-09 22:55:51 +00:00
do
$id = genRandomPhrase ( 64 );
while ( file_exists ( " $LOCAL_PATH /files/ $id " ));
2019-08-21 20:56:46 +00:00
2019-09-09 22:55:51 +00:00
if ( ! rename ( $this -> file [ " tmp_name " ], " $LOCAL_PATH /files/ $id " ))
throw new AssertionError ( " Une erreur est survenue lors de l'envoi du fichier. " );
2019-08-21 20:56:46 +00:00
2019-09-09 22:55:51 +00:00
$req = $DB -> prepare ( " INSERT INTO `solutions`(`file_id`, `team`, `tournament`, `problem`) VALUES (?, ?, ?, ?); " );
$req -> execute ([ $id , $team -> getId (), $team -> isSelectedForFinal () ? $FINAL -> getId () : $tournament -> getId (), $this -> problem ]);
2019-08-21 20:56:46 +00:00
2019-09-09 22:55:51 +00:00
return false ;
}
2019-08-21 20:56:46 +00:00
}
2019-09-07 11:42:36 +00:00
require_once " server_files/views/solutions.php " ;