GetFieldController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Table;
  4. use PhpMyAdmin\Core;
  5. use PhpMyAdmin\DatabaseInterface;
  6. use PhpMyAdmin\Html\Generator;
  7. use PhpMyAdmin\Mime;
  8. use PhpMyAdmin\Response;
  9. use PhpMyAdmin\Template;
  10. use PhpMyAdmin\Util;
  11. use function htmlspecialchars;
  12. use function ini_set;
  13. use function sprintf;
  14. use function strlen;
  15. /**
  16. * Provides download to a given field defined in parameters.
  17. */
  18. class GetFieldController extends AbstractController
  19. {
  20. /** @var DatabaseInterface */
  21. private $dbi;
  22. /**
  23. * @param Response $response
  24. * @param string $db Database name.
  25. * @param string $table Table name.
  26. * @param DatabaseInterface $dbi
  27. */
  28. public function __construct($response, Template $template, $db, $table, $dbi)
  29. {
  30. parent::__construct($response, $template, $db, $table);
  31. $this->dbi = $dbi;
  32. }
  33. public function index(): void
  34. {
  35. global $db, $table;
  36. $this->response->disable();
  37. /* Check parameters */
  38. Util::checkParameters([
  39. 'db',
  40. 'table',
  41. ]);
  42. /* Select database */
  43. if (! $this->dbi->selectDb($db)) {
  44. Generator::mysqlDie(
  45. sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
  46. '',
  47. false
  48. );
  49. }
  50. /* Check if table exists */
  51. if (! $this->dbi->getColumns($db, $table)) {
  52. Generator::mysqlDie(__('Invalid table name'));
  53. }
  54. if (! isset($_GET['where_clause'])
  55. || ! isset($_GET['where_clause_sign'])
  56. || ! Core::checkSqlQuerySignature($_GET['where_clause'], $_GET['where_clause_sign'])
  57. ) {
  58. /* l10n: In case a SQL query did not pass a security check */
  59. Core::fatalError(__('There is an issue with your request.'));
  60. return;
  61. }
  62. /* Grab data */
  63. $sql = 'SELECT ' . Util::backquote($_GET['transform_key'])
  64. . ' FROM ' . Util::backquote($table)
  65. . ' WHERE ' . $_GET['where_clause'] . ';';
  66. $result = $this->dbi->fetchValue($sql);
  67. /* Check return code */
  68. if ($result === false) {
  69. Generator::mysqlDie(
  70. __('MySQL returned an empty result set (i.e. zero rows).'),
  71. $sql
  72. );
  73. }
  74. /* Avoid corrupting data */
  75. ini_set('url_rewriter.tags', '');
  76. Core::downloadHeader(
  77. $table . '-' . $_GET['transform_key'] . '.bin',
  78. Mime::detect($result),
  79. strlen($result)
  80. );
  81. echo $result;
  82. }
  83. }