Lines: 79.37%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Service; | |
| 6 | ||
| 7 | use Drupal\Component\Utility\Unicode; | |
| 8 | use Drupal\path_alias\AliasManagerInterface; | |
| 9 | use Drupal\visitors\VisitorsReferrerInterface; | |
| 10 | use Symfony\Component\HttpFoundation\Request; | |
| 11 | use Symfony\Component\HttpFoundation\RequestStack; | |
| 12 | use Symfony\Component\Routing\Exception\ExceptionInterface; | |
| 13 | use Symfony\Component\Routing\Matcher\RequestMatcherInterface; | |
| 14 | ||
| 15 | /** | |
| 16 | * Referrer url service. | |
| 17 | */ | |
| 18 | final class Referrer implements VisitorsReferrerInterface { | |
| 19 | ||
| 20 | /** | |
| 21 | * The URL. | |
| 22 | * | |
| 23 | * @var array | |
| 24 | */ | |
| 25 | private $url; | |
| 26 | ||
| 27 | /** | |
| 28 | * The host of the page that was viewed. | |
| 29 | * | |
| 30 | * @var string | |
| 31 | */ | |
| 32 | private $pageHost = ''; | |
| 33 | ||
| 34 | /** | |
| 35 | * The router matcher. | |
| 36 | * | |
| 37 | * @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface | |
| 38 | */ | |
| 39 | private $routerMatcher; | |
| 40 | ||
| 41 | /** | |
| 42 | * The alias manager. | |
| 43 | * | |
| 44 | * @var \Drupal\path_alias\AliasManagerInterface|null | |
| 45 | */ | |
| 46 | private $aliasManager; | |
| 47 | ||
| 48 | /** | |
| 49 | * The request stack. | |
| 50 | * | |
| 51 | * @var \Symfony\Component\HttpFoundation\RequestStack|null | |
| 52 | */ | |
| 53 | private $requestStack; | |
| 54 | ||
| 55 | /** | |
| 56 | * Constructs a new Referrer. | |
| 57 | * | |
| 58 | * @param \Symfony\Component\Routing\Matcher\RequestMatcherInterface $router_matcher | |
| 59 | * The router matcher. | |
| 60 | * @param \Drupal\path_alias\AliasManagerInterface|null $alias_manager | |
| 61 | * The alias manager. | |
| 62 | * @param \Symfony\Component\HttpFoundation\RequestStack|null $request_stack | |
| 63 | * The request stack. Used to remove the site base path. | |
| 64 | */ | |
| 65 | public function __construct(RequestMatcherInterface $router_matcher, ?AliasManagerInterface $alias_manager, ?RequestStack $request_stack = NULL) { | |
| 66 | 12 | $this->routerMatcher = $router_matcher; |
| 67 | 12 | $this->aliasManager = $alias_manager; |
| 68 | 12 | $this->requestStack = $request_stack; |
| 69 | 12 | $this->url = []; |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * {@inheritdoc} | |
| 74 | */ | |
| 75 | public function setUrl(string $url): void { | |
| 76 | 5 | $parsed = parse_url($url); |
| 77 | 5 | $this->url = is_array($parsed) ? $parsed : []; |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * {@inheritdoc} | |
| 82 | */ | |
| 83 | public function setPageHost(string $host): void { | |
| 84 | 5 | $this->pageHost = $host; |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * {@inheritdoc} | |
| 89 | */ | |
| 90 | public function hostOf(string $url): string { | |
| 91 | 12 | if ($url === '') { |
| 92 | 0 | return ''; |
| 93 | } | |
| 94 | 12 | $parts = parse_url($url); |
| 95 | 12 | if (!is_array($parts)) { |
| 96 | 0 | return ''; |
| 97 | } | |
| 98 | 12 | if (!empty($parts['host'])) { |
| 99 | 11 | return (string) $parts['host']; |
| 100 | } | |
| 101 | ||
| 102 | 1 | $path = (string) ($parts['path'] ?? ''); |
| 103 | 1 | $slash = strpos($path, '/'); |
| 104 | 1 | $host = $slash === FALSE ? $path : substr($path, 0, $slash); |
| 105 | 1 | $colon = strpos($host, ':'); |
| 106 | 1 | if ($colon !== FALSE) { |
| 107 | 0 | $host = substr($host, 0, $colon); |
| 108 | } | |
| 109 | ||
| 110 | 1 | return $host; |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * {@inheritdoc} | |
| 115 | */ | |
| 116 | public function storageFields(string $referrer, string $page_host): array { | |
| 117 | 12 | $empty = [ |
| 118 | 12 | 'referrer_url' => '', |
| 119 | 12 | 'referrer_url_prefix' => NULL, |
| 120 | 12 | 'referrer_internal' => 0, |
| 121 | 12 | 'referrer_path' => '', |
| 122 | 12 | 'referrer_route' => '', |
| 123 | 12 | ]; |
| 124 | 12 | if ($referrer === '') { |
| 125 | 11 | return $empty; |
| 126 | } | |
| 127 | ||
| 128 | 5 | $this->setUrl($referrer); |
| 129 | 5 | $this->setPageHost($page_host); |
| 130 | 5 | $internal = $this->isInternal(); |
| 131 | 5 | $path = ''; |
| 132 | 5 | $route = ''; |
| 133 | 5 | if ($internal) { |
| 134 | 4 | $path = Unicode::truncate($this->getPath() ?? '', 255, FALSE, FALSE); |
| 135 | 4 | $route = Unicode::truncate($this->getRoute() ?? '', 255, FALSE, FALSE); |
| 136 | } | |
| 137 | ||
| 138 | 5 | return [ |
| 139 | 5 | 'referrer_url' => $this->getUrl(), |
| 140 | 5 | 'referrer_url_prefix' => $this->getPrefix(), |
| 141 | 5 | 'referrer_internal' => $internal ? 1 : 0, |
| 142 | 5 | 'referrer_path' => $path, |
| 143 | 5 | 'referrer_route' => $route, |
| 144 | 5 | ]; |
| 145 | } | |
| 146 | ||
| 147 | /** | |
| 148 | * {@inheritdoc} | |
| 149 | */ | |
| 150 | public function getPrefix(): ?int { | |
| 151 | 5 | $prefix = NULL; |
| 152 | 5 | if (isset($this->url['scheme'])) { |
| 153 | 5 | switch ($this->url['scheme']) { |
| 154 | 5 | case 'http': |
| 155 | 4 | $prefix = 0; |
| 156 | 4 | break; |
| 157 | ||
| 158 | 5 | case 'https': |
| 159 | 5 | $prefix = 2; |
| 160 | 0 | break; |
| 161 | } | |
| 162 | } | |
| 163 | ||
| 164 | 5 | if (isset($this->url['host']) && str_starts_with($this->url['host'], 'www.')) { |
| 165 | 1 | $prefix = ($prefix ?? 0) + 1; |
| 166 | } | |
| 167 | ||
| 168 | 5 | return $prefix; |
| 169 | } | |
| 170 | ||
| 171 | /** | |
| 172 | * {@inheritdoc} | |
| 173 | */ | |
| 174 | public function getHost($ignore_www = TRUE): ?string { | |
| 175 | 5 | $host = NULL; |
| 176 | 5 | if (isset($this->url['host'])) { |
| 177 | 5 | $host = $this->url['host']; |
| 178 | } | |
| 179 | if ( | |
| 180 | 5 | $host |
| 181 | && $ignore_www | |
| 182 | 5 | && str_starts_with($host, 'www.')) { |
| 183 | 1 | $host = substr($host, 4); |
| 184 | } | |
| 185 | ||
| 186 | 5 | return $host; |
| 187 | } | |
| 188 | ||
| 189 | /** | |
| 190 | * {@inheritdoc} | |
| 191 | */ | |
| 192 | public function isInternal(): bool { | |
| 193 | 5 | $host = $this->comparableHost($this->getHost(TRUE)); |
| 194 | 5 | $page_host = $this->comparableHost($this->pageHost); |
| 195 | 5 | if ($host === '' || $page_host === '') { |
| 196 | 0 | return FALSE; |
| 197 | } | |
| 198 | ||
| 199 | 5 | return $host === $page_host; |
| 200 | } | |
| 201 | ||
| 202 | /** | |
| 203 | * {@inheritdoc} | |
| 204 | */ | |
| 205 | public function getType(): ?int { | |
| 206 | 0 | $type = NULL; |
| 207 | 0 | if (empty($this->url['path'])) { |
| 208 | 0 | $type = 1; |
| 209 | } | |
| 210 | 0 | elseif ($this->isInternal()) { |
| 211 | 0 | $type = 3; |
| 212 | } | |
| 213 | ||
| 214 | 0 | return $type; |
| 215 | } | |
| 216 | ||
| 217 | /** | |
| 218 | * {@inheritdoc} | |
| 219 | */ | |
| 220 | public function getRoute(): ?string { | |
| 221 | 4 | if (!$this->isInternal()) { |
| 222 | 0 | return NULL; |
| 223 | } | |
| 224 | 4 | $path = $this->getPath(); |
| 225 | 4 | if ($path === NULL || $path === '') { |
| 226 | 0 | return NULL; |
| 227 | } | |
| 228 | 4 | if ($path[0] !== '/') { |
| 229 | 0 | $path = '/' . $path; |
| 230 | } | |
| 231 | ||
| 232 | try { | |
| 233 | 4 | $result = $this->routerMatcher->matchRequest(Request::create($path)); |
| 234 | } | |
| 235 | 1 | catch (ExceptionInterface) { |
| 236 | 1 | return NULL; |
| 237 | } | |
| 238 | ||
| 239 | 3 | if (empty($result['_route'])) { |
| 240 | 0 | return NULL; |
| 241 | } | |
| 242 | ||
| 243 | 3 | return (string) $result['_route']; |
| 244 | } | |
| 245 | ||
| 246 | /** | |
| 247 | * {@inheritdoc} | |
| 248 | */ | |
| 249 | public function getPath(): ?string { | |
| 250 | 4 | if (!$this->isInternal() || !isset($this->url['path']) || $this->url['path'] === '') { |
| 251 | 0 | return NULL; |
| 252 | } | |
| 253 | ||
| 254 | 4 | $path = $this->url['path']; |
| 255 | 4 | if ($path[0] !== '/') { |
| 256 | 0 | $path = '/' . $path; |
| 257 | } | |
| 258 | 4 | $path = $this->stripBasePath($path); |
| 259 | 4 | if ($this->aliasManager) { |
| 260 | 4 | $path = $this->aliasManager->getPathByAlias($path); |
| 261 | } | |
| 262 | ||
| 263 | 4 | return $path; |
| 264 | } | |
| 265 | ||
| 266 | /** | |
| 267 | * Removes the site base path and front controller. | |
| 268 | * | |
| 269 | * @param string $path | |
| 270 | * A path from the referrer URL. | |
| 271 | * | |
| 272 | * @return string | |
| 273 | * The path Drupal routes against. | |
| 274 | */ | |
| 275 | private function stripBasePath(string $path): string { | |
| 276 | 4 | $request = $this->requestStack?->getCurrentRequest(); |
| 277 | 4 | if ($request) { |
| 278 | 4 | $base = $request->getBasePath(); |
| 279 | 4 | $prefixed = $base !== '' && str_starts_with($path, $base . '/'); |
| 280 | 4 | if ($base !== '' && ($path === $base || $prefixed)) { |
| 281 | 2 | $path = substr($path, strlen($base)); |
| 282 | } | |
| 283 | } | |
| 284 | 4 | if ($path === '/index.php' || str_starts_with($path, '/index.php/')) { |
| 285 | 1 | $path = substr($path, strlen('/index.php')); |
| 286 | } | |
| 287 | 4 | if ($path === '') { |
| 288 | 0 | $path = '/'; |
| 289 | } | |
| 290 | ||
| 291 | 4 | return $path; |
| 292 | } | |
| 293 | ||
| 294 | /** | |
| 295 | * {@inheritdoc} | |
| 296 | */ | |
| 297 | public function getUrl(): string { | |
| 298 | ||
| 299 | 5 | $host = $this->getHost() ?? ''; |
| 300 | 5 | $port = isset($this->url['port']) ? ':' . $this->url['port'] : ''; |
| 301 | 5 | $path = $this->url['path'] ?? ''; |
| 302 | 5 | $query = isset($this->url['query']) ? '?' . $this->url['query'] : ''; |
| 303 | 5 | $fragment = isset($this->url['fragment']) ? '#' . $this->url['fragment'] : ''; |
| 304 | ||
| 305 | 5 | return "$host$port$path$query$fragment"; |
| 306 | } | |
| 307 | ||
| 308 | /** | |
| 309 | * {@inheritdoc} | |
| 310 | */ | |
| 311 | public function getInternalKeyword(): ?string { | |
| 312 | 0 | $keyword = NULL; |
| 313 | 0 | if (isset($this->url['query'])) { |
| 314 | 0 | $params = []; |
| 315 | 0 | parse_str($this->url['query'], $params); |
| 316 | 0 | $keyword = $params['keys'] ?? NULL; |
| 317 | } | |
| 318 | ||
| 319 | 0 | return $keyword; |
| 320 | } | |
| 321 | ||
| 322 | /** | |
| 323 | * Host used when comparing a referrer with the viewed page. | |
| 324 | * | |
| 325 | * @param string|null $host | |
| 326 | * The host. | |
| 327 | * | |
| 328 | * @return string | |
| 329 | * The host without a leading www., lowercased. | |
| 330 | */ | |
| 331 | private function comparableHost(?string $host): string { | |
| 332 | 5 | if ($host === NULL || $host === '') { |
| 333 | 0 | return ''; |
| 334 | } | |
| 335 | 5 | $host = strtolower($host); |
| 336 | 5 | if (str_starts_with($host, 'www.')) { |
| 337 | 0 | return substr($host, 4); |
| 338 | } | |
| 339 | ||
| 340 | 5 | return $host; |
| 341 | } | |
| 342 | ||
| 343 | } |