Merge commit 'b47a5d992d' into QA_5_0
[phpmyadmin.git] / transformation_wrapper.php
blob4f7dbf0358123b262564d0b78f4e2b7738c305af
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Wrapper script for rendering transformations
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\DatabaseInterface;
12 use PhpMyAdmin\Relation;
13 use PhpMyAdmin\Response;
14 use PhpMyAdmin\Transformations;
16 if (! defined('ROOT_PATH')) {
17 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
20 define('IS_TRANSFORMATION_WRAPPER', true);
22 global $db, $table;
24 require_once ROOT_PATH . 'libraries/common.inc.php';
26 /** @var Response $response */
27 $response = $containerBuilder->get(Response::class);
29 /** @var DatabaseInterface $dbi */
30 $dbi = $containerBuilder->get(DatabaseInterface::class);
32 /** @var Transformations $transformations */
33 $transformations = $containerBuilder->get('transformations');
34 /** @var Relation $relation */
35 $relation = $containerBuilder->get('relation');
36 $cfgRelation = $relation->getRelationsParam();
38 /**
39 * Ensures db and table are valid, else moves to the "parent" script
41 require_once ROOT_PATH . 'libraries/db_table_exists.inc.php';
44 /**
45 * Sets globals from $_REQUEST
47 $request_params = [
48 'cn',
49 'ct',
50 'sql_query',
51 'transform_key',
52 'where_clause',
54 $size_params = [
55 'newHeight',
56 'newWidth',
58 foreach ($request_params as $one_request_param) {
59 if (isset($_REQUEST[$one_request_param])) {
60 if (in_array($one_request_param, $size_params)) {
61 $GLOBALS[$one_request_param] = intval($_REQUEST[$one_request_param]);
62 if ($GLOBALS[$one_request_param] > 2000) {
63 $GLOBALS[$one_request_param] = 2000;
65 } else {
66 $GLOBALS[$one_request_param] = $_REQUEST[$one_request_param];
72 /**
73 * Get the list of the fields of the current table
75 $dbi->selectDb($db);
76 if (isset($where_clause)) {
78 if (! Core::checkSqlQuerySignature($where_clause, isset($_GET['where_clause_sign']) ? $_GET['where_clause_sign'] : '')) {
79 /* l10n: In case a SQL query did not pass a security check */
80 Core::fatalError(__('There is an issue with your request.'));
81 exit;
83 $result = $dbi->query(
84 'SELECT * FROM ' . PhpMyAdmin\Util::backquote($table)
85 . ' WHERE ' . $where_clause . ';',
86 PhpMyAdmin\DatabaseInterface::CONNECT_USER,
87 PhpMyAdmin\DatabaseInterface::QUERY_STORE
89 $row = $dbi->fetchAssoc($result);
90 } else {
91 $result = $dbi->query(
92 'SELECT * FROM ' . PhpMyAdmin\Util::backquote($table) . ' LIMIT 1;',
93 PhpMyAdmin\DatabaseInterface::CONNECT_USER,
94 PhpMyAdmin\DatabaseInterface::QUERY_STORE
96 $row = $dbi->fetchAssoc($result);
99 // No row returned
100 if (! $row) {
101 exit;
102 } // end if (no record returned)
104 $default_ct = 'application/octet-stream';
106 if ($cfgRelation['commwork'] && $cfgRelation['mimework']) {
107 $mime_map = $transformations->getMime($db, $table);
108 $mime_options = $transformations->getOptions(
109 isset($mime_map[$transform_key]['transformation_options'])
110 ? $mime_map[$transform_key]['transformation_options'] : ''
113 foreach ($mime_options as $key => $option) {
114 if (substr($option, 0, 10) == '; charset=') {
115 $mime_options['charset'] = $option;
120 $response->getHeader()->sendHttpHeaders();
122 // [MIME]
123 if (isset($ct) && ! empty($ct)) {
124 $mime_type = $ct;
125 } else {
126 $mime_type = (! empty($mime_map[$transform_key]['mimetype'])
127 ? str_replace('_', '/', $mime_map[$transform_key]['mimetype'])
128 : $default_ct)
129 . (isset($mime_options['charset']) ? $mime_options['charset'] : '');
132 Core::downloadHeader($cn, $mime_type);
134 if (! isset($_REQUEST['resize'])) {
135 if (stripos($mime_type, 'html') === false) {
136 echo $row[$transform_key];
137 } else {
138 echo htmlspecialchars($row[$transform_key]);
140 } else {
141 // if image_*__inline.inc.php finds that we can resize,
142 // it sets the resize parameter to jpeg or png
144 $srcImage = imagecreatefromstring($row[$transform_key]);
145 $srcWidth = imagesx($srcImage);
146 $srcHeight = imagesy($srcImage);
148 // Check to see if the width > height or if width < height
149 // if so adjust accordingly to make sure the image
150 // stays smaller than the new width and new height
152 $ratioWidth = $srcWidth / $_REQUEST['newWidth'];
153 $ratioHeight = $srcHeight / $_REQUEST['newHeight'];
155 if ($ratioWidth < $ratioHeight) {
156 $destWidth = $srcWidth / $ratioHeight;
157 $destHeight = $_REQUEST['newHeight'];
158 } else {
159 $destWidth = $_REQUEST['newWidth'];
160 $destHeight = $srcHeight / $ratioWidth;
163 if ($_REQUEST['resize']) {
164 $destImage = imagecreatetruecolor($destWidth, $destHeight);
166 // ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0,
167 // $destWidth, $destHeight, $srcWidth, $srcHeight);
168 // better quality but slower:
169 imagecopyresampled(
170 $destImage,
171 $srcImage,
176 $destWidth,
177 $destHeight,
178 $srcWidth,
179 $srcHeight
181 if ($_REQUEST['resize'] == 'jpeg') {
182 imagejpeg($destImage, null, 75);
184 if ($_REQUEST['resize'] == 'png') {
185 imagepng($destImage);
187 imagedestroy($destImage);
189 imagedestroy($srcImage);