diff --git a/app.js b/app.js index 4d9462f..7ce98b0 100755 --- a/app.js +++ b/app.js @@ -155,8 +155,7 @@ function hyperlink_tags(tags) { // the post index (int) as an optional paramter to indicate what post is to be used (for replacing things like content and titles) // the tag (strig) as an optional parameter to indicate what tag is being used (for /tag/:tag pages) // returns the template with it's format indiactors replaced (string) -function replace_format_indicators(template, post_index=0, tag_name="tag") { - post_object = posts.posts[post_index] // Defines the post object for easy reference +function replace_format_indicators(template, post_index=-1, tag_name="tag", user_index=-1) { output_string = template // These should always be replaceable .replaceAll("%%", "%") .replaceAll("%P", "/post") @@ -165,7 +164,9 @@ function replace_format_indicators(template, post_index=0, tag_name="tag") { .replaceAll("%Y", config.site_name) .replaceAll("%W", config.site_description) .replaceAll("%Z", config.attribution) - if (posts.posts.length > 0) { // These can only be replaced if there are more than 0 posts in the posts list + .replaceAll("%S", config.seperator) + if (post_index >= 0) { // These can only be replaced if a post is specified (by default the post id is -1) + post_object = posts.posts[post_index] // Defines the post object for easy reference output_string = output_string .replaceAll("%A", (post_object["tags"])) .replaceAll("%B", (hyperlink_tags(post_object["tags"]))) @@ -174,7 +175,7 @@ function replace_format_indicators(template, post_index=0, tag_name="tag") { .replaceAll("%E", unix_time_to_date_format(post_object["editdate"])) .replaceAll("%F", users.users[post_object["userID"]]['prettyname']) .replaceAll("%G", tag_name) - .replaceAll("%I", users.users[post_object['userID']]['description']) + .replaceAll("%I", converter.makeHtml(users.users[post_object['userID']]['description'])) .replaceAll("%L", `/post/${post_index}`) .replaceAll("%M", return_comments(post_index)) .replaceAll("%N", users.users[post_object["userID"]]['username']) @@ -187,11 +188,21 @@ function replace_format_indicators(template, post_index=0, tag_name="tag") {
`) - } - if (config.enable_hitcount == true) { // Finally, the hitcounter should only be replaced if config.enable_hitcount is true - output_string = output_string - .replaceAll("%H", fs.readFileSync('hitcount.txt')) - } + } + if (user_index >= 0) { // these should only be replaced if a user is specified (by default the user id is -1) + output_string = output_string + .replaceAll("%F", users.users[user_index]['prettyname']) + .replaceAll("%G", tag_name) + .replaceAll("%I", converter.makeHtml(users.users[user_index]['description'])) + .replaceAll("%L", `/post/${post_index}`) + .replaceAll("%N", users.users[user_index]['username']) + .replaceAll("%S", config.seperator) + .replaceAll("%U", `/user/${users.users[user_index]['username']}`) + } + if (config.enable_hitcount == true) { // Finally, the hitcounter should only be replaced if config.enable_hitcount is true + output_string = output_string + .replaceAll("%H", fs.readFileSync('hitcount.txt')) + } return output_string } @@ -220,6 +231,7 @@ function return_comments(post_id) { let comment = {...post_comments[comment_index]}; comment['content'] = comment['content'] .replaceAll(/>> ([0-9]*)/g, ">> $1") + .replaceAll(/>> ([0-9]*)/g, ">> $1") .replaceAll("\n", "
") comment_content += `
${comment['name']} ${unix_time_to_date_format(comment['pubdate'])} No. ${comment['id']}
${comment['content']}

` } @@ -282,7 +294,7 @@ app.get("/", (req,res) => { }); // / app.get("/user/:username", (req, res) => { header_div = config.user_page_header - header_div = replace_format_indicators(header_div) + header_div = replace_format_indicators(header_div,-1,"tag",get_userID(req.params.username)) posts_div = ""; for (let post_index = posts.posts.length-1; post_index >= 0; post_index--) { if (users.users[posts.posts[post_index]["userID"]]["username"] == req.params.username) { @@ -323,6 +335,28 @@ app.get("/post", (req,res) => { * Markdown supported `); }); // /post +app.get("/signup", (req,res) => { + // if the server does allow signup + if (config.allow_signup == true) { + // Send the page for signing up to the server + res.send(`
+
+
+
+
+
+
`); + } + // if the server does not allow signup + else if (config.allow_signup == false) { + res.send(`${config.signups_unavailable}`) + } + // If allow_signup is undefined or not a boolean, error + else { + res.redirect(301,"/") + console.log("Error, invalid value for allow_signup (bool)") + } +}); // /signup app.get("/edit/:post_id", (req,res) => { const post_id = req.params.post_id const post = posts.posts[post_id] @@ -359,36 +393,6 @@ app.post("/submit_comment", (req,res) => { res.redirect(301,`/post/${req.body.post_index}`) }); // /submit_comment -app.post("/submit_edit", (req,res) => { - const password = crypto.createHash('sha512').update(req.body.password).digest('hex'); - const postID = req.body.postID - const userID = req.body.userID - const title = req.body.title - const content = req.body.content - const tags = req.body.tags.split(','); - const delete_bool = req.body.delete - const unix_timestamp = getUnixTime(new Date()) - console.log(users.users[userID]['prettyname'], "is editting the post titled:", title); - - if (users.users[userID]['hash'] == password) { // password matches - let post = posts.posts[postID] - post['title'] = title - post['content'] = content - post['tags'] = tags - post['editdate'] = unix_timestamp - if (typeof delete_bool != "undefined") { - console.log("Deleting post!") - posts.posts.splice(postID,1) - comments.comments.splice(postID,1) - fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = ${JSON.stringify(comments.comments)}\nexport const counter = ${comments.counter}`, 'utf-8'); - } - fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8'); - res.redirect(302, "/"); - } - else { - res.send(`Invalid Password for user`,users.users[userID]['prettyname']); - } -}); // /submit_edit app.post("/submit_post", (req,res) => { const password = crypto.createHash('sha512').update(req.body.password).digest('hex'); const username = escape_input(req.body.username) @@ -420,6 +424,70 @@ app.post("/submit_post", (req,res) => { res.send(`Invalid Password for user`,username); } }); // /submit_post +app.post("/submit_signup", (req,res) => { + const password = crypto.createHash('sha512').update(req.body.password).digest('hex'); + const username = escape_input(req.body.username) + const prettyname = escape_input(req.body.prettyname) + const description = escape_input(req.body.description) + + // Check that signups are allowed + if (config.allow_signup == true) { + // get_userID will return -1 if the user does not exist + // so this checks that the user does not exist + if (get_userID(username) == -1) { + users.users.push({ + "username": username, + "prettyname": prettyname, + "hash": password, + "description": description, + }) + fs.writeFileSync(`${__dirname}/users.js`, `export const users = ${JSON.stringify(users.users)}`, 'utf-8'); + res.redirect(301, `/user/${username}`) + } + // if the user does exist then + else { + res.send(`${config.user_exists}`) + } + } + else if (config.allow_signup == false) { + res.send(`${config.signups_unavailable}`) + } + // If allow_signup is undefined or not a boolean, error + else { + res.redirect(301,"/") + console.log("Error, invalid value for allow_signup (bool)") + } +}); // /submit_signup +app.post("/submit_edit", (req,res) => { + const password = crypto.createHash('sha512').update(req.body.password).digest('hex'); + const postID = req.body.postID + const userID = req.body.userID + const title = req.body.title + const content = req.body.content + const tags = req.body.tags.split(','); + const delete_bool = req.body.delete + const unix_timestamp = getUnixTime(new Date()) + console.log(users.users[userID]['prettyname'], "is editting the post titled:", title); + + if (users.users[userID]['hash'] == password) { // password matches + let post = posts.posts[postID] + post['title'] = title + post['content'] = content + post['tags'] = tags + post['editdate'] = unix_timestamp + if (typeof delete_bool != "undefined") { + console.log("Deleting post!") + posts.posts.splice(postID,1) + comments.comments.splice(postID,1) + fs.writeFileSync(`${__dirname}/comments.js`, `export const comments = ${JSON.stringify(comments.comments)}\nexport const counter = ${comments.counter}`, 'utf-8'); + } + fs.writeFileSync(`${__dirname}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8'); + res.redirect(302, "/"); + } + else { + res.send(`Invalid Password for user`,users.users[userID]['prettyname']); + } +}); // /submit_edit app.listen(config.port, () => { console.log(`Server is running at http://localhost:${config.port} in ${config.root_path}`); diff --git a/example-config.js b/example-config.js index d385c33..43928ca 100755 --- a/example-config.js +++ b/example-config.js @@ -2,6 +2,7 @@ export const seperator = "
" export const site_name = "My Blog" export const site_url = "https://example.com" export const port = 8080 +export const allow_signup = false export const site_description = "Read my blogs!" export const timeline_length = 20 export const enable_hitcount = true // Can slow down page loading a bit diff --git a/hitcount.txt b/hitcount.txt index ddc17b2..495ae25 100644 --- a/hitcount.txt +++ b/hitcount.txt @@ -1 +1 @@ -260 \ No newline at end of file +464 \ No newline at end of file