Skip to content

Commit

Permalink
[mms] Deprecated Horde_Mime::uudecode() and move to new Horde_Mime_Uu…
Browse files Browse the repository at this point in the history
…decode class.
  • Loading branch information
slusarz committed Sep 23, 2014
1 parent ca1cf03 commit 4fe8879
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 110 deletions.
24 changes: 24 additions & 0 deletions framework/Mime/doc/Horde/Mime/UPGRADING
Expand Up @@ -11,6 +11,30 @@
This lists the API changes between releases of the package.


Upgrading to 2.5
================

- Horde_Mime

- uudecode()

This method is deprecated. Use the Horde_Mime_Uudecode() class instead.

- Horde_Mime_Uudecode

This class was added.


Upgrading to 2.4
================

- Horde_Mime_Mail

- getRaw()

This method was added.


Upgrading to 2.3
================

Expand Down
67 changes: 5 additions & 62 deletions framework/Mime/lib/Horde/Mime.php
Expand Up @@ -640,72 +640,15 @@ static public function isChild($base, $id)
(strpos(strval($id), strval($base)) === 0));
}

/**
* Scans $input for uuencoded data and converts it to unencoded data.
*
* @param string $input The input data
*
* @return array A list of arrays, with each array corresponding to
* a file in the input and containing the following keys:
* - data: (string) Unencoded data.
* - name: (string) Filename.
* - perms: (string) Octal permissions.
*/
static public function uudecode($input)
{
$data = array();

/* Find all uuencoded sections. */
if (preg_match_all("/begin ([0-7]{3}) (.+)\r?\n(.+)\r?\nend/Us", $input, $matches, PREG_SET_ORDER)) {
reset($matches);
while (list(,$v) = each($matches)) {
$data[] = array(
'data' => self::_uudecode($v[3]),
'name' => $v[2],
'perm' => $v[1]
);
}
}

return $data;
}
/* Deprecated methods. */

/**
* PHP 5's built-in convert_uudecode() is broken. Need this wrapper.
*
* @param string $input UUencoded input.
*
* @return string Decoded string.
* @deprecated Use Horde_Mime_Uudecode instead.
*/
static protected function _uudecode($input)
static public function uudecode($input)
{
$decoded = '';

foreach (explode("\n", $input) as $line) {
$c = count($bytes = unpack('c*', substr(trim($line,"\r\n\t"), 1)));

while ($c % 4) {
$bytes[++$c] = 0;
}

foreach (array_chunk($bytes, 4) as $b) {
$b0 = ($b[0] == 0x60) ? 0 : $b[0] - 0x20;
$b1 = ($b[1] == 0x60) ? 0 : $b[1] - 0x20;
$b2 = ($b[2] == 0x60) ? 0 : $b[2] - 0x20;
$b3 = ($b[3] == 0x60) ? 0 : $b[3] - 0x20;

$b0 <<= 2;
$b0 |= ($b1 >> 4) & 0x03;
$b1 <<= 4;
$b1 |= ($b2 >> 2) & 0x0F;
$b2 <<= 6;
$b2 |= $b3 & 0x3F;

$decoded .= pack('c*', $b0, $b1, $b2);
}
}

return rtrim($decoded, "\0");
$uudecode = new Horde_Mime_Uudecode($input);
return iterator_to_array($input);
}

}
130 changes: 130 additions & 0 deletions framework/Mime/lib/Horde/Mime/Uudecode.php
@@ -0,0 +1,130 @@
<?php
/**
* Copyright 1999-2014 Horde LLC (http://www.horde.org/)
*
* -----
*
* This file contains code adapted from PEAR's PHP_Compat library (v1.6.0a3).
*
* http://pear.php.net/package/PHP_Compat
*
* This code was released under the LGPL 2.1
*
* -----
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2009-2014 Horde LLC
* @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mime
*/

