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