-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadexpg.php
345 lines (296 loc) · 8.03 KB
/
uploadexpg.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
<?php
session_start();
require_once "functions.php";
$user = $_SESSION['user'];
$subj = sanitizeString($_POST['subj']);
$year = date("Y");
$sem = semester();
setlocale(LC_CTYPE, 'ko_KR.eucKR'); //CSV데이타 추출시 한글깨짐방지
$error = $success = $msg = "";
function getExt($filename){
$ext = substr(strrchr($filename,"."),1);
$ext = strtolower($ext);
return $ext;
}
function detectFileEncoding($filepath) {
// 리눅스 기본 기능을 활용한 파일 인코딩 검사
$output = array();
exec('file -i ' . $filepath, $output);
if (isset($output[0])){
$ex = explode('charset=', $output[0]);
return isset($ex[1]) ? $ex[1] : null;
}
return null;
}
if($_FILES['file']['name'] != "") {
if($subj == "") {
$error = "과목명을 입력해주세요";
}
else {
//if there was an error uploading the file
if($_FILES["file"]["error"] > 0) {
$error = "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else {
//Print file details
$success = "Uploaded File Name: " . $_FILES["file"]["name"] . "<br><br>";
// Edit upload location here
$tmpname = $_FILES['file']['tmp_name'];
$realname = $_FILES['file']['name'];
$subj = $_POST['subj'];
$fileExt = getExt($realname);
if (!strstr('[c]',$fileExt)) {
$error = "c 파일만 등록할 수 있습니다.<br>";
}
else {
$readFile = "example.c";
$errorFile ="errorFile.txt";
$TABLENAME ='user'; // 테이블명
if (is_uploaded_file($tmpname)) {
move_uploaded_file($tmpname,$readFile);
@chmod($readFile,0606);
}
$file_read = fopen($readFile,"r");
if(!$file_read){
$error = "파일을 찾을 수가 없습니다!<br>";
}
// 파일 인코딩 모드 검사
$current_encoding = detectFileEncoding($readFile);
$code = fread($file_read,filesize("example.c"));
//이미 있는 프로그램인지 검사
$same = 0;
$sql = queryMysql("SELECT code FROM codes WHERE ID='$user' AND subject='$subj' AND year='$year' AND semester='$sem'");
while($row = mysqli_fetch_array($sql)) {
$result = (string)$row[0];
if($result == $code)
$same = 1;
}
if($same != 1) {
$code = str_replace("\\", "\\\\", $code);
$code = str_replace("'", "\'", $code);
$result = queryMysql("INSERT INTO codes VALUES('$user', '$subj', '$code', '$year', '$sem')");
$msg = "프로그램 업로딩 성공";
}
else {
$msg = "이미 있는 프로그램입니다.";
}
fclose($file_read);
unlink($readFile); // 업로드 완료후에 파일 삭제 처리
}
}
}
}
echo <<<_END
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Nanum+Gothic+Coding:wght@700&display=swap" rel="stylesheet">
<title>Upload Example Programs</title>
<style>
body {
margin: 0;
padding: 0;
position: relative;
font-family: 'Nanum Gothic Coding', monospace;
}
#title {
position: fixed;
width: 100%;
background-color: white;
}
#uploaddiv {
padding: 5px 20px;
position: absolute;
top: 25vh;
left: 30%;
width: 450px;
height: 250px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#uploadstd {
position: fixed;
top: 13vh;
height: 87vh;
border-right: 0.7px solid;
background: #4F86C6;
}
#exprogram, #resetpw, #settimeform, #submittedform, #logout {
height: 5px;
width: 170px;
position: fixed;
top: 13vh;
}
#subj {
position: absolute;
left: 190px;
}
.uploadform {
width: 450px;
}
.uploadform > div {
display: flex;
justify-content: center;
padding-bottom: 7px;
align-items: center;
}
.error {
position: absolute;
left: 35%;
top: 5vh;
color: red;
}
.success {
position: absolute;
left: 25%;
top: 0vh;
}
.std {
width: 170px;
margin: 40px 10px 0px 5px;
padding: 2px;
border: 1px solid #353866;
border-radius: 6px;
background-color: white;
color: #353866;
font-family: 'Nanum Gothic Coding', monospace;
}
.expr {
width: 170px;
position: absolute;
margin: 100px 10px 0px 5px;
padding: 2px;
border: 1px solid #353866;
border-radius: 6px;
background-color: white;
color: #353866;
font-family: 'Nanum Gothic Coding', monospace;
}
.rspw {
width: 170px;
position: absolute;
margin: 160px 5px 0px 5px;
padding: 2px;
border: 1px solid #353866;
border-radius: 6px;
background-color: white;
color: #353866;
font-family: 'Nanum Gothic Coding', monospace;
}
.settime {
width: 170px;
position: absolute;
margin: 220px 5px 0px 5px;
padding: 2px;
border: 1px solid #353866;
border-radius: 6px;
background-color: white;
color: #353866;
font-family: 'Nanum Gothic Coding', monospace;
}
.submitted {
width: 170px;
position: absolute;
margin: 280px 5px 0px 5px;
padding: 2px;
border: 1px solid #353866;
border-radius: 6px;
background-color: white;
color: #353866;
font-family: 'Nanum Gothic Coding', monospace;
}
.msg {
position: absolute;
left: 30%;
top: 35vh;
}
.exit {
width: 170px;
margin: 480px 5px 0px 5px;
padding: 2px;
border: 1px solid #353866;
border-radius: 6px;
background-color: white;
color: #353866;
font-family: 'Nanum Gothic Coding', monospace;
}
.submit, #file {
padding: 6px;
font-family: 'Nanum Gothic Coding', monospace;
}
label {
flex: 1;
text-align: left;
}
</style>
</head>
_END;
if($_SESSION['user'] == 'admin') {
echo <<<_END
<body>
<div id='title'>
<h2 align='center'>Upload Example Programs</h2>
<hr size='1' noshade></hr>
</div>
<div id='uploaddiv'>
<div class='error'>$error</div>
<div class='success'>$success</div>
<form class='uploadform' action="uploadexpg.php" method="post" enctype='multipart/form-data'>
<div>
<label>예제 프로그램 선택</label>
<input type="file" name="file" id="file"></input>
</div>
<div>
<label>과목 입력</label>
<input type="text" name="subj" id="subj"></input>
</div>
<div>
<label></label>
<br><br>
<input class='submit' type='submit'>
</div>
</form>
<div class='msg'>$msg</div>
</div>
<form id='uploadstd' method='POST' action='uploaduser.php'>
<input class='std' type='submit' value='학생 정보 업로드'></input>
</form>
<form id='exprogram' method='POST' action='uploadexpg.php'>
<input class='expr' type='submit' value='예제프로그램 업로드'></input>
</form>
<form id='resetpw' method='POST' action='resetpw.php'>
<input class='rspw' type='submit' value='비밀번호 초기화'></input>
</form>
<form id='settimeform' method='POST' action='settime.php'>
<input class='settime' type='submit' value='마감 날짜 설정'></input>
</form>
<form id='submittedform' method='POST' action='submittedfile.php'>
<input class='submitted' type='submit' value='제출 프로그램 보기'></input>
</form>
<form id='logout' method='POST' action='logout.php' align='right'>
<input type='submit' class='exit' value='로그아웃'></input>
</form>
</body>
</html>
_END;
}
else {
echo <<<_END
<body>
<script>
alert('Please log in as administer to use this page.');
location.href='login.php';
</script>
</body>
</html>
_END;
}
?>