From 8d97170e8016b969505a29b8cd39f001294653a2 Mon Sep 17 00:00:00 2001 From: look-was-here Date: Sat, 31 Aug 2024 21:49:52 -0500 Subject: mysqli: Improve setConnectionParameter - Simplify control statements with single if expressions. - Updated phpdoc with additional details on how to set connection property values. - Removed to-do that seemed wrong since the user should use setConnectionParameter for those or set the property directly. - Allow setting of ssl cert property values, clientFlags, port, and socket using the standard setConnectionParameter functionality. Feedback updates: - Return false for special cases with type checks on sslsert, socket, clientflags, or port fail - Added invalid connection param to output. - Simplified isset checks. - Changed sslcert to ssl and simplified subkeys to ('key', 'cert', 'ca', 'capath', 'cipher'). - Removed code duplication. - is_numeric should be checking value not parameter Fixes #1044 --- drivers/adodb-mysqli.inc.php | 68 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/drivers/adodb-mysqli.inc.php b/drivers/adodb-mysqli.inc.php index 7efd1878..a6678274 100644 --- a/drivers/adodb-mysqli.inc.php +++ b/drivers/adodb-mysqli.inc.php @@ -145,22 +145,74 @@ class ADODB_mysqli extends ADOConnection { } /** - * Adds a parameter to the connection string. + * Adds a parameter to the connection string, can also set connection property values. * * Parameter must be one of the constants listed in mysqli_options(). * @see https://www.php.net/manual/en/mysqli.options.php - * - * @param int $parameter The parameter to set - * @param string $value The value of the parameter + * + * OR + * + * Parameter must be a string matching one of the following special cases. + * 'ssl' - SSL values e.g. ('ssl' => ['ca' => '/path/to/ca.crt.pem']) + * 'clientflags' - Client flags of type 'MYSQLI_CLIENT_' + * @see https://www.php.net/manual/en/mysqli.real-connect.php + * @see https://www.php.net/manual/en/mysqli.constants.php + * 'socket' - The socket or named pipe that should be used + * 'port' - The port number to attempt to connect to the MySQL server + * + * @param string|int $parameter The parameter to set + * @param string|int|array $value The value of the parameter * * @return bool */ public function setConnectionParameter($parameter, $value) { - if(!is_numeric($parameter)) { - $this->outp_throw("Invalid connection parameter '$parameter'", __METHOD__); - return false; + + // Special case for setting SSL values. + if ("ssl" === $parameter && is_array($value)) { + if (isset($value["key"])) { + $this->ssl_key = $value["key"]; + } + if (isset($value["cert"])) { + $this->ssl_cert = $value["cert"]; + } + if (isset($value["ca"])) { + $this->ssl_ca = $value["ca"]; + } + if (isset($value["capath"])) { + $this->ssl_capath = $value["capath"]; + } + if (isset($value["cipher"])) { + $this->ssl_cipher = $value["cipher"]; + } + + return true; } - return parent::setConnectionParameter($parameter, $value); + + // Special case for setting the client flag(s). + if ("clientflags" === $parameter && is_numeric($value)) { + $this->clientFlags = $value; + return true; + } + + // Special case for setting the socket. + if ("socket" === $parameter && is_string($value)) { + $this->socket = $value; + return true; + } + + // Special case for setting the port. + if ("port" === $parameter && is_numeric($value)) { + $this->port = (int)$value; + return true; + } + + // Standard mysqli_options. + if (is_numeric($parameter)) { + return parent::setConnectionParameter($parameter, $value); + } + + $this->outp_throw("Invalid connection parameter '$parameter'", __METHOD__); + return false; } /** -- cgit v1.3