Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Prepend the cache information to the cache file
Chris PeBenito 696b41
 * and write it
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * @param string $tpl_file
Chris PeBenito 696b41
 * @param string $cache_id
Chris PeBenito 696b41
 * @param string $compile_id
Chris PeBenito 696b41
 * @param string $results
Chris PeBenito 696b41
 * @return true|null
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
 // $tpl_file, $cache_id, $compile_id, $results
Chris PeBenito 696b41
Chris PeBenito 696b41
function smarty_core_write_cache_file($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
Chris PeBenito 696b41
    // put timestamp in cache header
Chris PeBenito 696b41
    $smarty->_cache_info['timestamp'] = time();
Chris PeBenito 696b41
    if ($smarty->cache_lifetime > -1){
Chris PeBenito 696b41
        // expiration set
Chris PeBenito 696b41
        $smarty->_cache_info['expires'] = $smarty->_cache_info['timestamp'] + $smarty->cache_lifetime;
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        // cache will never expire
Chris PeBenito 696b41
        $smarty->_cache_info['expires'] = -1;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    // collapse nocache.../nocache-tags
Chris PeBenito 696b41
    if (preg_match_all('!\{(/?)nocache\:[0-9a-f]{32}#\d+\}!', $params['results'], $match, PREG_PATTERN_ORDER)) {
Chris PeBenito 696b41
        // remove everything between every pair of outermost noache.../nocache-tags
Chris PeBenito 696b41
        // and replace it by a single nocache-tag
Chris PeBenito 696b41
        // this new nocache-tag will be replaced by dynamic contents in
Chris PeBenito 696b41
        // smarty_core_process_compiled_includes() on a cache-read
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        $match_count = count($match[0]);
Chris PeBenito 696b41
        $results = preg_split('!(\{/?nocache\:[0-9a-f]{32}#\d+\})!', $params['results'], -1, PREG_SPLIT_DELIM_CAPTURE);
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        $level = 0;
Chris PeBenito 696b41
        $j = 0;
Chris PeBenito 696b41
        for ($i=0, $results_count = count($results); $i < $results_count && $j < $match_count; $i++) {
Chris PeBenito 696b41
            if ($results[$i] == $match[0][$j]) {
Chris PeBenito 696b41
                // nocache tag
Chris PeBenito 696b41
                if ($match[1][$j]) { // closing tag
Chris PeBenito 696b41
                    $level--;
Chris PeBenito 696b41
                    unset($results[$i]);
Chris PeBenito 696b41
                } else { // opening tag
Chris PeBenito 696b41
                    if ($level++ > 0) unset($results[$i]);
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
                $j++;
Chris PeBenito 696b41
            } elseif ($level > 0) {
Chris PeBenito 696b41
                unset($results[$i]);
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $params['results'] = implode('', $results);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    $smarty->_cache_info['cache_serials'] = $smarty->_cache_serials;
Chris PeBenito 696b41
Chris PeBenito 696b41
    // prepend the cache header info into cache file
Chris PeBenito 696b41
    $_cache_info = serialize($smarty->_cache_info);
Chris PeBenito 696b41
    $params['results'] = strlen($_cache_info) . "\n" . $_cache_info . $params['results'];
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (!empty($smarty->cache_handler_func)) {
Chris PeBenito 696b41
        // use cache_handler function
Chris PeBenito 696b41
        call_user_func_array($smarty->cache_handler_func,
Chris PeBenito 696b41
                             array('write', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null));
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        // use local cache file
Chris PeBenito 696b41
Chris PeBenito 696b41
        if(!@is_writable($smarty->cache_dir)) {
Chris PeBenito 696b41
            // cache_dir not writable, see if it exists
Chris PeBenito 696b41
            if(!@is_dir($smarty->cache_dir)) {
Chris PeBenito 696b41
                $smarty->trigger_error('the $cache_dir \'' . $smarty->cache_dir . '\' does not exist, or is not a directory.', E_USER_ERROR);
Chris PeBenito 696b41
                return false;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $smarty->trigger_error('unable to write to $cache_dir \'' . realpath($smarty->cache_dir) . '\'. Be sure $cache_dir is writable by the web server user.', E_USER_ERROR);
Chris PeBenito 696b41
            return false;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']);
Chris PeBenito 696b41
        $_cache_file = $smarty->_get_auto_filename($smarty->cache_dir, $params['tpl_file'], $_auto_id);
Chris PeBenito 696b41
        $_params = array('filename' => $_cache_file, 'contents' => $params['results'], 'create_dirs' => true);
Chris PeBenito 696b41
        require_once(SMARTY_CORE_DIR . 'core.write_file.php');
Chris PeBenito 696b41
        smarty_core_write_file($_params, $smarty);
Chris PeBenito 696b41
        return true;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>