CatPtain commited on
Commit
88dde93
·
verified ·
1 Parent(s): 5186047

Upload 7 files

Browse files
Files changed (4) hide show
  1. editor.html +103 -51
  2. editor.php +148 -30
  3. save.php +2 -2
  4. user-manager.php +1 -1
editor.html CHANGED
@@ -13,84 +13,133 @@
13
  <link href="css/editor.css" rel="stylesheet">
14
 
15
  <script>
16
- // Add authentication check for save operations
17
  window.addEventListener('DOMContentLoaded', function() {
18
- // Override the default save function to ensure authentication
19
- if (typeof Vvveb !== 'undefined') {
20
- const originalSaveAjax = Vvveb.Builder.saveAjax;
21
- Vvveb.Builder.saveAjax = function(data, saveUrl, callback, error) {
22
- // The authentication is handled server-side via HTTP Basic Auth
23
- return originalSaveAjax.call(this, data, saveUrl, callback, error);
24
- };
25
- }
26
-
27
- // Check if we need authentication for the editor
28
  fetch('save.php?action=checkAuth', {
29
  method: 'GET',
30
  credentials: 'include'
31
  }).then(response => {
32
  if (response.status === 401) {
33
- // Redirect to config page which will handle authentication
34
- window.location.href = 'config.php';
 
35
  }
 
 
 
36
  }).catch(err => {
37
- console.log('Auth check failed, continuing anyway');
 
 
38
  });
39
-
40
- // File loading functionality
41
  function loadFileList() {
42
- fetch('save.php?action=list')
43
  .then(response => response.json())
44
  .then(data => {
 
45
  const selector = document.getElementById('file-selector');
46
- selector.innerHTML = '<option value="">Select a file...</option>';
47
-
48
- if (data.success && data.files) {
49
- data.files.forEach(file => {
50
- const option = document.createElement('option');
51
- option.value = file.filename;
52
- option.textContent = file.title || file.filename;
53
- selector.appendChild(option);
54
- });
 
 
 
 
 
 
 
55
  }
56
  })
57
- .catch(error => console.error('Error loading file list:', error));
 
 
58
  }
59
 
60
- // Load file when selected
61
- document.getElementById('load-file-btn').addEventListener('click', function() {
62
- const selector = document.getElementById('file-selector');
63
- const filename = selector.value;
64
-
65
  if (!filename) {
66
  alert('Please select a file to load');
67
  return;
68
  }
69
 
70
- fetch(`save.php?action=load&filename=${encodeURIComponent(filename)}`)
 
 
71
  .then(response => response.json())
72
  .then(data => {
 
73
  if (data.success) {
74
- // Load the HTML content into the editor
75
- Vvveb.Builder.loadHtml(data.html);
76
- // Update the page title input if it exists
77
- const titleInput = document.getElementById('page-title');
78
- if (titleInput && data.title) {
79
- titleInput.value = data.title;
 
 
 
 
 
 
 
 
 
 
 
80
  }
81
- console.log('File loaded successfully');
82
  } else {
83
  alert('Error loading file: ' + (data.message || 'Unknown error'));
84
  }
85
  })
86
  .catch(error => {
87
  console.error('Error loading file:', error);
88
- alert('Error loading file');
89
  });
90
- });
91
 
92
- // Load file list on page load
93
- loadFileList();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  });
95
  </script>
96
  </head>
@@ -124,14 +173,17 @@
124
  </button>
125
  </div>
126
 
127
- <!-- File loading dropdown -->
128
  <div class="btn-group me-3" role="group">
129
- <select class="form-select" id="file-selector" title="Select file to load">
130
- <option value="">Select a file...</option>
131
  </select>
132
- <button class="btn btn-light" title="Load selected file" id="load-file-btn">
133
  <i class="la la-folder-open"></i>
134
  </button>
 
 
 
135
  </div>
136
 
137
  <div class="btn-group me-3" role="group">
@@ -159,16 +211,16 @@
159
 
160
 
161
  <div class="btn-group me-2 float-end" role="group">
162
- <button class="btn btn-primary btn-sm btn-icon save-btn" title="Save (Ctrl + S)" id="save-btn" data-vvveb-action="saveAjax" data-vvveb-url="save.php" data-v-vvveb-shortcut="ctrl+e">
163
 
164
  <span class="loading d-none">
165
  <i class="icon-save-outline"></i>
166
  <span class="spinner-border spinner-border-sm align-middle" role="status" aria-hidden="true">
167
  </span>
168
- <span>Saving </span> ... </span>
169
 
170
  <span class="button-text">
