-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
362 lines (328 loc) · 11.4 KB
/
main.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
session_start();
require_once "common.php";
//Check if logged in. If not, redirect to index.php
if (!isset($username) || empty($username)) header("Location: logout.php");
if ($_SESSION["authkey"] != AUTHKEY) {
header("Location:index.php?ERROR=Failed%20Auth%20Key");
}
$db = connectToDB();
$error_message = "";
//Find total number of programs in database
$sql = "SELECT COUNT(*) AS total FROM fileinfo";
$result = mysqli_query($db,$sql);
if (!$result) {
die("Query to count programs in 'fileinfo' failed");
}
$totalNum = $result->fetch_row()[0];
//Find number of unmarked programs in database
$sql = "SELECT COUNT(*) AS total FROM fileinfo WHERE mark IS NULL";
$result = mysqli_query($db,$sql);
if (!$result) {
die("Query to count unmarked programs in 'fileinfo' failed");
}
$totalUnmarked = $result->fetch_row()[0];
$sql = "SELECT id,filename,path,timeUploaded,timeMarked,comment,mark FROM fileinfo WHERE username = ? ORDER by timeUploaded DESC";
if ($stmt = $db->prepare($sql)) {
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
//$stmt->fetch();
$stmt->close();
} else {
$message_ = 'Invalid query: ' . mysqli_error($db) . "\n<br>";
$message_ .= 'SQL: ' . $sql;
die($message_);
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
/* Find all (top level) folders for this user */
$dir = "./files/$username/";
//remove . and .. from directory listings
$scanned_directory = array_diff(scandir($dir), array('..', '.'));
$folders = array();
foreach($scanned_directory as $file)
{
if (is_dir($dir.$file)) $folders[]=$file;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Uploader : <?= $username?></title>
<link rel="stylesheet" href="./resources/bootstrap.min.css">
<link rel="stylesheet" href="local.css">
<script>
var showMarked = true;
function hideShowMarked() {
//document.getElementById("results_box").style = "display:inline-block;";
// alert("hello button 3");
showMarked = !showMarked;
if (showMarked) {
$('.marked').css('display','table-row');
$('#btnMarked').text('Hide marked work');
} else {
$('.marked').css('display','none');
$('#btnMarked').text('Show marked work');
}
}
</script>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
function confirmAction() {
return confirm("Are you sure");
}
function validateFileData() {
var x, text;
x = document.getElementById("fileToUpload").value;
if (!x || 0 === x.length) {
text = "You must choose a file";
//text = "<div class=\"error\">" + text + "</div>";
document.getElementById("error_message").outerHTML =
'<div id="error_message" class="alert alert-danger w-50"></div>';
document.getElementById("error_message").innerHTML = text;
return false;
}
/* Now check if it is a duplicate file */
//I can't make this a separate function as it ends too soon. ajax is not blocking output
/*
var filename, foldername;
filename = document.getElementById("fileToUpload").value;
foldername = document.getElementById("foldername").value;
console.log("start");
event.preventDefault();
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
async: true,
url: 'checkDuplicateFile.php',
data: {
filename: filename,
foldername: foldername
},
// timeout:3000,
dataType: "json",
success: function (msg,status,xhr) {
console.log(JSON.stringify(xhr));
console.log("status:"+msg.status);
if (msg.status == true ) {
var x = confirm("This FILE already exists. Do you want to overwrite it?");
if (x) {
console.log("perform upload2")
return true;
} else {
return false;
}
} else if (msg.status == false) {
console.log("perform upload1")
return true;
} else {
ajaxResult = "error";
alert("A strange error has happened. check duplicate files. JSON." + msg);
return false;
}
},
error: function (xhr) {
//alert("An error occured: " + xhr.status + " " + xhr.statusText);
console.log(JSON.stringify(xhr));
// console.log("ERROR:" + xhr.status + " " + xhr.statusText);
return false;
}
});
*/
return true;
}
function validateFolder() {
var x;
x = document.getElementById("folder").value;
if (!x || 0 === x.length) return false;
return true;
}
</script>
<div class="">
<a href="help.html">
<button class="btn m-1 btn-outline-primary ">Help</button></a>
<span class="float-right mt-2 mr-5 text-secondary">
♦ Number not marked = <?=$totalUnmarked?> ♦
Total # of programs = <?=$totalNum?> ♦
</span></div>
<div class="container-fluid my-2">
<div class="card Xtext-center bg-secondary my-2 py-2">
<h3 class="text-center text-white">Hello <?php echo $fullname?>
<button class="btn float-right btn-warning mr-2 shadow"
onclick="location.href='logout.php'">Logout</button></h3>
<form action="upload.php" method="post" enctype="multipart/form-data" >
<div class="row mx-2">
<div class="col-md-4 overflow-hidden">
<input class="btn btn-primary shadow pb-1" type="file" name="fileToUpload" id="fileToUpload">
</div>
<div class="col-md-4 btn btn-outline-info text-white pb-0">
<label for="foldername">Select folder: </label>
<select id="foldername" name="foldername">
<option value="none" selected="selected">none</option>
<?php
foreach($folders as $f) {
echo '<option value="'.$f.'">'.$f.'</option>';
}
?>
</select>
<!-- <input type="text" name="username" id="username" class="pb-2" placeholder="Save in folder (name)"> -->
</div>
<div class="col-md-4">
<input class="btn btn-success shadow pb-2" type="submit" value="Upload chosen file"
name="submit" onclick="return validateFileData();">
</div>
</div>
</form>
</div>
<div id="error_message"></div>
<div class="text-secondary">Uploaded files
<button id="btnMarked" class="btn btn-outline-warning mb-1 float-right" type="button" onclick="hideShowMarked()">Hide marked work</button></h2>
</div>
<table class="table table-bordered">
<tr>
<th>FileName</th>
<th>Folder</th>
<th>Date Uploaded<br><span class="purp">Marked</span></th>
<th></th>
<th class="commentW">Comments</th>
<th>Mark</th>
</tr>
<?php
//filename,path,time,comment,mark
foreach ($data as $row){
/*
$id = $item[0];
$filename = $item[1];
$path = $item[2];
$time = $item[3];
$comment = stripslashes($item[4]);
$mark = $item[5];
*/
$id = $row['id'];
$filename = $row['filename'];
$path = $row['path'];
$timeMK = $row['timeMarked'];
$timeUP = $row['timeUploaded'];
$comment = stripslashes($row['comment']);
$mark = $row['mark'];
#marked:
if ($mark != "") {
echo "<tr class=\"marked\">";
echo "<td>$filename</td>";
echo "<td>$path</td>";
echo "<td>$timeUP<br><span class='purp'>$timeMK</span></td>";
echo "<td colspan=2>";
echo "<form class='d-inline' method='post' action='download.php'><input name='id' value='$id' hidden>";
echo "<button class='btn btn-info shadow smallbtn sml'>Download</button>";
echo "</form> ";
echo "<form class='d-inline' method='post' action='delete.php' onsubmit=\"return confirmAction()\"> <input name='id' value='$id' style='outline: none;' hidden>";
echo "<button class='btn btn-danger shadow smallbtn smr'>Delete</button>";
echo "</form></td>";
#echo '<td><textarea readonly rows="2" style="width:100%">'.$comment.'</textarea></td>';
echo "<td>$mark</td>";
echo "</tr>";
echo "<tr class=\"marked\">";
echo "<td> </td>";
echo '<td colspan=4><textarea readonly rows="2" style="width:100%;background-color:#C7C7F4;">'.$comment.'</textarea></td>';
echo "<td> </td>";
echo "</tr>";
}
//TODO: smallbtn does nothing. What is smr ??? Use btn-sm
#notmarked and not comment
elseif ($mark == "" && $comment == "") {
echo "<tr>";
echo "<td>$filename</td>";
echo "<td>$path</td>";
echo "<td>$timeUP</td>";
echo "<td colspan=2>";
echo "<form class='d-inline' method='post' action='download.php'><input name='id' value='$id' hidden>";
echo "<button class='btn btn-info shadow smallbtn sml'>Download</button>";
echo "</form> ";
echo "<form class='d-inline' method='post' action='delete.php' onsubmit=\"return confirmAction()\"> <input name='id' value='$id' style='outline: none;' hidden>";
echo "<button class='btn btn-danger shadow smallbtn smr'>Delete</button>";
echo "</form></td>";
#echo '<td><textarea readonly rows="1" style="width:100%">'.$comment.'</textarea></td>';
echo "<td>$mark</td>";
echo "</tr>";
#not marked, but has comment
} else {
echo "<tr>";
echo "<td>$filename</td>";
echo "<td>$path</td>";
echo "<td>$time</td>";
echo "<td>";
echo "<form class='d-inline' method='post' action='download.php'><input name='id' value='$id' hidden>";
echo "<button class='btn btn-info shadow smallbtn sml'>Download</button>";
echo "</form> ";
echo "<form class='d-inline' method='post' action='delete.php' onsubmit=\"return confirmAction()\"> <input name='id' value='$id' style='outline: none;' hidden>";
echo "<button class='btn btn-danger shadow smallbtn smr'>Delete</button>";
echo "</form></td>";
echo '<td><textarea readonly rows="1" style="width:100%">'.$comment.'</textarea></td>';
echo "<td>$mark</td>";
echo "</tr>";
}
}
?>
</table>
<div class="divider py-1 mb-3 bg-info rounded"></div>
<div class="row">
<div class="col-md-6">
<div class="text-secondary">Folders</div>
<table class="table table-bordered">
<tr>
<td>
<?php
if (count($folders) == 0) echo "-- none --";
foreach ($folders as $f) {
echo "• ".$f."<br>";
}
?>
</td>
</tr>
</table>
</div>
<div class="col-md-6">
<div class="card border-success">
<div class="card-header">Checklist for Java programs before uploading</div>
<div class="card-body">
<div class="card-text">
<ul type="square">
<li>comment at top with your name, date and purpose of program
<li>class names are uppercase
<li>variable names are lowercase
<li>method names are lower case
<li>program is indented correctly
</ul>
</div>
</div>
</div>
</div>
</div>
<div id="folderForm" class="d-none">
<form method='post' action='createFolder.php' onsubmit="return validateFolder()">
<div class="row input-group ml-0">
<input type="text" name="folder" id="folder" class="col-4 form-control"
placeholder="Enter folder name">
<input type="submit" class="btn btn-outline-success mr-2 shadow" value="Create folder">
</div>
</form>
</div>
<button id="folderBtn" class="btn btn-outline-secondary mr-2 shadow" onclick="displayForm()">Create
folder</button>
<p class="text-success">Only create a folder if you have a project with many files</p>
</div>
<script>
function displayForm() {
document.getElementById("folderBtn").outerHTML = '<div id="folderBtn" class="d-none"></div>';
document.getElementById("folderForm").classList.remove('d-none');
document.getElementById("folderForm").classList.add('d-block');
}
</script>
</body>
</html>