Commit 8631d2e4 authored by jan.koester's avatar jan.koester
Browse files

bugfixes

parent 49bf3c71
Loading
Loading
Loading
Loading
+65 −46
Changes for plugins/staticpage/static.cpp: 65 added lines, 46 removed lines.
Original line number Diff line number Diff line
@@ -237,52 +237,80 @@ void blogi::StaticPage::editPage(const int tid,libhttppp::HttpRequest& req, libh
    libhttppp::HttpForm form;
    form.parse(req);

    auto sv_from_vec = [](const std::vector<char>& v) -> std::string_view {
        std::string_view sv(v.data(), v.size());
        // gängige Parser hinterlassen ein '\0' am Ende – weg damit
        if (!sv.empty() && sv.back() == '\0') sv.remove_suffix(1);
        return sv;
    };

    try {
        sql.clear();
        // 1) pageid aus multipart lesen (alle Dispositions iterieren!)
        for (auto* curformdat = form.MultipartFormData.getFormData();
             curformdat; curformdat = curformdat->nextData())
        {
            for (auto* cd = curformdat->getDisposition();
                 cd; cd = cd->nextContentDisposition())
            {
                // Viele Parser liefern Paare wie name="pageid".
                // Falls du auch getKey() hast, prüfe zusätzlich: if (cd->getKey()=="name")
                const std::string_view disp = sv_from_vec(cd->getValue());
                if (disp == "pageid") {
                    std::string_view val = sv_from_vec(curformdat->Value);
                    uuid::uuid tmp;
                    tmp.parse(std::string(val).c_str());
                    if (!tmp.empty()) { id = tmp; break; }
                }
            }
            if (!id.empty()) break;
        }

        for (libhttppp::HttpForm::MultipartForm::Data* curformdat = form.MultipartFormData.getFormData(); curformdat; curformdat = curformdat->nextData()) {
            libhttppp::HttpForm::MultipartForm::Data::ContentDisposition* curctdisp = curformdat->getDisposition();{
                if(curctdisp->getValue().size() == 6 && std::equal(curctdisp->getValue().begin(),curctdisp->getValue().end(),"pageid")){
                    std::vector<char> buf;
                    std::copy(curformdat->Value.begin(),curformdat->Value.end(),
                              std::inserter<std::vector<char>>(buf,buf.begin()));
                    buf.push_back('\0');
                    id.parse(buf.data());
        // Fallback: urlencoded
        if (id.empty()) {
            for (auto* cdat = form.UrlFormData.getFormData(); cdat; cdat = cdat->nextData()) {
                if (cdat->getKey() == "pageid") {
                    id.parse(cdat->getValue());
                    break;
                }
            }
        }

        if (id.empty()) {
            libhttppp::HTTPException excep;
            excep[libhttppp::HTTPException::Error] << "Staticpage with this id not found!";
            throw excep;
        }

        if (!id.empty()) {
        // 2) Updates einsammeln
        sql.clear();
        for (auto* curformdat = form.MultipartFormData.getFormData();
             curformdat; curformdat = curformdat->nextData())
        {
                     for (auto* curctdisp = curformdat->getDisposition();
                          curctdisp; curctdisp = curctdisp->nextContentDisposition())
            for (auto* cd = curformdat->getDisposition();
                 cd; cd = cd->nextContentDisposition())
            {
                const std::string_view key = sv_from_vec(cd->getValue());
                const std::string_view val_sv = sv_from_vec(curformdat->Value);
                const std::string val(val_sv);  // kopieren wenn wir escapen

                              const std::string key(curctdisp->getValue().begin(),
                                                    curctdisp->getValue().end());

                              const std::string val(curformdat->Value.begin(),
                                                    curformdat->Value.end());
                auto put_update = [&](const char* col, const char* raw) {
                    std::string esc = sql.escaped(sbuf, raw);
                    sql << "UPDATE static_content SET " << col << "='" << esc.c_str()
                        << "' WHERE id='" << id.c_str() << "'; ";
                };

                              std::string esc;
                std::string enc;

                if (key == "url") {
                                  esc = sql.escaped(sbuf, val.c_str());
                                  sql << "UPDATE static_content SET url='" << esc.c_str()
                                  << "' WHERE id='" << id.c_str() << "'; ";
                    libhtmlpp::HtmlEncode(val, enc);
                    put_update("url", enc.c_str());
                } else if (key == "meta") {
                                  esc = sql.escaped(sbuf, val.c_str());
                                  sql << "UPDATE static_content SET meta='" << esc.c_str()
                                  << "' WHERE id='" << id.c_str() << "'; ";
                    libhtmlpp::HtmlEncode(val, enc);
                    put_update("meta", enc.c_str());
                } else if (key == "text") {
                                  libhtmlpp::HtmlString html;
                                  libhtmlpp::HtmlEncode(val.c_str(), &html);
                                  esc = sql.escaped(sbuf, html.c_str());
                                  sql << "UPDATE static_content SET text='" << esc.c_str()
                                  << "' WHERE id='" << id.c_str() << "'; ";
                    libhtmlpp::HtmlString html(val);
                    html.parse();
                    put_update("text", html.c_str());
                }
            }
        }
@@ -291,25 +319,15 @@ void blogi::StaticPage::editPage(const int tid,libhttppp::HttpRequest& req, libh
            Args->database[tid]->exec(sql, res);
            sql.clear();
        }
        } else {
            for (auto* cdat = form.UrlFormData.getFormData(); cdat; cdat = cdat->nextData()) {
                if (cdat->getKey() == "pageid") {
                    id.parse(cdat->getValue());
                }
            }
            if (id.empty()) {
                libhttppp::HTTPException excep;
                excep[libhttppp::HTTPException::Error] << "Staticpage with this id not found!";
                throw excep;
            }
    } catch (const std::exception& e) {
        // Fehler nicht schlucken – wenigstens loggen/weiterreichen
        // log(e.what());
        throw;
    }
    }catch(...){};

    sql << "select id,url,meta,text from static_content where id='" << id.c_str() << "' LIMIT 1";

    if(Args->database[tid]->exec(sql,res)<1){
        return;
    }
    // Seite erneut laden
    sql << "SELECT id,url,meta,text FROM static_content WHERE id='" << id.c_str() << "' LIMIT 1";
    if (Args->database[tid]->exec(sql, res) < 1) return;

    setdiv << "<div id=\"staticsettings\">"
           << "<span>Statische Seiten</span>"
@@ -327,6 +345,7 @@ void blogi::StaticPage::editPage(const int tid,libhttppp::HttpRequest& req, libh
           << "</form></div>";
}


void blogi::StaticPage::uploadPage(const int tid,libhttppp::HttpRequest& req, libhtmlpp::HtmlString& setdiv){
}