diff --git a/create.php b/create.php
index bca5c73..61682c3 100644
--- a/create.php
+++ b/create.php
@@ -2,6 +2,29 @@
include('templates/header.php');
include('db.php');
+
+if (empty($_POST)) {
+?>
+
+
+
+isInit()) {
echo 'Error db init';
@@ -12,9 +35,25 @@ $db->cleanCloud();
$length = 6;
$token = bin2hex(random_bytes($length));
-
-while (!$db->createCloud($token)) {
+if (isset($_POST['fsize']) && is_numeric($_POST['fsize'])) {
+ $size = $_POST['fsize'];
+} else {
+ $size = 3;
+}
+if (isset($_POST['ftext'])) {
+ $text = $_POST['ftext'];
+} else {
+ $text = '';
+}
+if (isset($_POST['fduration']) && in_array($_POST['fduration'], DataBase::OPTIONS_DURATION)) {
+ $duration = $_POST['fduration'];
+} else {
+ $duration = DataBase::DEFAULT_DURATION;
+}
+$cpt = 0;
+while (!$db->createCloud($token, $text, $size, $duration) && $cpt < 10) {
$token = bin2hex(random_bytes($length));
+ $cpt++;
}
?>
@@ -33,4 +72,5 @@ while (!$db->createCloud($token)) {
'+1 day',
+ 'week' => '+7 day',
+ '2week' => '+14 day',
+ 'month' => '+1 month'
+ );
+
private $db;
public function __construct()
@@ -28,21 +36,28 @@ class DataBase
public function buildTables()
{
- $stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS clouds('
- . 'id_cloud INTEGER PRIMARY KEY, '
- . 'code TEXT NOT NULL UNIQUE, '
- . 'create_t TIMESTAMP DEFAULT CURRENT_TIMESTAMP);'
- );
+ $stmt = $this->db->prepare("
+ CREATE TABLE IF NOT EXISTS clouds(
+ id_cloud INTEGER PRIMARY KEY,
+ code TEXT NOT NULL UNIQUE,
+ size INTEGER NOT NULL DEFAULT 3,
+ text TEXT,
+ delete_t TIMESTAMP DEFAULT
+ (datetime('now', '" . self::DEFAULT_DURATION . "'))
+ );
+ ");
$stmt->execute();
- $stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS words('
- . 'id_word INTEGER PRIMARY KEY, '
- . 'word TEXT NOT NULL, '
- . 'count INT DEFAULT 1, '
- . 'cloud_id INT NOT NULL, '
- . 'FOREIGN KEY (cloud_id) '
- . 'REFERENCES clouds(id_cloud) ON UPDATE CASCADE '
- . 'ON DELETE CASCADE);'
- );
+ $stmt = $this->db->prepare("
+ CREATE TABLE IF NOT EXISTS words(
+ id_word INTEGER PRIMARY KEY,
+ word TEXT NOT NULL,
+ count INT DEFAULT 1,
+ cloud_id INT NOT NULL,
+ FOREIGN KEY (cloud_id)
+ REFERENCES clouds(id_cloud) ON UPDATE CASCADE
+ ON DELETE CASCADE
+ );
+ ");
$stmt->execute();
}
@@ -51,7 +66,11 @@ class DataBase
if (empty($word)) {
return false;
}
- $stmt = $this->db->prepare('SELECT * FROM clouds WHERE code = :id;');
+ $stmt = $this->db->prepare("
+ SELECT *
+ FROM clouds
+ WHERE code = :id;
+ ");
$stmt->bindValue(':id', $cloud, PDO::PARAM_STR);
$stmt->execute();
$cloudId = null;
@@ -60,23 +79,29 @@ class DataBase
} else {
return false;
}
- $stmt = $this->db->prepare('SELECT * FROM words '
- . 'WHERE cloud_id = :cid AND word = :w;'
- );
+ $stmt = $this->db->prepare("
+ SELECT *
+ FROM words
+ WHERE cloud_id = :cid
+ AND word = :w;
+ ");
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT);
$stmt->bindValue(':w', $word, PDO::PARAM_STR);
$stmt->execute();
if ($data = $stmt->fetch()) {
$wordId = $data['id_word'];
- $stmt = $this->db->prepare(
- 'UPDATE words SET count = count + 1 WHERE id_word = :id;'
- );
+ $stmt = $this->db->prepare("
+ UPDATE words
+ SET count = count + 1
+ WHERE id_word = :id;
+ ");
$stmt->bindValue(':id', $wordId, PDO::PARAM_INT);
$stmt->execute();
} else {
- $stmt = $this->db->prepare(
- 'INSERT INTO words(word, cloud_id) VALUES(:w, :cid);'
- );
+ $stmt = $this->db->prepare("
+ INSERT INTO words(word, cloud_id)
+ VALUES (:w, :cid);
+ ");
$stmt->bindValue(':w', $word, PDO::PARAM_STR);
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT);
$stmt->execute();
@@ -84,10 +109,13 @@ class DataBase
}
public function getWords(string $id)
{
- $stmt = $this->db->prepare('SELECT * FROM words '
- . 'JOIN clouds ON words.cloud_id = clouds.id_cloud '
- . 'WHERE code = :id;'
- );
+ $stmt = $this->db->prepare("
+ SELECT *
+ FROM words
+ JOIN clouds
+ ON words.cloud_id = clouds.id_cloud
+ WHERE code = :id;
+ ");
$stmt->bindValue(':id', $id);
$stmt->execute();
$words = [];
@@ -105,8 +133,6 @@ class DataBase
public function getWordsPercentage(string $id)
{
$words = $this->getWords($id);
- //echo 'test';
- //var_dump($words);
$total = 0;
foreach ($words as $word) {
$total += $word[1];
@@ -121,8 +147,6 @@ class DataBase
public function getWordsMax(string $id)
{
$words = $this->getWords($id);
- //echo 'test';
- //var_dump($words);
$max = 0;
foreach ($words as $word) {
$max = max($max, $word[1]);
@@ -134,10 +158,29 @@ class DataBase
return $words;
}
- public function createCloud(string $ref)
+ public function createCloud(
+ string $ref,
+ string $text = '',
+ int $size = null,
+ string $duration = null)
{
- $stmt = $this->db->prepare('INSERT INTO clouds(code) VALUES(:code);');
+ if (!isset($size)) {
+ $size = 3;
+ }
+ if (!isset($duration)) {
+ $duration = self::DEFAULT_DURATION;
+ } elseif (!in_array($duration, self::OPTIONS_DURATION)) {
+ $duration = self::DEFAULT_DURATION;
+ }
+ $duration = date('Y-m-d H:i:s', strtotime($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(':duration', $duration, PDO::PARAM_STR);
try {
$stmt->execute();
} catch (PDOEXception $e) {
@@ -146,18 +189,54 @@ class DataBase
return true;
}
+ public function getCloudSize(string $ref)
+ {
+ $stmt = $this->db->prepare("
+ SELECT *
+ FROM clouds
+ WHERE code= :code;
+ ");
+ $stmt->bindValue(':code', $ref, PDO::PARAM_STR);
+ $stmt->execute();
+ if ($data = $stmt->fetch()) {
+ return $data['size'];
+ }
+ return null;
+ }
+
+ public function getCloudText(string $ref)
+ {
+ $stmt = $this->db->prepare("
+ SELECT *
+ FROM clouds
+ WHERE code= :code;
+ ");
+ $stmt->bindValue(':code', $ref, PDO::PARAM_STR);
+ $stmt->execute();
+ if ($data = $stmt->fetch()) {
+ return $data['text'];
+ }
+ return null;
+ }
+
public function cleanCloud()
{
- $stmt = $this->db->prepare('DELETE FROM clouds '
- . "WHERE create_t < (SELECT datetime('now', '-1 week'));"
- );
+ $stmt = $this->db->prepare("
+ DELETE
+ FROM clouds
+ WHERE delete_t < CURRENT_TIMESTAMP;
+ ");
$stmt->execute();
return true;
}
public function isCloud(string $id)
{
- $stmt = $this->db->prepare('SELECT * FROM clouds WHERE code = :id;');
+ $stmt = $this->db->prepare("
+ SELECT *
+ FROM clouds
+ WHERE code = :id;
+ ");
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
$stmt->execute();
if ($stmt->fetch()) {
diff --git a/index.php b/index.php
index d0bd21b..199426e 100644
--- a/index.php
+++ b/index.php
@@ -15,17 +15,18 @@ if (isset($_GET['id'])) {
echo 'NO CLOUD';
return;
}
- $nbWords = 3;
+ $nbWords = $db->getCloudSize($id);
+ echo sprintf('%s
', $db->getCloudText($id));
?>
-