Skip to content

Commit

Permalink
Allow Horde_Mime to be unserialized from the previous version
Browse files Browse the repository at this point in the history
Certain things (i.e. Alarms) have hard coded Horde_Mime serialized
objects as the data storage.  To work around these design decisions, it
is necessary to put into the effort to allow that data to be upgraded
instead of discarded
  • Loading branch information
slusarz committed Feb 16, 2015
1 parent f4d0c92 commit db0776b
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 3 deletions.
15 changes: 14 additions & 1 deletion framework/Mime/lib/Horde/Mime/Part.php
Expand Up @@ -2268,7 +2268,20 @@ public function unserialize($data)
if (!is_array($data) ||
!isset($data[0]) ||
($data[0] != self::VERSION)) {
throw new Exception('Cache version change');
switch ($data[0]) {
case 1:
$convert = new Horde_Mime_Part_Upgrade_V1($data);
$data = $convert->data;
break;

default:
$data = null;
break;
}

if (is_null($data)) {
throw new Exception('Cache version change');
}
}

$key = 0;
Expand Down
118 changes: 118 additions & 0 deletions framework/Mime/lib/Horde/Mime/Part/Upgrade/V1.php
@@ -0,0 +1,118 @@
<?php
/**
* Copyright 2015 Horde LLC (http://www.horde.org/)
*
* 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 2015 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mime
*/

/**
* Upgrade V1 of MIME serialized data to V2.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2015 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mime
* @since 2.8.0
*/
class Horde_Mime_Part_Upgrade_V1
{
/**
* Converted data.
*
* @var array
*/
public $data = null;

/**
* Constructor.
*
* @param array $data V1 data.
*/
public function __construct($data)
{
// Version number
array_shift($data);

$d = array();

$type = array_shift($data);
$subtype = array_shift($data);

$ct = Horde_Mime_Headers_ContentParam_ContentType::create();
$d[4] = new Horde_Mime_Headers();
$d[4]->addHeaderOb($ct);
$ct->setContentParamValue($type . '/' . $subtype);

$d[9] = array_shift($data);

if ($lang = array_shift($data)) {
$d[4]->addHeaderOb(
new Horde_Mime_Headers_ContentLanguage('', $lang)
);
}

if ($cd = array_shift($data)) {
$hdr = new Horde_Mime_Headers_ContentDescription(null, '');
$d[4]->addHeaderOb($hdr);
$hdr->setValue($cd);
}

$cd = new Horde_Mime_Headers_ContentParam_ContentDisposition(null, '');
$d[4]->addHeaderOb($cd);
$cd->setContentParamValue(array_shift($data));

foreach (array_shift($data) as $key => $val) {
$cd[$key] = $val;
}

foreach (array_shift($data) as $key => $val) {
$ct[$key] = $val;
}

$d[7] = array_shift($data);
$d[6] = array_shift($data);
$d[2] = array_shift($data);
$d[5] = array_shift($data);

if ($boundary = array_shift($data)) {
$ct['boundary'] = $boundary;
}

$d[1] = array_shift($data);

if ($cid = array_shift($data)) {
$hdr = new Horde_Mime_Headers_ContentId(null, $cid);
$d[4]->addHeaderOb($hdr);
}

if ($cd = array_shift($data)) {
$hdr = new Horde_Mime_Headers_Element_Single('Content-Duration', '');
$d[4]->addHeaderOb($hdr);
$hdr->setValue($cd);
}

$d[8] = 0;
if (array_shift($data)) {
$d[8] |= STATUS_REINDEX;
}
if (array_shift($data)) {
$d[8] |= STATUS_BASEPART;
}

$d[3] = array_shift($data);

if (count($data)) {
$d[10] = reset($data);
}

$this->data = $d;
}

}
12 changes: 10 additions & 2 deletions framework/Mime/package.xml
Expand Up @@ -17,7 +17,7 @@
<email>slusarz@horde.org</email>
<active>yes</active>
</lead>
<date>2015-01-22</date>
<date>2015-02-16</date>
<version>
<release>2.8.0</release>
<api>2.8.0</api>
Expand Down Expand Up @@ -98,6 +98,11 @@
<file name="Subject.php" role="php" />
<file name="UserAgent.php" role="php" />
</dir> <!-- /lib/Horde/Mime/Headers -->
<dir name="Part">
<dir name="Upgrade">
<file name="V1.php" role="php" />
</dir> <!-- /lib/Horde/Mime/Part/Upgrade -->
</dir> <!-- /lib/Horde/Mime/Part -->
<file name="Exception.php" role="php" />
<file name="Headers.php" role="php" />
<file name="Id.php" role="php" />
Expand Down Expand Up @@ -390,6 +395,7 @@
<file name="deeply_nested.txt" role="test" />
<file name="flowed_msg.txt" role="test" />
<file name="header_trailing_ws.txt" role="test" />
<file name="mime_part_v1.txt" role="test" />
<file name="multiple_to.txt" role="test" />
<file name="related_msg.txt" role="test" />
<file name="related_msg_2.txt" role="test" />
Expand Down Expand Up @@ -592,6 +598,7 @@
<install as="Horde/Mime/Headers/Element/Multiple.php" name="lib/Horde/Mime/Headers/Element/Multiple.php" />
<install as="Horde/Mime/Headers/Element/Single.php" name="lib/Horde/Mime/Headers/Element/Single.php" />
<install as="Horde/Mime/Headers/Extension/Mime.php" name="lib/Horde/Mime/Headers/Extension/Mime.php" />
<install as="Horde/Mime/Part/Upgrade/V1.php" name="lib/Horde/Mime/Part/Upgrade/V1.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 @@ -701,6 +708,7 @@
<install as="Horde/Mime/fixtures/deeply_nested.txt" name="test/Horde/Mime/fixtures/deeply_nested.txt" />
<install as="Horde/Mime/fixtures/flowed_msg.txt" name="test/Horde/Mime/fixtures/flowed_msg.txt" />
<install as="Horde/Mime/fixtures/header_trailing_ws.txt" name="test/Horde/Mime/fixtures/header_trailing_ws.txt" />
<install as="Horde/Mime/fixtures/mime_part_v1.txt" name="test/Horde/Mime/fixtures/mime_part_v1.txt" />
<install as="Horde/Mime/fixtures/multiple_to.txt" name="test/Horde/Mime/fixtures/multiple_to.txt" />
<install as="Horde/Mime/fixtures/related_msg.txt" name="test/Horde/Mime/fixtures/related_msg.txt" />
<install as="Horde/Mime/fixtures/related_msg_2.txt" name="test/Horde/Mime/fixtures/related_msg_2.txt" />
Expand Down Expand Up @@ -1656,7 +1664,7 @@
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2015-01-22</date>
<date>2015-02-16</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Work around broken line-oriented data when transfer encoding (Bug #13835).
Expand Down
89 changes: 89 additions & 0 deletions framework/Mime/test/Horde/Mime/PartTest.php
Expand Up @@ -837,6 +837,95 @@ public function testNullCharactersNotAllowedInMimeHeaderData()
);
}

