Move the cloud.code generation to the DataBase class

This commit is contained in:
Gregory Trolliet 2020-11-25 23:15:38 +01:00
parent 0980d0a82f
commit 3ff96924fb
3 changed files with 58 additions and 19 deletions

View File

@ -29,7 +29,7 @@ if (empty($_POST)) {
</div>
<?php
} else {
$token = bin2hex(random_bytes(DataBase::CLOUD_CODE_LENGTH));
/*$token = bin2hex(random_bytes(DataBase::CLOUD_CODE_LENGTH));*/
if (isset($_POST['fsize']) && is_numeric($_POST['fsize'])) {
$size = $_POST['fsize'];
} else {
@ -45,16 +45,12 @@ if (empty($_POST)) {
} else {
$duration = DataBase::DEFAULT_DURATION;
}
$cpt = 0;
while (!$db->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;
?>
<h2><?php echo L::cloud_links_warning ?></h2>
@ -73,5 +69,10 @@ if (empty($_POST)) {
</div>
</div>
<?php
} else {
?>
<h2><?php echo L::create_errorCode ?></h2>
<?php
}
}
include('templates/footer.php');

51
db.php
View File

@ -12,6 +12,7 @@ class DataBase
'month' => '+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)
{

View File

@ -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 <em>%s</em> 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"