CatPtain commited on
Commit
bae6fa2
·
verified ·
1 Parent(s): 456e734

Upload 2 files

Browse files
Files changed (2) hide show
  1. auth-test.html +208 -0
  2. save.php +68 -10
auth-test.html ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>VvvebJs 身份验证测试</title>
7
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
8
+ <style>
9
+ .status-ok { color: #28a745; }
10
+ .status-error { color: #dc3545; }
11
+ .status-warning { color: #ffc107; }
12
+ .debug-info { background: #f8f9fa; padding: 15px; border-radius: 5px; font-family: monospace; font-size: 12px; }
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <div class="container mt-5">
17
+ <div class="row">
18
+ <div class="col-md-8 mx-auto">
19
+ <h1 class="mb-4">🔐 VvvebJs 身份验证测试</h1>
20
+
21
+ <div class="card mb-4">
22
+ <div class="card-header">
23
+ <h3>身份验证状态检查</h3>
24
+ </div>
25
+ <div class="card-body">
26
+ <div id="authStatus">
27
+ <div class="text-center">
28
+ <div class="spinner-border" role="status">
29
+ <span class="visually-hidden">检查中...</span>
30
+ </div>
31
+ <p class="mt-2">正在检查身份验证状态...</p>
32
+ </div>
33
+ </div>
34
+
35
+ <div id="testResults" style="display: none;">
36
+ <h5>测试结果</h5>
37
+ <div class="debug-info" id="debugInfo"></div>
38
+ </div>
39
+
40
+ <div class="mt-3">
41
+ <button class="btn btn-primary" onclick="testAuth()">🔄 重新测试</button>
42
+ <button class="btn btn-warning" onclick="testSave()">🚀 测试保存</button>
43
+ <a href="index.html" class="btn btn-secondary">🏠 返回登录页</a>
44
+ <a href="editor.html" class="btn btn-success">📝 返回编辑器</a>
45
+ </div>
46
+ </div>
47
+ </div>
48
+
49
+ <div class="card">
50
+ <div class="card-header">
51
+ <h3>保存测试</h3>
52
+ </div>
53
+ <div class="card-body">
54
+ <div id="saveTestResults">
55
+ <p>点击 "测试保存" 按钮来测试文件保存功能</p>
56
+ </div>
57
+ </div>
58
+ </div>
59
+ </div>
60
+ </div>
61
+ </div>
62
+
63
+ <script>
64
+ // 页面加载时自动检查身份验证
65
+ document.addEventListener('DOMContentLoaded', function() {
66
+ testAuth();
67
+ });
68
+
69
+ function testAuth() {
70
+ const statusDiv = document.getElementById('authStatus');
71
+ const resultsDiv = document.getElementById('testResults');
72
+ const debugDiv = document.getElementById('debugInfo');
73
+
74
+ statusDiv.innerHTML = `
75
+ <div class="text-center">
76
+ <div class="spinner-border" role="status">
77
+ <span class="visually-hidden">检查中...</span>
78
+ </div>
79
+ <p class="mt-2">正在检查身份验证状态...</p>
80
+ </div>
81
+ `;
82
+
83
+ fetch('save.php?action=checkAuth')
84
+ .then(response => {
85
+ console.log('Auth check response status:', response.status);
86
+ return response.json();
87
+ })
88
+ .then(data => {
89
+ console.log('Auth check data:', data);
90
+
91
+ if (data.authenticated) {
92
+ statusDiv.innerHTML = `
93
+ <div class="alert alert-success">
94
+ <h5 class="status-ok">✅ 身份验证成功</h5>
95
+ <p><strong>用户:</strong> ${data.user || '未知'}</p>
96
+ <p>你已成功登录,可以使用VvvebJs编辑器的所有功能。</p>
97
+ </div>
98
+ `;
99
+ } else {
100
+ statusDiv.innerHTML = `
101
+ <div class="alert alert-danger">
102
+ <h5 class="status-error">❌ 身份验证失败</h5>
103
+ <p>你当前未登录,需要先登录才能使用编辑器功能。</p>
104
+ <a href="index.html" class="btn btn-primary">前往登录</a>
105
+ </div>
106
+ `;
107
+ }
108
+
109
+ debugDiv.textContent = JSON.stringify(data, null, 2);
110
+ resultsDiv.style.display = 'block';
111
+ })
112
+ .catch(error => {
113
+ console.error('Auth check error:', error);
114
+ statusDiv.innerHTML = `
115
+ <div class="alert alert-danger">
116
+ <h5 class="status-error">❌ 检查失败</h5>
117
+ <p>无法检查身份验证状态: ${error.message}</p>
118
+ </div>
119
+ `;
120
+
121
+ debugDiv.textContent = 'Error: ' + error.message;
122
+ resultsDiv.style.display = 'block';
123
+ });
124
+ }
125
+
126
+ function testSave() {
127
+ const saveResultsDiv = document.getElementById('saveTestResults');
128
+
129
+ saveResultsDiv.innerHTML = `
130
+ <div class="text-center">
131
+ <div class="spinner-border" role="status">
132
+ <span class="visually-hidden">测试中...</span>
133
+ </div>
134
+ <p class="mt-2">正在测试保存功能...</p>
135
+ </div>
136
+ `;
137
+
138
+ const testHtml = `<!DOCTYPE html>
139
+ <html lang="en">
140
+ <head>
141
+ <meta charset="UTF-8">
142
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
143
+ <title>VvvebJs Auth Test</title>
144
+ </head>
145
+ <body>
146
+ <h1>认证测试文件</h1>
147
+ <p>创建时间: ${new Date().toISOString()}</p>
148
+ <p>如果你能看到这个文件,说明身份验证和保存功能都正常工作。</p>
149
+ </body>
150
+ </html>`;
151
+
152
+ const formData = new FormData();
153
+ formData.append('html', testHtml);
154
+ formData.append('file', 'auth-test-' + Date.now() + '.html');
155
+
156
+ fetch('save.php', {
157
+ method: 'POST',
158
+ body: formData,
159
+ headers: {
160
+ 'X-Requested-With': 'XMLHttpRequest'
161
+ }
162
+ })
163
+ .then(response => {
164
+ console.log('Save test response status:', response.status);
165
+ return response.json();
166
+ })
167
+ .then(data => {
168
+ console.log('Save test data:', data);
169
+
170
+ if (data.success) {
171
+ saveResultsDiv.innerHTML = `
172
+ <div class="alert alert-success">
173
+ <h5 class="status-ok">✅ 保存测试成功</h5>
174
+ <p><strong>文件:</strong> ${data.file}</p>
175
+ <p><strong>外部存储:</strong> ${data.external_storage ? '✅ 成功' : '❌ 失败'}</p>
176
+ <p><strong>本地缓存:</strong> ${data.local_cache ? '✅ 成功' : '❌ 失败'}</p>
177
+ <p><strong>用户:</strong> ${data.user}</p>
178
+ </div>
179
+ <div class="debug-info">
180
+ ${JSON.stringify(data, null, 2)}
181
+ </div>
182
+ `;
183
+ } else {
184
+ saveResultsDiv.innerHTML = `
185
+ <div class="alert alert-danger">
186
+ <h5 class="status-error">❌ 保存测试失败</h5>
187
+ <p><strong>错误:</strong> ${data.message}</p>
188
+ ${data.external_error ? '<p><strong>外部错误:</strong> ' + data.external_error + '</p>' : ''}
189
+ </div>
190
+ <div class="debug-info">
191
+ ${JSON.stringify(data, null, 2)}
192
+ </div>
193
+ `;
194
+ }
195
+ })
196
+ .catch(error => {
197
+ console.error('Save test error:', error);
198
+ saveResultsDiv.innerHTML = `
199
+ <div class="alert alert-danger">
200
+ <h5 class="status-error">❌ 保存测试异常</h5>
201
+ <p>请求失败: ${error.message}</p>
202
+ </div>
203
+ `;
204
+ });
205
+ }
206
+ </script>
207
+ </body>
208
+ </html>
save.php CHANGED
@@ -8,7 +8,8 @@ You may obtain a copy of the License at
8
 
9
  http://www.apache.org/licenses/LICENSE-2.0
10
 
11
- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
  See the License for the specific language governing permissions and
14
  limitations under the License.
@@ -20,6 +21,12 @@ https://github.com/givanz/VvvebJs
20
  require_once __DIR__ . '/storage.php';
21
  require_once __DIR__ . '/user-manager.php';
22
 
 
 
 
 
 
 
23
  // Authentication check
24
  function checkAuth() {
25
  $userManager = new UserManager();
@@ -29,6 +36,18 @@ function checkAuth() {
29
  return true;
30
  }
31
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  // No authentication found - redirect to index.html which is the login page
33
  header('Location: index.html');
34
  exit;
@@ -104,6 +123,14 @@ function sanitizeFileName($file, $allowedExtension = 'html') {
104
  }
105
 
106
  function showError($error) {
 
 
 
 
 
 
 
 
107
  header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
108
  die($error);
109
  }
@@ -333,7 +360,11 @@ if ($action) {
333
  }
334
  break;
335
  default:
336
- showError("Invalid action '$action'!");
 
 
 
 
337
  }
338
  } else {
339
  //save page
@@ -343,10 +374,11 @@ if ($action) {
343
  $relativePath = str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $file);
344
  $relativePath = str_replace('\\', '/', $relativePath);
345
 
346
- // Debug logging
347
  error_log("VvvebJs Save Debug - File: $relativePath");
348
  error_log("VvvebJs Save Debug - HTML length: " . strlen($html));
349
  error_log("VvvebJs Save Debug - Storage type: " . StorageConfig::getStorageType());
 
350
 
351
  // Try external storage first
352
  $externalSuccess = false;
@@ -366,13 +398,8 @@ if ($action) {
366
  $dir = dirname($file);
367
  $localSuccess = false;
368
  if (!is_dir($dir)) {
369
- echo "$dir folder does not exist\n";
370
  if (mkdir($dir, 0777, true)) {
371
- echo "$dir folder was created\n";
372
- } else {
373
- if (!$externalSuccess) {
374
- showError("Error creating folder '$dir'\n");
375
- }
376
  }
377
  }
378
 
@@ -380,7 +407,38 @@ if ($action) {
380
  $localSuccess = file_put_contents($file, $html);
381
  }
382
 
383
- // Detailed response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  if ($externalSuccess || $localSuccess) {
385
  $message = "File saved '$file'";
386
  if ($externalSuccess) {
 
8
 
9
  http://www.apache.org/licenses/LICENSE-2.0
10
 
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
  See the License for the specific language governing permissions and
15
  limitations under the License.
 
21
  require_once __DIR__ . '/storage.php';
22
  require_once __DIR__ . '/user-manager.php';
23
 
24
+ // Enhanced error logging for debugging
25
+ error_log("VvvebJs Save Request - Method: " . ($_SERVER['REQUEST_METHOD'] ?? 'unknown'));
26
+ error_log("VvvebJs Save Request - Action: " . ($_GET['action'] ?? $_POST['action'] ?? 'none'));
27
+ error_log("VvvebJs Save Request - Has HTML: " . (isset($_POST['html']) ? 'yes' : 'no'));
28
+ error_log("VvvebJs Save Request - Has File: " . (isset($_POST['file']) ? 'yes' : 'no'));
29
+
30
  // Authentication check
31
  function checkAuth() {
32
  $userManager = new UserManager();
 
36
  return true;
37
  }
38
 
39
+ // Enhanced auth error logging
40
+ error_log("VvvebJs Auth Failed - Session logged in: " . ($userManager->isLoggedIn() ? 'yes' : 'no'));
41
+ error_log("VvvebJs Auth Failed - Session data: " . json_encode($_SESSION ?? []));
42
+
43
+ // For AJAX requests, return JSON error instead of redirect
44
+ if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
45
+ header('Content-Type: application/json');
46
+ http_response_code(401);
47
+ echo json_encode(['success' => false, 'message' => 'Authentication required', 'redirect' => 'index.html']);
48
+ exit;
49
+ }
50
+
51
  // No authentication found - redirect to index.html which is the login page
52
  header('Location: index.html');
53
  exit;
 
123
  }
124
 
125
  function showError($error) {
126
+ // Enhanced error response for AJAX requests
127
+ if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
128
+ header('Content-Type: application/json');
129
+ http_response_code(500);
130
+ echo json_encode(['success' => false, 'message' => $error]);
131
+ exit;
132
+ }
133
+
134
  header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
135
  die($error);
136
  }
 
360
  }
361
  break;
362
  default:
363
+ error_log("VvvebJs Invalid Action: '$action'");
364
+ header('Content-Type: application/json');
365
+ http_response_code(400);
366
+ echo json_encode(['success' => false, 'message' => "Invalid action '$action'"]);
367
+ exit;
368
  }
369
  } else {
370
  //save page
 
374
  $relativePath = str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $file);
