|
| 1 | +<?php namespace Webmaxx\Seo\Classes; |
| 2 | + |
| 3 | +use Webmaxx\Seo\Models\Settings; |
| 4 | + |
| 5 | +class UrlNormalizer |
| 6 | +{ |
| 7 | + use \Winter\Storm\Support\Traits\Singleton; |
| 8 | + |
| 9 | + /** |
| 10 | + * Settings. |
| 11 | + * |
| 12 | + * @var array |
| 13 | + */ |
| 14 | + protected $settings = []; |
| 15 | + |
| 16 | + /** |
| 17 | + * Paths to ignore |
| 18 | + * |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + protected $pathsToIgnore = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * Paths to always ignore |
| 25 | + * |
| 26 | + * @var array |
| 27 | + */ |
| 28 | + protected $alwaysIgnore = [ |
| 29 | + 'backend/*' |
| 30 | + ]; |
| 31 | + |
| 32 | + /** |
| 33 | + * Initialize this singleton. |
| 34 | + * |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + protected function init() |
| 38 | + { |
| 39 | + $this->settings = Settings::instance(); |
| 40 | + |
| 41 | + if (!empty($this->settings->url_ignore)) { |
| 42 | + $this->pathsToIgnore = array_merge( |
| 43 | + $this->alwaysIgnore, |
| 44 | + preg_split( |
| 45 | + '/[\r\n]+/', |
| 46 | + $this->settings->url_ignore, |
| 47 | + -1, |
| 48 | + PREG_SPLIT_NO_EMPTY |
| 49 | + ) |
| 50 | + ); |
| 51 | + } else { |
| 52 | + $this->pathsToIgnore = $this->alwaysIgnore; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Normalize a given URL using your URL normalisation settings. |
| 58 | + * |
| 59 | + * This will ignore any URLs that appear to be external. You may force the normalisation if you wish. |
| 60 | + * |
| 61 | + * @param string $url |
| 62 | + * @param bool $force |
| 63 | + * @return string The normalized URL |
| 64 | + */ |
| 65 | + public static function normalize(string $url, bool $force = false) |
| 66 | + { |
| 67 | + $instance = self::instance(); |
| 68 | + |
| 69 | + if (!$force && $instance->isExternal($url)) { |
| 70 | + return $url; |
| 71 | + } |
| 72 | + |
| 73 | + $originalUrl = $url; |
| 74 | + $url = parse_url($url); |
| 75 | + |
| 76 | + // If the link is not a HTTP or HTTPS link, return the URL as is |
| 77 | + if (!empty($url['scheme']) && !in_array($url['scheme'], ['http', 'https'])) { |
| 78 | + return \http_build_url($url); |
| 79 | + } |
| 80 | + |
| 81 | + // Set default URL parts, if not provided |
| 82 | + if (empty($url['host'])) { |
| 83 | + $url['host'] = $instance->getHostname(); |
| 84 | + |
| 85 | + // If we cannot determine the hostname, return the URL as is |
| 86 | + if (empty($url['host'])) { |
| 87 | + return \http_build_url($url); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (empty($url['scheme'])) { |
| 92 | + $url['scheme'] = ($instance->getPort() === 443) ? 'https' : 'http'; |
| 93 | + } |
| 94 | + if (empty($url['port'])) { |
| 95 | + if ( |
| 96 | + ($url['scheme'] === 'http' && $instance->getPort() !== 80) |
| 97 | + || ($url['scheme'] === 'https' && $instance->getPort() !== 443) |
| 98 | + ) { |
| 99 | + $url['port'] = $instance->getPort(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Check if path is ignored |
| 104 | + if (empty($url['path'])) { |
| 105 | + $url['path'] = '/'; |
| 106 | + } |
| 107 | + |
| 108 | + if (count($instance->pathsToIgnore) && $url['path'] !== '/') { |
| 109 | + foreach ($instance->pathsToIgnore as $ignorePath) { |
| 110 | + $ignorePath = (substr($ignorePath, 0, 1) !== '/') |
| 111 | + ? '/' . $ignorePath |
| 112 | + : $ignorePath; |
| 113 | + $targetPath = (substr($url['path'], 0, 1) !== '/') |
| 114 | + ? '/' . $url['path'] |
| 115 | + : $url['path']; |
| 116 | + $wildcardPos = strpos($ignorePath, '*'); |
| 117 | + |
| 118 | + if ($wildcardPos !== false) { |
| 119 | + $ignorePath = substr($ignorePath, 0, $wildcardPos); |
| 120 | + $targetPath = substr($targetPath, 0, $wildcardPos); |
| 121 | + } |
| 122 | + |
| 123 | + if ($ignorePath === $targetPath) { |
| 124 | + return $originalUrl; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Add or remove trailing slash if preferenced |
| 130 | + if ($instance->settings->url_trailing_slash !== 'none') { |
| 131 | + // Do not apply trailing slash rules if the URL has an extension |
| 132 | + $extension = pathinfo($url['path'], PATHINFO_EXTENSION); |
| 133 | + $hasSlash = (preg_match('/\/$/', $url['path']) === 1); |
| 134 | + |
| 135 | + if (empty($extension) && $url['path'] !== '/') { |
| 136 | + if ($instance->settings->url_trailing_slash === 'use' && $hasSlash === false) { |
| 137 | + $url['path'] = $url['path'] . '/'; |
| 138 | + } |
| 139 | + if ($instance->settings->url_trailing_slash === 'unuse' && $hasSlash === true) { |
| 140 | + $url['path'] = preg_replace('/\/+$/', '', $url['path']); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Add or remove www prefix if preferenced |
| 146 | + if ($instance->settings->url_www_prefix !== 'none') { |
| 147 | + $hasPrefix = (preg_match('/^www./i', $url['host']) === 1); |
| 148 | + |
| 149 | + if ($instance->settings->url_www_prefix === 'use' && $hasPrefix === false) { |
| 150 | + $url['host'] = 'www.' . $url['host']; |
| 151 | + } |
| 152 | + if ($instance->settings->url_www_prefix === 'unuse' && $hasPrefix === true) { |
| 153 | + $url['host'] = preg_replace('/^www./i', '', $url['host']); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // Add HTTPS if it is forced |
| 158 | + if (empty($url['scheme'])) { |
| 159 | + $url['scheme'] = 'https'; |
| 160 | + } |
| 161 | + if (boolval($instance->settings->url_force_https) === true && $url['scheme'] === 'http') { |
| 162 | + $url['scheme'] = 'https'; |
| 163 | + } |
| 164 | + |
| 165 | + return \http_build_url($url); |
| 166 | + } |
| 167 | + |
| 168 | + public static function doRedirect() |
| 169 | + { |
| 170 | + return self::instance()->settings->url_use_redirect; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Determines if a URL is external, based on the server name. |
| 175 | + * |
| 176 | + * @param string $url |
| 177 | + * @return bool |
| 178 | + */ |
| 179 | + protected function isExternal(string $url) |
| 180 | + { |
| 181 | + $urlHostname = parse_url($url, PHP_URL_HOST) ?? null; |
| 182 | + |
| 183 | + if (empty($urlHostname)) { |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + $serverName = $this->getHostname(); |
| 188 | + |
| 189 | + if (empty($serverName)) { |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + $urlHostname = preg_replace('/www\./i', '', $urlHostname); |
| 194 | + $serverName = preg_replace('/www\./i', '', $serverName); |
| 195 | + |
| 196 | + return (strtolower($serverName) !== strtolower($urlHostname)); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Get the hostname, either from the server variables or from the current URL. |
| 201 | + * |
| 202 | + * @return string|null |
| 203 | + */ |
| 204 | + protected function getHostname() |
| 205 | + { |
| 206 | + return |
| 207 | + $_SERVER['X_FORWARDED_HOST'] |
| 208 | + ?? $_SERVER['SERVER_NAME'] |
| 209 | + ?? parse_url(url()->current(), PHP_URL_HOST) |
| 210 | + ?? null; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Get the port, either from the server variables or from the current URL. |
| 215 | + * |
| 216 | + * @return int |
| 217 | + */ |
| 218 | + protected function getPort() |
| 219 | + { |
| 220 | + return intval( |
| 221 | + $_SERVER['X_FORWARDED_PORT'] |
| 222 | + ?? $_SERVER['SERVER_PORT'] |
| 223 | + ?? parse_url(url()->current(), PHP_URL_PORT) |
| 224 | + ?? 80 |
| 225 | + ); |
| 226 | + } |
| 227 | +} |
0 commit comments