Skip to content

Commit

Permalink
[+]: use "symfony/polyfill" for old php-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed May 20, 2017
1 parent f5621b0 commit 536c787
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 68 deletions.
4 changes: 2 additions & 2 deletions lib/classes/Swift/FileSpool.php
Expand Up @@ -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 <strong>null</strong> use only "uniqid()"<br />
* <strong>int</strong> use "mt_rand()" with a fixed- / fs-safe string
* <strong>int</strong> use "random_int()" with a fixed- / fs-safe string
*
* @return string
*/
Expand All @@ -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;
Expand Down
28 changes: 17 additions & 11 deletions lib/classes/Swift/Mime/Headers/ParameterizedHeader.php
Expand Up @@ -8,6 +8,8 @@
* file that was distributed with this source code.
*/

use voku\helper\UTF8;

/**
* An abstract base MIME Header.
*
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 ...
Expand Down
66 changes: 15 additions & 51 deletions lib/classes/Swift/Transport/Esmtp/Auth/NTLMAuthenticator.php
Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -141,7 +144,7 @@ protected function parseMessage2($response)
$serverName,
$DNSDomainName,
$DNSServerName,
$this->hex2bin($targetInfoBlock),
\hex2bin($targetInfoBlock),
$terminatorByte,
);
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -470,7 +473,7 @@ protected function createDesKey($key)
}
}

return $this->hex2bin(implode('', $material));
return \hex2bin(implode('', $material));
}

/** HELPER FUNCTIONS */
Expand Down Expand Up @@ -550,32 +553,14 @@ 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");
}

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 */

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
*/
Expand All @@ -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 . "<br />\n";
Expand Down Expand Up @@ -733,7 +697,7 @@ protected function debug($message)
);

foreach ($map as $key => $value) {
echo $data[$key] . ' - ' . $this->hex2bin($data[$key]) . ' ||| ' . $value . "<br />\n";
echo $data[$key] . ' - ' . \hex2bin($data[$key]) . ' ||| ' . $value . "<br />\n";
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Swift/Plugins/LoggerPluginTest.php
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/Swift/Signers/SMimeSignerTest.php
@@ -1,5 +1,8 @@
<?php

/**
* Class Swift_Signers_SMimeSignerTest
*/
class Swift_Signers_SMimeSignerTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -499,8 +502,9 @@ protected static function getBodyOfMessage($message)
/**
* Strips of the sender headers and Mime-Version.
*
* @param Swift_ByteStream_TemporaryFileByteStream $messageStream
* @param Swift_ByteStream_TemporaryFileByteStream $inputStream
* @param string $content
*
* @return string
*/
protected function cleanMessage($content)
{
Expand Down Expand Up @@ -549,6 +553,7 @@ protected static function getHeadersOfMessage($message)

foreach ($headerLines as $headerLine) {
if (ctype_space($headerLines[0]) || false === strpos($headerLine, ':')) {
// TODO; $currentHeaderName is undefined, maybe ...
$headers[$currentHeaderName] .= ' '.trim($headerLine);
continue;
}
Expand Down

0 comments on commit 536c787

Please sign in to comment.