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