Update website

This commit is contained in:
Guilhem Lavaux 2024-11-23 20:45:29 +01:00
parent 41ce1aa076
commit ea0eb1c6e0
4222 changed files with 721797 additions and 14 deletions

View file

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
use Stringable;
use Webmozart\Assert\Assert;
use Webmozart\Assert\InvalidArgumentException;
/**
* @psalm-immutable
*/
final class DatabaseName implements Stringable
{
/**
* @see https://dev.mysql.com/doc/refman/en/identifier-length.html
* @see https://mariadb.com/kb/en/identifier-names/#maximum-length
*/
private const MAX_LENGTH = 64;
/**
* @var string
* @psalm-var non-empty-string
*/
private $name;
/**
* @param mixed $name
*
* @throws InvalidArgumentException
*/
private function __construct($name)
{
Assert::stringNotEmpty($name);
Assert::maxLength($name, self::MAX_LENGTH);
Assert::notEndsWith($name, ' ');
$this->name = $name;
}
/**
* @param mixed $name
*
* @throws InvalidArgumentException
*/
public static function fromValue($name): self
{
return new self($name);
}
/**
* @psalm-return non-empty-string
*/
public function getName(): string
{
return $this->name;
}
/**
* @psalm-return non-empty-string
*/
public function __toString(): string
{
return $this->name;
}
}

View file

