xref: /plugin/combo/action/svg.php (revision 23723136447b7913f53b9b11536e95bab29a3790)
1<?php
2
3
4require_once(__DIR__ . '/../class/Cache.php');
5
6use ComboStrap\Auth;
7use ComboStrap\Cache;
8use ComboStrap\MediaLink;
9use ComboStrap\LogUtility;
10use ComboStrap\Resources;
11use ComboStrap\SvgImageLink;
12use ComboStrap\TagAttributes;
13
14if (!defined('DOKU_INC')) exit;
15if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
16
17/**
18 * Class action_plugin_combo_svg
19 * Returned an svg optimized version
20 */
21class action_plugin_combo_svg extends DokuWiki_Action_Plugin
22{
23
24
25    const CONF_SVG_UPLOAD_GROUP_NAME = "svgUploadGroupName";
26
27    public function register(Doku_Event_Handler $controller)
28    {
29
30        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'svg_optimization');
31
32
33        /**
34         * Hack the upload is done via the ajax.php file
35         * {@link media_upload()}
36         */
37        $controller->register_hook('AUTH_ACL_CHECK', 'BEFORE', $this, 'svg_mime');
38        /**
39         * When the parsing of a page starts
40         */
41        $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'BEFORE', $this, 'svg_mime');
42
43    }
44
45    /**
46     * @param Doku_Event $event
47     * https://www.dokuwiki.org/devel:event:fetch_media_status
48     */
49    public function svg_optimization(Doku_Event &$event)
50    {
51
52        if ($event->data['ext'] != 'svg') return;
53        if ($event->data['status'] >= 400) return; // ACLs and precondition checks
54
55
56        $tagAttributes = TagAttributes::createEmpty();
57        $width = $event->data['width'];
58        if ($width != 0) {
59            $tagAttributes->addComponentAttributeValue(TagAttributes::WIDTH_KEY, $width);
60        }
61        $height = $event->data['height'];
62        if ($height != 0) {
63            $tagAttributes->addComponentAttributeValue(TagAttributes::HEIGHT_KEY, $height);
64        }
65        $tagAttributes->addComponentAttributeValue(\ComboStrap\Cache::CACHE_KEY, $event->data['cache']);
66
67        $mime = "image/svg+xml";
68        $event->data["mime"] = $mime;
69        $tagAttributes->setMime($mime);
70
71        /**
72         * Add the extra attributes
73         */
74        $rev = null;
75        foreach ($_REQUEST as $name => $value) {
76            switch ($name) {
77                case "media":
78                case "w":
79                case "h":
80                case "cache":
81                case Cache::CACHE_BUSTER_KEY:
82                case "tok": // A checker
83                    // Nothing to do, we take them
84                    break;
85                case "rev":
86                    $rev = $value;
87                    break;
88                case "u":
89                case "p":
90                case "http_credentials":
91                    // Credentials data
92                    break;
93                default:
94                    if (!empty($value)) {
95                        if (!in_array($name, MediaLink::NON_URL_ATTRIBUTES)) {
96                            $tagAttributes->addComponentAttributeValue($name, $value);
97                        } else {
98                            LogUtility::msg("The attribute ($name) is not a valid fetch image URL attribute and was not added", LogUtility::LVL_MSG_WARNING, SvgImageLink::CANONICAL);
99                        }
100                    } else {
101                        LogUtility::msg("Internal Error: the value of the query name ($name) is empty", LogUtility::LVL_MSG_WARNING, SvgImageLink::CANONICAL);
102                    }
103            }
104        }
105
106
107        $id = $event->data["media"];
108        $svgImageLink = SvgImageLink::createMediaLinkFromPathId($id, $rev, $tagAttributes);
109        $event->data['file'] = $svgImageLink->getSvgFile();
110
111
112    }
113
114    /**
115     * @param Doku_Event $event
116     * {@link media_save} is checking the authorized mime type
117     * Svg is not by default, we add it here if the user is admin or
118     * in a specified group
119     */
120    public function svg_mime(Doku_Event &$event)
121    {
122
123        self::allowSvgIfAuthorized();
124
125    }
126
127    /**
128     *
129     */
130    public static function allowSvgIfAuthorized()
131    {
132        $isadmin = Auth::isAdmin();
133        $isMember = Auth::isMember("@" . self::CONF_SVG_UPLOAD_GROUP_NAME);
134
135        if ($isadmin || $isMember) {
136            /**
137             * Enhance the svg mime type
138             * {@link getMimeTypes()}
139             */
140            global $config_cascade;
141            $svgMimeConf = Resources::getConfResourceDirectory() . "/svg.mime.conf";
142            $config_cascade['mime']['local'][] = $svgMimeConf;
143        }
144
145    }
146
147
148}
149