2121use function mysqli_init ;
2222use function mysqli_report ;
2323use function sprintf ;
24- use function stripos ;
25- use function trigger_error ;
24+ use function str_contains ;
25+ use function strtolower ;
2626
27- use const E_USER_ERROR ;
28- use const E_USER_WARNING ;
2927use const MYSQLI_CLIENT_COMPRESS ;
3028use const MYSQLI_CLIENT_SSL ;
3129use const MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT ;
@@ -106,35 +104,18 @@ public function connect(Server $server): Connection|null
106104 $ server ->socket ,
107105 $ clientFlags ,
108106 );
109- } catch (mysqli_sql_exception ) {
110- /**
111- * Switch to SSL if server asked us to do so, unfortunately
112- * there are more ways MySQL server can tell this:
113- *
114- * - MySQL 8.0 and newer should return error 3159
115- * - #2001 - SSL Connection is required. Please specify SSL options and retry.
116- * - #9002 - SSL connection is required. Please specify SSL options and retry.
117- */
118- // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
119- $ errorNumber = $ mysqli ->connect_errno ;
120- // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
121- $ errorMessage = $ mysqli ->connect_error ;
122- if (
123- ! $ server ->ssl
124- && ($ errorNumber == 3159
125- || (($ errorNumber == 2001 || $ errorNumber == 9002 )
126- && stripos ($ errorMessage , 'SSL Connection is required ' ) !== false ))
127- ) {
128- trigger_error (
129- __ ('SSL connection enforced by server, automatically enabling it. ' ),
130- E_USER_WARNING ,
131- );
107+ } catch (mysqli_sql_exception $ exception ) {
108+ $ errorNumber = $ exception ->getCode ();
109+ $ errorMessage = $ exception ->getMessage ();
132110
111+ if (! $ server ->ssl && $ this ->isSslRequiredByServer ($ errorNumber , $ errorMessage )) {
133112 return self ::connect ($ server ->withSSL (true ));
134113 }
135114
115+ mysqli_report (MYSQLI_REPORT_OFF );
116+
136117 if ($ errorNumber === 1045 && $ server ->hideConnectionErrors ) {
137- trigger_error (
118+ throw new ConnectionException (
138119 sprintf (
139120 __ (
140121 'Error 1045: Access denied for user. Additional error information '
@@ -143,15 +124,12 @@ public function connect(Server $server): Connection|null
143124 '[code][doc@cfg_Servers_hide_connection_errors] '
144125 . '$cfg[ \'Servers \'][$i][ \'hide_connection_errors \'][/doc][/code] ' ,
145126 ),
146- E_USER_ERROR ,
127+ $ errorNumber ,
128+ $ exception ,
147129 );
148- } else {
149- trigger_error ($ errorNumber . ': ' . $ errorMessage , E_USER_WARNING );
150130 }
151131
152- mysqli_report (MYSQLI_REPORT_OFF );
153-
154- return null ;
132+ throw new ConnectionException ($ errorNumber . ': ' . $ errorMessage , $ errorNumber , $ exception );
155133 }
156134
157135 $ mysqli ->options (MYSQLI_OPT_LOCAL_INFILE , (int ) defined ('PMA_ENABLE_LDI ' ));
@@ -356,4 +334,19 @@ public function getWarningCount(Connection $connection): int
356334 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
357335 return $ mysqli ->warning_count ;
358336 }
337+
338+ /**
339+ * Switch to SSL if server asked us to do so, unfortunately
340+ * there are more ways MySQL server can tell this:
341+ *
342+ * - MySQL 8.0 and newer should return error 3159
343+ * - #2001 - SSL Connection is required. Please specify SSL options and retry.
344+ * - #9002 - SSL connection is required. Please specify SSL options and retry.
345+ */
346+ private function isSslRequiredByServer (int $ errorNumber , string $ errorMessage ): bool
347+ {
348+ return $ errorNumber === 3159
349+ || ($ errorNumber === 2001 || $ errorNumber === 9002 )
350+ && str_contains (strtolower ($ errorMessage ), 'ssl connection is required ' );
351+ }
359352}
0 commit comments