initial commit

This commit is contained in:
ns77@siliconpin.com
2025-08-18 12:05:35 +00:00
commit 5c1e30c087
4586 changed files with 818325 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Util;
use function __;
use function count;
final class AddIndexController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
DatabaseInterface $dbi,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->dbi = $dbi;
$this->structureController = $structureController;
}
public function __invoke(): void
{
global $sql_query, $db, $table, $message;
$selected = $_POST['selected_fld'] ?? [];
if (empty($selected)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No column selected.'));
return;
}
$i = 1;
$selectedCount = count($selected);
$sql_query = 'ALTER TABLE ' . Util::backquote($table) . ' ADD INDEX(';
foreach ($selected as $field) {
$sql_query .= Util::backquote($field);
$sql_query .= $i++ === $selectedCount ? ');' : ', ';
}
$this->dbi->selectDb($db);
$result = $this->dbi->tryQuery($sql_query);
if (! $result) {
$message = Message::error($this->dbi->getError());
}
if (empty($message)) {
$message = Message::success();
}
($this->structureController)();
}
}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Sql\SqlController;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
final class AddKeyController extends AbstractController
{
/** @var SqlController */
private $sqlController;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
SqlController $sqlController,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->sqlController = $sqlController;
$this->structureController = $structureController;
}
public function __invoke(): void
{
global $reload;
($this->sqlController)();
$reload = true;
($this->structureController)();
}
}

View File

@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\ParseAnalyze;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Sql;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
use function __;
use function implode;
use function sprintf;
final class BrowseController extends AbstractController
{
/** @var Sql */
private $sql;
public function __construct(ResponseRenderer $response, Template $template, string $db, string $table, Sql $sql)
{
parent::__construct($response, $template, $db, $table);
$this->sql = $sql;
}
public function __invoke(): void
{
if (empty($_POST['selected_fld'])) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No column selected.'));
return;
}
$this->displayTableBrowseForSelectedColumns($GLOBALS['goto']);
}
/**
* Function to display table browse for selected columns
*
* @param string $goto goto page url
*/
private function displayTableBrowseForSelectedColumns($goto): void
{
$GLOBALS['active_page'] = Url::getFromRoute('/sql');
$fields = [];
foreach ($_POST['selected_fld'] as $sval) {
$fields[] = Util::backquote($sval);
}
$sql_query = sprintf(
'SELECT %s FROM %s.%s',
implode(', ', $fields),
Util::backquote($this->db),
Util::backquote($this->table)
);
// Parse and analyze the query
[$analyzed_sql_results, $this->db] = ParseAnalyze::sqlQuery($sql_query, $this->db);
$this->response->addHTML(
$this->sql->executeQueryAndGetQueryResponse(
$analyzed_sql_results ?? '',
false, // is_gotofile
$this->db, // db
$this->table, // table
null, // find_real_end
null, // sql_query_for_bookmark
null, // extra_data
null, // message_to_show
null, // sql_data
$goto, // goto
null, // disp_query
null, // disp_message
$sql_query, // sql_query
null // complete_query
)
);
}
}

View File

@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\Database\CentralColumns;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use function __;
final class CentralColumnsAddController extends AbstractController
{
/** @var CentralColumns */
private $centralColumns;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
CentralColumns $centralColumns,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->centralColumns = $centralColumns;
$this->structureController = $structureController;
}
public function __invoke(): void
{
global $message;
$selected = $_POST['selected_fld'] ?? [];
if (empty($selected)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No column selected.'));
return;
}
$centralColsError = $this->centralColumns->syncUniqueColumns($selected, false);
if ($centralColsError instanceof Message) {
$message = $centralColsError;
}
if (empty($message)) {
$message = Message::success();
}
($this->structureController)();
}
}

View File

@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\Database\CentralColumns;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use function __;
final class CentralColumnsRemoveController extends AbstractController
{
/** @var CentralColumns */
private $centralColumns;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
CentralColumns $centralColumns,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->centralColumns = $centralColumns;
$this->structureController = $structureController;
}
public function __invoke(): void
{
global $db, $message;
$selected = $_POST['selected_fld'] ?? [];
if (empty($selected)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No column selected.'));
return;
}
$centralColsError = $this->centralColumns->deleteColumnsFromList($db, $selected, false);
if ($centralColsError instanceof Message) {
$message = $centralColsError;
}
if (empty($message)) {
$message = Message::success();
}
($this->structureController)();
}
}

View File

