2003-05-08 20:21:16 +00:00
< ? php
/**
* Smarty plugin
* @ package Smarty
* @ subpackage plugins
*/
/**
* Prepend the cache information to the cache file
* and write it
*
* @ param string $tpl_file
* @ param string $cache_id
* @ param string $compile_id
* @ param string $results
* @ return true | null
*/
2003-06-19 16:32:28 +00:00
2003-05-08 20:21:16 +00:00
// $tpl_file, $cache_id, $compile_id, $results
2003-06-19 16:32:28 +00:00
2003-06-22 03:13:25 +00:00
function smarty_core_write_cache_file ( $params , & $smarty )
2003-05-08 20:21:16 +00:00
{
2003-06-18 23:01:42 +00:00
2003-05-08 20:21:16 +00:00
// put timestamp in cache header
2003-06-22 03:13:25 +00:00
$smarty -> _cache_info [ 'timestamp' ] = time ();
if ( $smarty -> cache_lifetime > - 1 ){
2003-05-08 20:21:16 +00:00
// expiration set
2003-06-22 03:13:25 +00:00
$smarty -> _cache_info [ 'expires' ] = $smarty -> _cache_info [ 'timestamp' ] + $smarty -> cache_lifetime ;
2003-05-08 20:21:16 +00:00
} else {
// cache will never expire
2003-06-22 03:13:25 +00:00
$smarty -> _cache_info [ 'expires' ] = - 1 ;
2003-05-08 20:21:16 +00:00
}
2004-10-02 18:03:25 +00:00
// collapse nocache.../nocache-tags
if ( preg_match_all ( '!\{(/?)nocache\:[0-9a-f]{32}#\d+\}!' , $params [ 'results' ], $match , PREG_PATTERN_ORDER )) {
// remove everything between every pair of outermost noache.../nocache-tags
// and replace it by a single nocache-tag
// this new nocache-tag will be replaced by dynamic contents in
// smarty_core_process_compiled_includes() on a cache-read
$match_count = count ( $match [ 0 ]);
$results = preg_split ( '!(\{/?nocache\:[0-9a-f]{32}#\d+\})!' , $params [ 'results' ], - 1 , PREG_SPLIT_DELIM_CAPTURE );
$level = 0 ;
$j = 0 ;
for ( $i = 0 , $results_count = count ( $results ); $i < $results_count && $j < $match_count ; $i ++ ) {
if ( $results [ $i ] == $match [ 0 ][ $j ]) {
// nocache tag
if ( $match [ 1 ][ $j ]) { // closing tag
$level -- ;
unset ( $results [ $i ]);
} else { // opening tag
if ( $level ++ > 0 ) unset ( $results [ $i ]);
}
$j ++ ;
} elseif ( $level > 0 ) {
unset ( $results [ $i ]);
}
}
$params [ 'results' ] = implode ( '' , $results );
}
2003-06-22 03:13:25 +00:00
$smarty -> _cache_info [ 'cache_serials' ] = $smarty -> _cache_serials ;
2003-06-18 23:01:42 +00:00
2003-05-08 20:21:16 +00:00
// prepend the cache header info into cache file
2005-02-01 10:19:08 +00:00
$_cache_info = serialize ( $smarty -> _cache_info );
$params [ 'results' ] = strlen ( $_cache_info ) . " \n " . $_cache_info . $params [ 'results' ];
2003-05-08 20:21:16 +00:00
2003-06-22 03:13:25 +00:00
if ( ! empty ( $smarty -> cache_handler_func )) {
2003-05-08 20:21:16 +00:00
// use cache_handler function
2003-06-22 03:13:25 +00:00
call_user_func_array ( $smarty -> cache_handler_func ,
2003-11-06 16:53:39 +00:00
array ( 'write' , & $smarty , & $params [ 'results' ], $params [ 'tpl_file' ], $params [ 'cache_id' ], $params [ 'compile_id' ], null ));
2003-05-08 20:21:16 +00:00
} else {
// use local cache file
2003-06-19 16:32:28 +00:00
2003-06-22 03:13:25 +00:00
if ( !@ is_writable ( $smarty -> cache_dir )) {
2003-06-19 16:32:28 +00:00
// cache_dir not writable, see if it exists
2003-06-22 03:13:25 +00:00
if ( !@ is_dir ( $smarty -> cache_dir )) {
$smarty -> trigger_error ( 'the $cache_dir \'' . $smarty -> cache_dir . '\' does not exist, or is not a directory.' , E_USER_ERROR );
2003-06-19 16:32:28 +00:00
return false ;
}
2003-06-22 03:13:25 +00:00
$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 );
2003-06-19 16:32:28 +00:00
return false ;
}
2003-06-22 03:13:25 +00:00
$_auto_id = $smarty -> _get_auto_id ( $params [ 'cache_id' ], $params [ 'compile_id' ]);
$_cache_file = $smarty -> _get_auto_filename ( $smarty -> cache_dir , $params [ 'tpl_file' ], $_auto_id );
2003-06-19 16:32:28 +00:00
$_params = array ( 'filename' => $_cache_file , 'contents' => $params [ 'results' ], 'create_dirs' => true );
2004-09-16 23:07:32 +00:00
require_once ( SMARTY_CORE_DIR . 'core.write_file.php' );
2003-06-22 03:13:25 +00:00
smarty_core_write_file ( $_params , $smarty );
2003-05-08 20:21:16 +00:00
return true ;
}
}
/* vim: set expandtab: */
?>