-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload.php
256 lines (238 loc) · 13.5 KB
/
upload.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
<?php
require_once('core/Main.php');
if (!$userSystem->isLoggedIn()) {
$log->info('upload.php', 'User was not logged in');
$redirect->redirectTo('login.php');
}
$status = NULL;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$lectureID1 = filter_input(INPUT_POST, 'lectureSelection1', FILTER_SANITIZE_NUMBER_INT);
$lectureID2 = filter_input(INPUT_POST, 'lectureSelection2', FILTER_SANITIZE_NUMBER_INT);
$lectureID3 = filter_input(INPUT_POST, 'lectureSelection3', FILTER_SANITIZE_NUMBER_INT);
$examiner = filter_input(INPUT_POST, 'examiner', FILTER_SANITIZE_SPECIAL_CHARS);
$collaborators = filter_input(INPUT_POST, 'collaborators', FILTER_SANITIZE_SPECIAL_CHARS);
$remark = filter_input(INPUT_POST, 'remark', FILTER_SANITIZE_SPECIAL_CHARS);
$legal = filter_input(INPUT_POST, 'legal', FILTER_SANITIZE_SPECIAL_CHARS);
$status = 'CHECK_FILE';
if ($lectureID1 == '') {
$status = 'LECTURE_MISSING';
$log->warning('upload.php', 'Lecture missing for uploaded file');
} else if ($examiner == '') {
$status = 'EXAMINER_MISSING';
$log->warning('upload.php', 'Examiner missing for uploaded file');
} else if ($legal != 'checkedLegal') {
$status = 'LEGAL_MISSING';
$log->warning('upload.php', 'Legal missing for uploaded file');
}
if ($status == 'CHECK_FILE') {
$file = $_FILES['protocol_file'];
$fileName = $file['name'];
$fileNameExtension = pathinfo($fileName, PATHINFO_EXTENSION);
$fileNameTmp = $file['tmp_name'];
$fileError = $file['error'];
$fileSizeBytes = $file['size'];
$fileType = 'application/' . $fileNameExtension;
if ($fileError == UPLOAD_ERR_OK) {
if (in_array($fileNameExtension, Constants::ALLOWED_FILE_EXTENSION_UPLOAD)) {
$status = 'ACCEPT_FILE';
$log->debug('upload.php', 'Accepting uploaded file: ' . $fileNameTmp);
} else {
$status = 'WRONG_EXTENSION';
$log->warning('upload.php', 'Uploaded file extension forbidden: ' . $fileNameExtension);
}
} else {
$status = 'NO_FILE_UPLOADED_ERROR';
$log->warning('upload.php', 'No file uploaded error: ' . $fileError);
}
}
if ($status == 'ACCEPT_FILE') {
$justAddedProtocol = $examProtocolSystem->addProtocol($currentUser, $collaborators, $remark, $examiner, $fileNameTmp, $fileNameExtension, $fileSizeBytes, $fileType);
if ($justAddedProtocol == NULL) {
$status = 'ERROR_ON_INSERTING_PROTOCOL';
$log->error('upload.php', 'Error on inserting exam protocol into DB');
} else {
$lectureIDs = array($lectureID1);
if ($lectureID2 != '' && $lectureID1 != $lectureID2) {
$lectureIDs[] = $lectureID2;
}
if ($lectureID3 != '' && $lectureID1 != $lectureID3 && $lectureID2 != $lectureID3) {
$lectureIDs[] = $lectureID3;
}
$result = $lectureSystem->addProtocolIDsToLecture($lectureIDs, $justAddedProtocol->getID());
if ($result) {
$status = 'PROTOCOL_UPLOADED_SUCCESSFULLY';
$log->debug('upload.php', 'Inserted protocol successfully');
} else {
$status = 'ERROR_ON_INSERTING_PROTOCOL';
$log->error('upload.php', 'Error on inserting exam protocol IDs into lecture on DB');
}
}
}
}
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
if (isset($_GET['name'])) {
if ($name == '') {
$status = 'NAME_MISSING';
} else {
$justAddedLecture = $lectureSystem->addLecture($name);
if ($justAddedLecture == NULL) {
$status = 'ERROR_ON_INSERTING_LECTURE';
$log->error('upload.php', 'Error on inserting lecture into DB');
} else {
$status = 'LECTURE_ADDED_SUCCESSFULLY';
$log->debug('upload.php', 'Successfully inserted lecture into DB');
}
}
}
}
echo $header->getHeader($i18n->get('title'), $i18n->get('uploadProtocol'), array('upload.css', 'button.css'));
echo $mainMenu->getMainMenu($i18n, $currentUser);
$allLectures = $lectureSystem->getAllLectures();
#Sort list of lectures by name
usort($allLectures, function($a, $b) {
return strcmp($a->getName(), $b->getName());
});
$allLectureOptions = '';
for ($i = 0; $i < count($allLectures); $i++) {
$lecture = $allLectures[$i];
$allLectureOptions .= '<option value="' . $lecture->getID() . '">' . $lecture->getName() . '</option>';
}
echo '<script type="text/javascript">
function checkUploadFileSize() {
var input = document.getElementById("protocol_file");
if(input.files && input.files.length == 1) {
if (input.files[0].size > ' . Constants::MAX_UPLOAD_FILE_SIZE_BYTES . ') {
alert("' . $i18n->get('uploadWillFailAsMaximumFileSizeIs') . ' " + (' . Constants::MAX_UPLOAD_FILE_SIZE_BYTES . '/1024/1024) + "MB.");
return false;
}
}
return true;
}
</script>';
$colorSelectLecture = '';
$colorExaminer = '';
$colorFile = '';
$colorLegal = '';
if ($status == 'LECTURE_MISSING' || $status == 'ERROR_ON_INSERTING_PROTOCOL') {
$colorSelectLecture = ' background-color: #A11E3B;';
}
if ($status == 'EXAMINER_MISSING' || $status == 'ERROR_ON_INSERTING_PROTOCOL') {
$colorExaminer = ' background-color: #A11E3B;';
}
if ($status == 'WRONG_EXTENSION' || $status == 'NO_FILE_UPLOADED_ERROR' || $status == 'ERROR_ON_INSERTING_PROTOCOL') {
$colorFile = ' background-color: #A11E3B;';
}
if ($status == 'LEGAL_MISSING') {
$colorLegal = ' background-color: #A11E3B;';
}
$addLectureFieldOpen = '';
$colorName = '';
if ($status == 'NAME_MISSING' || $status == 'ERROR_ON_INSERTING_LECTURE') {
$addLectureFieldOpen = 'open ';
}
if ($status == 'NAME_MISSING' || $status == 'ERROR_ON_INSERTING_LECTURE') {
$colorName = ' background-color: #A11E3B;';
}
$lectureAddedSuccessfullyMessage = '';
if ($status == 'LECTURE_ADDED_SUCCESSFULLY') {
$lectureAddedSuccessfullyMessage = '<p>' . $i18n->get('lectureAddedSuccessfullyMessage') . '</p>';
}
if ($status == 'PROTOCOL_UPLOADED_SUCCESSFULLY') {
echo '<center><br><a class="styledButton" href="lectures.php"><span style="font-size: 20px">«</span> ' . $i18n->get('back') . '</a><br><br>';
echo '<p>' . $i18n->get('uploadedSuccessfullyMessage') . '</p>
<p>' . $i18n->get('tokensWillBeGrantedAfterReviewOfProtocol') . '</p>';
} else {
echo '<div style="margin-left: 20%; width: 60%;">
' . $lectureAddedSuccessfullyMessage . '
<div class="info">' . $i18n->get('uploadExplanation') . '
<br><br>' . $i18n->get('templateRemark'). '</div>
<a href="static/templates/template.tex" class="styledButton">
<img src="static/img/protocolDownload.png' . $GLOBALS["VERSION_STRING"] . '" alt="download protocol" style="height: 24px; vertical-align: middle;"> Latex
</a>
<a href="static/templates/template.docx" class="styledButton">
<img src="static/img/protocolDownload.png' . $GLOBALS["VERSION_STRING"] . '" alt="download protocol" style="height: 24px; vertical-align: middle;"> Word
</a>
<a href="static/templates/template.txt" class="styledButton">
<img src="static/img/protocolDownload.png' . $GLOBALS["VERSION_STRING"] . '" alt="download protocol" style="height: 24px; vertical-align: middle;"> Txt
</a>
<br><br>
<details open id="uploadField">
<summary>' . $i18n->get('selectLecture') . '</summary>
<br>
<form enctype="multipart/form-data" action="upload.php" method="POST" onsubmit="checkUploadFileSize();">
<input type="hidden" name="MAX_FILE_SIZE" value="' . Constants::MAX_UPLOAD_FILE_SIZE_BYTES . '" />
<table style="width: 100%;">
<tr>
<td style="width: 20%;' . $colorSelectLecture . '">' . $i18n->get('lecture') . ' 1:</td>
<td>
<select name="lectureSelection1" style="width: 100%;' . $colorSelectLecture . '" required>
<option value="">' . $i18n->get('pleaseSelect') . '</option>
' . $allLectureOptions . '
</select>
</td>
</tr>
<tr>
<td style="width: 20%;">' . $i18n->get('lecture') . ' 2:</td>
<td>
<select name="lectureSelection2" style="width: 100%;">
<option value="">' . $i18n->get('notMandatory') . '</option>
' . $allLectureOptions . '
</select>
</td>
</tr>
<tr>
<td style="width: 20%;">' . $i18n->get('lecture') . ' 3:</td>
<td>
<select name="lectureSelection3" style="width: 100%;">
<option value="">' . $i18n->get('notMandatory') . '</option>
' . $allLectureOptions . '
</select>
</td>
</tr>
<tr>
<td style="width: 20%;' . $colorExaminer . '">' . $i18n->get('examiners') . ':</td>
<td><input type="text" name="examiner" placeholder="' . $i18n->get('lecturersAndAssessors') . '" style="display: table-cell; width: calc(100% - 18px);' . $colorExaminer . '" required></td>
</tr>
<tr>
<td style="width: 20%;">' . $i18n->get('collaborators') . ':</td>
<td><input type="text" name="collaborators" placeholder="' . $i18n->get('zxShortsOfPeopleThatHelpedAndAlsoShallGetTokens') . ' (' . $i18n->get('optional') . ')" style="display: table-cell; width: calc(100% - 18px);"></td>
</tr>
<tr>
<td style="width: 20%;">' . $i18n->get('remark') . ':</td>
<td><input type="text" name="remark" placeholder="' . $i18n->get('remark') . ' (' . $i18n->get('optional') . ')" style="display: table-cell; width: calc(100% - 18px);"></td>
</tr>
<tr>
<td style="width: 20%;' . $colorFile . '">' . $i18n->get('file') . ':</td>
<td><input type="file" id="protocol_file" name="protocol_file" placeholder="" style="display: table-cell; width: calc(100% - 18px);' . $colorFile . '" required accept=".pdf,.txt"></td>
</tr>
<tr>
<td style="width: 20%;' . $colorLegal . '"><input type="checkbox" id="legal" name="legal" value="checkedLegal" required></td>
<td><label for="legal">' . $i18n->get('legalDisclaimer'). '</label></td>
</tr>
</table>
<br>
<input type="submit" value="' . $i18n->get('submit') . '">
</form>
</details>
<details ' . $addLectureFieldOpen . 'id="uploadField">
<summary>' . $i18n->get('createNewLecture') . '</summary>
<br>
' . $i18n->get('createNewLectureExplanation') . '
<br>
<br>
<form action="upload.php" method="GET">
<table style="width: 100%;">
<tr>
<td style="width: 20%;' . $colorName . '">' . $i18n->get('lectureTitle') . ':</td>
<td><input type="text" name="name" placeholder="" style="display: table-cell; width: calc(100% - 18px);' . $colorName . '" required></td>
</tr>
</table>
<br>
<input type="submit" value="' . $i18n->get('submit') . '">
</form>
</details>
</div>';
}
echo $footer->getFooter();
?>