Update website

This commit is contained in:
Guilhem Lavaux 2025-02-11 21:30:02 +01:00
parent 0a686aeb9a
commit c4ffa0f6ee
4360 changed files with 1727 additions and 718385 deletions

View file

@ -1,236 +0,0 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Export;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Core;
use PhpMyAdmin\Encoding;
use PhpMyAdmin\Plugins;
use PhpMyAdmin\Plugins\ExportPlugin;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\Table;
use PhpMyAdmin\Util;
use function explode;
use function function_exists;
use function in_array;
use function is_array;
use function is_string;
use function mb_strpos;
use function strlen;
use function urldecode;
final class Options
{
/** @var Relation */
private $relation;
/** @var TemplateModel */
private $templateModel;
public function __construct(Relation $relation, TemplateModel $templateModel)
{
$this->relation = $relation;
$this->templateModel = $templateModel;
}
/**
* Outputs appropriate checked statement for checkbox.
*
* @param string $str option name
*/
private function checkboxCheck($str): bool
{
return isset($GLOBALS['cfg']['Export'][$str])
&& $GLOBALS['cfg']['Export'][$str];
}
/**
* Prints Html For Export Selection Options
*
* @param string $tmpSelect Tmp selected method of export
*
* @return array
*/
public function getDatabasesForSelectOptions($tmpSelect = '')
{
/** @var array|string|null $dbSelect */
$dbSelect = $_POST['db_select'] ?? null;
// Check if the selected databases are defined in $_POST
// (from clicking Back button on /export page)
if (isset($dbSelect) && is_string($dbSelect)) {
$dbSelect = urldecode($dbSelect);
$dbSelect = explode(',', $dbSelect);
}
$databases = [];
foreach ($GLOBALS['dblist']->databases as $currentDb) {
if (Utilities::isSystemSchema($currentDb, true)) {
continue;
}
$isSelected = false;
if (isset($dbSelect) && is_array($dbSelect)) {
if (in_array($currentDb, $dbSelect)) {
$isSelected = true;
}
} elseif (! empty($tmpSelect)) {
if (mb_strpos(' ' . $tmpSelect, '|' . $currentDb . '|')) {
$isSelected = true;
}
} else {
$isSelected = true;
}
$databases[] = [
'name' => $currentDb,
'is_selected' => $isSelected,
];
}
return $databases;
}
/**
* @param string $exportType export type: server|database|table
* @param string $db selected DB
* @param string $table selected table
* @param string $sqlQuery SQL query
* @param int|string $numTables number of tables
* @param int|string $unlimNumRows unlimited number of rows
* @param ExportPlugin[] $exportList
*
* @return array<string, mixed>
*/
public function getOptions(
$exportType,
$db,
$table,
$sqlQuery,
$numTables,
$unlimNumRows,
array $exportList
) {
global $cfg;
$exportTemplatesFeature = $this->relation->getRelationParameters()->exportTemplatesFeature;
$templates = [];
if ($exportTemplatesFeature !== null) {
$templates = $this->templateModel->getAll(
$exportTemplatesFeature->database,
$exportTemplatesFeature->exportTemplates,
$GLOBALS['cfg']['Server']['user'],
$exportType
);
$templates = is_array($templates) ? $templates : [];
}
$default = isset($_GET['what']) ? (string) $_GET['what'] : Plugins::getDefault('Export', 'format');
$dropdown = Plugins::getChoice($exportList, $default);
$tableObject = new Table($table, $db);
$rows = [];
if (strlen($table) > 0 && empty($numTables) && ! $tableObject->isMerge() && $exportType !== 'raw') {
$rows = [
'allrows' => $_POST['allrows'] ?? null,
'limit_to' => $_POST['limit_to'] ?? null,
'limit_from' => $_POST['limit_from'] ?? null,
'unlim_num_rows' => $unlimNumRows,
'number_of_rows' => $tableObject->countRecords(),
];
}
$hasAliases = isset($_SESSION['tmpval']['aliases']) && ! Core::emptyRecursive($_SESSION['tmpval']['aliases']);
$aliases = $_SESSION['tmpval']['aliases'] ?? [];
unset($_SESSION['tmpval']['aliases']);
$filenameTemplate = $this->getFileNameTemplate($exportType, $_POST['filename_template'] ?? null);
$isEncodingSupported = Encoding::isSupported();
$selectedCompression = $_POST['compression'] ?? $cfg['Export']['compression'] ?? 'none';
if (isset($cfg['Export']['as_separate_files']) && $cfg['Export']['as_separate_files']) {
$selectedCompression = 'zip';
}
$hiddenInputs = [
'db' => $db,
'table' => $table,
'export_type' => $exportType,
'export_method' => $_POST['export_method'] ?? $cfg['Export']['method'] ?? 'quick',
'template_id' => $_POST['template_id'] ?? '',
];
if (! empty($GLOBALS['single_table'])) {
$hiddenInputs['single_table'] = true;
}
if (! empty($sqlQuery)) {
$hiddenInputs['sql_query'] = $sqlQuery;
}
return [
'export_type' => $exportType,
'db' => $db,
'table' => $table,
'templates' => [
'is_enabled' => $exportTemplatesFeature !== null,
'templates' => $templates,
'selected' => $_POST['template_id'] ?? null,
],
'sql_query' => $sqlQuery,
'hidden_inputs' => $hiddenInputs,
'export_method' => $_POST['quick_or_custom'] ?? $cfg['Export']['method'] ?? '',
'plugins_choice' => $dropdown,
'options' => Plugins::getOptions('Export', $exportList),
'can_convert_kanji' => Encoding::canConvertKanji(),
'exec_time_limit' => $cfg['ExecTimeLimit'],
'rows' => $rows,
'has_save_dir' => isset($cfg['SaveDir']) && ! empty($cfg['SaveDir']),
'save_dir' => Util::userDir((string) ($cfg['SaveDir'] ?? '')),
'export_is_checked' => $this->checkboxCheck('quick_export_onserver'),
'export_overwrite_is_checked' => $this->checkboxCheck('quick_export_onserver_overwrite'),
'has_aliases' => $hasAliases,
'aliases' => $aliases,
'is_checked_lock_tables' => $this->checkboxCheck('lock_tables'),
'is_checked_asfile' => $this->checkboxCheck('asfile'),
'is_checked_as_separate_files' => $this->checkboxCheck('as_separate_files'),
'is_checked_export' => $this->checkboxCheck('onserver'),
'is_checked_export_overwrite' => $this->checkboxCheck('onserver_overwrite'),
'is_checked_remember_file_template' => $this->checkboxCheck('remember_file_template'),
'repopulate' => isset($_POST['repopulate']),
'lock_tables' => isset($_POST['lock_tables']),
'is_encoding_supported' => $isEncodingSupported,
'encodings' => $isEncodingSupported ? Encoding::listEncodings() : [],
'export_charset' => $cfg['Export']['charset'],
'export_asfile' => $cfg['Export']['asfile'],
'has_zip' => $cfg['ZipDump'] && function_exists('gzcompress'),
'has_gzip' => $cfg['GZipDump'] && function_exists('gzencode'),
'selected_compression' => $selectedCompression,
'filename_template' => $filenameTemplate,
];
}
private function getFileNameTemplate(string $exportType, ?string $filename = null): string
{
global $cfg, $config;
if ($filename !== null) {
return $filename;
}
if ($exportType === 'database') {
return (string) $config->getUserValue('pma_db_filename_template', $cfg['Export']['file_template_database']);
}
if ($exportType === 'table') {
return (string) $config->getUserValue('pma_table_filename_template', $cfg['Export']['file_template_table']);
}
return (string) $config->getUserValue('pma_server_filename_template', $cfg['Export']['file_template_server']);
}
}

