From eae907c4eda325180fac1bb4995f1c0f5ddffca6 Mon Sep 17 00:00:00 2001 From: Vladyslav Petrovych Date: Sat, 21 Sep 2013 14:40:33 +0300 Subject: [PATCH 1/3] fixes issue with logging array of non-utf8 data --- .../Doctrine/Tests/Logger/DbalLoggerTest.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php b/src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php index 872779c2f8..eacbd4f3c9 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php @@ -73,6 +73,37 @@ class DbalLoggerTest extends \PHPUnit_Framework_TestCase )); } + public function testLogNonUtf8Array() + { + $logger = $this->getMock('Psr\\Log\\LoggerInterface'); + + $dbalLogger = $this + ->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger') + ->setConstructorArgs(array($logger, null)) + ->setMethods(array('log')) + ->getMock() + ; + + $dbalLogger + ->expects($this->once()) + ->method('log') + ->with('SQL', array( + 'utf8' => 'foo', + array( + 'nonutf8' => DbalLogger::BINARY_DATA_VALUE, + ) + ) + ) + ; + + $dbalLogger->startQuery('SQL', array( + 'utf8' => 'foo', + array( + 'nonutf8' => "\x7F\xFF", + ) + )); + } + public function testLogLongString() { $logger = $this->getMock('Symfony\\Component\\HttpKernel\\Log\\LoggerInterface'); From 8edd4c6ffb52b3a87a30ad7a3d77ac3fc8066a02 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sat, 27 Jun 2015 18:19:06 -0400 Subject: [PATCH 2/3] No change - the normalizeParams is a copy-and-paste of the earlier logic --- .../Bridge/Doctrine/Logger/DbalLogger.php | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php b/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php index 617dd5c3fa..9908262f30 100644 --- a/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php +++ b/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php @@ -50,30 +50,7 @@ class DbalLogger implements SQLLogger } if (is_array($params)) { - foreach ($params as $index => $param) { - if (!is_string($params[$index])) { - continue; - } - - // non utf-8 strings break json encoding - if (!preg_match('//u', $params[$index])) { - $params[$index] = self::BINARY_DATA_VALUE; - continue; - } - - // detect if the too long string must be shorten - if (function_exists('mb_strlen')) { - if (self::MAX_STRING_LENGTH < mb_strlen($params[$index], 'UTF-8')) { - $params[$index] = mb_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]'; - continue; - } - } else { - if (self::MAX_STRING_LENGTH < strlen($params[$index])) { - $params[$index] = substr($params[$index], 0, self::MAX_STRING_LENGTH - 6).' [...]'; - continue; - } - } - } + $params = $this->normalizeParams($params); } if (null !== $this->logger) { @@ -101,4 +78,34 @@ class DbalLogger implements SQLLogger { $this->logger->debug($message, $params); } + + private function normalizeParams(array $params) + { + foreach ($params as $index => $param) { + if (!is_string($params[$index])) { + continue; + } + + // non utf-8 strings break json encoding + if (!preg_match('//u', $params[$index])) { + $params[$index] = self::BINARY_DATA_VALUE; + continue; + } + + // detect if the too long string must be shorten + if (function_exists('mb_strlen')) { + if (self::MAX_STRING_LENGTH < mb_strlen($params[$index], 'UTF-8')) { + $params[$index] = mb_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]'; + continue; + } + } else { + if (self::MAX_STRING_LENGTH < strlen($params[$index])) { + $params[$index] = substr($params[$index], 0, self::MAX_STRING_LENGTH - 6).' [...]'; + continue; + } + } + } + + return $params; + } } From 1882a2ae472a4fcdf846756f63460ef720c8df61 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sat, 27 Jun 2015 18:20:43 -0400 Subject: [PATCH 3/3] Normalizing recursively - see #9096 --- src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php b/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php index 9908262f30..2d834d7d40 100644 --- a/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php +++ b/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php @@ -82,6 +82,12 @@ class DbalLogger implements SQLLogger private function normalizeParams(array $params) { foreach ($params as $index => $param) { + // normalize recursively + if (is_array($param)) { + $params[$index] = $this->normalizeParams($param); + continue; + } + if (!is_string($params[$index])) { continue; }