Commit 22801166 authored by jan.koester's avatar jan.koester
Browse files

test

parent fda8583e
Loading
Loading
Loading
Loading
+142 −88
Changes for plugins/survey/survey.cpp: 142 added lines, 88 removed lines.
Original line number Diff line number Diff line
@@ -2065,6 +2065,7 @@ namespace blogi {
        json_object* settingsGDrive(const int tid, libhttppp::HttpRequest &req) {
            std::string _lang = getLang(*Args->database[tid]);
            bool saved = false;
            std::string statusMsg;

            libhttppp::HttpForm form;
            form.parse(req);
@@ -2094,10 +2095,8 @@ namespace blogi {
                    setConfig(*Args->database[tid], "SURVEY_GDRIVE_CLIENT_SECRET", cur.value);
                    _gdriveClientSecret = cur.value;
                    saved = true;
                } else if (cur.key == "gdrive_refresh_token") {
                    setConfig(*Args->database[tid], "SURVEY_GDRIVE_REFRESH_TOKEN", cur.value);
                    _gdriveRefreshToken = cur.value;
                    saved = true;
                } else if (cur.key == "status") {
                    statusMsg = cur.value;
                }
            }

@@ -2122,6 +2121,11 @@ namespace blogi {
                try { getConfig(*Args->database[tid], "SURVEY_GDRIVE_REFRESH_TOKEN", _gdriveRefreshToken); } catch (...) {}
            }

            // Check status query param (set by OAuth callback)
            for (const auto &cur : form.urlData()) {
                if (cur.key == "status") statusMsg = cur.value;
            }

            json_object *jroot = json_object_new_object();
            json_object_object_add(jroot, "title", json_object_new_string(tr(_lang, "Google Sheets Settings").c_str()));

@@ -2130,106 +2134,156 @@ namespace blogi {
                json_object_object_add(jroot, "message_type", json_object_new_string("success"));
            }

            json_object *jform = json_object_new_object();
            json_object_object_add(jform, "method", json_object_new_string("POST"));
            json_object_object_add(jform, "action", json_object_new_string(
                Args->config->buildurl("settings/survey/gdrive").c_str()));

            json_object *jfields = json_object_new_array();

            // Enabled
            json_object *jf1 = json_object_new_object();
            json_object_object_add(jf1, "name", json_object_new_string("gdrive_enabled"));
            json_object_object_add(jf1, "label", json_object_new_string(tr(_lang, "Enable Google Sheets").c_str()));
            json_object_object_add(jf1, "type", json_object_new_string("checkbox"));
            json_object_object_add(jf1, "value", json_object_new_string(_gdriveEnabled ? "on" : ""));
            json_object_array_add(jfields, jf1);

            // Auth mode
            json_object *jfMode = json_object_new_object();
            json_object_object_add(jfMode, "name", json_object_new_string("gdrive_auth_mode"));
            json_object_object_add(jfMode, "label", json_object_new_string(tr(_lang, "Auth Mode").c_str()));
            json_object_object_add(jfMode, "type", json_object_new_string("select"));
            json_object *jmodeOpts = json_object_new_array();
            json_object *jmoSA = json_object_new_object();
            json_object_object_add(jmoSA, "value", json_object_new_string("serviceaccount"));
            json_object_object_add(jmoSA, "label", json_object_new_string("Service Account (JWT)"));
            if (_gdriveAuthMode != "oauth") json_object_object_add(jmoSA, "selected", json_object_new_boolean(true));
            json_object_array_add(jmodeOpts, jmoSA);
            json_object *jmoOA = json_object_new_object();
            json_object_object_add(jmoOA, "value", json_object_new_string("oauth"));
            json_object_object_add(jmoOA, "label", json_object_new_string("OAuth2"));
            if (_gdriveAuthMode == "oauth") json_object_object_add(jmoOA, "selected", json_object_new_boolean(true));
            json_object_array_add(jmodeOpts, jmoOA);
            json_object_object_add(jfMode, "options", jmodeOpts);
            json_object_array_add(jfields, jfMode);

            // Spreadsheet ID (both modes)
            json_object *jf3 = json_object_new_object();
            json_object_object_add(jf3, "name", json_object_new_string("gdrive_spreadsheet_id"));
            json_object_object_add(jf3, "label", json_object_new_string(tr(_lang, "Spreadsheet ID").c_str()));
            json_object_object_add(jf3, "type", json_object_new_string("text"));
            json_object_object_add(jf3, "value", json_object_new_string(_gdriveSpreadsheetId.c_str()));
            json_object_array_add(jfields, jf3);

            // Service Account file (SA mode)
            json_object *jf2 = json_object_new_object();
            json_object_object_add(jf2, "name", json_object_new_string("gdrive_sa_file"));
            json_object_object_add(jf2, "label", json_object_new_string(tr(_lang, "Service Account JSON file path").c_str()));
            json_object_object_add(jf2, "type", json_object_new_string("text"));
            json_object_object_add(jf2, "value", json_object_new_string(_gdriveServiceAccountFile.c_str()));
            json_object_array_add(jfields, jf2);

            // OAuth2 fields
            json_object *jfCid = json_object_new_object();
            json_object_object_add(jfCid, "name", json_object_new_string("gdrive_client_id"));
            json_object_object_add(jfCid, "label", json_object_new_string("OAuth2 Client ID"));
            json_object_object_add(jfCid, "type", json_object_new_string("text"));
            json_object_object_add(jfCid, "value", json_object_new_string(_gdriveClientId.c_str()));
            json_object_array_add(jfields, jfCid);

            json_object *jfCs = json_object_new_object();
            json_object_object_add(jfCs, "name", json_object_new_string("gdrive_client_secret"));
            json_object_object_add(jfCs, "label", json_object_new_string("OAuth2 Client Secret"));
            json_object_object_add(jfCs, "type", json_object_new_string("password"));
            json_object_object_add(jfCs, "value", json_object_new_string(_gdriveClientSecret.c_str()));
            json_object_array_add(jfields, jfCs);

            json_object *jfRt = json_object_new_object();
            json_object_object_add(jfRt, "name", json_object_new_string("gdrive_refresh_token"));
            json_object_object_add(jfRt, "label", json_object_new_string("OAuth2 Refresh Token"));
            json_object_object_add(jfRt, "type", json_object_new_string("text"));
            json_object_object_add(jfRt, "value", json_object_new_string(_gdriveRefreshToken.c_str()));
            json_object_array_add(jfields, jfRt);

            json_object_object_add(jform, "fields", jfields);
            json_object_object_add(jform, "submit", json_object_new_string(tr(_lang, "save").c_str()));
            json_object_object_add(jroot, "form", jform);

            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);

            // OAuth2 authorize link
            if (_gdriveAuthMode == "oauth" && !_gdriveClientId.empty()) {
                json_object *jauth = json_object_new_object();
            // Build OAuth redirect URL
            std::string redirectUri = Args->config->getsiteurl() + Args->config->buildurl("survey/_api/gdrive/callback");
                std::string authUrl = "https://accounts.google.com/o/oauth2/v2/auth"
            std::string authUrl;
            if (!_gdriveClientId.empty()) {
                authUrl = "https://accounts.google.com/o/oauth2/v2/auth"
                    "?client_id=" + _gdriveClientId
                    + "&redirect_uri=" + redirectUri
                    + "&response_type=code"
                    + "&scope=https://www.googleapis.com/auth/spreadsheets"
                    + "&access_type=offline&prompt=consent";
                json_object_object_add(jauth, "label", json_object_new_string(tr(_lang, "Authorize with Google").c_str()));
                json_object_object_add(jauth, "url", json_object_new_string(authUrl.c_str()));
                json_object_array_add(jnav, jauth);
            }

            json_object_object_add(jroot, "nav", jnav);
            bool oauthConnected = (_gdriveAuthMode == "oauth" && !_gdriveRefreshToken.empty());
            std::string formAction = Args->config->buildurl("settings/survey/gdrive");

            // Build Wizard HTML
            std::ostringstream html;
            html << "<style>"
                 << ".gd-wizard{font-family:system-ui,sans-serif;max-width:640px;}"
                 << ".gd-step{border:1px solid var(--blogi-border,#ddd);border-radius:8px;padding:16px;margin:12px 0;"
                 << "background:var(--blogi-card-bg,#fff);}"
                 << ".gd-step h3{margin:0 0 12px;font-size:1.1em;display:flex;align-items:center;gap:8px;}"
                 << ".gd-step-num{display:inline-flex;align-items:center;justify-content:center;width:28px;height:28px;"
                 << "border-radius:50%;font-weight:700;font-size:.9em;flex-shrink:0;}"
                 << ".gd-step-num.done{background:#22c55e;color:#fff;}"
                 << ".gd-step-num.pending{background:var(--blogi-border,#ddd);color:#666;}"
                 << ".gd-step-num.active{background:#3b82f6;color:#fff;}"
                 << ".gd-field{margin:8px 0;}"
                 << ".gd-field label{display:block;font-weight:600;margin-bottom:4px;font-size:.9em;}"
                 << ".gd-field input,.gd-field select{width:100%;padding:8px;border:1px solid var(--blogi-border,#ddd);"
                 << "border-radius:6px;font-size:.95em;box-sizing:border-box;}"
                 << ".gd-btn{display:inline-block;padding:10px 20px;border:none;border-radius:6px;font-size:.95em;"
                 << "cursor:pointer;text-decoration:none;font-weight:600;margin:4px 4px 4px 0;}"
                 << ".gd-btn-primary{background:#3b82f6;color:#fff;}"
                 << ".gd-btn-google{background:#4285f4;color:#fff;}"
                 << ".gd-btn-save{background:#22c55e;color:#fff;}"
                 << ".gd-status{padding:10px 14px;border-radius:6px;margin:8px 0;font-size:.9em;}"
                 << ".gd-status-ok{background:#dcfce7;color:#166534;border:1px solid #86efac;}"
                 << ".gd-status-warn{background:#fef3c7;color:#92400e;border:1px solid #fcd34d;}"
                 << ".gd-status-err{background:#fef2f2;color:#991b1b;border:1px solid #fca5a5;}"
                 << ".gd-row{display:flex;gap:12px;align-items:end;}"
                 << ".gd-row>.gd-field{flex:1;}"
                 << "</style>"
                 << "<div class=\"gd-wizard\">";

            // Status message from OAuth callback
            if (statusMsg == "ok")
                html << "<div class=\"gd-status gd-status-ok\">&#10004; Google-Konto erfolgreich verbunden!</div>";
            else if (statusMsg == "error")
                html << "<div class=\"gd-status gd-status-err\">Verbindung fehlgeschlagen. Bitte erneut versuchen.</div>";

            html << "<form method=\"POST\" action=\"" << htmlEscape(formAction) << "\">";

            // Step 1: Enable + Auth Mode
            int step1State = _gdriveEnabled ? 2 : 1; // 2=done, 1=active
            html << "<div class=\"gd-step\">"
                 << "<h3><span class=\"gd-step-num " << (step1State == 2 ? "done" : "active") << "\">1</span>Aktivieren</h3>"
                 << "<div class=\"gd-row\">"
                 << "<div class=\"gd-field\"><label>Google Sheets aktivieren</label>"
                 << "<input type=\"checkbox\" name=\"gdrive_enabled\" value=\"on\"" << (_gdriveEnabled ? " checked" : "") << " style=\"width:auto;\"/></div>"
                 << "<div class=\"gd-field\"><label>Auth-Methode</label>"
                 << "<select name=\"gdrive_auth_mode\" id=\"gd-mode\">"
                 << "<option value=\"oauth\"" << (_gdriveAuthMode == "oauth" ? " selected" : "") << ">OAuth2 (empfohlen)</option>"
                 << "<option value=\"serviceaccount\"" << (_gdriveAuthMode != "oauth" ? " selected" : "") << ">Service Account</option>"
                 << "</select></div></div></div>";

            // Step 2: Credentials
            bool hasCredentials = (_gdriveAuthMode == "oauth")
                ? (!_gdriveClientId.empty() && !_gdriveClientSecret.empty())
                : (!_gdriveServiceAccountFile.empty());
            int step2State = hasCredentials ? 2 : (_gdriveEnabled ? 1 : 0);

            html << "<div class=\"gd-step\">"
                 << "<h3><span class=\"gd-step-num " << (step2State == 2 ? "done" : step2State == 1 ? "active" : "pending") << "\">2</span>Zugangsdaten</h3>";

            // OAuth fields
            html << "<div id=\"gd-oauth-fields\">"
                 << "<div class=\"gd-field\"><label>Client ID</label>"
                 << "<input type=\"text\" name=\"gdrive_client_id\" value=\"" << htmlEscape(_gdriveClientId) << "\" placeholder=\"aus Google Cloud Console\"/></div>"
                 << "<div class=\"gd-field\"><label>Client Secret</label>"
                 << "<input type=\"password\" name=\"gdrive_client_secret\" value=\"" << htmlEscape(_gdriveClientSecret) << "\" placeholder=\"aus Google Cloud Console\"/></div>"
                 << "<p style=\"font-size:.85em;color:#666;margin:4px 0;\">Redirect URI f&uuml;r Google Cloud Console: <code>"
                 << htmlEscape(redirectUri) << "</code></p>"
                 << "</div>";

            // SA fields
            html << "<div id=\"gd-sa-fields\">"
                 << "<div class=\"gd-field\"><label>Service Account JSON Dateipfad</label>"
                 << "<input type=\"text\" name=\"gdrive_sa_file\" value=\"" << htmlEscape(_gdriveServiceAccountFile) << "\" placeholder=\"/pfad/zur/sa.json\"/></div>"
                 << "</div></div>";

            // Step 3: Google verbinden (OAuth only)
            int step3State = oauthConnected ? 2 : (hasCredentials && _gdriveAuthMode == "oauth" ? 1 : 0);

            html << "<div class=\"gd-step\" id=\"gd-step-connect\">"
                 << "<h3><span class=\"gd-step-num " << (step3State == 2 ? "done" : step3State == 1 ? "active" : "pending") << "\">3</span>Google-Konto verbinden</h3>";

            if (oauthConnected) {
                html << "<div class=\"gd-status gd-status-ok\">&#10004; Verbunden &mdash; Refresh Token vorhanden</div>";
            } else if (_gdriveAuthMode == "oauth" && hasCredentials) {
                html << "<p style=\"font-size:.9em;margin:4px 0;\">Speichere zuerst die Zugangsdaten, dann klicke hier:</p>";
            } else if (_gdriveAuthMode == "oauth") {
                html << "<p style=\"font-size:.9em;color:#666;\">Bitte zuerst Client ID und Secret eingeben und speichern.</p>";
            } else {
                html << "<p style=\"font-size:.9em;color:#666;\">Bei Service Account nicht nötig.</p>";
            }

            if (!authUrl.empty() && _gdriveAuthMode == "oauth")
                html << "<a href=\"" << htmlEscape(authUrl) << "\" class=\"gd-btn gd-btn-google\">&#x1F511; Bei Google anmelden</a>";

            html << "</div>";

            // Step 4: Spreadsheet ID
            int step4State = !_gdriveSpreadsheetId.empty() ? 2 : ((step3State >= 1 || _gdriveAuthMode != "oauth") ? 1 : 0);

            html << "<div class=\"gd-step\">"
                 << "<h3><span class=\"gd-step-num " << (step4State == 2 ? "done" : step4State == 1 ? "active" : "pending") << "\">4</span>Spreadsheet</h3>"
                 << "<div class=\"gd-field\"><label>Spreadsheet ID</label>"
                 << "<input type=\"text\" name=\"gdrive_spreadsheet_id\" value=\"" << htmlEscape(_gdriveSpreadsheetId) << "\" placeholder=\"aus der Spreadsheet-URL\"/></div>"
                 << "<p style=\"font-size:.85em;color:#666;margin:4px 0;\">Die ID steht in der URL: docs.google.com/spreadsheets/d/<b>SPREADSHEET_ID</b>/edit</p>"
                 << "</div>";

            html << "<button type=\"submit\" class=\"gd-btn gd-btn-save\">&#x1F4BE; Speichern</button>"
                 << "</form>";

            // JS: show/hide fields based on auth mode
            html << "<script>"
                 << "(function(){"
                 << "var mode=document.getElementById('gd-mode');"
                 << "var oaF=document.getElementById('gd-oauth-fields');"
                 << "var saF=document.getElementById('gd-sa-fields');"
                 << "var con=document.getElementById('gd-step-connect');"
                 << "function upd(){"
                 << "var o=mode.value==='oauth';"
                 << "oaF.style.display=o?'':'none';"
                 << "saF.style.display=o?'none':'';"
                 << "con.style.display=o?'':'none';}"
                 << "mode.addEventListener('change',upd);upd();"
                 << "})();</script>"
                 << "</div>";

            json_object_object_add(jroot, "html", json_object_new_string(html.str().c_str()));
            return jroot;
        }