The easy part
Here's the code for
expirychecker.php4 - which consists almost solely of reused bits from
version 2.4. I decided to write the url, owner and expirydate of any expired page to the file, rather than just the url as I had previously intended; this is because I've thought of a better (?) design for the
expiredpagehandler.php4 script - see
next blog post.
expirychecker.php4
<?php
/*
// WEB PAGE EXPIRY CHECKER - version 3.1.1
//
// script name = "expirychecker.php4"
//
// A script to record the url, owner and expirydate of any expired
// LSBU web page that invokes this script correctly.
//
// See also these accompanying scripts:
// (1) expiredpagehandler.php4
// (2) 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!
*/
/*
// 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 please bear this in mind when deciding where to postion it.
*/
//----------------------------------------------------------------
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);
}
}
//----------------------------------------------------------------
function AppendExpiryDataToFile($theURL, $theOwner, $theExpiryDate,
$theMessage, $theFile) {
// open and lock the expiry data file
$theFile_fp = fopen($theFile, "a");
$lock = flock($theFile_fp, 2);
// continue when lock is obtained
if ($lock) {
if (fwrite($theFile_fp, "\n<expiredpage>") === FALSE) {
echo "Cannot write to ExpiryData.xml file";
exit;
}
fwrite($theFile_fp, "\n <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");
echo "Success, wrote ($theURL) to file ExpiryData.xml file";
}
// unlock and close ExpiryData file
$lock = flock($theFile_fp, 3);
fclose($theFile_fp);
}
//----------------------------------------------------------------
/*
// MAIN PROGRAM
*/
// ExpiryData.xml and ExpiryDataLastWeek.xml must exist initially
$file = "ExpiryData.xml";
// Discover url of referring web page
$url = $_ENV['HTTP_REFERER'];
// Get $owner, $expirydate and $message from referring web page
$owner = $_GET['owner'];
$expirydate = $_GET['expirydate'];
$message = $_GET['message'];
// Use ConvertDateToYYMMDD to re-format $expirydate
$expirydateYYMMDD = ConvertDateToYYMMDD($expirydate);
// Get today's date in YYMMDD format
$todayYYMMDD = GetTodayYYMMDD();
// If the referring page has expired then
if ($expirydateYYMMDD <= $todayYYMMDD) {
// If the referring page is a valid 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"))) {
// Truncate $url if it ends with a "?", and subsequently with
// "/index.html", "/index.shtml" or "/", to avoid duplicate
// entries for the same web page
$url = TruncateURL($url);
// Append page data to ExpiryData.xml and retrieve $filedate
$filedate = AppendExpiryDataToFile($url, $owner, $expirydate,
$message, $file);
}
}
?>
Given an initialised
ExpiryData.xml file that looks like this...
<?xml version="1.0"?>
<expiredpages>
<date>20/12/05</date>
...the script appends entries to it like this, leaving out the last "
</expiredpages>":
<?xml version="1.0"?>
<expiredpages>
<date>20/12/05</date>
<expiredpage>
<url>http://myweb.lsbu.ac.uk/~bushm/index2.html</url>
<owner>mickey.mouse@lsbu.ac.uk</owner>
<expired>14/12/05</expired>
<message>testing</message>
</expiredpage>
<expiredpage>
<url>http://myweb.lsbu.ac.uk/~bushm/index2.html</url>
<owner>mickey.mouse@lsbu.ac.uk</owner>
<expired>14/12/05</expired>
<message>testing</message>
</expiredpage>