171
- <i class="icon-save-outline"></i> <span>Save page</span>
172
  </span>
173
 
174
  </button>
 
13
  <link href="css/editor.css" rel="stylesheet">
14
 
15
  <script>
16
+ // Enhanced authentication and file loading for GitHub integration
17
  window.addEventListener('DOMContentLoaded', function() {
18
+ // Check authentication first
 
 
 
 
 
 
 
 
 
19
  fetch('save.php?action=checkAuth', {
20
  method: 'GET',
21
  credentials: 'include'
22
  }).then(response => {
23
  if (response.status === 401) {
24
+ // Redirect to login page
25
+ window.location.href = '/';
26
+ return;
27
  }
28
+
29
+ // If authenticated, load file list
30
+ loadFileList();
31
  }).catch(err => {
32
+ console.log('Auth check failed:', err);
33
+ // Redirect to login page on error
34
+ window.location.href = '/';
35
  });
36
+
37
+ // GitHub-aware file loading functionality
38
  function loadFileList() {
39
+ fetch('save.php?action=listFiles')
40
  .then(response => response.json())
41
  .then(data => {
42
+ console.log('File list response:', data);
43
  const selector = document.getElementById('file-selector');
44
+ if (selector) {
45
+ selector.innerHTML = '<option value="">Select a file...</option>';
46
+
47
+ if (data.success && data.files && data.files.length > 0) {
48
+ data.files.forEach(file => {
49
+ const option = document.createElement('option');
50
+ option.value = file.name || file.filename || file.path;
51
+ option.textContent = file.name || file.filename || 'Untitled';
52
+ selector.appendChild(option);
53
+ });
54
+
55
+ // Show user info
56
+ console.log(`Loaded ${data.files.length} files for user: ${data.user}`);
57
+ } else {
58
+ console.log('No files found or error loading files');
59
+ }
60
  }
61
  })
62
+ .catch(error => {
63
+ console.error('Error loading file list:', error);
64
+ });
65
  }
66
 
67
+ // Enhanced file loading with GitHub support
68
+ function loadFileFromGitHub(filename) {
 
 
 
69
  if (!filename) {
70
  alert('Please select a file to load');
71
  return;
72
  }
73
 
74
+ console.log('Loading file from GitHub:', filename);
75
+
76
+ fetch(`save.php?action=loadFile&file=${encodeURIComponent(filename)}`)
77
  .then(response => response.json())
78
  .then(data => {
79
+ console.log('Load file response:', data);
80
  if (data.success) {
81
+ // Use VvvebJs API to load HTML content
82
+ if (typeof Vvveb !== 'undefined' && Vvveb.Builder) {
83
+ Vvveb.Builder.loadHtml(data.content);
84
+ console.log('File loaded successfully from GitHub');
85
+
86
+ // Update current file reference
87
+ window.currentFile = filename;
88
+
89
+ // Update page title if available
90
+ const titleMatch = data.content.match(/<title>(.*?)<\/title>/i);
91
+ if (titleMatch) {
92
+ const titleInput = document.querySelector("input[name=title]");
93
+ if (titleInput) titleInput.value = titleMatch[1];
94
+ }
95
+ } else {
96
+ console.warn('VvvebJs not ready yet, retrying...');
97
+ setTimeout(() => loadFileFromGitHub(filename), 1000);
98
  }
 
99
  } else {
100
  alert('Error loading file: ' + (data.message || 'Unknown error'));
101
  }
102
  })
103
  .catch(error => {
104
  console.error('Error loading file:', error);
105
+ alert('Error loading file from GitHub');
106
  });
107
+ }
108
 
109
+ // Load file when selected from dropdown
110
+ const loadFileBtn = document.getElementById('load-file-btn');
111
+ if (loadFileBtn) {
112
+ loadFileBtn.addEventListener('click', function() {
113
+ const selector = document.getElementById('file-selector');
114
+ const filename = selector ? selector.value : '';
115
+ loadFileFromGitHub(filename);
116
+ });
117
+ }
118
+
119
+ // Make functions globally available
120
+ window.loadFileList = loadFileList;
121
+ window.loadFileFromGitHub = loadFileFromGitHub;
122
+
123
+ // Override the default save function to ensure proper authentication
124
+ window.setTimeout(() => {
125
+ if (typeof Vvveb !== 'undefined' && Vvveb.Builder) {
126
+ const originalSaveAjax = Vvveb.Builder.saveAjax;
127
+ Vvveb.Builder.saveAjax = function(data, saveUrl, callback, error) {
128
+ console.log('Saving to GitHub via:', saveUrl);
129
+ return originalSaveAjax.call(this, data, saveUrl, callback, error);
130
+ };
131
+
132
+ // Add file refresh functionality
133
+ const originalCallback = Vvveb.Builder.saveAjax;
134
+ Vvveb.Builder.saveAjax = function(data, saveUrl, callback, error) {
135
+ return originalSaveAjax.call(this, data, saveUrl, function(result) {
136
+ if (callback) callback(result);
137
+ // Refresh file list after save
138
+ setTimeout(loadFileList, 1000);
139
+ }, error);
140
+ };
141
+ }
142
+ }, 2000);
143
  });
