Pimcore\Controller\Plugin\Cache::routeStartup PHP Method

routeStartup() public method

public routeStartup ( Zend_Controller_Request_Abstract $request ) : boolean | void
$request Zend_Controller_Request_Abstract
return boolean | void
    public function routeStartup(\Zend_Controller_Request_Abstract $request)
    {
        $requestUri = $request->getRequestUri();
        $excludePatterns = [];
        // only enable GET method
        if (!$request->isGet()) {
            return $this->disable();
        }
        // disable the output-cache if browser wants the most recent version
        // unfortunately only Chrome + Firefox if not using SSL
        if (!$request->isSecure()) {
            if (isset($_SERVER["HTTP_CACHE_CONTROL"]) && $_SERVER["HTTP_CACHE_CONTROL"] == "no-cache") {
                return $this->disable("HTTP Header Cache-Control: no-cache was sent");
            }
            if (isset($_SERVER["HTTP_PRAGMA"]) && $_SERVER["HTTP_PRAGMA"] == "no-cache") {
                return $this->disable("HTTP Header Pragma: no-cache was sent");
            }
        }
        try {
            $conf = \Pimcore\Config::getSystemConfig();
            if ($conf->cache) {
                $conf = $conf->cache;
                if (!$conf->enabled) {
                    return $this->disable();
                }
                if (\Pimcore::inDebugMode()) {
                    return $this->disable("in debug mode");
                }
                if ($conf->lifetime) {
                    $this->setLifetime((int) $conf->lifetime);
                }
                if ($conf->excludePatterns) {
                    $confExcludePatterns = explode(",", $conf->excludePatterns);
                    if (!empty($confExcludePatterns)) {
                        $excludePatterns = $confExcludePatterns;
                    }
                }
                if ($conf->excludeCookie) {
                    $cookies = explode(",", strval($conf->excludeCookie));
                    foreach ($cookies as $cookie) {
                        if (!empty($cookie) && isset($_COOKIE[trim($cookie)])) {
                            return $this->disable("exclude cookie in system-settings matches");
                        }
                    }
                }
                // output-cache is always disabled when logged in at the admin ui
                if (isset($_COOKIE["pimcore_admin_sid"])) {
                    return $this->disable("backend user is logged in");
                }
            } else {
                return $this->disable();
            }
        } catch (\Exception $e) {
            Logger::error($e);
            return $this->disable("ERROR: Exception (see debug.log)");
        }
        foreach ($excludePatterns as $pattern) {
            if (@preg_match($pattern, $requestUri)) {
                return $this->disable("exclude path pattern in system-settings matches");
            }
        }
        $deviceDetector = Tool\DeviceDetector::getInstance();
        $device = $deviceDetector->getDevice();
        $deviceDetector->setWasUsed(false);
        $appendKey = "";
        // this is for example for the image-data-uri plugin
        if (isset($_REQUEST["pimcore_cache_tag_suffix"])) {
            $tags = $_REQUEST["pimcore_cache_tag_suffix"];
            if (is_array($tags)) {
                $appendKey = "_" . implode("_", $tags);
            }
        }
        $this->defaultCacheKey = "output_" . md5(\Pimcore\Tool::getHostname() . $requestUri . $appendKey);
        $cacheKeys = [$this->defaultCacheKey . "_" . $device, $this->defaultCacheKey];
        $cacheItem = null;
        foreach ($cacheKeys as $cacheKey) {
            $cacheItem = CacheManager::load($cacheKey, true);
            if ($cacheItem) {
                break;
            }
        }
        if (is_array($cacheItem) && !empty($cacheItem)) {
            header("X-Pimcore-Output-Cache-Tag: " . $cacheKey, true, 200);
            header("X-Pimcore-Output-Cache-Date: " . $cacheItem["date"]);
            foreach ($cacheItem["rawHeaders"] as $header) {
                header($header);
            }
            foreach ($cacheItem["headers"] as $header) {
                header($header['name'] . ': ' . $header['value'], $header['replace']);
            }
            echo $cacheItem["content"];
            exit;
        } else {
            // set headers to tell the client to not cache the contents
            // this can/will be overwritten in $this->dispatchLoopShutdown() if the cache is enabled
            $date = new \DateTime();
            $date->setTimestamp(1);
            $this->getResponse()->setHeader("Expires", $date->format(\DateTime::RFC1123), true);
            $this->getResponse()->setHeader("Cache-Control", "max-age=0, no-cache", true);
        }
    }