Pimcore\Controller\Router\Route\Frontend::match PHP Method

match() public method

public match ( $path, boolean $partial = false ) : array | boolean
$path
$partial boolean
return array | boolean
    public function match($path, $partial = false)
    {
        // this allows the usage of UTF8 URLs and within static routes
        $path = urldecode($path);
        $front = \Zend_Controller_Front::getInstance();
        $matchFound = false;
        $config = Config::getSystemConfig();
        $routeingDefaults = Tool::getRoutingDefaults();
        $params = array_merge($_GET, $_POST);
        $params = array_merge($routeingDefaults, $params);
        // set the original path
        $originalPath = $path;
        // check for password protection (http auth)
        if ($config->general->http_auth) {
            $username = $config->general->http_auth->username;
            $password = $config->general->http_auth->password;
            if ($username && $password && (!Tool::isFrontentRequestByAdmin() || !Tool\Authentication::authenticateSession())) {
                $adapter = new \Zend_Auth_Adapter_Http(["accept_schemes" => "basic", "realm" => Tool::getHostname()]);
                $basicResolver = new \Pimcore\Helper\Auth\Adapter\Http\ResolverStatic($username, $password);
                $adapter->setBasicResolver($basicResolver);
                $adapter->setRequest($front->getRequest());
                $adapter->setResponse($front->getResponse());
                $result = $adapter->authenticate();
                if (!$result->isValid()) {
                    // Bad userame/password, or canceled password prompt
                    echo "Authentication Required";
                    $front->getResponse()->sendResponse();
                    exit;
                }
            }
        }
        // check for a registered site
        try {
            // do not initialize a site if it is a "special" admin request
            if (!Tool::isFrontentRequestByAdmin()) {
                $domain = Tool::getHostname();
                $site = \Zend_Registry::isRegistered("pimcore_site") ? \Zend_Registry::get("pimcore_site") : Site::getByDomain($domain);
                $path = $site->getRootPath() . $path;
                \Zend_Registry::set("pimcore_site", $site);
            }
        } catch (\Exception $e) {
        }
        // test if there is a suitable redirect with override = all (=> priority = 99)
        $this->checkForRedirect($originalPath, true);
        // do not allow requests including /index.php/ => SEO
        // this is after the first redirect check, to allow redirects in index.php?xxx
        if (preg_match("@^/index.php(.*)@", $_SERVER["REQUEST_URI"], $matches) && strtolower($_SERVER["REQUEST_METHOD"]) == "get") {
            $redirectUrl = $matches[1];
            $redirectUrl = ltrim($redirectUrl, "/");
            $redirectUrl = "/" . $redirectUrl;
            header("Location: " . $redirectUrl, true, 301);
            exit;
        }
        // redirect to the main domain if specified
        try {
            $hostRedirect = null;
            if ($config->general->redirect_to_maindomain && $config->general->domain && $config->general->domain != Tool::getHostname() && !Site::isSiteRequest() && !Tool::isFrontentRequestByAdmin()) {
                $hostRedirect = $config->general->domain;
            }
            if (Site::isSiteRequest()) {
                $site = Site::getCurrentSite();
                if ($site->getRedirectToMainDomain() && $site->getMainDomain() != Tool::getHostname()) {
                    $hostRedirect = $site->getMainDomain();
                }
            }
            if ($hostRedirect && !isset($_GET["pimcore_disable_host_redirect"])) {
                $url = ($front->getRequest()->isSecure() ? "https" : "http") . "://" . $hostRedirect . $_SERVER["REQUEST_URI"];
                header("HTTP/1.1 301 Moved Permanently");
                header("Location: " . $url, true, 301);
                // log all redirects to the redirect log
                \Pimcore\Log\Simple::log("redirect", Tool::getAnonymizedClientIp() . " \t Host-Redirect Source: " . $_SERVER["REQUEST_URI"] . " -> " . $url);
                exit;
            }
        } catch (\Exception $e) {
        }
        // check for direct definition of controller/action
        if (!empty($_REQUEST["controller"]) && !empty($_REQUEST["action"])) {
            $matchFound = true;
        }
        // test if there is a suitable page
        if (!$matchFound) {
            try {
                $document = Document::getByPath($path);
                // check for a pretty url inside a site
                if (!$document && Site::isSiteRequest()) {
                    $documentService = new Document\Service();
                    $sitePrettyDocId = $documentService->getDocumentIdByPrettyUrlInSite(Site::getCurrentSite(), $originalPath);
                    if ($sitePrettyDocId) {
                        if ($sitePrettyDoc = Document::getById($sitePrettyDocId)) {
                            $document = $sitePrettyDoc;
                            // undo the modification of the path by the site detection (prefixing with site root path)
                            // this is not necessary when using pretty-urls and will cause problems when validating the
                            // prettyUrl later (redirecting to the prettyUrl in the case the page was called by the real path)
                            $path = $originalPath;
                        }
                    }
                }
                // check for a parent hardlink with childs
                if (!$document instanceof Document) {
                    $hardlinkedParentDocument = $this->getNearestDocumentByPath($path, true);
                    if ($hardlinkedParentDocument instanceof Document\Hardlink) {
                        if ($hardLinkedDocument = Document\Hardlink\Service::getChildByPath($hardlinkedParentDocument, $path)) {
                            $document = $hardLinkedDocument;
                        }
                    }
                }
                // check for direct hardlink
                if ($document instanceof Document\Hardlink) {
                    $hardlinkParentDocument = $document;
                    $document = Document\Hardlink\Service::wrap($hardlinkParentDocument);
                }
                if ($document instanceof Document) {
                    if (in_array($document->getType(), self::getDirectRouteDocumentTypes())) {
                        if (Tool::isFrontentRequestByAdmin() || $document->isPublished()) {
                            $redirectTargetUrl = $originalPath;
                            // check for a pretty url, and if the document is called by that, otherwise redirect to pretty url
                            if ($document instanceof Document\Page && !$document instanceof Document\Hardlink\Wrapper\WrapperInterface && $document->getPrettyUrl() && !Tool::isFrontentRequestByAdmin()) {
                                if (rtrim(strtolower($document->getPrettyUrl()), " /") != rtrim(strtolower($originalPath), "/")) {
                                    $redirectTargetUrl = $document->getPrettyUrl();
                                }
                            }
                            $params["document"] = $document;
                            if ($controller = $document->getController()) {
                                $params["controller"] = $controller;
                                $params["action"] = "index";
                            }
                            if ($action = $document->getAction()) {
                                $params["action"] = $action;
                            }
                            if ($module = $document->getModule()) {
                                $params["module"] = $module;
                            }
                            // check for a trailing slash in path, if exists, redirect to this page without the slash
                            // the only reason for this is: SEO, Analytics, ... there is no system specific reason, pimcore would work also with a trailing slash without problems
                            // use $originalPath because of the sites
                            // only do redirecting with GET requests
                            if (strtolower($_SERVER["REQUEST_METHOD"]) == "get") {
                                if ($config->documents->allowtrailingslash) {
                                    if ($config->documents->allowtrailingslash == "no") {
                                        if (substr($redirectTargetUrl, strlen($redirectTargetUrl) - 1, 1) == "/" && $redirectTargetUrl != "/") {
                                            $redirectTargetUrl = rtrim($redirectTargetUrl, "/");
                                        }
                                    }
                                }
                                // only allow the original key of a document to be the URL (lowercase/uppercase)
                                if ($redirectTargetUrl != rawurldecode($document->getFullPath())) {
                                    $redirectTargetUrl = $document->getFullPath();
                                }
                            }
                            if ($redirectTargetUrl !== $originalPath) {
                                if ($_SERVER["QUERY_STRING"]) {
                                    $redirectTargetUrl .= "?" . $_SERVER["QUERY_STRING"];
                                }
                                header("Location: " . $redirectTargetUrl, true, 301);
                                exit;
                            }
                            $matchFound = true;
                        }
                    } elseif ($document->getType() == "link") {
                        // if the document is a link just redirect to the location/href of the link
                        header("Location: " . $document->getHref(), true, 301);
                        exit;
                    }
                }
            } catch (\Exception $e) {
                // no suitable page found
                $foo = "bar";
            }
        }
        // test if there is a suitable static route
        if (!$matchFound) {
            try {
                $list = new Staticroute\Listing();
                $list->setOrder(function ($a, $b) {
                    if ($a["priority"] == $b["priority"]) {
                        return 0;
                    }
                    return $a["priority"] < $b["priority"] ? 1 : -1;
                });
                $routes = $list->load();
                foreach ($routes as $route) {
                    if (!$matchFound) {
                        $routeParams = $route->match($originalPath, $params);
                        if ($routeParams) {
                            $params = $routeParams;
                            // try to get nearest document to the route
                            $document = $this->getNearestDocumentByPath($path, false, ["page", "snippet", "hardlink"]);
                            if ($document instanceof Document\Hardlink) {
                                $document = Document\Hardlink\Service::wrap($document);
                            }
                            $params["document"] = $document;
                            $matchFound = true;
                            Staticroute::setCurrentRoute($route);
                            // add the route object also as parameter to the request object, this is needed in
                            // Pimcore_Controller_Action_Frontend::getRenderScript()
                            // to determine if a call to an action was made through a staticroute or not
                            // more on that infos see Pimcore_Controller_Action_Frontend::getRenderScript()
                            $params["pimcore_request_source"] = "staticroute";
                            break;
                        }
                    }
                }
            } catch (\Exception $e) {
                // no suitable route found
            }
        }
        // test if there is a suitable redirect
        if (!$matchFound) {
            $this->checkForRedirect($originalPath, false);
        }
        if (!$matchFound) {
            return false;
        }
        // remove pimcore magic parameters
        unset($params["pimcore_outputfilters_disabled"]);
        unset($params["pimcore_document"]);
        unset($params["nocache"]);
        return $params;
    }