@@ -0,0 +1,125 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\CheckUserPrivileges;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Table\ColumnsDefinition;
use PhpMyAdmin\Template;
use PhpMyAdmin\Transformations;
use PhpMyAdmin\Url;
use function __;
use function count;
final class ChangeController extends AbstractController
{
/** @var Relation */
private $relation;
/** @var Transformations */
private $transformations;
/** @var DatabaseInterface */
private $dbi;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
Relation $relation,
Transformations $transformations,
DatabaseInterface $dbi
) {
parent::__construct($response, $template, $db, $table);
$this->relation = $relation;
$this->transformations = $transformations;
$this->dbi = $dbi;
}
public function __invoke(): void
{
if (isset($_GET['change_column'])) {
$this->displayHtmlForColumnChange(null);
return;
}
$selected = $_POST['selected_fld'] ?? [];
if (empty($selected)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No column selected.'));
return;
}
$this->displayHtmlForColumnChange($selected);
}
/**
* Displays HTML for changing one or more columns
*
* @param array|null $selected the selected columns
*/
private function displayHtmlForColumnChange(?array $selected): void
{
global $action, $num_fields;
if (empty($selected)) {
$selected[] = $_REQUEST['field'];
$selected_cnt = 1;
} else { // from a multiple submit
$selected_cnt = count($selected);
}
/**
* @todo optimize in case of multiple fields to modify
*/
$fields_meta = [];
for ($i = 0; $i < $selected_cnt; $i++) {
$value = $this->dbi->getColumn($this->db, $this->table, $selected[$i], true);
if (count($value) === 0) {
$message = Message::error(
__('Failed to get description of column %s!')
);
$message->addParam($selected[$i]);
$this->response->addHTML($message->getDisplay());
} else {
$fields_meta[] = $value;
}
}
$num_fields = count($fields_meta);
$action = Url::getFromRoute('/table/structure/save');
/**
* Form for changing properties.
*/
$checkUserPrivileges = new CheckUserPrivileges($this->dbi);
$checkUserPrivileges->getPrivileges();
$this->addScriptFiles(['vendor/jquery/jquery.uitablefilter.js', 'indexes.js']);
$templateData = ColumnsDefinition::displayForm(
$this->transformations,
$this->relation,
$this->dbi,
$action,
$num_fields,
null,
$selected,
$fields_meta
);
$this->render('columns_definitions/column_definitions_form', $templateData);
}
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Util;
use function __;
use function count;
final class FulltextController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
DatabaseInterface $dbi,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->dbi = $dbi;
$this->structureController = $structureController;
}
public function __invoke(): void
{
global $sql_query, $db, $table, $message;
$selected = $_POST['selected_fld'] ?? [];
if (empty($selected)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No column selected.'));
return;
}
$i = 1;
$selectedCount = count($selected);
$sql_query = 'ALTER TABLE ' . Util::backquote($table) . ' ADD FULLTEXT(';
foreach ($selected as $field) {
$sql_query .= Util::backquote($field);
$sql_query .= $i++ === $selectedCount ? ');' : ', ';
}
$this->dbi->selectDb($db);
$result = $this->dbi->tryQuery($sql_query);
if (! $result) {
$message = Message::error($this->dbi->getError());
}
if (empty($message)) {
$message = Message::success();
}
($this->structureController)();
}
}

View File