public function testSerializeUpgradeFromVersion1()
{
$data = base64_decode(
file_get_contents(__DIR__ . '/fixtures/mime_part_v1.txt')
);

$part = unserialize($data);

$this->assertEquals(
'text/plain',
$part->getType()
);

$this->assertEquals(
array('en'),
$part->getLanguage()
);

$this->assertEquals(
'foo',
$part->getDescription()
);

$this->assertEquals(
'attachment',
$part->getDisposition()
);

$this->assertEquals(
'bar',
$part->getDispositionParameter('foo')
);

$this->assertEquals(
'foo',
$part->getDispositionParameter('filename')
);

$this->assertEquals(
'foo',
$part->getContentTypeParameter('name')
);

$this->assertEquals(
'bar',
$part->getContentTypeParameter('foo')
);

$this->assertEquals(
'us-ascii',
$part->getCharset()
);

$this->assertEquals(
array(),
$part->getParts()
);

$this->assertEquals(
'1',
$part->getMimeId()
);

$this->assertEquals(
"\n",
$part->getEOL()
);

$this->assertEquals(
'bar',
$part->getMetadata('foo')
);

$this->assertEquals(
'svl8CVtZEEO5bgqR-wFIFQ8@bigworm.curecanti.org',
$part->getContentId()
);

$this->assertEquals(
10,
$part->getDuration()
);

$this->assertEquals(
'foo',
$part->getContents()
);
}

protected function _getTestPart()
{
$part = new Horde_Mime_Part();
Expand Down
1 change: 1 addition & 0 deletions framework/Mime/test/Horde/Mime/fixtures/mime_part_v1.txt
@@ -0,0 +1 @@
QzoxNToiSG9yZGVfTWltZV9QYXJ0Ijo0NDQ6e2E6MjE6e2k6MDtpOjE7aToxO3M6NDoidGV4dCI7aToyO3M6NToicGxhaW4iO2k6MztzOjQ6IjdiaXQiO2k6NDthOjE6e2k6MDtzOjI6ImVuIjt9aTo1O3M6MzoiZm9vIjtpOjY7czoxMDoiYXR0YWNobWVudCI7aTo3O2E6Mjp7czozOiJmb28iO3M6MzoiYmFyIjtzOjg6ImZpbGVuYW1lIjtzOjM6ImZvbyI7fWk6ODthOjM6e3M6NDoibmFtZSI7czozOiJmb28iO3M6NzoiY2hhcnNldCI7czo4OiJ1cy1hc2NpaSI7czozOiJmb28iO3M6MzoiYmFyIjt9aTo5O2E6MDp7fWk6MTA7czoxOiIxIjtpOjExO3M6MToiCiI7aToxMjthOjE6e3M6MzoiZm9vIjtzOjM6ImJhciI7fWk6MTM7TjtpOjE0O047aToxNTtzOjQ1OiJzdmw4Q1Z0WkVFTzViZ3FSLXdGSUZROEBiaWd3b3JtLmN1cmVjYW50aS5vcmciO2k6MTY7aToxMDtpOjE3O2I6MDtpOjE4O2I6MDtpOjE5O047aToyMDtzOjM6ImZvbyI7fX0K

0 comments on commit db0776b

Please sign in to comment.