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