-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage_data.php
222 lines (216 loc) · 8.89 KB
/
manage_data.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
<?php
require_once(__DIR__."/config/config.php");
require_once(__DIR__."/func/data.php");
require_once(__DIR__."/func/log.php");
$showform = true;
if (!$U["islogin"]) {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
此功能需要驗證帳號,請<a href="<?=$C["path"]?>/login/">登入</a>
</div>
<?php
$showform = false;
}
$action = "none";
if (isset($_POST["action"])) {
$action = $_POST["action"];
} else if (isset($_GET["action"]) && in_array($_GET["action"], ["view", "viewhead"])) {
$action = $_GET["action"];
}
if ($showform && $action === "download") {
writelog(sprintf("[manage_data] %s %s download", $U["data"]["account"], $U["ip"]));
header('Content-Description: File Transfer');
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="學生資料.csv"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $G['BOM'];
@readfile(__DIR__."/data/data-student.csv");
exit;
}
if ($showform && $action === "upload") {
writelog(sprintf("[manage_data] %s %s upload", $U["data"]["account"], $U["ip"]));
if (isset($_FILES["file"])) {
if ($_FILES["file"]["error"] == 0) {
?>
<div class="alert alert-info alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
檔案上傳成功
</div>
<?php
$file = @file_get_contents($_FILES["file"]["tmp_name"]);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$filemime = finfo_file($finfo, $_FILES["file"]["tmp_name"]);
finfo_close($finfo);
if ($file === false) {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
更新失敗:檔案讀取錯誤
</div>
<?php
writelog(sprintf("[manage_data] %s %s upload failed. read failed.", $U["data"]["account"], $U["ip"]));
} else if ($filemime !== "text/plain") {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
更新失敗:檔案格式可能不是純文字檔
</div>
<?php
writelog(sprintf("[manage_data] %s %s upload failed. not plain text.", $U["data"]["account"], $U["ip"]));
} else if (!in_array($_FILES["file"]["type"], $G["csvmime"])) {
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
更新失敗:檔案格式可能不是逗號分隔值(CSV),你的檔案格式是<?=$_FILES["file"]["type"]?>
</div>
<?php
writelog(sprintf("[manage_data] %s %s upload failed. foramt: %s.", $U["data"]["account"], $U["ip"], $_FILES["file"]["type"]));
} else {
$encoding = mb_detect_encoding($file, mb_detect_order(), true);
if ($encoding === false) {
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
訊息:無法檢測檔案編碼,將強制轉為UTF-8
</div>
<?php
// $file = mb_convert_encoding($file, "UTF-8");
$file = iconv("BIG5", "UTF-8//IGNORE", $file);
} else {
?>
<div class="alert alert-info alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
訊息:檢測檔案編碼為<?=$encoding?><?=($encoding!=="UTF-8"?",將轉為UTF-8":"")?>
</div>
<?php
if ($encoding !== "UTF-8") {
// $file = mb_convert_encoding($file, "UTF-8");
$file = iconv($encoding, "UTF-8//IGNORE", $file);
}
}
if (substr($file, 0, 3) == $G['BOM']) {
?>
<div class="alert alert-info alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
訊息:自動處理BOM
</div>
<?php
$file = substr($file, 3);
}
$res = file_put_contents(__DIR__."/data/data-student.csv", $file);
if ($res === false) {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
更新失敗:覆寫檔案出錯
</div>
<?php
writelog(sprintf("[manage_data] %s %s upload failed. rewrite failed.", $U["data"]["account"], $U["ip"]));
} else {
writelog(sprintf("[manage_data] %s %s upload successed.", $U["data"]["account"], $U["ip"]));
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
更新成功
</div>
<?php
}
}
} else {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
檔案上傳失敗,錯誤代碼:<?=$_FILES["file"]["error"]?>
</div>
<?php
writelog(sprintf("[manage_data] %s %s upload failed. upload error code: %s", $U["data"]["account"], $U["ip"], $_FILES["file"]["error"]));
}
} else {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
發生未知錯誤
</div>
<?php
}
}
?>
<!DOCTYPE html>
<html lang="zh-Hant-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<title><?=$C["titlename"]?>/管理學生資料</title>
<style type="text/css">
body {
padding-top: 4.5rem;
}
</style>
</head>
<body>
<?php
require("header.php");
if ($showform) {
?>
<div class="container-fluid">
<h2>管理學生資料</h2>
<form action="" method="post" enctype="multipart/form-data" class="form-inline">
<div class="form-group">
<label class="form-control-label" for="file">選擇檔案:</label>
<input type="file" id="file" name="file" accept=".csv" class="form-control-file" required>
</div>
<span style="color: red;">(會直接覆蓋舊檔案)</span>
<button class="btn btn-success" type="submit" name="action" value="upload"><i class="fa fa-upload" aria-hidden="true"></i> 上傳</button>
</form>
<form action="" method="post">
<button class="btn btn-default" type="submit" name="action" value="view"><i class="fa fa-eye" aria-hidden="true"></i> 檢視所有資料</button>
<button class="btn btn-default" type="submit" name="action" value="viewhead"><i class="fa fa-eye" aria-hidden="true"></i> 檢視欄位標題</button>
<button class="btn btn-success" type="submit" name="action" value="download"><i class="fa fa-download" aria-hidden="true"></i> 下載</button>
</form>
<?php
if (in_array($action, ["view", "viewhead"])) {
writelog(sprintf("[manage_data] %s %s %s.", $U["data"]["account"], $U["ip"], $action));
?>
<div class="table-responsive">
<table class="table">
<tr>
<?php
foreach ($D['student_data']['head'] as $head) {
?><th><?=$head?></th><?php
}
?>
</tr>
<?php
if ($action === "view") {
foreach ($D['student_data']['data'] as $data) {
?>
<tr>
<?php
foreach ($D['student_data']['head'] as $head) {
?><td><?=$data[$head]?></td><?php
}
?>
</tr>
<?php
}
}
?>
</table>
</div>
<?php
}
?>
</div>
<?php
}
require("footer.php");
?>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DzthAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous"></body>
</html>