Loading plugins/content/content.cpp +169 −42 Changes for plugins/content/content.cpp: 169 added lines, 42 removed lines. Original line number Diff line number Diff line Loading @@ -33,6 +33,7 @@ #include <iostream> #include <algorithm> #include <compare> #include <random> #include <htmlpp/html.h> #include <htmlpp/exception.h> Loading @@ -48,6 +49,7 @@ #include <session.h> #include <i18n.h> #include <compress.h> #include <mail.h> #define SITELIMIT 10 Loading Loading @@ -354,7 +356,6 @@ namespace blogi { std::string displayName; if(loggedIn){ // Check GPO_CONTENT_COMMENT try { std::string authid; Session sess(*Args->database[tid]); Loading @@ -369,9 +370,8 @@ namespace blogi { authcon.setClientSecret(src.clientSecret); authcon.setSessionID(uuid::uuid(authid.c_str())); authdb::client::Client clt(authcon); if(clt.ClientAuth() && clt.GPOcheck(uuid::uuid(GPO_CONTENT_COMMENT))){ canComment = true; // Get display name if(clt.ClientAuth()){ // Get display name for all logged-in users authdb::SessionData sdat; clt.SessionInfo(sdat); uuid::uuid uid; Loading @@ -379,9 +379,12 @@ namespace blogi { class authdb::UserData ud(uid); clt.UserInfo(ud); displayName = ud.getFirstname() + " " + ud.getLastname(); // GPO controls auto-approve if(clt.GPOcheck(uuid::uuid(GPO_CONTENT_COMMENT))) canComment = true; } } catch(...) { continue; } if(canComment) break; if(!displayName.empty()) break; } } } catch(...) {} Loading @@ -391,8 +394,8 @@ namespace blogi { << "<h4>" << tr(_lang,"Leave a comment") << "</h4>" << "<form method=\"POST\" action=\"" << Args->config->buildurl("content/comment/") << cid << "\">"; if(canComment){ // Logged in with GPO: name is pre-filled, auto-approved if(loggedIn && !displayName.empty()){ // Logged in: name from session, no email needed cmtdat << "<input type=\"hidden\" name=\"comment_author\" value=\"" << displayName << "\" />" << "<p>" << tr(_lang,"Commenting as") << " <strong>" << displayName << "</strong></p>"; } else { Loading Loading @@ -489,13 +492,13 @@ namespace blogi { } } if(authorName.empty() || commentText.empty()){ if(commentText.empty()){ libhttppp::HTTPException exp; exp[libhttppp::HTTPException::Error] << tr(_lang,"Name and comment text are required!"); exp[libhttppp::HTTPException::Error] << tr(_lang,"Comment text is required!"); throw exp; } // Determine if logged-in user with GPO_CONTENT_COMMENT → auto-approve // Determine if logged-in user → pull name/email from session bool autoApprove = false; int userId = 0; bool loggedIn = Args->auth->isLoggedIn(tid, curreq, sessionid); Loading @@ -522,25 +525,49 @@ namespace blogi { authcon.setClientSecret(src.clientSecret); authcon.setSessionID(uuid::uuid(authid.c_str())); authdb::client::Client clt(authcon); if(clt.ClientAuth() && clt.GPOcheck(uuid::uuid(GPO_CONTENT_COMMENT))){ if(clt.ClientAuth()){ // Pull name + email from authdb authdb::SessionData sdat; clt.SessionInfo(sdat); uuid::uuid auid; sdat.getUid(auid); class authdb::UserData ud(auid); clt.UserInfo(ud); authorName = ud.getFirstname() + " " + ud.getLastname(); email = ud.getMail(); // GPO controls auto-approve if(clt.GPOcheck(uuid::uuid(GPO_CONTENT_COMMENT)) || clt.GPOcheck(uuid::uuid(GPO_CONTENT_MODERATE))) autoApprove = true; } } catch(...) { continue; } if(autoApprove) break; if(!authorName.empty()) break; } } } catch(...) {} } if(!loggedIn && email.empty()){ if(!loggedIn && (authorName.empty() || email.empty())){ libhttppp::HTTPException exp; exp[libhttppp::HTTPException::Error] << tr(_lang,"Email is required for guest comments!"); exp[libhttppp::HTTPException::Error] << tr(_lang,"Name and email are required for guest comments!"); throw exp; } // Generate verify token for guest comments std::string verifyToken; if(!autoApprove && !loggedIn && !email.empty()){ static const char alphanum[] = "0123456789abcdef"; std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, sizeof(alphanum) - 2); verifyToken.reserve(32); for(int ti = 0; ti < 32; ++ti) verifyToken += alphanum[dis(gen)]; } // Insert comment dbpp::SQL isql; dbpp::DBResult ires; isql << "INSERT INTO comments (content_id,user_id,author_name,email,text,approved) VALUES (" isql << "INSERT INTO comments (content_id,user_id,author_name,email,text,approved,email_verified,verify_token) VALUES (" << cid << ","; if(userId > 0) isql << userId; Loading @@ -554,14 +581,37 @@ namespace blogi { isql << "true"; else isql << "false"; isql << ","; if(autoApprove || loggedIn) isql << "true"; else isql << "false"; isql << ","; if(!verifyToken.empty()) isql << "'" << verifyToken.c_str() << "'"; else isql << "NULL"; isql << ");"; Args->database[tid]->exec(isql, ires); // Send verification email for guest comments if(!verifyToken.empty() && Args->smtp){ try { std::string vurl = Args->config->getsiteurl() + Args->config->buildurl("content/comment/verify") + "?token=" + verifyToken; std::string subject = "Verify your comment"; std::string body = "Please verify your comment by clicking this link:\n\n" + vurl + "\n"; sendMail(Args->smtp.get(), email.c_str(), subject.c_str(), body); } catch(...) {} } // Show confirmation and redirect back libhtmlpp::HtmlString condat; if(autoApprove){ condat << "<div id=\"content\"><p class=\"comment_success\">" << tr(_lang,"Comment posted!") << "</p></div>"; } else if(!verifyToken.empty()) { condat << "<div id=\"content\"><p class=\"comment_pending\">" << tr(_lang,"A verification email has been sent. Please check your inbox.") << "</p></div>"; } else { condat << "<div id=\"content\"><p class=\"comment_pending\">" << tr(_lang,"Comment submitted. It will appear after moderation.") << "</p></div>"; Loading @@ -583,6 +633,55 @@ namespace blogi { sendCompressed(curreq, resp, out.c_str(), out.length()); } void verifyCommentPage(const int tid,libhttppp::HttpRequest& curreq,libhtmlpp::HtmlElement &page,const std::string &sessionid) { std::string _lang = getLang(*Args->database[tid]); std::vector<char> sbuf; std::string token; libhttppp::HttpForm form; form.parse(curreq); for(const auto &cur : form.urlData()){ if(cur.key == "token") { token = cur.value; break; } } libhtmlpp::HtmlString condat; if(token.empty() || token.length() != 32){ condat << "<div id=\"content\"><p class=\"comment_error\">" << tr(_lang,"Invalid verification link.") << "</p></div>"; } else { dbpp::SQL vsql; dbpp::DBResult vres; vsql << "SELECT id,content_id FROM comments WHERE verify_token='" << dbpp::SQL::escaped(sbuf, token.c_str()) << "' AND email_verified=false LIMIT 1;"; int vcount = Args->database[tid]->exec(vsql, vres); if(vcount > 0){ int cmtId = atoi(vres[0][0]); int contentId = atoi(vres[0][1]); dbpp::SQL usql; dbpp::DBResult ures; usql << "UPDATE comments SET email_verified=true, verify_token=NULL WHERE id=" << cmtId << ";"; Args->database[tid]->exec(usql, ures); condat << "<div id=\"content\"><p class=\"comment_success\">" << tr(_lang,"Email verified! Your comment will be published after moderation.") << "</p></div>" << "<a class=\"back_content\" href=\"" << Args->config->buildurl("content/read/") << contentId << "\">" << tr(_lang,"Back to post") << "</a>"; } else { condat << "<div id=\"content\"><p class=\"comment_error\">" << tr(_lang,"Verification link is invalid or has already been used.") << "</p></div>"; } } libhtmlpp::HtmlElement *main = page.getElementbyID("main"); if(main){ try { main->insertChild(condat.parse()); } catch(...) {} } libhtmlpp::HtmlString out; Args->theme->printSite(tid, out, &page, curreq.getRequestURL(), false); libhttppp::HttpResponse resp; resp.setState(HTTP200); resp.setContentType("text/html"); sendCompressed(curreq, resp, out.c_str(), out.length()); } void addPostPage(const int tid,libhttppp::HttpRequest &curreq,libhtmlpp::HtmlElement &page,const std::string &sessionid){ std::string _lang = getLang(*Args->database[tid]); char url[512]; Loading Loading @@ -1145,6 +1244,22 @@ namespace blogi { std::string curl = req.getRequestURL(); std::string base = Args->config->buildurl("settings/content/"); // Helper to build page_tabs array auto makePageTabs = [&](bool postsActive) -> json_object* { json_object *tabs = json_object_new_array(); json_object *t1 = json_object_new_object(); json_object_object_add(t1, "label", json_object_new_string(tr(_lang,"Posts").c_str())); json_object_object_add(t1, "url", json_object_new_string(Args->config->buildurl("settings/content").c_str())); json_object_object_add(t1, "active", json_object_new_boolean(postsActive)); json_object_array_add(tabs, t1); json_object *t2 = json_object_new_object(); json_object_object_add(t2, "label", json_object_new_string(tr(_lang,"Comments").c_str())); json_object_object_add(t2, "url", json_object_new_string(Args->config->buildurl("settings/content/comments").c_str())); json_object_object_add(t2, "active", json_object_new_boolean(!postsActive)); json_object_array_add(tabs, t2); return tabs; }; // Resolve current user for GPO checks int currentAuthor = -1; std::string authid; Loading Loading @@ -1361,6 +1476,9 @@ namespace blogi { // Sub-route: comments (moderation) if(sub.find("comments") == 0){ bool canModerate = checkGPO(GPO_CONTENT_MODERATE); // Handle approve/delete actions (require GPO) if(sub.find("comments/approve") == 0){ if(!canModerate){ json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Comments").c_str())); Loading @@ -1368,9 +1486,6 @@ namespace blogi { json_object_object_add(jroot, "message_type", json_object_new_string("error")); return jroot; } // Handle approve/delete actions if(sub.find("comments/approve") == 0){ int cmtId = -1; libhttppp::HttpForm cmtform; cmtform.parse(req); Loading @@ -1396,6 +1511,13 @@ namespace blogi { } if(sub.find("comments/delete") == 0){ if(!canModerate){ json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Comments").c_str())); json_object_object_add(jroot, "message", json_object_new_string(tr(_lang,"Permission denied").c_str())); json_object_object_add(jroot, "message_type", json_object_new_string("error")); return jroot; } int cmtId = -1; bool confirmed = false; libhttppp::HttpForm cmtform; Loading Loading @@ -1432,16 +1554,10 @@ namespace blogi { return jroot; } // Default: list pending comments // Default: list all comments json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Pending Comments").c_str())); json_object *jnav = json_object_new_array(); json_object *jback = json_object_new_object(); json_object_object_add(jback, "label", json_object_new_string(tr(_lang,"Back to content").c_str())); json_object_object_add(jback, "url", json_object_new_string(Args->config->buildurl("settings/content").c_str())); json_object_array_add(jnav, jback); json_object_object_add(jroot, "nav", jnav); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Content").c_str())); json_object_object_add(jroot, "page_tabs", makePageTabs(false)); json_object *jtable = json_object_new_object(); json_object *jheaders = json_object_new_array(); Loading @@ -1451,13 +1567,15 @@ namespace blogi { json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Comment").c_str())); json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Post").c_str())); json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Date").c_str())); json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Status").c_str())); json_object_object_add(jtable, "headers", jheaders); dbpp::SQL csql; dbpp::DBResult cres; csql << "SELECT comments.id,comments.author_name,comments.email," << "substring(comments.text from 1 for 80),content.title,comments.created " << "substring(comments.text from 1 for 80),content.title,comments.created," << "comments.approved,comments.email_verified " << "FROM comments LEFT JOIN content ON comments.content_id=content.id " << "WHERE comments.approved=false ORDER BY comments.created DESC LIMIT 100;"; << "ORDER BY comments.approved ASC, comments.created DESC LIMIT 100;"; int ccount = Args->database[tid]->exec(csql, cres); json_object *jrows = json_object_new_array(); Loading @@ -1470,20 +1588,36 @@ namespace blogi { json_object_array_add(jcells, json_object_new_string(cres[i][3] ? cres[i][3] : "")); json_object_array_add(jcells, json_object_new_string(cres[i][4] ? cres[i][4] : "")); json_object_array_add(jcells, json_object_new_string(cres[i][5] ? cres[i][5] : "")); bool isApproved = cres[i][6] && std::string(cres[i][6]) == "t"; bool isVerified = cres[i][7] && std::string(cres[i][7]) == "t"; std::string status; if(isApproved) status = tr(_lang,"Approved"); else if(isVerified) status = tr(_lang,"Email verified"); else status = tr(_lang,"Pending verification"); json_object_array_add(jcells, json_object_new_string(status.c_str())); json_object_object_add(jrow, "cells", jcells); json_object *jactions = json_object_new_array(); if(canModerate){ if(!isApproved && isVerified){ json_object *japprove = json_object_new_object(); json_object_object_add(japprove, "label", json_object_new_string(tr(_lang,"Approve").c_str())); json_object_object_add(japprove, "url", json_object_new_string( (Args->config->buildurl("settings/content/comments/approve?id=") + std::string(cres[i][0])).c_str())); json_object_array_add(jactions, japprove); } json_object *jdel = json_object_new_object(); json_object_object_add(jdel, "label", json_object_new_string(tr(_lang,"Delete").c_str())); json_object_object_add(jdel, "url", json_object_new_string( (Args->config->buildurl("settings/content/comments/delete?id=") + std::string(cres[i][0])).c_str())); json_object_array_add(jactions, jdel); } json_object_object_add(jrow, "actions", jactions); json_object_array_add(jrows, jrow); Loading @@ -1497,6 +1631,7 @@ namespace blogi { // Default: post list with edit/delete actions json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Content").c_str())); json_object_object_add(jroot, "page_tabs", makePageTabs(true)); json_object *jtable = json_object_new_object(); json_object *jheaders = json_object_new_array(); Loading Loading @@ -1548,18 +1683,6 @@ namespace blogi { json_object_object_add(jtable, "rows", jrows); json_object_object_add(jroot, "table", jtable); // Link to comment moderation if user has GPO bool canModerate = checkGPO(GPO_CONTENT_MODERATE); if(canModerate){ json_object *jnav = json_object_new_array(); json_object *jcmts = json_object_new_object(); json_object_object_add(jcmts, "label", json_object_new_string(tr(_lang,"Moderate Comments").c_str())); json_object_object_add(jcmts, "url", json_object_new_string( Args->config->buildurl("settings/content/comments").c_str())); json_object_array_add(jnav, jcmts); json_object_object_add(jroot, "nav", jnav); } return jroot; } Loading Loading @@ -1597,7 +1720,7 @@ namespace blogi { registerGPO(GPO_CONTENT_MODERATE, "Blogi: Moderate Comments", "Allows users to approve or delete guest comments", false); true); dbpp::SQL sql; dbpp::DBResult res; Loading Loading @@ -1629,6 +1752,8 @@ namespace blogi { << "text text NOT NULL," << "created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," << "approved boolean NOT NULL DEFAULT false," << "email_verified boolean NOT NULL DEFAULT false," << "verify_token character varying(64)," << "FOREIGN KEY (content_id) REFERENCES content (id)," << "FOREIGN KEY (user_id) REFERENCES users (id)" << ");"; Loading @@ -1648,6 +1773,8 @@ namespace blogi { if (curl.compare(0,Args->config->buildurl("content/addpost").length(),Args->config->buildurl("content/addpost")) == 0){ addPostPage(tid,req,*page,sessionid); }else if (curl.compare(0,Args->config->buildurl("content/comment/verify").length(), Args->config->buildurl("content/comment/verify")) == 0) { verifyCommentPage(tid,req,*page,sessionid); }else if (curl.compare(0,Args->config->buildurl("content/comment").length(), Args->config->buildurl("content/comment")) == 0) { commentPage(tid,req,*page,sessionid); }else if (curl.compare(0,Args->config->buildurl("content/read").length(), Args->config->buildurl("content/read")) == 0) { Loading src/blogi.cpp +38 −0 Changes for src/blogi.cpp: 38 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -496,6 +496,28 @@ static void renderSettingsJson(json_object *jroot, libhtmlpp::HtmlString &out, c out << "<h3 class=\"set-title\">" << json_object_get_string(jtitle) << "</h3>"; } // Page-level tabs (AJAX link tabs) json_object *jptabs = nullptr; if(json_object_object_get_ex(jroot, "page_tabs", &jptabs)){ int ptlen = json_object_array_length(jptabs); out << "<div style=\"display:flex;gap:0;border-bottom:2px solid var(--blogi-border,#3a3f44);margin-bottom:12px;\">"; for(int t=0; t<ptlen; ++t){ json_object *jpt = json_object_array_get_idx(jptabs, t); json_object *jptl=nullptr, *jptu=nullptr, *jpta=nullptr; json_object_object_get_ex(jpt, "label", &jptl); json_object_object_get_ex(jpt, "url", &jptu); json_object_object_get_ex(jpt, "active", &jpta); bool active = jpta && json_object_get_boolean(jpta); out << "<a href=\"" << (jptu ? json_object_get_string(jptu) : "#") << "\" style=\"" << "padding:8px 18px;text-decoration:none;font-size:0.95em;border-bottom:2px solid " << (active ? "var(--blogi-accent,#3ad43a)" : "transparent") << ";margin-bottom:-2px;color:" << (active ? "var(--blogi-accent,#3ad43a)" : "var(--blogi-text-muted,#aaa)") << ";\">" << (jptl ? json_object_get_string(jptl) : "") << "</a>"; } out << "</div>"; } // Message (success / error) json_object *jmsg = nullptr; if(json_object_object_get_ex(jroot, "message", &jmsg)){ Loading Loading @@ -1626,6 +1648,22 @@ void blogi::Blogi::Settings(libhttppp::HttpRequest& curreq,const int tid,const s if(curplg->getInstace()->haveSettings()){ setgui << "<li data-url=\"" << PlgArgs->config->buildurl("settings/" + curplg->getInstace()->getName()) << "\">" << curplg->getInstace()->getName() << "</li>"; // Extra sub-tabs from plugin json_object *tabs = curplg->getInstace()->getSettingsTabs(); if(tabs){ int tlen = json_object_array_length(tabs); for(int t = 0; t < tlen; ++t){ json_object *tab = json_object_array_get_idx(tabs, t); json_object *jlabel = nullptr, *jurl = nullptr; json_object_object_get_ex(tab, "label", &jlabel); json_object_object_get_ex(tab, "url", &jurl); if(jlabel && jurl){ setgui << "<li data-url=\"" << json_object_get_string(jurl) << "\">" << json_object_get_string(jlabel) << "</li>"; } } json_object_put(tabs); } } } Loading src/plugin.cpp +4 −0 Changes for src/plugin.cpp: 4 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -124,6 +124,10 @@ bool blogi::PluginApi::haveSettings(){ return false; } json_object* blogi::PluginApi::getSettingsTabs(){ return nullptr; } void blogi::PluginApi::Rendering(const int tid,libhttppp::HttpRequest &req,libhtmlpp::HtmlElement *curpage,int type,const std::string &sessionid){ return; } Loading src/plugin.h +2 −0 Changes for src/plugin.h: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -137,6 +137,8 @@ namespace blogi { virtual const std::string getAuthor()=0; virtual bool haveSettings(); virtual json_object* getSettingsTabs(); void setArgs(PluginArgs *args); virtual void initPlugin(); Loading Loading
plugins/content/content.cpp +169 −42 Changes for plugins/content/content.cpp: 169 added lines, 42 removed lines. Original line number Diff line number Diff line Loading @@ -33,6 +33,7 @@ #include <iostream> #include <algorithm> #include <compare> #include <random> #include <htmlpp/html.h> #include <htmlpp/exception.h> Loading @@ -48,6 +49,7 @@ #include <session.h> #include <i18n.h> #include <compress.h> #include <mail.h> #define SITELIMIT 10 Loading Loading @@ -354,7 +356,6 @@ namespace blogi { std::string displayName; if(loggedIn){ // Check GPO_CONTENT_COMMENT try { std::string authid; Session sess(*Args->database[tid]); Loading @@ -369,9 +370,8 @@ namespace blogi { authcon.setClientSecret(src.clientSecret); authcon.setSessionID(uuid::uuid(authid.c_str())); authdb::client::Client clt(authcon); if(clt.ClientAuth() && clt.GPOcheck(uuid::uuid(GPO_CONTENT_COMMENT))){ canComment = true; // Get display name if(clt.ClientAuth()){ // Get display name for all logged-in users authdb::SessionData sdat; clt.SessionInfo(sdat); uuid::uuid uid; Loading @@ -379,9 +379,12 @@ namespace blogi { class authdb::UserData ud(uid); clt.UserInfo(ud); displayName = ud.getFirstname() + " " + ud.getLastname(); // GPO controls auto-approve if(clt.GPOcheck(uuid::uuid(GPO_CONTENT_COMMENT))) canComment = true; } } catch(...) { continue; } if(canComment) break; if(!displayName.empty()) break; } } } catch(...) {} Loading @@ -391,8 +394,8 @@ namespace blogi { << "<h4>" << tr(_lang,"Leave a comment") << "</h4>" << "<form method=\"POST\" action=\"" << Args->config->buildurl("content/comment/") << cid << "\">"; if(canComment){ // Logged in with GPO: name is pre-filled, auto-approved if(loggedIn && !displayName.empty()){ // Logged in: name from session, no email needed cmtdat << "<input type=\"hidden\" name=\"comment_author\" value=\"" << displayName << "\" />" << "<p>" << tr(_lang,"Commenting as") << " <strong>" << displayName << "</strong></p>"; } else { Loading Loading @@ -489,13 +492,13 @@ namespace blogi { } } if(authorName.empty() || commentText.empty()){ if(commentText.empty()){ libhttppp::HTTPException exp; exp[libhttppp::HTTPException::Error] << tr(_lang,"Name and comment text are required!"); exp[libhttppp::HTTPException::Error] << tr(_lang,"Comment text is required!"); throw exp; } // Determine if logged-in user with GPO_CONTENT_COMMENT → auto-approve // Determine if logged-in user → pull name/email from session bool autoApprove = false; int userId = 0; bool loggedIn = Args->auth->isLoggedIn(tid, curreq, sessionid); Loading @@ -522,25 +525,49 @@ namespace blogi { authcon.setClientSecret(src.clientSecret); authcon.setSessionID(uuid::uuid(authid.c_str())); authdb::client::Client clt(authcon); if(clt.ClientAuth() && clt.GPOcheck(uuid::uuid(GPO_CONTENT_COMMENT))){ if(clt.ClientAuth()){ // Pull name + email from authdb authdb::SessionData sdat; clt.SessionInfo(sdat); uuid::uuid auid; sdat.getUid(auid); class authdb::UserData ud(auid); clt.UserInfo(ud); authorName = ud.getFirstname() + " " + ud.getLastname(); email = ud.getMail(); // GPO controls auto-approve if(clt.GPOcheck(uuid::uuid(GPO_CONTENT_COMMENT)) || clt.GPOcheck(uuid::uuid(GPO_CONTENT_MODERATE))) autoApprove = true; } } catch(...) { continue; } if(autoApprove) break; if(!authorName.empty()) break; } } } catch(...) {} } if(!loggedIn && email.empty()){ if(!loggedIn && (authorName.empty() || email.empty())){ libhttppp::HTTPException exp; exp[libhttppp::HTTPException::Error] << tr(_lang,"Email is required for guest comments!"); exp[libhttppp::HTTPException::Error] << tr(_lang,"Name and email are required for guest comments!"); throw exp; } // Generate verify token for guest comments std::string verifyToken; if(!autoApprove && !loggedIn && !email.empty()){ static const char alphanum[] = "0123456789abcdef"; std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, sizeof(alphanum) - 2); verifyToken.reserve(32); for(int ti = 0; ti < 32; ++ti) verifyToken += alphanum[dis(gen)]; } // Insert comment dbpp::SQL isql; dbpp::DBResult ires; isql << "INSERT INTO comments (content_id,user_id,author_name,email,text,approved) VALUES (" isql << "INSERT INTO comments (content_id,user_id,author_name,email,text,approved,email_verified,verify_token) VALUES (" << cid << ","; if(userId > 0) isql << userId; Loading @@ -554,14 +581,37 @@ namespace blogi { isql << "true"; else isql << "false"; isql << ","; if(autoApprove || loggedIn) isql << "true"; else isql << "false"; isql << ","; if(!verifyToken.empty()) isql << "'" << verifyToken.c_str() << "'"; else isql << "NULL"; isql << ");"; Args->database[tid]->exec(isql, ires); // Send verification email for guest comments if(!verifyToken.empty() && Args->smtp){ try { std::string vurl = Args->config->getsiteurl() + Args->config->buildurl("content/comment/verify") + "?token=" + verifyToken; std::string subject = "Verify your comment"; std::string body = "Please verify your comment by clicking this link:\n\n" + vurl + "\n"; sendMail(Args->smtp.get(), email.c_str(), subject.c_str(), body); } catch(...) {} } // Show confirmation and redirect back libhtmlpp::HtmlString condat; if(autoApprove){ condat << "<div id=\"content\"><p class=\"comment_success\">" << tr(_lang,"Comment posted!") << "</p></div>"; } else if(!verifyToken.empty()) { condat << "<div id=\"content\"><p class=\"comment_pending\">" << tr(_lang,"A verification email has been sent. Please check your inbox.") << "</p></div>"; } else { condat << "<div id=\"content\"><p class=\"comment_pending\">" << tr(_lang,"Comment submitted. It will appear after moderation.") << "</p></div>"; Loading @@ -583,6 +633,55 @@ namespace blogi { sendCompressed(curreq, resp, out.c_str(), out.length()); } void verifyCommentPage(const int tid,libhttppp::HttpRequest& curreq,libhtmlpp::HtmlElement &page,const std::string &sessionid) { std::string _lang = getLang(*Args->database[tid]); std::vector<char> sbuf; std::string token; libhttppp::HttpForm form; form.parse(curreq); for(const auto &cur : form.urlData()){ if(cur.key == "token") { token = cur.value; break; } } libhtmlpp::HtmlString condat; if(token.empty() || token.length() != 32){ condat << "<div id=\"content\"><p class=\"comment_error\">" << tr(_lang,"Invalid verification link.") << "</p></div>"; } else { dbpp::SQL vsql; dbpp::DBResult vres; vsql << "SELECT id,content_id FROM comments WHERE verify_token='" << dbpp::SQL::escaped(sbuf, token.c_str()) << "' AND email_verified=false LIMIT 1;"; int vcount = Args->database[tid]->exec(vsql, vres); if(vcount > 0){ int cmtId = atoi(vres[0][0]); int contentId = atoi(vres[0][1]); dbpp::SQL usql; dbpp::DBResult ures; usql << "UPDATE comments SET email_verified=true, verify_token=NULL WHERE id=" << cmtId << ";"; Args->database[tid]->exec(usql, ures); condat << "<div id=\"content\"><p class=\"comment_success\">" << tr(_lang,"Email verified! Your comment will be published after moderation.") << "</p></div>" << "<a class=\"back_content\" href=\"" << Args->config->buildurl("content/read/") << contentId << "\">" << tr(_lang,"Back to post") << "</a>"; } else { condat << "<div id=\"content\"><p class=\"comment_error\">" << tr(_lang,"Verification link is invalid or has already been used.") << "</p></div>"; } } libhtmlpp::HtmlElement *main = page.getElementbyID("main"); if(main){ try { main->insertChild(condat.parse()); } catch(...) {} } libhtmlpp::HtmlString out; Args->theme->printSite(tid, out, &page, curreq.getRequestURL(), false); libhttppp::HttpResponse resp; resp.setState(HTTP200); resp.setContentType("text/html"); sendCompressed(curreq, resp, out.c_str(), out.length()); } void addPostPage(const int tid,libhttppp::HttpRequest &curreq,libhtmlpp::HtmlElement &page,const std::string &sessionid){ std::string _lang = getLang(*Args->database[tid]); char url[512]; Loading Loading @@ -1145,6 +1244,22 @@ namespace blogi { std::string curl = req.getRequestURL(); std::string base = Args->config->buildurl("settings/content/"); // Helper to build page_tabs array auto makePageTabs = [&](bool postsActive) -> json_object* { json_object *tabs = json_object_new_array(); json_object *t1 = json_object_new_object(); json_object_object_add(t1, "label", json_object_new_string(tr(_lang,"Posts").c_str())); json_object_object_add(t1, "url", json_object_new_string(Args->config->buildurl("settings/content").c_str())); json_object_object_add(t1, "active", json_object_new_boolean(postsActive)); json_object_array_add(tabs, t1); json_object *t2 = json_object_new_object(); json_object_object_add(t2, "label", json_object_new_string(tr(_lang,"Comments").c_str())); json_object_object_add(t2, "url", json_object_new_string(Args->config->buildurl("settings/content/comments").c_str())); json_object_object_add(t2, "active", json_object_new_boolean(!postsActive)); json_object_array_add(tabs, t2); return tabs; }; // Resolve current user for GPO checks int currentAuthor = -1; std::string authid; Loading Loading @@ -1361,6 +1476,9 @@ namespace blogi { // Sub-route: comments (moderation) if(sub.find("comments") == 0){ bool canModerate = checkGPO(GPO_CONTENT_MODERATE); // Handle approve/delete actions (require GPO) if(sub.find("comments/approve") == 0){ if(!canModerate){ json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Comments").c_str())); Loading @@ -1368,9 +1486,6 @@ namespace blogi { json_object_object_add(jroot, "message_type", json_object_new_string("error")); return jroot; } // Handle approve/delete actions if(sub.find("comments/approve") == 0){ int cmtId = -1; libhttppp::HttpForm cmtform; cmtform.parse(req); Loading @@ -1396,6 +1511,13 @@ namespace blogi { } if(sub.find("comments/delete") == 0){ if(!canModerate){ json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Comments").c_str())); json_object_object_add(jroot, "message", json_object_new_string(tr(_lang,"Permission denied").c_str())); json_object_object_add(jroot, "message_type", json_object_new_string("error")); return jroot; } int cmtId = -1; bool confirmed = false; libhttppp::HttpForm cmtform; Loading Loading @@ -1432,16 +1554,10 @@ namespace blogi { return jroot; } // Default: list pending comments // Default: list all comments json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Pending Comments").c_str())); json_object *jnav = json_object_new_array(); json_object *jback = json_object_new_object(); json_object_object_add(jback, "label", json_object_new_string(tr(_lang,"Back to content").c_str())); json_object_object_add(jback, "url", json_object_new_string(Args->config->buildurl("settings/content").c_str())); json_object_array_add(jnav, jback); json_object_object_add(jroot, "nav", jnav); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Content").c_str())); json_object_object_add(jroot, "page_tabs", makePageTabs(false)); json_object *jtable = json_object_new_object(); json_object *jheaders = json_object_new_array(); Loading @@ -1451,13 +1567,15 @@ namespace blogi { json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Comment").c_str())); json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Post").c_str())); json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Date").c_str())); json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Status").c_str())); json_object_object_add(jtable, "headers", jheaders); dbpp::SQL csql; dbpp::DBResult cres; csql << "SELECT comments.id,comments.author_name,comments.email," << "substring(comments.text from 1 for 80),content.title,comments.created " << "substring(comments.text from 1 for 80),content.title,comments.created," << "comments.approved,comments.email_verified " << "FROM comments LEFT JOIN content ON comments.content_id=content.id " << "WHERE comments.approved=false ORDER BY comments.created DESC LIMIT 100;"; << "ORDER BY comments.approved ASC, comments.created DESC LIMIT 100;"; int ccount = Args->database[tid]->exec(csql, cres); json_object *jrows = json_object_new_array(); Loading @@ -1470,20 +1588,36 @@ namespace blogi { json_object_array_add(jcells, json_object_new_string(cres[i][3] ? cres[i][3] : "")); json_object_array_add(jcells, json_object_new_string(cres[i][4] ? cres[i][4] : "")); json_object_array_add(jcells, json_object_new_string(cres[i][5] ? cres[i][5] : "")); bool isApproved = cres[i][6] && std::string(cres[i][6]) == "t"; bool isVerified = cres[i][7] && std::string(cres[i][7]) == "t"; std::string status; if(isApproved) status = tr(_lang,"Approved"); else if(isVerified) status = tr(_lang,"Email verified"); else status = tr(_lang,"Pending verification"); json_object_array_add(jcells, json_object_new_string(status.c_str())); json_object_object_add(jrow, "cells", jcells); json_object *jactions = json_object_new_array(); if(canModerate){ if(!isApproved && isVerified){ json_object *japprove = json_object_new_object(); json_object_object_add(japprove, "label", json_object_new_string(tr(_lang,"Approve").c_str())); json_object_object_add(japprove, "url", json_object_new_string( (Args->config->buildurl("settings/content/comments/approve?id=") + std::string(cres[i][0])).c_str())); json_object_array_add(jactions, japprove); } json_object *jdel = json_object_new_object(); json_object_object_add(jdel, "label", json_object_new_string(tr(_lang,"Delete").c_str())); json_object_object_add(jdel, "url", json_object_new_string( (Args->config->buildurl("settings/content/comments/delete?id=") + std::string(cres[i][0])).c_str())); json_object_array_add(jactions, jdel); } json_object_object_add(jrow, "actions", jactions); json_object_array_add(jrows, jrow); Loading @@ -1497,6 +1631,7 @@ namespace blogi { // Default: post list with edit/delete actions json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Content").c_str())); json_object_object_add(jroot, "page_tabs", makePageTabs(true)); json_object *jtable = json_object_new_object(); json_object *jheaders = json_object_new_array(); Loading Loading @@ -1548,18 +1683,6 @@ namespace blogi { json_object_object_add(jtable, "rows", jrows); json_object_object_add(jroot, "table", jtable); // Link to comment moderation if user has GPO bool canModerate = checkGPO(GPO_CONTENT_MODERATE); if(canModerate){ json_object *jnav = json_object_new_array(); json_object *jcmts = json_object_new_object(); json_object_object_add(jcmts, "label", json_object_new_string(tr(_lang,"Moderate Comments").c_str())); json_object_object_add(jcmts, "url", json_object_new_string( Args->config->buildurl("settings/content/comments").c_str())); json_object_array_add(jnav, jcmts); json_object_object_add(jroot, "nav", jnav); } return jroot; } Loading Loading @@ -1597,7 +1720,7 @@ namespace blogi { registerGPO(GPO_CONTENT_MODERATE, "Blogi: Moderate Comments", "Allows users to approve or delete guest comments", false); true); dbpp::SQL sql; dbpp::DBResult res; Loading Loading @@ -1629,6 +1752,8 @@ namespace blogi { << "text text NOT NULL," << "created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," << "approved boolean NOT NULL DEFAULT false," << "email_verified boolean NOT NULL DEFAULT false," << "verify_token character varying(64)," << "FOREIGN KEY (content_id) REFERENCES content (id)," << "FOREIGN KEY (user_id) REFERENCES users (id)" << ");"; Loading @@ -1648,6 +1773,8 @@ namespace blogi { if (curl.compare(0,Args->config->buildurl("content/addpost").length(),Args->config->buildurl("content/addpost")) == 0){ addPostPage(tid,req,*page,sessionid); }else if (curl.compare(0,Args->config->buildurl("content/comment/verify").length(), Args->config->buildurl("content/comment/verify")) == 0) { verifyCommentPage(tid,req,*page,sessionid); }else if (curl.compare(0,Args->config->buildurl("content/comment").length(), Args->config->buildurl("content/comment")) == 0) { commentPage(tid,req,*page,sessionid); }else if (curl.compare(0,Args->config->buildurl("content/read").length(), Args->config->buildurl("content/read")) == 0) { Loading
src/blogi.cpp +38 −0 Changes for src/blogi.cpp: 38 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -496,6 +496,28 @@ static void renderSettingsJson(json_object *jroot, libhtmlpp::HtmlString &out, c out << "<h3 class=\"set-title\">" << json_object_get_string(jtitle) << "</h3>"; } // Page-level tabs (AJAX link tabs) json_object *jptabs = nullptr; if(json_object_object_get_ex(jroot, "page_tabs", &jptabs)){ int ptlen = json_object_array_length(jptabs); out << "<div style=\"display:flex;gap:0;border-bottom:2px solid var(--blogi-border,#3a3f44);margin-bottom:12px;\">"; for(int t=0; t<ptlen; ++t){ json_object *jpt = json_object_array_get_idx(jptabs, t); json_object *jptl=nullptr, *jptu=nullptr, *jpta=nullptr; json_object_object_get_ex(jpt, "label", &jptl); json_object_object_get_ex(jpt, "url", &jptu); json_object_object_get_ex(jpt, "active", &jpta); bool active = jpta && json_object_get_boolean(jpta); out << "<a href=\"" << (jptu ? json_object_get_string(jptu) : "#") << "\" style=\"" << "padding:8px 18px;text-decoration:none;font-size:0.95em;border-bottom:2px solid " << (active ? "var(--blogi-accent,#3ad43a)" : "transparent") << ";margin-bottom:-2px;color:" << (active ? "var(--blogi-accent,#3ad43a)" : "var(--blogi-text-muted,#aaa)") << ";\">" << (jptl ? json_object_get_string(jptl) : "") << "</a>"; } out << "</div>"; } // Message (success / error) json_object *jmsg = nullptr; if(json_object_object_get_ex(jroot, "message", &jmsg)){ Loading Loading @@ -1626,6 +1648,22 @@ void blogi::Blogi::Settings(libhttppp::HttpRequest& curreq,const int tid,const s if(curplg->getInstace()->haveSettings()){ setgui << "<li data-url=\"" << PlgArgs->config->buildurl("settings/" + curplg->getInstace()->getName()) << "\">" << curplg->getInstace()->getName() << "</li>"; // Extra sub-tabs from plugin json_object *tabs = curplg->getInstace()->getSettingsTabs(); if(tabs){ int tlen = json_object_array_length(tabs); for(int t = 0; t < tlen; ++t){ json_object *tab = json_object_array_get_idx(tabs, t); json_object *jlabel = nullptr, *jurl = nullptr; json_object_object_get_ex(tab, "label", &jlabel); json_object_object_get_ex(tab, "url", &jurl); if(jlabel && jurl){ setgui << "<li data-url=\"" << json_object_get_string(jurl) << "\">" << json_object_get_string(jlabel) << "</li>"; } } json_object_put(tabs); } } } Loading
src/plugin.cpp +4 −0 Changes for src/plugin.cpp: 4 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -124,6 +124,10 @@ bool blogi::PluginApi::haveSettings(){ return false; } json_object* blogi::PluginApi::getSettingsTabs(){ return nullptr; } void blogi::PluginApi::Rendering(const int tid,libhttppp::HttpRequest &req,libhtmlpp::HtmlElement *curpage,int type,const std::string &sessionid){ return; } Loading
src/plugin.h +2 −0 Changes for src/plugin.h: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -137,6 +137,8 @@ namespace blogi { virtual const std::string getAuthor()=0; virtual bool haveSettings(); virtual json_object* getSettingsTabs(); void setArgs(PluginArgs *args); virtual void initPlugin(); Loading