]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/internals/core.write_cache_file.php
Release 6.5.0
[Github/sugarcrm.git] / include / Smarty / internals / core.write_cache_file.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
20
21 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
22
23
24 */
25
26
27 /**
28  * Smarty plugin
29  * @package Smarty
30  * @subpackage plugins
31  */
32
33 /**
34  * Prepend the cache information to the cache file
35  * and write it
36  *
37  * @param string $tpl_file
38  * @param string $cache_id
39  * @param string $compile_id
40  * @param string $results
41  * @return true|null
42  */
43
44  // $tpl_file, $cache_id, $compile_id, $results
45
46 function smarty_core_write_cache_file($params, &$smarty)
47 {
48
49     // put timestamp in cache header
50     $smarty->_cache_info['timestamp'] = time();
51     if ($smarty->cache_lifetime > -1){
52         // expiration set
53         $smarty->_cache_info['expires'] = $smarty->_cache_info['timestamp'] + $smarty->cache_lifetime;
54     } else {
55         // cache will never expire
56         $smarty->_cache_info['expires'] = -1;
57     }
58
59     // collapse nocache.../nocache-tags
60     if (preg_match_all('!\{(/?)nocache\:[0-9a-f]{32}#\d+\}!', $params['results'], $match, PREG_PATTERN_ORDER)) {
61         // remove everything between every pair of outermost noache.../nocache-tags
62         // and replace it by a single nocache-tag
63         // this new nocache-tag will be replaced by dynamic contents in
64         // smarty_core_process_compiled_includes() on a cache-read
65         
66         $match_count = count($match[0]);
67         $results = preg_split('!(\{/?nocache\:[0-9a-f]{32}#\d+\})!', $params['results'], -1, PREG_SPLIT_DELIM_CAPTURE);
68         
69         $level = 0;
70         $j = 0;
71         for ($i=0, $results_count = count($results); $i < $results_count && $j < $match_count; $i++) {
72             if ($results[$i] == $match[0][$j]) {
73                 // nocache tag
74                 if ($match[1][$j]) { // closing tag
75                     $level--;
76                     unset($results[$i]);
77                 } else { // opening tag
78                     if ($level++ > 0) unset($results[$i]);
79                 }
80                 $j++;
81             } elseif ($level > 0) {
82                 unset($results[$i]);
83             }
84         }
85         $params['results'] = implode('', $results);
86     }
87     $smarty->_cache_info['cache_serials'] = $smarty->_cache_serials;
88
89     // prepend the cache header info into cache file
90     $_cache_info = serialize($smarty->_cache_info);
91     $params['results'] = strlen($_cache_info) . "\n" . $_cache_info . $params['results'];
92
93     if (!empty($smarty->cache_handler_func)) {
94         // use cache_handler function
95         call_user_func_array($smarty->cache_handler_func,
96                              array('write', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null));
97     } else {
98         // use local cache file
99
100         if(!@is_writable($smarty->cache_dir)) {
101             // cache_dir not writable, see if it exists
102             if(!@is_dir($smarty->cache_dir)) {
103                 $smarty->trigger_error('the $cache_dir \'' . $smarty->cache_dir . '\' does not exist, or is not a directory.', E_USER_ERROR);
104                 return false;
105             }
106             $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);
107             return false;
108         }
109
110         $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']);
111         $_cache_file = $smarty->_get_auto_filename($smarty->cache_dir, $params['tpl_file'], $_auto_id);
112         $_params = array('filename' => $_cache_file, 'contents' => $params['results'], 'create_dirs' => true);
113         require_once(SMARTY_CORE_DIR . 'core.write_file.php');
114         smarty_core_write_file($_params, $smarty);
115         return true;
116     }
117 }
118
119 /* vim: set expandtab: */
120
121 ?>