From 31d43467ce85f4fbf72c8ee08f61d718d1ed1402 Mon Sep 17 00:00:00 2001
From: Gregory Trolliet <gregory@trolliet.info>
Date: Thu, 26 Nov 2020 16:59:44 +0100
Subject: [PATCH 1/4] Fix the override of the 'create' value

---
 create.php       | 5 ++---
 index.php        | 2 +-
 lang/lang_fr.ini | 2 +-
 3 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/create.php b/create.php
index f24f899..7b8f98f 100644
--- a/create.php
+++ b/create.php
@@ -24,12 +24,11 @@ if (empty($_POST)) {
 			<label for="fsize"><?php echo L::cloud_size ?></label>
 			<input type="number" id="fsize" name="fsize" min="1" max="9" value=<?php echo DataBase::DEFAULT_SIZE ?>>
 		</div>
-		<input type="submit" id="cloud_create_submit" value="<?php echo L::create ?>">
+		<input type="submit" id="cloud_create_submit" value="<?php echo L::create_button ?>">
 	</form>
 </div>
 <?php
 } else {
-	/*$token = bin2hex(random_bytes(DataBase::CLOUD_CODE_LENGTH));*/
 	if (isset($_POST['fsize']) && is_numeric($_POST['fsize'])) {
 		$size = $_POST['fsize'];
 	} else {
@@ -45,7 +44,7 @@ if (empty($_POST)) {
 	} else {
 		$duration = DataBase::DEFAULT_DURATION;
 	}
-	
+
 	if ($cloud = $db->createCloud($text, $size, $duration)) {
 		$viewUrl  = 'result.php?id=' . $cloud['code'];
 		$viewName = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $viewUrl;
diff --git a/index.php b/index.php
index ced4ef2..806b854 100644
--- a/index.php
+++ b/index.php
@@ -29,7 +29,7 @@ if (isset($_GET['id'])) {
 ?>
 	<p><?php echo L::cloud_create_message ?></p>
 	<form method="post" action="create.php">
-		<input type="submit" value="<?php echo L::create ?>">
+		<input type="submit" value="<?php echo L::create_button ?>">
 	</form>
 <?php
 }
diff --git a/lang/lang_fr.ini b/lang/lang_fr.ini
index fea109d..d002dff 100644
--- a/lang/lang_fr.ini
+++ b/lang/lang_fr.ini
@@ -18,7 +18,6 @@
 
 title = "SimpleWordsCloud"
 description = "Création de nuage de mots, simple, sans inscriptions et respectant la vie privée"
-create = "Créer"
 
 [cloud]
 description = "Description du nuage"
@@ -47,6 +46,7 @@ cloudNotFound = "Le nuage <em>%s</em> n'a pas été trouvé, veuillez vérifier
 
 [create]
 errorCode = "Erreur lors de la création du nuage, désolé du dérangement."
+button = "Créer"
 
 [duration]
 day = "un jour"

From 12045275d5e48d9e9fcd357c11a30601ed2ca3ea Mon Sep 17 00:00:00 2001
From: Gregory Trolliet <gregory@trolliet.info>
Date: Thu, 26 Nov 2020 17:03:27 +0100
Subject: [PATCH 2/4] Simplify the cloud generation code

---
 db.php | 59 ++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 22 deletions(-)

diff --git a/db.php b/db.php
index 5e9fbbe..6063d7d 100644
--- a/db.php
+++ b/db.php
@@ -131,16 +131,40 @@ class DataBase
 			$stmt->execute();
 		}
 	}
-	public function getWordsList(string $id)
+	/**
+	 * Get the list of the words inside the cloud
+	 *
+	 * If there is no $code parameter and the cloud isn't already loaded,
+	 * return null.
+	 *
+	 * Return a list of words, orderer from the most used to the least,
+	 * with the following fields:
+	 * - word     => the word
+	 * - count    => the number of occurences of the word
+	 * - relative => the relative number of occurences,
+	 *               relative to the occurence max
+	 * - percent  => the percent of word in the cloud,
+	 *               relative to the total number of words
+	 * @param  string $code Code of the cloud
+	 * @return array|null   The words list
+	 */
+	public function getWordsList(string $code = null)
 	{
+		if (isset($code)) {
+			$this->loadCloudByCode($code);
+		}
+		if (!isset($code) && !$this->isCloudSet()) {
+			return null;
+		}
+
 		$stmt = $this->db->prepare("
 			SELECT *
 			FROM   words
 			JOIN   clouds
 			ON     words.cloud_id = clouds.id_cloud
-			WHERE  code = :id;
+			WHERE  code = :code;
 		");
-		$stmt->bindValue(':id', $id);
+		$stmt->bindValue(':code', $this->cloud['code']);
 		$stmt->execute();
 		$words  = [];
 		$values = [];
@@ -157,7 +181,7 @@ class DataBase
 		}
 		foreach ($words as $key => $word) {
 			$words[$key]['relative'] = $word['count'] / $max;
-			$words[$key]['percent'] = $word['count'] / $total * 100;
+			$words[$key]['percent']  = $word['count'] / $total * 100;
 		}
 
 		array_multisort($values, SORT_DESC, $words);
@@ -193,34 +217,25 @@ class DataBase
 			$duration = self::DEFAULT_DURATION;
 		}
 
-		$cpt = 0;
-		$codeIsUsed = true;
-		while ($codeIsUsed && $cpt < 0) {
+		for ($i = 0; $i < 10; $i++) {
 			$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;
+			if (!$this->loadCloudByCode($code)) {
+				echo 'LOAD CLOUD';
+				break;
 			}
-			$cpt++;
 		}
-		if ($codeIsUsed) {
+		if ($this->isCloudSet()) {
 			return false;
 		}
-		$duration = date('Y-m-d H:i:s', strtotime(self::OPTIONS_DURATION[$duration]));
+		$delete = 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);
+			VALUES      (:code, :text, :size, :delete);
 		");
 		$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);
+		$stmt->bindValue(':delete', $delete, PDO::PARAM_STR);
 		try {
 			$stmt->execute();
 		} catch (PDOEXception $e) {
@@ -230,7 +245,7 @@ class DataBase
 			'id'       => $this->db->lastInsertId(),
 			'code'     => $code,
 			'size'     => $size,
-			'delete_t' => $duration,
+			'delete_t' => $delete,
 			'text'     => $text,
 		);
 		return $this->cloud;

From 4d5f50109c6255f7ae365be4ff3aa16b90d9a302 Mon Sep 17 00:00:00 2001
From: Gregory Trolliet <gregory@trolliet.info>
Date: Thu, 26 Nov 2020 17:10:20 +0100
Subject: [PATCH 3/4] Set the max try for code generation as a constant

---
 db.php | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/db.php b/db.php
index 6063d7d..8868b4f 100644
--- a/db.php
+++ b/db.php
@@ -14,6 +14,7 @@ class DataBase
 	const DEFAULT_SIZE = 3;
 	const MAX_SIZE = 9;
 	const CLOUD_CODE_LENGTH = 6;
+	const CLOUD_CODE_MAXTRY = 10;
 
 	private $db;
 	private $cloud;
@@ -217,10 +218,10 @@ class DataBase
 			$duration = self::DEFAULT_DURATION;
 		}
 