@ -0,0 +1,721 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\FieldMetadata;
use PhpMyAdmin\SystemDatabase;
use PhpMyAdmin\Table;
/**
* Main interface for database interactions
*/
interface DbalInterface
{
public const FETCH_NUM = 'NUM';
public const FETCH_ASSOC = 'ASSOC';
/**
* runs a query
*
* @param string $query SQL query to execute
* @param mixed $link optional database link to use
* @param int $options optional query options
* @param bool $cache_affected_rows whether to cache affected rows
*/
public function query(
string $query,
$link = DatabaseInterface::CONNECT_USER,
int $options = 0,
bool $cache_affected_rows = true
): ResultInterface;
/**
* runs a query and returns the result
*
* @param string $query query to run
* @param mixed $link link type
* @param int $options query options
* @param bool $cache_affected_rows whether to cache affected row
*
* @return mixed
*/
public function tryQuery(
string $query,
$link = DatabaseInterface::CONNECT_USER,
int $options = 0,
bool $cache_affected_rows = true
);
/**
* Send multiple SQL queries to the database server and execute the first one
*
* @param string $multiQuery multi query statement to execute
* @param int $linkIndex index of the opened database link
*/
public function tryMultiQuery(
string $multiQuery = '',
$linkIndex = DatabaseInterface::CONNECT_USER
): bool;
/**
* returns array with table names for given db
*
* @param string $database name of database
* @param mixed $link mysql link resource|object
*
* @return array tables names
*/
public function getTables(string $database, $link = DatabaseInterface::CONNECT_USER): array;
/**
* returns array of all tables in given db or dbs
* this function expects unquoted names:
* RIGHT: my_database
* WRONG: `my_database`
* WRONG: my\_database
* if $tbl_is_group is true, $table is used as filter for table names
*
* <code>
* $dbi->getTablesFull('my_database');
* $dbi->getTablesFull('my_database', 'my_table'));
* $dbi->getTablesFull('my_database', 'my_tables_', true));
* </code>
*
* @param string $database database
* @param string|array $table table name(s)
* @param bool $tbl_is_group $table is a table group
* @param int $limit_offset zero-based offset for the count
* @param bool|int $limit_count number of tables to return
* @param string $sort_by table attribute to sort by
* @param string $sort_order direction to sort (ASC or DESC)
* @param string|null $table_type whether table or view
* @param mixed $link link type
*
* @return array list of tables in given db(s)
*
* @todo move into Table
*/
public function getTablesFull(
string $database,
$table = '',
bool $tbl_is_group = false,
int $limit_offset = 0,
$limit_count = false,
string $sort_by = 'Name',
string $sort_order = 'ASC',
?string $table_type = null,
$link = DatabaseInterface::CONNECT_USER
): array;
/**
* Get VIEWs in a particular database
*
* @param string $db Database name to look in
*
* @return Table[] Set of VIEWs inside the database
*/
public function getVirtualTables(string $db): array;
/**
* returns array with databases containing extended infos about them
*
* @param string|null $database database
* @param bool $force_stats retrieve stats also for MySQL < 5
* @param int $link link type
* @param string $sort_by column to order by
* @param string $sort_order ASC or DESC
* @param int $limit_offset starting offset for LIMIT
* @param bool|int $limit_count row count for LIMIT or true
* for $GLOBALS['cfg']['MaxDbList']
*
* @return array
*
* @todo move into ListDatabase?
*/
public function getDatabasesFull(
?string $database = null,
bool $force_stats = false,
$link = DatabaseInterface::CONNECT_USER,
string $sort_by = 'SCHEMA_NAME',
string $sort_order = 'ASC',
int $limit_offset = 0,
$limit_count = false
): array;
/**
* returns detailed array with all columns for sql
*
* @param string $sql_query target SQL query to get columns
* @param array $view_columns alias for columns
*
* @return array
*/
public function getColumnMapFromSql(string $sql_query, array $view_columns = []): array;
/**
* returns detailed array with all columns for given table in database,
* or all tables/databases
*
* @param string|null $database name of database
* @param string|null $table name of table to retrieve columns from
* @param string|null $column name of specific column
* @param mixed $link mysql link resource
*
* @return array
*/
public function getColumnsFull(
?string $database = null,
?string $table = null,
?string $column = null,
$link = DatabaseInterface::CONNECT_USER
): array;
/**
* Returns description of a $column in given table
*
* @param string $database name of database
* @param string $table name of table to retrieve columns from
* @param string $column name of column
* @param bool $full whether to return full info or only column names
* @param int $link link type
*
* @return array flat array description
*/
public function getColumn(
string $database,
string $table,
string $column,
bool $full = false,
$link = DatabaseInterface::CONNECT_USER
): array;
/**
* Returns descriptions of columns in given table
*
* @param string $database name of database
* @param string $table name of table to retrieve columns from
* @param bool $full whether to return full info or only column names
* @param int $link link type
*
* @return array<string, array> array indexed by column names
*/
public function getColumns(
string $database,
string $table,
bool $full = false,
$link = DatabaseInterface::CONNECT_USER
): array;
/**
* Returns all column names in given table
*
* @param string $database name of database
* @param string $table name of table to retrieve columns from
* @param mixed $link mysql link resource
*
* @return string[]
*/
public function getColumnNames(
string $database,
string $table,
$link = DatabaseInterface::CONNECT_USER
): array;
/**
* Returns indexes of a table
*
* @param string $database name of database
* @param string $table name of the table whose indexes are to be retrieved
* @param mixed $link mysql link resource
*
* @return array
*/
public function getTableIndexes(
string $database,
string $table,
$link = DatabaseInterface::CONNECT_USER
): array;
/**
* returns value of given mysql server variable
*
* @param string $var mysql server variable name
* @param int $type DatabaseInterface::GETVAR_SESSION |
* DatabaseInterface::GETVAR_GLOBAL
* @param int $link mysql link resource|object
*
* @return false|string|null value for mysql server variable
*/
public function getVariable(
string $var,
int $type = DatabaseInterface::GETVAR_SESSION,
$link = DatabaseInterface::CONNECT_USER
);
/**
* Sets new value for a variable if it is different from the current value
*
* @param string $var variable name
* @param string $value value to set
* @param int $link mysql link resource|object
*/
public function setVariable(string $var, string $value, $link = DatabaseInterface::CONNECT_USER): bool;
/**
* Function called just after a connection to the MySQL database server has
* been established. It sets the connection collation, and determines the
* version of MySQL which is running.
*/
public function postConnect(): void;
/**
* Sets collation connection for user link
*
* @param string $collation collation to set
*/
public function setCollation(string $collation): void;
/**
* Function called just after a connection to the MySQL database server has
* been established. It sets the connection collation, and determines the
* version of MySQL which is running.
*/
public function postConnectControl(Relation $relation): void;
/**
* returns a single value from the given result or query,
* if the query or the result has more than one row or field
* the first field of the first row is returned
*
* <code>
* $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
* $user_name = $dbi->fetchValue($sql);
* // produces
* // $user_name = 'John Doe'
* </code>
*
* @param string $query The query to execute
* @param int|string $field field to fetch the value from,
* starting at 0, with 0 being
* default
* @param int $link link type
*
* @return string|false|null value of first field in first row from result
* or false if not found
*/
public function fetchValue(
string $query,
$field = 0,
$link = DatabaseInterface::CONNECT_USER
);
/**
* Returns only the first row from the result or null if result is empty.
*
* <code>
* $sql = 'SELECT * FROM `user` WHERE `id` = 123';
* $user = $dbi->fetchSingleRow($sql);
* // produces
* // $user = array('id' => 123, 'name' => 'John Doe')
* </code>
*
* @param string $query The query to execute
* @param string $type NUM|ASSOC returned array should either numeric
* associative or both
* @param int $link link type
* @psalm-param self::FETCH_NUM|self::FETCH_ASSOC $type
*/
public function fetchSingleRow(
string $query,
string $type = DbalInterface::FETCH_ASSOC,
$link = DatabaseInterface::CONNECT_USER
): ?array;
/**
* returns all rows in the resultset in one array
*
* <code>
* $sql = 'SELECT * FROM `user`';
* $users = $dbi->fetchResult($sql);
* // produces
* // $users[] = array('id' => 123, 'name' => 'John Doe')
*
* $sql = 'SELECT `id`, `name` FROM `user`';
* $users = $dbi->fetchResult($sql, 'id');
* // produces
* // $users['123'] = array('id' => 123, 'name' => 'John Doe')
*
* $sql = 'SELECT `id`, `name` FROM `user`';
* $users = $dbi->fetchResult($sql, 0);
* // produces
* // $users['123'] = array(0 => 123, 1 => 'John Doe')
*
* $sql = 'SELECT `id`, `name` FROM `user`';
* $users = $dbi->fetchResult($sql, 'id', 'name');
* // or
* $users = $dbi->fetchResult($sql, 0, 1);
* // produces
* // $users['123'] = 'John Doe'
*
* $sql = 'SELECT `name` FROM `user`';
* $users = $dbi->fetchResult($sql);
* // produces
* // $users[] = 'John Doe'
*
* $sql = 'SELECT `group`, `name` FROM `user`'
* $users = $dbi->fetchResult($sql, array('group', null), 'name');
* // produces
* // $users['admin'][] = 'John Doe'
*
* $sql = 'SELECT `group`, `name` FROM `user`'
* $users = $dbi->fetchResult($sql, array('group', 'name'), 'id');
* // produces
* // $users['admin']['John Doe'] = '123'
* </code>
*
* @param string $query query to execute
* @param string|int|array $key field-name or offset
* used as key for
* array or array of
* those
* @param string|int $value value-name or offset
* used as value for
* array
* @param int $link link type
*
* @return array resultrows or values indexed by $key
*/
public function fetchResult(
string $query,
$key = null,
$value = null,
$link = DatabaseInterface::CONNECT_USER
): array;
/**
* Get supported SQL compatibility modes
*
* @return array supported SQL compatibility modes
*/
public function getCompatibilities(): array;
/**
* returns warnings for last query
*
* @param int $link link type
*
* @return array warnings
*/
public function getWarnings($link = DatabaseInterface::CONNECT_USER): array;
/**
* returns an array of PROCEDURE or FUNCTION names for a db
*
* @param string $db db name
* @param string $which PROCEDURE | FUNCTION
* @param int $link link type
*
* @return array the procedure names or function names
*/
public function getProceduresOrFunctions(
string $db,
string $which,
$link = DatabaseInterface::CONNECT_USER
): array;
/**
* returns the definition of a specific PROCEDURE, FUNCTION, EVENT or VIEW
*
* @param string $db db name
* @param string $which PROCEDURE | FUNCTION | EVENT | VIEW
* @param string $name the procedure|function|event|view name
* @param int $link link type
*
* @return string|null the definition
*/
public function getDefinition(
string $db,
string $which,
string $name,
$link = DatabaseInterface::CONNECT_USER
): ?string;
/**
* returns details about the PROCEDUREs or FUNCTIONs for a specific database
* or details about a specific routine
*
* @param string $db db name
* @param string|null $which PROCEDURE | FUNCTION or null for both
* @param string $name name of the routine (to fetch a specific routine)
*
* @return array information about ROCEDUREs or FUNCTIONs
*/
public function getRoutines(string $db, ?string $which = null, string $name = ''): array;
/**
* returns details about the EVENTs for a specific database
*
* @param string $db db name
* @param string $name event name
*
* @return array information about EVENTs
*/
public function getEvents(string $db, string $name = ''): array;
/**
* returns details about the TRIGGERs for a specific table or database
*
* @param string $db db name
* @param string $table table name
* @param string $delimiter the delimiter to use (may be empty)
*
* @return array information about triggers (may be empty)
*/
public function getTriggers(string $db, string $table = '', string $delimiter = '//'): array;
/**
* gets the current user with host
*
* @return string the current user i.e. user@host
*/
public function getCurrentUser(): string;
/**
* Checks if current user is superuser
*/
public function isSuperUser(): bool;
public function isGrantUser(): bool;
public function isCreateUser(): bool;
public function isConnected(): bool;
/**
* Get the current user and host
*
* @return array array of username and hostname
*/
public function getCurrentUserAndHost(): array;
/**
* Returns value for lower_case_table_names variable
*
* @return string
*/
public function getLowerCaseNames();
/**
* connects to the database server
*
* @param int $mode Connection mode on of CONNECT_USER, CONNECT_CONTROL
* or CONNECT_AUXILIARY.
* @param array|null $server Server information like host/port/socket/persistent
* @param int|null $target How to store connection link, defaults to $mode
*
* @return mixed false on error or a connection object on success
*/
public function connect(int $mode, ?array $server = null, ?int $target = null);
/**
* selects given database
*
* @param string|DatabaseName $dbname database name to select
* @param int $link link type
*/
public function selectDb($dbname, $link = DatabaseInterface::CONNECT_USER): bool;
/**
* Check if there are any more query results from a multi query
*
* @param int $link link type
*/
public function moreResults($link = DatabaseInterface::CONNECT_USER): bool;
/**
* Prepare next result from multi_query
*
* @param int $link link type
*/
public function nextResult($link = DatabaseInterface::CONNECT_USER): bool;
/**
* Store the result returned from multi query
*
* @param int $link link type
*
* @return mixed false when empty results / result set when not empty
*/
public function storeResult($link = DatabaseInterface::CONNECT_USER);
/**
* Returns a string representing the type of connection used
*
* @param int $link link type
*
* @return string|bool type of connection used
*/
public function getHostInfo($link = DatabaseInterface::CONNECT_USER);
/**
* Returns the version of the MySQL protocol used
*
* @param int $link link type
*
* @return int|bool version of the MySQL protocol used
*/
public function getProtoInfo($link = DatabaseInterface::CONNECT_USER);
/**
* returns a string that represents the client library version
*
* @return string MySQL client library version
*/
public function getClientInfo(): string;
/**
* Returns last error message or an empty string if no errors occurred.
*
* @param int $link link type
*/
public function getError($link = DatabaseInterface::CONNECT_USER): string;
/**
* returns the number of rows returned by last query
* used with tryQuery as it accepts false
*
* @param string $query query to run
*
* @return string|int
* @psalm-return int|numeric-string
*/
public function queryAndGetNumRows(string $query);
/**
* returns last inserted auto_increment id for given $link
* or $GLOBALS['userlink']
*
* @param int $link link type
*
* @return int
*/
public function insertId($link = DatabaseInterface::CONNECT_USER);
/**
* returns the number of rows affected by last query
*
* @param int $link link type
* @param bool $get_from_cache whether to retrieve from cache
*
* @return int|string
* @psalm-return int|numeric-string
*/
public function affectedRows($link = DatabaseInterface::CONNECT_USER, bool $get_from_cache = true);
/**
* returns metainfo for fields in $result
*
* @param ResultInterface $result result set identifier
*
* @return FieldMetadata[] meta info for fields in $result
*/
public function getFieldsMeta(ResultInterface $result): array;
/**
* returns properly escaped string for use in MySQL queries
*
* @param string $str string to be escaped
* @param mixed $link optional database link to use
*
* @return string a MySQL escaped string
*/
public function escapeString(string $str, $link = DatabaseInterface::CONNECT_USER);
/**
* returns properly escaped string for use in MySQL LIKE clauses
*
* @param string $str string to be escaped
* @param int $link optional database link to use
*
* @return string a MySQL escaped LIKE string
*/
public function escapeMysqlLikeString(string $str, int $link = DatabaseInterface::CONNECT_USER);
/**
* Checks if this database server is running on Amazon RDS.
*/
public function isAmazonRds(): bool;
/**
* Gets SQL for killing a process.
*
* @param int $process Process ID
*/
public function getKillQuery(int $process): string;
/**
* Get the phpmyadmin database manager
*/
public function getSystemDatabase(): SystemDatabase;
/**
* Get a table with database name and table name
*
* @param string $db_name DB name
* @param string $table_name Table name
*/
public function getTable(string $db_name, string $table_name): Table;
/**
* returns collation of given db
*
* @param string $db name of db
*
* @return string collation of $db
*/
public function getDbCollation(string $db): string;
/**
* returns default server collation from show variables
*/
public function getServerCollation(): string;
/**
* Server version as number
*/
public function getVersion(): int;
/**
* Server version
*/
public function getVersionString(): string;
/**
* Server version comment
*/
public function getVersionComment(): string;
/**
* Whether connection is MariaDB
*/
public function isMariaDB(): bool;
/**
* Whether connection is Percona
*/
public function isPercona(): bool;
/**
* Prepare an SQL statement for execution.
*
* @param string $query The query, as a string.
* @param int $link Link type.
*
* @return object|false A statement object or false.
*/
public function prepare(string $query, $link = DatabaseInterface::CONNECT_USER);
}

