From 536c7872368bff6ef1123c2b8ee17c8f7bc984ad Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Sat, 20 May 2017 18:17:59 +0200 Subject: [PATCH] [+]: use "symfony/polyfill" for old php-versions --- lib/classes/Swift/FileSpool.php | 4 +- .../Mime/Headers/ParameterizedHeader.php | 28 ++++---- .../Esmtp/Auth/NTLMAuthenticator.php | 66 +++++-------------- tests/unit/Swift/Plugins/LoggerPluginTest.php | 4 +- tests/unit/Swift/Signers/SMimeSignerTest.php | 9 ++- 5 files changed, 43 insertions(+), 68 deletions(-) diff --git a/lib/classes/Swift/FileSpool.php b/lib/classes/Swift/FileSpool.php index f7386c007..c536af253 100644 --- a/lib/classes/Swift/FileSpool.php +++ b/lib/classes/Swift/FileSpool.php @@ -196,7 +196,7 @@ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null * Returns a random string needed to generate a fileName for the queue. * * @param int|null $count null use only "uniqid()"
- * int use "mt_rand()" with a fixed- / fs-safe string + * int use "random_int()" with a fixed- / fs-safe string * * @return string */ @@ -211,7 +211,7 @@ protected function getRandomString($count = null) $ret = ''; $strlen = strlen($base) - 1; for ($i = 0; $i < $count; ++$i) { - $ret .= $base[(int)mt_rand(0, $strlen)]; + $ret .= $base[\random_int(0, $strlen - 1)]; } return $ret; diff --git a/lib/classes/Swift/Mime/Headers/ParameterizedHeader.php b/lib/classes/Swift/Mime/Headers/ParameterizedHeader.php index 5a140b2f3..825e38e01 100644 --- a/lib/classes/Swift/Mime/Headers/ParameterizedHeader.php +++ b/lib/classes/Swift/Mime/Headers/ParameterizedHeader.php @@ -8,6 +8,8 @@ * file that was distributed with this source code. */ +use voku\helper\UTF8; + /** * An abstract base MIME Header. * @@ -25,7 +27,7 @@ class Swift_Mime_Headers_ParameterizedHeader extends Swift_Mime_Headers_Unstruct /** * The Encoder used to encode the parameters. * - * @var Swift_Encoder + * @var Swift_Mime_HeaderEncoder_Base64HeaderEncoder */ private $_paramEncoder; @@ -124,9 +126,11 @@ public function getParameters() /** * Get the value of this header prepared for rendering. * + * //TODO: Check caching here + * * @return string */ - public function getFieldBody() //TODO: Check caching here + public function getFieldBody() { $body = parent::getFieldBody(); foreach ($this->_params as $name => $value) { @@ -185,17 +189,19 @@ private function _createParameter($name, $value) $maxValueLength = $this->getMaxLineLength() - strlen($name . '=*N"";') - 1; $firstLineOffset = 0; - // If it's not already a valid parameter value ... - if (!preg_match('/^' . self::TOKEN_REGEX . '$/D', $value)) { - // TODO: text, or something else?? + + if ( + // If it's not already a valid parameter value ... + !preg_match('/^' . self::TOKEN_REGEX . '$/D', $value) + && // ... and it's not ascii - if (!preg_match('/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $value)) { - $encoded = true; + !UTF8::is_ascii($value) + ) { + $encoded = true; - // Allow space for the indices, charset and language. - $maxValueLength = $this->getMaxLineLength() - strlen($name . '*N*="";') - 1; - $firstLineOffset = strlen($this->getCharset() . "'" . $this->getLanguage() . "'"); - } + // Allow space for the indices, charset and language. + $maxValueLength = $this->getMaxLineLength() - strlen($name . '*N*="";') - 1; + $firstLineOffset = strlen($this->getCharset() . "'" . $this->getLanguage() . "'"); } // Encode if we need to ... diff --git a/lib/classes/Swift/Transport/Esmtp/Auth/NTLMAuthenticator.php b/lib/classes/Swift/Transport/Esmtp/Auth/NTLMAuthenticator.php index 5779188e2..0dab0edd9 100644 --- a/lib/classes/Swift/Transport/Esmtp/Auth/NTLMAuthenticator.php +++ b/lib/classes/Swift/Transport/Esmtp/Auth/NTLMAuthenticator.php @@ -56,7 +56,7 @@ public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $passw // extra parameters for our unit cases $timestamp = func_num_args() > 3 ? func_get_arg(3) : $this->getCorrectTimestamp(bcmul(microtime(true), '1000')); - $client = func_num_args() > 4 ? func_get_arg(4) : $this->getRandomBytes(8); + $client = func_num_args() > 4 ? func_get_arg(4) : \random_bytes(8); // Message 3 response $this->sendMessage3($response, $username, $password, $timestamp, $client, $agent); @@ -71,6 +71,9 @@ public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $passw /** * @param string $si + * @param int $bits + * + * @return null|string */ protected function si2bin($si, $bits = 32) { @@ -124,10 +127,10 @@ protected function parseMessage2($response) $responseHex = bin2hex($response); $length = floor(hexdec(substr($responseHex, 28, 4)) / 256) * 2; $offset = floor(hexdec(substr($responseHex, 32, 4)) / 256) * 2; - $challenge = $this->hex2bin(substr($responseHex, 48, 16)); - $context = $this->hex2bin(substr($responseHex, 64, 16)); - $targetInfoH = $this->hex2bin(substr($responseHex, 80, 16)); - $targetName = $this->hex2bin(substr($responseHex, $offset, $length)); + $challenge = \hex2bin(substr($responseHex, 48, 16)); + $context = \hex2bin(substr($responseHex, 64, 16)); + $targetInfoH = \hex2bin(substr($responseHex, 80, 16)); + $targetName = \hex2bin(substr($responseHex, $offset, $length)); $offset = floor(hexdec(substr($responseHex, 88, 4)) / 256) * 2; $targetInfoBlock = substr($responseHex, $offset); list($domainName, $serverName, $DNSDomainName, $DNSServerName, $terminatorByte) = $this->readSubBlock($targetInfoBlock); @@ -141,7 +144,7 @@ protected function parseMessage2($response) $serverName, $DNSDomainName, $DNSServerName, - $this->hex2bin($targetInfoBlock), + \hex2bin($targetInfoBlock), $terminatorByte, ); } @@ -164,7 +167,7 @@ protected function readSubBlock($block) while ($offset < $length) { $blockLength = hexdec(substr(substr($block, $offset, 8), -4)) / 256; $offset += 8; - $data[] = $this->hex2bin(substr($block, $offset, $blockLength * 2)); + $data[] = \hex2bin(substr($block, $offset, $blockLength * 2)); $offset += $blockLength * 2; } @@ -470,7 +473,7 @@ protected function createDesKey($key) } } - return $this->hex2bin(implode('', $material)); + return \hex2bin(implode('', $material)); } /** HELPER FUNCTIONS */ @@ -550,7 +553,7 @@ protected function uRShift($a, $b) protected function createByte($input, $bytes = 4, $isHex = true) { if ($isHex) { - $byte = $this->hex2bin(str_pad($input, $bytes * 2, '00')); + $byte = \hex2bin(str_pad($input, $bytes * 2, '00')); } else { $byte = str_pad($input, $bytes, "\x00"); } @@ -558,24 +561,6 @@ protected function createByte($input, $bytes = 4, $isHex = true) return $byte; } - /** - * Create random bytes. - * - * @param integer $length - * - * @return string - */ - protected function getRandomBytes($length) - { - $bytes = openssl_random_pseudo_bytes($length, $strong); - - if (false !== $bytes && true === $strong) { - return $bytes; - } - - throw new RuntimeException('OpenSSL did not produce a secure random number.'); - } - /** ENCRYPTION ALGORITHMS */ /** @@ -627,7 +612,7 @@ protected function md4Encrypt($input) { $input = $this->convertTo16bit($input); - return function_exists('hash') ? $this->hex2bin(hash('md4', $input)) : mhash(MHASH_MD4, $input); + return function_exists('hash') ? \hex2bin(hash('md4', $input)) : mhash(MHASH_MD4, $input); } /** @@ -642,27 +627,6 @@ protected function convertTo16bit($input) return iconv('UTF-8', 'UTF-16LE', $input); } - /** - * Hex2bin replacement for < PHP 5.4. - * - * TODO: use the next code, when the bug-fix, is tagged - * -> https://github.com/symfony/polyfill/commit/ad8296bf38a40b0b3e8cdfc31e0a04f025f0d99a - * - * if (function_exists('hex2bin')) { - * return hex2bin($hex); - * } else { - * return pack('H*', $hex); - * } - * - * @param string $hex - * - * @return string Binary - */ - public function hex2bin($hex) - { - return pack('H*', $hex); - } - /** * @param string $message */ @@ -687,7 +651,7 @@ protected function debug($message) 'Target Information Terminator', ); - $data = $this->parseMessage2($this->hex2bin($message)); + $data = $this->parseMessage2(\hex2bin($message)); foreach ($map as $key => $value) { echo bin2hex($data[$key]) . ' - ' . $data[$key] . ' ||| ' . $value . "
\n"; @@ -733,7 +697,7 @@ protected function debug($message) ); foreach ($map as $key => $value) { - echo $data[$key] . ' - ' . $this->hex2bin($data[$key]) . ' ||| ' . $value . "
\n"; + echo $data[$key] . ' - ' . \hex2bin($data[$key]) . ' ||| ' . $value . "
\n"; } } diff --git a/tests/unit/Swift/Plugins/LoggerPluginTest.php b/tests/unit/Swift/Plugins/LoggerPluginTest.php index 2b477b410..d837313ea 100644 --- a/tests/unit/Swift/Plugins/LoggerPluginTest.php +++ b/tests/unit/Swift/Plugins/LoggerPluginTest.php @@ -40,7 +40,7 @@ public function testCommandIsSentToLogger() $logger = $this->_createLogger(); $logger->expects($this->once()) ->method('add') - ->with($this->regExp('~foo\r\n~')); + ->with(static::regExp('~foo\r\n~')); $plugin = $this->_createPlugin($logger); $plugin->commandSent($evt); @@ -52,7 +52,7 @@ public function testResponseIsSentToLogger() $logger = $this->_createLogger(); $logger->expects($this->once()) ->method('add') - ->with($this->regExp('~354 Go ahead\r\n~')); + ->with(static::regExp('~354 Go ahead\r\n~')); $plugin = $this->_createPlugin($logger); $plugin->responseReceived($evt); diff --git a/tests/unit/Swift/Signers/SMimeSignerTest.php b/tests/unit/Swift/Signers/SMimeSignerTest.php index 553cbe524..e479396de 100644 --- a/tests/unit/Swift/Signers/SMimeSignerTest.php +++ b/tests/unit/Swift/Signers/SMimeSignerTest.php @@ -1,5 +1,8 @@