You are here

function pCache::dbRemoval in Visitors 7

Same name and namespace in other branches
  1. 7.2 pChart/class/pCache.class.php \pCache::dbRemoval()
2 calls to pCache::dbRemoval()
pCache::remove in pChart/class/pCache.class.php
pCache::removeOlderThan in pChart/class/pCache.class.php

File

pChart/class/pCache.class.php, line 92

Class

pCache

Code

function dbRemoval($Settings) {
  $ID = isset($Settings["Name"]) ? $Settings["Name"] : NULL;
  $Expiry = isset($Settings["Expiry"]) ? $Settings["Expiry"] : -(24 * 60 * 60);
  $TS = time() - $Expiry;

  /* Compute the paths */
  $Database = $this->CacheFolder . "/" . $this->CacheDB;
  $Index = $this->CacheFolder . "/" . $this->CacheIndex;
  $DatabaseTemp = $this->CacheFolder . "/" . $this->CacheDB . ".tmp";
  $IndexTemp = $this->CacheFolder . "/" . $this->CacheIndex . ".tmp";

  /* Single file removal */
  if ($ID != NULL) {

    /* Retrieve object informations */
    $Object = $this
      ->isInCache($ID, TRUE);

    /* If it's not in the cache DB, go away */
    if (!$Object) {
      return 0;
    }
  }

  /* Create the temporary files */
  if (!file_exists($DatabaseTemp)) {
    touch($DatabaseTemp);
  }
  if (!file_exists($IndexTemp)) {
    touch($IndexTemp);
  }

  /* Open the file handles */
  $IndexHandle = @fopen($Index, "r");
  $IndexTempHandle = @fopen($IndexTemp, "w");
  $DBHandle = @fopen($Database, "r");
  $DBTempHandle = @fopen($DatabaseTemp, "w");

  /* Remove the selected ID from the database */
  while (!feof($IndexHandle)) {
    $Entry = fgets($IndexHandle, 4096);
    $Entry = str_replace("\r", "", $Entry);
    $Entry = str_replace("\n", "", $Entry);
    $Settings = preg_split("/,/", $Entry);
    if ($Entry != "") {
      $PicID = $Settings[0];
      $DBPos = $Settings[1];
      $PicSize = $Settings[2];
      $GeneratedTS = $Settings[3];
      $Hits = $Settings[4];
      if ($Settings[0] != $ID && $GeneratedTS > $TS) {
        $CurrentPos = ftell($DBTempHandle);
        fwrite($IndexTempHandle, $PicID . "," . $CurrentPos . "," . $PicSize . "," . $GeneratedTS . "," . $Hits . "\r\n");
        fseek($DBHandle, $DBPos);
        $Picture = fread($DBHandle, $PicSize);
        fwrite($DBTempHandle, $Picture);
      }
    }
  }

  /* Close the handles */
  fclose($IndexHandle);
  fclose($IndexTempHandle);
  fclose($DBHandle);
  fclose($DBTempHandle);

  /* Remove the prod files */
  unlink($Database);
  unlink($Index);

  /* Swap the temp & prod DB */
  rename($DatabaseTemp, $Database);
  rename($IndexTemp, $Index);
}