View file

@ -0,0 +1,143 @@
<?php
/**
* Contract for every database extension supported by phpMyAdmin
*/
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
/**
* Contract for every database extension supported by phpMyAdmin
*/
interface DbiExtension
{
/**
* connects to the database server
*
* @param string $user user name
* @param string $password user password
* @param array $server host/port/socket/persistent
*
* @return mixed false on error or a connection object on success
*/
public function connect(
$user,
$password,
array $server
);
/**
* selects given database
*
* @param string|DatabaseName $databaseName database name to select
* @param object $link connection object
*/
public function selectDb($databaseName, $link): bool;
/**
* runs a query and returns the result
*
* @param string $query query to execute
* @param object $link connection object
* @param int $options query options
*
* @return ResultInterface|false result
*/
public function realQuery(string $query, $link, int $options);
/**
* Run the multi query and output the results
*
* @param object $link connection object
* @param string $query multi query statement to execute
*
* @return bool
*/
public function realMultiQuery($link, $query);
/**
* Check if there are any more query results from a multi query
*
* @param object $link the connection object
*/
public function moreResults($link): bool;
/**
* Prepare next result from multi_query
*
* @param object $link the connection object
*/
public function nextResult($link): bool;
/**
* Store the result returned from multi query
*
* @param object $link mysql link
*
* @return ResultInterface|false false when empty results / result set when not empty
*/
public function storeResult($link);
/**
* Returns a string representing the type of connection used
*
* @param object $link mysql link
*
* @return string type of connection used
*/
public function getHostInfo($link);
/**
* Returns the version of the MySQL protocol used
*
* @param object $link mysql link
*
* @return int|string version of the MySQL protocol used
*/
public function getProtoInfo($link);
/**
* returns a string that represents the client library version
*
* @return string MySQL client library version
*/
public function getClientInfo();
/**
* Returns last error message or an empty string if no errors occurred.
*
* @param object $link connection link
*/
public function getError($link): string;
/**
* returns the number of rows affected by last query
*
* @param object $link the connection object
*
* @return int|string
* @psalm-return int|numeric-string
*/
public function affectedRows($link);
/**
* returns properly escaped string for use in MySQL queries
*
* @param mixed $link database link
* @param string $string string to be escaped
*
* @return string a MySQL escaped string
*/
public function escapeString($link, $string);
/**
* Prepare an SQL statement for execution.
*
* @param mixed $link database link
* @param string $query The query, as a string.
*
* @return object|false A statement object or false.
*/
public function prepare($link, string $query);
}

