Testing EmailExpiryMessages


14/10/04
 

Testing EmailExpiryMessages

I'm going to test it using echo statements instead of sending emails...
 
<?php

//================================================================
// This section based on: http://jp2.php.net/xml_parse_into_struct
//----------------------------------------------------------------
class ExpiredPage {
  var $url;
  var $owner;
  var $expired;
  var $message;
  function ExpiredPage ($aa) {
    foreach ($aa as $k=>$v)
      $this->$k = $aa[$k];
  }
}
//----------------------------------------------------------------
function readDatabase($file) {
  // read the xml database of ExpiredPages
  $data = implode("",file($file));
  $parser = xml_parser_create();
  xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
  xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
  xml_parse_into_struct($parser,$data,$values,$tags);
  xml_parser_free($parser);
  // loop through the structures
  foreach ($tags as $key=>$val) {
    if ($key == "expiredpage") {
      $molranges = $val;
      // each contiguous pair of array entries are the 
      // lower and upper range for each expiredpage definition
      for ($i=0; $i < count($molranges); $i+=2) {
        $offset = $molranges[$i] + 1;
        $len = $molranges[$i + 1] - $offset;
        $tdb[] = parseMol(array_slice($values, $offset, $len));
      }
    } else {
      continue;
    }
  }
  return $tdb;
}
//----------------------------------------------------------------
function parseMol($mvalues) {
  for ($i=0; $i < count($mvalues); $i++)
    $mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
  return new ExpiredPage($mol);
}
//================================================================

function EmailExpiryMessages($FileName) {

  // open and lock the expiry data file
  $theFile_fp = fopen($FileName, "r+");
  $lock = flock($theFile_fp, 2);

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

    $db = readDatabase($FileName);

    // Pop each ExpiredPage object and send the email
    while ($poppedExpiredPage = (array_pop($db))):

      $pageURL = $poppedExpiredPage->url;
      $pageOwner = $poppedExpiredPage->owner;
      $pageExpiryDate = $poppedExpiredPage->expired;
      $pageMessage = $poppedExpiredPage->message;

// Prepare the email
$mail_message = "This is to remind you that this web page...
\n  $pageURL
\n...expired on $pageExpiryDate. Here is the reminder message (if any):
\n*** $pageMessage ***
\nYou will receive a reminder each day that this page is hit. 
Please update the page as necessary, and remember to specify a new 
expiry date."; 

    // Now [send the email] echo the email contents

echo "<html><head><title></title></head><body><pre>";
echo "\n\n  pageOwner = ";
echo $pageOwner;
echo "\n  pageURL = ";
echo $pageURL;
echo "\n  mail_message = ";
echo $mail_message;
echo "</pre></body></html>";

    endwhile;

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

}



/*
// MAIN PROGRAM
*/

$file = "ExpiryData.xml";

EmailExpiryMessages($file);

?>

 
I'm relieved to say that it works!  

 
 

<<contents ^top^