24 $config = Main\Config\Configuration::getValue(
"cache");
25 if ($config && is_array($config) && isset($config[
"use_lock"]))
27 $this->useLock = (bool)$config[
"use_lock"];
30 if (!empty($options) && isset($options[
'actual_data']))
32 $this->useLock = !((bool) $options[
'actual_data']);
156 public function clean($baseDir, $initDir =
'', $filename =
'')
158 $documentRoot = Main\Loader::getDocumentRoot();
159 if (($filename !==
false) && ($filename !==
""))
161 static::unlink($documentRoot.$baseDir.$initDir.$filename);
165 $initDir = trim($initDir,
"/");
168 $sourceDir = $documentRoot.
"/".trim($baseDir,
"/");
169 if (file_exists($sourceDir) && is_dir($sourceDir))
171 $dh = opendir($sourceDir);
172 if (is_resource($dh))
174 while ($entry = readdir($dh))
176 if (preg_match(
"/^(\\.|\\.\\.|.*\\.~\\d+)\$/", $entry))
179 if (is_dir($sourceDir.
"/".$entry))
180 static::clean($baseDir, $entry);
181 elseif (is_file($sourceDir.
"/".$entry))
182 static::unlink($sourceDir.
"/".$entry);
189 $source =
"/".trim($baseDir,
"/").
"/".$initDir;
190 $source = rtrim($source,
"/");
191 $delayedDelete =
false;
193 if (!preg_match(
"/^(\\.|\\.\\.|.*\\.~\\d+)\$/", $source) && file_exists($documentRoot.$source))
195 if (is_file($documentRoot.$source))
197 static::unlink($documentRoot.$source);
201 $target = static::randomizeFile($source.
".~");
204 $con = Main\Application::getConnection();
205 $con->queryExecute(
"INSERT INTO b_cache_tag (SITE_ID, CACHE_SALT, RELATIVE_PATH, TAG) VALUES ('*', '*', '".$con->getSqlHelper()->forSql($target).
"', '*')");
206 if (@rename($documentRoot.$source, $documentRoot.$target))
208 $delayedDelete =
true;
217 DeleteDirFilesEx($baseDir.$initDir);
269 public function read(&$vars, $baseDir, $initDir, $filename, $ttl)
271 $documentRoot = Main\Loader::getDocumentRoot();
272 $fn = $documentRoot.
"/".ltrim($baseDir.$initDir,
"/").$filename;
274 if (!file_exists($fn))
285 $INCLUDE_FROM_CACHE =
'Y';
292 $handle = fopen($fn,
"rb");
296 $datecreate = fread($handle, 2);
297 if ($datecreate ==
"BX")
299 $datecreate = fread($handle, 12);
300 $dateexpire = fread($handle, 12);
304 $datecreate .= fread($handle, 10);
309 $this->
read = @filesize($fn);
313 if (intval($datecreate) < (time() - $ttl))
317 if ($this->
lock($fn))
332 $vars = unserialize($ser_content);
336 $vars = fread($handle, $this->
read);
359 public function write($vars, $baseDir, $initDir, $filename, $ttl)
361 static $search = array(
"\\",
"'",
"\0");
362 static $replace = array(
"\\\\",
"\\'",
"'.chr(0).'");
363 $documentRoot = Main\Loader::getDocumentRoot();
364 $folder = $documentRoot.
"/".ltrim($baseDir.$initDir,
"/");
365 $fn = $folder.$filename;
366 $fnTmp = $folder.md5(mt_rand()).
".tmp";
368 if (!CheckDirPath($fn))
371 if ($handle = fopen($fnTmp,
"wb+"))
376 $contents .=
"\nif(\$INCLUDE_FROM_CACHE!='Y')return false;";
377 $contents .=
"\n\$datecreate = '".str_pad(time(), 12,
"0", STR_PAD_LEFT).
"';";
378 $contents .=
"\n\$dateexpire = '".str_pad(time() + intval($ttl), 12,
"0", STR_PAD_LEFT).
"';";
379 $contents .=
"\n\$ser_content = '".str_replace($search, $replace, serialize($vars)).
"';";
380 $contents .=
"\nreturn true;";
385 $contents =
"BX".str_pad(time(), 12,
"0", STR_PAD_LEFT).str_pad(time() + intval($this->ttl), 12,
"0", STR_PAD_LEFT);
389 $this->written = fwrite($handle, $contents);
391 $len = strlen($contents);
397 if ($this->written === $len)
400 static::unlink($fnTmp);