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://cdn.rawgit.com/Templarian/MaterialDesign/master/icons/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 io_download(self::CDNBASE . $svg, $file); 115 } 116 117 } 118 // check if media exists 119 if(!file_exists($file)) $this->abort(404); 120 121 $this->file = $file; 122 } 123 124 /** 125 * Generate and output 126 */ 127 public function out() { 128 $file = $this->file; 129 $params = $this->getParameters(); 130 131 header('Content-Type: image/svg+xml'); 132 $cachekey = md5($file . serialize($params)); 133 $cache = new \cache($cachekey, '.svg'); 134 $cache->_event = 'SVG_CACHE'; 135 136 http_cached($cache->cache, $cache->useCache(array('files' => array($file, __FILE__)))); 137 http_cached_finish($cache->cache, $this->generateSVG($file, $params)); 138 } 139 140 /** 141 * Generate a new SVG based on the input file and the parameters 142 * 143 * @param string $file the SVG file to load 144 * @param array $params the parameters as returned by getParameters() 145 * @return string the new XML contents 146 */ 147 protected function generateSVG($file, $params) { 148 /** @var SvgNode $xml */ 149 $xml = simplexml_load_file($file, SvgNode::class); 150 $xml->addStyle($this->makeStyle($params)); 151 $this->createBackground($xml); 152 $xml->groupChildren(); 153 154 return $xml->asXML(); 155 } 156 157 /** 158 * Get the supported parameters from request 159 * 160 * @return array 161 */ 162 protected function getParameters() { 163 global $INPUT; 164 165 $params = array( 166 's' => $this->fixColor($INPUT->str('s')), 167 'f' => $this->fixColor($INPUT->str('f')), 168 'b' => $this->fixColor($INPUT->str('b')), 169 'sh' => $this->fixColor($INPUT->str('sh')), 170 'fh' => $this->fixColor($INPUT->str('fh')), 171 'bh' => $this->fixColor($INPUT->str('bh')), 172 ); 173 174 return $params; 175 } 176 177 /** 178 * Generate a style setting from the input variables 179 * 180 * @param array $params associative array with the given parameters 181 * @return string 182 */ 183 protected function makeStyle($params) { 184 $element = 'path'; // FIXME configurable? 185 186 if(empty($params['b'])) { 187 $params['b'] = $this->fixColor('00000000'); 188 } 189 190 $style = 'g rect.' . self::BACKGROUNDCLASS . '{fill:' . $params['b'] . ';}'; 191 192 if($params['bh']) { 193 $style .= 'g:hover rect.' . self::BACKGROUNDCLASS . '{fill:' . $params['bh'] . ';}'; 194 } 195 196 if($params['s'] || $params['f']) { 197 $style .= 'g ' . $element . '{'; 198 if($params['s']) $style .= 'stroke:' . $params['s'] . ';'; 199 if($params['f']) $style .= 'fill:' . $params['f'] . ';'; 200 $style .= '}'; 201 } 202 203 if($params['sh'] || $params['fh']) { 204 $style .= 'g:hover ' . $element . '{'; 205 if($params['sh']) $style .= 'stroke:' . $params['sh'] . ';'; 206 if($params['fh']) $style .= 'fill:' . $params['fh'] . ';'; 207 $style .= '}'; 208 } 209 210 return $style; 211 } 212 213 /** 214 * Takes a hexadecimal color string in the following forms: 215 * 216 * RGB 217 * RRGGBB 218 * RRGGBBAA 219 * 220 * Converts it to rgba() form. 221 * 222 * Alternatively takes a replacement name from the current template's style.ini 223 * 224 * @param string $color 225 * @return string 226 */ 227 protected function fixColor($color, $ini = true) { 228 229 if(preg_match('/^([0-9a-f])([0-9a-f])([0-9a-f])$/i', $color, $m)) { 230 $r = hexdec($m[1] . $m[1]); 231 $g = hexdec($m[2] . $m[2]); 232 $b = hexdec($m[3] . $m[3]); 233 $a = hexdec('ff'); 234 } elseif(preg_match('/^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i', $color, $m)) { 235 $r = hexdec($m[1]); 236 $g = hexdec($m[2]); 237 $b = hexdec($m[3]); 238 if(isset($m[4])) { 239 $a = hexdec($m[4]); 240 } else { 241 $a = hexdec('ff'); 242 } 243 } else { 244 if($ini) { 245 if(!$this->replacements) $this->initReplacements(); 246 if(isset($this->replacements[$color])) { 247 return $this->replacements[$color]; 248 } 249 } 250 return ''; 251 } 252 253 return "rgba($r,$g,$b,$a)"; 254 } 255 256 /** 257 * sets a rectangular background of the size of the svg/this itself 258 * 259 * @param SvgNode $g 260 * @return SvgNode 261 */ 262 protected function createBackground(SvgNode $g) { 263 $rect = $g->prependChild('rect'); 264 $rect->addAttribute('class', self::BACKGROUNDCLASS); 265 266 $rect->addAttribute('x', '0'); 267 $rect->addAttribute('y', '0'); 268 $rect->addAttribute('height', '100%'); 269 $rect->addAttribute('width', '100%'); 270 return $rect; 271 } 272 273 /** 274 * Abort processing with given status code 275 * 276 * @param int $status 277 */ 278 protected function abort($status) { 279 http_status($status); 280 exit; 281 } 282 283 /** 284 * Initialize the available replacement patterns 285 * 286 * Loads the style.ini from the template (and various local locations) 287 * via a core function only available through some hack. 288 */ 289 protected function initReplacements() { 290 global $conf; 291 define('SIMPLE_TEST', 1); // hacky shit 292 include DOKU_INC . 'lib/exe/css.php'; 293 $ini = css_styleini($conf['tpl']); 294 $this->replacements = $ini['replacements']; 295 } 296} 297 298// main 299$svg = new SVG(); 300$svg->out(); 301 302