Skip to content

Commit

Permalink
Return vCard exactly from storage if we don't need to convert.
Browse files Browse the repository at this point in the history
Fixes #834.
  • Loading branch information
evert committed May 29, 2016
1 parent 8a266c7 commit b4d4e50
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
ChangeLog
=========

3.1.4 (2016-??-??)
------------------

* #834: Backport from `master`: Return vCards exactly as they were stored if
we don't need to convert in between versions.


3.1.3 (2016-04-06)
------------------

Expand Down
56 changes: 36 additions & 20 deletions lib/CardDAV/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,33 +803,49 @@ protected function negotiateVCard($input, &$mimeType = null) {
/**
* Converts a vcard blob to a different version, or jcard.
*
* @param string $data
* @param string|resource $data
* @param string $target
* @return string
*/
protected function convertVCard($data, $target) {

$data = VObject\Reader::read($data);
switch ($target) {
default :
case 'vcard3' :
$data = $data->convert(VObject\Document::VCARD30);
$newResult = $data->serialize();
break;
case 'vcard4' :
$data = $data->convert(VObject\Document::VCARD40);
$newResult = $data->serialize();
break;
case 'jcard' :
$data = $data->convert(VObject\Document::VCARD40);
$newResult = json_encode($data->jsonSerialize());
break;

if (is_resource($data)) {
$data = stream_get_contents($data);
}
// Destroy circular references to PHP will GC the object.
$data->destroy();
$input = VObject\Reader::read($data);
$output = null;
try {

return $newResult;
switch ($target) {
default :
case 'vcard3' :
if ($input->getDocumentType() === VObject\Document::VCARD30) {
// Do nothing
return $data;
}
$output = $input->convert(VObject\Document::VCARD30);
return $output->serialize();
case 'vcard4' :
if ($input->getDocumentType() === VObject\Document::VCARD40) {
// Do nothing
return $data;
}
$output = $input->convert(VObject\Document::VCARD40);
return $output->serialize();
case 'jcard' :
$output = $input->convert(VObject\Document::VCARD40);

This comment has been minimized.

Copy link
@staabm

staabm May 29, 2016

Member

Is this the right constant for jcard?

This comment has been minimized.

Copy link
@evert

evert May 29, 2016

Author Member

Yea its a bit odd, but jCard uses the vCard4 data model. It's just a different serialization.

return json_encode($output);

}

} finally {

// Destroy circular references to PHP will GC the object.
$input->destroy();
if (!is_null($output)) {
$output->destroy();
}
}

}

Expand Down
16 changes: 8 additions & 8 deletions tests/Sabre/CardDAV/MultiGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ function testMultiGet() {

$result = $client->parseMultiStatus($response->body);

$this->assertEquals(array(
'/addressbooks/user1/book1/card1' => array(
200 => array(
'{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD") . '"',
'{urn:ietf:params:xml:ns:carddav}address-data' => "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:12345\r\nEND:VCARD\r\n",
)
)
), $result);
$this->assertEquals([
'/addressbooks/user1/book1/card1' => [
200 => [
'{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD") . '"',
'{urn:ietf:params:xml:ns:carddav}address-data' => "BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD",
]
]
], $result);

}

Expand Down

0 comments on commit b4d4e50

Please sign in to comment.