LinkReferenceProvider.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\Collaboration\Reference;
  8. use Fusonic\OpenGraph\Consumer;
  9. use GuzzleHttp\Exception\GuzzleException;
  10. use GuzzleHttp\Psr7\LimitStream;
  11. use GuzzleHttp\Psr7\Utils;
  12. use OC\Security\RateLimiting\Exception\RateLimitExceededException;
  13. use OC\Security\RateLimiting\Limiter;
  14. use OC\SystemConfig;
  15. use OCP\Files\AppData\IAppDataFactory;
  16. use OCP\Files\NotFoundException;
  17. use OCP\Http\Client\IClientService;
  18. use OCP\IRequest;
  19. use OCP\IURLGenerator;
  20. use OCP\IUserSession;
  21. use Psr\Log\LoggerInterface;
  22. /**
  23. * @since 29.0.0
  24. */
  25. class LinkReferenceProvider implements IReferenceProvider {
  26. /**
  27. * for image size and webpage header
  28. * @since 29.0.0
  29. */
  30. public const MAX_CONTENT_LENGTH = 5 * 1024 * 1024;
  31. /**
  32. * @since 29.0.0
  33. */
  34. public const ALLOWED_CONTENT_TYPES = [
  35. 'image/png',
  36. 'image/jpg',
  37. 'image/jpeg',
  38. 'image/gif',
  39. 'image/svg+xml',
  40. 'image/webp'
  41. ];
  42. /**
  43. * @since 29.0.0
  44. */
  45. public function __construct(
  46. private IClientService $clientService,
  47. private LoggerInterface $logger,
  48. private SystemConfig $systemConfig,
  49. private IAppDataFactory $appDataFactory,
  50. private IURLGenerator $urlGenerator,
  51. private Limiter $limiter,
  52. private IUserSession $userSession,
  53. private IRequest $request,
  54. ) {
  55. }
  56. /**
  57. * @inheritDoc
  58. * @since 29.0.0
  59. */
  60. public function matchReference(string $referenceText): bool {
  61. if ($this->systemConfig->getValue('reference_opengraph', true) !== true) {
  62. return false;
  63. }
  64. return (bool)preg_match(IURLGenerator::URL_REGEX, $referenceText);
  65. }
  66. /**
  67. * @inheritDoc
  68. * @since 29.0.0
  69. */
  70. public function resolveReference(string $referenceText): ?IReference {
  71. if ($this->matchReference($referenceText)) {
  72. $reference = new Reference($referenceText);
  73. $this->fetchReference($reference);
  74. return $reference;
  75. }
  76. return null;
  77. }
  78. /**
  79. * Populates the reference with OpenGraph data
  80. *
  81. * @param Reference $reference
  82. * @since 29.0.0
  83. */
  84. private function fetchReference(Reference $reference): void {
  85. try {
  86. $user = $this->userSession->getUser();
  87. if ($user) {
  88. $this->limiter->registerUserRequest('opengraph', 10, 120, $user);
  89. } else {
  90. $this->limiter->registerAnonRequest('opengraph', 10, 120, $this->request->getRemoteAddress());
  91. }
  92. } catch (RateLimitExceededException $e) {
  93. return;
  94. }
  95. $client = $this->clientService->newClient();
  96. try {
  97. $headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]);
  98. } catch (\Exception $e) {
  99. $this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]);
  100. return;
  101. }
  102. $linkContentLength = $headResponse->getHeader('Content-Length');
  103. if (is_numeric($linkContentLength) && (int) $linkContentLength > self::MAX_CONTENT_LENGTH) {
  104. $this->logger->debug('Skip resolving links pointing to content length > 5 MiB');
  105. return;
  106. }
  107. $linkContentType = $headResponse->getHeader('Content-Type');
  108. $expectedContentTypeRegex = '/^text\/html;?/i';
  109. // check the header begins with the expected content type
  110. if (!preg_match($expectedContentTypeRegex, $linkContentType)) {
  111. $this->logger->debug('Skip resolving links pointing to content type that is not "text/html"');
  112. return;
  113. }
  114. try {
  115. $response = $client->get($reference->getId(), [ 'timeout' => 10 ]);
  116. } catch (\Exception $e) {
  117. $this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]);
  118. return;
  119. }
  120. $responseBody = (string)$response->getBody();
  121. // OpenGraph handling
  122. $consumer = new Consumer();
  123. $consumer->useFallbackMode = true;
  124. $object = $consumer->loadHtml($responseBody);
  125. $reference->setUrl($reference->getId());
  126. if ($object->title) {
  127. $reference->setTitle($object->title);
  128. }
  129. if ($object->description) {
  130. $reference->setDescription($object->description);
  131. }
  132. if ($object->images) {
  133. try {
  134. $host = parse_url($object->images[0]->url, PHP_URL_HOST);
  135. if ($host === false || $host === null) {
  136. $this->logger->warning('Could not detect host of open graph image URI for ' . $reference->getId());
  137. return;
  138. }
  139. $appData = $this->appDataFactory->get('core');
  140. try {
  141. $folder = $appData->getFolder('opengraph');
  142. } catch (NotFoundException $e) {
  143. $folder = $appData->newFolder('opengraph');
  144. }
  145. $response = $client->get($object->images[0]->url, ['timeout' => 10]);
  146. $contentType = $response->getHeader('Content-Type');
  147. $contentLength = $response->getHeader('Content-Length');
  148. if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_CONTENT_LENGTH) {
  149. $stream = Utils::streamFor($response->getBody());
  150. $bodyStream = new LimitStream($stream, self::MAX_CONTENT_LENGTH, 0);
  151. $reference->setImageContentType($contentType);
  152. $folder->newFile(md5($reference->getId()), $bodyStream->getContents());
  153. $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())]));
  154. }
  155. } catch (GuzzleException $e) {
  156. $this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
  157. } catch (\Throwable $e) {
  158. $this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
  159. }
  160. }
  161. }
  162. /**
  163. * @inheritDoc
  164. * @since 29.0.0
  165. */
  166. public function getCachePrefix(string $referenceId): string {
  167. return $referenceId;
  168. }
  169. /**
  170. * @inheritDoc
  171. * @since 29.0.0
  172. */
  173. public function getCacheKey(string $referenceId): ?string {
  174. return null;
  175. }
  176. }