@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\SqlParser\Components\CreateDefinition;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use PhpMyAdmin\Template;
use PhpMyAdmin\Util;
use function __;
use function array_diff;
use function array_keys;
use function array_search;
use function array_splice;
use function assert;
use function count;
use function implode;
use function is_array;
final class MoveColumnsController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
DatabaseInterface $dbi
) {
parent::__construct($response, $template, $db, $table);
$this->dbi = $dbi;
}
public function __invoke(): void
{
$moveColumns = $_POST['move_columns'] ?? null;
$previewSql = $_REQUEST['preview_sql'] ?? null;
if (! is_array($moveColumns) || ! $this->response->isAjax()) {
$this->response->setRequestStatus(false);
return;
}
$this->dbi->selectDb($this->db);
$createTableSql = $this->dbi->getTable($this->db, $this->table)->showCreate();
$sql_query = $this->generateAlterTableSql($createTableSql, $moveColumns);
if ($sql_query === null) {
$this->response->setRequestStatus(false);
return;
}
if ($previewSql) {
$this->response->addJSON(
'sql_data',
$this->template->render('preview_sql', ['query_data' => $sql_query])
);
return;
}
$this->dbi->tryQuery($sql_query);
$tmp_error = $this->dbi->getError();
if ($tmp_error !== '') {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', Message::error($tmp_error));
return;
}
$message = Message::success(
__('The columns have been moved successfully.')
);
$this->response->addJSON('message', $message);
$this->response->addJSON('columns', $moveColumns);
}
/**
* @param array<int,mixed> $moveColumns
* @psalm-param list<mixed> $moveColumns
*/
private function generateAlterTableSql(string $createTableSql, array $moveColumns): ?string
{
$parser = new Parser($createTableSql);
/** @var CreateStatement $statement */
$statement = $parser->statements[0];
/** @var CreateDefinition[] $fields */
$fields = $statement->fields;
$columns = [];
foreach ($fields as $field) {
if ($field->name === null) {
continue;
}
$columns[$field->name] = $field;
}
$columnNames = array_keys($columns);
// Ensure the columns from client match the columns from the table
if (
count($columnNames) !== count($moveColumns) ||
array_diff($columnNames, $moveColumns) !== []
) {
return null;
}
$changes = [];
// move columns from first to last
/** @psalm-var list<string> $moveColumns */
foreach ($moveColumns as $i => $columnName) {
// is this column already correctly placed?
if ($columnNames[$i] == $columnName) {
continue;
}
$changes[] =
'CHANGE ' . Util::backquote($columnName) . ' ' . CreateDefinition::build($columns[$columnName]) .
($i === 0 ? ' FIRST' : ' AFTER ' . Util::backquote($columnNames[$i - 1]));
// Move column to its new position
/** @var int $j */
$j = array_search($columnName, $columnNames, true);
array_splice($columnNames, $j, 1);
array_splice($columnNames, $i, 0, $columnName);
}
if ($changes === []) {
return null;
}
assert($statement->name !== null, 'Alter table statement has no name');
return 'ALTER TABLE ' . Util::backquote($statement->name->table) . "\n " . implode(",\n ", $changes);
}
}

View File

