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

test

parent 1ff678bf
Loading
Loading
Loading
Loading
+63 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <functional>

#include <authdb/client.h>
#include <authdb/user.h>
@@ -2807,6 +2808,68 @@ void blogi::webedit::WebEditor::Renderer(
            }
        }

        // Register image widget preview sizes in MEDIA_ALLOWED_SIZES
        if (DocRoot) {
            std::string sizesJson;
            try { getConfig(*Args.database[tid], "MEDIA_ALLOWED_SIZES", sizesJson); } catch (...) {}

            json_object *sizesArr = nullptr;
            if (!sizesJson.empty())
                sizesArr = json_tokener_parse(sizesJson.c_str());
            if (!sizesArr || !json_object_is_type(sizesArr, json_type_array)) {
                if (sizesArr) json_object_put(sizesArr);
                sizesArr = json_object_new_array();
            }

            bool sizesChanged = false;
            // Recursive lambda to traverse widget tree
            std::function<void(const EditPlugin*)> collectImageSizes = [&](const EditPlugin *el) {
                while (el) {
                    if (el->getName() == "Image") {
                        tinyxml2::XMLDocument tmpDoc;
                        tinyxml2::XMLElement *xmlEl = const_cast<EditPlugin*>(el)->toXmlObject(&tmpDoc);
                        if (xmlEl) {
                            int rw = 0, rh = 0;
                            xmlEl->QueryIntAttribute("real_width", &rw);
                            xmlEl->QueryIntAttribute("real_height", &rh);
                            if (rw > 0 || rh > 0) {
                                // Check for duplicates
                                bool found = false;
                                size_t len = json_object_array_length(sizesArr);
                                for (size_t i = 0; i < len; ++i) {
                                    json_object *item = json_object_array_get_idx(sizesArr, i);
                                    json_object *jw = nullptr, *jh = nullptr;
                                    json_object_object_get_ex(item, "w", &jw);
                                    json_object_object_get_ex(item, "h", &jh);
                                    if (json_object_get_int(jw) == rw && json_object_get_int(jh) == rh) {
                                        found = true;
                                        break;
                                    }
                                }
                                if (!found) {
                                    json_object *entry = json_object_new_object();
                                    json_object_object_add(entry, "w", json_object_new_int(rw));
                                    json_object_object_add(entry, "h", json_object_new_int(rh));
                                    json_object_array_add(sizesArr, entry);
                                    sizesChanged = true;
                                }
                            }
                        }
                    }
                    if (el->getChildElement())
                        collectImageSizes(el->getChildElement());
                    el = el->nextElement();
                }
            };
            collectImageSizes(DocRoot);

            if (sizesChanged) {
                setConfig(*Args.database[tid], "MEDIA_ALLOWED_SIZES",
                          json_object_to_json_string_ext(sizesArr, JSON_C_TO_STRING_NOSLASHESCAPE));
            }
            json_object_put(sizesArr);
        }

        // Inject click-to-select and highlight script into rendered preview
        libhtmlpp::HtmlElement selectScript("script");
        selectScript.appendChild(libhtmlpp::TextElement(