-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.php
182 lines (152 loc) · 4.64 KB
/
handler.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
<?php
session_start();
//Variables
include_once './captcha/securimage.php';
$securimage = new Securimage();
if ($securimage->check($_POST['captcha']) == false) {
$_SESSION['error']='captcha';
header("Location: ./error.php");
exit();
}
$stunumber = $_POST["number"];
$stunumber = substr($stunumber,0,12);//Overflow prevention
$stunumber = strip_tags($stunumber);
$stuip = $_SERVER['REMOTE_ADDR'];
$stuname = $_POST['name'];
$filename = $_FILES['userfile']['name'];
$courseid = $_POST['courseid'];
$coursecategoryid = $_POST['coursecategoryid'];
//SQL injection prevention
if (preg_match('#(DROP|INSERT|UPDATE|TABLE|;)#i',$stuname))
{
$_SESSION['error']= 'injection';
header("Location: ./error.php");
exit();
}
//End SQL injection prevention
//Begin Database connection
$upconnect = new PDO('sqlite:./db/db.sqlite');
$uploaders = $upconnect->query("SELECT * FROM uploaded WHERE course = '$courseid'");
$trustlists = $upconnect->query("SELECT * FROM trustlist");
//End Database connection
$accept = 0;
function validate($test_key,$stunumber)
{
$stulen = strlen($stunumber);
$test_keylen = strlen($test_key);
if($stulen != $test_keylen)
{
return 0;//Meet error
}
for($i=0;$i<$stulen;$i++)
{
if($test_key[$i]!= '*' && ($test_key[$i]-$stunumber[$i]) )
return 0;
}
return 1;
}
//Begin Student number isolation and validation
foreach ($trustlists as $test_key) {
if(validate($test_key['number'],$stunumber))
{
$accept = 1;
}
}
//End Student number isolation and validation
//Prevent Duplication
foreach ($uploaders as $test_key) {
if($stunumber == $test_key["number"])
{
$_SESSION['error']= 'duplicate';
header("Location: ./error.php");
exit();
}
}
//End Duplication Prevention
//If anything failed, return to upload page
if($accept != 1)
{
header("Location: ./index.php");
exit();
}
//If success,keep uploading,Upload to /upload/$coursecategory/$courseid
$uploaddir = './upload/';
$uploadfile = $uploaddir . $coursecategoryid . "/" . $courseid . "/" . basename($_FILES['userfile']['name']);
$FileType = pathinfo($uploadfile,PATHINFO_EXTENSION);
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>提交信息</title>
<!-- Font Awesome -->
<link rel="stylesheet" href="./css/font-awesome.min.css">
<!-- Bootstrap core CSS -->
<link href="./css/bootstrap.min.css" rel="stylesheet">
<!-- Material Design Bootstrap -->
<link href="./css/mdb.min.css" rel="stylesheet">
</head>
<body>
<img src="./img/success.jpg" style="position: fixed;width: 100%; opacity: 0.6; z-index: -100" alt="">
<?php include('./partial/nav.php') ?>
<div class="container">
<div class="page-header">
<h1>上传信息</h1>
</div><!--/ header -->
<div class="jumbotron">
<?php
if (
(
$FileType == "7z"
|| $FileType == "rar"
|| $FileType == "zip"
)
&& ($_FILES["userfile"]["size"] < 1024000000)) // Less than 1 Gib
{
if ($_FILES["userfile"]["error"] > 0)
{
echo "错误:: " . $_FILES["userfile"]["error"] . "<br>";
}
else
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "<p>";
echo "文件";
echo $_FILES['userfile']['name'];
echo "已经成功上传";
echo "</p>";
$realname = $stunumber . "-" . $filename;
date_default_timezone_set('Asia/Hong_Kong');
$time = date('Y-m-d H:i:s');
$upconnect->exec("INSERT INTO 'uploaded' ('number','ip','name','filename','course','time') VALUES ('$stunumber','$stuip','$stuname','$realname','$courseid','$time');");
} else {
echo "<p>";
echo "文件错误!";
echo "</p>";
}
}
}
else
{
echo "文件格式不正确!";
}
$correctname= $uploaddir . $coursecategoryid . "/" . $courseid . "/" . $stunumber . "-" . $_FILES['userfile']['name'];
rename($uploadfile,$correctname);
?>
</div>
</div>
</div> <!-- /container -->
<?php include('./partial/footer.php') ?>
<!-- SCRIPTS -->
<!-- JQuery -->
<script type="text/javascript" src="./js/jquery-3.1.1.min.js"></script>
<!-- Bootstrap tooltips -->
<script type="text/javascript" src="./js/tether.min.js"></script>
<!-- Bootstrap core JavaScript -->
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<!-- MDB core JavaScript -->
<script type="text/javascript" src="./js/mdb.min.js"></script>
</body>
</html>