-
Notifications
You must be signed in to change notification settings - Fork 1
/
post.php
121 lines (103 loc) · 3.93 KB
/
post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL & E_NOTICE & E_WARNING);
require_once 'helpers.php';
require_once 'functions.php';
require_once 'data.php';
require_once 'session.php';
$post_id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$validation_errors = [];
$tags = [];
$navigation_link = 'post.php';
$comments_offset = ' LIMIT 3;';
$tab = filter_input(INPUT_GET, 'tab');
if ($tab === 'comments_all') {
$comments_offset = ';';
}
$sql = 'SELECT p.id, title, text, quote_auth, img, video, link, views, p.dt_add, user_id, type,' .
' (SELECT COUNT(post_id)' .
' FROM likes' .
' WHERE likes.post_id = p.id)' .
' AS likes,' .
' (SELECT COUNT(content) FROM comments' .
' WHERE post_id = p.id)' .
' AS comment_sum,' .
' (SELECT COUNT(original_id) FROM posts' .
' WHERE original_id = p.id)' .
' AS reposts_sum' .
' FROM posts p' .
' JOIN users u ON p.user_id = u.id' .
' JOIN content_type ct' .
' ON p.content_type_id = ct.id' .
' WHERE p.id = ?';
$stmt = mysqli_prepare($connection, $sql);
mysqli_stmt_bind_param($stmt, 'i', $post_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($result) {
$post = mysqli_fetch_assoc($result);
$user_info = get_user_info($connection, $post['user_id']);
$this_user = get_user($connection, $post['user_id']);
$is_subscribe = check_subscription($connection, $this_user['id'], $user['user_id']);
$sql = 'UPDATE posts SET views = views + 1 WHERE id = ?;';
$stmt = mysqli_prepare($connection, $sql);
mysqli_stmt_bind_param($stmt, 'i', $post_id);
mysqli_stmt_execute($stmt);
$tags = get_tags($connection, $post_id);
$sql = 'SELECT content, user_id, c.dt_add, login, avatar' .
' FROM comments c' .
' JOIN users u ON c.user_id = u.id' .
' WHERE post_id = ?' . $comments_offset;
$stmt = mysqli_prepare($connection, $sql);
mysqli_stmt_bind_param($stmt, 'i', $post_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$comments = mysqli_fetch_all($result, MYSQLI_ASSOC);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$required = ['comment', 'post_id'];
$rules = [
'comment' => function ($value) {
return validate_comment($value, COMMENT_MIN_LENGTH);
},
'post_id' => function ($value) use ($connection) {
return validate_post_id($connection, $value);
}
];
$comment = filter_input_array(INPUT_POST, [
'comment' => FILTER_DEFAULT,
'post_id' => FILTER_VALIDATE_INT
], true);
$validation_errors = full_form_validation($comment, $rules, $required);
if (!$validation_errors) {
$sql = 'INSERT INTO comments (content, user_id, post_id)' .
' VALUES (?, ?, ?)';
$stmt = mysqli_prepare($connection, $sql);
mysqli_stmt_bind_param($stmt, 'sii', $comment['comment'], $user['user_id'], $comment['post_id']);
mysqli_stmt_execute($stmt);
header('Location: users_profile.php?id=' . $this_user['id']);
exit;
}
}
$page_content = include_template('post_templates/post-window.php', [
'post' => $post,
'tags' => $tags,
'tab' => $tab,
'comments' => $comments,
'user_info' => $user_info,
'this_user' => $this_user,
'is_subscribe' => $is_subscribe,
'user' => $user,
'validation_errors' => $validation_errors
]);
} else {
$error = mysqli_error($connection);
$page_content = include_template('error.php', ['error' => $error]);
}
$layout_content = include_template('layout.php', [
'content' => $page_content,
'title' => 'readme: блог, каким он должен быть',
'user' => $user,
'navigation_link' => $navigation_link,
'message_notification' => $message_notification
]);
print($layout_content);