library/smarty/libs/sysplugins/smarty_template_cached.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Created by PhpStorm.
       
     4  * User: Uwe Tews
       
     5  * Date: 04.12.2014
       
     6  * Time: 06:08
       
     7  */
       
     8 
       
     9 /**
       
    10  * Smarty Resource Data Object
       
    11  * Cache Data Container for Template Files
       
    12  *
       
    13  * @package    Smarty
       
    14  * @subpackage TemplateResources
       
    15  * @author     Rodney Rehm
       
    16  */
       
    17 class Smarty_Template_Cached {
       
    18     /**
       
    19      * Source Filepath
       
    20      *
       
    21      * @var string
       
    22      */
       
    23     public $filepath = false;
       
    24 
       
    25     /**
       
    26      * Source Content
       
    27      *
       
    28      * @var string
       
    29      */
       
    30     public $content = null;
       
    31 
       
    32     /**
       
    33      * Source Timestamp
       
    34      *
       
    35      * @var integer
       
    36      */
       
    37     public $timestamp = false;
       
    38 
       
    39     /**
       
    40      * Source Existence
       
    41      *
       
    42      * @var boolean
       
    43      */
       
    44     public $exists = false;
       
    45 
       
    46     /**
       
    47      * Cache Is Valid
       
    48      *
       
    49      * @var boolean
       
    50      */
       
    51     public $valid = null;
       
    52 
       
    53     /**
       
    54      * Cache was processed
       
    55      *
       
    56      * @var boolean
       
    57      */
       
    58     public $processed = false;
       
    59 
       
    60     /**
       
    61      * CacheResource Handler
       
    62      *
       
    63      * @var Smarty_CacheResource
       
    64      */
       
    65     public $handler = null;
       
    66 
       
    67     /**
       
    68      * Template Compile Id (Smarty_Internal_Template::$compile_id)
       
    69      *
       
    70      * @var string
       
    71      */
       
    72     public $compile_id = null;
       
    73 
       
    74     /**
       
    75      * Template Cache Id (Smarty_Internal_Template::$cache_id)
       
    76      *
       
    77      * @var string
       
    78      */
       
    79     public $cache_id = null;
       
    80 
       
    81     /**
       
    82      * Id for cache locking
       
    83      *
       
    84      * @var string
       
    85      */
       
    86     public $lock_id = null;
       
    87 
       
    88     /**
       
    89      * flag that cache is locked by this instance
       
    90      *
       
    91      * @var bool
       
    92      */
       
    93     public $is_locked = false;
       
    94 
       
    95     /**
       
    96      * Source Object
       
    97      *
       
    98      * @var Smarty_Template_Source
       
    99      */
       
   100     public $source = null;
       
   101 
       
   102     /**
       
   103      * create Cached Object container
       
   104      *
       
   105      * @param Smarty_Internal_Template $_template template object
       
   106      */
       
   107     public function __construct(Smarty_Internal_Template $_template) {
       
   108         $this->compile_id = $_template->compile_id;
       
   109         $this->cache_id = $_template->cache_id;
       
   110         if (!isset($_template->source)) {
       
   111             $_template->loadSource();
       
   112         }
       
   113         $this->source = $_template->source;
       
   114         if (!class_exists('Smarty_CacheResource', false)) {
       
   115             require SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
       
   116         }
       
   117         $this->handler = Smarty_CacheResource::load($_template->smarty);
       
   118     }
       
   119 
       
   120     /**
       
   121      * @param Smarty_Internal_Template $_template
       
   122      *
       
   123      * @return Smarty_Template_Cached
       
   124      */
       
   125     static function load(Smarty_Internal_Template $_template) {
       
   126         $_template->cached = $cached = new Smarty_Template_Cached($_template);
       
   127         $cached->handler->populate($cached, $_template);
       
   128         // caching enabled ?
       
   129         if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || $_template->source->recompiled) {
       
   130             $cached->valid = false;
       
   131         }
       
   132         return $cached;
       
   133     }
       
   134 
       
   135     /**
       
   136      * Check if cache is valid, lock cache if required
       
   137      *
       
   138      * @param \Smarty_Internal_Template $_template
       
   139      *
       
   140      * @return bool flag true if cache is valid
       
   141      */
       
   142     public function isCached(Smarty_Internal_Template $_template) {
       
   143         if ($this->valid !== null) {
       
   144             return $this->valid;
       
   145         }
       
   146         while (true) {
       
   147             while (true) {
       
   148                 if ($this->exists === false || $_template->smarty->force_compile || $_template->smarty->force_cache) {
       
   149                     $this->valid = false;
       
   150                 } else {
       
   151                     $this->valid = true;
       
   152                 }
       
   153                 if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT && $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)) {
       
   154                     // lifetime expired
       
   155                     $this->valid = false;
       
   156                 }
       
   157                 if ($this->valid && $_template->source->timestamp > $this->timestamp) {
       
   158                     $this->valid = false;
       
   159                 }
       
   160                 if ($this->valid || !$_template->smarty->cache_locking) {
       
   161                     break;
       
   162                 }
       
   163                 if (!$this->handler->locked($_template->smarty, $this)) {
       
   164                     $this->handler->acquireLock($_template->smarty, $this);
       
   165                     break 2;
       
   166                 }
       
   167                 $this->handler->populate($this, $_template);
       
   168             }
       
   169             if ($this->valid) {
       
   170                 if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
       
   171                     // load cache file for the following checks
       
   172                     if ($_template->smarty->debugging) {
       
   173                         Smarty_Internal_Debug::start_cache($_template);
       
   174                     }
       
   175                     if ($this->handler->process($_template, $this) === false) {
       
   176                         $this->valid = false;
       
   177                     } else {
       
   178                         $this->processed = true;
       
   179                     }
       
   180                     if ($_template->smarty->debugging) {
       
   181                         Smarty_Internal_Debug::end_cache($_template);
       
   182                     }
       
   183                 } else {
       
   184                     $this->is_locked = true;
       
   185                     continue;
       
   186                 }
       
   187             } else {
       
   188                 return $this->valid;
       
   189             }
       
   190             if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED && $_template->properties['cache_lifetime'] >= 0 && (time() > ($_template->cached->timestamp + $_template->properties['cache_lifetime']))) {
       
   191                 $this->valid = false;
       
   192             }
       
   193             if ($_template->smarty->cache_locking) {
       
   194                 if (!$this->valid) {
       
   195                     $this->handler->acquireLock($_template->smarty, $this);
       
   196                 } elseif ($this->is_locked) {
       
   197                     $this->handler->releaseLock($_template->smarty, $this);
       
   198                 }
       
   199             }
       
   200             return $this->valid;
       
   201         }
       
   202         return $this->valid;
       
   203     }
       
   204 
       
   205     /**
       
   206      * Process cached template
       
   207      *
       
   208      * @param Smarty_Internal_Template $_template template object
       
   209      */
       
   210     public function process(Smarty_Internal_Template $_template) {
       
   211         if ($this->handler->process($_template, $this) === false) {
       
   212             $this->valid = false;
       
   213         }
       
   214         if ($this->valid) {
       
   215             $this->processed = true;
       
   216         } else {
       
   217             $this->processed = false;
       
   218         }
       
   219     }
       
   220 
       
   221     /**
       
   222      * Render cached template
       
   223      *
       
   224      * @param Smarty_Internal_Template $_template
       
   225      *
       
   226      * @return string
       
   227      * @throws Exception
       
   228      */
       
   229     public function render(Smarty_Internal_Template $_template) {
       
   230         if (!$this->processed) {
       
   231             $this->process($_template);
       
   232         }
       
   233         return $_template->getRenderedTemplateCode();
       
   234     }
       
   235 
       
   236     /**
       
   237      * Write this cache object to handler
       
   238      *
       
   239      * @param Smarty_Internal_Template $_template template object
       
   240      * @param string $content content to cache
       
   241      *
       
   242      * @return boolean success
       
   243      */
       
   244     public function write(Smarty_Internal_Template $_template, $content) {
       
   245         if (!$_template->source->recompiled) {
       
   246             if ($this->handler->writeCachedContent($_template, $content)) {
       
   247                 $this->content = null;
       
   248                 $this->timestamp = time();
       
   249                 $this->exists = true;
       
   250                 $this->valid = true;
       
   251                 $this->processed = false;
       
   252                 if ($_template->smarty->cache_locking) {
       
   253                     $this->handler->releaseLock($_template->smarty, $this);
       
   254                 }
       
   255 
       
   256                 return true;
       
   257             }
       
   258             $this->content = null;
       
   259             $this->timestamp = false;
       
   260             $this->exists = false;
       
   261             $this->valid = false;
       
   262             $this->processed = false;
       
   263         }
       
   264 
       
   265         return false;
       
   266     }
       
   267 
       
   268     /**
       
   269      * Read cache content from handler
       
   270      *
       
   271      * @param Smarty_Internal_Template $_template template object
       
   272      *
       
   273      * @return string content
       
   274      */
       
   275     public function read(Smarty_Internal_Template $_template) {
       
   276         if (!$_template->source->recompiled) {
       
   277             return $this->handler->readCachedContent($_template);
       
   278         }
       
   279         return false;
       
   280     }
       
   281 
       
   282     /**
       
   283      * Sanitize content and write it to cache resource
       
   284      *
       
   285      * @param Smarty_Internal_Template $_template
       
   286      * @param string $content
       
   287      * @param bool $no_output_filter
       
   288      *
       
   289      * @throws SmartyException
       
   290      */
       
   291     public function updateCache(Smarty_Internal_Template $_template, $content, $no_output_filter) {
       
   292         $_template->properties['has_nocache_code'] = false;
       
   293         // get text between non-cached items
       
   294         $cache_split = preg_split("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $content);
       
   295         // get non-cached items
       
   296         preg_match_all("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $content, $cache_parts);
       
   297         $output = '';
       
   298         // loop over items, stitch back together
       
   299         foreach ($cache_split as $curr_idx => $curr_split) {
       
   300             // escape PHP tags in template content
       
   301             $output .= preg_replace('/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/', "<?php echo '\$1'; ?>\n", $curr_split);
       
   302             if (isset($cache_parts[0][$curr_idx])) {
       
   303                 $_template->properties['has_nocache_code'] = true;
       
   304                 $output .= $cache_parts[1][$curr_idx];
       
   305             }
       
   306         }
       
   307         if (!$no_output_filter && !$_template->has_nocache_code && (isset($_template->smarty->autoload_filters['output']) || isset($_template->smarty->registered_filters['output']))) {
       
   308             $output = Smarty_Internal_Filter_Handler::runFilter('output', $output, $_template);
       
   309         }
       
   310         // write cache file content
       
   311         $this->writeCachedContent($_template, $output);
       
   312     }
       
   313 
       
   314     /**
       
   315      * Writes the content to cache resource
       
   316      *
       
   317      * @param Smarty_Internal_Template $_template
       
   318      * @param string $content
       
   319      *
       
   320      * @return bool
       
   321      */
       
   322     public function writeCachedContent(Smarty_Internal_Template $_template, $content) {
       
   323         if ($_template->source->recompiled || !($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED)) {
       
   324             // don't write cache file
       
   325             return false;
       
   326         }
       
   327         $_template->properties['cache_lifetime'] = $_template->cache_lifetime;
       
   328         $_template->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
       
   329         $content = Smarty_Internal_Extension_CodeFrame::create($_template, $content, true);
       
   330         if (!empty($_template->properties['tpl_function'])) {
       
   331             foreach ($_template->properties['tpl_function'] as $funcParam) {
       
   332                 if (is_file($funcParam['compiled_filepath'])) {
       
   333                     // read compiled file
       
   334                     $code = file_get_contents($funcParam['compiled_filepath']);
       
   335                     // grab template function
       
   336                     if (preg_match("/\/\* {$funcParam['call_name']} \*\/([\S\s]*?)\/\*\/ {$funcParam['call_name']} \*\//", $code, $match)) {
       
   337                         unset($code);
       
   338                         $content .= "<?php " . $match[0] . "?>\n";
       
   339                     }
       
   340                 }
       
   341             }
       
   342         }
       
   343         return $this->write($_template, $content);
       
   344     }
       
   345 
       
   346     /**
       
   347      * check client side cache
       
   348      *
       
   349      * @param Smarty_Internal_Template $_template
       
   350      * @param  string $content
       
   351      */
       
   352     public function cacheModifiedCheck(Smarty_Internal_Template $_template, $content) {
       
   353         $_isCached = $_template->isCached() && !$_template->has_nocache_code;
       
   354         $_last_modified_date = @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
       
   355         if ($_isCached && $this->timestamp <= strtotime($_last_modified_date)) {
       
   356             switch (PHP_SAPI) {
       
   357                 case 'cgi': // php-cgi < 5.3
       
   358                 case 'cgi-fcgi': // php-cgi >= 5.3
       
   359                 case 'fpm-fcgi': // php-fpm >= 5.3.3
       
   360                     header('Status: 304 Not Modified');
       
   361                     break;
       
   362 
       
   363                 case 'cli':
       
   364                     if ( /* ^phpunit */
       
   365                     !empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS']) /* phpunit$ */
       
   366                     ) {
       
   367                         $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = '304 Not Modified';
       
   368                     }
       
   369                     break;
       
   370 
       
   371                 default:
       
   372                     header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
       
   373                     break;
       
   374             }
       
   375         } else {
       
   376             switch (PHP_SAPI) {
       
   377                 case 'cli':
       
   378                     if ( /* ^phpunit */
       
   379                     !empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS']) /* phpunit$ */
       
   380                     ) {
       
   381                         $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = 'Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->timestamp) . ' GMT';
       
   382                     }
       
   383                     break;
       
   384 
       
   385                 default:
       
   386                     header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->timestamp) . ' GMT');
       
   387                     break;
       
   388             }
       
   389             echo $content;
       
   390         }
       
   391     }
       
   392 }