xref: /plugin/combo/action/webcode.php (revision 531e725cdb5a652164f2d97f556304e31f720033)
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        $mainContent = p_render('xhtml', p_get_instructions($marki), $info);
65
66        /**
67         * Html
68         */
69        $htmlBeforeHeads = '<!DOCTYPE html>' . DOKU_LF;
70        $htmlBeforeHeads .= '<html>' . DOKU_LF;
71        $htmlBeforeHeads .= '<head>' . DOKU_LF;
72        $htmlBeforeHeads .= "  <title>$title</title>" . DOKU_LF;
73        // we echo because the tpl function just flush
74        echo $htmlBeforeHeads;
75
76        if (Site::isStrapTemplate()) {
77
78            /**
79             * The strap header function
80             */
81            $loaded = PluginUtility::loadStrapUtilityTemplateIfPresentAndSameVersion();
82            if($loaded) {
83                TplUtility::registerHeaderHandler();
84            }
85        }
86
87        /**
88         * To delete the not needed headers for an export
89         * such as manifest, alternate, ...
90         */
91        global $EVENT_HANDLER;
92        $EVENT_HANDLER->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_delete_not_needed_headers');
93
94        /**
95         * meta headers
96         */
97        tpl_metaheaders();
98
99
100        $htmlAfterHeads = '</head>' . DOKU_LF;
101        $htmlAfterHeads .= '<body>' . DOKU_LF;
102        $htmlAfterHeads .= $mainContent . DOKU_LF;
103        $htmlAfterHeads .= '</body>' . DOKU_LF;
104        $htmlAfterHeads .= '</html>' . DOKU_LF;
105        echo $htmlAfterHeads;
106        http_response_code(200);
107
108    }
109
110    /**
111     * Dynamically called in the previous function
112     * to delete the head
113     * @param $event
114     */
115    public function _delete_not_needed_headers(&$event)
116    {
117        $data = &$event->data;
118
119        foreach ($data as $tag => &$heads) {
120            switch ($tag) {
121                case "link":
122                    $deletedRel = ["manifest", "search", "start", "alternate", "contents"];
123                    foreach ($heads as $id => $headAttributes) {
124                        if (isset($headAttributes['rel'])) {
125                            $rel = $headAttributes['rel'];
126                            if (in_array($rel, $deletedRel)) {
127                                unset($heads[$id]);
128                            }
129                        }
130                    }
131                    break;
132                case "meta":
133                    $deletedMeta = ["robots"];
134                    foreach ($heads as $id => $headAttributes) {
135                        if (isset($headAttributes['name'])) {
136                            $rel = $headAttributes['name'];
137                            if (in_array($rel, $deletedMeta)) {
138                                unset($heads[$id]);
139                            }
140                        }
141                    }
142                    break;
143                case "script":
144                    foreach ($heads as $id => $headAttributes) {
145                        if (isset($headAttributes['src'])) {
146                            $src = $headAttributes['src'];
147                            if (strpos($src, "lib/exe/js.php") !== false) {
148                                unset($heads[$id]);
149                            }
150                        }
151                    }
152            }
153        }
154    }
155
156
157}
158