Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow URI as data for vcard PHOTO #13117

Merged
merged 3 commits into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/dav/appinfo/v1/carddav.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@

$server->addPlugin(new \Sabre\DAV\Sync\Plugin());
$server->addPlugin(new \Sabre\CardDAV\VCFExportPlugin());
$server->addPlugin(new \OCA\DAV\CardDAV\ImageExportPlugin(new \OCA\DAV\CardDAV\PhotoCache(\OC::$server->getAppDataDir('dav-photocache'))));
$server->addPlugin(new \OCA\DAV\CardDAV\ImageExportPlugin(new \OCA\DAV\CardDAV\PhotoCache(
\OC::$server->getAppDataDir('dav-photocache'),
\OC::$server->getLogger()
)));
$server->addPlugin(new ExceptionLoggerPlugin('carddav', \OC::$server->getLogger()));

// And off we go!
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public function __construct() {

$container->registerService(PhotoCache::class, function(SimpleContainer $s) use ($server) {
return new PhotoCache(
$server->getAppDataDir('dav-photocache')
$server->getAppDataDir('dav-photocache'),
$server->getLogger()
);
});

Expand Down
40 changes: 26 additions & 14 deletions apps/dav/lib/CardDAV/PhotoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\DAV\CardDAV;

use OCP\Files\IAppData;
use OCP\ILogger;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
Expand All @@ -34,16 +35,21 @@

class PhotoCache {

/** @var IAppData $appData */
/** @var IAppData */
protected $appData;

/** @var ILogger */
protected $logger;

/**
* PhotoCache constructor.
*
* @param IAppData $appData
* @param ILogger $logger
*/
public function __construct(IAppData $appData) {
public function __construct(IAppData $appData, ILogger $logger) {
$this->appData = $appData;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -134,13 +140,14 @@ private function getFile(ISimpleFolder $folder, $size) {

$ratio = $photo->width() / $photo->height();
if ($ratio < 1) {
$ratio = 1/$ratio;
$ratio = 1 / $ratio;
}
$size = (int)($size * $ratio);

$size = (int) ($size * $ratio);
if ($size !== -1) {
$photo->resize($size);
}

try {
$file = $folder->newFile($path);
$file->putContent($photo->data());
Expand All @@ -152,7 +159,6 @@ private function getFile(ISimpleFolder $folder, $size) {
return $file;
}


/**
* @param int $addressBookId
* @param string $cardUri
Expand Down Expand Up @@ -193,19 +199,23 @@ private function getPhoto(Card $node) {
}

$photo = $vObject->PHOTO;
$type = $this->getType($photo);

$val = $photo->getValue();

// handle data URI. e.g PHOTO;VALUE=URI:data:image/jpeg;base64,/9j/4AAQSkZJRgABAQE
if ($photo->getValueType() === 'URI') {
$parsed = \Sabre\URI\parse($val);
skjnldsv marked this conversation as resolved.
Show resolved Hide resolved
//only allow data://

// only allow data://
if ($parsed['scheme'] !== 'data') {
return false;
}
if (substr_count($parsed['path'], ';') === 1) {
list($type,) = explode(';', $parsed['path']);
list($type) = explode(';', $parsed['path']);
}
$val = file_get_contents($val);
skjnldsv marked this conversation as resolved.
Show resolved Hide resolved
} else {
// get type if binary data
$type = $this->getBinaryType($photo);
}

$allowedContentTypes = [
Expand All @@ -214,16 +224,18 @@ private function getPhoto(Card $node) {
'image/gif',
];

if(!in_array($type, $allowedContentTypes, true)) {
if (!in_array($type, $allowedContentTypes, true)) {
$type = 'application/octet-stream';
}

return [
'Content-Type' => $type,
'body' => $val
'body' => $val
];
} catch(\Exception $ex) {

} catch (\Exception $e) {
$this->logger->logException($e, [
'message' => 'Exception during vcard photo parsing'
]);
}
return false;
}
Expand All @@ -240,7 +252,7 @@ private function readCard($cardData) {
* @param Binary $photo
* @return string
*/
private function getType(Binary $photo) {
private function getBinaryType(Binary $photo) {
$params = $photo->parameters();
if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) {
/** @var Parameter $typeParam */
Expand Down
5 changes: 4 additions & 1 deletion apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ public function __construct(IRequest $request, $baseUri) {
$this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
$this->server->addPlugin(new VCFExportPlugin());
$this->server->addPlugin(new MultiGetExportPlugin());
$this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache'))));
$this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
\OC::$server->getAppDataDir('dav-photocache'),
\OC::$server->getLogger())
));
}

// system tags plugins
Expand Down