CreateController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Table;
  4. use PhpMyAdmin\Config;
  5. use PhpMyAdmin\Core;
  6. use PhpMyAdmin\CreateAddField;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\Html\Generator;
  9. use PhpMyAdmin\Relation;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\Table\ColumnsDefinition;
  12. use PhpMyAdmin\Template;
  13. use PhpMyAdmin\Transformations;
  14. use PhpMyAdmin\Url;
  15. use PhpMyAdmin\Util;
  16. use function htmlspecialchars;
  17. use function is_array;
  18. use function mb_strtolower;
  19. use function sprintf;
  20. use function strlen;
  21. /**
  22. * Displays table create form and handles it.
  23. */
  24. class CreateController extends AbstractController
  25. {
  26. /** @var Transformations */
  27. private $transformations;
  28. /** @var Config */
  29. private $config;
  30. /** @var Relation */
  31. private $relation;
  32. /** @var DatabaseInterface */
  33. private $dbi;
  34. /**
  35. * @param Response $response
  36. * @param string $db Database name.
  37. * @param string $table Table name.
  38. * @param DatabaseInterface $dbi
  39. */
  40. public function __construct(
  41. $response,
  42. Template $template,
  43. $db,
  44. $table,
  45. Transformations $transformations,
  46. Config $config,
  47. Relation $relation,
  48. $dbi
  49. ) {
  50. parent::__construct($response, $template, $db, $table);
  51. $this->transformations = $transformations;
  52. $this->config = $config;
  53. $this->relation = $relation;
  54. $this->dbi = $dbi;
  55. }
  56. public function index(): void
  57. {
  58. global $num_fields, $action, $sql_query, $result, $db, $table;
  59. Util::checkParameters(['db']);
  60. $cfg = $this->config->settings;
  61. /* Check if database name is empty */
  62. if (strlen($db) === 0) {
  63. Generator::mysqlDie(
  64. __('The database name is empty!'),
  65. '',
  66. false,
  67. 'index.php'
  68. );
  69. }
  70. /**
  71. * Selects the database to work with
  72. */
  73. if (! $this->dbi->selectDb($db)) {
  74. Generator::mysqlDie(
  75. sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
  76. '',
  77. false,
  78. 'index.php'
  79. );
  80. }
  81. if ($this->dbi->getColumns($db, $table)) {
  82. // table exists already
  83. Generator::mysqlDie(
  84. sprintf(__('Table %s already exists!'), htmlspecialchars($table)),
  85. '',
  86. false,
  87. Url::getFromRoute('/database/structure', ['db' => $db])
  88. );
  89. }
  90. $createAddField = new CreateAddField($this->dbi);
  91. $num_fields = $createAddField->getNumberOfFieldsFromRequest();
  92. $action = Url::getFromRoute('/table/create');
  93. /**
  94. * The form used to define the structure of the table has been submitted
  95. */
  96. if (isset($_POST['do_save_data'])) {
  97. // lower_case_table_names=1 `DB` becomes `db`
  98. if ($this->dbi->getLowerCaseNames() === '1') {
  99. $db = mb_strtolower(
  100. $db
  101. );
  102. $table = mb_strtolower(
  103. $table
  104. );
  105. }
  106. $sql_query = $createAddField->getTableCreationQuery($db, $table);
  107. // If there is a request for SQL previewing.
  108. if (isset($_POST['preview_sql'])) {
  109. Core::previewSQL($sql_query);
  110. return;
  111. }
  112. // Executes the query
  113. $result = $this->dbi->tryQuery($sql_query);
  114. if ($result) {
  115. // Update comment table for mime types [MIME]
  116. if (isset($_POST['field_mimetype'])
  117. && is_array($_POST['field_mimetype'])
  118. && $cfg['BrowseMIME']
  119. ) {
  120. foreach ($_POST['field_mimetype'] as $fieldindex => $mimetype) {
  121. if (! isset($_POST['field_name'][$fieldindex])
  122. || strlen($_POST['field_name'][$fieldindex]) <= 0
  123. ) {
  124. continue;
  125. }
  126. $this->transformations->setMime(
  127. $db,
  128. $table,
  129. $_POST['field_name'][$fieldindex],
  130. $mimetype,
  131. $_POST['field_transformation'][$fieldindex],
  132. $_POST['field_transformation_options'][$fieldindex],
  133. $_POST['field_input_transformation'][$fieldindex],
  134. $_POST['field_input_transformation_options'][$fieldindex]
  135. );
  136. }
  137. }
  138. } else {
  139. $this->response->setRequestStatus(false);
  140. $this->response->addJSON('message', $this->dbi->getError());
  141. }
  142. return;
  143. }
  144. // This global variable needs to be reset for the header class to function properly
  145. $table = '';
  146. $this->addScriptFiles(['vendor/jquery/jquery.uitablefilter.js', 'indexes.js']);
  147. $templateData = ColumnsDefinition::displayForm(
  148. $this->transformations,
  149. $this->relation,
  150. $this->dbi,
  151. $action,
  152. $num_fields
  153. );
  154. $this->render('columns_definitions/column_definitions_form', $templateData);
  155. }
  156. }