XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
file.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
56 {
63  var $file = null;
64 
76  var $settings = array();
77 
84  var $active = false;
85 
92  var $init = true;
93 
104  function init($settings = array())
105  {
106  parent::init($settings);
107  $defaults = array('path' => XOOPS_VAR_PATH . '/caches/xoops_cache' , 'extension' => '.php' , 'prefix' => 'xoops_' , 'lock' => false , 'serialize' => false , 'duration' => 31556926);
108  $this->settings = array_merge($defaults, $this->settings);
109  if (!isset($this->file)) {
110  XoopsLoad::load('XoopsFile');
111  $this->file = XoopsFile::getHandler('file', $this->settings['path'] . '/index.html', true);
112  }
113  $this->settings['path'] = $this->file->folder->cd($this->settings['path']);
114  if (empty($this->settings['path'])) {
115  return false;
116  }
117  return $this->active();
118  }
119 
126  function gc()
127  {
128  return $this->clear(true);
129  }
130 
140  function write($key, $data = null, $duration = null)
141  {
142  if (!isset($data) || ! $this->init) {
143  return false;
144  }
145 
146  if ($this->setKey($key) === false) {
147  return false;
148  }
149 
150  if ($duration == null) {
151  $duration = $this->settings['duration'];
152  }
153  $windows = false;
154  $lineBreak = "\n";
155 
156  if (substr(PHP_OS, 0, 3) == "WIN") {
157  $lineBreak = "\r\n";
158  $windows = true;
159  }
160  $expires = time() + $duration;
161  if (!empty($this->settings['serialize'])) {
162  if ($windows) {
163  $data = str_replace('\\', '\\\\\\\\', serialize($data));
164  } else {
165  $data = serialize($data);
166  }
167  $contents = $expires . $lineBreak . $data . $lineBreak;
168  } else {
169  $contents = $expires . $lineBreak . "return " . var_export($data, true) . ";" . $lineBreak;
170  }
171 
172  if ($this->settings['lock']) {
173  $this->file->lock = true;
174  }
175  $success = $this->file->write($contents);
176  $this->file->close();
177  return $success;
178  }
179 
187  function read($key)
188  {
189  if ($this->setKey($key) === false || ! $this->init) {
190  return false;
191  }
192  if ($this->settings['lock']) {
193  $this->file->lock = true;
194  }
195  $cachetime = $this->file->read(11);
196 
197  if ($cachetime !== false && intval($cachetime) < time()) {
198  $this->file->close();
199  $this->file->delete();
200  return false;
201  }
202 
203  $data = $this->file->read(true);
204  if (!empty($data) && !empty($this->settings['serialize'])) {
205  $data = stripslashes($data);
206  $data = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $data);
207  $data = unserialize($data);
208  if (is_array($data)) {
209  XoopsLoad::load('XoopsUtility');
210  $data = XoopsUtility::recursive('stripslashes', $data);
211  }
212  } else if ($data && empty($this->settings['serialize'])) {
213  $data = eval($data);
214  }
215  $this->file->close();
216  return $data;
217  }
218 
226  function delete($key)
227  {
228  if ($this->setKey($key) === false || ! $this->init) {
229  return false;
230  }
231  return $this->file->delete();
232  }
233 
241  function clear($check = true)
242  {
243  if (!$this->init) {
244  return false;
245  }
246  $dir = dir($this->settings['path']);
247  if ($check) {
248  $now = time();
249  $threshold = $now - $this->settings['duration'];
250  }
251  while (($entry = $dir->read()) !== false) {
252  if ($this->setKey(str_replace($this->settings['prefix'], '', $entry)) === false) {
253  continue;
254  }
255  if ($check) {
256  $mtime = $this->file->lastChange();
257 
258  if ($mtime === false || $mtime > $threshold) {
259  continue;
260  }
261 
262  $expires = $this->file->read(11);
263  $this->file->close();
264 
265  if ($expires > $now) {
266  continue;
267  }
268  }
269  $this->file->delete();
270  }
271  $dir->close();
272  return true;
273  }
274 
282  function setKey($key)
283  {
284  $this->file->folder->cd($this->settings['path']);
285  $this->file->name = $this->settings['prefix'] . $key . $this->settings['extension'];
286  $this->file->handle = null;
287  $this->file->info = null;
288  if (!$this->file->folder->inPath($this->file->pwd(), true)) {
289  return false;
290  }
291  }
298  function active()
299  {
300  if (!$this->active && $this->init && ! is_writable($this->settings['path'])) {
301  $this->init = false;
302  trigger_error(sprintf('%s is not writable', $this->settings['path']), E_USER_WARNING);
303  } else {
304  $this->active = true;
305  }
306  return true;
307  }
308 }
309 
310 ?>