From fa1d452e03a7522fbcf37b9649bd5b7421367c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 18 Apr 2016 12:21:23 +0200 Subject: [PATCH 1/4] Do not hide errors from database connection attempts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without these errors it's impossible to figure out what went wrong in the SSL negotiation. All you get is error 2002 without any means to diagnose and all important information is issued as PHP warnings. Issue #12146 Signed-off-by: Michal Čihař --- libraries/dbi/DBIMysqli.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/dbi/DBIMysqli.php b/libraries/dbi/DBIMysqli.php index 6f1584f026..f2f8a160dc 100644 --- a/libraries/dbi/DBIMysqli.php +++ b/libraries/dbi/DBIMysqli.php @@ -84,7 +84,7 @@ class DBIMysqli implements DBIExtension } if ($client_flags === null) { - return @mysqli_real_connect( + return mysqli_real_connect( $link, $host, $user, @@ -94,7 +94,7 @@ class DBIMysqli implements DBIExtension $server_socket ); } else { - return @mysqli_real_connect( + return mysqli_real_connect( $link, $host, $user, From ea428dc86002261bff653067cbf28dd6da895144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 18 Apr 2016 13:22:53 +0200 Subject: [PATCH 2/4] Make ErrorHandler test more complete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Actually test handling of stored errors. Signed-off-by: Michal Čihař --- test/classes/ErrorHandlerTest.php | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/test/classes/ErrorHandlerTest.php b/test/classes/ErrorHandlerTest.php index 4e31ce9f2a..008e325f54 100644 --- a/test/classes/ErrorHandlerTest.php +++ b/test/classes/ErrorHandlerTest.php @@ -184,16 +184,11 @@ class ErrorHandlerTest extends PMATestCase */ public function testCountErrors() { - - $err = array(); - $err[] = new PMA\libraries\Error('256', 'Compile Error', 'error.txt', 15); - $errHandler = $this->getMock('PMA\libraries\ErrorHandler'); - $errHandler->expects($this->any()) - ->method('getErrors') - ->will($this->returnValue($err)); - + $this->object->addError( + 'Compile Error', E_WARNING, 'error.txt', 15 + ); $this->assertEquals( - 0, + 1, $this->object->countErrors() ); } @@ -205,18 +200,20 @@ class ErrorHandlerTest extends PMATestCase */ public function testCountUserErrors() { - - $err = array(); - $err[] = new PMA\libraries\Error('256', 'Compile Error', 'error.txt', 15); - $errHandler = $this->getMock('PMA\libraries\ErrorHandler'); - $errHandler->expects($this->any()) - ->method('countErrors', 'getErrors') - ->will($this->returnValue(1, $err)); - + $this->object->addError( + 'Compile Error', E_WARNING, 'error.txt', 15 + ); $this->assertEquals( 0, $this->object->countUserErrors() ); + $this->object->addError( + 'Compile Error', E_USER_WARNING, 'error.txt', 15 + ); + $this->assertEquals( + 1, + $this->object->countUserErrors() + ); } /** From 951f1fed5f2123ad7e10f8d2b0779d5ec7cb8a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 18 Apr 2016 13:24:54 +0200 Subject: [PATCH 3/4] Add method to slice errors from the handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This can be useful to pop n-last errors. Signed-off-by: Michal Čihař --- libraries/ErrorHandler.php | 16 ++++++++++++++- test/classes/ErrorHandlerTest.php | 34 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/libraries/ErrorHandler.php b/libraries/ErrorHandler.php index 5fc72198dd..54f73ee4de 100644 --- a/libraries/ErrorHandler.php +++ b/libraries/ErrorHandler.php @@ -82,7 +82,7 @@ class ErrorHandler * * @return Error[] */ - protected function getErrors() + public function getErrors() { $this->checkSavedErrors(); return $this->errors; @@ -99,6 +99,20 @@ class ErrorHandler return $this->errors; } + /** + * Pops recent erros from the storage + * + * @param int $count Old error count + * + * @return Error[] + */ + public function sliceErrors($count) + { + $errors = $this->getErrors(); + $this->errors = array_splice($errors, 0, $count); + return array_splice($errors, $count); + } + /** * Error handler - called when errors are triggered/occurred * diff --git a/test/classes/ErrorHandlerTest.php b/test/classes/ErrorHandlerTest.php index 008e325f54..53b3e85dbc 100644 --- a/test/classes/ErrorHandlerTest.php +++ b/test/classes/ErrorHandlerTest.php @@ -193,6 +193,40 @@ class ErrorHandlerTest extends PMATestCase ); } + /** + * Test for sliceErrors + * + * @return void + * + * @group medium + */ + public function testSliceErrors() + { + $this->object->addError( + 'Compile Error', E_WARNING, 'error.txt', 15 + ); + $this->assertEquals( + 1, + $this->object->countErrors() + ); + $this->assertEquals( + array(), + $this->object->sliceErrors(1) + ); + $this->assertEquals( + 1, + $this->object->countErrors() + ); + $this->assertEquals( + 1, + count($this->object->sliceErrors(0)) + ); + $this->assertEquals( + 0, + $this->object->countErrors() + ); + } + /** * Test for countUserErrors * From cfbc31bfa62ea3841f766146adc06fecdfd38f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 18 Apr 2016 13:27:37 +0200 Subject: [PATCH 4/4] Better reporting of connection errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We really do not care about traceback or parameters in this case, all we need is to display the message coming from PHP. Signed-off-by: Michal Čihař --- libraries/DatabaseInterface.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libraries/DatabaseInterface.php b/libraries/DatabaseInterface.php index 87ab7eb181..c9183d1cdc 100644 --- a/libraries/DatabaseInterface.php +++ b/libraries/DatabaseInterface.php @@ -2351,10 +2351,22 @@ class DatabaseInterface $user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false ) { + $error_count = $GLOBALS['error_handler']->countErrors(); $result = $this->_extension->connect( $user, $password, $is_controluser, $server, $auxiliary_connection ); + /* Any errors from connection? */ + if ($GLOBALS['error_handler']->countErrors() > $error_count) { + $errors = $GLOBALS['error_handler']->sliceErrors($error_count); + foreach ($errors as $error) { + trigger_error( + $error->getMessage(), + E_USER_ERROR + ); + } + } + if ($result) { if (! $auxiliary_connection && ! $is_controluser) { $GLOBALS['dbi']->postConnect($result);