-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
163 lines (151 loc) · 6.43 KB
/
index.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
<?php
session_start();
require_once('conn.php');
require_once('utils.php');
$username = NULL;
$user = NULL;
if (!empty($_SESSION['username'])) {
$username = $_SESSION['username'];
$user = getUserFromUsername($username);
}
$page = 1;
if (!empty($_GET['page'])) {
$page = intval($_GET['page']);
}
$items_per_page = 3;
$offset = ($page - 1) * $items_per_page;
$stmt = $conn->prepare(
'SELECT '.
'C.id as id, C.content as content, '.
'C.created_at as created_at, U.nickname as nickname, U.username as username '.
'FROM peipei_comments as C '.
'LEFT JOIN peipei_users as U ON C.username = U.username '.
'WHERE C.is_deleted IS NULL '.
'ORDER BY C.id DESC '.
'LIMIT ? OFFSET ? '
);
$stmt->bind_param('ii', $items_per_page, $offset);
$result = $stmt->execute();
if (!$result) {
die('Error' . $conn->error);
}
$result = $stmt->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP message board</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="warning">
<strong>This is a website for practicing php. Please do not leave your normal password.</strong>
</header>
<main class="board">
<section class="form_container">
<div class="form_head">
<?php if (!$username) { ?>
<a class="member_btn" href="register.php">Register</a>
<a class="member_btn" href="login.php">Login</a>
<?php } else { ?>
<a class="member_btn" href="logout.php">Logout</a>
<span class="member_btn update_nickname">Edit nickname</span>
<?php if ($user && $user["roles"] === "ADMIN") { ?>
<a class="member_btn" href="admin_index.php">Admin area</a>
<?php } ?>
<form class="hide board_nickname-form form_body" method="POST" action="update_user.php">
<span>New nickname:</span>
<input class="form_body_content" type="text" name="nickname" /><br>
<input class="member_btn" type="submit" />
</form>
<h3>Hi there!<?php echo $user['nickname']; ?></h3>
<?php } ?>
<h1>What do you want to say...?</h1>
<?php
if (!empty($_GET['errCode'])) {
$code = $_GET['errCode'];
$msg = 'Error';
if ($code === '1') {
$msg = 'Please fill in all fields';
}
echo '<h2>Error message:' . $msg . '</h2>';
}
?>
</div>
<div class="form_body">
<form method="POST" action="handle_add_post.php">
<textarea class="form_body_content" rows="5" name="content" placeholder="Leave your message here..."></textarea>
<?php if ($username && !hasPermission($user, "create", NULL)) { ?>
<h3>You are unauthrised. Please contact the admin.</h3>
<?php } else if ($username) { ?>
<input class="form_body_btn"type="submit" value="Submit" />
<?php } else {?>
<h3>Please login to leave your message</h3>
<?php }?>
</form>
<div class="form_hr"></div>
</div>
</section>
<section class="posts_container">
<?php
while ($row = $result->fetch_assoc()) {
?>
<div class="posts">
<div class="posts_avatar"></div>
<div class="posts_info">
<div class="info_upper">
<span class="info_upper_nickname">
<?php echo escape($row['nickname']); ?>
(@<?php echo escape($row['username']); ?>)
</span>
<span class="info_upper_time"><?php echo escape($row['created_at']); ?></span>
<?php if ($username) { ?>
<?php if (hasPermission($user, "update", $row)) { ?>
<a href="update_comment.php?id=<?php echo $row['id']; ?>">Edit</a>
<a href="delete_comment.php?id=<?php echo $row['id']; ?>">Delete</a>
<? } ?>
<?php }?>
</div>
<p class="info_lower"><?php echo escape($row['content']); ?></p>
</div>
</div>
<?php } ?>
</section>
<section>
<div class="dividen"></div>
<?php
$stmt = $conn->prepare(
'SELECT count(id) as count FROM peipei_comments WHERE is_deleted IS NULL'
);
$result = $stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$count = $row['count'];
$total_page = ceil($count / $items_per_page);
?>
<div class="page_info">
<span>Total of messages are <?php echo $count; ?>,this is page:</span>
<span><?php echo $page; ?> / <?php echo $total_page; ?></span>
</div>
<div class="pagenator">
<?php if ($page != 1) { ?>
<a href="index.php?page=1">First page</a>
<a href="index.php?page=<?php echo $page - 1; ?>">Previous</a>
<?php } ?>
<?php if ($page != $total_page) { ?>
<a href="index.php?page=<?php echo $page + 1; ?>">Next</a>
<a href="index.php?page=<?php echo $total_page; ?>">Last page</a>
<?php } ?>
</div>
</section>
</main>
<script>
let btn = document.querySelector('.update_nickname')
btn.addEventListener('click', function() {
let form = document.querySelector('.board_nickname-form')
form.classList.toggle('hide')
})
</script>
</body>
</html>