home » zplus/freepost.git
Author zPlus <-> 2016-03-23 20:30:32
Committer zPlus <-> 2016-03-23 20:30:32
Commit 898d592 (patch)
Tree 01a17b8
Parent(s)

post, reply: Prevent empty comments


commits diff: 6f0f083..898d592
5 files changed, 32 insertions, 9 deletionsdownload


Diffstat
-rw-r--r-- post.php 19
-rw-r--r-- reply.php 16
-rw-r--r-- submit.php 2
-rw-r--r-- template/post.twig 2
-rw-r--r-- template/reply.twig 2

Diff options
View
Side
Whitespace
Context lines
Inter-hunk lines
+14/-5 M   post.php
index 5be3509..da262c9
old size: 2K - new size: 2K
@@ -12,7 +12,7 @@ $db->connect ();
12 12 if ($_SERVER['REQUEST_METHOD'] === 'POST')
13 13 {
14 14 // Must be logged in
15 - if (!Session::is_valid())
15 + if (!Session::is_valid ())
16 16 {
17 17 header ('Location: ./');
18 18 exit ();
@@ -28,12 +28,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
28 28 // Clear input data
29 29 $comment = trim ($_POST['new_comment']);
30 30
31 - if (strlen($comment) == 0)
31 + // Empty text... do nothing
32 + if (strlen ($comment) == 0)
32 33 {
33 - header ('Location: ./');
34 - exit ();
34 + // Retrieve the post
35 + $post = $db->get_post ($_GET['hash_id']);
36 +
37 + if (is_null ($post) || empty ($post))
38 + exit ();
39 +
40 + header ('Location: ./' . $post['hashId']);
41 + exit();
35 42 }
36 43
44 + // Everything seems OK, add the new comment
37 45 $post_hash_id = $_GET['hash_id'];
38 46
39 47 $db->new_comment ($comment, $post_hash_id, Session::get_userid());
@@ -49,7 +57,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
49 57 // Retrieve the post
50 58 $post = $db->get_post ($_GET['hash_id']);
51 59
52 - if (empty ($post))
60 + // Wrong hash_id
61 + if (is_null ($post) || empty ($post))
53 62 {
54 63 echo '404';
55 64 exit ();

+15/-1 M   reply.php
index ed531f9..de13857
old size: 1K - new size: 2K
@@ -32,7 +32,21 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
32 32
33 33 $parent_comment = $db->get_comment ($_POST['parent_comment']);
34 34
35 - $hash_id = $db->new_reply ($_POST['text'], $parent_comment['hashId'], Session::get_userid ());
35 + $text = trim ($_POST['text']);
36 +
37 + // Empty comment. Redirect to parent comment
38 + if (strlen ($text) == 0)
39 + {
40 + header ('Location: ./post/' . $parent_comment['postHashId'] . '#comment-' . $parent_comment['hashId']);
41 + exit ();
42 + }
43 +
44 + // We have a text, add the reply and redirect to the new reply
45 +
46 + $hash_id = $db->new_reply (
47 + $text,
48 + $parent_comment['hashId'],
49 + Session::get_userid ());
36 50
37 51 // Can't post?! What happened?!
38 52 if (is_null ($hash_id))

+1/-1 M   submit.php
index 6f905c0..45fba67
old size: 925B - new size: 926B
@@ -5,7 +5,7 @@ require_once 'database.php';
5 5 require_once 'twig.php';
6 6
7 7 // Must be logged in
8 - if (!Session::is_valid())
8 + if (!Session::is_valid ())
9 9 {
10 10 header ('Location: ./login');
11 11 exit ();

+1/-1 M   template/post.twig
index a917f06..71a62a3
old size: 2K - new size: 2K
@@ -40,7 +40,7 @@
40 40
41 41 {% if user %}
42 42 <form action="" method="post" class="new_comment">
43 - <textarea name="new_comment" class="form-control" placeholder="Write a comment"></textarea>
43 + <textarea name="new_comment" required="required" class="form-control" placeholder="Write a comment"></textarea>
44 44 <input type="submit" value="Add comment" class="btn btn-primary" />
45 45 </form>
46 46 {% endif %}

+1/-1 M   template/reply.twig
index 1c1c846..ebc48ee
old size: 859B - new size: 879B
@@ -16,7 +16,7 @@
16 16 <input type="hidden" name="parent_comment" value="{{ comment.hashId }}" />
17 17
18 18 <div style="margin: 2em 0;">
19 - <textarea name="text" rows=10 class="form-control"></textarea>
19 + <textarea name="text" required="required" rows=10 class="form-control"></textarea>
20 20 </div>
21 21
22 22 <div>