-		for ($i = 0; $i < 10; $i++) {
+		for ($i = 0; $i < self::CLOUD_CODE_MAXTRY; $i++) {
 			$code = bin2hex(random_bytes(self::CLOUD_CODE_LENGTH));
+			$code = '2ae606bfb6d2';
 			if (!$this->loadCloudByCode($code)) {
-				echo 'LOAD CLOUD';
 				break;
 			}
 		}

From 5a0037f11ac56285bd2aebce481fd0491e998a54 Mon Sep 17 00:00:00 2001
From: Gregory Trolliet <gregory@trolliet.info>
Date: Thu, 26 Nov 2020 17:23:13 +0100
Subject: [PATCH 4/4] Add librairies in readme file

---
 README.md | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/README.md b/README.md
index a76c7db..9daf270 100644
--- a/README.md
+++ b/README.md
@@ -5,3 +5,24 @@ There is nothing stored on the server except the words.
 ## Database
 Database config option is set in the dbconfig.php file,
 example is in dbconfig_empty.php.
+This software is developped with PostgreSQL,
+I don't know if it is working with others DB management systems.
+
+## Libraries
+
+### php-i18n
+
+The internationalization is made possible by
+[Philipp Schröer (php-i18n project)](https://github.com/Philipp15b/php-i18n).
+For now it's only in french but the traduction should be easy.
+
+### Wordcloud2
+
+The display of the words cloud is done by the Wordcloud2 javascript
+code, made by
+[Timothy Guan-tin Chien](http://timdream.org/wordcloud2.js/).
+
+### Soundex_fr
+
+The words comparison is made with PHP tools and the work of
+Florent Bruneau on [the french translation of soundex](http://blog.mymind.fr/blog/2007/03/15/soundex-francais/).