htdocs/node/app.js

132 lines
3.8 KiB
JavaScript
Raw Normal View History

2024-10-31 14:52:17 +01:00
const express = require('express');
const app = express();
const port = 8003
const path = require("path")
//let comments = require("/srv/www/htdocs/blog/comments.js")
const fs = require("fs")
const validator = require('validator');
2024-11-02 02:03:32 +01:00
const rootPath = "/var/www/html/"
2024-11-09 13:32:43 +01:00
const { exec } = require("child_process");
2024-10-31 14:52:17 +01:00
const escapeHtml = (unsafe) => {
return unsafe
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
2024-11-17 00:47:14 +01:00
.replace(/'/g, "&#039;")
};
const encode = (unsafe) => {
console.log(unsafe)
return unsafe
// Bold
.replace(/\*\*(.*?)\*\*/g,'<b>$1</b>')
// Italics
.replace(/\*(.*?)\*/g,'<i>$1</i>')
2024-11-17 23:23:37 +01:00
//links
.replace(/\[(.*?)\]\((.*?)\)/g,'<a href="$2">$1</a>')
2024-10-31 14:52:17 +01:00
};
// Middleware to parse URL-encoded and JSON bodies
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
2024-11-09 13:32:43 +01:00
app.post("/music/playpause",(req,res) => {
console.log("toggling pause!")
exec('echo cycle pause | socat - "/tmp/mpvsocket"');
res.redirect(302, req.get("referer"));
});
app.post("/music/next",(req,res) => {
console.log("skipping to next song")
exec('echo playlist-next | socat - "/tmp/mpvsocket"')
setTimeout(function() {
res.redirect(302, req.get("referer"));
}, 1000);
});
app.post("/music/prev",(req,res) => {
console.log("going back to previous song")
exec('echo playlist-prev | socat - "/tmp/mpvsocket"')
setTimeout(function() {
res.redirect(302, req.get("referer"));
}, 1000);
});
2024-11-15 22:16:58 +01:00
app.post("/board/submit-comment", (req,res) => {
res.redirect(302, req.get("referer"));
let comments = require(`${rootPath}/board/${req.body.pageID}/comments-database.js`)
console.log(req.get("referer"))
console.log(comments)
if (req.body.comment != "") {
if (req.body.name == "") {
req.body.name = "Anonymous";
}
2024-11-17 00:47:14 +01:00
comments.push([encode(escapeHtml(req.body.name)),encode(escapeHtml(req.body.comment))])
2024-11-15 22:16:58 +01:00
console.log(comments)
content = `let comments = ${JSON.stringify(comments)}
if (typeof module !== "undefined" && module.exports) {
module.exports = comments;
}`
fs.writeFile(`${rootPath}/board/${req.body.pageID}/comments-database.js`, content, (err) => {
if (err) {
console.log("Error writing comments to file");
}
else {
console.log("Wrote comments to file :)");
}
});
}
});
2024-10-31 14:52:17 +01:00
app.post("/blog/submit-comment",(req,res) => {
2024-11-02 02:03:32 +01:00
let jsonString = fs.readFileSync('/var/www/html/blog/comments-database.js', 'utf8');
2024-10-31 14:52:17 +01:00
let comments = JSON.parse(jsonString);
res.redirect(302, req.get("referer"));
console.log(req.get("referer"))
console.log(req.body.name)
if (req.body.comment !="") {
if (req.body.name == "") {
req.body.name = "Anonymous";
}
2024-11-17 00:47:14 +01:00
comments[req.body.pageID].push([encode(escapeHtml(req.body.name)),encode(escapeHtml(req.body.comment))])
2024-10-31 14:52:17 +01:00
content=`comments=${JSON.stringify(comments)}
if (typeof module !== "undefined" && module.exports) {
module.exports = comments;
}`
2024-11-02 02:03:32 +01:00
fs.writeFile("/var/www/html/blog/comments.js", content, (err) => {
2024-10-31 14:52:17 +01:00
if (err) {
console.error('Error creating file:', err);
} else {
2024-11-02 02:03:32 +01:00
console.log('File created written: comments.js');
2024-10-31 14:52:17 +01:00
}
});
motd_content = `
!ALERT!
${escapeHtml(req.body.name)} Commented:
${escapeHtml(req.body.comment)}
on the blog: https://deadvey.com/blog/blogs/${req.body.pageID}.html
`
fs.writeFile("/etc/motd", motd_content, (err) => {
if (err) {
console.error('Error creating file:', err);
} else {
console.log('File created written: Comments.js');
}
});
const jsonString = JSON.stringify(comments);
2024-11-02 02:03:32 +01:00
fs.writeFileSync('/var/www/html/blog/comments-database.js', jsonString, 'utf8');
2024-10-31 14:52:17 +01:00
}
console.log(comments)
});
app.use(express.static(rootPath));
// Start the server
app.listen(port, () => {
console.log(`Server is running at
http://localhost:${port}
in directory: ${rootPath}`);
});