-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
175 lines (150 loc) · 5.87 KB
/
Copy pathlib.php
File metadata and controls
175 lines (150 loc) · 5.87 KB
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
<?php
function start_session() {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
}
function require_session_json() {
start_session();
if (!isset($_SESSION['id'])) {
http_response_code(401);
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => '請先登入']);
exit;
}
}
function require_session_redirect() {
start_session();
if (!isset($_SESSION['id'])) {
header('Location: index.php');
exit;
}
}
function get_answer_base() {
static $base = null;
if ($base !== null) return $base;
$config = json_decode(file_get_contents(__DIR__ . '/config.json'), true);
$p = trim($config['answer_path'] ?? '');
$base = $p !== '' ? rtrim($p, '/\\') : __DIR__ . DIRECTORY_SEPARATOR . 'answers';
return $base;
}
function get_folder_path() {
return get_answer_base() . DIRECTORY_SEPARATOR . $_SESSION['folder'];
}
// 建立資料夾並驗證寫入權限,回傳 true 或錯誤訊息字串
function ensure_user_folder($folder_name) {
$base = get_answer_base();
// 確保 answers 根目錄存在
if (!is_dir($base)) {
if (!@mkdir($base, 0755, true)) {
return '無法建立答案根目錄:' . $base . ' 請確認 Web 伺服器對該路徑有寫入權限。';
}
}
$path = $base . DIRECTORY_SEPARATOR . $folder_name;
if (!is_dir($path)) {
if (!@mkdir($path, 0755, true)) {
return '無法建立考生資料夾:' . $path . ' 請確認 Web 伺服器對 NAS 路徑有寫入權限,或請管理員先執行 setup.php。';
}
}
// 寫入測試
$testFile = $path . DIRECTORY_SEPARATOR . '.writable_test';
if (@file_put_contents($testFile, '1') === false) {
return '資料夾存在但無法寫入:' . $path . ' 請確認權限後再試,或聯絡管理員執行 setup.php。';
}
@unlink($testFile);
return true;
}
function validate_question($config, $main, $sub) {
foreach ($config['questions'] as $q) {
if ($q['no'] == $main) {
foreach ($q['subs'] as $s) {
if ($s['no'] == $sub) return $s;
}
}
}
return null;
}
function find_answer_file($folder_path, $main, $sub) {
$files = glob($folder_path . '/' . $main . '-' . $sub . '.*');
if (!$files) return null;
$files = array_filter($files, fn($f) => pathinfo($f, PATHINFO_EXTENSION) !== 'log');
return $files ? array_values($files)[0] : null;
}
function get_client_name(): string {
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
$host = @gethostbyaddr($ip);
return ($host && $host !== $ip) ? $host . ' (' . $ip . ')' : $ip;
}
function write_log($folder_path, $action, $filename, $label) {
$client = get_client_name();
$line = date('Y-m-d H:i:s') . "\t" . $action . "\t" . $filename . "\t" . $label . "\t" . $client . "\n";
file_put_contents($folder_path . '/activity.log', $line, FILE_APPEND | LOCK_EX);
}
function parse_question_key($q) {
if (!preg_match('/^(\d+)-(\d+)$/', $q, $m)) return null;
return [(int)$m[1], (int)$m[2]];
}
// 讀取各小題最後一次 ANSWER/UPLOAD 的存檔時間,回傳 ['1-1' => 'YYYY-MM-DD HH:MM:SS', ...]
function get_last_save_times(string $folder_path): array {
$times = [];
$logFile = $folder_path . '/activity.log';
if (!is_file($logFile)) return $times;
foreach (file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [] as $line) {
$p = explode("\t", $line);
if (count($p) < 4) continue;
if ($p[1] !== 'ANSWER' && $p[1] !== 'UPLOAD') continue;
$times[$p[3]] = $p[0]; // label (e.g. '1-1') => datetime
}
return $times;
}
// ── 批改相關 ──────────────────────────────────────────────────────────────────
function get_grades_path(string $fp): string { return $fp . '/grades.json'; }
function get_grades(string $fp): array {
$f = get_grades_path($fp);
if (!is_file($f)) return ['readonly' => false, 'questions' => []];
$d = json_decode(file_get_contents($f), true);
return is_array($d) ? $d : ['readonly' => false, 'questions' => []];
}
function save_grades(string $fp, array $g): void {
file_put_contents(get_grades_path($fp), json_encode($g, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX);
}
function is_readonly(string $fp): bool {
return !empty(get_grades($fp)['readonly']);
}
function calc_total_score(array $grades): ?int {
if (empty($grades['questions'])) return null;
$total = 0; $any = false;
foreach ($grades['questions'] as $q) {
if (isset($q['score']) && $q['score'] !== null) {
$total += (int)$q['score'];
$any = true;
}
}
return $any ? $total : null;
}
function require_grader_session_redirect(): void {
start_session();
if (empty($_SESSION['grader_authed'])) {
header('Location: grade.php');
exit;
}
}
function require_grader_session_json(): void {
start_session();
if (empty($_SESSION['grader_authed'])) {
http_response_code(401);
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => '請先以批改者身份登入']);
exit;
}
}
// ─────────────────────────────────────────────────────────────────────────────
// 將文字中的 URL 轉為可點擊連結(target="_blank")
function linkify($text) {
$safe = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
return preg_replace(
'/(https?:\/\/[^\s\,\,\<\>\"\')\)]+)/iu',
'<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>',
$safe
);
}