Sanitize content and write it to cache resource

@param \Smarty_Template_Cached  $cached
@param Smarty_Internal_Template $_template
@param bool                     $no_output_filter

@throws \SmartyException
18471
public function removeNoCacheHash(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template,
                                      $no_output_filter)
    {
        $content = ob_get_clean();
        unset($cached->hashes[$_template->compiled->nocache_hash]);
        if (!empty($cached->hashes)) {
            $hash_array = array();
            foreach ($cached->hashes as $hash => $foo) {
                $hash_array[] = "/{$hash}/";
            }
            $content = preg_replace($hash_array, $_template->compiled->nocache_hash, $content);
        }
        $_template->cached->has_nocache_code = false;
        // get text between non-cached items
        $cache_split =
            preg_split("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
                       $content);
        // get non-cached items
        preg_match_all("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
                       $content, $cache_parts);
        $content = '';
        // loop over items, stitch back together
        foreach ($cache_split as $curr_idx => $curr_split) {
            // escape PHP tags in template content
            $content .= preg_replace('/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/',
                                     "<?php echo '\$1'; ?>\n", $curr_split);
            if (isset($cache_parts[0][$curr_idx])) {
                $_template->cached->has_nocache_code = true;
                $content .= $cache_parts[1][$curr_idx];
            }
        }
        if (!$no_output_filter && !$_template->compiled->has_nocache_code &&
            (isset($_template->smarty->autoload_filters['output']) ||
                isset($_template->smarty->registered_filters['output']))
        ) {
            $content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
        }
        // write cache file content
        $this->writeCachedContent($cached, $_template, $content);
    }
389796
public function writeCachedContent(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
    {
        if ($_template->source->handler->recompiled || !($_template->caching == Smarty::CACHING_LIFETIME_CURRENT ||
                $_template->caching == Smarty::CACHING_LIFETIME_SAVED)
        ) {
            // don't write cache file
            return false;
        }
        $content = $_template->smarty->ext->_codeFrame->create($_template, $content, '', true);
        return $this->write($cached, $_template, $content);
    }
700816
public function invalidLoadedCache(Smarty $smarty)
    {
        $smarty->_cache['isCached'] = array();
        if (isset($smarty->ext->_subtemplate)) {
            $smarty->ext->_subtemplate->tplObjects = array();
        }
    }
1152192
public static function invalidLoadedCache(Smarty $smarty)
    {
        foreach ($smarty->template_objects as $tpl) {
            if (isset($tpl->cached)) {
                $tpl->cached->valid = false;
                $tpl->cached->processed = false;
            }
        }
    }
599784
protected function doCompile($_content, $isTemplateSource = false)
    {
        /* here is where the compiling takes place. Smarty
          tags in the templates are replaces with PHP code,
          then written to compiled files. */
        // init the lexer/parser to compile the template
        $this->parser =
            new $this->parser_class(new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $_content), $this),
                                    $this);
        if ($isTemplateSource && $this->template->caching) {
            $this->parser->insertPhpCode("<?php\n\$_smarty_tpl->compiled->nocache_hash = '{$this->nocache_hash}';\n?>\n");
        }
        if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
            $mbEncoding = mb_internal_encoding();
            mb_internal_encoding('ASCII');
        } else {
            $mbEncoding = null;
        }

        if ($this->smarty->_parserdebug) {
            $this->parser->PrintTrace();
            $this->parser->lex->PrintTrace();
        }
        // get tokens from lexer and parse them
        while ($this->parser->lex->yylex()) {
            if ($this->smarty->_parserdebug) {
                echo "<pre>Line {$this->parser->lex->line} Parsing  {$this->parser->yyTokenName[$this->parser->lex->token]} Token " .
                    htmlentities($this->parser->lex->value) . "</pre>";
            }
            $this->parser->doParse($this->parser->lex->token, $this->parser->lex->value);
        }

        // finish parsing process
        $this->parser->doParse(0, 0);
        if ($mbEncoding) {
            mb_internal_encoding($mbEncoding);
        }
        // check for unclosed tags
        if (count($this->_tag_stack) > 0) {
            // get stacked info
            list($openTag, $_data) = array_pop($this->_tag_stack);
            $this->trigger_template_error("unclosed {$this->smarty->left_delimiter}" . $openTag .
                                          "{$this->smarty->right_delimiter} tag");
        }
        // call post compile callbacks
        foreach ($this->postCompileCallbacks as $cb) {
            $parameter = $cb;
            $parameter[0] = $this;
            call_user_func_array($cb[0], $parameter);
        }
        // return compiled code
        return $this->prefixCompiledCode . $this->parser->retvalue . $this->postfixCompiledCode;
    }
