-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdrawshield.php
More file actions
363 lines (332 loc) · 15.2 KB
/
drawshield.php
File metadata and controls
363 lines (332 loc) · 15.2 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php /* Copyright 2010-2021 Karl Wilcox, Mattias Basaglia
This file is part of the DrawShield.net heraldry image creation program
DrawShield is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DrawShield is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DrawShield. If not, see https://www.gnu.org/licenses/.
*/
include 'utils/general.inc'; // general utilities, duh.
set_exception_handler('display_bug');
$timings = array('start' => microtime(true));
$memory = array('start' => memory_get_usage(true));
register_shutdown_function(function(){
global $options;
$error = error_get_last();
if (null !== $error && $error['type'] == "1") {
error_log($options['blazon'] . ' causes ' . $error['type'] . ' ' . $error['message'] .
" at " . $error['file'] . ':' . $error['line']);
}
});
//////////////////////////////////////////////
// Stage 1a - Set default and fallback options
/////////////////////////////////////////////
$options = array();
$version = array();
$request = array();
include 'version.inc'; // sets default and fallback options
//////////////////////////////////////////////
// Stage 1b - command line arguments (if any)
/////////////////////////////////////////////
if (isset($argc)) {
$options['requestSource'] = 'shell';
if ($argc > 1) { // run in debug mode, probably
// Do we have any command line arguments? If so, incorporate into options array
$myArgs = arguments($argv);
foreach ($myArgs['options'] as $option => $value) {
$value = urldecode($value);
$request[$option] = $value;
}
if (!array_key_exists('blazon', $request))
$request['blazon'] = implode(' ', $myArgs['arguments']);
} else { // otherwise, run anything in the debug script
// This NEVER HAPPENS ON THE SERVER (as $argc is never set)
// so it is safe (but useless) to copy debug.inc to the server
if (file_exists('debug.inc')) include 'debug.inc';
}
} else {
$request = array_merge($_GET, $_POST);
}
//////////////////////////////////////////////
// Stage 1c -process any arguments from the superglobals? SERVER ONLY (probably)
/////////////////////////////////////////////
$ar = null;
$svgOutput = null;
$dom = null;
$messages = null;
$jsonData = [];
// Process superglobal settings into the options array
if (isset($request['outputformat'])) $options['outputFormat'] = strip_tags($request['outputformat']);
if (isset($request['saveformat'])) $options['saveFormat'] = strip_tags($request['saveformat']);
if (isset($request['asfile']) && $request['asfile'] != '0') $options['asFile'] = strip_tags($request['asfile']);
if (isset($request['palette'])) $options['palette'] = strip_tags($request['palette']);
if (isset($request['shape'])) $options['shape'] = strip_tags($request['shape']);
if (isset($request['stage'])) $options['stage'] = strip_tags($request['stage']);
if (isset($request['filename'])) $options['filename'] = strip_tags($request['filename']);
if (isset($request['effect'])) $options['effect'] = strip_tags($request['effect']);
if (isset($request['background'])) $options['background'] = strip_tags($request['background']);
if (isset($request['size'])) $options['size'] = intval($request['size']);
if (isset($request['margin'])) $options['margin'] = intval($request['margin']);
if (isset($request['units'])) $options['units'] = strip_tags($request['units']);
if (isset($request['ar'])) $ar = strip_tags($request['ar']);
if (isset($request['webcols'])) $options['useWebColours'] = $request['webcols'] == 'yes';
if (isset($request['tartancols'])) $options['useTartanColours'] = $request['tartancols'] == 'yes';
if (isset($request['whcols'])) $options['useWarhammerColours'] = $request['whcols'] == 'yes';
if (isset($request['customPalette']) && is_array($request['customPalette'])) $options['customPalette'] = $request['customPalette'];
//////////////////////////////////////////////
// Stage 2a - blazon preparation
/////////////////////////////////////////////
// We do support sending the blazon in an uploaded file but this is not currently implemented
// on the site anywhere, we always use POST fields
if (isset($_FILES['blazonfile']) && ($_FILES['blazonfile']['name'] != "")) {
$fileName = $_FILES['blazonfile']['name'];
$fileSize = $_FILES['blazonfile']['size'];
$fileTmpName = $_FILES['blazonfile']['tmp_name'];
// $fileType = $_FILES['blazonfile']['type']; // not currently used
if (preg_match('/.txt$/i', $fileName) && $fileSize < 1000000) {
$options['blazon'] = file_get_contents($fileTmpName);
$options['requestSource'] = 'upload';
}
} else {
if (isset($request['blazon'])) $options['blazon'] = $request['blazon'];
}
// If blazon is null assume 'argent', but skip parsing as we already know the outcome
if (!array_key_exists('blazon', $options) || $options['blazon'] == '') {
$dom = new DOMDocument('1.0');
$dom->loadXML($version['dummyAST']);
} else {
if ($options['outputFormat'] == 'json') {
// start gathering our parts here
$jsonData['blazon'] = $options['blazon'];
$jsonData['creationTime'] = date(DATE_RFC2822);
$jsonData['pathInfo'] = $_SERVER['REQUEST_URI'] ?? '???';
$jsonData['options'] = $options;
unset($jsonData['options']['blazon']); // already have this
}
$options['original'] = $options['blazon'];
$options['blazon'] = strip_tags($options['blazon']);
$options['blazon'] = preg_replace("/&#?[a-z0-9]{2,8};/i", "", $options['blazon']); // strip all entities.
$options['blazon'] = preg_replace('/\\\x[0-9a-f]{2}/i', "", $options['blazon']); // strip all entities.
// log the blazon for research... (unless told not too)
if ($version['logBlazon']) ds_error_log($options['blazon']);
}
include 'parser/utilities.inc';
include "analyser/utilities.inc";
if (is_null($dom)) {
//////////////////////////////////////////////
// Stage 2b - Parsing into XML AST
/////////////////////////////////////////////
include "parser/parser.inc";
$p = new parser('english');
$dom = $p->parse($options['blazon']);
$memory['parser'] = memory_get_usage(true);
$p = null; // destroy parser to save memory
$timings['parser'] = microtime(true);
//////////////////////////////////////////////
// Stage 2c - Filtering out stuff we don't like (code not in repo)
/////////////////////////////////////////////
if (file_exists("../../etc/filter.inc")) {
include "../../etc/filter.inc";
$filter = new filter($dom);
$dom = $filter->runFilter();
}
//////////////////////////////////////////////
// Stage 2d - Additional Options may have been in blazon, tidy them up
/////////////////////////////////////////////
if ($options['asFile']) {
$options['printSize'] = $options['size'];
$options['size'] = 1000;
}
// Minimum sensible size
if ($options['size'] < 100) $options['size'] = 100;
if ($ar != null) {
$options['aspectRatio'] = calculateAR($ar);
}
// Same for margin, don't pad more than 25% of width
if ($options['margin'] > $options['size'] / 4) {
$options['margin'] = intval($options['size'] / 4);
}
tidyOptions();;
//////////////////////////////////////////////
// Stage 2e - resolve cross references & other fixups
/////////////////////////////////////////////
include "analyser/fixups.inc";
$references = new references($dom);
$dom = $references->doFixups();
$memory['fixups'] = memory_get_usage(true);
$references = null; // destroy references to save memory
$timings['fixups'] = microtime(true);
}
//////////////////////////////////////////////
// Stage 3a - preparation for drawing
/////////////////////////////////////////////
$xpath = new DOMXPath($dom);
include 'utils/messages.inc';
$messages = new messageStore($dom);
include "svg/draw.inc";
set_error_handler("report_errors_svg");
//////////////////////////////////////////////
// Stage 3b - create SVG version (all formats start as SVG)
/////////////////////////////////////////////
$svgOutput = draw();
$memory['svg draw'] = memory_get_usage(true);
// At this point we should have some valid SVG in $svgOutput, now what?
$target = $options['outputFormat'];
if ($target == 'json') {
$target = $options['saveFormat'];
} elseif ($options['asFile'] == 'printable') {
$target = 'svg';
} elseif ($options['asFile']) {
$target = $options['saveFormat'];
}
// (Sorry about the above mess, needed for historic reasons and to preserve API arguments)
//////////////////////////////////////////////
// Stage 3c - Change image format if required
/////////////////////////////////////////////
if ($target == 'svg') {
$targetImage = $svgOutput;
} else {
include 'utils/render.inc';
$targetImage = convertImageFormat($svgOutput, $target);
$timings['conversion'] = microtime(true);
$memory['conversion'] = memory_get_usage(true);
}
if ($options['outputFormat'] == 'json') { // package up things for output as needed
$targetImage = makeJsonArray(true);
}
//////////////////////////////////////////////
// Stage 4 - output image as per option request
/////////////////////////////////////////////
if ($options['asFile'] == 'printable') {
//////////////////////////////////////////////
// Stage 4 option 1 - a printable HTML page
/////////////////////////////////////////////
$xpath = new DOMXPath($dom); // re-build xpath with new messages
header('Content-Type: text/html; charset=utf-8');
echo "<!doctype html>\n\n<html lang=\"en\">\n<head>\n<title>Shield</title>\n";
echo "<style>\nsvg { margin-left:auto; margin-right:auto; display:block;}</style>\n</head>\n<body>\n";
echo "<div>\n$targetImage</div>\n";
echo "<h2>Blazon</h2>\n";
echo "<pre class=\"blazon\">{$options['original']}</pre>\n";
echo "<h2>Image Credits</h2>\n";
echo "<p>This work is licensed under a <em>Creative Commons Attribution-ShareAlike 4.0 International License</em>.";
echo " It is a derivative work based on the following source images:</p>";
echo $messages->getCredits();
echo "</body>\n</html>\n";
//////////////////////////////////////////////
// Stage 4 option 2 - file saved locally in file system
/////////////////////////////////////////////
} elseif ($options['asFile'] && $options['requestSource'] == 'shell') {
// We always just write the file locally.
$name = $options['filename'];
if ($name == '') $name = 'shield';
$fileContent = '';
switch ($options['saveFormat']) {
case 'json':
if (substr($name, -5) != '.json') $name .= '.json';
break;
case 'svg':
if (substr($name, -4) != '.svg') $name .= '.svg';
break;
case 'pdfLtr':
case 'pdfA4':
case 'pdfltr':
case 'pdfa4':
if (substr($name, -4) != '.pdf') $name .= '.pdf';
break;
case 'jpg':
case 'jpeg':
if (substr($name, -4) != '.jpg') $name .= '.jpg';
break;
case 'png':
if (substr($name, -4) != '.png') $name .= '.png';
default:
break;
}
file_put_contents($name, $targetImage);
//////////////////////////////////////////////
// Stage 4 option 3 - An image file downloaded over HTTP
/////////////////////////////////////////////
} elseif ($options['asFile']) {
if ($options['outputFormat'] == 'json') { // override saveFormat
$options['saveFormat'] = 'json';
}
$name = $options['filename'];
if ($name == '') $name = 'shield';
switch ($options['saveFormat']) {
case 'json':
if (substr($name, -5) != '.json') $name .= '.json';
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: text");
header('Content-Disposition: attachment; filename="' . $name .'"');
header('Content-Type: application/json');
break;
case 'svg':
if (substr($name, -4) != '.svg') $name .= '.svg';
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: text");
header('Content-Disposition: attachment; filename="' . $name .'"');
header('Content-Type: image/svg+xml');
break;
case 'pdfLtr':
case 'pdfA4':
if (substr($name, -4) != '.pdf') $name .= '.pdf';
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: 8bit");
header('Content-Disposition: attachment; filename="' . $name .'"');
header('Content-Type: application/pdf');
break;
case 'jpg':
case 'jpeg':
if (substr($name, -4) != '.jpg') $name .= '.jpg';
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="' . $name .'"');
header('Content-Type: image/jpg');
break;
case 'png':
if (substr($name, -4) != '.png') $name .= '.png';
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="' . $name .'"');
header('Content-Type: image/png');
break;
case 'webp':
if (substr($name, -4) != '.webp') $name .= '.webp';
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="' . $name .'"');
header('Content-Type: image/webp');
break;
}
echo $targetImage;
//////////////////////////////////////////////
// Stage 4 option 4 - image returned over HTTP
/////////////////////////////////////////////
} else {
switch ($options['outputFormat']) {
case 'jpg':
case 'jpeg':
header('Content-Type: image/jpg');
break;
case 'json': // A PNG image + other data all wrapped up in JSON
header('Content-Type: application/json');
break;
case 'webp':
header('Content-Type: image/webp');
break;
case 'png':
header('Content-Type: image/png');
break;
default:
case 'svg':
header('Content-Type: text/xml; charset=utf-8');
break;
}
echo $targetImage;
}