View file

@ -1,70 +0,0 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Export;
/** @psalm-immutable */
final class Template
{
/** @var int */
private $id;
/** @var string */
private $username;
/** @var string */
private $exportType;
/** @var string */
private $name;
/** @var string JSON */
private $data;
private function __construct(int $id, string $username, string $exportType, string $name, string $data)
{
$this->id = $id;
$this->username = $username;
$this->exportType = $exportType;
$this->name = $name;
$this->data = $data;
}
/** @param array<string, mixed> $state */
public static function fromArray(array $state): self
{
return new self(
$state['id'] ?? 0,
$state['username'],
$state['exportType'] ?? '',
$state['name'] ?? '',
$state['data']
);
}
public function getId(): int
{
return $this->id;
}
public function getUsername(): string
{
return $this->username;
}
public function getExportType(): string
{
return $this->exportType;
}
public function getName(): string
{
return $this->name;
}
public function getData(): string
{
return $this->data;
}
}

View file

@ -1,140 +0,0 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Export;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Dbal\DatabaseName;
use PhpMyAdmin\Dbal\TableName;
use PhpMyAdmin\Util;
use function sprintf;
final class TemplateModel
{
/** @var DatabaseInterface */
private $dbi;
public function __construct(DatabaseInterface $dbi)
{
$this->dbi = $dbi;
}
public function create(DatabaseName $db, TableName $table, Template $template): string
{
$query = sprintf(
'INSERT INTO %s.%s (`username`, `export_type`, `template_name`, `template_data`)'
. ' VALUES (\'%s\', \'%s\', \'%s\', \'%s\');',
Util::backquote($db),
Util::backquote($table),
$this->dbi->escapeString($template->getUsername()),
$this->dbi->escapeString($template->getExportType()),
$this->dbi->escapeString($template->getName()),
$this->dbi->escapeString($template->getData())
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result !== false) {
return '';
}
return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
}
public function delete(DatabaseName $db, TableName $table, string $user, int $id): string
{
$query = sprintf(
'DELETE FROM %s.%s WHERE `id` = %s AND `username` = \'%s\';',
Util::backquote($db),
Util::backquote($table),
$id,
$this->dbi->escapeString($user)
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result !== false) {
return '';
}
return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
}
/**
* @return Template|string
*/
public function load(DatabaseName $db, TableName $table, string $user, int $id)
{
$query = sprintf(
'SELECT * FROM %s.%s WHERE `id` = %s AND `username` = \'%s\';',
Util::backquote($db),
Util::backquote($table),
$id,
$this->dbi->escapeString($user)
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result === false) {
return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
}
$data = [];
while ($row = $result->fetchAssoc()) {
$data = $row;
}
return Template::fromArray([
'id' => (int) $data['id'],
'username' => $data['username'],
'exportType' => $data['export_type'],
'name' => $data['template_name'],
'data' => $data['template_data'],
]);
}
public function update(DatabaseName $db, TableName $table, Template $template): string
{
$query = sprintf(
'UPDATE %s.%s SET `template_data` = \'%s\' WHERE `id` = %s AND `username` = \'%s\';',
Util::backquote($db),
Util::backquote($table),
$this->dbi->escapeString($template->getData()),
$template->getId(),
$this->dbi->escapeString($template->getUsername())
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result !== false) {
return '';
}
return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
}
/**
* @return Template[]|string
*/
public function getAll(DatabaseName $db, TableName $table, string $user, string $exportType)
{
$query = sprintf(
'SELECT * FROM %s.%s WHERE `username` = \'%s\' AND `export_type` = \'%s\' ORDER BY `template_name`;',
Util::backquote($db),
Util::backquote($table),
$this->dbi->escapeString($user),
$this->dbi->escapeString($exportType)
);
$result = $this->dbi->tryQueryAsControlUser($query);
if ($result === false) {
return $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
}
$templates = [];
while ($row = $result->fetchAssoc()) {
$templates[] = Template::fromArray([
'id' => (int) $row['id'],
'username' => $row['username'],
'exportType' => $row['export_type'],
'name' => $row['template_name'],
'data' => $row['template_data'],
]);
}
return $templates;
}
}