From c715b9488ac0f7fbb65151af2e3d8b2ca78889d5 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Thu, 8 Feb 2018 14:22:27 +0000 Subject: [PATCH] =?UTF-8?q?N=C2=B01260=20MySQL=20connection=20:=20allow=20?= =?UTF-8?q?to=20use=20p:=20host=20prefix=20(persistent=20connection,=20see?= =?UTF-8?q?=20http://php.net/manual/en/mysqli.persistconns.php)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SVN:trunk[5316] --- core/cmdbsource.class.inc.php | 41 ++++++++++++++++++++--------------- core/mutex.class.inc.php | 2 +- setup/backup.class.inc.php | 2 +- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/core/cmdbsource.class.inc.php b/core/cmdbsource.class.inc.php index be225815b..b90c57ccb 100644 --- a/core/cmdbsource.class.inc.php +++ b/core/cmdbsource.class.inc.php @@ -174,11 +174,8 @@ class CMDBSource self::$m_sDBTlsCaPath = empty($sTlsCaPath) ? null : $sTlsCaPath; self::$m_sDBTlsCipher = empty($sTlsCipher) ? null : $sTlsCipher; - // when using TLS add persistent connection to reduce overhead - $bUsePersistentConnection = self::IsDbConnectionUsingTls($sServer, $sServer, $sServer); - self::$m_oMysqli = self::GetMysqliInstance($sServer, $sUser, $sPwd, $sSource, $sTlsKey, $sTlsCert, $sTlsCA, - $sTlsCaPath, $sTlsCipher, $bUsePersistentConnection, true); + $sTlsCaPath, $sTlsCipher, true); } /** @@ -191,7 +188,6 @@ class CMDBSource * @param string $sTlsCa * @param string $sTlsCaPath * @param string $sTlsCipher - * @param boolean $bUsePersistentConnection {@see http://php.net/manual/en/mysqli.persistconns.php} * @param boolean $bCheckTlsAfterConnection * * @return \mysqli @@ -199,7 +195,7 @@ class CMDBSource */ public static function GetMysqliInstance( $sServer, $sUser, $sPwd, $sSource = '', $sTlsKey = null, $sTlsCert = null, $sTlsCa = null, $sTlsCaPath = null, - $sTlsCipher = null, $bUsePersistentConnection = false, $bCheckTlsAfterConnection = false + $sTlsCipher = null, $bCheckTlsAfterConnection = false ) { $oMysqli = null; @@ -207,10 +203,6 @@ class CMDBSource $iPort = null; $bTlsEnabled = self::IsDbConnectionUsingTls($sTlsKey, $sTlsCert, $sTlsCa); self::InitServerAndPort($sServer, $iPort); - if ($bUsePersistentConnection) - { - $sServer = 'p:'.$sServer; - } $iFlags = null; @@ -263,7 +255,7 @@ class CMDBSource } /** - * Initialize variables from the static attribute (containing "domain:port" syntax) + * Initialize variables from the static attribute (containing "p:domain:port" syntax) * * @param string $sServer * @param int $iPort @@ -271,16 +263,31 @@ class CMDBSource private static function InitServerAndPort(&$sServer, &$iPort) { $aConnectInfo = explode(':', self::$m_sDBHost); - if (count($aConnectInfo) > 1) + + $bUsePersistentConnection = false; + if (strcasecmp($aConnectInfo[0], 'p') == 0) { - // Override the default port - $sServer = $aConnectInfo[0]; - $iPort = (int)$aConnectInfo[1]; + // we might have "p:" prefix to use persistent connections (see http://php.net/manual/en/mysqli.persistconns.php) + $bUsePersistentConnection = true; + $sServer = $aConnectInfo[0].':'.$aConnectInfo[1]; } else { - $sServer = self::$m_sDBHost; - $iPort = null; + $sServer = $aConnectInfo[0]; + } + + $iConnectInfoCount = count($aConnectInfo); + if ($bUsePersistentConnection && ($iConnectInfoCount == 3)) + { + $iPort = $aConnectInfo[2]; + } + else if (!$bUsePersistentConnection && ($iConnectInfoCount == 2)) + { + $iPort = $aConnectInfo[1]; + } + else + { + $iPort = 3306; } } diff --git a/core/mutex.class.inc.php b/core/mutex.class.inc.php index e8aeaeeb7..6bb7ba627 100644 --- a/core/mutex.class.inc.php +++ b/core/mutex.class.inc.php @@ -245,7 +245,7 @@ class iTopMutex $sTlsCipher = $this->sDBTlsCipher; $this->hDBLink = CMDBSource::GetMysqliInstance($sServer, $sUser, $sPwd, $sSource, $sTlsKey, $sTlsCert, $sTlsCA, - $sTlsCaPath, $sTlsCipher, false, false); + $sTlsCaPath, $sTlsCipher, false); if (!$this->hDBLink) { diff --git a/setup/backup.class.inc.php b/setup/backup.class.inc.php index 5b79fd8fd..fe832f1dd 100644 --- a/setup/backup.class.inc.php +++ b/setup/backup.class.inc.php @@ -534,7 +534,7 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the " try { $oMysqli = CMDBSource::GetMysqliInstance($sServer, $sUser, $sPwd, $sSource, $sTlsKey, $sTlsCert, - $sTlsCA, $sTlsCaPath, $sTlsCipher, true, false); + $sTlsCA, $sTlsCaPath, $sTlsCipher, false); if ($oMysqli->connect_errno) {