Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/linkor.redirectpro/lib/ |
Upload File : |
<? namespace Linkor\Redirectpro; class Redirect { public $options; // Settings from config/options.php public $redirects; // Redirect array from config/urls.php public $host; // Domain name public $protocol; // http/https public $port; // public $currentUri; // public $uri = null; // Final uri public $isAbsoluteUrl = false; // public $changed = false; // Is url changed public $arUrl; // Separated url public $url; // Url redirected to public $status; // Redirect status (301/302) function __construct($options, $redirects) { // Init options $this->options = $options; $this->redirects = $redirects; $this->host = $_SERVER["SERVER_NAME"]; $this->protocol = !empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] != "off" ? "https" : "http"; $this->port = !empty($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443" ? (":" . $_SERVER["SERVER_PORT"]) : ""; $this->currentUri = urldecode($_SERVER["REQUEST_URI"]); //trim first double slash if (substr($this->currentUri, 0, 2) === "//") { $this->currentUri = substr_replace($this->currentUri, "/", 0, 2); $this->changed = true; } $this->arUrl = parse_url($this->currentUri); // $this->createBaseRedirectsUri(); // Get final URI $this->createUri(); // Check URI in config/urls.php $this->useRedirectList(); // $this->executeFinalRedirect(); } function createBaseRedirectsUri() { $this->redirectWWW(); $this->redirectIndex(); $this->redirectSlash(); $this->redirectMultislash(); $this->redirectManyslash(); $this->redirectFromUppercase(); } function redirectWWW() { if ($this->options["redirect_www"] == "Y" && substr($_SERVER["SERVER_NAME"], 0, 4) == "www.") { $this->host = substr($_SERVER["SERVER_NAME"], 4); $this->url = $this->currentUri; } } function redirectIndex() { if ($this->options["redirect_index"] == "Y") { $tmp = rtrim($this->arUrl["path"], "/"); if (basename($tmp) == "index.php") { $dname = dirname($tmp); $this->arUrl["path"] = ($dname != DIRECTORY_SEPARATOR ? $dname : "") . "/"; $this->changed = true; } } } function redirectSlash() { if ($this->options["redirect_slash"] == "Y") { $tmp = basename(rtrim($this->arUrl["path"], "/")); if (substr($this->arUrl["path"], -1, 1) != "/" && substr($tmp, -4) != ".php" && substr($tmp, -4) != ".htm" && substr($tmp, -5) != ".html") { $this->arUrl["path"] .= "/"; $this->changed = true; } } } function redirectMultislash() { if ($this->options["redirect_multislash"] == "Y") { if (strpos($this->arUrl["path"], "//") !== false) { $this->arUrl["path"] = preg_replace('{/+}s', "/", $this->arUrl["path"]); $this->changed = true; } } } function redirectManyslash() { if ($this->options["redirect_manyslash"] == "Y") { $slashCount = 15; if (substr_count($this->arUrl["path"], "/") > $slashCount) { $urlParts = explode("/", $this->arUrl["path"]); $this->arUrl["path"] = "/"; for ($i = 1; $i < $slashCount; $i++) { $this->arUrl["path"] .= $urlParts[$i] . "/"; } $this->changed = true; } } } function redirectFromUppercase() { if ($this->options["redirect_from_uppercase"] == "Y") { if ($this->arUrl["path"] != strtolower($this->arUrl["path"])) { $this->arUrl["path"] = strtolower($this->arUrl["path"]); $this->changed = true; } } } function createUri() { if ($this->changed) { $this->url = $this->arUrl["path"]; if (!empty($this->arUrl["query"])) { $this->url .= "?" . $this->arUrl["query"]; } } } function useRedirectList($currentUri = false) { if ($this->options["use_redirect_urls"] == "Y") { if (!$currentUri) { $currentUri = $this->currentUri; if ($this->options["redirect_ignore_query"] == "Y") { $currentUri = parse_url($currentUri)["path"]; } } if (isset($this->redirects[$currentUri])) { list($this->url, $this->status) = $this->redirects[$currentUri]; if (substr($this->url, 0, 7) == "http://" || substr($this->url, 0, 8) == "https://") { $this->isAbsoluteUrl = true; } $this->useRedirectList($this->url); } else { foreach ($this->redirects as $fromUri => $v) { list($toUri, $this->status, $partUrl) = $v; if ($partUrl != "Y" || ($partUrl == "Y" && strpos($fromUri, "*") === false)) { continue; } $reFromUri = '{^' . str_replace("\*", "(.+)", preg_quote($fromUri)) . '}s'; if (preg_match($reFromUri, $currentUri, $m)) { $tmp = []; foreach ($m as $matchIdx => $matchValue) { if ($matchIdx > 0) { $tmp['{' . $matchIdx . '}'] = $matchValue; } } $this->url = str_replace(array_keys($tmp), array_values($tmp), $toUri); break; } } } } $this->status = ($this->status == "302") ? "302 Found" : "301 Moved Permanently"; } function executeFinalRedirect() { if (!empty($this->url)) { if ($this->isAbsoluteUrl) { LocalRedirect($this->url, true, $this->status); } else { LocalRedirect($this->protocol . "://" . $this->host . $this->port . $this->url, true, $this->status); } exit; } } }