From fa6c25708455d061da20178d021674ebb661c297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Mon, 17 Dec 2018 12:45:07 +0100 Subject: [PATCH 1/3] Allow URI as data for vcard PHOTO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- apps/dav/lib/CardDAV/ImageExportPlugin.php | 1 + apps/dav/lib/CardDAV/PhotoCache.php | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/dav/lib/CardDAV/ImageExportPlugin.php b/apps/dav/lib/CardDAV/ImageExportPlugin.php index cac8e79574..7aa95922f1 100644 --- a/apps/dav/lib/CardDAV/ImageExportPlugin.php +++ b/apps/dav/lib/CardDAV/ImageExportPlugin.php @@ -108,6 +108,7 @@ class ImageExportPlugin extends ServerPlugin { $response->setBody($file->getContent()); } catch (NotFoundException $e) { + var_dump($e); $response->setStatus(404); } diff --git a/apps/dav/lib/CardDAV/PhotoCache.php b/apps/dav/lib/CardDAV/PhotoCache.php index 0029af7bff..2f3cc66f1d 100644 --- a/apps/dav/lib/CardDAV/PhotoCache.php +++ b/apps/dav/lib/CardDAV/PhotoCache.php @@ -30,6 +30,7 @@ use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use Sabre\CardDAV\Card; use Sabre\VObject\Property\Binary; +use Sabre\VObject\Property\Uri; use Sabre\VObject\Reader; class PhotoCache { @@ -193,12 +194,13 @@ class PhotoCache { } $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); - //only allow data:// + + // only allow data:// if ($parsed['scheme'] !== 'data') { return false; } @@ -206,6 +208,9 @@ class PhotoCache { list($type,) = explode(';', $parsed['path']); } $val = file_get_contents($val); + } else { + // get type if binary data + $type = $this->getBinaryType($photo); } $allowedContentTypes = [ @@ -240,7 +245,7 @@ class PhotoCache { * @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 */ From 1d2d7fd60d7f663310dc05088f01fea8144eeb2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Mon, 17 Dec 2018 17:28:19 +0100 Subject: [PATCH 2/3] Log failure on parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- apps/dav/appinfo/v1/carddav.php | 5 +++- apps/dav/lib/AppInfo/Application.php | 3 ++- apps/dav/lib/CardDAV/ImageExportPlugin.php | 1 - apps/dav/lib/CardDAV/PhotoCache.php | 29 ++++++++++++++-------- apps/dav/lib/Server.php | 5 +++- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/apps/dav/appinfo/v1/carddav.php b/apps/dav/appinfo/v1/carddav.php index e55eee610e..a424d7afab 100644 --- a/apps/dav/appinfo/v1/carddav.php +++ b/apps/dav/appinfo/v1/carddav.php @@ -84,7 +84,10 @@ if ($debugging) { $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! diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index a557128635..a90e42d56f 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -54,7 +54,8 @@ class Application extends App { $container->registerService(PhotoCache::class, function(SimpleContainer $s) use ($server) { return new PhotoCache( - $server->getAppDataDir('dav-photocache') + $server->getAppDataDir('dav-photocache'), + $server->getLogger() ); }); diff --git a/apps/dav/lib/CardDAV/ImageExportPlugin.php b/apps/dav/lib/CardDAV/ImageExportPlugin.php index 7aa95922f1..cac8e79574 100644 --- a/apps/dav/lib/CardDAV/ImageExportPlugin.php +++ b/apps/dav/lib/CardDAV/ImageExportPlugin.php @@ -108,7 +108,6 @@ class ImageExportPlugin extends ServerPlugin { $response->setBody($file->getContent()); } catch (NotFoundException $e) { - var_dump($e); $response->setStatus(404); } diff --git a/apps/dav/lib/CardDAV/PhotoCache.php b/apps/dav/lib/CardDAV/PhotoCache.php index 2f3cc66f1d..a252984d5f 100644 --- a/apps/dav/lib/CardDAV/PhotoCache.php +++ b/apps/dav/lib/CardDAV/PhotoCache.php @@ -24,27 +24,32 @@ namespace OCA\DAV\CardDAV; use OCP\Files\IAppData; +use OCP\ILogger; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use Sabre\CardDAV\Card; use Sabre\VObject\Property\Binary; -use Sabre\VObject\Property\Uri; use Sabre\VObject\Reader; 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; } /** @@ -135,13 +140,14 @@ class PhotoCache { $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()); @@ -153,7 +159,6 @@ class PhotoCache { return $file; } - /** * @param int $addressBookId * @param string $cardUri @@ -205,7 +210,7 @@ class PhotoCache { return false; } if (substr_count($parsed['path'], ';') === 1) { - list($type,) = explode(';', $parsed['path']); + list($type) = explode(';', $parsed['path']); } $val = file_get_contents($val); } else { @@ -219,16 +224,18 @@ class PhotoCache { '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($ex, [ + 'message' => 'Exception during vcard photo parsing' + ]); } return false; } diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index dac2fa102b..c2c903fa19 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -167,7 +167,10 @@ class Server { $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 From 7f5c38bb604f54889071de7dfe529abf0d38eaa5 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Fri, 4 Jan 2019 15:02:15 +0100 Subject: [PATCH 3/3] Update apps/dav/lib/CardDAV/PhotoCache.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) Co-Authored-By: skjnldsv --- apps/dav/lib/CardDAV/PhotoCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/dav/lib/CardDAV/PhotoCache.php b/apps/dav/lib/CardDAV/PhotoCache.php index a252984d5f..fa244857e3 100644 --- a/apps/dav/lib/CardDAV/PhotoCache.php +++ b/apps/dav/lib/CardDAV/PhotoCache.php @@ -233,7 +233,7 @@ class PhotoCache { 'body' => $val ]; } catch (\Exception $e) { - $this->logger->logException($ex, [ + $this->logger->logException($e, [ 'message' => 'Exception during vcard photo parsing' ]); }