Commit 8dd6cea9 authored by jan.koester's avatar jan.koester
Browse files

test

parent 0184f395
Loading
Loading
Loading
Loading
+1 −0
Changes for data/themes/default/public/mstyle.css: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ body {

#searchbar input {
   border:none;
   background:none;
   width: 55vw;
   font-size:1.2em;
}
+85 −12
Changes for plugins/content/content.cpp: 85 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -51,6 +51,8 @@
#include <compress.h>
#include <mail.h>

#include <cctype>

#define SITELIMIT 10

#define GPO_CONTENT_EDIT_OWN      "c3eaf8g4-9d5f-6a7c-b123-4f6d8e0g2c34"
@@ -69,10 +71,33 @@ namespace blogi {
        ~Content(){
        };

        static std::string makeSlug(const char *title) {
            std::string slug;
            for (const char *p = title; *p; ++p) {
                unsigned char c = static_cast<unsigned char>(*p);
                if (std::isalnum(c))
                    slug += static_cast<char>(std::tolower(c));
                else if (c == ' ' || c == '\t')
                    slug += '_';
                // skip other characters
            }
            // collapse consecutive underscores
            std::string out;
            for (char c : slug) {
                if (c == '_' && !out.empty() && out.back() == '_')
                    continue;
                out += c;
            }
            // trim trailing underscores
            while (!out.empty() && out.back() == '_')
                out.pop_back();
            return out;
        }

        void GetSiteMap(const int tid,std::map<libhttppp::HttpUrl,std::chrono::year_month_day> &sitemap){
            dbpp::SQL sql;
            dbpp::DBResult res;
            sql="SELECT content.id,content.created FROM content;";
            sql="SELECT content.slug,content.created FROM content;";
            int ncount=Args->database[tid]->exec(sql,res);
            for(int i=0; i<ncount; ++i){
#ifndef FreeBSD
@@ -113,14 +138,14 @@ namespace blogi {
                    excep[libhttppp::HTTPException::Critical] << "no tag data found for this name!";
                    throw excep;
                }else {
                    sql = "SELECT content.id,content.title,content.descrition,users.displayname,content.created FROM content ";
                    sql = "SELECT content.id,content.title,content.descrition,users.displayname,content.created,content.slug FROM content ";
                    sql <<"LEFT JOIN users ON content.author=users.id LEFT JOIN tags_content ON tags_content.content_id=content.id where tags_content.tag_id='"
                    << res[0][0]
                    <<"' ORDER BY content.id DESC LIMIT '" << end << "' OFFSET " << start;
                    ncount=Args->database[tid]->exec(sql,res);
                }
            } else {
                sql="SELECT content.id,content.title,content.descrition,users.displayname,content.created FROM content LEFT JOIN users ON content.author=users.id ORDER BY content.id DESC";
                sql="SELECT content.id,content.title,content.descrition,users.displayname,content.created,content.slug FROM content LEFT JOIN users ON content.author=users.id ORDER BY content.id DESC";
                sql << " LIMIT '" << end << "' OFFSET " << start;
                ncount=Args->database[tid]->exec(sql,res);
            }
@@ -147,7 +172,7 @@ namespace blogi {
                condat << "<div class=\"blog_entry\">"
                << "<span class=\"title\">" << res[i][1] << "</span>"
                << "<div class=\"blog_entry_idx_content\" ><div  class=\"entry_text\">" << res[i][2] << "</div>"
                << "<span class=\"readmore\"><a href=\""<< Args->config->buildurl("content/read/") << res[i][0] << "\">"+tr(_lang,"read more ...")+"</a></span>"
                << "<span class=\"readmore\"><a href=\""<< Args->config->buildurl("content/read/") << res[i][5] << "\">"+tr(_lang,"read more ...")+"</a></span>"
                << "<span class=\"author\">"+tr(_lang,"written by")+" " << res[i][3] << " "+tr(_lang,"on")+" "<< res[i][4]  <<" </span></div></div>";
                meta.append(res[i][1]);
                meta.append(" author: ");
@@ -230,23 +255,48 @@ namespace blogi {
            std::string _lang = getLang(*Args->database[tid]);
            libhttppp::HTTPException excep;

            char url[512];
            int cid;

            sscanf(curreq.getRequestURL().c_str(), Args->config->buildurl("content/read/%d").c_str(),&cid);
            // Extract slug from URL: /content/read/<slug>
            std::string prefix = Args->config->buildurl("content/read/");
            std::string slug;
            std::string requrl = curreq.getRequestURL();
            if (requrl.length() > prefix.length())
                slug = requrl.substr(prefix.length());
            // URL-decode the slug
            std::string decodedSlug;
            libhttppp::HttpForm::urlDecode(slug, decodedSlug);

            std::vector<char> sbuf;
            dbpp::SQL sql;

            sql << "select content.id,content.title,content.text,users.displayname,content.created,content.author from content LEFT JOIN users ON content.author=users.id WHERE content.id='" << cid <<"' LIMIT 1";
            sql << "select content.id,content.title,content.text,users.displayname,content.created,content.author,content.slug from content LEFT JOIN users ON content.author=users.id WHERE content.slug='" << dbpp::SQL::escaped(sbuf, decodedSlug.c_str()) <<"' LIMIT 1";

            dbpp::DBResult res;


            if(Args->database[tid]->exec(sql,res)<1){
                excep[libhttppp::HTTPException::Critical] << "No entry found for content id: " << cid;
                // Fallback: try as numeric ID for old links
                int cid = 0;
                try { cid = std::stoi(decodedSlug); } catch (...) {}
                if (cid > 0) {
                    sql.clear();
                    sql << "select content.id,content.title,content.text,users.displayname,content.created,content.author,content.slug from content LEFT JOIN users ON content.author=users.id WHERE content.id='" << cid <<"' LIMIT 1";
                    if(Args->database[tid]->exec(sql,res) >= 1 && res[0][6] && res[0][6][0] != '\0') {
                        // Redirect old numeric URL to slug URL
                        libhttppp::HttpResponse resp;
                        resp.setState(HTTP301);
                        resp.setContentType("text/html");
                        std::string loc = Args->config->getsiteurl() + "/" + Args->config->getprefix() + "/content/read/" + res[0][6];
                        resp.setHeaderData("Location", loc.c_str());
                        sendCompressed(curreq, resp, "", 0);
                        return;
                    }
                }
                excep[libhttppp::HTTPException::Critical] << "No entry found for: " << decodedSlug;
                throw excep;
            }

            int cid = atoi(res[0][0]);
            std::string contentSlug = res[0][6] ? res[0][6] : "";

            // Admin menu with GPO-based edit/delete visibility
            if (Args->auth->isLoggedIn(tid,curreq,sessionid)) {
                bool canEdit = false;
@@ -433,7 +483,7 @@ namespace blogi {

            libhtmlpp::HtmlElement canon("link");
            std::stringstream href;
            href << Args->config->getsiteurl() << "/content/read/" << cid;
            href << Args->config->getsiteurl() << "/content/read/" << contentSlug;
            canon.setAttribute("rel","canonical");
            canon.setAttribute("href",href.str());
            if(page.getElementbyTag("head"))
@@ -1727,6 +1777,7 @@ namespace blogi {
            sql << "CREATE TABLE IF NOT EXISTS content ("
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement(sql) << ","
                <<     "title character varying(255) NOT NULL,"
                <<     "slug character varying(255) NOT NULL DEFAULT '',"
                <<     "text text NOT NULL,"
                <<     "descrition character varying(255) NOT NULL,"
                <<     "author integer ,"
@@ -1758,6 +1809,28 @@ namespace blogi {
                <<   "FOREIGN KEY (user_id) REFERENCES users (id)"
                << ");";
            Args->database[0]->exec(sql,res);

            // Migration: add slug column if missing, populate empty slugs
            try {
                sql.clear();
                sql << "ALTER TABLE content ADD COLUMN slug character varying(255) NOT NULL DEFAULT '';";
                Args->database[0]->exec(sql, res);
            } catch (...) { /* column already exists */ }
            try {
                sql.clear();
                sql << "SELECT id,title FROM content WHERE slug='' OR slug IS NULL;";
                int migCount = Args->database[0]->exec(sql, res);
                std::vector<char> mbuf;
                for (int i = 0; i < migCount; ++i) {
                    std::string s = makeSlug(res[i][1]);
                    dbpp::SQL usql;
                    dbpp::DBResult ures;
                    usql << "UPDATE content SET slug='" << dbpp::SQL::escaped(mbuf, s.c_str())
                         << "' WHERE id=" << res[i][0] << ";";
                    Args->database[0]->exec(usql, ures);
                }
            } catch (...) {}

            return;
        }