|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\UpwardConnector\Model; |
| 9 | + |
| 10 | +use Magento\Framework\App\RequestInterface; |
| 11 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 12 | +use Magento\Store\Model\ScopeInterface; |
| 13 | +use Magento\Framework\Escaper; |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Laminas\Http\Exception; |
| 16 | +use Laminas\Http\ClientFactory; |
| 17 | +use Laminas\Http\Client\Adapter\Curl; |
| 18 | + |
| 19 | +class Prerender |
| 20 | +{ |
| 21 | + const XML_PATH_WEB_UPWARD_PRERENDER = 'web/upward/prerender_enabled'; |
| 22 | + const XML_PATH_WEB_UPWARD_PRERENDER_TOKEN = 'web/upward/prerender_token'; |
| 23 | + const XML_PATH_WEB_UPWARD_PRERENDER_URL = 'web/upward/prerender_url'; |
| 24 | + const XML_PATH_WEB_UPWARD_PRERENDER_CRAWLERS = 'web/upward/prerender_crawlers'; |
| 25 | + const XML_PATH_WEB_UPWARD_PRERENDER_ALLOWED_LIST = 'web/upward/prerender_allowed_list'; |
| 26 | + const XML_PATH_WEB_UPWARD_PRERENDER_BLOCKED_LIST = 'web/upward/prerender_blocked_list'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ScopeConfigInterface |
| 30 | + */ |
| 31 | + private $config; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ClientFactory |
| 35 | + */ |
| 36 | + private $clientFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var LoggerInterface |
| 40 | + */ |
| 41 | + private $logger; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Escaper |
| 45 | + */ |
| 46 | + private $escaper; |
| 47 | + |
| 48 | + /** |
| 49 | + * @param ScopeConfigInterface $config |
| 50 | + * @param ClientFactory $clientFactory |
| 51 | + * @param LoggerInterface $logger |
| 52 | + * @param Escaper $escaper |
| 53 | + */ |
| 54 | + public function __construct( |
| 55 | + ScopeConfigInterface $config, |
| 56 | + ClientFactory $clientFactory, |
| 57 | + LoggerInterface $logger, |
| 58 | + Escaper $escaper |
| 59 | + ) { |
| 60 | + $this->config = $config; |
| 61 | + $this->clientFactory = $clientFactory; |
| 62 | + $this->logger = $logger; |
| 63 | + $this->escaper = $escaper; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Send the request to prerender services to get prerendered html content. |
| 68 | + * |
| 69 | + * @param RequestInterface $request |
| 70 | + * @return \Laminas\Http\Response|false |
| 71 | + */ |
| 72 | + public function getPrerenderedPageResponse(RequestInterface $request) |
| 73 | + { |
| 74 | + $headers = [ |
| 75 | + 'User-Agent' => $request->getServer('HTTP_USER_AGENT'), |
| 76 | + ]; |
| 77 | + if ($this->getPrerenderToken()) { |
| 78 | + $headers['X-Prerender-Token'] = $this->getPrerenderToken(); |
| 79 | + } |
| 80 | + |
| 81 | + $protocol = $request->isSecure() ? 'https' : 'http'; |
| 82 | + |
| 83 | + $host = $request->getHttpHost(); |
| 84 | + $path = $request->getRequestUri(); |
| 85 | + // Fix '//' 404 error |
| 86 | + if ($path === '/') { |
| 87 | + $path = ''; |
| 88 | + } |
| 89 | + |
| 90 | + $url = $this->escaper->escapeUrl($this->getPrerenderUrl() . $protocol . '://' . $host . $path); |
| 91 | + |
| 92 | + $config = [ |
| 93 | + 'adapter' => Curl::class, |
| 94 | + 'curloptions' => [ |
| 95 | + CURLOPT_MAXREDIRS => 10, |
| 96 | + CURLOPT_TIMEOUT => 50, |
| 97 | + CURLOPT_FOLLOWLOCATION => true |
| 98 | + ] |
| 99 | + ]; |
| 100 | + |
| 101 | + try { |
| 102 | + $client = $this->clientFactory->create(); |
| 103 | + $client->setUri($url); |
| 104 | + $client->setOptions($config); |
| 105 | + $client->setHeaders($headers); |
| 106 | + |
| 107 | + return $client->send(); |
| 108 | + } catch (Exception\RuntimeException | Exception\InvalidArgumentException $e) { |
| 109 | + $this->logger->critical($e); |
| 110 | + |
| 111 | + return false; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Check if resources should be prerendered. |
| 117 | + * |
| 118 | + * @param RequestInterface $request |
| 119 | + * @return bool |
| 120 | + */ |
| 121 | + public function shouldShowPrerenderedPage(RequestInterface $request) |
| 122 | + { |
| 123 | + if (!$this->getPrerenderUrl() || |
| 124 | + !$this->config->getValue(static::XML_PATH_WEB_UPWARD_PRERENDER, scopeInterface::SCOPE_STORE) |
| 125 | + ) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + $requestUri = $request->getRequestUri(); |
| 129 | + $referer = $request->getHeader('referer'); |
| 130 | + |
| 131 | + if (!$request->isGet()) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + if (!$this->isInAllowedList($requestUri)) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + // we also check for a blocked referer |
| 140 | + $uris = array_filter([$requestUri, ($referer ? $referer : '')]); |
| 141 | + if ($this->isInBlockedList($uris)) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + if (!$this->isCrawlerUserAgent($request)) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get prerender token from configuration. |
| 154 | + * |
| 155 | + * @return string|null |
| 156 | + */ |
| 157 | + private function getPrerenderToken() |
| 158 | + { |
| 159 | + return $this->config->getValue(static::XML_PATH_WEB_UPWARD_PRERENDER_TOKEN); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Get prerender url from configuration. |
| 164 | + * |
| 165 | + * @return string|null |
| 166 | + */ |
| 167 | + private function getPrerenderUrl() |
| 168 | + { |
| 169 | + return $this->config->getValue(static::XML_PATH_WEB_UPWARD_PRERENDER_URL); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Check if user agent is crawler bot. |
| 174 | + * |
| 175 | + * @param RequestInterface $request |
| 176 | + * @return bool |
| 177 | + */ |
| 178 | + private function isCrawlerUserAgent(RequestInterface $request) |
| 179 | + { |
| 180 | + $userAgent = strtolower($request->getServer('HTTP_USER_AGENT')); |
| 181 | + if (!$userAgent) { |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + $bufferAgent = $request->getServer('X-BUFFERBOT'); |
| 186 | + |
| 187 | + // prerender if _escaped_fragment_ is in the query string |
| 188 | + if ($bufferAgent || $request->getQuery('_escaped_fragment_')) { |
| 189 | + return true; |
| 190 | + } |
| 191 | + |
| 192 | + $crawlerUserAgents = $this->getList( |
| 193 | + (string) $this->config->getValue(self::XML_PATH_WEB_UPWARD_PRERENDER_CRAWLERS) |
| 194 | + ); |
| 195 | + |
| 196 | + foreach ($crawlerUserAgents as $crawlerUserAgent) { |
| 197 | + if (strpos(strtolower($userAgent), strtolower($crawlerUserAgent)) !== false) { |
| 198 | + return true; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Checks if uri is in allowed list. |
| 207 | + * |
| 208 | + * @param string $requestUri |
| 209 | + * @return bool |
| 210 | + */ |
| 211 | + private function isInAllowedList(string $requestUri) |
| 212 | + { |
| 213 | + $allowedList = $this->getList( |
| 214 | + (string) $this->config->getValue(self::XML_PATH_WEB_UPWARD_PRERENDER_ALLOWED_LIST) |
| 215 | + ); |
| 216 | + |
| 217 | + return empty($allowedList) || $this->isListed([$requestUri], $allowedList); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Checks if uri is in blocked list. |
| 222 | + * |
| 223 | + * @param array $uris |
| 224 | + * @return bool |
| 225 | + */ |
| 226 | + private function isInBlockedList(array $uris) |
| 227 | + { |
| 228 | + $blockedList = $this->getList( |
| 229 | + (string) $this->config->getValue(self::XML_PATH_WEB_UPWARD_PRERENDER_BLOCKED_LIST) |
| 230 | + ); |
| 231 | + |
| 232 | + return !empty($blockedList) && $this->isListed($uris, $blockedList); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Transforms string from configuration to the array. |
| 237 | + * |
| 238 | + * @param string $list |
| 239 | + * @return string[] array |
| 240 | + */ |
| 241 | + private function getList(string $list) |
| 242 | + { |
| 243 | + return array_filter( |
| 244 | + array_map( |
| 245 | + 'trim', |
| 246 | + preg_split("/(\r\n|\n)/", $list ?? '') |
| 247 | + ) |
| 248 | + ); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Checks if provided uri is listed in the list from configuration. |
| 253 | + * |
| 254 | + * @param array $needles |
| 255 | + * @param array $list |
| 256 | + * @return bool |
| 257 | + */ |
| 258 | + private function isListed(array $needles, array $list) |
| 259 | + { |
| 260 | + foreach ($list as $pattern) { |
| 261 | + foreach ($needles as $needle) { |
| 262 | + if (fnmatch($pattern, $needle)) { |
| 263 | + return true; |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + return false; |
| 269 | + } |
| 270 | +} |
0 commit comments