transformation_wrapper.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Wrapper script for rendering transformations
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. use PhpMyAdmin\Core;
  9. use PhpMyAdmin\Relation;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\Transformations;
  12. /**
  13. *
  14. */
  15. define('IS_TRANSFORMATION_WRAPPER', true);
  16. /**
  17. * Gets a core script and starts output buffering work
  18. */
  19. require_once './libraries/common.inc.php';
  20. $relation = new Relation();
  21. $cfgRelation = $relation->getRelationsParam();
  22. /**
  23. * Ensures db and table are valid, else moves to the "parent" script
  24. */
  25. require_once './libraries/db_table_exists.inc.php';
  26. /**
  27. * Sets globals from $_REQUEST
  28. */
  29. $request_params = array(
  30. 'cn',
  31. 'ct',
  32. 'sql_query',
  33. 'transform_key',
  34. 'where_clause'
  35. );
  36. $size_params = array(
  37. 'newHeight',
  38. 'newWidth',
  39. );
  40. foreach ($request_params as $one_request_param) {
  41. if (isset($_REQUEST[$one_request_param])) {
  42. if (in_array($one_request_param, $size_params)) {
  43. $GLOBALS[$one_request_param] = intval($_REQUEST[$one_request_param]);
  44. if ($GLOBALS[$one_request_param] > 2000) {
  45. $GLOBALS[$one_request_param] = 2000;
  46. }
  47. } else {
  48. $GLOBALS[$one_request_param] = $_REQUEST[$one_request_param];
  49. }
  50. }
  51. }
  52. /**
  53. * Get the list of the fields of the current table
  54. */
  55. $GLOBALS['dbi']->selectDb($db);
  56. if (isset($where_clause)) {
  57. $result = $GLOBALS['dbi']->query(
  58. 'SELECT * FROM ' . PhpMyAdmin\Util::backquote($table)
  59. . ' WHERE ' . $where_clause . ';',
  60. PhpMyAdmin\DatabaseInterface::CONNECT_USER,
  61. PhpMyAdmin\DatabaseInterface::QUERY_STORE
  62. );
  63. $row = $GLOBALS['dbi']->fetchAssoc($result);
  64. } else {
  65. $result = $GLOBALS['dbi']->query(
  66. 'SELECT * FROM ' . PhpMyAdmin\Util::backquote($table) . ' LIMIT 1;',
  67. PhpMyAdmin\DatabaseInterface::CONNECT_USER,
  68. PhpMyAdmin\DatabaseInterface::QUERY_STORE
  69. );
  70. $row = $GLOBALS['dbi']->fetchAssoc($result);
  71. }
  72. // No row returned
  73. if (! $row) {
  74. exit;
  75. } // end if (no record returned)
  76. $default_ct = 'application/octet-stream';
  77. if ($cfgRelation['commwork'] && $cfgRelation['mimework']) {
  78. $mime_map = Transformations::getMIME($db, $table);
  79. $mime_options = Transformations::getOptions(
  80. isset($mime_map[$transform_key]['transformation_options'])
  81. ? $mime_map[$transform_key]['transformation_options'] : ''
  82. );
  83. foreach ($mime_options as $key => $option) {
  84. if (substr($option, 0, 10) == '; charset=') {
  85. $mime_options['charset'] = $option;
  86. }
  87. }
  88. }
  89. // Only output the http headers
  90. $response = Response::getInstance();
  91. $response->getHeader()->sendHttpHeaders();
  92. // [MIME]
  93. if (isset($ct) && ! empty($ct)) {
  94. $mime_type = $ct;
  95. } else {
  96. $mime_type = (!empty($mime_map[$transform_key]['mimetype'])
  97. ? str_replace('_', '/', $mime_map[$transform_key]['mimetype'])
  98. : $default_ct)
  99. . (isset($mime_options['charset']) ? $mime_options['charset'] : '');
  100. }
  101. Core::downloadHeader($cn, $mime_type);
  102. if (! isset($_REQUEST['resize'])) {
  103. if (stripos($mime_type, 'html') === false) {
  104. echo $row[$transform_key];
  105. } else {
  106. echo htmlspecialchars($row[$transform_key]);
  107. }
  108. } else {
  109. // if image_*__inline.inc.php finds that we can resize,
  110. // it sets the resize parameter to jpeg or png
  111. $srcImage = imagecreatefromstring($row[$transform_key]);
  112. $srcWidth = ImageSX($srcImage);
  113. $srcHeight = ImageSY($srcImage);
  114. // Check to see if the width > height or if width < height
  115. // if so adjust accordingly to make sure the image
  116. // stays smaller than the new width and new height
  117. $ratioWidth = $srcWidth/$_REQUEST['newWidth'];
  118. $ratioHeight = $srcHeight/$_REQUEST['newHeight'];
  119. if ($ratioWidth < $ratioHeight) {
  120. $destWidth = $srcWidth/$ratioHeight;
  121. $destHeight = $_REQUEST['newHeight'];
  122. } else {
  123. $destWidth = $_REQUEST['newWidth'];
  124. $destHeight = $srcHeight/$ratioWidth;
  125. }
  126. if ($_REQUEST['resize']) {
  127. $destImage = ImageCreateTrueColor($destWidth, $destHeight);
  128. }
  129. // ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0,
  130. // $destWidth, $destHeight, $srcWidth, $srcHeight);
  131. // better quality but slower:
  132. ImageCopyResampled(
  133. $destImage, $srcImage, 0, 0, 0, 0, $destWidth,
  134. $destHeight, $srcWidth, $srcHeight
  135. );
  136. if ($_REQUEST['resize'] == 'jpeg') {
  137. ImageJPEG($destImage, null, 75);
  138. }
  139. if ($_REQUEST['resize'] == 'png') {
  140. ImagePNG($destImage);
  141. }
  142. ImageDestroy($srcImage);
  143. ImageDestroy($destImage);
  144. }