20 defined(
'XOOPS_ROOT_PATH') or die('Restricted access');
76 var $settings = array();
104 function init($settings = array())
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)) {
113 $this->settings[
'path'] = $this->file->folder->cd($this->settings[
'path']);
114 if (empty($this->settings[
'path'])) {
117 return $this->active();
128 return $this->clear(
true);
140 function write($key, $data = null, $duration = null)
142 if (!isset($data) || ! $this->init) {
146 if ($this->setKey($key) ===
false) {
150 if ($duration == null) {
151 $duration = $this->settings[
'duration'];
156 if (substr(PHP_OS, 0, 3) ==
"WIN") {
161 if (!empty($this->settings[
'serialize'])) {
163 $data = str_replace(
'\\',
'\\\\\\\\', serialize($data));
165 $data = serialize($data);
167 $contents =
$expires . $lineBreak . $data . $lineBreak;
169 $contents =
$expires . $lineBreak .
"return " . var_export($data,
true) .
";" . $lineBreak;
172 if ($this->settings[
'lock']) {
173 $this->file->lock =
true;
175 $success = $this->file->write($contents);
176 $this->file->close();
189 if ($this->setKey($key) ===
false || ! $this->init) {
192 if ($this->settings[
'lock']) {
193 $this->file->lock =
true;
195 $cachetime = $this->file->read(11);
197 if ($cachetime !==
false && intval($cachetime) < time()) {
198 $this->file->close();
199 $this->file->delete();
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)) {
212 }
else if ($data && empty($this->settings[
'serialize'])) {
215 $this->file->close();
226 function delete($key)
228 if ($this->setKey($key) ===
false || ! $this->init) {
231 return $this->file->delete();
241 function clear($check =
true)
246 $dir = dir($this->settings[
'path']);
249 $threshold = $now - $this->settings[
'duration'];
251 while (($entry =
$dir->read()) !==
false) {
252 if ($this->setKey(str_replace($this->settings[
'prefix'],
'', $entry)) ===
false) {
256 $mtime = $this->file->lastChange();
258 if ($mtime ===
false || $mtime > $threshold) {
263 $this->file->close();
269 $this->file->delete();
282 function setKey($key)
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)) {
300 if (!$this->active && $this->init && ! is_writable($this->settings[
'path'])) {
302 trigger_error(sprintf(
'%s is not writable', $this->settings[
'path']), E_USER_WARNING);
304 $this->active =
true;