You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.4 KiB

9 years ago
  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. // Options
  15. define ('BATIK_PATH', 'batik-rasterizer.jar');
  16. ///////////////////////////////////////////////////////////////////////////////
  17. ini_set('magic_quotes_gpc', 'off');
  18. $type = $_POST['type'];
  19. $svg = (string) $_POST['svg'];
  20. $filename = (string) $_POST['filename'];
  21. // prepare variables
  22. if (!$filename or !preg_match('/^[A-Za-z0-9\-_ ]+$/', $filename)) {
  23. $filename = 'chart';
  24. }
  25. if (get_magic_quotes_gpc()) {
  26. $svg = stripslashes($svg);
  27. }
  28. // check for malicious attack in SVG
  29. if(strpos($svg,"<!ENTITY") !== false || strpos($svg,"<!DOCTYPE") !== false){
  30. exit("Execution is stopped, the posted SVG could contain code for a malicious attack");
  31. }
  32. $tempName = md5(rand());
  33. // allow no other than predefined types
  34. if ($type == 'image/png') {
  35. $typeString = '-m image/png';
  36. $ext = 'png';
  37. } elseif ($type == 'image/jpeg') {
  38. $typeString = '-m image/jpeg';
  39. $ext = 'jpg';
  40. } elseif ($type == 'application/pdf') {
  41. $typeString = '-m application/pdf';
  42. $ext = 'pdf';
  43. } elseif ($type == 'image/svg+xml') {
  44. $ext = 'svg';
  45. } else { // prevent fallthrough from global variables
  46. $ext = 'txt';
  47. }
  48. $outfile = "temp/$tempName.$ext";
  49. if (isset($typeString)) {
  50. // size
  51. $width = '';
  52. if ($_POST['width']) {
  53. $width = (int)$_POST['width'];
  54. if ($width) $width = "-w $width";
  55. }
  56. // generate the temporary file
  57. if (!file_put_contents("temp/$tempName.svg", $svg)) {
  58. die("Couldn't create temporary file. Check that the directory permissions for
  59. the /temp directory are set to 777.");
  60. }
  61. // Troubleshooting snippet
  62. /*
  63. $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";
  64. $output = shell_exec($command);
  65. echo "<pre>Command: $command <br>";
  66. echo "Output: $output</pre>";
  67. die;
  68. // */
  69. // Do the conversion
  70. $output = shell_exec("java -jar ". BATIK_PATH ." $typeString -d $outfile $width temp/$tempName.svg");
  71. // catch error
  72. if (!is_file($outfile) || filesize($outfile) < 10) {
  73. echo "<pre>$output</pre>";
  74. echo "Error while converting SVG. ";
  75. if (strpos($output, 'SVGConverter.error.while.rasterizing.file') !== false) {
  76. echo "
  77. <h4>Debug steps</h4>
  78. <ol>
  79. <li>Copy the SVG:<br/><textarea rows=5>" . htmlentities(str_replace('>', ">\n", $svg)) . "</textarea></li>
  80. <li>Go to <a href='http://validator.w3.org/#validate_by_input' target='_blank'>validator.w3.org/#validate_by_input</a></li>
  81. <li>Paste the SVG</li>
  82. <li>Click More Options and select SVG 1.1 for Use Doctype</li>
  83. <li>Click the Check button</li>
  84. </ol>";
  85. }
  86. }
  87. // stream it
  88. else {
  89. header("Content-Disposition: attachment; filename=\"$filename.$ext\"");
  90. header("Content-Type: $type");
  91. echo file_get_contents($outfile);
  92. }
  93. // delete it
  94. unlink("temp/$tempName.svg");
  95. unlink($outfile);
  96. // SVG can be streamed directly back
  97. } else if ($ext == 'svg') {
  98. header("Content-Disposition: attachment; filename=\"$filename.$ext\"");
  99. header("Content-Type: $type");
  100. echo $svg;
  101. } else {
  102. echo "Invalid type";
  103. }
  104. ?>