1<?php 2 3namespace dokuwiki\template\sprintdoc; 4 5if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__) . '/../../../'); 6require_once(DOKU_INC . 'inc/init.php'); 7 8/** 9 * Custom XML node that allows prepending 10 */ 11class SvgNode extends \SimpleXMLElement { 12 /** 13 * @param string $name Name of the new node 14 * @param null|string $value 15 * @return SvgNode 16 */ 17 public function prependChild($name, $value = null) { 18 $dom = dom_import_simplexml($this); 19 20 $new = $dom->insertBefore( 21 $dom->ownerDocument->createElement($name, $value), 22 $dom->firstChild 23 ); 24 25 return simplexml_import_dom($new, get_class($this)); 26 } 27 28 /** 29 * @param \SimpleXMLElement $node the node to be added 30 * @return \SimpleXMLElement 31 */ 32 public function appendNode(\SimpleXMLElement $node) { 33 $dom = dom_import_simplexml($this); 34 $domNode = dom_import_simplexml($node); 35 36 $newNode = $dom->appendChild($domNode); 37 return simplexml_import_dom($newNode, get_class($this)); 38 } 39 40 /** 41 * @param \SimpleXMLElement $node the child to remove 42 * @return \SimpleXMLElement 43 */ 44 public function removeChild(\SimpleXMLElement $node) { 45 $dom = dom_import_simplexml($node); 46 $dom->parentNode->removeChild($dom); 47 return $node; 48 } 49 50 /** 51 * Wraps all elements of $this in a `<g>` tag 52 * 53 * @return SvgNode 54 */ 55 public function groupChildren() { 56 $dom = dom_import_simplexml($this); 57 58 $g = $dom->ownerDocument->createElement('g'); 59 while($dom->childNodes->length > 0) { 60 $child = $dom->childNodes->item(0); 61 $dom->removeChild($child); 62 $g->appendChild($child); 63 } 64 $g = $dom->appendChild($g); 65 66 return simplexml_import_dom($g, get_class($this)); 67 } 68 69 /** 70 * Add new style definitions to this element 71 * @param string $style 72 */ 73 public function addStyle($style) { 74 $defs = $this->defs; 75 if(!$defs) { 76 $defs = $this->prependChild('defs'); 77 } 78 $defs->addChild('style', $style); 79 } 80} 81 82/** 83 * Manage SVG recoloring 84 */ 85class SVG { 86 87 const IMGDIR = __DIR__ . '/img/'; 88 const BACKGROUNDCLASS = 'sprintdoc-background'; 89 const CDNBASE = 'https://raw.githubusercontent.com/Templarian/MaterialDesign/master/svg/'; 90 91 protected $file; 92 protected $replacements; 93 94 /** 95 * SVG constructor 96 */ 97 public function __construct() { 98 global $INPUT; 99 100 $svg = cleanID($INPUT->str('svg')); 101 if(blank($svg)) $this->abort(404); 102 103 // try local file first 104 $file = self::IMGDIR . $svg; 105 if(!file_exists($file)) { 106 // try media file 107 $file = mediaFN($svg); 108 if(file_exists($file)) { 109 // media files are ACL protected 110 if(auth_quickaclcheck($svg) < AUTH_READ) $this->abort(403); 111 } else { 112 // get it from material design icons 113 $file = getCacheName($svg, '.svg'); 114 if (!file_exists($file)) { 115 io_download(self::CDNBASE . $svg, $file); 116 } 117 } 118 119 } 120 // check if media exists 121 if(!file_exists($file)) $this->abort(404); 122 123 $this->file = $file; 124 } 125 126 /** 127 * Generate and output 128 */ 129 public function out() { 130 global $conf; 131 $file = $this->file; 132 $params = $this->getParameters(); 133 134 header('Content-Type: image/svg+xml'); 135 $cachekey = md5($file . serialize($params) . $conf['template'] . filemtime(__FILE__)); 136 $cache = new \dokuwiki\Cache\Cache($cachekey, '.svg'); 137 $cache->setEvent('SVG_CACHE'); 138 139 http_cached($cache->cache, $cache->useCache(array('files' => array($file, __FILE__)))); 140 if($params['e']) { 141 $content = $this->embedSVG($file); 142 } else { 143 $content = $this->generateSVG($file, $params); 144 } 145 http_cached_finish($cache->cache, $content); 146 } 147 148 /** 149 * Generate a new SVG based on the input file and the parameters 150 * 151 * @param string $file the SVG file to load 152 * @param array $params the parameters as returned by getParameters() 153 * @return string the new XML contents 154 */ 155 protected function generateSVG($file, $params) { 156 /** @var SvgNode $xml */ 157 $xml = simplexml_load_file($file, SvgNode::class); 158 $xml->addStyle($this->makeStyle($params)); 159 $this->createBackground($xml); 160 $xml->groupChildren(); 161 162 return $xml->asXML(); 163 } 164 165 /** 166 * Return the absolute minimum path definition for direct embedding 167 * 168 * No styles will be applied. They have to be done in CSS 169 * 170 * @param string $file the SVG file to load 171 * @return string the new XML contents 172 */ 173 protected function embedSVG($file) { 174 /** @var SvgNode $xml */ 175 $xml = simplexml_load_file($file, SvgNode::class); 176 177 $def = hsc((string) $xml->path['d']); 178 $w = hsc($xml['width']); 179 $h = hsc($xml['height']); 180 $v = hsc($xml['viewBox']); 181 182 return "<svg width=\"$w\" height=\"$h\" viewBox=\"$v\"><path d=\"$def\" /></svg>"; 183 } 184 185 /** 186 * Get the supported parameters from request 187 * 188 * @return array 189 */ 190 protected function getParameters() { 191 global $INPUT; 192 193 $params = array( 194 'e' => $INPUT->bool('e', false), 195 's' => $this->fixColor($INPUT->str('s')), 196 'f' => $this->fixColor($INPUT->str('f')), 197 'b' => $this->fixColor($INPUT->str('b')), 198 'sh' => $this->fixColor($INPUT->str('sh')), 199 'fh' => $this->fixColor($INPUT->str('fh')), 200 'bh' => $this->fixColor($INPUT->str('bh')), 201 ); 202 203 return $params; 204 } 205 206 /** 207 * Generate a style setting from the input variables 208 * 209 * @param array $params associative array with the given parameters 210 * @return string 211 */ 212 protected function makeStyle($params) { 213 $element = 'path'; // FIXME configurable? 214 215 if(empty($params['b'])) { 216 $params['b'] = $this->fixColor('00000000'); 217 } 218 219 $style = 'g rect.' . self::BACKGROUNDCLASS . '{fill:' . $params['b'] . ';}'; 220 221 if($params['bh']) { 222 $style .= 'g:hover rect.' . self::BACKGROUNDCLASS . '{fill:' . $params['bh'] . ';}'; 223 } 224 225 if($params['s'] || $params['f']) { 226 $style .= 'g ' . $element . '{'; 227 if($params['s']) $style .= 'stroke:' . $params['s'] . ';'; 228 if($params['f']) $style .= 'fill:' . $params['f'] . ';'; 229 $style .= '}'; 230 } 231 232 if($params['sh'] || $params['fh']) { 233 $style .= 'g:hover ' . $element . '{'; 234 if($params['sh']) $style .= 'stroke:' . $params['sh'] . ';'; 235 if($params['fh']) $style .= 'fill:' . $params['fh'] . ';'; 236 $style .= '}'; 237 } 238 239 return $style; 240 } 241 242 /** 243 * Takes a hexadecimal color string in the following forms: 244 * 245 * RGB 246 * RRGGBB 247 * RRGGBBAA 248 * 249 * Converts it to rgba() form. 250 * 251 * Alternatively takes a replacement name from the current template's style.ini 252 * 253 * @param string $color 254 * @return string 255 */ 256 protected function fixColor($color) { 257 if($color === '') return ''; 258 if(preg_match('/^([0-9a-f])([0-9a-f])([0-9a-f])$/i', $color, $m)) { 259 $r = hexdec($m[1] . $m[1]); 260 $g = hexdec($m[2] . $m[2]); 261 $b = hexdec($m[3] . $m[3]); 262 $a = hexdec('ff'); 263 } elseif(preg_match('/^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i', $color, $m)) { 264 $r = hexdec($m[1]); 265 $g = hexdec($m[2]); 266 $b = hexdec($m[3]); 267 if(isset($m[4])) { 268 $a = hexdec($m[4]); 269 } else { 270 $a = hexdec('ff'); 271 } 272 } else { 273 if(is_null($this->replacements)) $this->initReplacements(); 274 if(isset($this->replacements[$color])) { 275 return $this->replacements[$color]; 276 } 277 if(isset($this->replacements['__' . $color . '__'])) { 278 return $this->replacements['__' . $color . '__']; 279 } 280 return ''; 281 } 282 283 return "rgba($r,$g,$b,$a)"; 284 } 285 286 /** 287 * sets a rectangular background of the size of the svg/this itself 288 * 289 * @param SvgNode $g 290 * @return SvgNode 291 */ 292 protected function createBackground(SvgNode $g) { 293 $rect = $g->prependChild('rect'); 294 $rect->addAttribute('class', self::BACKGROUNDCLASS); 295 296 $rect->addAttribute('x', '0'); 297 $rect->addAttribute('y', '0'); 298 $rect->addAttribute('height', '100%'); 299 $rect->addAttribute('width', '100%'); 300 return $rect; 301 } 302 303 /** 304 * Abort processing with given status code 305 * 306 * @param int $status 307 */ 308 protected function abort($status) { 309 http_status($status); 310 exit; 311 } 312 313 /** 314 * Initialize the available replacement patterns 315 * 316 * Loads the style.ini from the template (and various local locations) 317 * via a core function only available through some hack. 318 */ 319 protected function initReplacements() { 320 global $conf; 321 if (!class_exists('\dokuwiki\StyleUtils')) { 322 // Pre-Greebo Compatibility 323 324 define('SIMPLE_TEST', 1); // hacky shit 325 include DOKU_INC . 'lib/exe/css.php'; 326 $ini = css_styleini($conf['template']); 327 $this->replacements = $ini['replacements']; 328 return; 329 } 330 331 $stuleUtils = new \dokuwiki\StyleUtils(); 332 $ini = $stuleUtils->cssStyleini('sprintdoc'); 333 $this->replacements = $ini['replacements']; 334 } 335} 336 337// main 338$svg = new SVG(); 339$svg->out(); 340 341 342