@@ -0,0 +1,282 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Config\PageSettings;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\CreateAddField;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Message;
use PhpMyAdmin\Partitioning\TablePartitionDefinition;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use PhpMyAdmin\StorageEngine;
use PhpMyAdmin\Table;
use PhpMyAdmin\Template;
use PhpMyAdmin\Util;
use function __;
use function count;
use function strpos;
use function strrpos;
use function substr;
use function trim;
final class PartitioningController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;
/** @var CreateAddField */
private $createAddField;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
DatabaseInterface $dbi,
CreateAddField $createAddField,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->dbi = $dbi;
$this->createAddField = $createAddField;
$this->structureController = $structureController;
}
public function __invoke(): void
{
if (isset($_POST['save_partitioning'])) {
$this->dbi->selectDb($this->db);
$this->updatePartitioning();
($this->structureController)();
return;
}
$pageSettings = new PageSettings('TableStructure');
$this->response->addHTML($pageSettings->getErrorHTML());
$this->response->addHTML($pageSettings->getHTML());
$this->addScriptFiles(['table/structure.js', 'indexes.js']);
$partitionDetails = null;
if (! isset($_POST['partition_by'])) {
$partitionDetails = $this->extractPartitionDetails();
}
$storageEngines = StorageEngine::getArray();
$partitionDetails = TablePartitionDefinition::getDetails($partitionDetails);
$this->render('table/structure/partition_definition_form', [
'db' => $this->db,
'table' => $this->table,
'partition_details' => $partitionDetails,
'storage_engines' => $storageEngines,
]);
}
/**
* Extracts partition details from CREATE TABLE statement
*
* @return array<string, array<int, array<string, mixed>>|bool|int|string>|null array of partition details
*/
private function extractPartitionDetails(): ?array
{
$createTable = (new Table($this->table, $this->db))->showCreate();
if ($createTable === '') {
return null;
}
$parser = new Parser($createTable);
/**
* @var CreateStatement $stmt
*/
$stmt = $parser->statements[0];
$partitionDetails = [];
$partitionDetails['partition_by'] = '';
$partitionDetails['partition_expr'] = '';
$partitionDetails['partition_count'] = 0;
if (! empty($stmt->partitionBy)) {
$openPos = strpos($stmt->partitionBy, '(');
$closePos = strrpos($stmt->partitionBy, ')');
if ($openPos !== false && $closePos !== false) {
$partitionDetails['partition_by'] = trim(substr($stmt->partitionBy, 0, $openPos));
$partitionDetails['partition_expr'] = trim(substr(
$stmt->partitionBy,
$openPos + 1,
$closePos - ($openPos + 1)
));
$count = $stmt->partitionsNum ?? count($stmt->partitions);
$partitionDetails['partition_count'] = $count;
}
}
$partitionDetails['subpartition_by'] = '';
$partitionDetails['subpartition_expr'] = '';
$partitionDetails['subpartition_count'] = 0;
if (! empty($stmt->subpartitionBy)) {
$openPos = strpos($stmt->subpartitionBy, '(');
$closePos = strrpos($stmt->subpartitionBy, ')');
if ($openPos !== false && $closePos !== false) {
$partitionDetails['subpartition_by'] = trim(substr($stmt->subpartitionBy, 0, $openPos));
$partitionDetails['subpartition_expr'] = trim(substr(
$stmt->subpartitionBy,
$openPos + 1,
$closePos - ($openPos + 1)
));
$count = $stmt->subpartitionsNum ?? count($stmt->partitions[0]->subpartitions);
$partitionDetails['subpartition_count'] = $count;
}
}
// Only LIST and RANGE type parameters allow subpartitioning
$partitionDetails['can_have_subpartitions'] = $partitionDetails['partition_count'] > 1
&& ($partitionDetails['partition_by'] === 'RANGE'
|| $partitionDetails['partition_by'] === 'RANGE COLUMNS'
|| $partitionDetails['partition_by'] === 'LIST'
|| $partitionDetails['partition_by'] === 'LIST COLUMNS');
// Values are specified only for LIST and RANGE type partitions
$partitionDetails['value_enabled'] = isset($partitionDetails['partition_by'])
&& ($partitionDetails['partition_by'] === 'RANGE'
|| $partitionDetails['partition_by'] === 'RANGE COLUMNS'
|| $partitionDetails['partition_by'] === 'LIST'
|| $partitionDetails['partition_by'] === 'LIST COLUMNS');
$partitionDetails['partitions'] = [];
for ($i = 0, $iMax = $partitionDetails['partition_count']; $i < $iMax; $i++) {
if (! isset($stmt->partitions[$i])) {
$partitionDetails['partitions'][$i] = [
'name' => 'p' . $i,
'value_type' => '',
'value' => '',
'engine' => '',
'comment' => '',
'data_directory' => '',
'index_directory' => '',
'max_rows' => '',
'min_rows' => '',
'tablespace' => '',
'node_group' => '',
];
} else {
$p = $stmt->partitions[$i];
$type = $p->type;
$expr = trim((string) $p->expr, '()');
if ($expr === 'MAXVALUE') {
$type .= ' MAXVALUE';
$expr = '';
}
$partitionDetails['partitions'][$i] = [
'name' => $p->name,
'value_type' => $type,
'value' => $expr,
'engine' => $p->options->has('ENGINE', true),
'comment' => trim((string) $p->options->has('COMMENT', true), "'"),
'data_directory' => trim((string) $p->options->has('DATA DIRECTORY', true), "'"),
'index_directory' => trim((string) $p->options->has('INDEX_DIRECTORY', true), "'"),
'max_rows' => $p->options->has('MAX_ROWS', true),
'min_rows' => $p->options->has('MIN_ROWS', true),
'tablespace' => $p->options->has('TABLESPACE', true),
'node_group' => $p->options->has('NODEGROUP', true),
];
}
$partition =& $partitionDetails['partitions'][$i];
$partition['prefix'] = 'partitions[' . $i . ']';
if ($partitionDetails['subpartition_count'] <= 1) {
continue;
}
$partition['subpartition_count'] = $partitionDetails['subpartition_count'];
$partition['subpartitions'] = [];
for ($j = 0, $jMax = $partitionDetails['subpartition_count']; $j < $jMax; $j++) {
if (! isset($stmt->partitions[$i]->subpartitions[$j])) {
$partition['subpartitions'][$j] = [
'name' => $partition['name'] . '_s' . $j,
'engine' => '',
'comment' => '',
'data_directory' => '',
'index_directory' => '',
'max_rows' => '',
'min_rows' => '',
'tablespace' => '',
'node_group' => '',
];
} else {
$sp = $stmt->partitions[$i]->subpartitions[$j];
$partition['subpartitions'][$j] = [
'name' => $sp->name,
'engine' => $sp->options->has('ENGINE', true),
'comment' => trim((string) $sp->options->has('COMMENT', true), "'"),
'data_directory' => trim((string) $sp->options->has('DATA DIRECTORY', true), "'"),
'index_directory' => trim((string) $sp->options->has('INDEX_DIRECTORY', true), "'"),
'max_rows' => $sp->options->has('MAX_ROWS', true),
'min_rows' => $sp->options->has('MIN_ROWS', true),
'tablespace' => $sp->options->has('TABLESPACE', true),
'node_group' => $sp->options->has('NODEGROUP', true),
];
}
$subpartition =& $partition['subpartitions'][$j];
$subpartition['prefix'] = 'partitions[' . $i . ']'
. '[subpartitions][' . $j . ']';
}
}
return $partitionDetails;
}
private function updatePartitioning(): void
{
$sql_query = 'ALTER TABLE ' . Util::backquote($this->table) . ' '
. $this->createAddField->getPartitionsDefinition();
// Execute alter query
$result = $this->dbi->tryQuery($sql_query);
if ($result === false) {
$this->response->setRequestStatus(false);
$this->response->addJSON(
'message',
Message::rawError(
__('Query error') . ':<br>' . $this->dbi->getError()
)
);
return;
}
$message = Message::success(
__('Table %1$s has been altered successfully.')
);
$message->addParam($this->table);
$this->response->addHTML(
Generator::getMessage($message, $sql_query, 'success')
);
}
}

