transformation_wrapper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. if (! Core::checkSqlQuerySignature($where_clause, isset($_GET['where_clause_sign']) ? $_GET['where_clause_sign'] : '')) {
  58. /* l10n: In case a SQL query did not pass a security check */
  59. Core::fatalError(__('There is an issue with your request.'));
  60. exit;
  61. }
  62. $result = $GLOBALS['dbi']->query(
  63. 'SELECT * FROM ' . PhpMyAdmin\Util::backquote($table)
  64. . ' WHERE ' . $where_clause . ';',
  65. PhpMyAdmin\DatabaseInterface::CONNECT_USER,
  66. PhpMyAdmin\DatabaseInterface::QUERY_STORE
  67. );
  68. $row = $GLOBALS['dbi']->fetchAssoc($result);
  69. } else {
  70. $result = $GLOBALS['dbi']->query(
  71. 'SELECT * FROM ' . PhpMyAdmin\Util::backquote($table) . ' LIMIT 1;',
  72. PhpMyAdmin\DatabaseInterface::CONNECT_USER,
  73. PhpMyAdmin\DatabaseInterface::QUERY_STORE
  74. );
  75. $row = $GLOBALS['dbi']->fetchAssoc($result);
  76. }
  77. // No row returned
  78. if (! $row) {
  79. exit;
  80. } // end if (no record returned)
  81. $default_ct = 'application/octet-stream';
  82. if ($cfgRelation['commwork'] && $cfgRelation['mimework']) {
  83. $mime_map = Transformations::getMIME($db, $table);
  84. $mime_options = Transformations::getOptions(
  85. isset($mime_map[$transform_key]['transformation_options'])
  86. ? $mime_map[$transform_key]['transformation_options'] : ''
  87. );
  88. foreach ($mime_options as $key => $option) {
  89. if (substr($option, 0, 10) == '; charset=') {
  90. $mime_options['charset'] = $option;
  91. }
  92. }
  93. }
  94. // Only output the http headers
  95. $response = Response::getInstance();
  96. $response->getHeader()->sendHttpHeaders();
  97. // [MIME]
  98. if (isset($ct) && ! empty($ct)) {
  99. $mime_type = $ct;
  100. } else {
  101. $mime_type = (!empty($mime_map[$transform_key]['mimetype'])
  102. ? str_replace('_', '/', $mime_map[$transform_key]['mimetype'])
  103. : $default_ct)
  104. . (isset($mime_options['charset']) ? $mime_options['charset'] : '');
  105. }
  106. Core::downloadHeader($cn, $mime_type);
  107. if (! isset($_REQUEST['resize'])) {
  108. if (stripos($mime_type, 'html') === false) {
  109. echo $row[$transform_key];
  110. } else {
  111. echo htmlspecialchars($row[$transform_key]);
  112. }
  113. } else {
  114. // if image_*__inline.inc.php finds that we can resize,
  115. // it sets the resize parameter to jpeg or png
  116. $srcImage = imagecreatefromstring($row[$transform_key]);
  117. $srcWidth = ImageSX($srcImage);
  118. $srcHeight = ImageSY($srcImage);
  119. // Check to see if the width > height or if width < height
  120. // if so adjust accordingly to make sure the image
  121. // stays smaller than the new width and new height
  122. $ratioWidth = $srcWidth/$_REQUEST['newWidth'];
  123. $ratioHeight = $srcHeight/$_REQUEST['newHeight'];
  124. if ($ratioWidth < $ratioHeight) {
  125. $destWidth = $srcWidth/$ratioHeight;
  126. $destHeight = $_REQUEST['newHeight'];
  127. } else {
  128. $destWidth = $_REQUEST['newWidth'];
  129. $destHeight = $srcHeight/$ratioWidth;
  130. }
  131. if ($_REQUEST['resize']) {
  132. $destImage = ImageCreateTrueColor($destWidth, $destHeight);
  133. }
  134. // ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0,
  135. // $destWidth, $destHeight, $srcWidth, $srcHeight);
  136. // better quality but slower:
  137. ImageCopyResampled(
  138. $destImage, $srcImage, 0, 0, 0, 0, $destWidth,
  139. $destHeight, $srcWidth, $srcHeight
  140. );
  141. if ($_REQUEST['resize'] == 'jpeg') {
  142. ImageJPEG($destImage, null, 75);
  143. }
  144. if ($_REQUEST['resize'] == 'png') {
  145. ImagePNG($destImage);
  146. }
  147. ImageDestroy($srcImage);
  148. ImageDestroy($destImage);
  149. }