<?php
//
// class.wwwcheckup.php
// version 1.1.0 - 22nd August, 2003
//
// Description
//
// This class allows you to keep track of when websites were last updated.
// This will not tell you what the changes are, but just let you know that
// it has been changed in some way.
//
// Author
//
// Andrew Collington, 2003
// hide@address.com, http://php.amnuts.com/
//
// Feedback
//
// http://php.amnuts.com/forums/index.php
// There is message board at the above URL where you can post
// feedback, bug reports, feature requests, etc., for this and
// any of the other scripts located at php.amnuts.com.
//
// DB Schema
//
// Website addresses are stored in a database with the schema:
//
// CREATE TABLE website (
// id int(11) DEFAULT '0' NOT NULL auto_increment,
// website varchar(255) NOT NULL,
// hash varchar(255) NOT NULL,
// lastchecked datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
// lastupdated datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
// PRIMARY KEY (id)
// );
//
// This script would be most efficiently used in conjunction with a cron job
// to automatically check the status of the pages, so that all you have to
// do is show the results.
//
class WebsiteCheckup
{
var $user;
var $pass;
var $host;
var $id;
var $db;
var $tbl;
var $autoupdate;
var $results;
function WebsiteCheckup($user='username', $pass='password', $table='websites', $database='demo', $host='localhost')
{
$this->user = $user;
$this->pass = $pass;
$this->host = $host;
$this->db = $database;
$this->tbl = $table;
$this->id = NULL;
$this->autoupdate = TRUE;
$this->id = @mysql_connect($this->host, $this->user, $this->pass) or
die("Unable to connect to mysql server: {$this->host}");
@mysql_select_db($this->db, $this->id) or
die("Unable to select database: {$this->db}");
}
function SetAutoUpdate($status = TRUE)
{
$this->autoupdate = ($status === TRUE ? TRUE : FALSE);
}
//
// Database interaction routines
//
function CheckAll()
{
$sql = "SELECT id,website,hash,UNIX_TIMESTAMP(lastchecked) AS lastchecked,UNIX_TIMESTAMP(lastupdated) AS lastupdated FROM {$this->tbl}";
$result = @mysql_query($sql, $this->id) or die("Unable to perform query: $sql");
while ($row = @mysql_fetch_array($result, MYSQL_ASSOC))
{
$site = array();
set_time_limit(120);
$site = @file($row['website']);
if (empty($site))
{
echo "<p>The website {$row['website']} could not be fetched.</p>\n";
}
else
{
$hash = md5(strip_tags(join('', $site)));
if ($this->autoupdate)
{
if ($hash != $row['hash'])
{
$this->results[] = $row;
$sql = "UPDATE {$this->tbl} SET lastchecked=NOW(),lastupdated=NOW(),hash='{$hash}' WHERE id={$row['id']}";
}
else
{
$sql = "UPDATE {$this->tbl} SET lastchecked=NOW() WHERE id={$row['id']}";
}
@mysql_query($sql, $this->id) or die("Unable to perform query: $sql");
}
else
{
if ($hash != $row['hash'])
{
$this->results[] = $row;
}
}
}
}
@mysql_free_result($result);
}
function GetAll(&$arr)
{
$sql = "SELECT id,website,hash,UNIX_TIMESTAMP(lastchecked) AS lastchecked,UNIX_TIMESTAMP(lastupdated) AS lastupdated FROM {$this->tbl} ORDER BY website";
$result = @mysql_query($sql, $this->id) or die("Unable to perform query: $sql");
while ($row = @mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr[] = $row;
}
@mysql_free_result($result);
}
function AddWebsite($website = '')
{
if ($website == '')
{
return;
}
if (!preg_match('/^http:\/\//i', $website))
{
$website = "http://{$website}";
}
$site = array();
set_time_limit(120);
$site = @file($website);
if (empty($site))
{
return FALSE;
}
else
{
$hash = md5(strip_tags(join("", $site)));
$sql = "INSERT INTO {$this->tbl} (website,hash,lastupdated,lastchecked) VALUES('{$website}','{$hash}',NOW(),NOW())";
if (!@mysql_query($sql,$this->id))
{
die("Unable to perform query: $sql");
}
}
return TRUE;
}
function RemoveWebsite($website = '')
{
if ($website == '')
{
return FALSE;
}
if (!preg_match("/^http:\/\//i", $website))
{
$website = "http://{$website}";
}
if (@mysql_query("DELETE FROM {$this->tbl} WHERE website='{$website}'", $this->id))
{
return TRUE;
}
else
{
die("Unable to perform query: $sql");
}
}
//
// Display Routines
//
function DisplayUpdates()
{
$this->CheckAll();
$this->DisplayTable($this->results);
}
function DisplayAll()
{
$this->GetAll($foo);
$this->DisplayTable($foo, 1);
}
function DisplayTable($array, $colour = 0)
{
if (empty($array))
{
echo "<p>There are no websites or results available.</p>\n";
}
else
{
echo '<table border="1" cellpadding="3" cellspacing="0">', "\n";
echo '<tr><td>website</td><td>last checked</td><td>last updated</td></tr>', "\n";
for ($r=0; $r<count($array); $r++)
{
if ($colour && ($array[$r]['lastchecked'] == $array[$r]['lastupdated']))
{
$bg = " bgcolor=\"#CCCCCC\"";
}
else
{
$bg = " bgcolor=\"#FFFFFF\"";
}
echo "<tr><td{$bg}><a href=\"{$array[$r]['website']}\" target=\"_blank\">{$array[$r]['website']}</a></td>";
echo "<td{$bg}>", $this->FuzzyTime($array[$r]['lastchecked']), '</td>';
echo "<td{$bg}>", $this->FuzzyTime($array[$r]['lastupdated']), "</td></tr>\n";
}
echo "</table>\n";
}
}
function FuzzyTime($time)
{
// sod = start of day :)
$sod = mktime(0, 0, 0, date('m',$time), date('d', $time), date('Y', $time));
$sod_now = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
if ($sod_now == $sod) return "today at " . date("g:ia", $time); // check 'today'
if (($sod_now-$sod) <= 86400) return "yesterday at " . date("g:ia", $time); // check 'yesterday'
if (($sod_now-$sod) <= 432000) return date("l \a\\t g:ia", $time); // give a day name if within the last 5 days
if (date("Y",$now) == date("Y", $time)) return date("M j \a\\t g:ia", $time); // miss off the year if it's this year
return date("M j, Y \a\\t g:ia", $time); // return the date as normal
}
}
?>