144
  </script>
145
  </head>
 
173
  </button>
174
  </div>
175
 
176
+ <!-- Enhanced File loading dropdown with GitHub support -->
177
  <div class="btn-group me-3" role="group">
178
+ <select class="form-select" id="file-selector" title="Select file to load from GitHub">
179
+ <option value="">Loading files...</option>
180
  </select>
181
+ <button class="btn btn-light" title="Load selected file from GitHub" id="load-file-btn">
182
  <i class="la la-folder-open"></i>
183
  </button>
184
+ <button class="btn btn-light" title="Refresh file list" onclick="window.loadFileList && window.loadFileList()">
185
+ <i class="la la-refresh"></i>
186
+ </button>
187
  </div>
188
 
189
  <div class="btn-group me-3" role="group">
 
211
 
212
 
213
  <div class="btn-group me-2 float-end" role="group">
214
+ <button class="btn btn-primary btn-sm btn-icon save-btn" title="Save to GitHub (Ctrl + S)" id="save-btn" data-vvveb-action="saveAjax" data-vvveb-url="save.php" data-v-vvveb-shortcut="ctrl+e">
215
 
216
  <span class="loading d-none">
217
  <i class="icon-save-outline"></i>
218
  <span class="spinner-border spinner-border-sm align-middle" role="status" aria-hidden="true">
219
  </span>
220
+ <span>Saving to GitHub</span> ... </span>
221
 
222
  <span class="button-text">
223
+ <i class="icon-save-outline"></i> <span>Save to GitHub</span>
224
  </span>
225
 
226
  </button>
editor.php CHANGED
@@ -1,61 +1,179 @@
1
  <?php
2
  require_once 'user-manager.php';
 
3
 
4
  // Initialize user manager and require login
5
  $userManager = new UserManager();
6
- $userManager->requireLogin();
 
 
 
7
 
8
  $currentUser = $userManager->getCurrentUser();
9
 
 
 
 
10
  // Get the editor HTML
11
  $html = file_get_contents('editor.html');
12
 
13
- // Search for HTML files in user's directory and demo folder
14
- $userDir = "user-files/$currentUser";
15
  $userFiles = [];
16
- if (is_dir($userDir)) {
17
- $userFiles = glob("$userDir/*.html");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  }
19
 
20
  // Also include demo files for reference
21
  $demoFiles = array_merge(glob('demo/*/*.html'), glob('demo/*.html'));
22
 
23
- $files = '';
24
-
25
- // Add user files first
26
- foreach ($userFiles as $file) {
27
  if (in_array(basename($file), array('new-page-blank-template.html', 'editor.html'))) continue;
28
  $pathInfo = pathinfo($file);
29
  $filename = $pathInfo['filename'];
30
- $folder = 'My Files';
31
  $url = $file;
32
  $name = $filename;
33
  $title = ucfirst($name);
34
- $files .= "{name:'$name', file:'$file', title:'$title', url: '$url', folder:'$folder', editable: true},";
 
 
 
 
 
35
  }
36
 
37
- // Add demo files (read-only)
38
- foreach ($demoFiles as $file) {
39
- if (in_array($file, array('new-page-blank-template.html', 'editor.html'))) continue;
40
- $pathInfo = pathinfo($file);
41
- $filename = $pathInfo['filename'];
42
- $folder = preg_replace('@/.+?$@', '', $pathInfo['dirname']);
43
- $subfolder = preg_replace('@^.+?/@', '', $pathInfo['dirname']);
44
- if ($filename == 'index' && $subfolder) {
45
- $filename = $subfolder;
46
- }
47
- $url = $pathInfo['dirname'] . '/' . $pathInfo['basename'];
48
- $name = $filename;
49
- $title = ucfirst($name) . ' (Demo)';
50
- $files .= "{name:'$name', file:'$file', title:'$title', url: '$url', folder:'$folder', editable: false},";
51
  }
 
 
 
 
 
 
 
52
 