375
  $relativePath = str_replace('\\', '/', $relativePath);
376
 
377
+ // Enhanced debug logging
378
  error_log("VvvebJs Save Debug - File: $relativePath");
379
  error_log("VvvebJs Save Debug - HTML length: " . strlen($html));
380
  error_log("VvvebJs Save Debug - Storage type: " . StorageConfig::getStorageType());
381
+ error_log("VvvebJs Save Debug - Current user: " . $storageManager->getCurrentUser());
382
 
383
  // Try external storage first
384
  $externalSuccess = false;
 
398
  $dir = dirname($file);
399
  $localSuccess = false;
400
  if (!is_dir($dir)) {
 
401
  if (mkdir($dir, 0777, true)) {
402
+ error_log("VvvebJs: Created directory $dir");
 
 
 
 
403
  }
404
  }
405
 
 
407
  $localSuccess = file_put_contents($file, $html);
408
  }
409
 
410
+ // Enhanced JSON response for AJAX requests
411
+ if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
412
+ header('Content-Type: application/json');
413
+
414
+ if ($externalSuccess || $localSuccess) {
415
+ echo json_encode([
416
+ 'success' => true,
417
+ 'message' => 'File saved successfully',
418
+ 'file' => $relativePath,
419
+ 'external_storage' => $externalSuccess,
420
+ 'local_cache' => $localSuccess !== false,
421
+ 'user' => $storageManager->getCurrentUser()
422
+ ]);
423
+ } else {
424
+ $github = StorageConfig::getGitHubConfig();
425
+ echo json_encode([
426
+ 'success' => false,
427
+ 'message' => 'Failed to save file',
428
+ 'external_error' => $externalError,
429
+ 'debug_info' => [
430
+ 'has_token' => !empty($github['token']),
431
+ 'owner' => $github['owner'],
432
+ 'repo' => $github['repo'],
433
+ 'target_file' => $relativePath,
434
+ 'user' => $storageManager->getCurrentUser()
435
+ ]
436
+ ]);
437
+ }
438
+ exit;
439
+ }
440
+
441
+ // Traditional text response for non-AJAX requests
442
  if ($externalSuccess || $localSuccess) {
443
  $message = "File saved '$file'";
444
  if ($externalSuccess) {