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