View File

@@ -0,0 +1,135 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\DbTableExists;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
use function __;
use function count;
final class PrimaryController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
DatabaseInterface $dbi,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->dbi = $dbi;
$this->structureController = $structureController;
}
public function __invoke(): void
{
global $db, $table, $message, $sql_query, $urlParams, $errorUrl, $cfg;
$selected = $_POST['selected'] ?? [];
$selected_fld = $_POST['selected_fld'] ?? [];
if (empty($selected) && empty($selected_fld)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No column selected.'));
return;
}
$primary = $this->getKeyForTablePrimary();
if (empty($primary) && ! empty($selected_fld)) {
// no primary key, so we can safely create new
$mult_btn = __('Yes');
$selected = $selected_fld;
}
$mult_btn = $_POST['mult_btn'] ?? $mult_btn ?? '';
if (! empty($selected_fld) && ! empty($primary)) {
Util::checkParameters(['db', 'table']);
$urlParams = ['db' => $db, 'table' => $table];
$errorUrl = Util::getScriptNameForOption($cfg['DefaultTabTable'], 'table');
$errorUrl .= Url::getCommon($urlParams, '&');
DbTableExists::check();
$this->render('table/structure/primary', [
'db' => $db,
'table' => $table,
'selected' => $selected_fld,
]);
return;
}
if ($mult_btn === __('Yes')) {
$sql_query = 'ALTER TABLE ' . Util::backquote($table);
if (! empty($primary)) {
$sql_query .= ' DROP PRIMARY KEY,';
}
$sql_query .= ' ADD PRIMARY KEY(';
$i = 1;
$selectedCount = count($selected);
foreach ($selected as $field) {
$sql_query .= Util::backquote($field);
$sql_query .= $i++ === $selectedCount ? ');' : ', ';
}
$this->dbi->selectDb($db);
$result = $this->dbi->tryQuery($sql_query);
if (! $result) {
$message = Message::error($this->dbi->getError());
}
}
if (empty($message)) {
$message = Message::success();
}
($this->structureController)();
}
/**
* Gets table primary key
*
* @return string
*/
private function getKeyForTablePrimary()
{
$this->dbi->selectDb($this->db);
$result = $this->dbi->query(
'SHOW KEYS FROM ' . Util::backquote($this->table) . ';'
);
$primary = '';
foreach ($result as $row) {
// Backups the list of primary keys
if ($row['Key_name'] !== 'PRIMARY') {
continue;
}
$primary .= $row['Column_name'] . ', ';
}
return $primary;
}
}

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\SqlParser\Context;
use function _ngettext;
use function count;
use function implode;
use function sprintf;
use function trim;
final class ReservedWordCheckController extends AbstractController
{
public function __invoke(): void
{
if ($GLOBALS['cfg']['ReservedWordDisableWarning'] !== false) {
$this->response->setRequestStatus(false);
return;
}
$columns_names = $_POST['field_name'];
$reserved_keywords_names = [];
foreach ($columns_names as $column) {
if (! Context::isKeyword(trim($column), true)) {
continue;
}
$reserved_keywords_names[] = trim($column);
}
if (Context::isKeyword(trim($this->table), true)) {
$reserved_keywords_names[] = trim($this->table);
}
if (count($reserved_keywords_names) === 0) {
$this->response->setRequestStatus(false);
}
$this->response->addJSON(
'message',
sprintf(
_ngettext(
'The name \'%s\' is a MySQL reserved keyword.',
'The names \'%s\' are MySQL reserved keywords.',
count($reserved_keywords_names)
),
implode(',', $reserved_keywords_names)
)
);
}
}

View File

