Loading plugins/survey/survey.cpp +246 −0 Changes for plugins/survey/survey.cpp: 246 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -144,6 +144,7 @@ namespace blogi { else if (sub.find("delete") == 0) return settingsDelete(tid, req); else if (sub.find("results") == 0) return settingsResults(tid, req); else if (sub.find("config") == 0) return settingsConfig(tid, req); else if (sub.find("import") == 0) return settingsImportForm(tid, req); else if (sub.find("emailtemplate") == 0) return settingsEmailTemplate(tid, req); else if (sub.find("gdrive") == 0) return settingsGDrive(tid, req); } Loading @@ -169,6 +170,16 @@ namespace blogi { return handleApiSave(tid, req, sessionid); } // Export endpoint: survey/_api/export?name=xxx if (surveyName == "_api/export") { return handleExport(tid, req, sessionid); } // Import endpoint: survey/_api/import (POST) if (surveyName == "_api/import" && req.getRequestType() == POSTREQUEST) { return handleImport(tid, req, sessionid); } auto it = _templates.find(surveyName); if (it == _templates.end()) return false; Loading Loading @@ -1563,6 +1574,12 @@ namespace blogi { (Args->config->buildurl("settings/survey/results?name=") + name).c_str())); json_object_array_add(jactions, jres); json_object *jexp = json_object_new_object(); json_object_object_add(jexp, "label", json_object_new_string(tr(_lang, "Export").c_str())); json_object_object_add(jexp, "url", json_object_new_string( (Args->config->buildurl("survey/_api/export?name=") + name).c_str())); json_object_array_add(jactions, jexp); json_object *jdel = json_object_new_object(); json_object_object_add(jdel, "label", json_object_new_string(tr(_lang, "Remove").c_str())); json_object_object_add(jdel, "url", json_object_new_string( Loading @@ -1582,6 +1599,12 @@ namespace blogi { Args->config->buildurl("settings/survey/add").c_str())); json_object_array_add(jlinks, jnewLink); json_object *jimportLink = json_object_new_object(); json_object_object_add(jimportLink, "label", json_object_new_string(tr(_lang, "Import Survey").c_str())); json_object_object_add(jimportLink, "url", json_object_new_string( Args->config->buildurl("settings/survey/import").c_str())); json_object_array_add(jlinks, jimportLink); json_object *jcfgLink = json_object_new_object(); json_object_object_add(jcfgLink, "label", json_object_new_string(tr(_lang, "Survey Settings").c_str())); json_object_object_add(jcfgLink, "url", json_object_new_string( Loading Loading @@ -2416,6 +2439,229 @@ namespace blogi { return true; } // ----- Export: download survey as JSON file ----- bool handleExport(const int tid, libhttppp::HttpRequest &req, const std::string &sessionid) { libhttppp::HttpResponse resp; if (!Args->auth->isLoggedIn(tid, req, sessionid)) { resp.setState(HTTP403); resp.send(req, "", 0); return true; } // Parse ?name= from query string std::string name; std::string fullUrl = req.getRequest(); size_t qpos = fullUrl.find("name="); if (qpos != std::string::npos) { name = fullUrl.substr(qpos + 5); size_t amp = name.find('&'); if (amp != std::string::npos) name = name.substr(0, amp); if (!name.empty() && name.back() == '/') name.pop_back(); } if (name.empty()) { std::string err = "{\"error\":\"missing name parameter\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } loadTemplates(tid); auto it = _templates.find(name); if (it == _templates.end()) { std::string err = "{\"error\":\"survey not found\"}"; resp.setState(HTTP404); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } // Build export JSON: { name, survey: {...} } json_object *jexport = json_object_new_object(); json_object_object_add(jexport, "name", json_object_new_string(name.c_str())); json_object *jtpl = templateToJson(it->second); json_object_object_add(jexport, "survey", jtpl); std::string body = json_object_to_json_string_ext(jexport, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE); json_object_put(jexport); resp.setState(HTTP200); resp.setContentType("application/json; charset=utf-8"); std::string disposition = "attachment; filename=\"" + name + ".json\""; resp.setHeaderData("Content-Disposition")->push_back(disposition); resp.setContentLength(body.size()); resp.send(req, body.c_str(), body.size()); return true; } // ----- Import: receive JSON file and create survey ----- bool handleImport(const int tid, libhttppp::HttpRequest &req, const std::string &sessionid) { libhttppp::HttpResponse resp; if (!Args->auth->isLoggedIn(tid, req, sessionid)) { resp.setState(HTTP403); resp.send(req, "", 0); return true; } size_t clen = req.getContentLength(); if (clen == 0 || clen > req.RecvData.size()) { std::string err = "{\"status\":\"error\",\"message\":\"Empty request body\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } std::string body(req.RecvData.data(), clen); json_object *jreq = json_tokener_parse(body.c_str()); if (!jreq) { std::string err = "{\"status\":\"error\",\"message\":\"Invalid JSON\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } json_object *jname_obj = nullptr, *jsurvey_obj = nullptr; json_object_object_get_ex(jreq, "name", &jname_obj); json_object_object_get_ex(jreq, "survey", &jsurvey_obj); if (!jname_obj || !jsurvey_obj) { json_object_put(jreq); std::string err = "{\"status\":\"error\",\"message\":\"Missing name or survey\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } std::string sname = json_object_get_string(jname_obj); SurveyTemplate tpl; if (!templateFromJson(jsurvey_obj, tpl)) { json_object_put(jreq); std::string err = "{\"status\":\"error\",\"message\":\"Invalid survey data\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } std::string xmlStr = toXml(tpl); json_object_put(jreq); dbpp::SQL sql; dbpp::DBResult res; std::vector<char> sbuf; // Check if name already exists sql << "SELECT name FROM survey_templates WHERE name='" << sql.escaped(sbuf, sname.c_str()) << "';"; int existing = 0; try { existing = Args->database[tid]->exec(sql, res); } catch (...) {} if (existing > 0) { std::string err = "{\"status\":\"error\",\"message\":\"Survey '\" + jsonEscape(sname) + \"' already exists\"}"; resp.setState(HTTP409); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } sql.clear(); sql << "INSERT INTO survey_templates (name, xml_data, title) VALUES('" << sql.escaped(sbuf, sname.c_str()) << "','" << sql.escaped(sbuf, xmlStr.c_str()) << "','" << sql.escaped(sbuf, tpl.title.c_str()) << "');"; try { Args->database[tid]->exec(sql, res); } catch (...) { std::string err = "{\"status\":\"error\",\"message\":\"Database error\"}"; resp.setState(HTTP500); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } loadTemplates(tid); std::string ok = "{\"status\":\"ok\",\"message\":\"Imported successfully\"}"; resp.setState(HTTP200); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(ok.size()); resp.send(req, ok.c_str(), ok.size()); return true; } // ----- Settings: Import form page ----- json_object* settingsImportForm(const int tid, libhttppp::HttpRequest &req) { std::string _lang = getLang(*Args->database[tid]); json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang, "Import Survey").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 list").c_str())); json_object_object_add(jback, "url", json_object_new_string( Args->config->buildurl("settings/survey").c_str())); json_object_array_add(jnav, jback); json_object_object_add(jroot, "nav", jnav); // Inline JS import UI std::ostringstream html; html << "<div id=\"survey-import\">" << "<p>" << tr(_lang, "Select a .json file exported from another survey instance.") << "</p>" << "<input type=\"file\" id=\"import-file\" accept=\".json,application/json\" />" << "<br/><br/>" << "<button id=\"import-btn\" disabled>" << tr(_lang, "Import") << "</button>" << "<div id=\"import-msg\" style=\"margin-top:10px;\"></div>" << "</div>" << "<script>" << "(function(){" << "var fileInput=document.getElementById('import-file');" << "var btn=document.getElementById('import-btn');" << "var msg=document.getElementById('import-msg');" << "var fileData=null;" << "fileInput.addEventListener('change',function(){" << "var f=fileInput.files[0];if(!f){btn.disabled=true;return;}" << "var reader=new FileReader();" << "reader.onload=function(e){" << "try{fileData=JSON.parse(e.target.result);" << "if(!fileData.name||!fileData.survey){msg.textContent='Invalid file format: missing name or survey';msg.style.color='red';btn.disabled=true;return;}" << "msg.textContent='Ready to import: '+fileData.name;msg.style.color='green';btn.disabled=false;" << "}catch(err){msg.textContent='Invalid JSON: '+err.message;msg.style.color='red';btn.disabled=true;}};" << "reader.readAsText(f);});" << "btn.addEventListener('click',function(){" << "if(!fileData)return;btn.disabled=true;btn.textContent='" << tr(_lang, "Importing...") << "';" << "var xhr=new XMLHttpRequest();" << "xhr.open('POST','" << Args->config->buildurl("survey/_api/import") << "',true);" << "xhr.setRequestHeader('Content-Type','application/json');" << "xhr.onreadystatechange=function(){" << "if(xhr.readyState===4){" << "try{var r=JSON.parse(xhr.responseText);" << "if(r.status==='ok'){msg.textContent=r.message||'Imported!';msg.style.color='green';" << "setTimeout(function(){window.location.href='" << Args->config->buildurl("settings/survey") << "';},1000);}" << "else{msg.textContent=r.message||'Error';msg.style.color='red';}" << "}catch(e){msg.textContent='Network error';msg.style.color='red';}" << "btn.disabled=false;btn.textContent='" << tr(_lang, "Import") << "';}};" << "xhr.send(JSON.stringify(fileData));});" << "})();" << "</script>"; json_object_object_add(jroot, "html", json_object_new_string(html.str().c_str())); return jroot; } // ----- Settings API: AJAX/JSON CRUD for survey templates (LEGACY) ----- json_object* settingsApi(const int tid, libhttppp::HttpRequest &req) { std::string curl = req.getRequest(); Loading Loading
plugins/survey/survey.cpp +246 −0 Changes for plugins/survey/survey.cpp: 246 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -144,6 +144,7 @@ namespace blogi { else if (sub.find("delete") == 0) return settingsDelete(tid, req); else if (sub.find("results") == 0) return settingsResults(tid, req); else if (sub.find("config") == 0) return settingsConfig(tid, req); else if (sub.find("import") == 0) return settingsImportForm(tid, req); else if (sub.find("emailtemplate") == 0) return settingsEmailTemplate(tid, req); else if (sub.find("gdrive") == 0) return settingsGDrive(tid, req); } Loading @@ -169,6 +170,16 @@ namespace blogi { return handleApiSave(tid, req, sessionid); } // Export endpoint: survey/_api/export?name=xxx if (surveyName == "_api/export") { return handleExport(tid, req, sessionid); } // Import endpoint: survey/_api/import (POST) if (surveyName == "_api/import" && req.getRequestType() == POSTREQUEST) { return handleImport(tid, req, sessionid); } auto it = _templates.find(surveyName); if (it == _templates.end()) return false; Loading Loading @@ -1563,6 +1574,12 @@ namespace blogi { (Args->config->buildurl("settings/survey/results?name=") + name).c_str())); json_object_array_add(jactions, jres); json_object *jexp = json_object_new_object(); json_object_object_add(jexp, "label", json_object_new_string(tr(_lang, "Export").c_str())); json_object_object_add(jexp, "url", json_object_new_string( (Args->config->buildurl("survey/_api/export?name=") + name).c_str())); json_object_array_add(jactions, jexp); json_object *jdel = json_object_new_object(); json_object_object_add(jdel, "label", json_object_new_string(tr(_lang, "Remove").c_str())); json_object_object_add(jdel, "url", json_object_new_string( Loading @@ -1582,6 +1599,12 @@ namespace blogi { Args->config->buildurl("settings/survey/add").c_str())); json_object_array_add(jlinks, jnewLink); json_object *jimportLink = json_object_new_object(); json_object_object_add(jimportLink, "label", json_object_new_string(tr(_lang, "Import Survey").c_str())); json_object_object_add(jimportLink, "url", json_object_new_string( Args->config->buildurl("settings/survey/import").c_str())); json_object_array_add(jlinks, jimportLink); json_object *jcfgLink = json_object_new_object(); json_object_object_add(jcfgLink, "label", json_object_new_string(tr(_lang, "Survey Settings").c_str())); json_object_object_add(jcfgLink, "url", json_object_new_string( Loading Loading @@ -2416,6 +2439,229 @@ namespace blogi { return true; } // ----- Export: download survey as JSON file ----- bool handleExport(const int tid, libhttppp::HttpRequest &req, const std::string &sessionid) { libhttppp::HttpResponse resp; if (!Args->auth->isLoggedIn(tid, req, sessionid)) { resp.setState(HTTP403); resp.send(req, "", 0); return true; } // Parse ?name= from query string std::string name; std::string fullUrl = req.getRequest(); size_t qpos = fullUrl.find("name="); if (qpos != std::string::npos) { name = fullUrl.substr(qpos + 5); size_t amp = name.find('&'); if (amp != std::string::npos) name = name.substr(0, amp); if (!name.empty() && name.back() == '/') name.pop_back(); } if (name.empty()) { std::string err = "{\"error\":\"missing name parameter\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } loadTemplates(tid); auto it = _templates.find(name); if (it == _templates.end()) { std::string err = "{\"error\":\"survey not found\"}"; resp.setState(HTTP404); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } // Build export JSON: { name, survey: {...} } json_object *jexport = json_object_new_object(); json_object_object_add(jexport, "name", json_object_new_string(name.c_str())); json_object *jtpl = templateToJson(it->second); json_object_object_add(jexport, "survey", jtpl); std::string body = json_object_to_json_string_ext(jexport, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE); json_object_put(jexport); resp.setState(HTTP200); resp.setContentType("application/json; charset=utf-8"); std::string disposition = "attachment; filename=\"" + name + ".json\""; resp.setHeaderData("Content-Disposition")->push_back(disposition); resp.setContentLength(body.size()); resp.send(req, body.c_str(), body.size()); return true; } // ----- Import: receive JSON file and create survey ----- bool handleImport(const int tid, libhttppp::HttpRequest &req, const std::string &sessionid) { libhttppp::HttpResponse resp; if (!Args->auth->isLoggedIn(tid, req, sessionid)) { resp.setState(HTTP403); resp.send(req, "", 0); return true; } size_t clen = req.getContentLength(); if (clen == 0 || clen > req.RecvData.size()) { std::string err = "{\"status\":\"error\",\"message\":\"Empty request body\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } std::string body(req.RecvData.data(), clen); json_object *jreq = json_tokener_parse(body.c_str()); if (!jreq) { std::string err = "{\"status\":\"error\",\"message\":\"Invalid JSON\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } json_object *jname_obj = nullptr, *jsurvey_obj = nullptr; json_object_object_get_ex(jreq, "name", &jname_obj); json_object_object_get_ex(jreq, "survey", &jsurvey_obj); if (!jname_obj || !jsurvey_obj) { json_object_put(jreq); std::string err = "{\"status\":\"error\",\"message\":\"Missing name or survey\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } std::string sname = json_object_get_string(jname_obj); SurveyTemplate tpl; if (!templateFromJson(jsurvey_obj, tpl)) { json_object_put(jreq); std::string err = "{\"status\":\"error\",\"message\":\"Invalid survey data\"}"; resp.setState(HTTP400); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } std::string xmlStr = toXml(tpl); json_object_put(jreq); dbpp::SQL sql; dbpp::DBResult res; std::vector<char> sbuf; // Check if name already exists sql << "SELECT name FROM survey_templates WHERE name='" << sql.escaped(sbuf, sname.c_str()) << "';"; int existing = 0; try { existing = Args->database[tid]->exec(sql, res); } catch (...) {} if (existing > 0) { std::string err = "{\"status\":\"error\",\"message\":\"Survey '\" + jsonEscape(sname) + \"' already exists\"}"; resp.setState(HTTP409); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } sql.clear(); sql << "INSERT INTO survey_templates (name, xml_data, title) VALUES('" << sql.escaped(sbuf, sname.c_str()) << "','" << sql.escaped(sbuf, xmlStr.c_str()) << "','" << sql.escaped(sbuf, tpl.title.c_str()) << "');"; try { Args->database[tid]->exec(sql, res); } catch (...) { std::string err = "{\"status\":\"error\",\"message\":\"Database error\"}"; resp.setState(HTTP500); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(err.size()); resp.send(req, err.c_str(), err.size()); return true; } loadTemplates(tid); std::string ok = "{\"status\":\"ok\",\"message\":\"Imported successfully\"}"; resp.setState(HTTP200); resp.setContentType("application/json; charset=utf-8"); resp.setContentLength(ok.size()); resp.send(req, ok.c_str(), ok.size()); return true; } // ----- Settings: Import form page ----- json_object* settingsImportForm(const int tid, libhttppp::HttpRequest &req) { std::string _lang = getLang(*Args->database[tid]); json_object *jroot = json_object_new_object(); json_object_object_add(jroot, "title", json_object_new_string(tr(_lang, "Import Survey").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 list").c_str())); json_object_object_add(jback, "url", json_object_new_string( Args->config->buildurl("settings/survey").c_str())); json_object_array_add(jnav, jback); json_object_object_add(jroot, "nav", jnav); // Inline JS import UI std::ostringstream html; html << "<div id=\"survey-import\">" << "<p>" << tr(_lang, "Select a .json file exported from another survey instance.") << "</p>" << "<input type=\"file\" id=\"import-file\" accept=\".json,application/json\" />" << "<br/><br/>" << "<button id=\"import-btn\" disabled>" << tr(_lang, "Import") << "</button>" << "<div id=\"import-msg\" style=\"margin-top:10px;\"></div>" << "</div>" << "<script>" << "(function(){" << "var fileInput=document.getElementById('import-file');" << "var btn=document.getElementById('import-btn');" << "var msg=document.getElementById('import-msg');" << "var fileData=null;" << "fileInput.addEventListener('change',function(){" << "var f=fileInput.files[0];if(!f){btn.disabled=true;return;}" << "var reader=new FileReader();" << "reader.onload=function(e){" << "try{fileData=JSON.parse(e.target.result);" << "if(!fileData.name||!fileData.survey){msg.textContent='Invalid file format: missing name or survey';msg.style.color='red';btn.disabled=true;return;}" << "msg.textContent='Ready to import: '+fileData.name;msg.style.color='green';btn.disabled=false;" << "}catch(err){msg.textContent='Invalid JSON: '+err.message;msg.style.color='red';btn.disabled=true;}};" << "reader.readAsText(f);});" << "btn.addEventListener('click',function(){" << "if(!fileData)return;btn.disabled=true;btn.textContent='" << tr(_lang, "Importing...") << "';" << "var xhr=new XMLHttpRequest();" << "xhr.open('POST','" << Args->config->buildurl("survey/_api/import") << "',true);" << "xhr.setRequestHeader('Content-Type','application/json');" << "xhr.onreadystatechange=function(){" << "if(xhr.readyState===4){" << "try{var r=JSON.parse(xhr.responseText);" << "if(r.status==='ok'){msg.textContent=r.message||'Imported!';msg.style.color='green';" << "setTimeout(function(){window.location.href='" << Args->config->buildurl("settings/survey") << "';},1000);}" << "else{msg.textContent=r.message||'Error';msg.style.color='red';}" << "}catch(e){msg.textContent='Network error';msg.style.color='red';}" << "btn.disabled=false;btn.textContent='" << tr(_lang, "Import") << "';}};" << "xhr.send(JSON.stringify(fileData));});" << "})();" << "</script>"; json_object_object_add(jroot, "html", json_object_new_string(html.str().c_str())); return jroot; } // ----- Settings API: AJAX/JSON CRUD for survey templates (LEGACY) ----- json_object* settingsApi(const int tid, libhttppp::HttpRequest &req) { std::string curl = req.getRequest(); Loading