53
- // Replace files list from html with the dynamic list
54
- $html = str_replace('= defaultPages;', " = [$files];", $html);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- // Add user info to the page
57
- $userInfo = "<script>window.currentUser = '$currentUser';</script>";
58
- $html = str_replace('</head>', "$userInfo</head>", $html);
 
59
 
60
  echo $html;
61
  ?>
 
1
  <?php
2
  require_once 'user-manager.php';
3
+ require_once 'storage.php';
4
 
5
  // Initialize user manager and require login
6
  $userManager = new UserManager();
7
+ if (!$userManager->isLoggedIn()) {
8
+ header('Location: index.html');
9
+ exit;
10
+ }
11
 
12
  $currentUser = $userManager->getCurrentUser();
13
 
14
+ // Initialize storage manager for GitHub file access
15
+ $storageManager = new StorageManager();
16
+
17
  // Get the editor HTML
18
  $html = file_get_contents('editor.html');
19
 
20
+ // Get user files from GitHub storage
21
+ $files = '';
22
  $userFiles = [];
23
+
24
+ try {
25
+ $githubFiles = $storageManager->listFiles();
26
+
27
+ // Add user files from GitHub
28
+ foreach ($githubFiles as $file) {
29
+ if (in_array($file['name'], array('new-page-blank-template.html', 'editor.html'))) continue;
30
+
31
+ $pathInfo = pathinfo($file['name']);
32
+ $filename = $pathInfo['filename'];
33
+ $folder = 'My Files (GitHub)';
34
+ $url = $file['path'];
35
+ $name = $filename;
36
+ $title = ucfirst($name);
37
+
38
+ $files .= '<li class="file" data-url="' . htmlspecialchars($url) . '" data-folder="' . htmlspecialchars($folder) . '">';
39
+ $files .= '<label class="folder-name">' . htmlspecialchars($folder) . '</label>';
40
+ $files .= '<div class="file-name"><span>' . htmlspecialchars($title) . '</span> <div class="file-actions">';
41
+ $files .= '<button class="btn btn-sm btn-light load-file-btn" data-file="' . htmlspecialchars($url) . '">Load</button>';
42
+ $files .= '</div></div></li>';
43
+
44
+ $userFiles[] = [
45
+ 'filename' => $url,
46
+ 'title' => $title,
47
+ 'folder' => $folder
48
+ ];
49
+ }
50
+ } catch (Exception $e) {
51
+ error_log("Editor: Error loading GitHub files: " . $e->getMessage());
52
+ }
53
+
54
+ // Fallback: check local user files as backup
55
+ $localUserDir = "user-files/$currentUser";
56
+ if (is_dir($localUserDir)) {
57
+ $localFiles = glob("$localUserDir/*.html");
58
+
59
+ foreach ($localFiles as $file) {
60
+ if (in_array(basename($file), array('new-page-blank-template.html', 'editor.html'))) continue;
61
+ $pathInfo = pathinfo($file);
62
+ $filename = $pathInfo['filename'];
63
+ $folder = 'Local Files';
64
+ $url = str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $file);
65
+ $url = str_replace('\\', '/', $url);
66
+ $name = $filename;
67
+ $title = ucfirst($name);
68
+
69
+ $files .= '<li class="file" data-url="' . htmlspecialchars($url) . '" data-folder="' . htmlspecialchars($folder) . '">';
70
+ $files .= '<label class="folder-name">' . htmlspecialchars($folder) . '</label>';
71
+ $files .= '<div class="file-name"><span>' . htmlspecialchars($title) . '</span> <div class="file-actions">';
72
+ $files .= '<button class="btn btn-sm btn-light load-file-btn" data-file="' . htmlspecialchars($url) . '">Load</button>';
73
+ $files .= '</div></div></li>';
74
+
75
+ $userFiles[] = [
76
+ 'filename' => $url,
77
+ 'title' => $title,
78
+ 'folder' => $folder
79
+ ];
80
+ }
81
  }
82
 
83
  // Also include demo files for reference
84
  $demoFiles = array_merge(glob('demo/*/*.html'), glob('demo/*.html'));
85
 
86
+ foreach ($demoFiles as $file) {
 
 
 
87
  if (in_array(basename($file), array('new-page-blank-template.html', 'editor.html'))) continue;
88
  $pathInfo = pathinfo($file);
89
  $filename = $pathInfo['filename'];
90
+ $folder = ucfirst(basename(dirname($file)));
91
  $url = $file;
92
  $name = $filename;
93
  $title = ucfirst($name);
94
+
95
+ $files .= '<li class="file" data-url="' . htmlspecialchars($url) . '" data-folder="' . htmlspecialchars($folder) . '">';
96
+ $files .= '<label class="folder-name">' . htmlspecialchars($folder) . ' (Demo)</label>';
97
+ $files .= '<div class="file-name"><span>' . htmlspecialchars($title) . '</span> <div class="file-actions">';
98
+ $files .= '<button class="btn btn-sm btn-light load-file-btn" data-file="' . htmlspecialchars($url) . '">Load</button>';
99
+ $files .= '</div></div></li>';
100
  }
