-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
351 lines (306 loc) · 9.71 KB
/
server.js
File metadata and controls
351 lines (306 loc) · 9.71 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const { exec } = require('child_process');
const app = express();
const PORT = process.env.PORT || 3000;
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production';
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Database setup
const db = new sqlite3.Database('codingapp.db');
// Initialize database tables
db.serialize(() => {
// Users table
db.run(`CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`);
// Projects table
db.run(`CREATE TABLE IF NOT EXISTS projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
title TEXT NOT NULL,
html TEXT DEFAULT '',
css TEXT DEFAULT '',
js TEXT DEFAULT '',
php TEXT DEFAULT '',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id)
)`);
// Create projects directory if it doesn't exist
if (!fs.existsSync('projects')) {
fs.mkdirSync('projects');
}
});
// Authentication middleware
const authenticateToken = (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Access token required' });
}
jwt.verify(token, JWT_SECRET, (err, user) => {
if (err) {
return res.status(403).json({ error: 'Invalid token' });
}
req.user = user;
next();
});
};
// Routes
// Register user
app.post('/api/register', async (req, res) => {
try {
const { username, email, password } = req.body;
if (!username || !email || !password) {
return res.status(400).json({ error: 'All fields are required' });
}
const hashedPassword = await bcrypt.hash(password, 10);
db.run(
'INSERT INTO users (username, email, password) VALUES (?, ?, ?)',
[username, email, hashedPassword],
function(err) {
if (err) {
if (err.message.includes('UNIQUE constraint failed')) {
return res.status(400).json({ error: 'Username or email already exists' });
}
return res.status(500).json({ error: 'Database error' });
}
const token = jwt.sign({ id: this.lastID, username }, JWT_SECRET);
res.json({ token, user: { id: this.lastID, username, email } });
}
);
} catch (error) {
res.status(500).json({ error: 'Server error' });
}
});
// Login user
app.post('/api/login', (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password required' });
}
db.get('SELECT * FROM users WHERE username = ?', [username], async (err, user) => {
if (err) {
return res.status(500).json({ error: 'Database error' });
}
if (!user) {
return res.status(401).json({ error: 'Invalid credentials' });
}
const validPassword = await bcrypt.compare(password, user.password);
if (!validPassword) {
return res.status(401).json({ error: 'Invalid credentials' });
}
const token = jwt.sign({ id: user.id, username: user.username }, JWT_SECRET);
res.json({ token, user: { id: user.id, username: user.username, email: user.email } });
});
});
// Get user projects
app.get('/api/projects', authenticateToken, (req, res) => {
db.all(
'SELECT id, title, created_at, updated_at FROM projects WHERE user_id = ? ORDER BY updated_at DESC',
[req.user.id],
(err, projects) => {
if (err) {
return res.status(500).json({ error: 'Database error' });
}
res.json(projects);
}
);
});
// Get single project
app.get('/api/projects/:id', authenticateToken, (req, res) => {
db.get(
'SELECT * FROM projects WHERE id = ? AND user_id = ?',
[req.params.id, req.user.id],
(err, project) => {
if (err) {
return res.status(500).json({ error: 'Database error' });
}
if (!project) {
return res.status(404).json({ error: 'Project not found' });
}
res.json(project);
}
);
});
// Create new project
app.post('/api/projects', authenticateToken, (req, res) => {
const { title, html, css, js, php } = req.body;
if (!title) {
return res.status(400).json({ error: 'Project title is required' });
}
db.run(
'INSERT INTO projects (user_id, title, html, css, js, php) VALUES (?, ?, ?, ?, ?, ?)',
[req.user.id, title, html || '', css || '', js || '', php || ''],
function(err) {
if (err) {
return res.status(500).json({ error: 'Database error' });
}
res.json({ id: this.lastID, title, html, css, js, php });
}
);
});
// Update project
app.put('/api/projects/:id', authenticateToken, (req, res) => {
const { title, html, css, js, php } = req.body;
const projectId = req.params.id;
db.run(
'UPDATE projects SET title = ?, html = ?, css = ?, js = ?, php = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? AND user_id = ?',
[title, html, css, js, php, projectId, req.user.id],
function(err) {
if (err) {
return res.status(500).json({ error: 'Database error' });
}
if (this.changes === 0) {
return res.status(404).json({ error: 'Project not found' });
}
res.json({ id: projectId, title, html, css, js, php });
}
);
});
// Delete project
app.delete('/api/projects/:id', authenticateToken, (req, res) => {
db.run(
'DELETE FROM projects WHERE id = ? AND user_id = ?',
[req.params.id, req.user.id],
function(err) {
if (err) {
return res.status(500).json({ error: 'Database error' });
}
if (this.changes === 0) {
return res.status(404).json({ error: 'Project not found' });
}
res.json({ message: 'Project deleted successfully' });
}
);
});
// Execute PHP code
app.post('/api/execute-php', authenticateToken, (req, res) => {
const { html, css, js, php } = req.body;
// Create a temporary PHP file
const tempDir = path.join(__dirname, 'temp');
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}
const tempFile = path.join(tempDir, `temp_${Date.now()}.php`);
// Create the complete PHP file with HTML, CSS, JS, and PHP
const phpContent = `<?php
// PHP code execution
${php || ''}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Preview</title>
<style>
${css || ''}
</style>
</head>
<body>
${html || ''}
<script>
${js || ''}
</script>
</body>
</html>`;
try {
fs.writeFileSync(tempFile, phpContent);
// Debug: Log the PHP content for troubleshooting
console.log('Generated PHP file content:');
console.log(phpContent);
// Execute PHP file
const { exec } = require('child_process');
exec(`php "${tempFile}"`, { timeout: 10000 }, (error, stdout, stderr) => {
// Clean up temp file
try {
fs.unlinkSync(tempFile);
} catch (cleanupError) {
console.error('Failed to cleanup temp file:', cleanupError);
}
if (error) {
console.error('PHP execution error:', error);
console.error('PHP stdout:', stdout);
console.error('PHP stderr:', stderr);
return res.status(500).send(`PHP Execution Error: ${error.message}\n\nOutput: ${stdout}\n\nErrors: ${stderr}`);
}
if (stderr && stderr.trim() !== '') {
console.error('PHP stderr:', stderr);
// Only treat as error if stderr contains actual errors (not just warnings)
if (stderr.includes('Parse error') || stderr.includes('Fatal error') || stderr.includes('Warning:')) {
return res.status(500).send(`PHP Error: ${stderr}\n\nOutput: ${stdout}`);
}
}
res.setHeader('Content-Type', 'text/html');
res.send(stdout);
});
} catch (error) {
// Clean up temp file if it exists
try {
if (fs.existsSync(tempFile)) {
fs.unlinkSync(tempFile);
}
} catch (cleanupError) {
console.error('Failed to cleanup temp file:', cleanupError);
}
console.error('File write error:', error);
res.status(500).send(`File Error: ${error.message}`);
}
});
// Export project as ZIP
app.get('/api/projects/:id/export', authenticateToken, (req, res) => {
db.get(
'SELECT * FROM projects WHERE id = ? AND user_id = ?',
[req.params.id, req.user.id],
(err, project) => {
if (err) {
return res.status(500).json({ error: 'Database error' });
}
if (!project) {
return res.status(404).json({ error: 'Project not found' });
}
// Create HTML file content
const htmlContent = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${project.title}</title>
<style>
${project.css}
</style>
</head>
<body>
${project.html}
<script>
${project.js}
</script>
</body>
</html>`;
res.setHeader('Content-Type', 'text/html');
res.setHeader('Content-Disposition', `attachment; filename="${project.title}.html"`);
res.send(htmlContent);
}
);
});
// Serve the main application
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Start server
app.listen(PORT, () => {
console.log(`CodingApp server running on http://localhost:${PORT}`);
});