Commit 90a946ef authored by jan.koester's avatar jan.koester
Browse files

test

parent b2ee4f0f
Loading
Loading
Loading
Loading
−182 KiB
Loading image diff...
+9.76 KiB
Loading image diff...
+70 −4
Changes for src/blogi.cpp: 70 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -121,7 +121,8 @@ blogi::Blogi::Blogi(std::vector<netplus::socket*> serversocket,bool debug) : Htt
               << "id " << initdb->getUUIDType(optsql) << " PRIMARY KEY,"
               << "profile character varying(32) NOT NULL,"
               << "variable character varying(64) NOT NULL,"
               << "value character varying(64) NOT NULL);";
               << "value character varying(64) NOT NULL);"
           << "ALTER TABLE options ALTER COLUMN value TYPE text;";

    try{
        initdb->exec(optsql,optres);
@@ -1357,10 +1358,12 @@ void blogi::Blogi::Settings(libhttppp::HttpRequest& curreq,const int tid,const s
    }

    if(isAjax){
      try {
        // Get JSON form descriptor from plugin's Settings() method
        json_object *jformdata = nullptr;
        std::string url = curreq.getRequestURL();

        try {
        if(url.compare(0, PlgArgs->config->buildurl("settings/mail").length(),
           PlgArgs->config->buildurl("settings/mail")) == 0){
            jformdata = SettingsMail(*PlgArgs->database[tid], curreq, sessiondid);
@@ -1400,6 +1403,22 @@ void blogi::Blogi::Settings(libhttppp::HttpRequest& curreq,const int tid,const s
                jformdata = nullptr;
            }
        }
        } catch(libhttppp::HTTPException &e) {
            std::cerr << "settings: handler threw HTTPException: " << e.what() << std::endl;
            jformdata = json_object_new_object();
            json_object_object_add(jformdata, "message", json_object_new_string(e.what().c_str()));
            json_object_object_add(jformdata, "message_type", json_object_new_string("error"));
        } catch(std::exception &e) {
            std::cerr << "settings: handler threw: " << e.what() << std::endl;
            jformdata = json_object_new_object();
            json_object_object_add(jformdata, "message", json_object_new_string(e.what()));
            json_object_object_add(jformdata, "message_type", json_object_new_string("error"));
        } catch(...) {
            std::cerr << "settings: handler threw unknown exception" << std::endl;
            jformdata = json_object_new_object();
            json_object_object_add(jformdata, "message", json_object_new_string("internal error"));
            json_object_object_add(jformdata, "message_type", json_object_new_string("error"));
        }

        // Render JSON form descriptor to HTML
        std::string _lang = blogi::getLang(*PlgArgs->database[tid]);
@@ -1424,6 +1443,51 @@ void blogi::Blogi::Settings(libhttppp::HttpRequest& curreq,const int tid,const s

        json_object_put(jres);
        return;
      } catch(libhttppp::HTTPException &e) {
        std::cerr << "settings AJAX: " << e.what() << std::endl;
        json_object *jerr = json_object_new_object();
        json_object_object_add(jerr, "success", json_object_new_boolean(0));
        json_object_object_add(jerr, "html",
            json_object_new_string(
                (std::string("<div class=\"set-msg set-msg-err\">") + e.what() + "</div>").c_str()));
        size_t elen = 0;
        const char *eout = json_object_to_json_string_length(jerr, 0, &elen);
        libhttppp::HttpResponse eresp;
        eresp.setState(HTTP200);
        eresp.setContentType("application/json");
        eresp.send(curreq, eout, elen);
        json_object_put(jerr);
        return;
      } catch(std::exception &e) {
        std::cerr << "settings AJAX: " << e.what() << std::endl;
        json_object *jerr = json_object_new_object();
        json_object_object_add(jerr, "success", json_object_new_boolean(0));
        json_object_object_add(jerr, "html",
            json_object_new_string(
                (std::string("<div class=\"set-msg set-msg-err\">") + e.what() + "</div>").c_str()));
        size_t elen = 0;
        const char *eout = json_object_to_json_string_length(jerr, 0, &elen);
        libhttppp::HttpResponse eresp;
        eresp.setState(HTTP200);
        eresp.setContentType("application/json");
        eresp.send(curreq, eout, elen);
        json_object_put(jerr);
        return;
      } catch(...) {
        std::cerr << "settings AJAX: unknown exception" << std::endl;
        json_object *jerr = json_object_new_object();
        json_object_object_add(jerr, "success", json_object_new_boolean(0));
        json_object_object_add(jerr, "html",
            json_object_new_string("<div class=\"set-msg set-msg-err\">Unknown error in settings handler</div>"));
        size_t elen = 0;
        const char *eout = json_object_to_json_string_length(jerr, 0, &elen);
        libhttppp::HttpResponse eresp;
        eresp.setState(HTTP200);
        eresp.setContentType("application/json");
        eresp.send(curreq, eout, elen);
        json_object_put(jerr);
        return;
      }
    }

    // Non-AJAX: render modal settings UI
@@ -1621,15 +1685,17 @@ void blogi::Blogi::Settings(libhttppp::HttpRequest& curreq,const int tid,const s
    <<   "var method=(form.method||'GET').toUpperCase();"
    <<   "var action=form.getAttribute('action')||_sectionUrl;"
    <<   "var opts={method:method,headers:{'X-Requested-With':'XMLHttpRequest'}};"
    <<   "var fd=new FormData(form);"
    <<   "if(ev.submitter&&ev.submitter.name){fd.append(ev.submitter.name,ev.submitter.value);}"
    <<   "if(method==='POST'){"
    <<     "if(form.enctype==='multipart/form-data'){"
    <<       "opts.body=new FormData(form);"
    <<       "opts.body=fd;"
    <<     "}else{"
    <<       "opts.headers['Content-Type']='application/x-www-form-urlencoded';"
    <<       "opts.body=new URLSearchParams(new FormData(form)).toString();"
    <<       "opts.body=new URLSearchParams(fd).toString();"
    <<     "}"
    <<   "}else{"
    <<     "var params=new URLSearchParams(new FormData(form)).toString();"
    <<     "var params=new URLSearchParams(fd).toString();"
    <<     "if(params) action+=(action.indexOf('?')!==-1?'&':'?')+params;"
    <<   "}"
    <<   "_sectionUrl=action;"
+18 −4
Changes for src/theme.cpp: 18 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -653,11 +653,25 @@ json_object* blogi::Theme::SettingsTheme(const int tid, libhttppp::HttpRequest &
    };

    if(reset){
        // Reset all theme colors to defaults
        // Batch reset: delete all theme color keys and re-insert defaults in one SQL
        dbpp::SQL rsql;
        dbpp::DBResult rres;
        std::vector<char> buf;
        rsql << "DELETE FROM options WHERE key LIKE 'THEME_DARK_%' OR key LIKE 'THEME_LIGHT_%';";
        for(auto &c : colors){
            setConfig(*_Config.TDatabase[tid], std::string("THEME_DARK_") + c.suffix, c.darkDef);
            setConfig(*_Config.TDatabase[tid], std::string("THEME_LIGHT_") + c.suffix, c.lightDef);
        }
            for(int p = 0; p < 2; ++p){
                const char *prefix = p == 0 ? "THEME_DARK_" : "THEME_LIGHT_";
                const char *val    = p == 0 ? c.darkDef : c.lightDef;
                uuid::uuid id;
                id.generate();
                rsql << "INSERT INTO options (id,key,value) VALUES ('"
                     << id.c_str() << "','"
                     << dbpp::SQL::escaped(buf, (std::string(prefix) + c.suffix).c_str())
                     << "','"
                     << dbpp::SQL::escaped(buf, val) << "');";
            }
        }
        _Config.TDatabase[tid]->exec(rsql, rres);
    } else {
        for(auto &profile : profiles){
            std::string pfx = std::string("THEME_") + (std::string(profile) == "light" ? "LIGHT" : "DARK") + "_";