101
 
102
+ // Add user info and file list to the HTML
103
+ $userInfo = '<div class="user-info alert alert-info">';
104
+ $userInfo .= '<strong>当前用户:</strong> ' . htmlspecialchars($currentUser);
105
+ $userInfo .= '<br><strong>存储:</strong> ' . StorageConfig::getStorageType();
106
+ $userInfo .= '<br><strong>GitHub文件:</strong> ' . count($githubFiles ?? []) . '';
107
+ if (isset($localFiles)) {
108
+ $userInfo .= '<br><strong>本地缓存:</strong> ' . count($localFiles) . '';
 
 
 
 
 
 
 
109
  }
110
+ $userInfo .= '</div>';
111
+
112
+ // Inject JavaScript for file management
113
+ $fileManagementScript = '
114
+ <script>
115
+ window.vvvebUserFiles = ' . json_encode($userFiles) . ';
116
+ window.vvvebCurrentUser = "' . htmlspecialchars($currentUser) . '";
117
 
118
+ // Enhanced file loading with GitHub support
119
+ function loadFileFromGitHub(filename) {
120
+ console.log("Loading file from GitHub:", filename);
121
+
122
+ fetch("save.php?action=loadFile&file=" + encodeURIComponent(filename))
123
+ .then(response => response.json())
124
+ .then(data => {
125
+ if (data.success) {
126
+ Vvveb.Builder.loadHtml(data.content);
127
+ console.log("File loaded successfully from GitHub");
128
+
129
+ // Update current file reference
130
+ window.currentFile = filename;
131
+
132
+ // Update page title if available
133
+ const titleMatch = data.content.match(/<title>(.*?)<\/title>/i);
134
+ if (titleMatch) {
135
+ const titleInput = document.querySelector("input[name=title]");
136
+ if (titleInput) titleInput.value = titleMatch[1];
137
+ }
138
+ } else {
139
+ alert("Error loading file: " + (data.message || "Unknown error"));
140
+ }
141
+ })
142
+ .catch(error => {
143
+ console.error("Error loading file:", error);
144
+ alert("Error loading file from GitHub");
145
+ });
146
+ }
147
+
148
+ // Override the existing file loading
149
+ document.addEventListener("DOMContentLoaded", function() {
150
+ // Add enhanced file loading for GitHub files
151
+ document.querySelectorAll(".load-file-btn").forEach(btn => {
152
+ btn.addEventListener("click", function() {
153
+ const filename = this.getAttribute("data-file");
154
+ loadFileFromGitHub(filename);
155
+ });
156
+ });
157
+
158
+ // Add refresh button for file list
159
+ const refreshBtn = document.createElement("button");
160
+ refreshBtn.className = "btn btn-sm btn-secondary";
161
+ refreshBtn.innerHTML = "🔄 刷新文件列表";
162
+ refreshBtn.onclick = function() {
163
+ location.reload();
164
+ };
165
+
166
+ const fileManager = document.querySelector("#filemanager .file-manager-header");
167
+ if (fileManager) {
168
+ fileManager.appendChild(refreshBtn);
169
+ }
170
+ });
171
+ </script>';
172
 
173
+ // Insert user info and files into the HTML
174
+ $html = str_replace('<!--files-li-->', $files, $html);
175
+ $html = str_replace('</head>', $fileManagementScript . '</head>', $html);
176
+ $html = str_replace('<div id="filemanager">', '<div id="filemanager">' . $userInfo, $html);
177
 
178
  echo $html;
179
  ?>
save.php CHANGED
@@ -29,8 +29,8 @@ function checkAuth() {
29
  return true;
30
  }
31
 
32
- // No authentication found
33
- header('Location: login.html');
34
  exit;
35
  }
36
 
 
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;
35
  }
36
 
user-manager.php CHANGED
@@ -44,7 +44,7 @@ class UserManager {
44
 
45
  public function requireLogin() {
46
  if (!$this->isLoggedIn()) {
47
- header('Location: login.html');
48
  exit;
49
  }
50
  }
 
44
 
45
  public function requireLogin() {
46
  if (!$this->isLoggedIn()) {
47
+ header('Location: index.html');
48
  exit;
49
  }
50
  }