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
|
<?php
function display_navbar($dbc) {
}
function display_categories($dbc, $sql_result) {
$sql = "SELECT thread_id, thread_subject, thread_date, user_id, user_name FROM threads JOIN users ON thread_author = user_id WHERE thread_cat = ? ORDER BY thread_id DESC LIMIT 1";
$stmt = mysqli_stmt_init($dbc);
if (!mysqli_stmt_prepare($stmt, $sql)) {
die('Could not create thread due to internal error: ' . mysqli_error($dbc));
}
while ($row = mysqli_fetch_assoc($sql_result)) {
mysqli_stmt_bind_param($stmt, "i", $row['cat_id']);
mysqli_stmt_execute($stmt);
$thread_res = mysqli_stmt_get_result($stmt);
$thread = mysqli_fetch_assoc($thread_res);
echo '<tr><td class="left">';
echo '<h4><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h4>';
echo $row['cat_description'];
if ($thread) {
echo '</td><td class="right">' . $thread['thread_subject'] . '<br>';
echo '<small>by <b><a href="user.php?id=' . $thread['user_id'] . '">' . $thread['user_name'] . '</a></b></small></td></tr>';
} else {
$no_threads_msg = 'There are no threads in this category yet.';
echo '</td><td class="right"><small>'. $no_threads_msg .'</small></td>';
}
}
mysqli_stmt_close($stmt);
mysqli_free_result($thread_res);
}
function display_threads($dbc, $sql_result, $show_category = false) {
$sql = "SELECT post_id, post_date, user_id, user_name FROM posts JOIN users ON post_author = user_id WHERE post_thread = ? ORDER BY post_id DESC LIMIT 1";
$stmt = mysqli_stmt_init($dbc);
if (!mysqli_stmt_prepare($stmt, $sql)) {
die('Could not create thread due to internal error: ' . mysqli_error($dbc));
}
while ($row = mysqli_fetch_assoc($sql_result)) {
mysqli_stmt_bind_param($stmt, "i", $row['thread_id']);
mysqli_stmt_execute($stmt);
$thread_res = mysqli_stmt_get_result($stmt);
$thread = mysqli_fetch_assoc($thread_res);
echo '<tr><td class="left">';
echo '<h4><a href="thread.php?id=' . $row['thread_id'] . '">' . $row['thread_subject'] . '</a></h4>';
echo '<small>by <b><a href="user.php?id=' . $row['user_id'] . '">' . $row['user_name'] . '</a></b> ';
if ($show_category) {
echo 'in <b><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></b> ';
}
echo 'on ' . date('M d, Y', strtotime($row['thread_date'])) . '</small>';
echo '</td><td class="right">by <b><a href="user.php?id=' . $thread['user_id'] . '">' . $thread['user_name'] . '</a></b><br>';
echo '<small>' . date('m/d/Y g:ia', strtotime($thread['post_date'])) . '</small></td></tr>';
}
mysqli_stmt_close($stmt);
}
function add_quote($dbc, $thread_id, $matches) {
foreach ($matches as $match) {
$id = (int) filter_var($match, FILTER_SANITIZE_NUMBER_INT) - 1;
$sql = "SELECT post_content, post_author, user_name FROM posts LEFT JOIN users ON post_author = user_id WHERE post_thread = " . $thread_id . " LIMIT 1 OFFSET " . $id;
$result = mysqli_query($dbc, $sql);
if (!$result) {
return '<blockquote></blockquote>';
}
$reply = mysqli_fetch_assoc($result);
if (empty($reply)) {
return '<blockquote>Invalid quote!</blockquote>';
}
$id = $id + 1;
return '<blockquote><a href="#' . $id .'">Quote from ' . $reply['user_name'] . '</a><br>' . $reply['post_content'] . '</blockquote>';
}
}
function display_posts($dbc, $thread_id, $sql_result) {
while ($row = mysqli_fetch_assoc($sql_result)) {
echo '#' . $row['post_id'] . ' Posted by <a href="user.php?id='. $row['user_id'] .'">' . $row['user_name'] . '</a> on ' . date('m/d/Y g:ia', strtotime($row['post_date'])) . '<br>';
$post_content = $row['post_content'];
$post_content = preg_replace_callback('/>#\d+/', function($matches) use($thread_id, $dbc) {
return add_quote($dbc, $thread_id, $matches);
}, $post_content);
// Replace YouTube URLs with embedded YouTube videos.
$post_content = preg_replace(
"/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
'<br><iframe class="youtube-embed" src="//www.youtube.com/embed/$2" allowfullscreen></iframe>', $post_content);
// Replace Image URLs with embedded images.
$post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+(?:\.jpg|\.png|\.gif))(?![^<]*?(?:</\w+>|/?>))@i', '<img class="image-embed" src="http$2://$3" alt="http$2://$3" />', $post_content);
// Replace other URLs with links.
$post_content = preg_replace('@\b(http(s)?://)([^\s]*?(?:\.[a-z\d?=/_-]+)+)(?![^<]*?(?:</\w+>|/?>))@i', '<a href="http$2://$3">$0</a>', $post_content);
echo $post_content;
}
}
|