View file

@ -0,0 +1,365 @@
<?php
/**
* Interface to the MySQL Improved extension (MySQLi)
*/
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
use mysqli;
use mysqli_stmt;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Query\Utilities;
use function __;
use function defined;
use function mysqli_connect_errno;
use function mysqli_connect_error;
use function mysqli_get_client_info;
use function mysqli_init;
use function mysqli_report;
use function sprintf;
use function stripos;
use function trigger_error;
use const E_USER_ERROR;
use const E_USER_WARNING;
use const MYSQLI_CLIENT_COMPRESS;
use const MYSQLI_CLIENT_SSL;
use const MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
use const MYSQLI_OPT_LOCAL_INFILE;
use const MYSQLI_OPT_SSL_VERIFY_SERVER_CERT;
use const MYSQLI_REPORT_OFF;
use const MYSQLI_STORE_RESULT;
use const MYSQLI_USE_RESULT;
/**
* Interface to the MySQL Improved extension (MySQLi)
*/
class DbiMysqli implements DbiExtension
{
/**
* connects to the database server
*
* @param string $user mysql user name
* @param string $password mysql user password
* @param array $server host/port/socket/persistent
*
* @return mysqli|bool false on error or a mysqli object on success
*/
public function connect($user, $password, array $server)
{
if ($server) {
$server['host'] = empty($server['host'])
? 'localhost'
: $server['host'];
}
mysqli_report(MYSQLI_REPORT_OFF);
$mysqli = mysqli_init();
if ($mysqli === false) {
return false;
}
$client_flags = 0;
/* Optionally compress connection */
if ($server['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
$client_flags |= MYSQLI_CLIENT_COMPRESS;
}
/* Optionally enable SSL */
if ($server['ssl']) {
$client_flags |= MYSQLI_CLIENT_SSL;
if (
! empty($server['ssl_key']) ||
! empty($server['ssl_cert']) ||
! empty($server['ssl_ca']) ||
! empty($server['ssl_ca_path']) ||
! empty($server['ssl_ciphers'])
) {
$mysqli->ssl_set(
$server['ssl_key'] ?? '',
$server['ssl_cert'] ?? '',
$server['ssl_ca'] ?? '',
$server['ssl_ca_path'] ?? '',
$server['ssl_ciphers'] ?? ''
);
}
/*
* disables SSL certificate validation on mysqlnd for MySQL 5.6 or later
* @link https://bugs.php.net/bug.php?id=68344
* @link https://github.com/phpmyadmin/phpmyadmin/pull/11838
*/
if (! $server['ssl_verify']) {
$mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, (int) $server['ssl_verify']);
$client_flags |= MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
}
}
if ($GLOBALS['cfg']['PersistentConnections']) {
$host = 'p:' . $server['host'];
} else {
$host = $server['host'];
}
if ($server['hide_connection_errors']) {
$return_value = @$mysqli->real_connect(
$host,
$user,
$password,
'',
$server['port'],
(string) $server['socket'],
$client_flags
);
} else {
$return_value = $mysqli->real_connect(
$host,
$user,
$password,
'',
$server['port'],
(string) $server['socket'],
$client_flags
);
}
if ($return_value === false) {
/*
* Switch to SSL if server asked us to do so, unfortunately
* there are more ways MySQL server can tell this:
*
* - MySQL 8.0 and newer should return error 3159
* - #2001 - SSL Connection is required. Please specify SSL options and retry.
* - #9002 - SSL connection is required. Please specify SSL options and retry.
*/
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$error_number = $mysqli->connect_errno;
$error_message = $mysqli->connect_error;
// phpcs:enable
if (
! $server['ssl']
&& ($error_number == 3159
|| (($error_number == 2001 || $error_number == 9002)
&& stripos($error_message, 'SSL Connection is required') !== false))
) {
trigger_error(
__('SSL connection enforced by server, automatically enabling it.'),
E_USER_WARNING
);
$server['ssl'] = true;
return self::connect($user, $password, $server);
}
if ($error_number === 1045 && $server['hide_connection_errors']) {
trigger_error(
sprintf(
__(
'Error 1045: Access denied for user. Additional error information'
. ' may be available, but is being hidden by the %s configuration directive.'
),
'[code][doc@cfg_Servers_hide_connection_errors]'
. '$cfg[\'Servers\'][$i][\'hide_connection_errors\'][/doc][/code]'
),
E_USER_ERROR
);
}
return false;
}
$mysqli->options(MYSQLI_OPT_LOCAL_INFILE, (int) defined('PMA_ENABLE_LDI'));
return $mysqli;
}
/**
* selects given database
*
* @param string|DatabaseName $databaseName database name to select
* @param mysqli $link the mysqli object
*/
public function selectDb($databaseName, $link): bool
{
return $link->select_db((string) $databaseName);
}
/**
* runs a query and returns the result
*
* @param string $query query to execute
* @param mysqli $link mysqli object
* @param int $options query options
*
* @return MysqliResult|false
*/
public function realQuery(string $query, $link, int $options)
{
$method = MYSQLI_STORE_RESULT;
if ($options == ($options | DatabaseInterface::QUERY_UNBUFFERED)) {
$method = MYSQLI_USE_RESULT;
}
$result = $link->query($query, $method);
if ($result === false) {
return false;
}
return new MysqliResult($result);
}
/**
* Run the multi query and output the results
*
* @param mysqli $link mysqli object
* @param string $query multi query statement to execute
*/
public function realMultiQuery($link, $query): bool
{
return $link->multi_query($query);
}
/**
* Check if there are any more query results from a multi query
*
* @param mysqli $link the mysqli object
*/
public function moreResults($link): bool
{
return $link->more_results();
}
/**
* Prepare next result from multi_query
*
* @param mysqli $link the mysqli object
*/
public function nextResult($link): bool
{
return $link->next_result();
}
/**
* Store the result returned from multi query
*
* @param mysqli $link the mysqli object
*
* @return MysqliResult|false false when empty results / result set when not empty
*/
public function storeResult($link)
{
$result = $link->store_result();
return $result === false ? false : new MysqliResult($result);
}
/**
* Returns a string representing the type of connection used
*
* @param mysqli $link mysql link
*
* @return string type of connection used
*/
public function getHostInfo($link)
{
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
return $link->host_info;
}
/**
* Returns the version of the MySQL protocol used
*
* @param mysqli $link mysql link
*
* @return string version of the MySQL protocol used
*/
public function getProtoInfo($link)
{
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
return $link->protocol_version;
}
/**
* returns a string that represents the client library version
*
* @return string MySQL client library version
*/
public function getClientInfo()
{
return mysqli_get_client_info();
}
/**
* Returns last error message or an empty string if no errors occurred.
*
* @param mysqli|false|null $link mysql link
*/
public function getError($link): string
{
$GLOBALS['errno'] = 0;
if ($link !== null && $link !== false) {
$error_number = $link->errno;
$error_message = $link->error;
} else {
$error_number = mysqli_connect_errno();
$error_message = (string) mysqli_connect_error();
}
if ($error_number === 0 || $error_message === '') {
return '';
}
// keep the error number for further check after
// the call to getError()
$GLOBALS['errno'] = $error_number;
return Utilities::formatError($error_number, $error_message);
}
/**
* returns the number of rows affected by last query
*
* @param mysqli $link the mysqli object
*
* @return int|string
* @psalm-return int|numeric-string
*/
public function affectedRows($link)
{
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
return $link->affected_rows;
}
/**
* returns properly escaped string for use in MySQL queries
*
* @param mysqli $link database link
* @param string $string string to be escaped
*
* @return string a MySQL escaped string
*/
public function escapeString($link, $string)
{
return $link->real_escape_string($string);
}
/**
* Prepare an SQL statement for execution.
*
* @param mysqli $link database link
* @param string $query The query, as a string.
*
* @return mysqli_stmt|false A statement object or false.
*/
public function prepare($link, string $query)
{
return $link->prepare($query);
}
}