@@ -0,0 +1,411 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Index;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Table;
use PhpMyAdmin\Template;
use PhpMyAdmin\Transformations;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
use function __;
use function count;
use function implode;
use function in_array;
use function is_array;
use function mb_strpos;
use function sprintf;
use function strlen;
final class SaveController extends AbstractController
{
/** @var Table The table object */
private $tableObj;
/** @var Relation */
private $relation;
/** @var Transformations */
private $transformations;
/** @var DatabaseInterface */
private $dbi;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
Relation $relation,
Transformations $transformations,
DatabaseInterface $dbi,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->relation = $relation;
$this->transformations = $transformations;
$this->dbi = $dbi;
$this->structureController = $structureController;
$this->tableObj = $this->dbi->getTable($this->db, $this->table);
}
public function __invoke(): void
{
$regenerate = $this->updateColumns();
if (! $regenerate) {
// continue to show the table's structure
unset($_POST['selected']);
}
($this->structureController)();
}
/**
* Update the table's structure based on $_REQUEST
*
* @return bool true if error occurred
*/
private function updateColumns(): bool
{
$err_url = Url::getFromRoute('/table/structure', [
'db' => $this->db,
'table' => $this->table,
]);
$regenerate = false;
$field_cnt = count($_POST['field_name'] ?? []);
$changes = [];
$adjust_privileges = [];
$columns_with_index = $this->dbi
->getTable($this->db, $this->table)
->getColumnsWithIndex(Index::PRIMARY | Index::UNIQUE);
for ($i = 0; $i < $field_cnt; $i++) {
if (! $this->columnNeedsAlterTable($i)) {
continue;
}
$changes[] = 'CHANGE ' . Table::generateAlter(
Util::getValueByKey($_POST, 'field_orig.' . $i, ''),
$_POST['field_name'][$i],
$_POST['field_type'][$i],
$_POST['field_length'][$i],
$_POST['field_attribute'][$i],
Util::getValueByKey($_POST, 'field_collation.' . $i, ''),
Util::getValueByKey($_POST, 'field_null.' . $i, 'NO'),
$_POST['field_default_type'][$i],
$_POST['field_default_value'][$i],
Util::getValueByKey($_POST, 'field_extra.' . $i, false),
Util::getValueByKey($_POST, 'field_comments.' . $i, ''),
Util::getValueByKey($_POST, 'field_virtuality.' . $i, ''),
Util::getValueByKey($_POST, 'field_expression.' . $i, ''),
Util::getValueByKey($_POST, 'field_move_to.' . $i, ''),
$columns_with_index
);
// find the remembered sort expression
$sorted_col = $this->tableObj->getUiProp(Table::PROP_SORTED_COLUMN);
// if the old column name is part of the remembered sort expression
if (mb_strpos((string) $sorted_col, Util::backquote($_POST['field_orig'][$i])) !== false) {
// delete the whole remembered sort expression
$this->tableObj->removeUiProp(Table::PROP_SORTED_COLUMN);
}
if (
! isset($_POST['field_adjust_privileges'][$i])
|| empty($_POST['field_adjust_privileges'][$i])
|| $_POST['field_orig'][$i] == $_POST['field_name'][$i]
) {
continue;
}
$adjust_privileges[$_POST['field_orig'][$i]] = $_POST['field_name'][$i];
}
if (count($changes) > 0 || isset($_POST['preview_sql'])) {
// Builds the primary keys statements and updates the table
$key_query = '';
/**
* this is a little bit more complex
*
* @todo if someone selects A_I when altering a column we need to check:
* - no other column with A_I
* - the column has an index, if not create one
*/
// To allow replication, we first select the db to use
// and then run queries on this db.
if (! $this->dbi->selectDb($this->db)) {
Generator::mysqlDie(
$this->dbi->getError(),
'USE ' . Util::backquote($this->db) . ';',
false,
$err_url
);
}
$sql_query = 'ALTER TABLE ' . Util::backquote($this->table) . ' ';
$sql_query .= implode(', ', $changes) . $key_query;
if (isset($_POST['online_transaction'])) {
$sql_query .= ', ALGORITHM=INPLACE, LOCK=NONE';
}
$sql_query .= ';';
// If there is a request for SQL previewing.
if (isset($_POST['preview_sql'])) {
Core::previewSQL(count($changes) > 0 ? $sql_query : '');
exit;
}
$columns_with_index = $this->dbi
->getTable($this->db, $this->table)
->getColumnsWithIndex(Index::PRIMARY | Index::UNIQUE | Index::INDEX | Index::SPATIAL | Index::FULLTEXT);
$changedToBlob = [];
// While changing the Column Collation
// First change to BLOB, MEDIUMBLOB, or LONGBLOB (depending on the original field type)
for ($i = 0; $i < $field_cnt; $i++) {
if (
isset($_POST['field_collation'][$i], $_POST['field_collation_orig'][$i])
&& $_POST['field_collation'][$i] !== $_POST['field_collation_orig'][$i]
&& ! in_array($_POST['field_orig'][$i], $columns_with_index)
) {
if ($_POST['field_type_orig'][$i] === 'MEDIUMTEXT') {
$blob_type = 'MEDIUMBLOB';
} elseif ($_POST['field_type_orig'][$i] === 'LONGTEXT') {
$blob_type = 'LONGBLOB';
} else {
$blob_type = 'BLOB';
}
$secondary_query = 'ALTER TABLE ' . Util::backquote($this->table)
. ' CHANGE ' . Util::backquote($_POST['field_orig'][$i])
. ' ' . Util::backquote($_POST['field_orig'][$i])
. ' ' . $blob_type;
if (isset($_POST['field_virtuality'][$i], $_POST['field_expression'][$i])) {
if ($_POST['field_virtuality'][$i]) {
$secondary_query .= ' AS (' . $_POST['field_expression'][$i] . ') '
. $_POST['field_virtuality'][$i];
}
}
$secondary_query .= ';';
$this->dbi->query($secondary_query);
$changedToBlob[$i] = true;
} else {
$changedToBlob[$i] = false;
}
}
// Then make the requested changes
$result = $this->dbi->tryQuery($sql_query);
if ($result !== false) {
$changed_privileges = $this->adjustColumnPrivileges($adjust_privileges);
if ($changed_privileges) {
$message = Message::success(
__(
'Table %1$s has been altered successfully. Privileges have been adjusted.'
)
);
} else {
$message = Message::success(
__('Table %1$s has been altered successfully.')
);
}
$message->addParam($this->table);
$this->response->addHTML(
Generator::getMessage($message, $sql_query, 'success')
);
} else {
// An error happened while inserting/updating a table definition
// Save the Original Error
$orig_error = $this->dbi->getError();
$changes_revert = [];
// Change back to Original Collation and data type
for ($i = 0; $i < $field_cnt; $i++) {
if (! $changedToBlob[$i]) {
continue;
}
$changes_revert[] = 'CHANGE ' . Table::generateAlter(
Util::getValueByKey($_POST, 'field_orig.' . $i, ''),
$_POST['field_name'][$i],
$_POST['field_type_orig'][$i],
$_POST['field_length_orig'][$i],
$_POST['field_attribute_orig'][$i],
Util::getValueByKey($_POST, 'field_collation_orig.' . $i, ''),
Util::getValueByKey($_POST, 'field_null_orig.' . $i, 'NO'),
$_POST['field_default_type_orig'][$i],
$_POST['field_default_value_orig'][$i],
Util::getValueByKey($_POST, 'field_extra_orig.' . $i, false),
Util::getValueByKey($_POST, 'field_comments_orig.' . $i, ''),
Util::getValueByKey($_POST, 'field_virtuality_orig.' . $i, ''),
Util::getValueByKey($_POST, 'field_expression_orig.' . $i, ''),
Util::getValueByKey($_POST, 'field_move_to_orig.' . $i, '')
);
}
$revert_query = 'ALTER TABLE ' . Util::backquote($this->table)
. ' ';
$revert_query .= implode(', ', $changes_revert) . '';
$revert_query .= ';';
// Column reverted back to original
$this->dbi->query($revert_query);
$this->response->setRequestStatus(false);
$message = Message::rawError(
__('Query error') . ':<br>' . $orig_error
);
$this->response->addHTML(
Generator::getMessage($message, $sql_query, 'error')
);
$regenerate = true;
}
}
// update field names in relation
if (isset($_POST['field_orig']) && is_array($_POST['field_orig'])) {
foreach ($_POST['field_orig'] as $fieldindex => $fieldcontent) {
if ($_POST['field_name'][$fieldindex] == $fieldcontent) {
continue;
}
$this->relation->renameField($this->db, $this->table, $fieldcontent, $_POST['field_name'][$fieldindex]);
}
}
// update mime types
if (isset($_POST['field_mimetype']) && is_array($_POST['field_mimetype']) && $GLOBALS['cfg']['BrowseMIME']) {
foreach ($_POST['field_mimetype'] as $fieldindex => $mimetype) {
if (! isset($_POST['field_name'][$fieldindex]) || strlen($_POST['field_name'][$fieldindex]) <= 0) {
continue;
}
$this->transformations->setMime(
$this->db,
$this->table,
$_POST['field_name'][$fieldindex],
$mimetype,
$_POST['field_transformation'][$fieldindex],
$_POST['field_transformation_options'][$fieldindex],
$_POST['field_input_transformation'][$fieldindex],
$_POST['field_input_transformation_options'][$fieldindex]
);
}
}
return $regenerate;
}
/**
* Verifies if some elements of a column have changed
*
* @param int $i column index in the request
*/
private function columnNeedsAlterTable($i): bool
{
// these two fields are checkboxes so might not be part of the
// request; therefore we define them to avoid notices below
if (! isset($_POST['field_null'][$i])) {
$_POST['field_null'][$i] = 'NO';
}
if (! isset($_POST['field_extra'][$i])) {
$_POST['field_extra'][$i] = '';
}
// field_name does not follow the convention (corresponds to field_orig)
if ($_POST['field_name'][$i] != $_POST['field_orig'][$i]) {
return true;
}
$fields = [
'field_attribute',
'field_collation',
'field_comments',
'field_default_value',
'field_default_type',
'field_extra',
'field_length',
'field_null',
'field_type',
'field_virtuality',
];
foreach ($fields as $field) {
if ($_POST[$field][$i] != $_POST[$field . '_orig'][$i]) {
return true;
}
}
return ! empty($_POST['field_move_to'][$i]);
}
/**
* Adjusts the Privileges for all the columns whose names have changed
*
* @param array $adjust_privileges assoc array of old col names mapped to new
* cols
*/
private function adjustColumnPrivileges(array $adjust_privileges): bool
{
$changed = false;
if (
Util::getValueByKey($GLOBALS, 'col_priv', false)
&& Util::getValueByKey($GLOBALS, 'is_reload_priv', false)
) {
$this->dbi->selectDb('mysql');
// For Column specific privileges
foreach ($adjust_privileges as $oldCol => $newCol) {
$this->dbi->query(
sprintf(
'UPDATE %s SET Column_name = "%s"
WHERE Db = "%s"
AND Table_name = "%s"
AND Column_name = "%s";',
Util::backquote('columns_priv'),
$newCol,
$this->db,
$this->table,
$oldCol
)
);
// i.e. if atleast one column privileges adjusted
$changed = true;
}
if ($changed) {
// Finally FLUSH the new privileges
$this->dbi->query('FLUSH PRIVILEGES;');
}
}
return $changed;
}
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Util;
use function __;
use function count;
final class SpatialController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
DatabaseInterface $dbi,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->dbi = $dbi;
$this->structureController = $structureController;
}
public function __invoke(): void
{
global $sql_query, $db, $table, $message;
$selected = $_POST['selected_fld'] ?? [];
if (empty($selected)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No column selected.'));
return;
}
$i = 1;
$selectedCount = count($selected);
$sql_query = 'ALTER TABLE ' . Util::backquote($table) . ' ADD SPATIAL(';
foreach ($selected as $field) {
$sql_query .= Util::backquote($field);
$sql_query .= $i++ === $selectedCount ? ');' : ', ';
}
$this->dbi->selectDb($db);
$result = $this->dbi->tryQuery($sql_query);
if (! $result) {
$message = Message::error($this->dbi->getError());
}
if (empty($message)) {
$message = Message::success();
}
($this->structureController)();
}
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\AbstractController;
use PhpMyAdmin\Controllers\Table\StructureController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Util;
use function __;
use function count;
final class UniqueController extends AbstractController
{
/** @var DatabaseInterface */
private $dbi;
/** @var StructureController */
private $structureController;
public function __construct(
ResponseRenderer $response,
Template $template,
string $db,
string $table,
DatabaseInterface $dbi,
StructureController $structureController
) {
parent::__construct($response, $template, $db, $table);
$this->dbi = $dbi;
$this->structureController = $structureController;
}
public function __invoke(): void
{
global $sql_query, $db, $table, $message;
$selected = $_POST['selected_fld'] ?? [];
if (empty($selected)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No column selected.'));
return;
}
$i = 1;
$selectedCount = count($selected);
$sql_query = 'ALTER TABLE ' . Util::backquote($table) . ' ADD UNIQUE(';
foreach ($selected as $field) {
$sql_query .= Util::backquote($field);
$sql_query .= $i++ === $selectedCount ? ');' : ', ';
}
$this->dbi->selectDb($db);
$result = $this->dbi->tryQuery($sql_query);
if (! $result) {
$message = Message::error($this->dbi->getError());
}
if (empty($message)) {
$message = Message::success();
}
($this->structureController)();
}
}