1143072
public function process(Smarty_Internal_Template $_template)
    {
        $_smarty_tpl = $_template;
        if ($_template->source->handler->recompiled || !$_template->compiled->exists ||
            $_template->smarty->force_compile || ($_template->smarty->compile_check &&
                $_template->source->getTimeStamp() > $_template->compiled->getTimeStamp())
        ) {
            $this->compileTemplateSource($_template);
            $compileCheck = $_template->smarty->compile_check;
            $_template->smarty->compile_check = false;
            if ($_template->source->handler->recompiled) {
                $level = ob_get_level();
                ob_start();
                try {
                    eval("?>" . $this->content);
                }
                catch (Exception $e) {
                    while (ob_get_level() > $level) {
                        ob_end_clean();
                    }
                    throw $e;
                }
                ob_get_clean();
                $this->content = null;
            } else {
                $this->loadCompiledTemplate($_template);
            }
            $_template->smarty->compile_check = $compileCheck;
        } else {
            $_template->mustCompile = true;
            @include($_template->compiled->filepath);
            if ($_template->mustCompile) {
                $this->compileTemplateSource($_template);
                $compileCheck = $_template->smarty->compile_check;
                $_template->smarty->compile_check = false;
                $this->loadCompiledTemplate($_template);
                $_template->smarty->compile_check = $compileCheck;
            }
        }
        $_template->smarty->ext->_subTemplate->registerSubTemplates($_template);

        $this->processed = true;
    }
1347740
public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null)
    {
        /** @var Smarty_Internal_Template $_smarty_tpl
         * used in included file
         */
        $_smarty_tpl = $_template;

        return @include $_template->cached->filepath;
    }
330192
public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
    {
        $cached->is_locked = true;

        $id = $cached->filepath;
        $name = $cached->source->name . '.lock';
        $this->save($id, $name, null, null, $smarty->locking_timeout, '');
    }
167446
public function is_cached($tpl, $cacheId = null, $compileId = null)
    {
        return $this->isCached($this->makeTemplate($tpl, $cacheId, $compileId));
    }
1238632
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
    {
        $uid = '';
        $sources = array();
        $components = explode('|', $source->name);
        $exists = true;
        foreach ($components as $component) {
            $s = Smarty_Resource::source(null, $source->smarty, $component);
            if ($s->type == 'php') {
                throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type");
            }
            $sources[$s->uid] = $s;
            $uid .= $s->filepath;
            if ($_template && $_template->smarty->compile_check) {
                $exists = $exists && $s->exists;
            }
        }
        $source->components = $sources;
        $source->filepath = $s->filepath;
        $source->uid = sha1($uid);
        if ($_template && $_template->smarty->compile_check) {
            $source->timestamp = $s->timestamp;
            $source->exists = $exists;
        }
        // need the template at getContent()
        $source->template = $_template;
    }
679327
public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
    {
        $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
        $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null;

        $cached->filepath = sha1($cached->source->filepath . $_cache_id . $_compile_id);
        $this->populateTimestamp($cached);
    }
271679
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
    {
        $uid = '';
        $sources = array();
        $components = explode('|', $source->name);
        $exists = true;
        foreach ($components as $component) {
            /* @var \Smarty_Template_Source $_s */
            $_s = Smarty_Template_Source::load(null, $source->smarty, $component);
            if ($_s->type == 'php') {
                throw new SmartyException("Resource type {$_s->type} cannot be used with the extends resource type");
            }
            $sources[ $_s->uid ] = $_s;
            $uid .= $_s->filepath;
            if ($_template) {
                $exists = $exists && $_s->exists;
            }
        }
        $source->components = $sources;
        $source->filepath = $_s->filepath;
        $source->uid = sha1($uid);
        $source->exists = $exists;
        if ($_template) {
            $source->timestamp = $_s->timestamp;
        }
    }
