1
0

PhotoCache.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CardDAV;
  25. use OCP\Files\IAppData;
  26. use OCP\ILogger;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\NotPermittedException;
  29. use OCP\Files\SimpleFS\ISimpleFile;
  30. use OCP\Files\SimpleFS\ISimpleFolder;
  31. use Sabre\CardDAV\Card;
  32. use Sabre\VObject\Property\Binary;
  33. use Sabre\VObject\Reader;
  34. class PhotoCache {
  35. /** @var IAppData */
  36. protected $appData;
  37. /** @var ILogger */
  38. protected $logger;
  39. /**
  40. * PhotoCache constructor.
  41. *
  42. * @param IAppData $appData
  43. * @param ILogger $logger
  44. */
  45. public function __construct(IAppData $appData, ILogger $logger) {
  46. $this->appData = $appData;
  47. $this->logger = $logger;
  48. }
  49. /**
  50. * @param int $addressBookId
  51. * @param string $cardUri
  52. * @param int $size
  53. * @param Card $card
  54. *
  55. * @return ISimpleFile
  56. * @throws NotFoundException
  57. */
  58. public function get($addressBookId, $cardUri, $size, Card $card) {
  59. $folder = $this->getFolder($addressBookId, $cardUri);
  60. if ($this->isEmpty($folder)) {
  61. $this->init($folder, $card);
  62. }
  63. if (!$this->hasPhoto($folder)) {
  64. throw new NotFoundException();
  65. }
  66. if ($size !== -1) {
  67. $size = 2 ** ceil(log($size) / log(2));
  68. }
  69. return $this->getFile($folder, $size);
  70. }
  71. /**
  72. * @param ISimpleFolder $folder
  73. * @return bool
  74. */
  75. private function isEmpty(ISimpleFolder $folder) {
  76. return $folder->getDirectoryListing() === [];
  77. }
  78. /**
  79. * @param ISimpleFolder $folder
  80. * @param Card $card
  81. */
  82. private function init(ISimpleFolder $folder, Card $card) {
  83. $data = $this->getPhoto($card);
  84. if ($data === false) {
  85. $folder->newFile('nophoto');
  86. } else {
  87. switch ($data['Content-Type']) {
  88. case 'image/png':
  89. $ext = 'png';
  90. break;
  91. case 'image/jpeg':
  92. $ext = 'jpg';
  93. break;
  94. case 'image/gif':
  95. $ext = 'gif';
  96. break;
  97. }
  98. $file = $folder->newFile('photo.' . $ext);
  99. $file->putContent($data['body']);
  100. }
  101. }
  102. private function hasPhoto(ISimpleFolder $folder) {
  103. return !$folder->fileExists('nophoto');
  104. }
  105. private function getFile(ISimpleFolder $folder, $size) {
  106. $ext = $this->getExtension($folder);
  107. if ($size === -1) {
  108. $path = 'photo.' . $ext;
  109. } else {
  110. $path = 'photo.' . $size . '.' . $ext;
  111. }
  112. try {
  113. $file = $folder->getFile($path);
  114. } catch (NotFoundException $e) {
  115. if ($size <= 0) {
  116. throw new NotFoundException;
  117. }
  118. $photo = new \OC_Image();
  119. /** @var ISimpleFile $file */
  120. $file = $folder->getFile('photo.' . $ext);
  121. $photo->loadFromData($file->getContent());
  122. $ratio = $photo->width() / $photo->height();
  123. if ($ratio < 1) {
  124. $ratio = 1 / $ratio;
  125. }
  126. $size = (int) ($size * $ratio);
  127. if ($size !== -1) {
  128. $photo->resize($size);
  129. }
  130. try {
  131. $file = $folder->newFile($path);
  132. $file->putContent($photo->data());
  133. } catch (NotPermittedException $e) {
  134. }
  135. }
  136. return $file;
  137. }
  138. /**
  139. * @param int $addressBookId
  140. * @param string $cardUri
  141. * @return ISimpleFolder
  142. */
  143. private function getFolder($addressBookId, $cardUri) {
  144. $hash = md5($addressBookId . ' ' . $cardUri);
  145. try {
  146. return $this->appData->getFolder($hash);
  147. } catch (NotFoundException $e) {
  148. return $this->appData->newFolder($hash);
  149. }
  150. }
  151. /**
  152. * Get the extension of the avatar. If there is no avatar throw Exception
  153. *
  154. * @param ISimpleFolder $folder
  155. * @return string
  156. * @throws NotFoundException
  157. */
  158. private function getExtension(ISimpleFolder $folder) {
  159. if ($folder->fileExists('photo.jpg')) {
  160. return 'jpg';
  161. } elseif ($folder->fileExists('photo.png')) {
  162. return 'png';
  163. } elseif ($folder->fileExists('photo.gif')) {
  164. return 'gif';
  165. }
  166. throw new NotFoundException;
  167. }
  168. private function getPhoto(Card $node) {
  169. try {
  170. $vObject = $this->readCard($node->get());
  171. if (!$vObject->PHOTO) {
  172. return false;
  173. }
  174. $photo = $vObject->PHOTO;
  175. $val = $photo->getValue();
  176. // handle data URI. e.g PHOTO;VALUE=URI:data:image/jpeg;base64,/9j/4AAQSkZJRgABAQE
  177. if ($photo->getValueType() === 'URI') {
  178. $parsed = \Sabre\URI\parse($val);
  179. // only allow data://
  180. if ($parsed['scheme'] !== 'data') {
  181. return false;
  182. }
  183. if (substr_count($parsed['path'], ';') === 1) {
  184. list($type) = explode(';', $parsed['path']);
  185. }
  186. $val = file_get_contents($val);
  187. } else {
  188. // get type if binary data
  189. $type = $this->getBinaryType($photo);
  190. }
  191. $allowedContentTypes = [
  192. 'image/png',
  193. 'image/jpeg',
  194. 'image/gif',
  195. ];
  196. if (!in_array($type, $allowedContentTypes, true)) {
  197. $type = 'application/octet-stream';
  198. }
  199. return [
  200. 'Content-Type' => $type,
  201. 'body' => $val
  202. ];
  203. } catch (\Exception $e) {
  204. $this->logger->logException($e, [
  205. 'message' => 'Exception during vcard photo parsing'
  206. ]);
  207. }
  208. return false;
  209. }
  210. /**
  211. * @param string $cardData
  212. * @return \Sabre\VObject\Document
  213. */
  214. private function readCard($cardData) {
  215. return Reader::read($cardData);
  216. }
  217. /**
  218. * @param Binary $photo
  219. * @return string
  220. */
  221. private function getBinaryType(Binary $photo) {
  222. $params = $photo->parameters();
  223. if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) {
  224. /** @var Parameter $typeParam */
  225. $typeParam = isset($params['TYPE']) ? $params['TYPE'] : $params['MEDIATYPE'];
  226. $type = $typeParam->getValue();
  227. if (strpos($type, 'image/') === 0) {
  228. return $type;
  229. } else {
  230. return 'image/' . strtolower($type);
  231. }
  232. }
  233. return '';
  234. }
  235. /**
  236. * @param int $addressBookId
  237. * @param string $cardUri
  238. */
  239. public function delete($addressBookId, $cardUri) {
  240. $folder = $this->getFolder($addressBookId, $cardUri);
  241. $folder->delete();
  242. }
  243. }