Browse code

use sqlite

devnewton authored on 28/12/2017 13:01:42
Showing 7 changed files
... ...
@@ -1,9 +1,9 @@
1 1
 # taab
2 2
 
3
-taab is a KISS tribune powered by vue.js, pegjs, blazecss and php (sorry).
3
+taab is a KISS tribune powered by vue.js, pegjs, blazecss, sqlite and php (sorry).
4 4
 
5 5
 ## features
6 6
 
7
-- simple TSV file backend.
7
+- TSV backend
8 8
 - lastId
9 9
 - X-Post-Id
10 10
\ No newline at end of file
... ...
@@ -2,17 +2,31 @@
2 2
 
3 3
 include_once "config.php";
4 4
 
5
-function taab_echo_backend($posts, $lastId, $newPostId = NULL) {
5
+function taab_echo_backend($lastId, $newPostId = NULL) {
6
+    global $pdo;
7
+    $posts = $pdo->prepare(
8
+                    "SELECT id, strftime('%Y%m%d%H%M%S', time) as time, login, info, message
9
+         FROM posts
10
+         WHERE id > :lastId
11
+         ORDER BY id DESC
12
+         LIMIT :maxPosts");
13
+    $posts->execute(array("lastId" => $lastId, "maxPosts" => TAAB_BACKEND_MAX_POSTS));
6 14
     header("Content-Type: text/tab-separated-values");
7 15
     if ($newPostId !== NULL) {
8 16
         header('X-Post-Id: ' . $newPostId);
9 17
     }
10 18
     $outstream = fopen("php://output", 'w');
11
-    foreach ($posts as $post) {
12
-        if ($post[0] > $lastId) {
13
-            fputs($outstream, implode("\t", $post));
14
-            fputs($outstream, "\n");
15
-        }
19
+    while($post = $posts->fetch(PDO::FETCH_OBJ)) {
20
+        fputs($outstream, $post->id);
21
+        fputs($outstream, "\t");
22
+        fputs($outstream, $post->time);
23
+        fputs($outstream, "\t");
24
+        fputs($outstream, $post->info);
25
+        fputs($outstream, "\t");
26
+        fputs($outstream, $post->login);
27
+        fputs($outstream, "\t");
28
+        fputs($outstream, $post->message);
29
+        fputs($outstream, "\n");
16 30
     }
17 31
     fclose($outstream);
18 32
 }
... ...
@@ -1,6 +1,15 @@
1 1
 <?php
2 2
 
3
-define("TAAB_BACKEND", "./data/backend.tsv");
3
+$pdo = new PDO('sqlite:./data/backend.db');
4
+
5
+$pdo->query("CREATE TABLE IF NOT EXISTS posts ( 
6
+	id    INTEGER PRIMARY KEY AUTOINCREMENT,
7
+	time  TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
8
+	info  TEXT DEFAULT '' NOT NULL,
9
+        login TEXT DEFAULT '' NOT NULL,
10
+        message TEXT DEFAULT '' NOT NULL   
11
+);");
12
+
4 13
 define("TAAB_BACKEND_MAX_POSTS", 200);
5 14
 define("TAAB_MAX_POST_LENGTH", 512);
6 15
 define("TAAB_MAX_LOGIN_LENGTH", 32);
7 16
new file mode 100644
8 17
Binary files /dev/null and b/data/backend.db differ
9 18
deleted file mode 100644
... ...
@@ -4,12 +4,5 @@ include_once 'common.php';
4 4
 
5 5
 $lastId = filter_input(INPUT_GET, 'lastId', FILTER_VALIDATE_INT, array('options' => array('default' => 0)));
6 6
 
7
-$file = fopen(TAAB_BACKEND, "r");
8
-$newPosts = array();
9
-while (($post = fgetcsv($file, TAAB_MAX_BACKEND_LINE_LENGTH, "\t")) !== FALSE) {
10
-    $newPosts[] = $post;
11
-}
12
-fclose($file);
13
-
14
-taab_echo_backend($newPosts, $lastId);
7
+taab_echo_backend($lastId);
15 8
 ?>
16 9
\ No newline at end of file
... ...
@@ -7,11 +7,11 @@ $message = mb_substr(filter_input(INPUT_POST, 'message', FILTER_UNSAFE_RAW, FILT
7 7
 $login = trim(mb_substr(filter_input(INPUT_POST, 'login', FILTER_SANITIZE_EMAIL), 0, TAAB_MAX_LOGIN_LENGTH));
8 8
 $info = trim(mb_substr(filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_EMAIL), 0, TAAB_MAX_INFO_LENGTH));
9 9
 
10
-if(!mb_detect_encoding($login, 'UTF-8', true)) {
10
+if (!mb_detect_encoding($login, 'UTF-8', true)) {
11 11
     $login = "relou";
12 12
 }
13 13
 
14
-if(!mb_detect_encoding($info, 'UTF-8', true)) {
14
+if (!mb_detect_encoding($info, 'UTF-8', true)) {
15 15
     $info = "relou";
16 16
 }
17 17
 
... ...
@@ -19,28 +19,12 @@ if (mb_strlen($login) === 0 && mb_strlen($info) === 0) {
19 19
     $info = "coward";
20 20
 }
21 21
 
22
-$file = fopen(TAAB_BACKEND, "c+");
23
-flock($file, LOCK_EX);
24
-$newPostId = 0;
25
-$newPosts = array();
26
-while (($post = fgetcsv($file, TAAB_MAX_BACKEND_LINE_LENGTH, "\t")) !== FALSE) {
27
-    $newPosts[] = $post;
28
-    $newPostId = max($newPostId, $post[0]);
29
-}
30 22
 if (mb_strlen(trim($message)) > 0 && mb_detect_encoding($message, 'UTF-8', true)) {
31
-    ++$newPostId;
32
-    $dateTime = date_create("now", timezone_open("Europe/Paris"));
33
-    $time = date_format($dateTime, 'YmdHis');
34
-    array_unshift($newPosts, array($newPostId, $time, $info, $login, $message));
35
-}
36
-array_splice($newPosts, TAAB_BACKEND_MAX_POSTS);
37
-ftruncate($file, 0);
38
-fseek($file, 0);
39
-foreach ($newPosts as $post) {
40
-    fputs($file, implode("\t", $post));
41
-    fputs($file, "\n");
23
+    $posts = $pdo->prepare(
24
+                    "INSERT INTO posts
25
+            (login, info, message)
26
+            VALUES (:login, :info, :message)
27
+         ")->execute(array("login" => $login, "info" => $info, "message" => $message));
42 28
 }
43
-fclose($file);
44
-
45
-taab_echo_backend($newPosts, $lastId, $newPostId);
29
+taab_echo_backend($lastId, $pdo->lastInsertId());
46 30
 ?>
47 31
\ No newline at end of file