-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.php
236 lines (220 loc) · 6.56 KB
/
list.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
$page = "My Lists";
include 'includes/dash.header.php';
include 'includes/library.php';
$pdo = connectDB();
$listId = $_GET['id'] ?? null;
$editId = $_GET['edit'] ?? null;
function alert($var){
echo '<script>alert("'.$var.'")</script>';
}
function randomPass() {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 6; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
if(!empty($_POST['newlist'])) {
$true = 1; $false = 0; $content = ""; $date = date("Y-m-d H:i:s");
$stmt = $pdo->prepare("INSERT INTO lists (listname,ownerId,public,disable,views,expiryDate,content) VALUES (:listname,:ownerId,:public,:disable,:views,:expiryDate,:content)");
$stmt->bindParam(':listname',$_POST['newlist']);
$stmt->bindParam(':ownerId',$_SESSION['id']);
$stmt->bindParam(':public', $true);
$stmt->bindParam(':disable', $false);
$stmt->bindParam(':views', $false);
$stmt->bindParam(':expiryDate', $date);
$stmt->bindParam(':content', $content);
$stmt->execute();
$listId = $pdo->lastInsertId();
header("Location: /list.php?edit=$listId");
}
if(!empty($editId))
{
$stmt = $pdo->prepare("SELECT * FROM lists WHERE id = ? AND ownerId = ?");
$stmt->execute([$editId,$id]);
$list = $stmt->fetch();
if($stmt->rowCount() == 0){
alert("You don't have permission to edit this list");
echo("<script>location.href='list.php'</script>");
}
}
/*
function update($new,$place,$id){
$pdo = connectDB();
$sql = "UPDATE lists set $place = '$new' WHERE id = $id";
$stmt = $pdo->prepare($sql);
$stmt->execute();
if($stmt->rowCount()){
alert("update successful!!");
}else {
alert("error");
}
}
if(!empty($_POST['listname'])){
update(RemoveXSS($_POST['listname']),'listname',$listId);
}else{
alert('new user name is not valid or repeat');
}
if(isset($_POST['public'])){
update(1,'public',$listId);
}else{
update(0,'public',$listId);
}
if(isset($_POST['disable'])){
update(1,'disable',$listId);
}else{
update(0,'disable',$listId);
}
if($_FILES['file']['error'] == 0) {
// Initialize counter if not set
if(!isset($_SESSION['file'])) {
$_SESSION['file'] = 1;
}
$unique_id = $_SESSION['file'];
$path = WEBROOT."www_data/";
$fileroot = "text";
if(is_uploaded_file($_FILES['file']['tmp_name'])){
$results = checkErrors('file', 1024000);
if(strlen($results) > 0){
$message = $results;
} else {
// Move file to www_data
$newname = createFilename('file', $path, $fileroot, $unique_id);
if(!move_uploaded_file($_FILES['file']['tmp_name'], $newname)){
$message = "Failed to move uploaded file.";
} else {
$message = "File uploaded successfully.";
update(file_get_contents($newname),'featureImage',$listId);
}
}
} else{
$results = checkErrors('file', 1024000);
$message = $results;
}
}
if(isset($_POST['content'])){
update(RemoveXSS($_POST['content']),'content',$listId);
}
*/
if(!empty($_GET['delete'])){
$deleteId = $_GET['delete'];
$sql = "SELECT ownerId FROM `lists` WHERE `id` = $deleteId";
$stmt = $pdo ->prepare($sql);
$stmt->execute();
$ownId = $stmt->fetch()['ownerId'];
if($ownId == $_SESSION['id']){
$sql = "DELETE FROM `lists` WHERE `id` = $deleteId";
$stmt = $pdo ->prepare($sql);
$stmt->execute();
$list = $stmt->rowCount();
if($list>0){
alert("delete list successful");
}
}else{
alert("User does not have delete permission");
}
echo("<script>location.href='list.php'</script>");
}
if(empty($listId)):
$pdo = connectDB();
$stmt = $pdo->prepare("SELECT * FROM lists where ownerId = ?");
$stmt->execute([$id]);
$rows = $stmt->rowCount();
$result = $stmt->fetchAll()
?>
<main>
<div class="list">
<!-- hidden form for self-processing and honeypot -->
<form method="get">
<input type="text" name="input">
</form>
<div>
<h2>My Lists</h2>
<div>
<ul>
<li></li>
<li><a href="?create=1"><button class="btn round" name="create">Create</button></a></li>
<li><button class="btn round" name="delete">Delete</button></li>
</ul>
</div>
<?php if(!empty($_GET['create'])): ?>
<form method="post">
<input type="text" name="newlist" placeholder="List Name">
<button class="btn dark round" type="submit">Create</button>
</form>
<?php endif ?>
<ul>
<?php if($rows> 0){
foreach ($result as $row):?>
<a href="list.php?id=<?php echo $row['id']?>">
<li>
<input type="checkbox" name="<?php echo $row['id']?>">
<!-- https://codepen.io/felquis/pen/JjMEEG -->
<p data-content="<?php echo $row['public'] == 1 ? "Public" : "Private" ?>"><?php echo $row['listname']?></p>
<button class="btn round" name="edit">Edit</button>
<button class="btn round" name="delete">Delete</button>
</li>
</a>
<?php endforeach;}
else{?>
<a href="list.php?create=1">
<li>
<span>You have no registry on the list, but you can create it now!</span>
</li>
</a>
<?php };?>
</ul>
</div>
</div>
</main>
<?php else:
$pdo = connectDB();
$stmt = $pdo->prepare("SELECT * FROM lists where id = ?");
$stmt->execute([$listId]);
$result = $stmt->fetch();
if($result) {
if($result['ownerId'] != $id) {
echo 'You are not the owner of this list';
echo '<script>window.location.href = "user.php"</script>';
}
else {
$stmt = $pdo->prepare("SELECT username FROM users where id = ?");
$stmt->execute([$result['ownerId']]);
$owner = $stmt->fetch()['username'];
}
} else {
alert("list not found");
echo '<script>window.location.href = "user.php"</script>';
}
?>
<main>
<div class="list">
<!-- hidden form for self-processing and honeypot -->
<form method="get">
<input type="text" name="input">
</form>
<div>
<h2><?php echo $result['listname'] ?></h2>
<p><i class="ri-user-fill">Owner: </i><span><?php echo $owner ?></span> <i class="ri-git-repository-private-fill"></i>Visible: <?php echo $result['public'] == 1 ? "Public" : "Private" ?></span></p>
<div>
<ul>
<li></li>
<li><button class="btn round" name="edit">Edit</button></li>
<li><button class="btn round" name="delete">Delete</button></li>
</ul>
</div>
<ul contenteditable>
<?php echo $result['content'] ?>
</ul>
<div class="edit">
<button class="btn round" onclick="newItem()">New item</button>
<button class="btn round" onclick="update()">Submit</button>
</div>
</div>
</div>
</main>
<?php endif; ?>
<?php include 'includes/dash.footer.php'; ?>