register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'svg_optimization'); /** * Hack the upload is done via the ajax.php file * {@link media_upload()} */ $controller->register_hook('AUTH_ACL_CHECK', 'BEFORE', $this, 'svg_mime'); /** * When the parsing of a page starts */ $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'BEFORE', $this, 'svg_mime'); } /** * @param Doku_Event $event * https://www.dokuwiki.org/devel:event:fetch_media_status */ public function svg_optimization(Doku_Event &$event) { if ($event->data['ext'] != 'svg') return; if ($event->data['status'] >= 400) return; // ACLs and precondition checks $tagAttributes = TagAttributes::createEmpty(); $width = $event->data['width']; if ($width != 0) { $tagAttributes->addComponentAttributeValue(TagAttributes::WIDTH_KEY, $width); } $height = $event->data['height']; if ($height != 0) { $tagAttributes->addComponentAttributeValue(TagAttributes::HEIGHT_KEY, $height); } $tagAttributes->addComponentAttributeValue(\ComboStrap\Cache::CACHE_KEY, $event->data['cache']); $mime = "image/svg+xml"; $event->data["mime"] = $mime; $tagAttributes->setMime($mime); /** * Add the extra attributes */ $rev = null; foreach ($_REQUEST as $name => $value) { switch ($name) { case "media": case "w": case "h": case "cache": case Cache::CACHE_BUSTER_KEY: case "tok": // A checker // Nothing to do, we take them break; case "rev": $rev = $value; break; case "u": case "p": case "http_credentials": // Credentials data break; default: if (!empty($value)) { if (!in_array($name, InternalMediaLink::TAG_ATTRIBUTES_ONLY)) { $tagAttributes->addComponentAttributeValue($name, $value); } else { LogUtility::msg("The attribute ($name) is not a valid fetch image URL attribute and was not added", LogUtility::LVL_MSG_WARNING, SvgImageLink::CANONICAL); } } else { LogUtility::msg("Internal Error: the value of the query name ($name) is empty", LogUtility::LVL_MSG_WARNING, SvgImageLink::CANONICAL); } } } $id = $event->data["media"]; $svgImageLink = SvgImageLink::createMediaLinkFromPathId($id, $rev, $tagAttributes); $event->data['file'] = $svgImageLink->getSvgFile(); } /** * @param Doku_Event $event * {@link media_save} is checking the authorized mime type * Svg is not by default, we add it here if the user is admin or * in a specified group */ public function svg_mime(Doku_Event &$event) { self::allowSvgIfAuthorized(); } /** * */ public static function allowSvgIfAuthorized() { $isadmin = Auth::isAdmin(); $isMember = Auth::isMember("@" . self::CONF_SVG_UPLOAD_GROUP_NAME); if ($isadmin || $isMember) { /** * Enhance the svg mime type * {@link getMimeTypes()} */ global $config_cascade; $svgMimeConf = Resources::getConfResourceDirectory() . "/svg.mime.conf"; $config_cascade['mime']['local'][] = $svgMimeConf; } } }