@@ -43,149 +43,149 @@ |
||
43 | 43 | use Psr\Log\LoggerInterface; |
44 | 44 | |
45 | 45 | class LinkReferenceProvider implements IReferenceProvider { |
46 | - public const MAX_PREVIEW_SIZE = 1024 * 1024; |
|
47 | - |
|
48 | - public const ALLOWED_CONTENT_TYPES = [ |
|
49 | - 'image/png', |
|
50 | - 'image/jpg', |
|
51 | - 'image/jpeg', |
|
52 | - 'image/gif', |
|
53 | - 'image/svg+xml', |
|
54 | - 'image/webp' |
|
55 | - ]; |
|
56 | - |
|
57 | - private IClientService $clientService; |
|
58 | - private LoggerInterface $logger; |
|
59 | - private SystemConfig $systemConfig; |
|
60 | - private IAppDataFactory $appDataFactory; |
|
61 | - private IURLGenerator $urlGenerator; |
|
62 | - private Limiter $limiter; |
|
63 | - private IUserSession $userSession; |
|
64 | - private IRequest $request; |
|
65 | - |
|
66 | - public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator, Limiter $limiter, IUserSession $userSession, IRequest $request) { |
|
67 | - $this->clientService = $clientService; |
|
68 | - $this->logger = $logger; |
|
69 | - $this->systemConfig = $systemConfig; |
|
70 | - $this->appDataFactory = $appDataFactory; |
|
71 | - $this->urlGenerator = $urlGenerator; |
|
72 | - $this->limiter = $limiter; |
|
73 | - $this->userSession = $userSession; |
|
74 | - $this->request = $request; |
|
75 | - } |
|
76 | - |
|
77 | - public function matchReference(string $referenceText): bool { |
|
78 | - if ($this->systemConfig->getValue('reference_opengraph', true) !== true) { |
|
79 | - return false; |
|
80 | - } |
|
81 | - |
|
82 | - return (bool)preg_match(IURLGenerator::URL_REGEX, $referenceText); |
|
83 | - } |
|
84 | - |
|
85 | - public function resolveReference(string $referenceText): ?IReference { |
|
86 | - if ($this->matchReference($referenceText)) { |
|
87 | - $reference = new Reference($referenceText); |
|
88 | - $this->fetchReference($reference); |
|
89 | - return $reference; |
|
90 | - } |
|
91 | - |
|
92 | - return null; |
|
93 | - } |
|
94 | - |
|
95 | - private function fetchReference(Reference $reference): void { |
|
96 | - try { |
|
97 | - $user = $this->userSession->getUser(); |
|
98 | - if ($user) { |
|
99 | - $this->limiter->registerUserRequest('opengraph', 10, 120, $user); |
|
100 | - } else { |
|
101 | - $this->limiter->registerAnonRequest('opengraph', 10, 120, $this->request->getRemoteAddress()); |
|
102 | - } |
|
103 | - } catch (RateLimitExceededException $e) { |
|
104 | - return; |
|
105 | - } |
|
106 | - |
|
107 | - $client = $this->clientService->newClient(); |
|
108 | - try { |
|
109 | - $headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]); |
|
110 | - } catch (\Exception $e) { |
|
111 | - $this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]); |
|
112 | - return; |
|
113 | - } |
|
114 | - $linkContentLength = $headResponse->getHeader('Content-Length'); |
|
115 | - if (is_numeric($linkContentLength) && (int) $linkContentLength > 5 * 1024 * 1024) { |
|
116 | - $this->logger->debug('Skip resolving links pointing to content length > 5 MB'); |
|
117 | - return; |
|
118 | - } |
|
119 | - $linkContentType = $headResponse->getHeader('Content-Type'); |
|
120 | - $expectedContentType = 'text/html'; |
|
121 | - $suffixedExpectedContentType = $expectedContentType . ';'; |
|
122 | - $startsWithSuffixed = substr($linkContentType, 0, strlen($suffixedExpectedContentType)) === $suffixedExpectedContentType; |
|
123 | - // check the header begins with the expected content type |
|
124 | - if ($linkContentType !== $expectedContentType && !$startsWithSuffixed) { |
|
125 | - $this->logger->debug('Skip resolving links pointing to content type that is not "text/html"'); |
|
126 | - return; |
|
127 | - } |
|
128 | - try { |
|
129 | - $response = $client->get($reference->getId(), [ 'timeout' => 10 ]); |
|
130 | - } catch (\Exception $e) { |
|
131 | - $this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]); |
|
132 | - return; |
|
133 | - } |
|
134 | - |
|
135 | - $responseBody = (string)$response->getBody(); |
|
136 | - |
|
137 | - // OpenGraph handling |
|
138 | - $consumer = new Consumer(); |
|
139 | - $consumer->useFallbackMode = true; |
|
140 | - $object = $consumer->loadHtml($responseBody); |
|
141 | - |
|
142 | - $reference->setUrl($reference->getId()); |
|
143 | - |
|
144 | - if ($object->title) { |
|
145 | - $reference->setTitle($object->title); |
|
146 | - } |
|
147 | - |
|
148 | - if ($object->description) { |
|
149 | - $reference->setDescription($object->description); |
|
150 | - } |
|
151 | - |
|
152 | - if ($object->images) { |
|
153 | - try { |
|
154 | - $host = parse_url($object->images[0]->url, PHP_URL_HOST); |
|
155 | - if ($host === false || $host === null) { |
|
156 | - $this->logger->warning('Could not detect host of open graph image URI for ' . $reference->getId()); |
|
157 | - } else { |
|
158 | - $appData = $this->appDataFactory->get('core'); |
|
159 | - try { |
|
160 | - $folder = $appData->getFolder('opengraph'); |
|
161 | - } catch (NotFoundException $e) { |
|
162 | - $folder = $appData->newFolder('opengraph'); |
|
163 | - } |
|
164 | - $response = $client->get($object->images[0]->url, ['timeout' => 10]); |
|
165 | - $contentType = $response->getHeader('Content-Type'); |
|
166 | - $contentLength = $response->getHeader('Content-Length'); |
|
167 | - |
|
168 | - if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) { |
|
169 | - $stream = Utils::streamFor($response->getBody()); |
|
170 | - $bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0); |
|
171 | - $reference->setImageContentType($contentType); |
|
172 | - $folder->newFile(md5($reference->getId()), $bodyStream->getContents()); |
|
173 | - $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())])); |
|
174 | - } |
|
175 | - } |
|
176 | - } catch (GuzzleException $e) { |
|
177 | - $this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); |
|
178 | - } catch (\Throwable $e) { |
|
179 | - $this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); |
|
180 | - } |
|
181 | - } |
|
182 | - } |
|
183 | - |
|
184 | - public function getCachePrefix(string $referenceId): string { |
|
185 | - return $referenceId; |
|
186 | - } |
|
187 | - |
|
188 | - public function getCacheKey(string $referenceId): ?string { |
|
189 | - return null; |
|
190 | - } |
|
46 | + public const MAX_PREVIEW_SIZE = 1024 * 1024; |
|
47 | + |
|
48 | + public const ALLOWED_CONTENT_TYPES = [ |
|
49 | + 'image/png', |
|
50 | + 'image/jpg', |
|
51 | + 'image/jpeg', |
|
52 | + 'image/gif', |
|
53 | + 'image/svg+xml', |
|
54 | + 'image/webp' |
|
55 | + ]; |
|
56 | + |
|
57 | + private IClientService $clientService; |
|
58 | + private LoggerInterface $logger; |
|
59 | + private SystemConfig $systemConfig; |
|
60 | + private IAppDataFactory $appDataFactory; |
|
61 | + private IURLGenerator $urlGenerator; |
|
62 | + private Limiter $limiter; |
|
63 | + private IUserSession $userSession; |
|
64 | + private IRequest $request; |
|
65 | + |
|
66 | + public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator, Limiter $limiter, IUserSession $userSession, IRequest $request) { |
|
67 | + $this->clientService = $clientService; |
|
68 | + $this->logger = $logger; |
|
69 | + $this->systemConfig = $systemConfig; |
|
70 | + $this->appDataFactory = $appDataFactory; |
|
71 | + $this->urlGenerator = $urlGenerator; |
|
72 | + $this->limiter = $limiter; |
|
73 | + $this->userSession = $userSession; |
|
74 | + $this->request = $request; |
|
75 | + } |
|
76 | + |
|
77 | + public function matchReference(string $referenceText): bool { |
|
78 | + if ($this->systemConfig->getValue('reference_opengraph', true) !== true) { |
|
79 | + return false; |
|
80 | + } |
|
81 | + |
|
82 | + return (bool)preg_match(IURLGenerator::URL_REGEX, $referenceText); |
|
83 | + } |
|
84 | + |
|
85 | + public function resolveReference(string $referenceText): ?IReference { |
|
86 | + if ($this->matchReference($referenceText)) { |
|
87 | + $reference = new Reference($referenceText); |
|
88 | + $this->fetchReference($reference); |
|
89 | + return $reference; |
|
90 | + } |
|
91 | + |
|
92 | + return null; |
|
93 | + } |
|
94 | + |
|
95 | + private function fetchReference(Reference $reference): void { |
|
96 | + try { |
|
97 | + $user = $this->userSession->getUser(); |
|
98 | + if ($user) { |
|
99 | + $this->limiter->registerUserRequest('opengraph', 10, 120, $user); |
|
100 | + } else { |
|
101 | + $this->limiter->registerAnonRequest('opengraph', 10, 120, $this->request->getRemoteAddress()); |
|
102 | + } |
|
103 | + } catch (RateLimitExceededException $e) { |
|
104 | + return; |
|
105 | + } |
|
106 | + |
|
107 | + $client = $this->clientService->newClient(); |
|
108 | + try { |
|
109 | + $headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]); |
|
110 | + } catch (\Exception $e) { |
|
111 | + $this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]); |
|
112 | + return; |
|
113 | + } |
|
114 | + $linkContentLength = $headResponse->getHeader('Content-Length'); |
|
115 | + if (is_numeric($linkContentLength) && (int) $linkContentLength > 5 * 1024 * 1024) { |
|
116 | + $this->logger->debug('Skip resolving links pointing to content length > 5 MB'); |
|
117 | + return; |
|
118 | + } |
|
119 | + $linkContentType = $headResponse->getHeader('Content-Type'); |
|
120 | + $expectedContentType = 'text/html'; |
|
121 | + $suffixedExpectedContentType = $expectedContentType . ';'; |
|
122 | + $startsWithSuffixed = substr($linkContentType, 0, strlen($suffixedExpectedContentType)) === $suffixedExpectedContentType; |
|
123 | + // check the header begins with the expected content type |
|
124 | + if ($linkContentType !== $expectedContentType && !$startsWithSuffixed) { |
|
125 | + $this->logger->debug('Skip resolving links pointing to content type that is not "text/html"'); |
|
126 | + return; |
|
127 | + } |
|
128 | + try { |
|
129 | + $response = $client->get($reference->getId(), [ 'timeout' => 10 ]); |
|
130 | + } catch (\Exception $e) { |
|
131 | + $this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]); |
|
132 | + return; |
|
133 | + } |
|
134 | + |
|
135 | + $responseBody = (string)$response->getBody(); |
|
136 | + |
|
137 | + // OpenGraph handling |
|
138 | + $consumer = new Consumer(); |
|
139 | + $consumer->useFallbackMode = true; |
|
140 | + $object = $consumer->loadHtml($responseBody); |
|
141 | + |
|
142 | + $reference->setUrl($reference->getId()); |
|
143 | + |
|
144 | + if ($object->title) { |
|
145 | + $reference->setTitle($object->title); |
|
146 | + } |
|
147 | + |
|
148 | + if ($object->description) { |
|
149 | + $reference->setDescription($object->description); |
|
150 | + } |
|
151 | + |
|
152 | + if ($object->images) { |
|
153 | + try { |
|
154 | + $host = parse_url($object->images[0]->url, PHP_URL_HOST); |
|
155 | + if ($host === false || $host === null) { |
|
156 | + $this->logger->warning('Could not detect host of open graph image URI for ' . $reference->getId()); |
|
157 | + } else { |
|
158 | + $appData = $this->appDataFactory->get('core'); |
|
159 | + try { |
|
160 | + $folder = $appData->getFolder('opengraph'); |
|
161 | + } catch (NotFoundException $e) { |
|
162 | + $folder = $appData->newFolder('opengraph'); |
|
163 | + } |
|
164 | + $response = $client->get($object->images[0]->url, ['timeout' => 10]); |
|
165 | + $contentType = $response->getHeader('Content-Type'); |
|
166 | + $contentLength = $response->getHeader('Content-Length'); |
|
167 | + |
|
168 | + if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) { |
|
169 | + $stream = Utils::streamFor($response->getBody()); |
|
170 | + $bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0); |
|
171 | + $reference->setImageContentType($contentType); |
|
172 | + $folder->newFile(md5($reference->getId()), $bodyStream->getContents()); |
|
173 | + $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())])); |
|
174 | + } |
|
175 | + } |
|
176 | + } catch (GuzzleException $e) { |
|
177 | + $this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); |
|
178 | + } catch (\Throwable $e) { |
|
179 | + $this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); |
|
180 | + } |
|
181 | + } |
|
182 | + } |
|
183 | + |
|
184 | + public function getCachePrefix(string $referenceId): string { |
|
185 | + return $referenceId; |
|
186 | + } |
|
187 | + |
|
188 | + public function getCacheKey(string $referenceId): ?string { |
|
189 | + return null; |
|
190 | + } |
|
191 | 191 | } |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | return false; |
80 | 80 | } |
81 | 81 | |
82 | - return (bool)preg_match(IURLGenerator::URL_REGEX, $referenceText); |
|
82 | + return (bool) preg_match(IURLGenerator::URL_REGEX, $referenceText); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | public function resolveReference(string $referenceText): ?IReference { |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | |
107 | 107 | $client = $this->clientService->newClient(); |
108 | 108 | try { |
109 | - $headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]); |
|
109 | + $headResponse = $client->head($reference->getId(), ['timeout' => 10]); |
|
110 | 110 | } catch (\Exception $e) { |
111 | 111 | $this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]); |
112 | 112 | return; |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | } |
119 | 119 | $linkContentType = $headResponse->getHeader('Content-Type'); |
120 | 120 | $expectedContentType = 'text/html'; |
121 | - $suffixedExpectedContentType = $expectedContentType . ';'; |
|
121 | + $suffixedExpectedContentType = $expectedContentType.';'; |
|
122 | 122 | $startsWithSuffixed = substr($linkContentType, 0, strlen($suffixedExpectedContentType)) === $suffixedExpectedContentType; |
123 | 123 | // check the header begins with the expected content type |
124 | 124 | if ($linkContentType !== $expectedContentType && !$startsWithSuffixed) { |
@@ -126,13 +126,13 @@ discard block |
||
126 | 126 | return; |
127 | 127 | } |
128 | 128 | try { |
129 | - $response = $client->get($reference->getId(), [ 'timeout' => 10 ]); |
|
129 | + $response = $client->get($reference->getId(), ['timeout' => 10]); |
|
130 | 130 | } catch (\Exception $e) { |
131 | 131 | $this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]); |
132 | 132 | return; |
133 | 133 | } |
134 | 134 | |
135 | - $responseBody = (string)$response->getBody(); |
|
135 | + $responseBody = (string) $response->getBody(); |
|
136 | 136 | |
137 | 137 | // OpenGraph handling |
138 | 138 | $consumer = new Consumer(); |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | try { |
154 | 154 | $host = parse_url($object->images[0]->url, PHP_URL_HOST); |
155 | 155 | if ($host === false || $host === null) { |
156 | - $this->logger->warning('Could not detect host of open graph image URI for ' . $reference->getId()); |
|
156 | + $this->logger->warning('Could not detect host of open graph image URI for '.$reference->getId()); |
|
157 | 157 | } else { |
158 | 158 | $appData = $this->appDataFactory->get('core'); |
159 | 159 | try { |
@@ -174,9 +174,9 @@ discard block |
||
174 | 174 | } |
175 | 175 | } |
176 | 176 | } catch (GuzzleException $e) { |
177 | - $this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); |
|
177 | + $this->logger->info('Failed to fetch and store the open graph image for '.$reference->getId(), ['exception' => $e]); |
|
178 | 178 | } catch (\Throwable $e) { |
179 | - $this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); |
|
179 | + $this->logger->error('Failed to fetch and store the open graph image for '.$reference->getId(), ['exception' => $e]); |
|
180 | 180 | } |
181 | 181 | } |
182 | 182 | } |