filesave.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * filesave.php
  4. * To be used with ext-server_opensave.js for SVG-edit
  5. *
  6. * Licensed under the MIT License
  7. *
  8. * Copyright(c) 2010 Alexis Deveria
  9. *
  10. */
  11. exit;
  12. function encodeRFC5987ValueChars ($str) {
  13. // See http://tools.ietf.org/html/rfc5987#section-3.2.1
  14. // For better readability within headers, add back the characters escaped by rawurlencode but still allowable
  15. // Although RFC3986 reserves "!" (%21), RFC5987 does not
  16. return preg_replace_callback('@%(2[1346B]|5E|60|7C)@', function ($matches) {
  17. return chr('0x' . $matches[1]);
  18. }, rawurlencode($str));
  19. }
  20. require('allowedMimeTypes.php');
  21. $mime = !isset($_POST['mime']) || !in_array($_POST['mime'], $allowedMimeTypesBySuffix) ? 'image/svg+xml;charset=UTF-8' : $_POST['mime'];
  22. if (!isset($_POST['output_svg']) && !isset($_POST['output_img'])) {
  23. die('post fail');
  24. }
  25. $file = '';
  26. $suffix = '.' . array_search($mime, $allowedMimeTypesBySuffix);
  27. if (isset($_POST['filename']) && strlen($_POST['filename']) > 0) {
  28. $file = $_POST['filename'] . $suffix;
  29. } else {
  30. $file = 'image' . $suffix;
  31. }
  32. if ($suffix == '.svg') {
  33. $contents = $_POST['output_svg'];
  34. } else {
  35. $contents = $_POST['output_img'];
  36. $pos = (strpos($contents, 'base64,') + 7);
  37. $contents = base64_decode(substr($contents, $pos));
  38. }
  39. header("Cache-Control: public");
  40. header("Content-Description: File Transfer");
  41. // See http://tools.ietf.org/html/rfc6266#section-4.1
  42. header("Content-Disposition: attachment; filename*=UTF-8''" . encodeRFC5987ValueChars(
  43. // preg_replace('@[\\\\/:*?"<>|]@', '', $file) // If we wanted to strip Windows-disallowed characters server-side (but not a security issue, so we can strip client-side instead)
  44. $file
  45. ));
  46. header("Content-Type: " . $mime);
  47. header("Content-Transfer-Encoding: binary");
  48. echo $contents;
  49. ?>