<commit_before><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 */

require_once 'Zend/Controller/Plugin/Abstract.php';

class Pimcore_Controller_Plugin_Cache extends Zend_Controller_Plugin_Abstract {

    protected $cacheKey;
    protected $enabled = true;
    protected $lifetime = null;

    public function routeStartup(Zend_Controller_Request_Abstract $request) {

        $requestUri = $request->getRequestUri();
        $excludePatterns = array();

        // only enable GET method
        if (!$request->isGet()) {
            return $this->disable();
        }

        try {
            $conf = Pimcore_Config::getSystemConfig();
            if ($conf->cache) {

                $conf = $conf->cache;

                if (!$conf->enabled) {
                    return $this->disable();
                }

                if ($conf->lifetime) {
                    $this->lifetime = (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 (isset($_COOKIE[trim($cookie)])) {
                            return $this->disable();
                        }
                    }
                }
            }
            else {
                return $this->disable();
            }
        }
        catch (Exception $e) {
            return $this->disable();
        }

        foreach ($excludePatterns as $pattern) {
            if (preg_match($pattern, $requestUri)) {
                return $this->disable();
            }
        }

        $appendKey = "";
        // this is for example for the image-data-uri plugin
        if ($request->getParam("pimcore_cache_tag_suffix")) {
            $tags = $request->getParam("pimcore_cache_tag_suffix");
            if (is_array($tags)) {
                $appendKey = "_" . implode("_", $tags);
            }
        }

        $this->cacheKey = "output_" . md5($_SERVER["HTTP_HOST"] . $requestUri) . $appendKey;

        if ($cacheItem = Pimcore_Model_Cache::load($this->cacheKey, true)) {
            header("X-Pimcore-Cache-Tag: " . $this->cacheKey, true, 200);
            header("X-Pimcore-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;
        }
    }

    public function disable() {
        $this->enabled = false;
        return true;
    }

    public function dispatchLoopShutdown() {
        if ($this->enabled && $this->getResponse()->getHttpResponseCode() == 200) {
            try {
                $cacheItem = array(
                    "headers" => $this->getResponse()->getHeaders(),
                    "rawHeaders" => $this->getResponse()->getRawHeaders(),
                    "content" => $this->getResponse()->getBody(),
                    "date" => Zend_Date::now()->getIso()
                );
                
                Pimcore_Model_Cache::save($cacheItem, $this->cacheKey, array("output"), $this->lifetime, 1000);
            }
            catch (Exception $e) {
                return;
            }
        }
    }
}<commit_msg>PIMCORE-1187 - Varnish compatibility<commit_after><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 */

require_once 'Zend/Controller/Plugin/Abstract.php';

class Pimcore_Controller_Plugin_Cache extends Zend_Controller_Plugin_Abstract {

    protected $cacheKey;
    protected $enabled = true;
    protected $lifetime = null;

    public function routeStartup(Zend_Controller_Request_Abstract $request) {

        $requestUri = $request->getRequestUri();
        $excludePatterns = array();

        // only enable GET method
        if (!$request->isGet()) {
            return $this->disable();
        }

        try {
            $conf = Pimcore_Config::getSystemConfig();
            if ($conf->cache) {

                $conf = $conf->cache;

                if (!$conf->enabled) {
                    return $this->disable();
                }

                if ($conf->lifetime) {
                    $this->lifetime = (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 (isset($_COOKIE[trim($cookie)])) {
                            return $this->disable();
                        }
                    }
                }
            }
            else {
                return $this->disable();
            }
        }
        catch (Exception $e) {
            return $this->disable();
        }

        foreach ($excludePatterns as $pattern) {
            if (preg_match($pattern, $requestUri)) {
                return $this->disable();
            }
        }

        $appendKey = "";
        // this is for example for the image-data-uri plugin
        if ($request->getParam("pimcore_cache_tag_suffix")) {
            $tags = $request->getParam("pimcore_cache_tag_suffix");
            if (is_array($tags)) {
                $appendKey = "_" . implode("_", $tags);
            }
        }

        $this->cacheKey = "output_" . md5($_SERVER["HTTP_HOST"] . $requestUri) . $appendKey;

        if ($cacheItem = Pimcore_Model_Cache::load($this->cacheKey, true)) {
            header("X-Pimcore-Cache-Tag: " . $this->cacheKey, true, 200);
            header("X-Pimcore-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;
        }
    }

    public function disable() {
        $this->enabled = false;
        return true;
    }

    public function dispatchLoopShutdown() {
        if ($this->enabled && $this->getResponse()->getHttpResponseCode() == 200) {
            try {

                if($this->lifetime) {
                    // add cache control for proxies and http-caches like varnish, ...
                    $this->getResponse()->setHeader("Cache-Control", "public, max-age=" . $this->lifetime, true);

                    // add expire header
                    $this->getResponse()->setHeader("Expires", Zend_Date::now()->add($this->lifetime)->get(Zend_Date::RFC_1123));
                }

                $cacheItem = array(
                    "headers" => $this->getResponse()->getHeaders(),
                    "rawHeaders" => $this->getResponse()->getRawHeaders(),
                    "content" => $this->getResponse()->getBody(),
                    "date" => Zend_Date::now()->getIso()
                );
                
                Pimcore_Model_Cache::save($cacheItem, $this->cacheKey, array("output"), $this->lifetime, 1000);
            }
            catch (Exception $e) {
                return;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.13746258346764909, 'fraction_numerical': 0.0052958784250518075, 'mean_word_length': 1.5557516916740217, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 92, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4256246', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2243, 'n_words': 454}