AppendExpiryDataToFile - v2


15/10/04
 

AppendExpiryDataToFile - v2

In my last but one entry I recognised that it would be a good idea to make the AppendExpiryDataToFile function return the file date. There's also my left-over [*** ADD "and not found"!] comment to deal with. So here's version 2...
 
function AppendExpiryDataToFile($theURL, $theOwner, $theExpiryDate, 
$theMessage, $theFile) {

  // if (theURL is a valid LSBU www address) then
  if ((substr($theURL, 0, 23) == "http://myweb.lsbu.ac.uk")) {
  
    // open and lock the expiry data file
    $theFile_fp = fopen($theFile, "r+");
    $lock = flock($theFile_fp, 2);

    // continue when lock is obtained
    if ($lock) {

      // read expired page data file
      $db = readDatabase($theFile);

      // rewind and read date on the 5th line of the file
      fseek($theFile_fp, 0);
      $i = 1;
      while ($i < 6):
        $line = fgets($theFile_fp);
        $i = $i + 1;
      endwhile;
      $theDate = substr($line, 6, 10);

    } 

    // unlock and close reminders file
    $lock = flock($theFile_fp, 3);
    fclose($theFile_fp);

    $found = FALSE;
    // Pop each ExpiredPage object and see if the URL matches
    while (($poppedExpiredPage = (array_pop($db))) && (!$found)):
      if ($poppedExpiredPage->url == $theURL) {
        $found = TRUE;
      }
    endwhile;
    
    // if there's no entry for the web page then append to file
    if ($found == FALSE) {

      // go to end of file, back two lines (i.e. 15 places)
      fseek($theFile_fp, -15, SEEK_END);

      // now append...
      fwrite($theFile_fp, "<expiredPage>\n");
      fwrite($theFile_fp, "  <url>");
      fwrite($theFile_fp, $theURL);
      fwrite($theFile_fp, "</url>\n");
      fwrite($theFile_fp, "  <owner>");
      fwrite($theFile_fp, $theOwner);
      fwrite($theFile_fp, "</owner>\n");
      fwrite($theFile_fp, "  <expired>");
      fwrite($theFile_fp, $theExpiryDate);
      fwrite($theFile_fp, "</expired>\n");
      fwrite($theFile_fp, "  <message>");
      fwrite($theFile_fp, $theMessage);
      fwrite($theFile_fp, "</message>\n");
      fwrite($theFile_fp, "</expiredPage>\n\n");
      fwrite($theFile_fp, "</expiredPages>");
    }
  }

  return $theDate;

}
 
 
 

<<contents ^top^