00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class FileEngine extends CacheEngine {
00034
00035
00036
00037
00038
00039
00040 var $__File = null;
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 var $settings = array();
00053
00054
00055
00056
00057
00058
00059 var $__active = false;
00060
00061
00062
00063
00064
00065
00066 var $__init = true;
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 function init($settings = array()) {
00078 parent::init(array_merge(
00079 array(
00080 'engine' => 'File', 'path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false,
00081 'serialize'=> true, 'isWindows' => false
00082 ),
00083 $settings
00084 ));
00085 if (!isset($this->__File)) {
00086 if (!class_exists('File')) {
00087 require LIBS . 'file.php';
00088 }
00089 $this->__File =& new File($this->settings['path'] . DS . 'cake');
00090 }
00091
00092 if (DIRECTORY_SEPARATOR === '\\') {
00093 $this->settings['isWindows'] = true;
00094 }
00095
00096 $this->settings['path'] = $this->__File->Folder->cd($this->settings['path']);
00097 if (empty($this->settings['path'])) {
00098 return false;
00099 }
00100 return $this->__active();
00101 }
00102
00103
00104
00105
00106
00107
00108 function gc() {
00109 return $this->clear(true);
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 function write($key, &$data, $duration) {
00121 if ($data === '' || !$this->__init) {
00122 return false;
00123 }
00124
00125 if ($this->__setKey($key) === false) {
00126 return false;
00127 }
00128
00129 $lineBreak = "\n";
00130
00131 if ($this->settings['isWindows']) {
00132 $lineBreak = "\r\n";
00133 }
00134
00135 if (!empty($this->settings['serialize'])) {
00136 if ($this->settings['isWindows']) {
00137 $data = str_replace('\\', '\\\\\\\\', serialize($data));
00138 } else {
00139 $data = serialize($data);
00140 }
00141 }
00142
00143 if ($this->settings['lock']) {
00144 $this->__File->lock = true;
00145 }
00146 $expires = time() + $duration;
00147 $contents = $expires . $lineBreak . $data . $lineBreak;
00148 $success = $this->__File->write($contents);
00149 $this->__File->close();
00150 return $success;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159 function read($key) {
00160 if ($this->__setKey($key) === false || !$this->__init || !$this->__File->exists()) {
00161 return false;
00162 }
00163 if ($this->settings['lock']) {
00164 $this->__File->lock = true;
00165 }
00166 $time = time();
00167 $cachetime = intval($this->__File->read(11));
00168
00169 if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
00170 $this->__File->close();
00171 return false;
00172 }
00173 $data = $this->__File->read(true);
00174
00175 if ($data !== '' && !empty($this->settings['serialize'])) {
00176 if ($this->settings['isWindows']) {
00177 $data = str_replace('\\\\\\\\', '\\', $data);
00178 }
00179 $data = unserialize((string)$data);
00180 }
00181 $this->__File->close();
00182 return $data;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 function delete($key) {
00192 if ($this->__setKey($key) === false || !$this->__init) {
00193 return false;
00194 }
00195 return $this->__File->delete();
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 function clear($check) {
00205 if (!$this->__init) {
00206 return false;
00207 }
00208 $dir = dir($this->settings['path']);
00209 if ($check) {
00210 $now = time();
00211 $threshold = $now - $this->settings['duration'];
00212 }
00213 while (($entry = $dir->read()) !== false) {
00214 if ($this->__setKey($entry) === false) {
00215 continue;
00216 }
00217 if ($check) {
00218 $mtime = $this->__File->lastChange();
00219
00220 if ($mtime === false || $mtime > $threshold) {
00221 continue;
00222 }
00223
00224 $expires = $this->__File->read(11);
00225 $this->__File->close();
00226
00227 if ($expires > $now) {
00228 continue;
00229 }
00230 }
00231 $this->__File->delete();
00232 }
00233 $dir->close();
00234 return true;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243 function __setKey($key) {
00244 $this->__File->Folder->cd($this->settings['path']);
00245 if ($key !== $this->__File->name) {
00246 $this->__File->name = $key;
00247 $this->__File->path = null;
00248 }
00249 if (!$this->__File->Folder->inPath($this->__File->pwd(), true)) {
00250 return false;
00251 }
00252 }
00253
00254
00255
00256
00257
00258
00259 function __active() {
00260 if (!$this->__active && $this->__init && !is_writable($this->settings['path'])) {
00261 $this->__init = false;
00262 trigger_error(sprintf(__('%s is not writable', true), $this->settings['path']), E_USER_WARNING);
00263 } else {
00264 $this->__active = true;
00265 }
00266 return true;
00267 }
00268 }
00269 ?>