Translated using Weblate (Czech)
[phpmyadmin.git] / transformation_wrapper.php
blob03c4a31c94778cfdb8be04a7f2195bf5c0372351
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)) {
77 if (! Core::checkSqlQuerySignature($where_clause, isset($_GET['where_clause_sign']) ? $_GET['where_clause_sign'] : '')) {
78 /* l10n: In case a SQL query did not pass a security check */
79 Core::fatalError(__('There is an issue with your request.'));
80 exit;
82 $result = $dbi->query(
83 'SELECT * FROM ' . PhpMyAdmin\Util::backquote($table)
84 . ' WHERE ' . $where_clause . ';',
85 PhpMyAdmin\DatabaseInterface::CONNECT_USER,
86 PhpMyAdmin\DatabaseInterface::QUERY_STORE
88 $row = $dbi->fetchAssoc($result);
89 } else {
90 $result = $dbi->query(
91 'SELECT * FROM ' . PhpMyAdmin\Util::backquote($table) . ' LIMIT 1;',
92 PhpMyAdmin\DatabaseInterface::CONNECT_USER,
93 PhpMyAdmin\DatabaseInterface::QUERY_STORE
95 $row = $dbi->fetchAssoc($result);
98 // No row returned
99 if (! $row) {
100 exit;
101 } // end if (no record returned)
103 $default_ct = 'application/octet-stream';
105 if ($cfgRelation['commwork'] && $cfgRelation['mimework']) {
106 $mime_map = $transformations->getMime($db, $table);
107 $mime_options = $transformations->getOptions(
108 isset($mime_map[$transform_key]['transformation_options'])
109 ? $mime_map[$transform_key]['transformation_options'] : ''
112 foreach ($mime_options as $key => $option) {
113 if (substr($option, 0, 10) == '; charset=') {
114 $mime_options['charset'] = $option;
119 $response->getHeader()->sendHttpHeaders();
121 // [MIME]
122 if (isset($ct) && ! empty($ct)) {
123 $mime_type = $ct;
124 } else {
125 $mime_type = (! empty($mime_map[$transform_key]['mimetype'])
126 ? str_replace('_', '/', $mime_map[$transform_key]['mimetype'])
127 : $default_ct)
128 . (isset($mime_options['charset']) ? $mime_options['charset'] : '');
131 Core::downloadHeader($cn, $mime_type);
133 if (! isset($_REQUEST['resize'])) {
134 if (stripos($mime_type, 'html') === false) {
135 echo $row[$transform_key];
136 } else {
137 echo htmlspecialchars($row[$transform_key]);
139 } else {
140 // if image_*__inline.inc.php finds that we can resize,
141 // it sets the resize parameter to jpeg or png
143 $srcImage = imagecreatefromstring($row[$transform_key]);
144 $srcWidth = imagesx($srcImage);
145 $srcHeight = imagesy($srcImage);
147 // Check to see if the width > height or if width < height
148 // if so adjust accordingly to make sure the image
149 // stays smaller than the new width and new height
151 $ratioWidth = $srcWidth / $_REQUEST['newWidth'];
152 $ratioHeight = $srcHeight / $_REQUEST['newHeight'];
154 if ($ratioWidth < $ratioHeight) {
155 $destWidth = $srcWidth / $ratioHeight;
156 $destHeight = $_REQUEST['newHeight'];
157 } else {
158 $destWidth = $_REQUEST['newWidth'];
159 $destHeight = $srcHeight / $ratioWidth;
162 if ($_REQUEST['resize']) {
163 $destImage = imagecreatetruecolor($destWidth, $destHeight);
165 // ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0,
166 // $destWidth, $destHeight, $srcWidth, $srcHeight);
167 // better quality but slower:
168 imagecopyresampled(
169 $destImage,
170 $srcImage,
175 $destWidth,
176 $destHeight,
177 $srcWidth,
178 $srcHeight
180 if ($_REQUEST['resize'] == 'jpeg') {
181 imagejpeg($destImage, null, 75);
183 if ($_REQUEST['resize'] == 'png') {
184 imagepng($destImage);
186 imagedestroy($destImage);
188 imagedestroy($srcImage);