xref: /plugin/combo/action/webcode.php (revision c3437056399326d621a01da73b649707fbb0ae69)
1<?php
2
3use ComboStrap\PluginUtility;
4use ComboStrap\Site;
5use ComboStrap\TplUtility;
6
7if (!defined('DOKU_INC')) die();
8
9/**
10 *
11 */
12class  action_plugin_combo_webcode extends DokuWiki_Action_Plugin
13{
14
15    const CALL_ID = "webcode";
16    const MARKI_PARAM = "marki";
17
18
19    function register(Doku_Event_Handler $controller)
20    {
21        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_ajax_call');
22    }
23
24    /**
25     * handle ajax requests
26     * @param $event Doku_Event
27     *
28     * {@link html_show()}
29     *
30     * https://www.dokuwiki.org/devel:plugin_programming_tips#handle_json_ajax_request
31     *
32     * CSRF checks are only for logged in users
33     * This is public ({@link getSecurityToken()}
34     */
35    function _ajax_call(&$event)
36    {
37
38        if ($event->data !== self::CALL_ID) {
39            return;
40        }
41        //no other ajax call handlers needed
42        $event->stopPropagation();
43        $event->preventDefault();
44
45
46        global $INPUT;
47        $marki = $INPUT->str(self::MARKI_PARAM);
48        $title = $INPUT->str('title') ?: "ComboStrap WebCode - Dokuwiki Renderer";
49
50
51        header('Content-Type: text/html; charset=utf-8');
52
53        /**
54         * Conf
55         */
56        PluginUtility::setConf(action_plugin_combo_css::CONF_DISABLE_DOKUWIKI_STYLESHEET, true);
57
58        /**
59         * Main content happens before the headers
60         * to set the headers right
61         */
62        global $conf;
63        $conf["renderer_xhtml"] = "xhtml";
64
65        global $ID;
66        $keep = $ID;
67        try {
68            $ID = "ajax_webcode_" . md5($marki);
69            $mainContent = p_render('xhtml', p_get_instructions($marki), $info);
70
71            /**
72             * Html
73             */
74            $htmlBeforeHeads = '<!DOCTYPE html>' . DOKU_LF;
75            $htmlBeforeHeads .= '<html>' . DOKU_LF;
76            $htmlBeforeHeads .= '<head>' . DOKU_LF;
77            $htmlBeforeHeads .= "  <title>$title</title>" . DOKU_LF;
78            // we echo because the tpl function just flush
79            echo $htmlBeforeHeads;
80
81            if (Site::isStrapTemplate()) {
82
83                /**
84                 * The strap header function
85                 */
86                $loaded = PluginUtility::loadStrapUtilityTemplateIfPresentAndSameVersion();
87                if ($loaded) {
88                    TplUtility::registerHeaderHandler();
89                }
90            }
91
92            /**
93             * To delete the not needed headers for an export
94             * such as manifest, alternate, ...
95             */
96            global $EVENT_HANDLER;
97            $EVENT_HANDLER->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_delete_not_needed_headers');
98
99            /**
100             * meta headers
101             */
102            tpl_metaheaders();
103
104
105            $htmlAfterHeads = '</head>' . DOKU_LF;
106            $htmlAfterHeads .= '<body>' . DOKU_LF;
107            $htmlAfterHeads .= $mainContent . DOKU_LF;
108            $htmlAfterHeads .= '</body>' . DOKU_LF;
109            $htmlAfterHeads .= '</html>' . DOKU_LF;
110            echo $htmlAfterHeads;
111            http_response_code(200);
112
113        } finally {
114            $ID = $keep;
115        }
116
117    }
118
119    /**
120     * Dynamically called in the previous function
121     * to delete the head
122     * @param $event
123     */
124    public function _delete_not_needed_headers(&$event)
125    {
126        $data = &$event->data;
127
128        foreach ($data as $tag => &$heads) {
129            switch ($tag) {
130                case "link":
131                    $deletedRel = ["manifest", "search", "start", "alternate", "contents"];
132                    foreach ($heads as $id => $headAttributes) {
133                        if (isset($headAttributes['rel'])) {
134                            $rel = $headAttributes['rel'];
135                            if (in_array($rel, $deletedRel)) {
136                                unset($heads[$id]);
137                            }
138                        }
139                    }
140                    break;
141                case "meta":
142                    $deletedMeta = ["robots"];
143                    foreach ($heads as $id => $headAttributes) {
144                        if (isset($headAttributes['name'])) {
145                            $rel = $headAttributes['name'];
146                            if (in_array($rel, $deletedMeta)) {
147                                unset($heads[$id]);
148                            }
149                        }
150                    }
151                    break;
152                case "script":
153                    foreach ($heads as $id => $headAttributes) {
154                        if (isset($headAttributes['src'])) {
155                            $src = $headAttributes['src'];
156                            if (strpos($src, "lib/exe/js.php") !== false) {
157                                unset($heads[$id]);
158                            }
159                        }
160                    }
161            }
162        }
163    }
164
165
166}
167