From 3ff96924fbc7055ede6109bfc54b7ac2f85a5aca Mon Sep 17 00:00:00 2001 From: Gregory Trolliet Date: Wed, 25 Nov 2020 23:15:38 +0100 Subject: [PATCH] Move the cloud.code generation to the DataBase class --- create.php | 23 +++++++++++----------- db.php | 51 ++++++++++++++++++++++++++++++++++++++++-------- lang/lang_fr.ini | 3 +++ 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/create.php b/create.php index 28f8b00..f24f899 100644 --- a/create.php +++ b/create.php @@ -29,7 +29,7 @@ if (empty($_POST)) { createCloud($token, $text, $size, $duration) && $cpt < 10) { - $token = bin2hex(random_bytes($length)); - $cpt++; - // TODO what to do if no cloud created? - } - $viewUrl = 'result.php?id=' . $token; - $viewName = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $viewUrl; - $voteUrl = 'index.php?id=' . $token; - $voteName = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $voteUrl; + + if ($cloud = $db->createCloud($text, $size, $duration)) { + $viewUrl = 'result.php?id=' . $cloud['code']; + $viewName = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $viewUrl; + $voteUrl = 'index.php?id=' . $cloud['code']; + $voteName = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $voteUrl; ?>

@@ -73,5 +69,10 @@ if (empty($_POST)) { +

+ '+1 month' ); const DEFAULT_SIZE = 3; + const MAX_SIZE = 9; const CLOUD_CODE_LENGTH = 6; private $db; @@ -162,29 +163,63 @@ class DataBase array_multisort($values, SORT_DESC, $words); return $words; } - + /** + * Create a new cloud + * + * The function will try to generate a new code for the cloud, + * if it fails, the function will return false, otherwise the + * cloud local variable will save the new cloud. + * @param string $text Text associated to the cloud. + * @param int $size The number of words asked each time, + * must be between 1 and MAX_SIZE. + * @param string $duration Duration code, from OPTIONS_DURATION. + * @return False if error, the cloud if success + */ public function createCloud( - string $ref, string $text = '', - int $size = null, + int $size = null, string $duration = null) { if (!isset($size)) { $size = 3; + } elseif ($size < 1) { + $size = 1; + } elseif ($size > self::MAX_SIZE) { + $size = self::MAX_SIZE; } if (!isset($duration)) { $duration = self::DEFAULT_DURATION; } elseif (!key_exists($duration, self::OPTIONS_DURATION)) { $duration = self::DEFAULT_DURATION; } + + $cpt = 0; + $codeIsUsed = true; + while ($codeIsUsed && $cpt < 0) { + $code = bin2hex(random_bytes(self::CLOUD_CODE_LENGTH)); + $stmt = $this->db->prepare(" + SELECT * + FROM clouds + WHERE code = :code; + "); + $stmt->bindValue(':code', $code); + $stmt->execute(); + if (!$data = $stmt->fetch()) { + $codeIsUsed = false; + } + $cpt++; + } + if ($codeIsUsed) { + return false; + } $duration = date('Y-m-d H:i:s', strtotime(self::OPTIONS_DURATION[$duration])); $stmt = $this->db->prepare(" INSERT INTO clouds(code, text, size, delete_t) VALUES (:code, :text, :size, :duration); "); - $stmt->bindValue(':code', $ref); - $stmt->bindValue(':text', $text); - $stmt->bindValue(':size', $size); + $stmt->bindValue(':code', $code, PDO::PARAM_STR); + $stmt->bindValue(':text', $text, PDO::PARAM_STR); + $stmt->bindValue(':size', $size, PDO::PARAM_INT); $stmt->bindValue(':duration', $duration, PDO::PARAM_STR); try { $stmt->execute(); @@ -193,12 +228,12 @@ class DataBase } $this->cloud = array( 'id' => $this->db->lastInsertId(), - 'code' => $ref, + 'code' => $code, 'size' => $size, 'delete_t' => $duration, 'text' => $text, ); - return true; + return $this->cloud; } public function loadCloud($ref) { diff --git a/lang/lang_fr.ini b/lang/lang_fr.ini index b41be26..fea109d 100644 --- a/lang/lang_fr.ini +++ b/lang/lang_fr.ini @@ -45,6 +45,9 @@ success = "Vos mots ont bien été enregistrés, merci de votre participation." missingId = "Le formulaire est mal formé, veuillez contacter la personne responsable du site." cloudNotFound = "Le nuage %s n'a pas été trouvé, veuillez vérifier votre lien." +[create] +errorCode = "Erreur lors de la création du nuage, désolé du dérangement." + [duration] day = "un jour" week = "une semaine"