<script language="php">
/******************************** IMPORTANT *******************************
If you edit this make sure there is no white space at the start of the file,
the first character *MUST* be the '<' in '<script lang...' above or this
stuff won't work!!!
***************************************************************************/
/**************************************************************************
countdown.phtml
Version 1.0
*** Note, some assembly required - i.e. it's up to you to write the code to
*** populate the database with valid entries - a pretty simple script do
*** the trick. Just slap a PHP (or whatever) front end on it and you're in
*** business.
This is a script to count how many times a particular file has been
accessed. It's also able to be a redirection counter if your site has
links you want to do click-through counts on. You see these quite a lot
on sites commercial sites and, of course, in banner ads.
The script uses a MySQL database and is capable of (optionally) supporting
multiple users. If you want to use the owner field you'll need another
table to link an owner to a record with their information in.
The table 'files' has the following format
uid int(10) unsigned auto_increment default 0 not null
owner int(10) unsigned default 0 not null
md5 char(32) default '' not null
file varchar(128) default '' not null
counter int(10) unsigned default 0 not null
tstamp timestamp(14)
uid is an auto-incrementing number and the primary key
owner, as mentioned above, is there if you want to allow multiple users
md5 is the md5 of a 'junk' string concatenated with the (padded) uid
file holds the *FULL* URL of the file to download / redirect to
counter records the number of times the calling link was clicked
tstamp shows the time the last click occurred
You call the script like this...
http://www.yourdomain.com/somedirectory/countdown.phtml?file=1234
The number at the end of the URL relates to the uid of the file to obtain.
The uid in the above example can be replaced by the two md5s or the file
field allowing flexibility in usage. Lookup happens on uid, md5 or file
depending on the setting chose below.
If you've not bothered about the user being able to easily work out the
real URL the requested file is coming from use file.
If you want to make things a little harder to figure out use the uid, this
isn't particularly hard to work around either as a user can just type in
the URL with different numbers which will (or won't) work. In the file case
the URL would end in ...down.phtml?file=http://.../file.tgz
If you want to make guessing where the file is located as obtuse as possible
then use the md5s. The md5 way gets around the guessing problem of the uid
method but does involve a *long* URL. A md5 is a 32 character hexadecimal
string so the URL would end in ...down.phtml?file=3A5B........9FEC
*** A quick note on generating md5s
Here's a quick little function you can use to fill in the md5 field, if you
don't use the md5 method for pickfield then you can just leave it blank.
function md5gen($uid)
{
$junk = "ksmntrks3"; // Change this
return md5($junk.sprintf("%010s",$uid));
}
Simon Booth (c) 2000
***************************************************************************/
// ********************* Start of configurable stuff **********************
// MySQL information - change to suit
$sql_host = "localhost";
$sql_user = "root";
$sql_pass = "";
$sql_data = "countdown";
$files = "files";
// Set this to 0 (zero) after you've run the script sucessfully at least
// once. The variable controls database and table creation if they don't
// exist when the script runs.
$docreate = 1;
// Contant definitions for pickfield, make pickfield MD5, UID or FILE
define("MD5", 1);
define("UID", 2);
define("FILE", 3);
// pickfield chooses which of uid, md5 or file are used to select the file
// to deliver to the end client. Default do UID (some people will hate MD5)
$pickfield = UID;
// ********************** End of configurable stuff ***********************
// Connect to MySQL
function db_connect()
{
global $sql_host, $sql_user, $sql_pass;
if(!($link = mysql_connect($sql_host, $sql_user, $sql_pass)))
{
echo "<H1>Can't connect to MySQL on line ".__LINE__."<br></H1>n";
echo "<H1>MySQL Error - ".mysql_error()."<br></H1>n";
die();
}
return $link;
}
// Select MySQL database
function db_select($database, $link)
{
if(!($db = mysql_select_db($database, $link)))
{
echo "<H1>Can't select database $database on line ".__LINE__."<br></H1>n";
echo "<H1>MySQL Error - ".mysql_error()."<br></H1>n";
mysql_close($link);
die();
}
}
// Check whether <database> exists
function db_exists($database, $link)
{
$rval = 0;
if($dbs = mysql_list_dbs($link))
{
$i = 0;
while ($i < mysql_num_rows ($dbs))
{
if($database == mysql_tablename ($dbs, $i))
{
$rval = 1;
break;
}
$i++;
}
mysql_free_result($dbs);
}
else
{
echo "<H1>Can't get list of databases on line ".__LINE__."<br></H1>n";
echo "<H1>MySQL Error - ".mysql_error()."<br></H1>n";
}
return $rval;
}
function db_table_exists($table, $database, $link)
{
$rval = 0;
$i = 0;
if($tbs = mysql_list_tables($database))
{
while ($i < mysql_num_rows ($tbs))
{
if($table == mysql_tablename ($tbs, $i))
{
$rval = 1;
break;
}
$i++;
}
mysql_free_result($tbs);
}
else
{
echo "<H1>Can't get list of tables in $database on line ".__LINE__."<br></H1>n";
echo "<H1>MySQL Error - ".mysql_error()."<br></H1>n";
}
return $rval;
}
// Create database if it doesn't exist
function countdown_db_create()
{
global $sql_data;
$link = db_connect();
if(!db_exists($sql_data, $link))
{
$db_query = "create database $sql_data";
$ret_val = mysql_query($db_query, $link);
if(!($ret_val))
{
echo "<H1>Can't create database $sql_data on line ".__LINE__."<br></H1>n";
echo "<H1>MySQL Error - ".mysql_error()."<br></H1>n";
mysql_close($link);
die();
}
}
mysql_close($link);
}
// Create table if it doesn't exist
function countdown_table_create()
{
global $sql_data, $files;
$tn = $files;
$tb = "create table $tn (";
$tb .= "uid int(10) unsigned auto_increment default 0 not null, ";
$tb .= "owner varchar(128) default '' not null, ";
$tb .= "md5 char(32) default '' not null, ";
$tb .= "file varchar(128) default '' not null, ";
$tb .= "counter int(10) unsigned default 0 not null, ";
$tb .= "tstamp timestamp(14), ";
$tb .= "primary key (uid), ";
$tb .= "key file (file), ";
$tb .= "key md5 (md5))";
$link = db_connect();
db_select($sql_data, $link);
for($i = 0; $i < sizeof($tn); $i++)
{
if(!db_table_exists($tn, $sql_data, $link))
{
$db_query = $tb;
$ret_val = mysql_query($db_query, $link);
if(!($ret_val))
{
echo "<H1>Can't create table ".$tn." on database $sql_data on line ".__LINE__."<br></H1>n";
echo "<H1>MySQL Error - ".mysql_error()."<br></H1>n";
mysql_close($link);
die();
}
}
}
mysql_close($link);
}
// Set everything up (also create database & tables if required)
function countdown_setup()
{
countdown_db_create();
countdown_table_create();
}
// Main entry point
function countdown($file)
{
global $sql_data, $files, $docreate, $pickfield;
$gowhere = "";
if($docreate)
{
countdown_setup();
}
switch($pickfield)
{
case MD5:
$db_query = "select * from $files where md5 = '$file'";
break;
case UID:
$db_query = "select * from $files where uid = $file";
break;
case FILE:
$db_query = "select * from $files where file = '$file'";
break;
default:
echo "<H1>Script error when file = $file<H1>n";
exit;
}
$link = db_connect();
db_select($sql_data, $link);
$ret_val = mysql_query($db_query, $link);
if($ret_val)
{
if($row = mysql_fetch_object($ret_val))
{
$db_query = "replace into $files values($row->uid, $row->owner, $row->md5, '$row->file', ".($row->counter + 1).", NULL)";
$ret_val = mysql_query($db_query, $link);
if($ret_val)
{
$gowhere = $row->file;
}
else
{
echo "<H1>Unable to update counter</H1>n";
echo "<br>$db_query<br>n";
echo mysql_error()."<br>n";
mysql_close($link);
exit;
}
}
else
{
echo "<H1>Unable to find $file</H1>n";
mysql_close($link);
exit;
}
mysql_close($link);
}
else
{
echo "<H1>Database error</H1>n";
mysql_close($link);
exit;
}
header("Location: $gowhere");
exit;
}
// Check if 'file' has been passed to the script, if so increment counter
// and redirect to the requested file so it gets downloaded.
if(isset($file))
{
countdown($file);
}
else
{
echo "<H1>Nothing requested - Nothing to do</H1><br>n";
}
</script>
|