View file

@ -0,0 +1,276 @@
<?php
/**
* Extension independent database result
*/
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
use Generator;
use mysqli_result;
use PhpMyAdmin\FieldMetadata;
use Webmozart\Assert\Assert;
use function array_column;
use function is_array;
use function is_bool;
use function is_string;
use function method_exists;
use const MYSQLI_ASSOC;
/**
* Extension independent database result
*/
final class MysqliResult implements ResultInterface
{
/**
* The result identifier produced by the DBiExtension
*
* @var mysqli_result|null $result
*/
private $result;
/**
* @param mysqli_result|bool $result
*/
public function __construct($result)
{
$this->result = is_bool($result) ? null : $result;
}
/**
* Returns a generator that traverses through the whole result set
* and returns each row as an associative array
*
* @psalm-return Generator<int, array<string, string|null>, mixed, void>
*/
public function getIterator(): Generator
{
if (! $this->result) {
return;
}
$this->result->data_seek(0);
/** @var array<string, string|null> $row */
foreach ($this->result as $row) {
yield $row;
}
}
/**
* Returns the next row of the result with associative keys
*
* @return array<string,string|null>
*/
public function fetchAssoc(): array
{
if (! $this->result) {
return [];
}
$row = $this->result->fetch_assoc();
return is_array($row) ? $row : [];
}
/**
* Returns the next row of the result with numeric keys
*
* @return array<int,string|null>
*/
public function fetchRow(): array
{
if (! $this->result) {
return [];
}
$row = $this->result->fetch_row();
return is_array($row) ? $row : [];
}
/**
* Returns a single value from the given result; false on error
*
* @param int|string $field
*
* @return string|false|null
*/
public function fetchValue($field = 0)
{
if (is_string($field)) {
$row = $this->fetchAssoc();
} else {
$row = $this->fetchRow();
}
return $row[$field] ?? false;
}
/**
* Returns all rows of the result
*
* @return array<int, array<string,string|null>>
*/
public function fetchAllAssoc(): array
{
if (! $this->result) {
return [];
}
// This function should return all rows, not only the remaining rows
$this->result->data_seek(0);
// Pre PHP 8.1 when compiled against libmysql doesn't support fetch_all
if (method_exists($this->result, 'fetch_all')) {
return $this->result->fetch_all(MYSQLI_ASSOC);
}
$rows = [];
while ($row = $this->result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
/**
* Returns values from the first column of each row
*
* @return array<int, string|null>
*/
public function fetchAllColumn(): array
{
if (! $this->result) {
return [];
}
// This function should return all rows, not only the remaining rows
$this->result->data_seek(0);
// Pre PHP 8.1 when compiled against libmysql doesn't support fetch_all
if (method_exists($this->result, 'fetch_all')) {
return array_column($this->result->fetch_all(), 0);
}
$rows = [];
while ($row = $this->result->fetch_row()) {
$rows[] = $row[0];
}
return $rows;
}
/**
* Returns values as single dimensional array where the key is the first column
* and the value is the second column, e.g.
* SELECT id, name FROM users
* produces: ['123' => 'John', '124' => 'Jane']
*
* @return array<string, string|null>
*/
public function fetchAllKeyPair(): array
{
if (! $this->result) {
return [];
}
Assert::greaterThanEq($this->result->field_count, 2);
// This function should return all rows, not only the remaining rows
$this->result->data_seek(0);
// Pre PHP 8.1 when compiled against libmysql doesn't support fetch_all
if (method_exists($this->result, 'fetch_all')) {
return array_column($this->result->fetch_all(), 1, 0);
}
$rows = [];
while ($row = $this->result->fetch_row()) {
$rows[$row[0] ?? ''] = $row[1];
}
return $rows;
}
/**
* Returns the number of fields in the result
*/
public function numFields(): int
{
if (! $this->result) {
return 0;
}
return $this->result->field_count;
}
/**
* Returns the number of rows in the result
*
* @return string|int
* @psalm-return int|numeric-string
*/
public function numRows()
{
if (! $this->result) {
return 0;
}
return $this->result->num_rows;
}
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param int $offset offset to seek
*
* @return bool True if the offset exists, false otherwise
*/
public function seek(int $offset): bool
{
if (! $this->result) {
return false;
}
return $this->result->data_seek($offset);
}
/**
* returns meta info for fields in $result
*
* @return array<int, FieldMetadata> meta info for fields in $result
*/
public function getFieldsMeta(): array
{
if (! $this->result) {
return [];
}
$fields = [];
foreach ($this->result->fetch_fields() as $k => $field) {
$fields[$k] = new FieldMetadata($field->type, $field->flags, $field);
}
return $fields;
}
/**
* Returns the names of the fields in the result
*
* @return array<int, string> Fields names
*/
public function getFieldNames(): array
{
if (! $this->result) {
return [];
}
/** @var list<string> $column */
$column = array_column($this->result->fetch_fields(), 'name');
return $column;
}
}

View file

@ -0,0 +1,111 @@
<?php
/**
* Extension independent database result interface
*/
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
use Generator;
use IteratorAggregate;
use PhpMyAdmin\FieldMetadata;
/**
* Extension independent database result interface
*
* @extends IteratorAggregate<array<string, (string|null)>>
*/
interface ResultInterface extends IteratorAggregate
{
/**
* Returns a generator that traverses through the whole result set
* and returns each row as an associative array
*
* @psalm-return Generator<int, array<string, string|null>, mixed, void>
*/
public function getIterator(): Generator;
/**
* Returns the next row of the result with associative keys
*
* @return array<string,string|null>
*/
public function fetchAssoc(): array;
/**
* Returns the next row of the result with numeric keys
*
* @return array<int,string|null>
*/
public function fetchRow(): array;
/**
* Returns a single value from the given result; false on error
*
* @param int|string $field
*
* @return string|false|null
*/
public function fetchValue($field = 0);
/**
* Returns all rows of the result
*
* @return array<int, array<string,string|null>>
*/
public function fetchAllAssoc(): array;
/**
* Returns values from the first column of each row
*
* @return array<int, string|null>
*/
public function fetchAllColumn(): array;
/**
* Returns values as single dimensional array where the key is the first column
* and the value is the second column,
* e.g. "SELECT id, name FROM users"
* produces: ['123' => 'John', '124' => 'Jane']
*
* @return array<string, string|null>
*/
public function fetchAllKeyPair(): array;
/**
* Returns the number of fields in the result
*/
public function numFields(): int;
/**
* Returns the number of rows in the result
*
* @return string|int
* @psalm-return int|numeric-string
*/
public function numRows();
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param int $offset offset to seek
*
* @return bool True if the offset exists, false otherwise
*/
public function seek(int $offset): bool;
/**
* Returns meta info for fields in $result
*
* @return array<int, FieldMetadata> meta info for fields in $result
*/
public function getFieldsMeta(): array;
/**
* Returns the names of the fields in the result
*
* @return array<int, string> Fields names
*/
public function getFieldNames(): array;
}

View file

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
use Stringable;
use Webmozart\Assert\Assert;
use Webmozart\Assert\InvalidArgumentException;
/**
* @psalm-immutable
*/
final class TableName implements Stringable
{
/**
* @see https://dev.mysql.com/doc/refman/en/identifier-length.html
* @see https://mariadb.com/kb/en/identifier-names/#maximum-length
*/
private const MAX_LENGTH = 64;
/**
* @var string
* @psalm-var non-empty-string
*/
private $name;
/**
* @param mixed $name
*
* @throws InvalidArgumentException
*/
private function __construct($name)
{
Assert::stringNotEmpty($name);
Assert::maxLength($name, self::MAX_LENGTH);
Assert::notEndsWith($name, ' ');
$this->name = $name;
}
/**
* @param mixed $name
*
* @throws InvalidArgumentException
*/
public static function fromValue($name): self
{
return new self($name);
}
/**
* @psalm-return non-empty-string
*/
public function getName(): string
{
return $this->name;
}
/**
* @psalm-return non-empty-string
*/
public function __toString(): string
{
return $this->name;
}
}

View file

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
use Stringable;
use function in_array;
use function is_numeric;
use function is_string;
/**
* @see https://mariadb.com/kb/en/show-warnings/
* @see https://dev.mysql.com/doc/refman/en/show-warnings.html
*
* @psalm-immutable
*/
final class Warning implements Stringable
{
/**
* @var string
* @psalm-var 'Note'|'Warning'|'Error'|'?'
*/
public $level;
/**
* @var int
* @psalm-var 0|positive-int
*/
public $code;
/** @var string */
public $message;
private function __construct(string $level, int $code, string $message)
{
$this->level = in_array($level, ['Note', 'Warning', 'Error'], true) ? $level : '?';
$this->code = $code >= 1 ? $code : 0;
$this->message = $message;
}
/**
* @param mixed[] $row
*/
public static function fromArray(array $row): self
{
$level = '';
$code = 0;
$message = '';
if (isset($row['Level']) && is_string($row['Level'])) {
$level = $row['Level'];
}
if (isset($row['Code']) && is_numeric($row['Code'])) {
$code = (int) $row['Code'];
}
if (isset($row['Message']) && is_string($row['Message'])) {
$message = $row['Message'];
}
return new self($level, $code, $message);
}
/**
* @psalm-return non-empty-string
*/
public function __toString(): string
{
return $this->level . ': #' . $this->code . ($this->message !== '' ? ' ' . $this->message : '');
}
}