· Martin's Expiry Checker v3 Blog ·

M.E.Bush > Misc. > Expiry Checker v3 Blog > 18-Aug-2006

The script for Sundays

Now for what should happen on Sundays, based roughly on the algorithm described previously.

I was stuck for ages trying to get fpassthru to work; it seemed to work fine when I invoked the script directly from a web browser, but not otherwise. The only thing I can think of is that this might be due to the fact that the Expiry Checker is invoked via a hidden image. When a page containing a hidden image is passed to a web brower (via an fpassthru instruction or otherwise) I think that the browser then requests the image, but I guess this doesn't happen when there's no web browser to process the output from an fpassthru.

So I've used an alternative approach; the script now reads in each expired web page and uses pattern matching to check the current expiry date. This has the advantage that if the page is found not to be expired then only that page can be removed from the database, rather than all of the pages belonging to the page owner.

The expirydataTEMP table is created and initialised to contain a duplicate of the expirydataLIVE table. The expiry date for each page entry is checked, and the entry is deleted from the expirydataLIVE table if the page is found not to be expired. The idea is to delete the expirydataTEMP table on the Monday, so that when next Sunday comes round the script can tell whether or not it's the first "hit" of the day.

This script seems to work...

<?php

function connect_to_db() {
  if ( $db = @mysql_connect( "mysql.lsbu.ac.uk", "bushm", "****")) {
    if ( @mysql_select_db( "bushm", $db)) {
      return $db;
    } else {
      die( "<h1>Failed to obtain access to database!</h1>");
    }
  } else {
    die( "<h1>Failed to connect to database server!</h1>");
  }
}

//----------------------------------------------------------------

function TruncateURL($theURL) {

// Truncate URL if it ends with a "?", and then truncate it further
// - if necessary - to remove "/index.html", "/index.shtml" or "/"

  if (substr($theURL,-strlen("?"))=="?") {
    $theURL = substr($theURL,0,-strlen("?"));
  }
  if (substr($theURL,-strlen("/index.html"))=="/index.html") {
    $theURL = substr($theURL,0,-strlen("/index.html"));
  }
  if (substr($theURL,-strlen("/index.shtml"))=="/index.shtml") {
    $theURL = substr($theURL,0,-strlen("/index.shtml"));
  }
  if (substr($theURL,-strlen("/"))=="/") {
    $theURL = substr($theURL,0,-strlen("/"));
  }
  return($theURL);
}

//----------------------------------------------------------------

function GetTodayYYMMDD() {

  // Get today's date in dd/mm/yy format
  $today = date("d/m/y");
  // Convert to yymmdd format - e.g. 24/08/02 becomes 020824
  // *** End-of-century bug - will fail in the year 2100! ***
  $today_day = substr($today,-8,2);
  $today_month = substr($today,-5,2);
  $today_year = substr($today,-2,2);
  $today_yymmdd = $today_year.$today_month.$today_day;
  return($today_yymmdd);
}

//----------------------------------------------------------------

function ConvertDateToYYMMDD($theDate) {

  // prepare $theDate for conversion
  // handle dd/mm/yy dates including single digits for dd, mm, yy
  // - e.g. 06/06/03, 6/6/03, 06/6/03, 6/06/03, 06/06/3 etc.
  $position_of_first_slash = strpos($theDate, "/");
  $expiry_day = substr($theDate, 0, $position_of_first_slash);
  $expiry_mm_yy = substr($theDate, $position_of_first_slash + 1, 
                                                strlen($theDate));
  $position_of_second_slash = strpos($expiry_mm_yy, "/");
  $expiry_month = substr($expiry_mm_yy, 0, $position_of_second_slash);
  $expiry_year = substr($expiry_mm_yy, $position_of_second_slash + 1, 
                                                strlen($theDate));
  // if any of dd, mm or yy are single digits then add a leading zero
  if ( strlen($expiry_day) == 1 ) { 
    $expiry_day = "0".$expiry_day; 
  }
  if ( strlen($expiry_month) == 1 ) {
    $expiry_month = "0".$expiry_month; 
  }
  if ( strlen($expiry_year) == 1 ) {
    $expiry_year = "0".$expiry_year; 
  }
  // now able to convert expiry date to yymmdd format
  $expiry_yymmdd = $expiry_year.$expiry_month.$expiry_day;

  // if $theDate contained no "/"s then it was invalid, in which case
  // return zero (this is important for the Expiry Checker)
  if ($position_of_first_slash == 0) {
    return(0);
  } else {
    return($expiry_yymmdd);
  }
}

