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