-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsublewit.php
More file actions
212 lines (169 loc) · 10 KB
/
sublewit.php
File metadata and controls
212 lines (169 loc) · 10 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
require "realconfig.php";
session_start();
$dbh = new PDO(DB_DSN, DB_USER, DB_PASSWORD);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sublewit</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" type="image/x-icon" href="images/reddit-logo.ico">
</head>
<?php
// valid id for sublewit or not
try {
$sublewitid = $_GET['id'];
$sth = $dbh->prepare("SELECT p.*, u.username FROM `bi_posts` p
JOIN `bi_users` u ON p.author_id = u.id
WHERE p.community_id = :id AND p.reply_id IS NULL");
$sth->bindValue(':id', $sublewitid);
$sth->execute();
$sublewits = $sth->fetchAll();
$sth2 = $dbh->prepare("SELECT * FROM bi_communities WHERE id = :commid");
$sth2->bindValue(':commid', $sublewitid);
$sth2->execute();
$community = $sth2->fetch();
$communityname = $community['name'];
$communityinfo = $community["description"];
} catch (PDOException $e) {
echo "<p>Error: {$e->getMessage()}</p>";
}
?>
<body>
<div class="container">
<?php
require_once "header.php";
?>
<div class="sidebar">
<h3 class="center">Top Sublewits</h3>
<ol>
<?php
$sth = $dbh->prepare("SELECT c.id, c.name, COUNT(p.id) as pcount FROM `bi_communities` c
JOIN bi_posts p ON c.id = p.community_id
WHERE p.reply_id IS NULL
GROUP BY p.community_id
ORDER BY pcount DESC LIMIT 5;");
$sth->execute();
$toptensublewits = $sth->fetchAll();
foreach ($toptensublewits as $toptensublewit) {
$toptensublewitid = $toptensublewit['id'];
echo "<li><a href = \" sublewit.php?id={$toptensublewitid}\">{$toptensublewit['name']}</a></li>";
}
?>
</ol>
</div>
<?php
//display the sublewit info
echo "<h1 class='center'><u>" . htmlspecialchars($communityname) . "</u></h1>";
echo "<h2 class='center'>" . htmlspecialchars($communityinfo) . "</h2>";
if ($community['admin_change'] != NULL) {
echo "<h4 class='center'>This sublewit has been modified by an admin</h4>";
}
//author of sublewit
if ($community['user_id'] != 0) {
$userTable = $dbh->prepare("SELECT username FROM bi_users WHERE id = :id");
$userTable->bindValue(":id", $community['user_id']);
$userTable->execute();
$userName = $userTable->fetch();
echo "<h2 class='center'><a href='profile.php?id=" . $community['user_id'] . "'>Sublewit created by <i>" . $userName['username'] . "</i></a></h2>";
}
?>
//display
<div class="posts-container">
<?php
try {
if (!empty($sublewits)) {
foreach ($sublewits as $post) {
$authorName = $dbh->prepare("SELECT * FROM `bi_users` WHERE :postAuthorId = `id`;");
$authorName->bindValue(':postAuthorId', $post['author_id']);
$authorName->execute();
$author = $authorName->fetch();
$sublewItName = $dbh->prepare("SELECT * FROM `bi_communities` WHERE :postSublewit = `id`;");
$sublewItName->bindValue(':postSublewit', $post['community_id']);
$sublewItName->execute();
$sublewIt = $sublewItName->fetch();
echo "<div class='post-div'>";
echo "<h2 class='post-user'><a href='profile.php?id=" . htmlspecialchars($author['id']) . "'>" . htmlspecialchars($author['username']) . "</a></h2>";
echo "<p class='post-sublewit'><a href='sublewit.php?id=" . htmlspecialchars($sublewIt['id']) . "'><i>" . htmlspecialchars($sublewIt['name']) . "</i></a></p>";
echo "<p class='post-content'>" . htmlspecialchars($post['content']) . "<a href='post.php?id=" . $post['id'] . "'> Click to see post</a></p>";
$upvotes = 0;
$downvotes = 0;
$interactionTable = $dbh->prepare("SELECT * FROM `bi_interactions` WHERE :postId = `post_id`;");
$interactionTable->bindValue(":postId", $post['id']);
$interactionTable->execute();
$interactions = $interactionTable->fetchAll();
foreach ($interactions as $interaction) {
if ($interaction['interaction_type'] == 1) {
$upvotes += 1;
} else if ($interaction['interaction_type'] == 2) {
$downvotes += 1;
}
}
if (isset($_SESSION['user'])) {
$userInteractionTable = $dbh->prepare("SELECT * FROM `bi_interactions` WHERE :postId = `post_id` AND :user = `user_id`;");
$userInteractionTable->bindValue(":user", $_SESSION['user']);
$userInteractionTable->bindValue(":postId", $post['id']);
$userInteractionTable->execute();
$userInteraction = $userInteractionTable->fetch();
if (!empty($userInteraction)) {
if ($userInteraction['interaction_type'] == 1) {
echo "<div class='bottomspan green'><a href= 'interaction.php?post=" . htmlspecialchars($post['id']) . "&inter=1&page=sublewit&sublewit=" . $_GET['id'] . "'>
<p class='post-upvotes'>Upvotes</p>
<p class='post-upvotes-total'>" . htmlspecialchars($upvotes) . "</p></a>
</div>
<div class='bottomspan'><a href= 'interaction.php?post=" . htmlspecialchars($post['id']) . "&inter=2&page=sublewit&sublewit=" . $_GET['id'] . "'>
<p class='post-downvotes'>Downvotes</p>
<p class='post-downvotes-total'>" . htmlspecialchars($downvotes) . "</p></a>
</div>";
} else if ($userInteraction['interaction_type'] == 2) {
echo "<div class='bottomspan'><a href= 'interaction.php?post=" . htmlspecialchars($post['id']) . "&inter=1&page=sublewit&sublewit=" . $_GET['id'] . "'>
<p class='post-upvotes'>Upvotes</p>
<p class='post-upvotes-total'>" . htmlspecialchars($upvotes) . "</p></a>
</div>
<div class='bottomspan green'><a href= 'interaction.php?post=" . htmlspecialchars($post['id']) . "&inter=2&page=sublewit&sublewit=" . $_GET['id'] . "'>
<p class='post-downvotes'>Downvotes</p>
<p class='post-downvotes-total'>" . htmlspecialchars($downvotes) . "</p></a>
</div>";
}
} else {
echo "<div class='bottomspan'><a href= 'interaction.php?post=" . htmlspecialchars($post['id']) . "&inter=1&page=sublewit&sublewit=" . $_GET['id'] . "'>
<p class='post-upvotes'>Upvotes</p>
<p class='post-upvotes-total'>" . htmlspecialchars($upvotes) . "</p></a>
</div>
<div class='bottomspan'><a href= 'interaction.php?post=" . htmlspecialchars($post['id']) . "&inter=2&page=sublewit&sublewit=" . $_GET['id'] . "'>
<p class='post-downvotes'>Downvotes</p>
<p class='post-downvotes-total'>" . htmlspecialchars($downvotes) . "</p></a>
</div>";
}
} else {
echo "<div class='bottomspan'>
<p class='post-upvotes'>Upvotes</p>
<p class='post-upvotes-total'>" . htmlspecialchars($upvotes) . "</p>
</div>
<div class='bottomspan'>
<p class='post-downvotes'>Downvotes</p>
<p class='post-downvotes-total'>" . htmlspecialchars($downvotes) . "</p>
</div>";
}
$datetime = strtotime($post['creation_time']);
$formatted_date = date('m/d/Y h:i:s A', $datetime);
echo "<p><i>" . htmlspecialchars($formatted_date) . "</i></p>";
if ($post['admin_change'] != NULL) {
echo "<p><i>This post was modified by an admin</i></p>";
}
echo "</div>";
}
} else {
echo "<p class='center'>No posts found. Make One!</p>";
}
} catch (PDOException $e) {
echo "<p>Error: {$e->getMessage()}</p>";
}
?>
</div>
</div>
</body>
</html>