Skip to content

Commit e25f1bb

Browse files
committed
Return vCard exactly from storage if we don't need to convert.
Fixes #834.
1 parent 3025391 commit e25f1bb

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ ChangeLog
55
-------------------------
66

77
* #833: Calendars throw exceptions when the sharing plugin is not enabled.
8+
* #834: Return vCards exactly as they were stored if we don't need to convert
9+
in between versions.
810

911

1012
3.2.0-alpha1 (2016-05-09)

lib/CardDAV/Plugin.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -843,33 +843,49 @@ protected function negotiateVCard($input, &$mimeType = null) {
843843
/**
844844
* Converts a vcard blob to a different version, or jcard.
845845
*
846-
* @param string $data
846+
* @param string|resource $data
847847
* @param string $target
848848
* @return string
849849
*/
850850
protected function convertVCard($data, $target) {
851851

852-
$data = VObject\Reader::read($data);
853-
switch ($target) {
854-
default :
855-
case 'vcard3' :
856-
$data = $data->convert(VObject\Document::VCARD30);
857-
$newResult = $data->serialize();
858-
break;
859-
case 'vcard4' :
860-
$data = $data->convert(VObject\Document::VCARD40);
861-
$newResult = $data->serialize();
862-
break;
863-
case 'jcard' :
864-
$data = $data->convert(VObject\Document::VCARD40);
865-
$newResult = json_encode($data->jsonSerialize());
866-
break;
867-
852+
if (is_resource($data)) {
853+
$data = stream_get_contents($data);
868854
}
869-
// Destroy circular references to PHP will GC the object.
870-
$data->destroy();
855+
$input = VObject\Reader::read($data);
856+
$output = null;
857+
try {
871858

872-
return $newResult;
859+
switch ($target) {
860+
default :
861+
case 'vcard3' :
862+
if ($input->getDocumentType() === VObject\Document::VCARD30) {
863+
// Do nothing
864+
return $data;
865+
}
866+
$output = $input->convert(VObject\Document::VCARD30);
867+
return $output->serialize();
868+
case 'vcard4' :
869+
if ($input->getDocumentType() === VObject\Document::VCARD40) {
870+
// Do nothing
871+
return $data;
872+
}
873+
$output = $input->convert(VObject\Document::VCARD40);
874+
return $output->serialize();
875+
case 'jcard' :
876+
$output = $input->convert(VObject\Document::VCARD40);
877+
return json_encode($output);
878+
879+
}
880+
881+
} finally {
882+
883+
// Destroy circular references to PHP will GC the object.
884+
$input->destroy();
885+
if (!is_null($output)) {
886+
$output->destroy();
887+
}
888+
}
873889

874890
}
875891

tests/Sabre/CardDAV/MultiGetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function testMultiGet() {
4545
'/addressbooks/user1/book1/card1' => [
4646
200 => [
4747
'{DAV:}getetag' => '"' . md5("BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD") . '"',
48-
'{urn:ietf:params:xml:ns:carddav}address-data' => "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:12345\r\nEND:VCARD\r\n",
48+
'{urn:ietf:params:xml:ns:carddav}address-data' => "BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD",
4949
]
5050
]
5151
], $result);

0 commit comments

Comments
 (0)