1112722
public function read(Smarty_Internal_Template $_template)
    {
        if (!$_template->source->handler->recompiled) {
            return file_get_contents($this->filepath);
        }
        return isset($this->content) ? $this->content : false;
    }
1071453
private function doCache($fileId, $version, TemplateInterface $template, CacheItem $cacheItem = null)
    {
        $volumeManager = $this->get('phlexible_media_manager.volume_manager');
        $instructionCreator = $this->get('phlexible_media_cache.instruction_creator');
        $instructionProcessor = $this->get('phlexible_media_cache.instruction_processor');

        $volume = $volumeManager->getByFileId($fileId);
        $file = $volume->findFile($fileId);
        if ($version && $file->getVersion() !== $version) {
            $fileVersion = $volume->findFileVersion($fileId, $version);
            if (!$file) {
                return null;
            }
            $input = InputDescriptor::fromFileVersion($fileVersion);
        } else {
            $input = InputDescriptor::fromFile($file);
        }

        if (!file_exists($file->getPhysicalPath())) {
            return null;
        }

        if ($cacheItem) {
            $instruction = new Instruction($input, $template, $cacheItem);
        } else {
            $instruction = $instructionCreator->createInstruction($input, $template);
        }
        $instructionProcessor->processInstruction($instruction);

        return $instruction->getCacheItem();
    }
856842
static function clearAllContentCache( $ignoreINISettings = false )
    {
        if ( !$ignoreINISettings )
        {
            $ini = eZINI::instance();
            $viewCacheEnabled = ( $ini->variable( 'ContentSettings', 'ViewCaching' ) === 'enabled' );
            $templateCacheEnabled = ( $ini->variable( 'TemplateSettings', 'TemplateCache' ) === 'enabled' );
        }
        else
        {
            $viewCacheEnabled = true;
            $templateCacheEnabled = true;
        }

        if ( $viewCacheEnabled || $templateCacheEnabled )
        {
            // view cache and/or ordinary template block cache
            eZContentObject::expireAllCache();

            ezpEvent::getInstance()->notify( 'content/cache/all' );

            // subtree template block caches
            if ( $templateCacheEnabled )
            {
                eZSubtreeCache::cleanupAll();
            }
        }
    }
1026559
public function cacheModifiedCheck(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
    {
        $_isCached = $_template->isCached() && !$_template->compiled->has_nocache_code;
        $_last_modified_date =
            @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
        if ($_isCached && $cached->timestamp <= strtotime($_last_modified_date)) {
            switch (PHP_SAPI) {
                case 'cgi': // php-cgi < 5.3
                case 'cgi-fcgi': // php-cgi >= 5.3
                case 'fpm-fcgi': // php-fpm >= 5.3.3
                    header('Status: 304 Not Modified');
                    break;

                case 'cli':
                    if ( /* ^phpunit */
                    !empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS']) /* phpunit$ */
                    ) {
                        $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = '304 Not Modified';
                    }
                    break;

                default:
                    if ( /* ^phpunit */
                    !empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS']) /* phpunit$ */
                    ) {
                        $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = '304 Not Modified';
                    } else {
                        header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
                    }
                    break;
            }
        } else {
            switch (PHP_SAPI) {
                case 'cli':
                    if ( /* ^phpunit */
                    !empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS']) /* phpunit$ */
                    ) {
                        $_SERVER['SMARTY_PHPUNIT_HEADERS'][] =
                            'Last-Modified: ' . gmdate('D, d M Y H:i:s', $cached->timestamp) . ' GMT';
                    }
                    break;
                default:
                    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $cached->timestamp) . ' GMT');
                    break;
            }
            echo $content;
        }
    }
547035
protected function renderCacheContent($cacheContent, $cacheHit = true)
    {
        if (!($this->config['debug'] && $this->twig)) {
            return $cacheContent['Content'];
        }
        $cacheContent['lastmodify'] = time() - $cacheContent['CreatedAt'];

        return $this->twig->render(
            'TagcacheBundle:html:tagcache_debug_panel.html.twig',
            array('CacheHit' => $cacheHit, 'CacheContent' => $cacheContent, 'uniqueid' => md5(microtime() . rand()))
        );
    }