/**
* Class used to uudecode data.
*
* Needed because PHP's built-in uudecode() method is (was) broken.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Aidan Lister <aidan@php.net>
* @author Michael Slusarz <slusarz@horde.org>
* @author Michael Wallner <mike@php.net>
* @category Horde
* @copyright 2009-2014 Horde LLC
* @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mime
* @since 2.5.0
*/
class Horde_Mime_Uudecode implements Countable, IteratorAggregate
{
const UUENCODE_REGEX = "/begin ([0-7]{3}) (.+)\r?\n(.+)\r?\nend/Us";

/**
* Uudecode data.
*
* A list of arrays, with each array corresponding to a file in the input
* and containing the following keys:
* - data: (string) Unencoded data.
* - name: (string) Filename.
* - perms: (string) Octal permissions.
*
* @var array
*/
protected $_data = array();

/**
* Scans $input for uuencoded data and converts it to unencoded data.
*
* @param string $input The input data
*/
public function __construct($input)
{
/* Find all uuencoded sections. */
if (preg_match_all(self::UUENCODE_REGEX, $input, $matches, PREG_SET_ORDER)) {
reset($matches);
while (list(,$v) = each($matches)) {
$this->_data[] = array(
'data' => $this->_uudecode($v[3]),
'name' => $v[2],
'perm' => $v[1]
);
}
}
}

/**
* PHP 5's built-in convert_uudecode() is broken. Need this wrapper.
*
* @param string $input UUencoded input.
*
* @return string Decoded string.
*/
protected function _uudecode($input)
{
$decoded = '';

foreach (explode("\n", $input) as $line) {
$c = count($bytes = unpack('c*', substr(trim($line,"\r\n\t"), 1)));

while ($c % 4) {
$bytes[++$c] = 0;
}

foreach (array_chunk($bytes, 4) as $b) {
$b0 = ($b[0] == 0x60) ? 0 : $b[0] - 0x20;
$b1 = ($b[1] == 0x60) ? 0 : $b[1] - 0x20;
$b2 = ($b[2] == 0x60) ? 0 : $b[2] - 0x20;
$b3 = ($b[3] == 0x60) ? 0 : $b[3] - 0x20;

$b0 <<= 2;
$b0 |= ($b1 >> 4) & 0x03;
$b1 <<= 4;
$b1 |= ($b2 >> 2) & 0x0F;
$b2 <<= 6;
$b2 |= $b3 & 0x3F;

$decoded .= pack('c*', $b0, $b1, $b2);
}
}

return rtrim($decoded, "\0");
}

/* Countable method. */

public function count()
{
return count($this->_data);
}

/* IteratorAggregate method. */

public function getIterator()
{
return new ArrayIterator($this->_data);
}

}
18 changes: 12 additions & 6 deletions framework/Mime/package.xml
Expand Up @@ -17,17 +17,18 @@
<email>slusarz@horde.org</email>
<active>yes</active>
</lead>
<date>2014-08-27</date>
<date>2014-09-20</date>
<version>
<release>2.4.6</release>
<api>2.4.0</api>
<release>2.5.0</release>
<api>2.5.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Deprecated Horde_Mime::uudecode() and move to new Horde_Mime_Uudecode class.
* [mms] Add Auto-Submitted header to outgoing MDN messages.
</notes>
<contents>
Expand All @@ -54,6 +55,7 @@
<file name="Translation.php" role="php">
<tasks:replace from="@data_dir@" to="data_dir" type="pear-config" />
</file>
<file name="Uudecode.php" role="php" />
</dir> <!-- /lib/Horde/Mime -->
<file name="Mime.php" role="php" />
</dir> <!-- /lib/Horde -->
Expand Down Expand Up @@ -350,6 +352,7 @@
<file name="PartTest.php" role="test" />
<file name="phpunit.xml" role="test" />
<file name="RelatedTest.php" role="test" />
<file name="UudecodeTest.php" role="test" />
</dir> <!-- /test/Horde/Mime -->
</dir> <!-- /test/Horde -->
</dir> <!-- /test -->
Expand Down Expand Up @@ -474,6 +477,7 @@
<install as="Horde/Mime/Part.php" name="lib/Horde/Mime/Part.php" />
<install as="Horde/Mime/Related.php" name="lib/Horde/Mime/Related.php" />
<install as="Horde/Mime/Translation.php" name="lib/Horde/Mime/Translation.php" />
<install as="Horde/Mime/Uudecode.php" name="lib/Horde/Mime/Uudecode.php" />
<install as="locale/Horde_Mime.pot" name="locale/Horde_Mime.pot" />
<install as="locale/ar/LC_MESSAGES/Horde_Mime.mo" name="locale/ar/LC_MESSAGES/Horde_Mime.mo" />
<install as="locale/ar/LC_MESSAGES/Horde_Mime.po" name="locale/ar/LC_MESSAGES/Horde_Mime.po" />
Expand Down Expand Up @@ -571,6 +575,7 @@
<install as="Horde/Mime/PartTest.php" name="test/Horde/Mime/PartTest.php" />
<install as="Horde/Mime/phpunit.xml" name="test/Horde/Mime/phpunit.xml" />
<install as="Horde/Mime/RelatedTest.php" name="test/Horde/Mime/RelatedTest.php" />
<install as="Horde/Mime/UudecodeTest.php" name="test/Horde/Mime/UudecodeTest.php" />
<install as="Horde/Mime/fixtures/attachment.bin" name="test/Horde/Mime/fixtures/attachment.bin" />
<install as="Horde/Mime/fixtures/blank_subject.txt" name="test/Horde/Mime/fixtures/blank_subject.txt" />
<install as="Horde/Mime/fixtures/bug12536.txt" name="test/Horde/Mime/fixtures/bug12536.txt" />
Expand Down Expand Up @@ -1422,14 +1427,15 @@
</release>
<release>
<version>
<release>2.4.6</release>
<api>2.4.0</api></version>
<release>2.5.0</release>
<api>2.5.0</api></version>
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2014-08-27</date>
<date>2014-09-20</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Deprecated Horde_Mime::uudecode() and move to new Horde_Mime_Uudecode class.
* [mms] Add Auto-Submitted header to outgoing MDN messages.
</notes>
</release>
Expand Down
42 changes: 0 additions & 42 deletions framework/Mime/test/Horde/Mime/MimeTest.php
Expand Up @@ -25,48 +25,6 @@ public function setUp()
Horde_Mime::$brokenRFC2231 = false;
}

public function testUudecode()
{
$data = Horde_Mime::uudecode(file_get_contents(__DIR__ . '/fixtures/uudecode.txt'));

$this->assertEquals(
2,
count($data)
);

$this->assertArrayHasKey('data', $data[0]);
$this->assertEquals(
'Test string',
$data[0]['data']
);
$this->assertArrayHasKey('name', $data[0]);
$this->assertEquals(
'test.txt',
$data[0]['name']
);
$this->assertArrayHasKey('perm', $data[0]);
$this->assertEquals(
'644',
$data[0]['perm']
);

$this->assertArrayHasKey('data', $data[1]);
$this->assertEquals(
'2nd string',
$data[1]['data']
);
$this->assertArrayHasKey('name', $data[1]);
$this->assertEquals(
'test2.txt',
$data[1]['name']
);
$this->assertArrayHasKey('perm', $data[1]);
$this->assertEquals(
'755',
$data[1]['perm']
);
}

public function testRfc2231()
{
// Horde_Mime RFC 2231 & workaround for broken MUA's
Expand Down

0 comments on commit 4fe8879

Please sign in to comment.