file_echo.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * "Echo" service to allow force downloading of exported charts (png or svg)
  5. * and server status monitor settings
  6. *
  7. * @package PhpMyAdmin
  8. */
  9. define('PMA_MINIMUM_COMMON', true);
  10. require_once 'libraries/common.inc.php';
  11. /* For chart exporting */
  12. if (isset($_REQUEST['filename']) && isset($_REQUEST['image'])) {
  13. $allowed = array(
  14. 'image/png' => 'png',
  15. 'image/svg+xml' => 'svg',
  16. );
  17. /* Check whether MIME type is allowed */
  18. if (! isset($allowed[$_REQUEST['type']])) {
  19. PMA_fatalError(__('Invalid export type'));
  20. }
  21. /*
  22. * Check file name to match mime type and not contain new lines
  23. * to prevent response splitting.
  24. */
  25. $extension = $allowed[$_REQUEST['type']];
  26. $valid_match = '/^[^\n\r]*\.' . $extension . '$/';
  27. if (! preg_match($valid_match, $_REQUEST['filename'])) {
  28. if (! preg_match('/^[^\n\r]*$/', $_REQUEST['filename'])) {
  29. /* Filename is unsafe, discard it */
  30. $filename = 'download.' . $extension;
  31. } else {
  32. /* Add extension */
  33. $filename = $_REQUEST['filename'] . '.' . $extension;
  34. }
  35. } else {
  36. /* Filename from request should be safe here */
  37. $filename = $_REQUEST['filename'];
  38. }
  39. /* Decode data */
  40. if ($extension != 'svg') {
  41. $data = substr($_REQUEST['image'], strpos($_REQUEST['image'], ',') + 1);
  42. $data = base64_decode($data);
  43. } else {
  44. $data = $_REQUEST['image'];
  45. }
  46. /* Send download header */
  47. PMA_downloadHeader($filename, $_REQUEST['type'], strlen($data));
  48. /* Send data */
  49. echo $data;
  50. } else if (isset($_REQUEST['monitorconfig'])) {
  51. /* For monitor chart config export */
  52. PMA_downloadHeader('monitor.cfg', 'application/force-download');
  53. echo urldecode($_REQUEST['monitorconfig']);
  54. } else if (isset($_REQUEST['import'])) {
  55. /* For monitor chart config import */
  56. header('Content-type: text/plain');
  57. if (!file_exists($_FILES['file']['tmp_name'])) {
  58. exit();
  59. }
  60. echo file_get_contents($_FILES['file']['tmp_name']);
  61. }
  62. ?>