2017-05-03 15:22:02 +03:00
|
|
|
<?php
|
2017-11-06 17:56:42 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2017-11-06 22:15:27 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2017-05-03 15:22:02 +03:00
|
|
|
namespace OCA\DAV\CardDAV;
|
|
|
|
|
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\NotPermittedException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\ILogger;
|
2017-05-03 15:22:02 +03:00
|
|
|
use Sabre\CardDAV\Card;
|
2020-08-14 16:10:48 +03:00
|
|
|
use Sabre\VObject\Parameter;
|
2017-05-03 15:22:02 +03:00
|
|
|
use Sabre\VObject\Property\Binary;
|
|
|
|
use Sabre\VObject\Reader;
|
|
|
|
|
|
|
|
class PhotoCache {
|
|
|
|
|
2019-01-26 17:28:31 +03:00
|
|
|
/** @var array */
|
|
|
|
protected const ALLOWED_CONTENT_TYPES = [
|
|
|
|
'image/png' => 'png',
|
|
|
|
'image/jpeg' => 'jpg',
|
|
|
|
'image/gif' => 'gif',
|
2019-01-28 23:39:51 +03:00
|
|
|
'image/vnd.microsoft.icon' => 'ico',
|
2019-01-26 17:28:31 +03:00
|
|
|
];
|
|
|
|
|
2018-12-17 19:28:19 +03:00
|
|
|
/** @var IAppData */
|
2017-05-03 15:22:02 +03:00
|
|
|
protected $appData;
|
|
|
|
|
2018-12-17 19:28:19 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
|
|
|
|
2017-05-03 15:22:02 +03:00
|
|
|
/**
|
|
|
|
* PhotoCache constructor.
|
|
|
|
*
|
|
|
|
* @param IAppData $appData
|
2018-12-17 19:28:19 +03:00
|
|
|
* @param ILogger $logger
|
2017-05-03 15:22:02 +03:00
|
|
|
*/
|
2018-12-17 19:28:19 +03:00
|
|
|
public function __construct(IAppData $appData, ILogger $logger) {
|
2017-05-03 15:22:02 +03:00
|
|
|
$this->appData = $appData;
|
2018-12-17 19:28:19 +03:00
|
|
|
$this->logger = $logger;
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-03 16:11:26 +03:00
|
|
|
* @param int $addressBookId
|
|
|
|
* @param string $cardUri
|
2017-05-03 15:22:02 +03:00
|
|
|
* @param int $size
|
|
|
|
* @param Card $card
|
|
|
|
*
|
|
|
|
* @return ISimpleFile
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
2017-05-03 16:11:26 +03:00
|
|
|
public function get($addressBookId, $cardUri, $size, Card $card) {
|
|
|
|
$folder = $this->getFolder($addressBookId, $cardUri);
|
2017-05-03 15:22:02 +03:00
|
|
|
|
|
|
|
if ($this->isEmpty($folder)) {
|
|
|
|
$this->init($folder, $card);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->hasPhoto($folder)) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($size !== -1) {
|
|
|
|
$size = 2 ** ceil(log($size) / log(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getFile($folder, $size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isEmpty(ISimpleFolder $folder) {
|
|
|
|
return $folder->getDirectoryListing() === [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @param Card $card
|
2019-01-26 17:28:31 +03:00
|
|
|
* @throws NotPermittedException
|
2017-05-03 15:22:02 +03:00
|
|
|
*/
|
2019-01-26 17:28:31 +03:00
|
|
|
private function init(ISimpleFolder $folder, Card $card): void {
|
2017-05-03 15:22:02 +03:00
|
|
|
$data = $this->getPhoto($card);
|
|
|
|
|
2019-01-26 17:28:31 +03:00
|
|
|
if ($data === false || !isset($data['Content-Type'])) {
|
2020-03-24 20:23:38 +03:00
|
|
|
$folder->newFile('nophoto', '');
|
2019-01-26 17:28:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contentType = $data['Content-Type'];
|
|
|
|
$extension = self::ALLOWED_CONTENT_TYPES[$contentType] ?? null;
|
|
|
|
|
|
|
|
if ($extension === null) {
|
2020-03-24 20:23:38 +03:00
|
|
|
$folder->newFile('nophoto', '');
|
2019-01-26 17:28:31 +03:00
|
|
|
return;
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|
2019-01-26 17:28:31 +03:00
|
|
|
|
|
|
|
$file = $folder->newFile('photo.' . $extension);
|
|
|
|
$file->putContent($data['body']);
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function hasPhoto(ISimpleFolder $folder) {
|
|
|
|
return !$folder->fileExists('nophoto');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getFile(ISimpleFolder $folder, $size) {
|
|
|
|
$ext = $this->getExtension($folder);
|
|
|
|
|
|
|
|
if ($size === -1) {
|
|
|
|
$path = 'photo.' . $ext;
|
|
|
|
} else {
|
|
|
|
$path = 'photo.' . $size . '.' . $ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$file = $folder->getFile($path);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
if ($size <= 0) {
|
|
|
|
throw new NotFoundException;
|
|
|
|
}
|
|
|
|
|
|
|
|
$photo = new \OC_Image();
|
|
|
|
/** @var ISimpleFile $file */
|
|
|
|
$file = $folder->getFile('photo.' . $ext);
|
|
|
|
$photo->loadFromData($file->getContent());
|
2017-05-03 15:44:23 +03:00
|
|
|
|
|
|
|
$ratio = $photo->width() / $photo->height();
|
|
|
|
if ($ratio < 1) {
|
2018-12-17 19:28:19 +03:00
|
|
|
$ratio = 1 / $ratio;
|
2017-05-03 15:44:23 +03:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:28:19 +03:00
|
|
|
$size = (int) ($size * $ratio);
|
2017-05-03 15:22:02 +03:00
|
|
|
if ($size !== -1) {
|
|
|
|
$photo->resize($size);
|
|
|
|
}
|
2019-01-26 17:28:31 +03:00
|
|
|
|
2017-05-03 15:22:02 +03:00
|
|
|
try {
|
|
|
|
$file = $folder->newFile($path);
|
|
|
|
$file->putContent($photo->data());
|
|
|
|
} catch (NotPermittedException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-29 17:15:07 +03:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws NotPermittedException
|
2017-05-03 15:22:02 +03:00
|
|
|
*/
|
2019-04-29 17:15:07 +03:00
|
|
|
private function getFolder(int $addressBookId, string $cardUri, bool $createIfNotExists = true): ISimpleFolder {
|
2017-05-03 16:11:26 +03:00
|
|
|
$hash = md5($addressBookId . ' ' . $cardUri);
|
2017-05-03 15:22:02 +03:00
|
|
|
try {
|
|
|
|
return $this->appData->getFolder($hash);
|
|
|
|
} catch (NotFoundException $e) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($createIfNotExists) {
|
2019-04-29 17:15:07 +03:00
|
|
|
return $this->appData->newFolder($hash);
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the extension of the avatar. If there is no avatar throw Exception
|
|
|
|
*
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @return string
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
2019-01-26 17:28:31 +03:00
|
|
|
private function getExtension(ISimpleFolder $folder): string {
|
|
|
|
foreach (self::ALLOWED_CONTENT_TYPES as $extension) {
|
|
|
|
if ($folder->fileExists('photo.' . $extension)) {
|
|
|
|
return $extension;
|
|
|
|
}
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|
2019-01-26 17:28:31 +03:00
|
|
|
|
|
|
|
throw new NotFoundException('Avatar not found');
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getPhoto(Card $node) {
|
|
|
|
try {
|
|
|
|
$vObject = $this->readCard($node->get());
|
|
|
|
if (!$vObject->PHOTO) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$photo = $vObject->PHOTO;
|
|
|
|
$val = $photo->getValue();
|
2018-12-17 14:45:07 +03:00
|
|
|
|
|
|
|
// handle data URI. e.g PHOTO;VALUE=URI:data:image/jpeg;base64,/9j/4AAQSkZJRgABAQE
|
2017-05-03 15:22:02 +03:00
|
|
|
if ($photo->getValueType() === 'URI') {
|
|
|
|
$parsed = \Sabre\URI\parse($val);
|
2018-12-17 14:45:07 +03:00
|
|
|
|
|
|
|
// only allow data://
|
2017-05-03 15:22:02 +03:00
|
|
|
if ($parsed['scheme'] !== 'data') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (substr_count($parsed['path'], ';') === 1) {
|
2018-12-17 19:28:19 +03:00
|
|
|
list($type) = explode(';', $parsed['path']);
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|
|
|
|
$val = file_get_contents($val);
|
2018-12-17 14:45:07 +03:00
|
|
|
} else {
|
|
|
|
// get type if binary data
|
|
|
|
$type = $this->getBinaryType($photo);
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|
|
|
|
|
2019-01-26 17:28:31 +03:00
|
|
|
if (empty($type) || !isset(self::ALLOWED_CONTENT_TYPES[$type])) {
|
2017-05-03 15:22:02 +03:00
|
|
|
$type = 'application/octet-stream';
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'Content-Type' => $type,
|
2018-12-17 19:28:19 +03:00
|
|
|
'body' => $val
|
2017-05-03 15:22:02 +03:00
|
|
|
];
|
2018-12-17 19:28:19 +03:00
|
|
|
} catch (\Exception $e) {
|
2019-01-04 17:02:15 +03:00
|
|
|
$this->logger->logException($e, [
|
2018-12-17 19:28:19 +03:00
|
|
|
'message' => 'Exception during vcard photo parsing'
|
|
|
|
]);
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $cardData
|
|
|
|
* @return \Sabre\VObject\Document
|
|
|
|
*/
|
|
|
|
private function readCard($cardData) {
|
|
|
|
return Reader::read($cardData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Binary $photo
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-12-17 14:45:07 +03:00
|
|
|
private function getBinaryType(Binary $photo) {
|
2017-05-03 15:22:02 +03:00
|
|
|
$params = $photo->parameters();
|
|
|
|
if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) {
|
|
|
|
/** @var Parameter $typeParam */
|
|
|
|
$typeParam = isset($params['TYPE']) ? $params['TYPE'] : $params['MEDIATYPE'];
|
|
|
|
$type = $typeParam->getValue();
|
|
|
|
|
|
|
|
if (strpos($type, 'image/') === 0) {
|
|
|
|
return $type;
|
|
|
|
} else {
|
|
|
|
return 'image/' . strtolower($type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2017-05-03 16:11:26 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $addressBookId
|
|
|
|
* @param string $cardUri
|
2019-04-29 17:15:07 +03:00
|
|
|
* @throws NotPermittedException
|
2017-05-03 16:11:26 +03:00
|
|
|
*/
|
|
|
|
public function delete($addressBookId, $cardUri) {
|
2019-04-29 17:15:07 +03:00
|
|
|
try {
|
|
|
|
$folder = $this->getFolder($addressBookId, $cardUri, false);
|
|
|
|
$folder->delete();
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// that's OK, nothing to do
|
|
|
|
}
|
2017-05-03 16:11:26 +03:00
|
|
|
}
|
2017-05-03 15:22:02 +03:00
|
|
|
}
|