Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
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      */
216ea1 104     function _checkCache ($tmplfile, $tmpl_from_string = false) {
MB 105         $this->_cachefile = $this->_getFilename($tmplfile, $tmpl_from_string);
b1a6a5 106         if ($this->_clearcache) {
MC 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      */
216ea1 136     function _getFilename($tmplfile, $tmpl_from_string = false) {
MB 137         if($tmpl_from_string == true) return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaRSTRING'.$tmplfile).'.'.$this->OPTIONS['CACHE_EXTENSION'];
138         else return $this->OPTIONS['CACHE_DIRECTORY'].'/'.md5('vlibCachestaR'.realpath($tmplfile)).'.'.$this->OPTIONS['CACHE_EXTENSION'];
b1a6a5 139     }
532ae5 140
L 141
b1a6a5 142     /**
MC 143      * FUNCTION: _createCache
144      * creates the cached file
145      *
146      */
147     function _createCache($data) {
148         $cache_file = $this->_cachefile;
149         if(!$this->_prepareDirs($cache_file)) return false; // prepare all of the directories
532ae5 150
b1a6a5 151         $f = fopen($cache_file, "w");
MC 152         flock($f, 2); // set an EXclusive lock
153         if (!$f) vlibTemplateError::raiseError('VT_ERROR_NO_CACHE_WRITE', KILL, $cache_file);
154         fputs($f, $data); // write the parsed string from vlibTemplate
155         flock($f, 3); // UNlock file
156         fclose($f);
157         touch($cache_file);
158         return true;
159     }
532ae5 160
L 161
b1a6a5 162     /**
MC 163      * FUNCTION: _prepareDirs
164      * prepares the directory structure
165      *
166      */
167     function _prepareDirs($file) {
168         if (empty($file)) die('no filename'); //do error in future
169         $filepath = dirname($file);
170         if (is_dir($filepath)) return true;
171
172         $dirs = preg_split('/[\\/]/', $filepath);
173         $currpath;
174         foreach ($dirs as $dir) {
175             $currpath .= $dir .'/';
176             $type = @filetype($currpath);
177
178             ($type=='link') and $type = 'dir';
179             if ($type != 'dir' && $type != false && !empty($type)) {
180                 vlibTemplateError::raiseError('VT_ERROR_WRONG_CACHE_TYPE', KILL, 'directory: '.$currpath.', type: '.$type);
181             }
182             if ($type == 'dir') {
183                 continue;
184             }
185             else {
186                 $s = @mkdir($currpath, 0775);
187                 if (!$s) vlibTemplateError::raiseError('VT_ERROR_CACHE_MKDIR_FAILURE', KILL, 'directory: '.$currpath);
188             }
189         }
190         return true;
191     }
532ae5 192
L 193 } // -- end vlibTemplateCache class
b1a6a5 194 ?>