commit | author | age
|
532ae5
|
1 |
<?php |
L |
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
|
3 |
// +----------------------------------------------------------------------+ |
|
4 |
// | PHP version 4.0 | |
|
5 |
// +----------------------------------------------------------------------+ |
|
6 |
// | Copyright (c) 2002 Active Fish Group | |
|
7 |
// +----------------------------------------------------------------------+ |
|
8 |
// | Authors: Kelvin Jones <kelvin@kelvinjones.co.uk> | |
|
9 |
// +----------------------------------------------------------------------+ |
|
10 |
// |
|
11 |
// $Id: cache.php,v 1.1 2003/07/08 12:32:06 platinum Exp $ |
|
12 |
|
|
13 |
/** |
|
14 |
* Class uses all of vlibTemplate's functionality but caches the template files. |
|
15 |
* It creates an identical tree structure to your filesystem but with cached files. |
|
16 |
* |
|
17 |
* @author Kelvin Jones <kelvin@kelvinjones.co.uk> |
|
18 |
* @since 22/02/2002 |
|
19 |
* @package vLIB |
|
20 |
* @access public |
|
21 |
*/ |
|
22 |
|
b1a6a5
|
23 |
|
532ae5
|
24 |
class tplc extends tpl { |
L |
25 |
|
b1a6a5
|
26 |
/*-----------------------------------------------------------------------------\ |
532ae5
|
27 |
| DO NOT TOUCH ANYTHING IN THIS CLASS, IT MAY NOT WORK OTHERWISE | |
L |
28 |
\-----------------------------------------------------------------------------*/ |
|
29 |
|
b1a6a5
|
30 |
var $_cache = 1; // tells vlibTemplate that we're caching |
MC |
31 |
var $_cachefile; // full path to current cache file (even if it doesn't yet exist) |
|
32 |
var $_cacheexists; // has this file been cached before |
|
33 |
var $_cachefilelocked; // is this file currently locked whilst writing |
|
34 |
var $_cachefiledir; // dir of current cache file |
|
35 |
var $_clearcache = 0; |
532ae5
|
36 |
|
L |
37 |
|
b1a6a5
|
38 |
/** |
MC |
39 |
* FUNCTION: clearCache |
|
40 |
* will unset a file, and set $this->_cacheexists to 0. |
|
41 |
* |
|
42 |
* @access public |
|
43 |
* @return boolean |
|
44 |
*/ |
|
45 |
function clearCache() { |
|
46 |
$this->_clearcache = 1; |
|
47 |
return true; |
|
48 |
} |
532ae5
|
49 |
|
L |
50 |
|
b1a6a5
|
51 |
/** |
MC |
52 |
* FUNCTION: recache |
|
53 |
* alias for clearCache(). |
|
54 |
* |
|
55 |
* @access public |
|
56 |
* @return boolean |
|
57 |
*/ |
|
58 |
function recache() { |
|
59 |
return $this->clearCache(); |
|
60 |
} |
|
61 |
|
|
62 |
|
|
63 |
/** |
|
64 |
* FUNCTION: setCacheLifeTime |
|
65 |
* sets the lifetime of the cached file |
|
66 |
* |
|
67 |
* @param int $int number of seconds to set lifetime to |
|
68 |
* @access public |
|
69 |
* @return boolean |
|
70 |
*/ |
|
71 |
function setCacheLifeTime($int = null) { |
|
72 |
if ($int == null || !is_int($int)) return false; |
|
73 |
if ($int == 0) $int = 60; |
|
74 |
if ($int == -1) $int = 157680000; // set to 5 yrs time |
|
75 |
$this->OPTIONS['CACHE_LIFETIME'] = $int; |
|
76 |
return true; |
|
77 |
} |
|
78 |
|
|
79 |
|
|
80 |
/** |
|
81 |
* FUNCTION: setCacheExtension |
|
82 |
* sets the extention of the cache file |
|
83 |
* |
|
84 |
* @param str $str name of new cache extention |
|
85 |
* @access public |
|
86 |
* @return boolean |
|
87 |
*/ |
|
88 |
function setCacheExtension($str = null) { |
|
89 |
if ($str == null || !preg_match('/^[a-z0-9]+$/', strtolower($str))) return false; |
|
90 |
$this->OPTIONS['CACHE_EXTENSION'] = strtolower($str); |
|
91 |
return true; |
|
92 |
} |
|
93 |
|
|
94 |
|
|
95 |
/*----------------------------------------\ |
532ae5
|
96 |
Private Functions |
L |
97 |
-----------------------------------------*/ |
|
98 |
|
|
99 |
|
b1a6a5
|
100 |
/** |
MC |
101 |
* FUNCTION: _checkCache |
|
102 |
* checks if there's a cache, if there is then it will read the cache file as the template. |
|
103 |
*/ |
|
104 |
function _checkCache ($tmplfile) { |
|
105 |
$this->_cachefile = $this->_getFilename($tmplfile); |
|
106 |
if ($this->_clearcache) { |
|
107 |
if (file_exists($this->_cachefile)) unlink($this->_cachefile); |
|
108 |
return false; |
|
109 |
} |
532ae5
|
110 |
|
b1a6a5
|
111 |
if (file_exists($this->_cachefile)) { |
MC |
112 |
$this->_cacheexists = 1; |
532ae5
|
113 |
|
b1a6a5
|
114 |
// if it's expired |
MC |
115 |
if ((filemtime($this->_cachefile) + $this->OPTIONS['CACHE_LIFETIME']) < date('U') |
|
116 |
|| filectime($this->_cachefile) < filemtime($tmplfile)) { |
|
117 |
$this->_cacheexists = 0; |
|
118 |
return false; // so that we know to recache |
|
119 |
} |
|
120 |
else { |
|
121 |
return true; |
|
122 |
} |
|
123 |
|
|
124 |
} else { |
|
125 |
$this->_cacheexists = 0; |
|
126 |
return false; |
|
127 |
} |
|
128 |
} |
532ae5
|
129 |
|
L |
130 |
|
b1a6a5
|
131 |
/** |
MC |
132 |
* FUNCTION: _getFilename |
|
133 |
* gets the full pathname for the cached file |
|
134 |
* |
|
135 |
*/ |
|
136 |
function _getFilename($tmplfile) { |
|
137 |
return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaR'.realpath($tmplfile)).'.'.$this->OPTIONS['CACHE_EXTENSION']; |
|
138 |
} |
532ae5
|
139 |
|
L |
140 |
|
b1a6a5
|
141 |
/** |
MC |
142 |
* FUNCTION: _createCache |
|
143 |
* creates the cached file |
|
144 |
* |
|
145 |
*/ |
|
146 |
function _createCache($data) { |
|
147 |
$cache_file = $this->_cachefile; |
|
148 |
if(!$this->_prepareDirs($cache_file)) return false; // prepare all of the directories |
532ae5
|
149 |
|
b1a6a5
|
150 |
$f = fopen($cache_file, "w"); |
MC |
151 |
flock($f, 2); // set an EXclusive lock |
|
152 |
if (!$f) vlibTemplateError::raiseError('VT_ERROR_NO_CACHE_WRITE', KILL, $cache_file); |
|
153 |
fputs($f, $data); // write the parsed string from vlibTemplate |
|
154 |
flock($f, 3); // UNlock file |
|
155 |
fclose($f); |
|
156 |
touch($cache_file); |
|
157 |
return true; |
|
158 |
} |
532ae5
|
159 |
|
L |
160 |
|
b1a6a5
|
161 |
/** |
MC |
162 |
* FUNCTION: _prepareDirs |
|
163 |
* prepares the directory structure |
|
164 |
* |
|
165 |
*/ |
|
166 |
function _prepareDirs($file) { |
|
167 |
if (empty($file)) die('no filename'); //do error in future |
|
168 |
$filepath = dirname($file); |
|
169 |
if (is_dir($filepath)) return true; |
|
170 |
|
|
171 |
$dirs = preg_split('/[\\/]/', $filepath); |
|
172 |
$currpath; |
|
173 |
foreach ($dirs as $dir) { |
|
174 |
$currpath .= $dir .'/'; |
|
175 |
$type = @filetype($currpath); |
|
176 |
|
|
177 |
($type=='link') and $type = 'dir'; |
|
178 |
if ($type != 'dir' && $type != false && !empty($type)) { |
|
179 |
vlibTemplateError::raiseError('VT_ERROR_WRONG_CACHE_TYPE', KILL, 'directory: '.$currpath.', type: '.$type); |
|
180 |
} |
|
181 |
if ($type == 'dir') { |
|
182 |
continue; |
|
183 |
} |
|
184 |
else { |
|
185 |
$s = @mkdir($currpath, 0775); |
|
186 |
if (!$s) vlibTemplateError::raiseError('VT_ERROR_CACHE_MKDIR_FAILURE', KILL, 'directory: '.$currpath); |
|
187 |
} |
|
188 |
} |
|
189 |
return true; |
|
190 |
} |
532ae5
|
191 |
|
L |
192 |
} // -- end vlibTemplateCache class |
b1a6a5
|
193 |
?> |