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
 * read a cache file, determine if it needs to be
Chris PeBenito 696b41
 * regenerated or not
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 boolean
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_read_cache_file(&$params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    static  $content_cache = array();
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($smarty->force_compile) {
Chris PeBenito 696b41
        // force compile enabled, always regenerate
Chris PeBenito 696b41
        return false;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (isset($content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']])) {
Chris PeBenito 696b41
        list($params['results'], $smarty->_cache_info) = $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']];
Chris PeBenito 696b41
        return true;
Chris PeBenito 696b41
    }
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('read', &$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
        $_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['results'] = $smarty->_read_file($_cache_file);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (empty($params['results'])) {
Chris PeBenito 696b41
        // nothing to parse (error?), regenerate cache
Chris PeBenito 696b41
        return false;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $_contents = $params['results'];
Chris PeBenito 696b41
    $_info_start = strpos($_contents, "\n") + 1;
Chris PeBenito 696b41
    $_info_len = (int)substr($_contents, 0, $_info_start - 1);
Chris PeBenito 696b41
    $_cache_info = unserialize(substr($_contents, $_info_start, $_info_len));
Chris PeBenito 696b41
    $params['results'] = substr($_contents, $_info_start + $_info_len);
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($smarty->caching == 2 && isset ($_cache_info['expires'])){
Chris PeBenito 696b41
        // caching by expiration time
Chris PeBenito 696b41
        if ($_cache_info['expires'] > -1 && (time() > $_cache_info['expires'])) {
Chris PeBenito 696b41
            // cache expired, regenerate
Chris PeBenito 696b41
            return false;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        // caching by lifetime
Chris PeBenito 696b41
        if ($smarty->cache_lifetime > -1 && (time() - $_cache_info['timestamp'] > $smarty->cache_lifetime)) {
Chris PeBenito 696b41
            // cache expired, regenerate
Chris PeBenito 696b41
            return false;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($smarty->compile_check) {
Chris PeBenito 696b41
        $_params = array('get_source' => false, 'quiet'=>true);
Chris PeBenito 696b41
        foreach (array_keys($_cache_info['template']) as $_template_dep) {
Chris PeBenito 696b41
            $_params['resource_name'] = $_template_dep;
Chris PeBenito 696b41
            if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) {
Chris PeBenito 696b41
                // template file has changed, regenerate cache
Chris PeBenito 696b41
                return false;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        if (isset($_cache_info['config'])) {
Chris PeBenito 696b41
            $_params = array('resource_base_path' => $smarty->config_dir, 'get_source' => false, 'quiet'=>true);
Chris PeBenito 696b41
            foreach (array_keys($_cache_info['config']) as $_config_dep) {
Chris PeBenito 696b41
                $_params['resource_name'] = $_config_dep;
Chris PeBenito 696b41
                if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) {
Chris PeBenito 696b41
                    // config file has changed, regenerate cache
Chris PeBenito 696b41
                    return false;
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    foreach ($_cache_info['cache_serials'] as $_include_file_path=>$_cache_serial) {
Chris PeBenito 696b41
        if (empty($smarty->_cache_serials[$_include_file_path])) {
Chris PeBenito 696b41
            $smarty->_include($_include_file_path, true);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        if ($smarty->_cache_serials[$_include_file_path] != $_cache_serial) {
Chris PeBenito 696b41
            /* regenerate */
Chris PeBenito 696b41
            return false;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']] = array($params['results'], $_cache_info);
Chris PeBenito 696b41
Chris PeBenito 696b41
    $smarty->_cache_info = $_cache_info;
Chris PeBenito 696b41
    return true;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>