First prototype


12/8/02
 

First prototype

Here's my first attempt at converting my pseudocode into PHP:
<?php

// "Expiry Checker"

// A script to automatically remind FaCES web page owners...
// ...when their web pages are older than their expiry dates.

// author = Martin Bush, South Bank University
// date = 7th August 2002
// filename = expirychecker.php3
// version = first prototype
// history = -

// Comments in quotations are inherited from the pseudocode

// "read Expiry Checker information (url&expiry&owner&message)"
// "parse the information using the parse_str function"
parse_str($QUERY_STRING);  // defines $url, $expiry, $owner, $message

// "if (expiry_date <= today's date) then"
$today = date ("d/m/y")
if ( $today <= $expiry ) {

  // "lock file reminders_sent_today.txt" -- abbreviate to "reminders"
  $reminders_file = "reminders_sent_today.txt";  //file is assumed to exist
  // need to open reminders file first -- "r+" means for reading and writing
  $reminders_file_pointer = fopen($reminders_file, "r+");
  // now lock reminders file to ensure mutually exclusive access
  $lock = flock($reminders_file_pointer, 2); // "2" for exclusive writing lock
  // continue when lock is obtained
  if ($lock) {

    // "read date_of_file from file" -- assumed to be present and valid
    $date_of_file = fgets($reminders_file_pointer, 8); // reads up to 8 chars

    // "if (date_of_file < today's date) then"
    if ( $date_of_file < $today ) {

      // "rewind file"
      rewind($reminders_file_pointer);

      // "write today's date to file"
      fwrite($reminders_file_pointer, "$today\n");

    }  // "endif"

    // "search file for url of web page" -- stored in alphabetical order
    $found = 0;
    //  get next entry if there is one
    if (!feof($reminders_file_pointer)) {
      $next_entry = fgets($reminders_file_pointer, 256);
      // "url" in web page corresponds to "filed_url" in reminders file...
      //  ...and likewise for "expiry", "owner" and "message"
      parse_str($next_entry);  // defines $filed_url, filed_expiry, etc.
      // while loop stops if it goes past the point where $url should be
      while (!feof($reminders_file_pointer) & (($filed_url <= $url)
                                                      & ($found == 0))) {
        if ($filed_url == $url) {
          $found = 1;
        } else {
          // accumulate entries read so far, and get next entry
          $file_contents_so_far = $file_contents_so_far.$next_entry;
          $next_entry = fgets($reminders_file_pointer, 256);
        }
      }
    }

    // "if (URL not found in file) then"
    if ($found == 0) {

      // "write URL to file"
      // must carry on reading contents first
      $file_contents_remainder = fread($reminders_file_pointer,
                                       filesize($reminders_file));
      // now rewind the file
      rewind($reminders_file_pointer);
      // write accumulate entries up to point of insertion
      fwrite($reminders_file_pointer, "$file_contents_so_far\n");
      // prepare new reminder entry
      $new_reminder = "filed-url=".$url."&filed-expiry=".$expiry.
               "&filed-owner=".$owner."&filed-message=".$message;
      // insert new reminder entry
      fwrite($reminders_file_pointer, "$new_reminder\n");
      // write remainder
      fwrite($reminders_file_pointer, "$file_contents_remainder");

      // "email URL and message to owner"
      $mail_message = "This message has been sent automatically,
      \nto remind you that the contents of this FaCES web page...
      \n    @url
      \nnow needs your attention.
      \n\nHere is the message that accompanied your email address...
      \n    @message";
      mail($owner, "Expiry Checker: FaCES web page needs your attention.",
      $mail_message)

    }  // "endif"

  }  // endif ($lock)
  // "unlock file reminders_sent_today.txt"
  $lock = flock($respondees_file_pointer, 3); // "3" means unlock
  // need to close respondees file
  fclose($respondees_file_pointer);

}  // "endif" ( $today <= $expiry )

?>
 
 
 

<<contents ^top^ next>>