The Full Monty!
In the completed (but not yet thoroughly tested!) Expiry Checker script below I've added some header comments as well as the last section of the code - which is to be executed first thing on a Monday morning. This part consists mainly of reused code from
version 2.4.
<?php
/*
// WEB PAGE EXPIRY CHECKER - version 3.1
//
// A script to automatically email web page content owners
// once every Monday after their web pages have expired.
//
// [See also this accompanying script: "expirydataviewer.php4".]
//
//
// martin.bush@lsbu.ac.uk, August 2006.
//
//
// Permission is granted to re-use all or part of this software
// under the terms of the GNU General Public License as published
// by the Free Software Foundation [www.gnu.org/copyleft/gpl.html].
// The author would very much appreciate being informed about any
// re-use of this software . . . thanks!
//
// For an indication of how to install this Expiry Checker see:
// [http://www.mebush.net/public_html/martin/blogs/expirychecker/
// v3/prepare-database.html]
//
// Once installed, this script can be invoked from any LSBU WWW or
// MYWEB page by including the following snippet of HTML (with "???"
// corresponding to the appropriate path) anywhere within the body
// of the web page:
//
// <!-- Insert values below for "owner", "expirydate" -->
// <!-- and "message" to activate the Expiry Checker. -->
// <!-- Avoid quote marks '" within the message text. -->
// <img width="1" height="1" border="0" alt=""
// src="http://www.lsbu.ac.uk/php4-cgiwrap/???/expirychecker.php4?
// owner=email@ddress
// &
// expirydate=dd/mm/yy
// &
// message=a line of free text - no quotation marks please!
// ">
//
// If the value for the expiry date is left as the literal
// character string "dd/mm/yy" this will be ignored. The script
// will accept dd/mm/yy dates containing single digits - e.g.
// any of {09/09/04, 9/09/04, 09/9/04, 9/9/04} would be accepted.
//
// The HTML snippet will insert a one pixel dot into the web page;
// it'll be imperceptible using modern browsers, but a small dot
// will be noticeable when using either Netscape 4 or IE 4 on a
// Mac (at least), and it may even cause a blank line to appear,
// so bear this in mind when deciding where to position it.
*/
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
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 { // today is a Sunday or a Monday
// if today is a Sunday and it's after 1:00am 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");
$contents = " ";
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
// search for the page expiry date
$explode = explode('&', $contents);
$count = count($explode);
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 the date
$expirydateYYMMDD = ConvertDateToYYMMDD($page_expiry_date);
// get today's date in YYMMDD format
$todayYYMMDD = GetTodayYYMMDD();
// if the web page has NOT expired or no longer exists then
if (($expirydateYYMMDD >= $todayYYMMDD) OR
($count <= 1)) {
// 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)
} else { // it's not a Sunday after 1:00am
// if today is a Monday then
if ($day_of_week == "Mon") {
// 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) {
// for each expired page recorded in expirydataLIVE
$sql = "SELECT * FROM expirydataLIVE";
$result = mysql_query($sql, $db);
while ($expired_page = mysql_fetch_row($result)) {
// prepare to send emails
$url = trim($expired_page[1]);
$expirydate = trim($expired_page[2]);
$owner = trim($expired_page[3]);
$message = trim($expired_page[4]);
// Prepare email message for page owner
$mail_message = "This is to remind you that the following web page --
\n$url
\n-- expired on $expirydate. Here is the reminder message (if any):
\n\n*** $message ***
\n\nYou will receive a reminder each Monday until the page is updated.
Please update the page as necessary if you can, or ask Martin Bush
(martin.bush@lsbu.ac.uk) or Mike Child (mike.child@lsbu.ac.uk) to
do so on your behalf. A new expiry date can then be set.";
// Email the page owner
mail($owner, "BCIM Expiry Checker: $url", $mail_message,
"From: bcim@lsbu.ac.uk");
// use ConvertDateToYYMMDD to re-format the date
$expirydateYYMMDD = ConvertDateToYYMMDD($expirydate);
// get today's date in YYMMDD format
$todayYYMMDD = GetTodayYYMMDD();
// if the page has expired over a month ago then
if ($todayYYMMDD - $expirydateYYMMDD >= 100) {
// Prepare email message for <bcim@lsbu.ac.uk>
$mail_message = "This is an alert that the following web page --
\n$url
\n-- expired on $expirydate, which is over a month ago.
\nThe page owner is: $owner
\nHere is the reminder message (if any):
\n*** $message ***
\n\nPlease investigate!";
// Email <bcim@lsbu.ac.uk>
mail("bcim@lsbu.ac.uk", "BCIM Expiry Checker ALERT: $url",
$mail_message, "From: bcim@lsbu.ac.uk");
} // endif (the page had expired over a month ago)
} // endfor (each expired web page)
// delete the expirydataLASTWEEK table
$sql = "DROP TABLE expirydataLASTWEEK";
$result = mysql_query($sql, $db);
// copy expirydataTEMP to expirydataLASTWEEK
$sql = "CREATE TABLE expirydataLASTWEEK SELECT ";
$sql .= "* FROM expirydataTEMP";
$result = mysql_query($sql, $db);
// delete the expirydataTEMP table
$sql = "DROP TABLE expirydataTEMP";
$result = mysql_query($sql, $db);
} // endif (this was the second or a subsequent hit)
} // endif (today was a Monday)
} // endif (today wasn't a Sunday after 1:00am)
} // endif (today was a Sunday or a Monday)
?>