Move the cloud.code generation to the DataBase class
This commit is contained in:
parent
0980d0a82f
commit
3ff96924fb
3 changed files with 58 additions and 19 deletions
19
create.php
19
create.php
|
@ -29,7 +29,7 @@ if (empty($_POST)) {
|
||||||
</div>
|
</div>
|
||||||
<?php
|
<?php
|
||||||
} else {
|
} 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'])) {
|
if (isset($_POST['fsize']) && is_numeric($_POST['fsize'])) {
|
||||||
$size = $_POST['fsize'];
|
$size = $_POST['fsize'];
|
||||||
} else {
|
} else {
|
||||||
|
@ -45,15 +45,11 @@ if (empty($_POST)) {
|
||||||
} else {
|
} else {
|
||||||
$duration = DataBase::DEFAULT_DURATION;
|
$duration = DataBase::DEFAULT_DURATION;
|
||||||
}
|
}
|
||||||
$cpt = 0;
|
|
||||||
while (!$db->createCloud($token, $text, $size, $duration) && $cpt < 10) {
|
if ($cloud = $db->createCloud($text, $size, $duration)) {
|
||||||
$token = bin2hex(random_bytes($length));
|
$viewUrl = 'result.php?id=' . $cloud['code'];
|
||||||
$cpt++;
|
|
||||||
// TODO what to do if no cloud created?
|
|
||||||
}
|
|
||||||
$viewUrl = 'result.php?id=' . $token;
|
|
||||||
$viewName = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $viewUrl;
|
$viewName = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $viewUrl;
|
||||||
$voteUrl = 'index.php?id=' . $token;
|
$voteUrl = 'index.php?id=' . $cloud['code'];
|
||||||
$voteName = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $voteUrl;
|
$voteName = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $voteUrl;
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
@ -73,5 +69,10 @@ if (empty($_POST)) {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<?php
|
<?php
|
||||||
|
} else {
|
||||||
|
?>
|
||||||
|
<h2><?php echo L::create_errorCode ?></h2>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
}
|
}
|
||||||
include('templates/footer.php');
|
include('templates/footer.php');
|
||||||
|
|
49
db.php
49
db.php
|
@ -12,6 +12,7 @@ class DataBase
|
||||||
'month' => '+1 month'
|
'month' => '+1 month'
|
||||||
);
|
);
|
||||||
const DEFAULT_SIZE = 3;
|
const DEFAULT_SIZE = 3;
|
||||||
|
const MAX_SIZE = 9;
|
||||||
const CLOUD_CODE_LENGTH = 6;
|
const CLOUD_CODE_LENGTH = 6;
|
||||||
|
|
||||||
private $db;
|
private $db;
|
||||||
|
@ -162,29 +163,63 @@ class DataBase
|
||||||
array_multisort($values, SORT_DESC, $words);
|
array_multisort($values, SORT_DESC, $words);
|
||||||
return $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(
|
public function createCloud(
|
||||||
string $ref,
|
|
||||||
string $text = '',
|
string $text = '',
|
||||||
int $size = null,
|
int $size = null,
|
||||||
string $duration = null)
|
string $duration = null)
|
||||||
{
|
{
|
||||||
if (!isset($size)) {
|
if (!isset($size)) {
|
||||||
$size = 3;
|
$size = 3;
|
||||||
|
} elseif ($size < 1) {
|
||||||
|
$size = 1;
|
||||||
|
} elseif ($size > self::MAX_SIZE) {
|
||||||
|
$size = self::MAX_SIZE;
|
||||||
}
|
}
|
||||||
if (!isset($duration)) {
|
if (!isset($duration)) {
|
||||||
$duration = self::DEFAULT_DURATION;
|
$duration = self::DEFAULT_DURATION;
|
||||||
} elseif (!key_exists($duration, self::OPTIONS_DURATION)) {
|
} elseif (!key_exists($duration, self::OPTIONS_DURATION)) {
|
||||||
$duration = self::DEFAULT_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]));
|
$duration = date('Y-m-d H:i:s', strtotime(self::OPTIONS_DURATION[$duration]));
|
||||||
$stmt = $this->db->prepare("
|
$stmt = $this->db->prepare("
|
||||||
INSERT INTO clouds(code, text, size, delete_t)
|
INSERT INTO clouds(code, text, size, delete_t)
|
||||||
VALUES (:code, :text, :size, :duration);
|
VALUES (:code, :text, :size, :duration);
|
||||||
");
|
");
|
||||||
$stmt->bindValue(':code', $ref);
|
$stmt->bindValue(':code', $code, PDO::PARAM_STR);
|
||||||
$stmt->bindValue(':text', $text);
|
$stmt->bindValue(':text', $text, PDO::PARAM_STR);
|
||||||
$stmt->bindValue(':size', $size);
|
$stmt->bindValue(':size', $size, PDO::PARAM_INT);
|
||||||
$stmt->bindValue(':duration', $duration, PDO::PARAM_STR);
|
$stmt->bindValue(':duration', $duration, PDO::PARAM_STR);
|
||||||
try {
|
try {
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
|
@ -193,12 +228,12 @@ class DataBase
|
||||||
}
|
}
|
||||||
$this->cloud = array(
|
$this->cloud = array(
|
||||||
'id' => $this->db->lastInsertId(),
|
'id' => $this->db->lastInsertId(),
|
||||||
'code' => $ref,
|
'code' => $code,
|
||||||
'size' => $size,
|
'size' => $size,
|
||||||
'delete_t' => $duration,
|
'delete_t' => $duration,
|
||||||
'text' => $text,
|
'text' => $text,
|
||||||
);
|
);
|
||||||
return true;
|
return $this->cloud;
|
||||||
}
|
}
|
||||||
public function loadCloud($ref)
|
public function loadCloud($ref)
|
||||||
{
|
{
|
||||||
|
|
|
@ -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."
|
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."
|
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]
|
[duration]
|
||||||
day = "un jour"
|
day = "un jour"
|
||||||
week = "une semaine"
|
week = "une semaine"
|
||||||
|
|
Loading…
Reference in a new issue