Pimcore\Tool::getRequestScheme PHP Method

getRequestScheme() public static method

public static getRequestScheme ( ) : string
return string
    public static function getRequestScheme()
    {
        $requestScheme = \Zend_Controller_Request_Http::SCHEME_HTTP;
        if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
            $requestScheme = \Zend_Controller_Request_Http::SCHEME_HTTPS;
        }
        return $requestScheme;
    }

Usage Example

示例#1
0
 /**
  * Checks for a suitable redirect
  * @throws Exception
  * @param bool $override
  * @return void
  */
 protected function checkForRedirect($override = false)
 {
     // not for admin requests
     if (Tool::isFrontentRequestByAdmin()) {
         return;
     }
     try {
         $front = \Zend_Controller_Front::getInstance();
         $config = Config::getSystemConfig();
         // get current site if available
         $sourceSite = null;
         if (Site::isSiteRequest()) {
             $sourceSite = Site::getCurrentSite();
         }
         $cacheKey = "system_route_redirect";
         if (empty($this->redirects) && !($this->redirects = Cache::load($cacheKey))) {
             $list = new Redirect\Listing();
             $list->setCondition("active = 1");
             $list->setOrder("DESC");
             $list->setOrderKey("priority");
             $this->redirects = $list->load();
             Cache::save($this->redirects, $cacheKey, ["system", "redirect", "route"], null, 998);
         }
         $requestScheme = Tool::getRequestScheme();
         $matchRequestUri = $_SERVER["REQUEST_URI"];
         $matchUrl = Tool::getHostUrl() . $matchRequestUri;
         foreach ($this->redirects as $redirect) {
             $matchAgainst = $matchRequestUri;
             if ($redirect->getSourceEntireUrl()) {
                 $matchAgainst = $matchUrl;
             }
             // if override is true the priority has to be 99 which means that overriding is ok
             if (!$override || $override && $redirect->getPriority() == 99) {
                 if (@preg_match($redirect->getSource(), $matchAgainst, $matches)) {
                     // check for a site
                     if ($sourceSite) {
                         if ($sourceSite->getId() != $redirect->getSourceSite()) {
                             continue;
                         }
                     }
                     array_shift($matches);
                     $target = $redirect->getTarget();
                     if (is_numeric($target)) {
                         $d = Document::getById($target);
                         if ($d instanceof Document\Page || $d instanceof Document\Link || $d instanceof Document\Hardlink) {
                             $target = $d->getFullPath();
                         } else {
                             \Logger::error("Target of redirect no found (Document-ID: " . $target . ")!");
                             continue;
                         }
                     }
                     // support for pcre backreferences
                     $url = replace_pcre_backreferences($target, $matches);
                     if ($redirect->getTargetSite() && !preg_match("@http(s)?://@i", $url)) {
                         try {
                             $targetSite = Site::getById($redirect->getTargetSite());
                             // if the target site is specified and and the target-path is starting at root (not absolute to site)
                             // the root-path will be replaced so that the page can be shown
                             $url = preg_replace("@^" . $targetSite->getRootPath() . "/@", "/", $url);
                             $url = $requestScheme . "://" . $targetSite->getMainDomain() . $url;
                         } catch (\Exception $e) {
                             \Logger::error("Site with ID " . $redirect->getTargetSite() . " not found.");
                             continue;
                         }
                     } elseif (!preg_match("@http(s)?://@i", $url) && $config->general->domain && $redirect->getSourceEntireUrl()) {
                         // prepend the host and scheme to avoid infinite loops when using "domain" redirects
                         $url = ($front->getRequest()->isSecure() ? "https" : "http") . "://" . $config->general->domain . $url;
                     }
                     // pass-through parameters if specified
                     $queryString = $_SERVER["QUERY_STRING"];
                     if ($redirect->getPassThroughParameters() && !empty($queryString)) {
                         $glue = "?";
                         if (strpos($url, "?")) {
                             $glue = "&";
                         }
                         $url .= $glue;
                         $url .= $queryString;
                     }
                     header($redirect->getHttpStatus());
                     header("Location: " . $url, true, $redirect->getStatusCode());
                     // log all redirects to the redirect log
                     \Pimcore\Log\Simple::log("redirect", Tool::getAnonymizedClientIp() . " \t Custom-Redirect ID: " . $redirect->getId() . " , Source: " . $_SERVER["REQUEST_URI"] . " -> " . $url);
                     exit;
                 }
             }
         }
     } catch (\Exception $e) {
         // no suitable route found
     }
 }