-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost.php
193 lines (149 loc) · 5.58 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
session_start();
include_once 'app/minifier.php';
require_once 'app/helpers.php';
$page_title = 'Post Page';
header('HTTP/1.1 201 Ok');
$pid = null;
if(isset($_GET['p'])){
$pid = $_GET['p'];
} else {
header('location ./');
}
$pid = filter_input(INPUT_GET, 'p', FILTER_SANITIZE_STRING, FILTER_VALIDATE_URL);
$error['article'] = '';
$article = $_POST['article'] ?? '';
$article = filter_input(INPUT_POST, 'article', FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
$article = trim($article);
$link = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD, MYSQL_DB);
$sql = "SELECT * FROM posts
WHERE id = '$pid'";
$result = mysqli_query($link,$sql);
if(isset($_POST['submit'])){
$uid = $_SESSION['user_id'];
$uname = $_SESSION['user_name'];
$form_valid = true;
if(strlen($article) < 10){
$error['article'] = 'A valid command need to have more than 10 letters!';
$form_valid = false;
header('HTTP/1.1 400 Not Found');
}
if(!$uid){
$error['article'] = "Pls Login before posting things!";
$form_valid = false;
header('HTTP/1.1 400 Not Found');
$_SESSION['error_alert'] = "You cannot post commands without logging in!";
header('location: signin.php');
}
if($form_valid){
$article = mysqli_real_escape_string($link,$article);
$sql_insert = "INSERT INTO commands (id, user_id, post_id, article, user_name) VALUES (NULL, $uid, $pid, '$article', '$uname')";
$result_insert = mysqli_query($link,$sql_insert);
$_SESSION['message'] = "comment has been added! <a href='post.php?p=$pid'>Go Back</a>";
echo "<script>window.location.href='blog.php';</script>";
}
}
$sql_commands = "SELECT * FROM commands
WHERE post_id = '$pid'
ORDER BY id DESC";
$sql_commands_res = mysqli_query($link,$sql_commands);
?>
<?php include 'tpl/header.php'; ?>
<?php while($post = mysqli_fetch_assoc($result)): ?>
<main class="min-h-900">
<div class="container">
<div class="row">
<div class="col-12 mt-4 text-center">
<h1 class="display-4 mb-5"><?=$post['title'];?></h1>
</div>
</div>
<div class="row">
<div class="col-lg-8 mt-4">
</div>
<main class='container-fluid'>
<div>
<h3 class="text-white">
Article
</h3>
<div>
<div class="alert alert-warning" role="alert">
<h5 class="alert-heading">
Posted At: <?=$post['date']?>
</h5>
<p class="text-dark display-4">
<?= $post['article'] ?>
<br>
<a href="vote.php?p=<?=$pid?>" class="btn btn-outline-dark" role="button" aria-pressed="true"><i class="far fa-thumbs-up"></i> Upvote</a>
</p>
</div>
</div>
</div>
</div>
<br>
<div style="padding-top: 100px" class="container mt-4">
<hr style="border-top: 1px solid white; width: auto">
<?php if($_SESSION['user_id']): ?>
<br><br>
<form id="add-post-form" action="" method="POST" novalidate="novalidate" autocomplete="off">
<div class="form-group">
<label for="article"><i class="far fa-comment-alt"></i> Reply</label>
<input placeholder="Reply to post number <?= $pid; ?> | Minimum of 10 letters!" type="text" name="article" class="form-control" id="article">
<span class="text-danger"><?= $error['article']; ?></span>
</div>
<input type="submit" value="Post" name="submit" id="submit" class="btn btn-primary">
</form>
<br><br>
<?php else: ?>
<br><br>
<form id="add-post-form" action="" method="POST" novalidate="novalidate" autocomplete="off">
<div class="form-group">
<label for="article"><i class="far fa-comment-alt"></i> Reply</label>
<input disabled type="text" name="article" class="form-control" id="article" value="<?=old("article")?>">
<span class="text-danger"><?= $error['article']; ?></span>
<span class="text-danger">Request: Login before posting something</span>
</div>
<input type="submit" value="Post" name="submit" id="submit" class="btn btn-primary">
</form>
<br><br>
<?php endif; ?>
<?php while($post_res = mysqli_fetch_assoc($sql_commands_res)): ?>
<?php
$user_id = $post_res['user_id'];
$sql_pfp = "SELECT * FROM users_profile
WHERE user_id = '$user_id'";
$result_pfp = mysqli_query($link, $sql_pfp);
$res = mysqli_fetch_assoc($result_pfp);
?>
<div class="container">
<div class="row">
<div class="media comment-box">
<div class="media-left">
<a href="profile.php?q=<?=$post_res['user_id']?>">
<img class="img-responsive user-photo" src="images/<?=$res['image_name']?>">
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><a href="profile.php?q=<?=$post_res['user_id']?>"><?= $post_res['user_name']; ?></a></h4>
<p style="width: 1000px"><?= htmlentities($post_res['article']); ?>
<br>
At: <?= $post_res['date'] ?>
</p>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
</div>
</main>
<?php endwhile; ?>
<?php if(mysqli_num_rows($result) == 0 || !$result): ?>
<?php header('HTTP/1.1 404 Not Found'); ?>
<center>
<h1 class="text-white">Post with id '<code class="text-white"><?=$pid?></code>' didn't found</h1>
</center>
<?php endif; ?>
<?php include 'tpl/footer.php'; ?>
<?php include 'app/status.php'; ?>