//================================================================


/*
// MAIN PROGRAM
*/

// if today is not a Sunday or a Monday then
$day_of_week = date("D");
if (!($day_of_week == "Sun") && !($day_of_week == "Mon")) {

  // Discover url of referring web page
  $url = $_ENV['HTTP_REFERER'];

  // Get $owner, $expirydate and $message from referring web page
  $owner = $_GET['owner'];
  if ($owner == "") {
    $owner = "bcim@lsbu.ac.uk";
  }
  $expirydate = $_GET['expirydate'];
  $message = $_GET['message'];
  
  // get $expirydate from referring web page
  $expirydate = $_GET['expirydate'];

  // use ConvertDateToYYMMDD to re-format $expirydate
  $expirydateYYMMDD = ConvertDateToYYMMDD($expirydate);

  // get today's date in YYMMDD format
  $todayYYMMDD = GetTodayYYMMDD();

  // if the referring web page has expired then
  if ($expirydateYYMMDD <= $todayYYMMDD) {

    // if the page is an LSBU WWW or MYWEB page then
    if (((substr($url, 0, 21) == "http://www.lsbu.ac.uk"))
        or ((substr($url, 0, 23) == "http://myweb.lsbu.ac.uk"))) {

      // normalise the page url
      $url = TruncateURL($url);
    
      // add page to expirydataLIVE, avoiding duplicates
      $db = connect_to_db();
      $sql = "SELECT * FROM expirydataLIVE WHERE url=\"$url\"";
      $result = mysql_query($sql, $db);
      if (!$result) die("Query Failed.");
      if (mysql_num_rows($result) == 0) {
        $sql = "INSERT INTO expirydataLIVE SET url=\"$url\", ";
        $sql .= "expirydate=\"$expirydate\", owner=\"$owner\", ";
        $sql .= "message=\"$message\"";
        $result = mysql_query($sql, $db);
        if (!$result) die("Query Failed.");
      }

    }   // endif (the page was an LSBU WWW or MYWEB page)

  }   // endif (the page had expired)
  
} else {

  // if today is a Sunday and it's after 1am then
  if (($day_of_week == "Sun") && (date("G") >= 1)) {
      
    // if this is the first hit of the day
    $db = connect_to_db();
    $sql = "SHOW COLUMNS FROM expirydataTEMP";
    $result = mysql_query($sql, $db);
    if ($result == FALSE) {

      // copy expirydataLIVE to expirydataTEMP
      $sql = "CREATE TABLE expirydataTEMP SELECT ";
      $sql .= "* FROM expirydataLIVE";
      $result = mysql_query($sql, $db);
    
      // for each expired page recorded in expirydataTEMP
      $sql = "SELECT * FROM expirydataTEMP";
      $result = mysql_query($sql, $db);
      while ($expired_page = mysql_fetch_row($result)) {

        // read contents of web page into a string
        $url = trim($expired_page[1]);
        $handle = fopen($url, "r");
        while (!feof($handle)) {
          $contents .= fread($handle, 8192);
        }

        // search for the page expiry date
        $explode = explode('&', $contents);
        $count = count($explode);
        $page_expiry_date = "";
        for($i=0; $i<$count; $i++){
          $string = trim($explode[$i]);
          if (substr($string, 0, 11) == "expirydate=") {
            $page_expiry_date .= substr($string, 12);
          }
        }

        // use ConvertDateToYYMMDD to re-format date
        $expirydateYYMMDD = ConvertDateToYYMMDD($page_expiry_date);

        // get today's date in YYMMDD format
        $todayYYMMDD = GetTodayYYMMDD();

        // if the referring web page has NOT expired then
        if ($expirydateYYMMDD >= $todayYYMMDD) {

          // delete page entry in expirydataLIVE
          $sql = "DELETE FROM expirydataLIVE WHERE url=\"$url\"";
          $result = mysql_query($sql, $db);

        }   // endif (the page had not expired)
      
      }   // endfor (each expired web page)
    
    }   // endif (this was the second or a subsequent hit)
  
  }   // endif (it was a Sunday and after 1am)

}   // endif (today was a Sunday or a Monday)

?>