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,109 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Import;
use PhpMyAdmin\Core;
use function function_exists;
use function ini_get;
use function json_encode;
use function ucwords;
use function uniqid;
/**
* Handles plugins that show the upload progress.
*/
final class Ajax
{
/**
* Sets up some variables for upload progress
*/
public static function uploadProgressSetup(): array
{
/**
* constant for differentiating array in $_SESSION variable
*/
$SESSION_KEY = '__upload_status';
/**
* sets default plugin for handling the import process
*/
$_SESSION[$SESSION_KEY]['handler'] = '';
/**
* unique ID for each upload
*/
$upload_id = uniqid('');
/**
* list of available plugins
*/
$plugins = [
// in PHP 5.4 session-based upload progress was problematic, see closed bug 3964
//"session",
'progress',
'noplugin',
];
// select available plugin
foreach ($plugins as $plugin) {
$check = $plugin . 'Check';
if (self::$check()) {
$upload_class = 'PhpMyAdmin\Plugins\Import\Upload\Upload' . ucwords($plugin);
$_SESSION[$SESSION_KEY]['handler'] = $upload_class;
break;
}
}
return [
$SESSION_KEY,
$upload_id,
$plugins,
];
}
/**
* Checks if PhpMyAdmin\Plugins\Import\Upload\UploadProgress bar extension is
* available.
*/
public static function progressCheck(): bool
{
return function_exists('uploadprogress_get_info');
}
/**
* Checks if PHP 5.4 session upload-progress feature is available.
*/
public static function sessionCheck(): bool
{
return ini_get('session.upload_progress.enabled') === '1';
}
/**
* Default plugin for handling import.
* If no other plugin is available, noplugin is used.
*
* @return true
*/
public static function nopluginCheck(): bool
{
return true;
}
/**
* The function outputs json encoded status of uploaded.
* It uses PMA_getUploadStatus, which is defined in plugin's file.
*
* @param string $id ID of transfer, usually $upload_id
*/
public static function status($id): void
{
Core::headerJSON();
echo json_encode(
$_SESSION[$GLOBALS['SESSION_KEY']]['handler']::getUploadStatus($id)
);
}
}

View File

@@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Import;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Html;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statement;
use PhpMyAdmin\SqlParser\Statements\DeleteStatement;
use PhpMyAdmin\SqlParser\Statements\UpdateStatement;
use PhpMyAdmin\SqlParser\Utils\Query;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
use Webmozart\Assert\Assert;
use function array_key_exists;
use function array_reverse;
use function implode;
final class SimulateDml
{
/** @var DatabaseInterface */
private $dbi;
public function __construct(DatabaseInterface $dbi)
{
$this->dbi = $dbi;
}
public function getError(): string
{
return $this->dbi->getError();
}
/**
* Find the matching rows for UPDATE/DELETE query.
*
* @param DeleteStatement|UpdateStatement $statement
*
* @return array<string, int|string>|null
* @psalm-return array{
* sql_query: string,
* matched_rows: int,
* matched_rows_url: string
* }
*/
public function getMatchedRows(Parser $parser, Statement $statement): array
{
if ($statement instanceof DeleteStatement) {
$matchedRowsQuery = $this->getSimulatedDeleteQuery($parser, $statement);
} else {
$matchedRowsQuery = $this->getSimulatedUpdateQuery($parser, $statement);
}
// Execute the query and get the number of matched rows.
$matchedRows = $this->executeMatchedRowQuery($matchedRowsQuery);
$matchedRowsUrl = Url::getFromRoute('/sql', [
'db' => $GLOBALS['db'],
'sql_query' => $matchedRowsQuery,
'sql_signature' => Core::signSqlQuery($matchedRowsQuery),
]);
return [
'sql_query' => Html\Generator::formatSql($statement->build()),
'matched_rows' => $matchedRows,
'matched_rows_url' => $matchedRowsUrl,
];
}
/**
* Executes the matched_row_query and returns the resultant row count.
*
* @param string $matchedRowQuery SQL query
*/
private function executeMatchedRowQuery(string $matchedRowQuery): int
{
$this->dbi->selectDb($GLOBALS['db']);
$result = $this->dbi->tryQuery($matchedRowQuery);
if (! $result) {
return 0;
}
return (int) $result->numRows();
}
/**
* Transforms a DELETE query into SELECT statement.
*
* @return string SQL query
*/
private function getSimulatedDeleteQuery(Parser $parser, DeleteStatement $statement): string
{
$tableReferences = Query::getTables($statement);
Assert::count($tableReferences, 1, 'No joins allowed in simulation query');
Assert::notNull($parser->list, 'Parser list not set');
$condition = Query::getClause($statement, $parser->list, 'WHERE');
$where = $condition === '' ? '' : ' WHERE ' . $condition;
$order = $statement->order === null || $statement->order === []
? ''
: ' ORDER BY ' . Query::getClause($statement, $parser->list, 'ORDER BY');
$limit = $statement->limit === null ? '' : ' LIMIT ' . Query::getClause($statement, $parser->list, 'LIMIT');
return 'SELECT * FROM ' . $tableReferences[0] . $where . $order . $limit;
}
/**
* Transforms a UPDATE query into SELECT statement.
*
* @return string SQL query
*/
private function getSimulatedUpdateQuery(Parser $parser, UpdateStatement $statement): string
{
$tableReferences = Query::getTables($statement);
Assert::count($tableReferences, 1, 'No joins allowed in simulation query');
Assert::isNonEmptyList($statement->set, 'SET statements missing');
Assert::notNull($parser->list, 'Parser list not set');
$values = [];
$newColumns = [];
$oldColumns = [];
foreach (array_reverse($statement->set) as $set) {
$column = Util::unQuote($set->column);
if (array_key_exists($column, $values)) {
continue;
}
$oldColumns[] = Util::backquote($column);
$values[$column] = $set->value . ' AS ' . ($newColumns[] = Util::backquote($column . ' `new`'));
}
$condition = Query::getClause($statement, $parser->list, 'WHERE');
$where = $condition === '' ? '' : ' WHERE ' . $condition;
$order = $statement->order === null || $statement->order === []
? ''
: ' ORDER BY ' . Query::getClause($statement, $parser->list, 'ORDER BY');
$limit = $statement->limit === null ? '' : ' LIMIT ' . Query::getClause($statement, $parser->list, 'LIMIT');
return 'SELECT *' .
' FROM (' .
'SELECT *, ' . implode(', ', $values) . ' FROM ' . $tableReferences[0] . $where . $order . $limit .
') AS `pma_tmp`' .
' WHERE NOT (' . implode(', ', $oldColumns) . ') <=> (' . implode(', ', $newColumns) . ')';
}
}