| 875 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* This file is part of the exporting module for Highcharts JS.
|
|
|
4 |
* www.highcharts.com/license
|
|
|
5 |
*
|
|
|
6 |
*
|
|
|
7 |
* Available POST variables:
|
|
|
8 |
*
|
|
|
9 |
* $filename string The desired filename without extension
|
|
|
10 |
* $type string The MIME type for export.
|
|
|
11 |
* $width int The pixel width of the exported raster image. The height is calculated.
|
|
|
12 |
* $svg string The SVG source code to convert.
|
|
|
13 |
*/
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
// Options
|
|
|
17 |
define ('BATIK_PATH', 'batik-rasterizer.jar');
|
|
|
18 |
|
|
|
19 |
///////////////////////////////////////////////////////////////////////////////
|
|
|
20 |
ini_set('magic_quotes_gpc', 'off');
|
|
|
21 |
|
|
|
22 |
$type = $_POST['type'];
|
|
|
23 |
$svg = (string) $_POST['svg'];
|
|
|
24 |
$filename = (string) $_POST['filename'];
|
|
|
25 |
|
|
|
26 |
// prepare variables
|
|
|
27 |
if (!$filename or !preg_match('/^[A-Za-z0-9\-_ ]+$/', $filename)) {
|
|
|
28 |
$filename = 'chart';
|
|
|
29 |
}
|
|
|
30 |
if (get_magic_quotes_gpc()) {
|
|
|
31 |
$svg = stripslashes($svg);
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
// check for malicious attack in SVG
|
|
|
35 |
if(strpos($svg,"<!ENTITY") !== false || strpos($svg,"<!DOCTYPE") !== false){
|
|
|
36 |
exit("Execution is stopped, the posted SVG could contain code for a malicious attack");
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
$tempName = md5(rand());
|
|
|
40 |
|
|
|
41 |
// allow no other than predefined types
|
|
|
42 |
if ($type == 'image/png') {
|
|
|
43 |
$typeString = '-m image/png';
|
|
|
44 |
$ext = 'png';
|
|
|
45 |
|
|
|
46 |
} elseif ($type == 'image/jpeg') {
|
|
|
47 |
$typeString = '-m image/jpeg';
|
|
|
48 |
$ext = 'jpg';
|
|
|
49 |
|
|
|
50 |
} elseif ($type == 'application/pdf') {
|
|
|
51 |
$typeString = '-m application/pdf';
|
|
|
52 |
$ext = 'pdf';
|
|
|
53 |
|
|
|
54 |
} elseif ($type == 'image/svg+xml') {
|
|
|
55 |
$ext = 'svg';
|
|
|
56 |
|
|
|
57 |
} else { // prevent fallthrough from global variables
|
|
|
58 |
$ext = 'txt';
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$outfile = "temp/$tempName.$ext";
|
|
|
62 |
|
|
|
63 |
if (isset($typeString)) {
|
|
|
64 |
|
|
|
65 |
// size
|
|
|
66 |
$width = '';
|
|
|
67 |
if ($_POST['width']) {
|
|
|
68 |
$width = (int)$_POST['width'];
|
|
|
69 |
if ($width) $width = "-w $width";
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
// generate the temporary file
|
|
|
73 |
if (!file_put_contents("temp/$tempName.svg", $svg)) {
|
|
|
74 |
die("Couldn't create temporary file. Check that the directory permissions for
|
|
|
75 |
the /temp directory are set to 777.");
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// Troubleshooting snippet
|
|
|
79 |
/*
|
|
|
80 |
$command = "/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bin/java -jar ". BATIK_PATH ." $typeString -d $outfile $width temp/$tempName.svg 2>&1";
|
|
|
81 |
$output = shell_exec($command);
|
|
|
82 |
echo "<pre>Command: $command <br>";
|
|
|
83 |
echo "Output: $output</pre>";
|
|
|
84 |
die;
|
|
|
85 |
// */
|
|
|
86 |
|
|
|
87 |
// Do the conversion
|
|
|
88 |
$output = shell_exec("java -jar ". BATIK_PATH ." $typeString -d $outfile $width temp/$tempName.svg");
|
|
|
89 |
|
|
|
90 |
// catch error
|
|
|
91 |
if (!is_file($outfile) || filesize($outfile) < 10) {
|
|
|
92 |
echo "<pre>$output</pre>";
|
|
|
93 |
echo "Error while converting SVG. ";
|
|
|
94 |
|
|
|
95 |
if (strpos($output, 'SVGConverter.error.while.rasterizing.file') !== false) {
|
|
|
96 |
echo "
|
|
|
97 |
<h4>Debug steps</h4>
|
|
|
98 |
<ol>
|
|
|
99 |
<li>Copy the SVG:<br/><textarea rows=5>" . htmlentities(str_replace('>', ">\n", $svg)) . "</textarea></li>
|
|
|
100 |
<li>Go to <a href='http://validator.w3.org/#validate_by_input' target='_blank'>validator.w3.org/#validate_by_input</a></li>
|
|
|
101 |
<li>Paste the SVG</li>
|
|
|
102 |
<li>Click More Options and select SVG 1.1 for Use Doctype</li>
|
|
|
103 |
<li>Click the Check button</li>
|
|
|
104 |
</ol>";
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
// stream it
|
|
|
109 |
else {
|
|
|
110 |
header("Content-Disposition: attachment; filename=\"$filename.$ext\"");
|
|
|
111 |
header("Content-Type: $type");
|
|
|
112 |
echo file_get_contents($outfile);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// delete it
|
|
|
116 |
unlink("temp/$tempName.svg");
|
|
|
117 |
unlink($outfile);
|
|
|
118 |
|
|
|
119 |
// SVG can be streamed directly back
|
|
|
120 |
} else if ($ext == 'svg') {
|
|
|
121 |
header("Content-Disposition: attachment; filename=\"$filename.$ext\"");
|
|
|
122 |
header("Content-Type: $type");
|
|
|
123 |
echo $svg;
|
|
|
124 |
|
|
|
125 |
} else {
|
|
|
126 |
echo "Invalid type";
|
|
|
127 |
}
|
|
|
128 |
?>
|