439 lines
15 KiB
PHP
439 lines
15 KiB
PHP
<?php
|
|
// MariaDBConnectionInfo.php
|
|
// Created: 2021-04-30
|
|
// Updated: 2022-02-28
|
|
|
|
namespace Index\Data\MariaDB;
|
|
|
|
use InvalidArgumentException;
|
|
use Index\Data\IDbConnectionInfo;
|
|
use Index\Net\EndPoint;
|
|
use Index\Net\DnsEndPoint;
|
|
use Index\Net\IPAddress;
|
|
use Index\Net\IPEndPoint;
|
|
use Index\Net\UnixEndPoint;
|
|
|
|
/**
|
|
* Describes a MariaDB or MySQL connection.
|
|
*/
|
|
class MariaDBConnectionInfo implements IDbConnectionInfo {
|
|
private EndPoint $endPoint;
|
|
private string $userName;
|
|
private string $password;
|
|
private string $dbName;
|
|
private ?string $charSet;
|
|
private ?string $initCommand;
|
|
private ?string $keyPath;
|
|
private ?string $certPath;
|
|
private ?string $certAuthPath;
|
|
private ?string $trustedCertsPath;
|
|
private ?string $cipherAlgos;
|
|
private bool $verifyCert;
|
|
private bool $useCompression;
|
|
|
|
/**
|
|
* Creates an instance of MariaDBConnectionInfo.
|
|
*
|
|
* @param EndPoint $endPoint End point at which the server can be found.
|
|
* @param string $userName User name to use for the connection.
|
|
* @param string $password Password with which the user authenticates.
|
|
* @param string $dbName Default database name.
|
|
* @param string|null $charSet Default character set.
|
|
* @param string|null $initCommand Command to execute on connect.
|
|
* @param string|null $keyPath Path at which to find the private key for SSL.
|
|
* @param string|null $certPath Path at which to find the certificate file for SSL.
|
|
* @param string|null $certAuthPath Path at which to find the certificate authority file for SSL.
|
|
* @param string|null $trustedCertsPath Path at which to find the trusted SSL CA certificates for SSL in PEM format.
|
|
* @param string|null $cipherAlgos List of SSL encryption cipher that are allowed.
|
|
* @param bool $verifyCert true if the client should verify the server's SSL certificate, false if not.
|
|
* @param bool $useCompression true if compression should be used, false if not.
|
|
* @return MariaDBConnectionInfo A connection info instance representing the given information.
|
|
*/
|
|
public function __construct(
|
|
EndPoint $endPoint,
|
|
string $userName,
|
|
string $password,
|
|
string $dbName,
|
|
string|null $charSet,
|
|
string|null $initCommand,
|
|
string|null $keyPath,
|
|
string|null $certPath,
|
|
string|null $certAuthPath,
|
|
string|null $trustedCertsPath,
|
|
string|null $cipherAlgos,
|
|
bool $verifyCert,
|
|
bool $useCompression
|
|
) {
|
|
if(!($endPoint instanceof IPEndPoint)
|
|
&& !($endPoint instanceof DnsEndPoint)
|
|
&& !($endPoint instanceof UnixEndPoint))
|
|
throw new InvalidArgumentException('$endPoint must be of type IPEndPoint, DnsEndPoint or UnixEndPoint.');
|
|
$this->endPoint = $endPoint;
|
|
$this->userName = $userName;
|
|
$this->password = $password;
|
|
$this->dbName = $dbName;
|
|
$this->charSet = empty($charSet) ? null : $charSet;
|
|
$this->initCommand = empty($initCommand) ? null : $initCommand;
|
|
$this->keyPath = empty($keyPath) ? null : $keyPath;
|
|
$this->certPath = empty($certPath) ? null : $certPath;
|
|
$this->certAuthPath = empty($certAuthPath) ? null : $certAuthPath;
|
|
$this->trustedCertsPath = empty($trustedCertsPath) ? null : $trustedCertsPath;
|
|
$this->cipherAlgos = empty($cipherAlgos) ? null : $cipherAlgos;
|
|
$this->verifyCert = $verifyCert;
|
|
$this->useCompression = $useCompression;
|
|
}
|
|
|
|
/**
|
|
* Gets the end point value.
|
|
*
|
|
* @return EndPoint An instance of EndPoint.
|
|
*/
|
|
public function getEndPoint(): EndPoint {
|
|
return $this->endPoint;
|
|
}
|
|
|
|
/**
|
|
* Returns whether the end point is a unix socket path.
|
|
*
|
|
* @return bool true if the end point is a unix socket, false if not.
|
|
*/
|
|
public function isUnixSocket(): bool {
|
|
return $this->endPoint instanceof UnixEndPoint;
|
|
}
|
|
|
|
/**
|
|
* Gets the host value of the end point.
|
|
*
|
|
* Will return an empty string if the end point is a unix socket path.
|
|
*
|
|
* @return string Target host name.
|
|
*/
|
|
public function getHost(): string {
|
|
if($this->endPoint instanceof DnsEndPoint)
|
|
return $this->endPoint->getHost();
|
|
if($this->endPoint instanceof IPEndPoint)
|
|
return $this->endPoint->getAddress()->getCleanAddress();
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Gets the port value of the end point.
|
|
*
|
|
* Will return -1 if the end point is a unix socket path.
|
|
*
|
|
* @return int Target port number.
|
|
*/
|
|
public function getPort(): int {
|
|
if(($this->endPoint instanceof DnsEndPoint) || ($this->endPoint instanceof IPEndPoint)) {
|
|
$port = $this->endPoint->getPort();
|
|
return $port < 1 ? 3306 : $port;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
/**
|
|
* Gets the unix socket path.
|
|
*
|
|
* Will return an empty string if the end point is not a unix socket path.
|
|
*
|
|
* @return string Target unix socket path.
|
|
*/
|
|
public function getSocketPath(): string {
|
|
if($this->endPoint instanceof UnixEndPoint)
|
|
return $this->endPoint->getPath();
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Returns the connection user name.
|
|
*
|
|
* @return string Connection user name.
|
|
*/
|
|
public function getUserName(): string {
|
|
return $this->userName;
|
|
}
|
|
|
|
/**
|
|
* Returns the connection user password.
|
|
*
|
|
* @return string Connection user password.
|
|
*/
|
|
public function getPassword(): string {
|
|
return $this->password;
|
|
}
|
|
|
|
/**
|
|
* Returns the connection default database name.
|
|
*
|
|
* @return string Default database name.
|
|
*/
|
|
public function getDatabaseName(): string {
|
|
return $this->dbName;
|
|
}
|
|
|
|
/**
|
|
* Whether this connection info carries a default character set name.
|
|
*
|
|
* @return bool true if it has a character set name, false if not.
|
|
*/
|
|
public function hasCharacterSet(): bool {
|
|
return $this->charSet !== null;
|
|
}
|
|
|
|
/**
|
|
* Returns the default character set name.
|
|
*
|
|
* @return ?string Default character set name.
|
|
*/
|
|
public function getCharacterSet(): ?string {
|
|
return $this->charSet;
|
|
}
|
|
|
|
/**
|
|
* Whether this connection info carries an initialisation command.
|
|
*
|
|
* @return bool true if it has an initialisation command, false if not.
|
|
*/
|
|
public function hasInitCommand(): bool {
|
|
return $this->initCommand !== null;
|
|
}
|
|
|
|
/**
|
|
* Returns the initialisation command.
|
|
*
|
|
* @return ?string Initialisation command.
|
|
*/
|
|
public function getInitCommand(): ?string {
|
|
return $this->initCommand;
|
|
}
|
|
|
|
/**
|
|
* Whether SSL is to be used with the connection.
|
|
*
|
|
* @return bool true if SSL should be used, false if not.
|
|
*/
|
|
public function isSecure(): bool {
|
|
return $this->keyPath !== null;
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the private key to use for this connection.
|
|
*
|
|
* @return ?string Private key path.
|
|
*/
|
|
public function getKeyPath(): ?string {
|
|
return $this->keyPath;
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the certificate to use for this connection.
|
|
*
|
|
* @return ?string Certificate path.
|
|
*/
|
|
public function getCertificatePath(): ?string {
|
|
return $this->certPath;
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the certificate authority file to use for this connection.
|
|
*
|
|
* @return ?string Certificate authority file path.
|
|
*/
|
|
public function getCertificateAuthorityPath(): ?string {
|
|
return $this->certAuthPath;
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the trusted CA certificates directory for this connection.
|
|
*
|
|
* @return ?string Trusted CA certificates directory path.
|
|
*/
|
|
public function getTrustedCertificatesPath(): ?string {
|
|
return $this->trustedCertsPath;
|
|
}
|
|
|
|
/**
|
|
* Returns the list of allowed SSL cipher algorithms.
|
|
*
|
|
* @return ?string Allowed SSL cipher algorithms.
|
|
*/
|
|
public function getCipherAlgorithms(): ?string {
|
|
return $this->cipherAlgos;
|
|
}
|
|
|
|
/**
|
|
* Whether the client should verify the validity of the server's certificate.
|
|
*
|
|
* @return bool true if the certificate should be verified, false if not.
|
|
*/
|
|
public function shouldVerifyCertificate(): bool {
|
|
return $this->verifyCert;
|
|
}
|
|
|
|
/**
|
|
* Whether compression should be applied on the connection.
|
|
*
|
|
* Only makes sense for remote connections, don't use this locally...
|
|
*
|
|
* @return bool true if compression should be used, false if not.
|
|
*/
|
|
public function shouldUseCompression(): bool {
|
|
return $this->useCompression;
|
|
}
|
|
|
|
/**
|
|
* Creates a connection info instance with a hostname and port number.
|
|
*
|
|
* @param string $host Server host name.
|
|
* @param int $port Server port.
|
|
* @param string $userName User name to use for the connection.
|
|
* @param string $password Password with which the user authenticates.
|
|
* @param string $dbName Default database name.
|
|
* @param string|null $charSet Default character set.
|
|
* @param string|null $initCommand Command to execute on connect.
|
|
* @param string|null $keyPath Path at which to find the private key for SSL.
|
|
* @param string|null $certPath Path at which to find the certificate file for SSL.
|
|
* @param string|null $certAuthPath Path at which to find the certificate authority file for SSL.
|
|
* @param string|null $trustedCertsPath Path at which to find the trusted SSL CA certificates for SSL in PEM format.
|
|
* @param string|null $cipherAlgos List of SSL encryption cipher that are allowed.
|
|
* @param bool $verifyCert true if the client should verify the server's SSL certificate, false if not.
|
|
* @param bool $useCompression true if compression should be used, false if not.
|
|
* @return MariaDBConnectionInfo A connection info instance representing the given information.
|
|
*/
|
|
public static function createHost(
|
|
string $host,
|
|
int $port,
|
|
string $userName,
|
|
string $password,
|
|
string $dbName = '',
|
|
string|null $charSet = null,
|
|
string|null $initCommand = null,
|
|
string|null $keyPath = null,
|
|
string|null $certPath = null,
|
|
string|null $certAuthPath = null,
|
|
string|null $trustedCertsPath = null,
|
|
string|null $cipherAlgos = null,
|
|
bool $verifyCert = false,
|
|
bool $useCompression = false
|
|
): MariaDBConnectionInfo {
|
|
return new MariaDBConnectionInfo(
|
|
new DnsEndPoint($host, $port),
|
|
$userName,
|
|
$password,
|
|
$dbName,
|
|
$charSet,
|
|
$initCommand,
|
|
$keyPath,
|
|
$certPath,
|
|
$certAuthPath,
|
|
$trustedCertsPath,
|
|
$cipherAlgos,
|
|
$verifyCert,
|
|
$useCompression
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Creates a connection info instance with a unix socket path.
|
|
*
|
|
* @param string $path Path to the unix socket.
|
|
* @param string $userName User name to use for the connection.
|
|
* @param string $password Password with which the user authenticates.
|
|
* @param string $dbName Default database name.
|
|
* @param string|null $charSet Default character set.
|
|
* @param string|null $initCommand Command to execute on connect.
|
|
* @param string|null $keyPath Path at which to find the private key for SSL.
|
|
* @param string|null $certPath Path at which to find the certificate file for SSL.
|
|
* @param string|null $certAuthPath Path at which to find the certificate authority file for SSL.
|
|
* @param string|null $trustedCertsPath Path at which to find the trusted SSL CA certificates for SSL in PEM format.
|
|
* @param string|null $cipherAlgos List of SSL encryption cipher that are allowed.
|
|
* @param bool $verifyCert true if the client should verify the server's SSL certificate, false if not.
|
|
* @param bool $useCompression true if compression should be used, false if not.
|
|
* @return MariaDBConnectionInfo A connection info instance representing the given information.
|
|
*/
|
|
public static function createUnix(
|
|
string $path,
|
|
string $userName,
|
|
string $password,
|
|
string $dbName = '',
|
|
string|null $charSet = null,
|
|
string|null $initCommand = null,
|
|
string|null $keyPath = null,
|
|
string|null $certPath = null,
|
|
string|null $certAuthPath = null,
|
|
string|null $trustedCertsPath = null,
|
|
string|null $cipherAlgos = null,
|
|
bool $verifyCert = false,
|
|
bool $useCompression = false
|
|
): MariaDBConnectionInfo {
|
|
return new MariaDBConnectionInfo(
|
|
UnixEndPoint::parse($path),
|
|
$userName,
|
|
$password,
|
|
$dbName,
|
|
$charSet,
|
|
$initCommand,
|
|
$keyPath,
|
|
$certPath,
|
|
$certAuthPath,
|
|
$trustedCertsPath,
|
|
$cipherAlgos,
|
|
$verifyCert,
|
|
$useCompression
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Tries to parse an end point string and creates a connection info instance using it..
|
|
*
|
|
* Supports IPv4, IPv6 and DNS hostnames with and without a port number and Unix socket paths prefixed with unix://
|
|
*
|
|
* @param string $endPoint End point string.
|
|
* @param string $userName User name to use for the connection.
|
|
* @param string $password Password with which the user authenticates.
|
|
* @param string $dbName Default database name.
|
|
* @param string|null $charSet Default character set.
|
|
* @param string|null $initCommand Command to execute on connect.
|
|
* @param string|null $keyPath Path at which to find the private key for SSL.
|
|
* @param string|null $certPath Path at which to find the certificate file for SSL.
|
|
* @param string|null $certAuthPath Path at which to find the certificate authority file for SSL.
|
|
* @param string|null $trustedCertsPath Path at which to find the trusted SSL CA certificates for SSL in PEM format.
|
|
* @param string|null $cipherAlgos List of SSL encryption cipher that are allowed.
|
|
* @param bool $verifyCert true if the client should verify the server's SSL certificate, false if not.
|
|
* @param bool $useCompression true if compression should be used, false if not.
|
|
* @return MariaDBConnectionInfo A connection info instance representing the given information.
|
|
*/
|
|
public static function create(
|
|
string $endPoint,
|
|
string $userName,
|
|
string $password,
|
|
string $dbName = '',
|
|
string|null $charSet = null,
|
|
string|null $initCommand = null,
|
|
string|null $keyPath = null,
|
|
string|null $certPath = null,
|
|
string|null $certAuthPath = null,
|
|
string|null $trustedCertsPath = null,
|
|
string|null $cipherAlgos = null,
|
|
bool $verifyCert = false,
|
|
bool $useCompression = false
|
|
): MariaDBConnectionInfo {
|
|
return new MariaDBConnectionInfo(
|
|
EndPoint::parse($endPoint),
|
|
$userName,
|
|
$password,
|
|
$dbName,
|
|
$charSet,
|
|
$initCommand,
|
|
$keyPath,
|
|
$certPath,
|
|
$certAuthPath,
|
|
$trustedCertsPath,
|
|
$cipherAlgos,
|
|
$verifyCert,
|
|
$useCompression
|
|
);
|
|
}
|
|
}
|