Update website

This commit is contained in:
Guilhem Lavaux 2025-03-24 09:27:39 +01:00
parent a0b0d3dae7
commit ae7ef6ad45
3151 changed files with 566766 additions and 48 deletions

View file

@ -0,0 +1,799 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
use mysqli_result;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\SystemDatabase;
use PhpMyAdmin\Table;
/**
* Main interface for database interactions
*/
interface DbalInterface
{
/**
* 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
*
* @return mixed
*/
public function query(
string $query,
$link = DatabaseInterface::CONNECT_USER,
int $options = 0,
bool $cache_affected_rows = true
);
/**
* 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
);
/**
* Run multi query statement and return results
*
* @param string $multiQuery multi query statement to execute
* @param int $linkIndex index of the opened database link
*
* @return mysqli_result[]|bool (false)
*/
public function tryMultiQuery(string $multiQuery = '', $linkIndex = DatabaseInterface::CONNECT_USER);
/**
* 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
*
* @param string $database name of database
* @param array $tables list of tables to search for for relations
* @param int $link mysql link resource|object
*
* @return array array of found foreign keys
*/
public function getForeignKeyConstrains(
string $database,
array $tables,
$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 $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 array Set of VIEWs inside the database
*/
public function getVirtualTables(string $db): array;
/**
* returns array with databases containing extended infos about them
*
* @param string $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 $database name of database
* @param string $table name of table to retrieve columns from
* @param string $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 descriptions of columns in given table (all or given by $column)
*
* @param string $database name of database
* @param string $table name of table to retrieve columns from
* @param string $column name of column, null to show all columns
* @param bool $full whether to return full info or only column names
* @param int $link link type
*
* @return array array indexed by column names or,
* if $column is given, flat array description
*/
public function getColumns(
string $database,
string $table,
?string $column = null,
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 array|null
*/
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 mixed $link mysql link resource|object
*
* @return mixed 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 mixed $link mysql link resource|object
*
* @return bool whether query was a successful
*/
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(): 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 $row_number row to fetch the value from,
* starting at 0, with 0 being
* default
* @param int|string $field field to fetch the value from,
* starting at 0, with 0 being
* default
* @param int $link link type
*
* @return mixed value of first field in first row from result
* or false if not found
*/
public function fetchValue(
string $query,
int $row_number = 0,
$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|BOTH returned array should either numeric
* associative or both
* @param int $link link type
*/
public function fetchSingleRow(
string $query,
string $type = '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
* @param int $options query options
*
* @return array resultrows or values indexed by $key
*/
public function fetchResult(
string $query,
$key = null,
$value = null,
$link = DatabaseInterface::CONNECT_USER,
int $options = 0
);
/**
* 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 $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 = '', $delimiter = '//');
/**
* 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
*
* @return bool Whether user is a 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|bool
*/
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 $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 $dbname database name to select
* @param int $link link type
*/
public function selectDb(string $dbname, $link = DatabaseInterface::CONNECT_USER): bool;
/**
* returns array of rows with associative and numeric keys from $result
*
* @param object $result result set identifier
*/
public function fetchArray($result): ?array;
/**
* returns array of rows with associative keys from $result
*
* @param object $result result set identifier
*/
public function fetchAssoc($result): ?array;
/**
* returns array of rows with numeric keys from $result
*
* @param object $result result set identifier
*/
public function fetchRow($result): ?array;
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param object $result database result
* @param int $offset offset to seek
*
* @return bool true on success, false on failure
*/
public function dataSeek($result, int $offset): bool;
/**
* Frees memory associated with the result
*
* @param object $result database result
*/
public function freeResult($result): void;
/**
* Check if there are any more query results from a multi query
*
* @param int $link link type
*
* @return bool true or false
*/
public function moreResults($link = DatabaseInterface::CONNECT_USER): bool;
/**
* Prepare next result from multi_query
*
* @param int $link link type
*
* @return bool true or false
*/
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
*
* @param int $link link type
*
* @return string MySQL client library version
*/
public function getClientInfo($link = DatabaseInterface::CONNECT_USER): string;
/**
* returns last error message or false if no errors occurred
*
* @param int $link link type
*
* @return string|bool error or false
*/
public function getError($link = DatabaseInterface::CONNECT_USER);
/**
* returns the number of rows returned by last query
*
* @param object $result result set identifier
*
* @return string|int
*/
public function numRows($result);
/**
* returns last inserted auto_increment id for given $link
* or $GLOBALS['userlink']
*
* @param int $link link type
*
* @return int|bool
*/
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|bool
*/
public function affectedRows($link = DatabaseInterface::CONNECT_USER, bool $get_from_cache = true);
/**
* returns metainfo for fields in $result
*
* @param object $result result set identifier
*
* @return mixed meta info for fields in $result
*/
public function getFieldsMeta($result);
/**
* return number of fields in given $result
*
* @param object $result result set identifier
*
* @return int field count
*/
public function numFields($result): int;
/**
* returns the length of the given field $i in $result
*
* @param object $result result set identifier
* @param int $i field
*
* @return int|bool length of field
*/
public function fieldLen($result, int $i);
/**
* returns name of $i. field in $result
*
* @param object $result result set identifier
* @param int $i field
*
* @return string name of $i. field in $result
*/
public function fieldName($result, int $i): string;
/**
* returns concatenated string of human readable field flags
*
* @param object $result result set identifier
* @param int $i field
*
* @return string field flags
*/
public function fieldFlags($result, $i): string;
/**
* 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);
/**
* 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,249 @@
<?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 $dbname database name to select
* @param object $link connection object
*
* @return bool
*/
public function selectDb($dbname, $link);
/**
* runs a query and returns the result
*
* @param string $query query to execute
* @param object $link connection object
* @param int $options query options
*
* @return mixed result
*/
public function realQuery($query, $link, $options);
/**
* Run the multi query and output the results
*
* @param object $link connection object
* @param string $query multi query statement to execute
*
* @return array|bool
*/
public function realMultiQuery($link, $query);
/**
* returns array of rows with associative and numeric keys from $result
*
* @param object $result result set identifier
*/
public function fetchArray($result): ?array;
/**
* returns array of rows with associative keys from $result
*
* @param object $result result set identifier
*/
public function fetchAssoc($result): ?array;
/**
* returns array of rows with numeric keys from $result
*
* @param object $result result set identifier
*/
public function fetchRow($result): ?array;
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param object $result database result
* @param int $offset offset to seek
*
* @return bool true on success, false on failure
*/
public function dataSeek($result, $offset);
/**
* Frees memory associated with the result
*
* @param object $result database result
*
* @return void
*/
public function freeResult($result);
/**
* Check if there are any more query results from a multi query
*
* @param object $link the connection object
*
* @return bool true or false
*/
public function moreResults($link);
/**
* Prepare next result from multi_query
*
* @param object $link the connection object
*
* @return bool true or false
*/
public function nextResult($link);
/**
* Store the result returned from multi query
*
* @param object $link mysql link
*
* @return mixed 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
*
* @param object $link mysql link
*
* @return string MySQL client library version
*/
public function getClientInfo($link);
/**
* returns last error message or false if no errors occurred
*
* @param object $link connection link
*
* @return string|bool error or false
*/
public function getError($link);
/**
* returns the number of rows returned by last query
*
* @param object $result result set identifier
*
* @return string|int
*/
public function numRows($result);
/**
* returns the number of rows affected by last query
*
* @param object $link the connection object
*
* @return int
*/
public function affectedRows($link);
/**
* returns metainfo for fields in $result
*
* @param object $result result set identifier
*
* @return array meta info for fields in $result
*/
public function getFieldsMeta($result);
/**
* return number of fields in given $result
*
* @param object $result result set identifier
*
* @return int field count
*/
public function numFields($result);
/**
* returns the length of the given field $i in $result
*
* @param object $result result set identifier
* @param int $i field
*
* @return int|bool length of field
*/
public function fieldLen($result, $i);
/**
* returns name of $i. field in $result
*
* @param object $result result set identifier
* @param int $i field
*
* @return string name of $i. field in $result
*/
public function fieldName($result, $i);
/**
* returns concatenated string of human readable field flags
*
* @param object $result result set identifier
* @param int $i field
*
* @return string field flags
*/
public function fieldFlags($result, $i);
/**
* returns properly escaped string for use in MySQL queries
*
* @param mixed $link database link
* @param string $str string to be escaped
*
* @return string a MySQL escaped string
*/
public function escapeString($link, $str);
/**
* 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,684 @@
<?php
/**
* Interface to the MySQL Improved extension (MySQLi)
*/
declare(strict_types=1);
namespace PhpMyAdmin\Dbal;
use mysqli;
use mysqli_result;
use mysqli_stmt;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Query\Utilities;
use stdClass;
use function mysqli_report;
use const E_USER_WARNING;
use const MYSQLI_ASSOC;
use const MYSQLI_AUTO_INCREMENT_FLAG;
use const MYSQLI_BLOB_FLAG;
use const MYSQLI_BOTH;
use const MYSQLI_CLIENT_COMPRESS;
use const MYSQLI_CLIENT_SSL;
use const MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
use const MYSQLI_ENUM_FLAG;
use const MYSQLI_MULTIPLE_KEY_FLAG;
use const MYSQLI_NOT_NULL_FLAG;
use const MYSQLI_NUM;
use const MYSQLI_NUM_FLAG;
use const MYSQLI_OPT_LOCAL_INFILE;
use const MYSQLI_OPT_SSL_VERIFY_SERVER_CERT;
use const MYSQLI_PART_KEY_FLAG;
use const MYSQLI_PRI_KEY_FLAG;
use const MYSQLI_REPORT_OFF;
use const MYSQLI_SET_FLAG;
use const MYSQLI_STORE_RESULT;
use const MYSQLI_TIMESTAMP_FLAG;
use const MYSQLI_TYPE_BIT;
use const MYSQLI_TYPE_BLOB;
use const MYSQLI_TYPE_DATE;
use const MYSQLI_TYPE_DATETIME;
use const MYSQLI_TYPE_DECIMAL;
use const MYSQLI_TYPE_DOUBLE;
use const MYSQLI_TYPE_ENUM;
use const MYSQLI_TYPE_FLOAT;
use const MYSQLI_TYPE_GEOMETRY;
use const MYSQLI_TYPE_INT24;
use const MYSQLI_TYPE_JSON;
use const MYSQLI_TYPE_LONG;
use const MYSQLI_TYPE_LONG_BLOB;
use const MYSQLI_TYPE_LONGLONG;
use const MYSQLI_TYPE_MEDIUM_BLOB;
use const MYSQLI_TYPE_NEWDATE;
use const MYSQLI_TYPE_NEWDECIMAL;
use const MYSQLI_TYPE_NULL;
use const MYSQLI_TYPE_SET;
use const MYSQLI_TYPE_SHORT;
use const MYSQLI_TYPE_STRING;
use const MYSQLI_TYPE_TIME;
use const MYSQLI_TYPE_TIMESTAMP;
use const MYSQLI_TYPE_TINY;
use const MYSQLI_TYPE_TINY_BLOB;
use const MYSQLI_TYPE_VAR_STRING;
use const MYSQLI_TYPE_YEAR;
use const MYSQLI_UNIQUE_KEY_FLAG;
use const MYSQLI_UNSIGNED_FLAG;
use const MYSQLI_USE_RESULT;
use const MYSQLI_ZEROFILL_FLAG;
use function define;
use function defined;
use function implode;
use function is_array;
use function is_bool;
use function mysqli_init;
use function stripos;
use function trigger_error;
use const PHP_VERSION_ID;
use function mysqli_get_client_info;
/**
* Interface to the MySQL Improved extension (MySQLi)
*/
class DbiMysqli implements DbiExtension
{
/** @var array */
private static $flagNames = [
MYSQLI_NUM_FLAG => 'num',
MYSQLI_PART_KEY_FLAG => 'part_key',
MYSQLI_SET_FLAG => 'set',
MYSQLI_TIMESTAMP_FLAG => 'timestamp',
MYSQLI_AUTO_INCREMENT_FLAG => 'auto_increment',
MYSQLI_ENUM_FLAG => 'enum',
MYSQLI_ZEROFILL_FLAG => 'zerofill',
MYSQLI_UNSIGNED_FLAG => 'unsigned',
MYSQLI_BLOB_FLAG => 'blob',
MYSQLI_MULTIPLE_KEY_FLAG => 'multiple_key',
MYSQLI_UNIQUE_KEY_FLAG => 'unique_key',
MYSQLI_PRI_KEY_FLAG => 'primary_key',
MYSQLI_NOT_NULL_FLAG => 'not_null',
];
/**
* 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();
$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,
$server['ssl_verify']
);
$client_flags |= MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
}
}
if ($GLOBALS['cfg']['PersistentConnections']) {
$host = 'p:' . $server['host'];
} else {
$host = $server['host'];
}
$return_value = $mysqli->real_connect(
$host,
$user,
$password,
'',
$server['port'],
(string) $server['socket'],
$client_flags
);
if ($return_value === false || $return_value === null) {
/*
* 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.
*/
$error_number = $mysqli->connect_errno;
$error_message = $mysqli->connect_error;
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);
}
return false;
}
if (defined('PMA_ENABLE_LDI')) {
$mysqli->options(MYSQLI_OPT_LOCAL_INFILE, true);
} else {
$mysqli->options(MYSQLI_OPT_LOCAL_INFILE, false);
}
return $mysqli;
}
/**
* selects given database
*
* @param string $databaseName database name to select
* @param mysqli $mysqli the mysqli object
*
* @return bool
*/
public function selectDb($databaseName, $mysqli)
{
return $mysqli->select_db($databaseName);
}
/**
* runs a query and returns the result
*
* @param string $query query to execute
* @param mysqli $mysqli mysqli object
* @param int $options query options
*
* @return mysqli_result|bool
*/
public function realQuery($query, $mysqli, $options)
{
if ($options == ($options | DatabaseInterface::QUERY_STORE)) {
$method = MYSQLI_STORE_RESULT;
} elseif ($options == ($options | DatabaseInterface::QUERY_UNBUFFERED)) {
$method = MYSQLI_USE_RESULT;
} else {
$method = 0;
}
return $mysqli->query($query, $method);
}
/**
* Run the multi query and output the results
*
* @param mysqli $mysqli mysqli object
* @param string $query multi query statement to execute
*
* @return bool
*/
public function realMultiQuery($mysqli, $query)
{
return $mysqli->multi_query($query);
}
/**
* returns array of rows with associative and numeric keys from $result
*
* @param mysqli_result $result result set identifier
*/
public function fetchArray($result): ?array
{
if (! $result instanceof mysqli_result) {
return null;
}
return $result->fetch_array(MYSQLI_BOTH);
}
/**
* returns array of rows with associative keys from $result
*
* @param mysqli_result $result result set identifier
*/
public function fetchAssoc($result): ?array
{
if (! $result instanceof mysqli_result) {
return null;
}
return $result->fetch_array(MYSQLI_ASSOC);
}
/**
* returns array of rows with numeric keys from $result
*
* @param mysqli_result $result result set identifier
*/
public function fetchRow($result): ?array
{
if (! $result instanceof mysqli_result) {
return null;
}
return $result->fetch_array(MYSQLI_NUM);
}
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param mysqli_result $result database result
* @param int $offset offset to seek
*
* @return bool true on success, false on failure
*/
public function dataSeek($result, $offset)
{
return $result->data_seek($offset);
}
/**
* Frees memory associated with the result
*
* @param mysqli_result $result database result
*
* @return void
*/
public function freeResult($result)
{
if (! ($result instanceof mysqli_result)) {
return;
}
$result->close();
}
/**
* Check if there are any more query results from a multi query
*
* @param mysqli $mysqli the mysqli object
*
* @return bool true or false
*/
public function moreResults($mysqli)
{
return $mysqli->more_results();
}
/**
* Prepare next result from multi_query
*
* @param mysqli $mysqli the mysqli object
*
* @return bool true or false
*/
public function nextResult($mysqli)
{
return $mysqli->next_result();
}
/**
* Store the result returned from multi query
*
* @param mysqli $mysqli the mysqli object
*
* @return mysqli_result|bool false when empty results / result set when not empty
*/
public function storeResult($mysqli)
{
return $mysqli->store_result();
}
/**
* Returns a string representing the type of connection used
*
* @param mysqli $mysqli mysql link
*
* @return string type of connection used
*/
public function getHostInfo($mysqli)
{
return $mysqli->host_info;
}
/**
* Returns the version of the MySQL protocol used
*
* @param mysqli $mysqli mysql link
*
* @return string version of the MySQL protocol used
*/
public function getProtoInfo($mysqli)
{
return $mysqli->protocol_version;
}
/**
* returns a string that represents the client library version
*
* @param mysqli $mysqli mysql link
*
* @return string MySQL client library version
*/
public function getClientInfo($mysqli)
{
// See: https://github.com/phpmyadmin/phpmyadmin/issues/16911
if (PHP_VERSION_ID < 80100) {
return $mysqli->get_client_info();
}
return mysqli_get_client_info();
}
/**
* returns last error message or false if no errors occurred
*
* @param mysqli $mysqli mysql link
*
* @return string|bool error or false
*/
public function getError($mysqli)
{
$GLOBALS['errno'] = 0;
if ($mysqli !== null && $mysqli !== false) {
$error_number = $mysqli->errno;
$error_message = $mysqli->error;
} else {
$error_number = $mysqli->connect_errno;
$error_message = $mysqli->connect_error;
}
if ($error_number == 0) {
return false;
}
// 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 returned by last query
*
* @param mysqli_result $result result set identifier
*
* @return string|int
*/
public function numRows($result)
{
// see the note for tryQuery();
if (is_bool($result)) {
return 0;
}
return $result->num_rows;
}
/**
* returns the number of rows affected by last query
*
* @param mysqli $mysqli the mysqli object
*
* @return int
*/
public function affectedRows($mysqli)
{
return $mysqli->affected_rows;
}
/**
* returns meta info for fields in $result
*
* @param mysqli_result $result result set identifier
*
* @return array|bool meta info for fields in $result
*/
public function getFieldsMeta($result)
{
if (! $result instanceof mysqli_result) {
return false;
}
// Issue #16043 - client API mysqlnd seem not to have MYSQLI_TYPE_JSON defined
if (! defined('MYSQLI_TYPE_JSON')) {
define('MYSQLI_TYPE_JSON', 245);
}
// Build an associative array for a type look up
$typeAr = [];
$typeAr[MYSQLI_TYPE_DECIMAL] = 'real';
$typeAr[MYSQLI_TYPE_NEWDECIMAL] = 'real';
$typeAr[MYSQLI_TYPE_BIT] = 'int';
$typeAr[MYSQLI_TYPE_TINY] = 'int';
$typeAr[MYSQLI_TYPE_SHORT] = 'int';
$typeAr[MYSQLI_TYPE_LONG] = 'int';
$typeAr[MYSQLI_TYPE_FLOAT] = 'real';
$typeAr[MYSQLI_TYPE_DOUBLE] = 'real';
$typeAr[MYSQLI_TYPE_NULL] = 'null';
$typeAr[MYSQLI_TYPE_TIMESTAMP] = 'timestamp';
$typeAr[MYSQLI_TYPE_LONGLONG] = 'int';
$typeAr[MYSQLI_TYPE_INT24] = 'int';
$typeAr[MYSQLI_TYPE_DATE] = 'date';
$typeAr[MYSQLI_TYPE_TIME] = 'time';
$typeAr[MYSQLI_TYPE_DATETIME] = 'datetime';
$typeAr[MYSQLI_TYPE_YEAR] = 'year';
$typeAr[MYSQLI_TYPE_NEWDATE] = 'date';
$typeAr[MYSQLI_TYPE_ENUM] = 'unknown';
$typeAr[MYSQLI_TYPE_SET] = 'unknown';
$typeAr[MYSQLI_TYPE_TINY_BLOB] = 'blob';
$typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
$typeAr[MYSQLI_TYPE_LONG_BLOB] = 'blob';
$typeAr[MYSQLI_TYPE_BLOB] = 'blob';
$typeAr[MYSQLI_TYPE_VAR_STRING] = 'string';
$typeAr[MYSQLI_TYPE_STRING] = 'string';
// MySQL returns MYSQLI_TYPE_STRING for CHAR
// and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY
// so this would override TINYINT and mark all TINYINT as string
// see https://github.com/phpmyadmin/phpmyadmin/issues/8569
//$typeAr[MYSQLI_TYPE_CHAR] = 'string';
$typeAr[MYSQLI_TYPE_GEOMETRY] = 'geometry';
$typeAr[MYSQLI_TYPE_BIT] = 'bit';
$typeAr[MYSQLI_TYPE_JSON] = 'json';
$fields = $result->fetch_fields();
if (! is_array($fields)) {
return false;
}
foreach ($fields as $k => $field) {
$fields[$k]->_type = $field->type;
$fields[$k]->type = $typeAr[$field->type];
$fields[$k]->_flags = $field->flags;
$fields[$k]->flags = $this->fieldFlags($result, $k);
// Enhance the field objects for mysql-extension compatibility
//$flags = explode(' ', $fields[$k]->flags);
//array_unshift($flags, 'dummy');
$fields[$k]->multiple_key
= (int) (bool) ($fields[$k]->_flags & MYSQLI_MULTIPLE_KEY_FLAG);
$fields[$k]->primary_key
= (int) (bool) ($fields[$k]->_flags & MYSQLI_PRI_KEY_FLAG);
$fields[$k]->unique_key
= (int) (bool) ($fields[$k]->_flags & MYSQLI_UNIQUE_KEY_FLAG);
$fields[$k]->not_null
= (int) (bool) ($fields[$k]->_flags & MYSQLI_NOT_NULL_FLAG);
$fields[$k]->unsigned
= (int) (bool) ($fields[$k]->_flags & MYSQLI_UNSIGNED_FLAG);
$fields[$k]->zerofill
= (int) (bool) ($fields[$k]->_flags & MYSQLI_ZEROFILL_FLAG);
$fields[$k]->numeric
= (int) (bool) ($fields[$k]->_flags & MYSQLI_NUM_FLAG);
$fields[$k]->blob
= (int) (bool) ($fields[$k]->_flags & MYSQLI_BLOB_FLAG);
}
return $fields;
}
/**
* return number of fields in given $result
*
* @param mysqli_result $result result set identifier
*
* @return int field count
*/
public function numFields($result)
{
return $result->field_count;
}
/**
* returns the length of the given field $i in $result
*
* @param mysqli_result $result result set identifier
* @param int $i field
*
* @return int|bool length of field
*/
public function fieldLen($result, $i)
{
if ($i >= $this->numFields($result)) {
return false;
}
/** @var stdClass $fieldDefinition */
$fieldDefinition = $result->fetch_field_direct($i);
if ($fieldDefinition !== false) {
return $fieldDefinition->length;
}
return false;
}
/**
* returns name of $i. field in $result
*
* @param mysqli_result $result result set identifier
* @param int $i field
*
* @return string name of $i. field in $result
*/
public function fieldName($result, $i)
{
if ($i >= $this->numFields($result)) {
return '';
}
/** @var stdClass $fieldDefinition */
$fieldDefinition = $result->fetch_field_direct($i);
if ($fieldDefinition !== false) {
return $fieldDefinition->name;
}
return '';
}
/**
* returns concatenated string of human readable field flags
*
* @param mysqli_result $result result set identifier
* @param int $i field
*
* @return string|false field flags
*/
public function fieldFlags($result, $i)
{
if ($i >= $this->numFields($result)) {
return false;
}
/** @var stdClass|false $fieldDefinition */
$fieldDefinition = $result->fetch_field_direct($i);
if ($fieldDefinition === false) {
return '';
}
$type = $fieldDefinition->type;
$charsetNumber = $fieldDefinition->charsetnr;
$fieldDefinitionFlags = $fieldDefinition->flags;
$flags = [];
foreach (self::$flagNames as $flag => $name) {
if (! ($fieldDefinitionFlags & $flag)) {
continue;
}
$flags[] = $name;
}
// See https://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
// to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
// but instead the charsetnr member of the MYSQL_FIELD
// structure. Watch out: some types like DATE returns 63 in charsetnr
// so we have to check also the type.
// Unfortunately there is no equivalent in the mysql extension.
if (($type == MYSQLI_TYPE_TINY_BLOB || $type == MYSQLI_TYPE_BLOB
|| $type == MYSQLI_TYPE_MEDIUM_BLOB || $type == MYSQLI_TYPE_LONG_BLOB
|| $type == MYSQLI_TYPE_VAR_STRING || $type == MYSQLI_TYPE_STRING)
&& $charsetNumber == 63
) {
$flags[] = 'binary';
}
return implode(' ', $flags);
}
/**
* returns properly escaped string for use in MySQL queries
*
* @param mysqli $mysqli database link
* @param string $string string to be escaped
*
* @return string a MySQL escaped string
*/
public function escapeString($mysqli, $string)
{
return $mysqli->real_escape_string($string);
}
/**
* Prepare an SQL statement for execution.
*
* @param mysqli $mysqli database link
* @param string $query The query, as a string.
*
* @return mysqli_stmt|false A statement object or false.
*/
public function prepare($mysqli, string $query)
{
return $mysqli->prepare($query);
}
}